<?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.TTrainPersonMapper">
    
    <resultMap type="TTrainPerson" id="TTrainPersonResult">
        <result property="id"    column="id"    />
        <result property="trainManageId"    column="train_manage_id"    />
        <result property="trainPersonId"    column="train_person_id"    />
        <result property="realityTrainDuration"    column="reality_train_duration"    />
        <result property="isFinish"    column="is_finish"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectTTrainPersonVo">
        SELECT
            person.id,
            person.train_manage_id,
            person.train_person_id,
            person.reality_train_duration,
            person.is_finish,
            person.create_time,
            person.update_time,
            person.remark,
            person.is_del,
            sysu.nick_name as trainPersonName
        FROM
            t_train_person person
        LEFT JOIN sys_user sysu on sysu.user_id = person.train_person_id
    </sql>

    <select id="selectTTrainPersonList" parameterType="TTrainPerson" resultMap="TTrainPersonResult">
        <include refid="selectTTrainPersonVo"/>
        <where>  
            <if test="trainManageId != null "> and person.train_manage_id = #{trainManageId}</if>
            <if test="trainPersonId != null "> and person.train_person_id = #{trainPersonId}</if>
            <if test="realityTrainDuration != null "> and person.reality_train_duration = #{realityTrainDuration}</if>
            <if test="isFinish != null  and isFinish != ''"> and person.is_finish = #{isFinish}</if>
            <if test="isDel != null  and isDel != ''"> and person.is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTTrainPersonById" parameterType="Long" resultMap="TTrainPersonResult">
        <include refid="selectTTrainPersonVo"/>
        where person.id = #{id}
    </select>
        
    <insert id="insertTTrainPerson" parameterType="TTrainPerson" useGeneratedKeys="true" keyProperty="id">
        insert into t_train_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainManageId != null">train_manage_id,</if>
            <if test="trainPersonId != null">train_person_id,</if>
            <if test="realityTrainDuration != null">reality_train_duration,</if>
            <if test="isFinish != null">is_finish,</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="trainManageId != null">#{trainManageId},</if>
            <if test="trainPersonId != null">#{trainPersonId},</if>
            <if test="realityTrainDuration != null">#{realityTrainDuration},</if>
            <if test="isFinish != null">#{isFinish},</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="updateTTrainPerson" parameterType="TTrainPerson">
        update t_train_person
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainManageId != null">train_manage_id = #{trainManageId},</if>
            <if test="trainPersonId != null">train_person_id = #{trainPersonId},</if>
            <if test="realityTrainDuration != null">reality_train_duration = #{realityTrainDuration},</if>
            <if test="isFinish != null">is_finish = #{isFinish},</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 id = #{id}
    </update>

    <delete id="deleteTTrainPersonById" parameterType="Long">
        delete from t_train_person where id = #{id}
    </delete>

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

    <insert id="batchInsertTTrainPerson">
        INSERT INTO t_train_person(train_manage_id,train_person_id,create_time) VALUES
        <foreach collection="tTrainPeople" separator="," item="item">
            (${trainManageId},${item.trainPersonId},sysdate())
        </foreach>
    </insert>

    <delete id="deleteTTrainPersonByTrainManageId" parameterType="Long">
        delete from t_train_person where train_manage_id = #{trainManageId}
    </delete>
</mapper>