<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zehong.system.mapper.TTrainCourseMapper">
    
    <resultMap type="TTrainCourse" id="TTrainCourseResult">
        <result property="courseId"    column="course_id"    />
        <result property="courseName"    column="course_name"    />
        <result property="courseType"    column="course_type"    />
        <result property="courseConent"    column="course_conent"    />
        <result property="status"    column="status"    />
        <result property="releaseTime"    column="release_time"    />
        <result property="enclosure"    column="enclosure"    />
        <result property="video"    column="video"    />
        <result property="qualifiedNum"    column="qualified_num"    />
        <result property="topicNum"    column="topic_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectTTrainCourseVo">
        select course_id, course_name, course_type, course_conent, status,release_time, enclosure, video, qualified_num, topic_num, create_time, create_user, is_del from t_train_course
    </sql>

    <select id="selectTTrainCourseList" parameterType="TTrainCourse" resultMap="TTrainCourseResult">
        <include refid="selectTTrainCourseVo"/>
        <where>  
            <if test="courseName != null  and courseName != ''"> and course_name like concat('%', #{courseName}, '%')</if>
            <if test="courseType != null "> and course_type = #{courseType}</if>
            <if test="courseConent != null  and courseConent != ''"> and course_conent = #{courseConent}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="enclosure != null  and enclosure != ''"> and enclosure = #{enclosure}</if>
            <if test="video != null  and video != ''"> and video = #{video}</if>
            <if test="qualifiedNum != null "> and qualified_num = #{qualifiedNum}</if>
            <if test="topicNum != null "> and topic_num = #{topicNum}</if>
            <if test="createUser != null  and createUser != ''"> and create_user = #{createUser}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTTrainCourseById" parameterType="Long" resultMap="TTrainCourseResult">
        <include refid="selectTTrainCourseVo"/>
        where course_id = #{courseId}
    </select>

    <insert id="insertTTrainCourse" parameterType="TTrainCourse" useGeneratedKeys="true" keyProperty="courseId">
        insert into t_train_course
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="courseName != null">course_name,</if>
            <if test="courseType != null">course_type,</if>
            <if test="courseConent != null">course_conent,</if>
            <if test="status != null">status,</if>
            <if test="releaseTime != null">release_time,</if>
            <if test="enclosure != null">enclosure,</if>
            <if test="video != null">video,</if>
            <if test="qualifiedNum != null">qualified_num,</if>
            <if test="topicNum != null">topic_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUser != null">create_user,</if>
            <if test="isDel != null">is_del,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="courseName != null">#{courseName},</if>
            <if test="courseType != null">#{courseType},</if>
            <if test="courseConent != null">#{courseConent},</if>
            <if test="status != null">#{status},</if>
            <if test="releaseTime != null">#{releaseTime},</if>
            <if test="enclosure != null">#{enclosure},</if>
            <if test="video != null">#{video},</if>
            <if test="qualifiedNum != null">#{qualifiedNum},</if>
            <if test="topicNum != null">#{topicNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="isDel != null">#{isDel},</if>
        </trim>
    </insert>

    <update id="updateTTrainCourse" parameterType="TTrainCourse">
        update t_train_course
        <trim prefix="SET" suffixOverrides=",">
            <if test="courseName != null">course_name = #{courseName},</if>
            <if test="courseType != null">course_type = #{courseType},</if>
            <if test="courseConent != null">course_conent = #{courseConent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="releaseTime != null">release_time = #{releaseTime},</if>
            <if test="enclosure != null">enclosure = #{enclosure},</if>
            <if test="video != null">video = #{video},</if>
            <if test="qualifiedNum != null">qualified_num = #{qualifiedNum},</if>
            <if test="topicNum != null">topic_num = #{topicNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where course_id = #{courseId}
    </update>

    <delete id="deleteTTrainCourseById" parameterType="Long">
        delete from t_train_course where course_id = #{courseId}
    </delete>

    <delete id="deleteTTrainCourseByIds" parameterType="String">
        delete from t_train_course where course_id in 
        <foreach item="courseId" collection="array" open="(" separator="," close=")">
            #{courseId}
        </foreach>
    </delete>
    <insert id="insertUserCourse">
        INSERT INTO t_train_user_course(user_id,course_id,create_time) VALUES
        <foreach collection="userIds" separator="," item="item">
            (#{item},#{courseId},NOW())
        </foreach>
    </insert>
    <select id="userCourseList" resultType="com.zehong.system.domain.vo.UserCourseVo">
        SELECT uc.user_course_id AS userCourseId,uc.state ,uc.examination_time AS examinationTime,
        uc.`examination_result` AS examinationResult,uc.`create_time` AS createTime,
        c.`course_name` AS courseName, c.`topic_num` AS topicNum,c.`release_time` AS releaseTime,
        p.`plan_name` AS courseType,c.course_id as courseId,c.qualified_num as qualifiedNum
        FROM t_train_user_course uc
        LEFT JOIN t_train_course c ON c.`course_id` = uc.`course_id`
        LEFT JOIN t_train_plan p ON p.`plan_id` = c.`course_type`
        WHERE uc.user_id = #{userId}
        <if test="type!=null and type == 1">
            and uc.state !=2
        </if>
        <if test="type!=null and type == 2">
            and uc.state = 2
        </if>
        <if test="type!=null and type == 3">
            and uc.state !=0
            order by uc.examination_time desc
        </if>
    </select>
</mapper>