<?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.TTrainManageMapper">
    
    <resultMap type="TTrainManage" id="TTrainManageResult">
        <result property="trainManageId"    column="train_manage_id"    />
        <result property="lessonName"    column="lesson_name"    />
        <result property="trainDuration"    column="train_duration"    />
        <result property="trainStartTime"    column="train_start_time"    />
        <result property="trainEndTime"    column="train_end_time"    />
        <result property="lessonContent"    column="lesson_content"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="annexUrl"    column="annex_url"    />
        <result property="trainStatus"    column="train_status"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="isDel"    column="is_del"    />
        <result property="isFinish" column="is_finish" />
    </resultMap>

    <sql id="selectTTrainManageVo">
        select train_manage_id, lesson_name, train_duration, train_start_time, train_end_time, lesson_content, video_url, annex_url, train_status, publish_time, create_time, update_time, remark, is_del from t_train_manage
    </sql>

    <select id="selectTTrainManageList" parameterType="TTrainManage" resultMap="TTrainManageResult">
        <include refid="selectTTrainManageVo"/>
        <where>  
            <if test="lessonName != null  and lessonName != ''"> and lesson_name like concat('%', #{lessonName}, '%')</if>
            <if test="trainDuration != null "> and train_duration = #{trainDuration}</if>
            <if test="trainStartTime != null "> and train_start_time = #{trainStartTime}</if>
            <if test="trainEndTime != null "> and train_end_time = #{trainEndTime}</if>
            <if test="lessonContent != null  and lessonContent != ''"> and lesson_content = #{lessonContent}</if>
            <if test="videoUrl != null  and videoUrl != ''"> and video_url = #{videoUrl}</if>
            <if test="annexUrl != null  and annexUrl != ''"> and annex_url = #{annexUrl}</if>
            <if test="trainStatus != null  and trainStatus != ''"> and train_status = #{trainStatus}</if>
            <if test="publishTime != null "> and publish_time = #{publishTime}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTTrainManageById" parameterType="Long" resultMap="TTrainManageResult">
        <include refid="selectTTrainManageVo"/>
        where train_manage_id = #{trainManageId}
    </select>
        
    <insert id="insertTTrainManage" parameterType="TTrainManage" useGeneratedKeys="true" keyProperty="trainManageId">
        insert into t_train_manage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lessonName != null">lesson_name,</if>
            <if test="trainDuration != null">train_duration,</if>
            <if test="trainStartTime != null">train_start_time,</if>
            <if test="trainEndTime != null">train_end_time,</if>
            <if test="lessonContent != null">lesson_content,</if>
            <if test="videoUrl != null">video_url,</if>
            <if test="annexUrl != null">annex_url,</if>
            <if test="trainStatus != null">train_status,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lessonName != null">#{lessonName},</if>
            <if test="trainDuration != null">#{trainDuration},</if>
            <if test="trainStartTime != null">#{trainStartTime},</if>
            <if test="trainEndTime != null">#{trainEndTime},</if>
            <if test="lessonContent != null">#{lessonContent},</if>
            <if test="videoUrl != null">#{videoUrl},</if>
            <if test="annexUrl != null">#{annexUrl},</if>
            <if test="trainStatus != null">#{trainStatus},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTTrainManage" parameterType="TTrainManage">
        update t_train_manage
        <trim prefix="SET" suffixOverrides=",">
            <if test="lessonName != null">lesson_name = #{lessonName},</if>
            <if test="trainDuration != null">train_duration = #{trainDuration},</if>
            <if test="trainStartTime != null">train_start_time = #{trainStartTime},</if>
            <if test="trainEndTime != null">train_end_time = #{trainEndTime},</if>
            <if test="lessonContent != null">lesson_content = #{lessonContent},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="annexUrl != null">annex_url = #{annexUrl},</if>
            <if test="trainStatus != null">train_status = #{trainStatus},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where train_manage_id = #{trainManageId}
    </update>

    <delete id="deleteTTrainManageById" parameterType="Long">
        delete from t_train_manage where train_manage_id = #{trainManageId}
    </delete>

    <delete id="deleteTTrainManageByIds" parameterType="String">
        delete from t_train_manage where train_manage_id in 
        <foreach item="trainManageId" collection="array" open="(" separator="," close=")">
            #{trainManageId}
        </foreach>
    </delete>

    <select id="getTrainInfoByPersonId" parameterType="Long" resultMap="TTrainManageResult">
        SELECT
            manage.train_manage_id,
            manage.lesson_name,
            manage.train_duration,
            manage.train_start_time,
            manage.train_end_time,
            manage.lesson_content,
            manage.video_url,
            manage.annex_url,
            manage.train_status,
            manage.publish_time,
            CASE WHEN manage.train_start_time >NOW() THEN '3'
            WHEN now() > manage.train_end_time THEN '2'
            ELSE person.is_finish END AS is_finish
        FROM
            t_train_manage manage
        LEFT JOIN t_train_person person ON person.train_manage_id = manage.train_manage_id
        <where>
            manage.train_status = '1'
            AND person.train_person_id = #{trainPersonId}
        </where>

    </select>
</mapper>