<?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.ruoyi.project.business.mapper.TtTrainTargetMapper">
    
    <resultMap type="TtTrainTarget" id="TtTrainTargetResult">
        <result property="id"    column="id"    />
        <result property="trainId"    column="train_id"    />
        <result property="targetPersonId"    column="target_person_id"    />
        <result property="trainCompleteFlag"    column="train_complete_flag"    />
        <result property="assessCompleteFlag"    column="assess_complete_flag"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTtTrainTargetVo">
        select id, train_id, target_person_id, train_complete_flag, assess_complete_flag, del_flag, create_time, create_by, update_time, update_by, remark from tt_train_target
    </sql>

    <sql id="selectTtTrainTargetVoWithNickname">
        select t.*,u.nick_name,u.dept_id,a.agent_name
        from tt_train_target t left join sys_user u on t.target_person_id = u.user_id and u.del_flag != '1'
        left join tb_agent a on u.dept_id = a.agent_id and a.del_flag != '1'
    </sql>

    <select id="selectTtTrainTargetList" parameterType="TtTrainTarget" resultType="com.ruoyi.project.business.domain.vo.train.TtTrainTargetVo">
        <include refid="selectTtTrainTargetVoWithNickname"/>
        <where>
            t.del_flag != '1'
            <if test="trainId != null "> and t.train_id = #{trainId}</if>
            <if test="targetPersonId != null "> and t.target_person_id = #{targetPersonId}</if>
            <if test="trainCompleteFlag != null  and trainCompleteFlag != ''"> and t.train_complete_flag = #{trainCompleteFlag}</if>
            <if test="assessCompleteFlag != null  and assessCompleteFlag != ''"> and t.assess_complete_flag = #{assessCompleteFlag}</if>
        </where>
    </select>
    
    <select id="selectTtTrainTargetById" parameterType="Long" resultMap="TtTrainTargetResult">
        <include refid="selectTtTrainTargetVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTtTrainTarget" parameterType="TtTrainTarget" useGeneratedKeys="true" keyProperty="id">
        insert into tt_train_target
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainId != null">train_id,</if>
            <if test="targetPersonId != null">target_person_id,</if>
            <if test="trainCompleteFlag != null">train_complete_flag,</if>
            <if test="assessCompleteFlag != null">assess_complete_flag,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="trainId != null">#{trainId},</if>
            <if test="targetPersonId != null">#{targetPersonId},</if>
            <if test="trainCompleteFlag != null">#{trainCompleteFlag},</if>
            <if test="assessCompleteFlag != null">#{assessCompleteFlag},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <insert id="insertTtTrainTargetBatch" parameterType="TtTrainTarget" useGeneratedKeys="true" keyProperty="id">
        insert into tt_train_target
        (train_id,target_person_id,train_complete_flag,assess_complete_flag,
        del_flag,create_time,create_by,update_time,update_by,remark) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.trainId},#{item.targetPersonId},#{item.trainCompleteFlag},#{item.assessCompleteFlag},
                #{item.delFlag},#{item.createTime},#{item.createBy},#{item.updateTime},#{item.updateBy},#{item.remark})
        </foreach>
    </insert>

    <update id="updateTtTrainTarget" parameterType="TtTrainTarget">
        update tt_train_target
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainId != null">train_id = #{trainId},</if>
            <if test="targetPersonId != null">target_person_id = #{targetPersonId},</if>
            <if test="trainCompleteFlag != null">train_complete_flag = #{trainCompleteFlag},</if>
            <if test="assessCompleteFlag != null">assess_complete_flag = #{assessCompleteFlag},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTtTrainTargetById" parameterType="Long">
        delete from tt_train_target where id = #{id}
    </delete>

    <update id="deleteTtTrainTargetByIds" parameterType="String">
        update tt_train_target set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="deleteTtTrainTargetByTrainIds">
        update tt_train_target set del_flag = '1' where train_id in
        <foreach item="trainId" collection="array" open="(" separator="," close=")">
            #{trainId}
        </foreach>
    </update>
</mapper>