<?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.yanzhu.exam.exam.mapper.ExamTaskPersonMapper">
    
     <resultMap type="com.yanzhu.exam.exam.domain.ExamTaskPerson" id="ExamTaskPersonResult">
        <result property="examCode"    column="exam_code"    />
        <result property="userCode"    column="user_code"    />
        <result property="taskCode"    column="task_code"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectExamTaskPersonVo">
        select exam_code, user_code, task_code, start_time, end_time, status from exam_task_person
    </sql>

    <select id="selectExamTaskPersonList" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson" resultMap="ExamTaskPersonResult">
        <include refid="selectExamTaskPersonVo"/>
        <where>  
         where exam_code = #{examCode}
        </where>
    </select>
    
    <select id="selectExamTaskPersonById" parameterType="String" resultMap="ExamTaskPersonResult">
        <include refid="selectExamTaskPersonVo"/>
        where exam_code = #{examCode}
    </select>

        
    <insert id="insertExamTaskPerson" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson" >
        insert into exam_task_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="examCode != null">exam_code,</if>
            <if test="userCode != null">user_code,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="examCode != null">#{examCode},</if>
            <if test="userCode != null">#{userCode},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>
    
    <insert id="batchInsertPerson" parameterType="String" >
            INSERT INTO exam_task_person (exam_code, task_code, user_code, STATUS)
        SELECT
            #{examCode}  AS exam_code,
            UUID()  AS task_code,
            user_code,
            1 AS STATUS
        FROM (
                 SELECT DISTINCT person_code AS user_code
                 FROM sys_group_person
                 WHERE group_code IN (
                     SELECT group_code
                     FROM exam_task_group
                     WHERE exam_code = #{examCode}
                 )
             ) AS subquery

    </insert>

    <select id="selectExamTaskPerson" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson" resultMap="ExamTaskPersonResult">
        <include refid="selectExamTaskPersonVo"/>
        where exam_code = #{examCode} and user_code = #{userCode}
    </select>
    
    <update id="updateExamTaskPerson" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson">
        update exam_task_person
        <trim prefix="SET" suffixOverrides=",">
            <if test="userCode != null">user_code = #{userCode},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where exam_code = #{examCode}
    </update>
    
   <update id="updateStartTime" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson">
        update exam_task_person set start_time = now()
        where exam_code = #{examCode} and user_code = #{userCode}
    </update>
    
    <update id="updateDoneStatus" parameterType="com.yanzhu.exam.exam.domain.ExamTaskPerson">
        update exam_task_person set end_time = now(), status = 0 
        where exam_code = #{examCode} and user_code = #{userCode}
    </update>

    <delete id="deleteExamTaskPersonById" parameterType="String">
        delete from exam_task_person where exam_code = #{examCode}
    </delete>

    <delete id="deleteExamTaskPersonByIds" parameterType="String">
        delete from exam_task_person where exam_code in 
        <foreach item="examCode" collection="array" open="(" separator="," close=")">
            #{examCode}
        </foreach>
    </delete>
</mapper>