<?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.TtTrainMapper">
    
    <resultMap type="com.ruoyi.project.business.domain.vo.train.TtTrainVo" id="TtTrainResult">
        <result property="id"    column="id"    />
        <result property="trainName"    column="train_name"    />
        <result property="trainType"    column="train_type"    />
        <result property="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="assessFlag"    column="assess_flag"    />
        <result property="trainStatus"    column="train_status"    />
        <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"    />
        <result property="createByName"    column="nick_name"    />
        <result property="trainingCompletionRate"    column="train_complete_rate"    />
        <result property="assessmentCompletionRate"    column="assess_complete_rate"    />
    </resultMap>

    <sql id="selectTtTrainVo">
        select t.*,
               u.nick_name,
               count(*),
               IFNULL(ROUND(SUM(tt.train_complete_flag = '1') / COUNT(*) * 100, 2), 0) AS train_complete_rate,
               IFNULL(ROUND(SUM(tt.assess_complete_flag != '0') / COUNT(*) * 100, 2), 0) AS assess_complete_rate
        from tt_train t
            left join sys_user u on t.create_by = u.user_id and u.del_flag != '1'
            LEFT JOIN tt_train_target tt ON t.id = tt.train_id and tt.del_flag = '0'
    </sql>

    <sql id="selectTtTrainVoNoJoin">
        select t.*,u.nick_name
        from tt_train t left join sys_user u on t.create_by = u.user_id and u.del_flag != '1'
    </sql>


    <select id="selectTtTrainList" parameterType="TtTrainDto" resultMap="TtTrainResult">
        <include refid="selectTtTrainVo"/>
        <where>
            t.del_flag != '1'
            <if test="trainName != null  and trainName != ''">and t.train_name like concat('%', #{trainName}, '%')</if>
            <if test="trainStatusList != null">
                and t.train_status in
                <foreach collection="trainStatusList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="trainTypeList != null">
                and t.train_type in
                <foreach collection="trainTypeList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startDateS != null and startDateE != null">and t.start_date between #{startDateS} and #{startDateE} </if>
            <if test="endDateS != null and endDateE != null">and t.end_date between #{endDateS} and #{endDateE}</if>
            <if test="assessFlag != null  and assessFlag != ''">and t.assess_flag = #{assessFlag}</if>
            <if test="createByName != null and createByName != ''">and u.nick_name like concat('%', #{createByName}, '%')</if>
            <if test="beginTime != null and endTime != null">and t.create_time between #{beginTime} and #{endTime}</if>
        </where>
        group by t.id
        having count(*) > 0
        <if test="trainingCompletionRate != null  and trainingCompletionRate != ''">and train_complete_rate = #{trainingCompletionRate}</if>
        <if test="assessmentCompletionRate != null  and assessmentCompletionRate != ''">and assess_complete_rate = #{assessmentCompletionRate}</if>
        order by t.train_status, t.update_time desc,t.id desc
    </select>
    
    <select id="selectTtTrainById" parameterType="Long" resultMap="TtTrainResult">
        <include refid="selectTtTrainVoNoJoin"/>
        where t.del_flag != '1' and id = #{id}
    </select>
        
    <insert id="insertTtTrain" parameterType="TtTrainDto" useGeneratedKeys="true" keyProperty="id">
        insert into tt_train
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainName != null">train_name,</if>
            <if test="trainType != null">train_type,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="assessFlag != null">assess_flag,</if>
            <if test="trainStatus != null">train_status,</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="trainName != null">#{trainName},</if>
            <if test="trainType != null">#{trainType},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="assessFlag != null">#{assessFlag},</if>
            <if test="trainStatus != null">#{trainStatus},</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>

    <update id="updateTtTrain" parameterType="TtTrain">
        update tt_train
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainName != null">train_name = #{trainName},</if>
            <if test="trainType != null">train_type = #{trainType},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="assessFlag != null">assess_flag = #{assessFlag},</if>
            <if test="trainStatus != null">train_status = #{trainStatus},</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="deleteTtTrainById" parameterType="Long">
        delete from tt_train where id = #{id}
    </delete>

    <update id="deleteTtTrainByIds" parameterType="String">
        update tt_train set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateBatchById" parameterType="java.util.List">
        UPDATE tt_train
        SET train_status =
        <foreach collection="list" item="train" open="case" close="end" >
            WHEN id = #{train.id} THEN #{train.trainStatus}
        </foreach>,
        update_time = CURRENT_TIMESTAMP
        WHERE id IN
        <foreach collection="list" item="train" open="(" close=")" separator="," >
            #{train.id}
        </foreach>
    </update>


    <select id="selectParticipantTrainListUserId"
            resultType="com.ruoyi.project.business.domain.vo.train.TrainParticipantRes">
        select t.id,
            t.train_name,
            t.train_type,
            t.start_date,
            t.end_date,
            t.assess_flag,
            t.train_status,
            t.create_time,
            t.create_by,
            u.nick_name as create_by_name,
            tt.assess_complete_flag,
            tt.train_complete_flag,
            t.remark,
            tt.total_score,
            tt.id as tt_target_id
        from tt_train t
            join tt_train_target tt on t.id = tt.train_id and tt.del_flag != '1'
            left join sys_user u on t.create_by = u.user_id and u.del_flag != '1'
        where tt.target_person_id = #{userId}
        <if test="id != null">
            and t.id = #{id}
        </if>
        <if test="trainName != null and trainName != ''">
            and t.train_name like concat('%', #{trainName}, '%')
        </if>
        <if test="startDateS != null and startDateE != null">
            and t.start_date between #{startDateS} and #{startDateE}
        </if>
        <if test="endDateS != null and endDateE != null">
            and t.end_date between #{endDateS} and #{endDateE}
        </if>
        <if test="trainTypes != null and trainTypes.size() > 0">
            and t.train_type in
            <foreach collection="trainTypes" item="trainType" open="(" separator="," close=")">
                #{trainType}
            </foreach>
        </if>
        <if test="trainStatuses != null and trainStatuses.size() > 0">
            and t.train_status in
            <foreach collection="trainStatuses" item="trainStatus" open="(" separator="," close=")">
                #{trainStatus}
            </foreach>
        </if>
        <if test="passFlags != null and passFlags.size() > 0">
            and tt.assess_complete_flag in
            <foreach collection="passFlags" item="passFlag" open="(" separator="," close=")">
                #{passFlag}
            </foreach>
        </if>
        <if test="trainCompleteFlags != null and trainCompleteFlags.size() > 0">
            and tt.train_complete_flag in
            <foreach collection="trainCompleteFlags" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by t.id desc
    </select>
</mapper>