<?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.ExamTaskManagerMapper">
    
    <resultMap type="com.yanzhu.exam.exam.domain.ExamTaskManager" id="ExamTaskManagerResult">
        <result property="examId"    column="exam_id"    />
        <result property="examCode"    column="exam_code"    />
        <result property="examName"    column="exam_name"    />
        <result property="examDescribe"    column="exam_describe"    />
        <result property="buildType"    column="build_type"    />
        <result property="forceDone"    column="force_done"    />
        <result property="examBank"    column="exam_bank"    />
        <result property="examType"    column="exam_type"    />
        <result property="examTimes"    column="exam_times"    />
        <result property="examBankText"    column="exam_bank_text"    />
        <result property="pictureUrl"    column="picture_url"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="examDuration"    column="exam_duration"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createDept"    column="create_dept"    />
        <result property="createTime"    column="create_time"    />        
    </resultMap>

    <sql id="selectExamTaskManagerVo">
         select exam_id, exam_code, exam_name,exam_type,exam_times, exam_describe, build_type, force_done, exam_bank, exam_bank_text, picture_url, start_time, end_time, exam_duration, status, create_by, create_dept, create_time from exam_task_manager
    </sql>

    <select id="selectExamTaskManagerList" parameterType="com.yanzhu.exam.exam.domain.ExamTaskManager" resultMap="ExamTaskManagerResult">
        <include refid="selectExamTaskManagerVo"/>
        <where>  
            <if test="examName != null  and examName != ''"> and exam_name  like concat('%', #{examName}, '%')</if>
            <if test="buildType != null  and buildType != ''"> and build_type = #{buildType}</if>
		    <if test="startDateText != null and startDateText != ''"><!-- 开始时间检索 -->
			    AND date_format(start_time,'%Y-%m-%d') &gt;= #{startDateText}
			</if>
			<if test="endDateText != null and endDateText != ''"><!-- 结束时间检索 -->
			    AND date_format(start_time,'%Y-%m-%d') &lt;= #{endDateText}
			</if>
            
        </where>
         order by create_time desc
    </select>
    
    <select id="selectExamTaskManagerById" parameterType="String" resultMap="ExamTaskManagerResult">
        <include refid="selectExamTaskManagerVo"/>
        where exam_code = #{examCode}
    </select>
        
    <insert id="insertExamTaskManager" parameterType="com.yanzhu.exam.exam.domain.ExamTaskManager" useGeneratedKeys="true" keyProperty="examId">
        insert into exam_task_manager
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="examCode != null and examCode != ''">exam_code,</if>
            <if test="examName != null and examName != ''">exam_name,</if>
            <if test="examDescribe != null">exam_describe,</if>
            <if test="buildType != null and buildType != ''">build_type,</if>
            <if test="examType != null and examType != ''">exam_type,</if>
            <if test="examTimes != null and examTimes != ''">exam_times,</if>
            <if test="forceDone != null">force_done,</if>
            <if test="examBank != null">exam_bank,</if>
            <if test="examBankText != null">exam_bank_text,</if>
            <if test="pictureUrl != null">picture_url,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="examDuration != null">exam_duration,</if>
            status,
            <if test="createBy != null">create_by,</if>
            <if test="createDept != null">create_dept,</if>
            create_time,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="examCode != null and examCode != ''">#{examCode},</if>
            <if test="examName != null and examName != ''">#{examName},</if>
            <if test="examDescribe != null">#{examDescribe},</if>
            <if test="buildType != null and buildType != ''">#{buildType},</if>
            <if test="examType != null and examType != ''">#{examType},</if>
            <if test="examTimes != null and examTimes != ''">#{examTimes},</if>
            <if test="forceDone != null">#{forceDone},</if>
            <if test="examBank != null">#{examBank},</if>
            <if test="examBankText != null">#{examBankText},</if>
            <if test="pictureUrl != null">#{pictureUrl},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="examDuration != null">#{examDuration},</if>
            0,
            <if test="createBy != null">#{createBy},</if>
            <if test="createDept != null">#{createDept},</if>
            now(),
         </trim>
    </insert>

    <update id="updateExamTaskManager" parameterType="com.yanzhu.exam.exam.domain.ExamTaskManager">
        update exam_task_manager
        <trim prefix="SET" suffixOverrides=",">
            <if test="examName != null and examName != ''">exam_name = #{examName},</if>
            <if test="examDescribe != null">exam_describe = #{examDescribe},</if>
            <if test="buildType != null and buildType != ''">build_type = #{buildType},</if>
            <if test="examType != null and examType != ''">exam_type = #{examType},</if>
            <if test="examTimes != null and examTimes != ''">exam_times = #{examTimes},</if>
            <if test="forceDone != null">force_done = #{forceDone},</if>
            <if test="examBank != null">exam_bank = #{examBank},</if>
            <if test="examBankText != null">exam_bank_text = #{examBankText},</if>
            <if test="pictureUrl != null">picture_url = #{pictureUrl},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="examDuration != null">exam_duration = #{examDuration},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createDept != null">create_dept = #{createDept},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where exam_code = #{examCode}
    </update>

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

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

    <select id="selectCurrentExam" parameterType="String" resultMap="ExamTaskManagerResult">
        <include refid="selectExamTaskManagerVo"/>
        where status=0
        AND exam_code in(select exam_code from exam_task_person where user_code=#{userCode})
        and start_time &lt;=now() and end_time &gt; now()

    </select>
    <select id="countExamScore" parameterType="List" resultMap="ExamTaskManagerResult">
        select tq.exam_code,sum(qp.questions_score) exam_type
        from exam_task_questions tq ,exam_questions_property qp
        where tq.questions_code=qp.questions_code
          <if test="examCodeList != null and examCodeList.size() > 0">
          and tq.exam_code in
        <foreach item="examCode" collection="examCodeList" open="(" separator="," close=")">
            #{examCode}
        </foreach>
        GROUP BY tq.exam_code
        </if>
    </select>

    <select id="groupExamTypeCount" parameterType="List" resultMap="ExamTaskManagerResult">
        select tq.exam_code,qp.questions_type exam_id,count(1) exam_type
        from exam_task_questions tq ,exam_questions_property qp
        where tq.questions_code=qp.questions_code
        <if test="examCodeList != null and examCodeList.size() > 0">
        and tq.exam_code in
        <foreach item="examCode" collection="examCodeList" open="(" separator="," close=")">
            #{examCode}
        </foreach>
        GROUP BY tq.exam_code,qp.questions_type
        </if>
    </select>
</mapper>