<?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.ynfy.buss.exam.exam.mapper.ExamMapper">
    <select id="detail" resultMap="examMap">
        SELECT
            e.id,
            e.title,
            e.open_type,
            e.start_time,
            e.end_time,
            e.total_score as exam_total_score,
            e.qualify_score,
            e.total_time,
            e.leave_on,
            e.total_leave_times,
            e.leave_time,
            p.id as exam_paper_id,
            e.limit_count,
            e.examiner,
            e.reviewer,
            e.reviewer_type,
            e.question_disorder,
            e.answer_disorder,
            e.image,
            e.exam_result_showtype,
            e.show_deadline,
            p.id as paper_id,
            p.title as paperName,
            p.total_score,
            p.join_type,
            p.create_by,
            p.create_time,
            p.question_count,
            p.has_subjective,
            <include refid="stateCondition"/>
        FROM exam e
        LEFT JOIN paper p on e.paper_id = p.id
        <where>
            <if test="id !='' and id !=null">
                and e.id = #{id}
            </if>
        </where>
    </select>

    <resultMap id="examMap" type="com.ynfy.buss.exam.exam.entity.Exam">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="open_type" property="openType" jdbcType="INTEGER"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="exam_total_score" property="totalScore" jdbcType="DOUBLE"/>
        <result column="qualify_score" property="qualifyScore" jdbcType="DOUBLE"/>
        <result column="total_time" property="totalTime" jdbcType="INTEGER"/>
        <result column="exam_paper_id" property="paperId" jdbcType="VARCHAR"/>
        <result column="limit_count" property="limitCount" jdbcType="INTEGER"/>
        <result column="examiner" property="examiner" jdbcType="VARCHAR"/>
        <result column="leave_on" property="leaveOn" jdbcType="BOOLEAN"/>
        <result column="total_leave_times" property="totalLeaveTimes" jdbcType="INTEGER"/>
        <result column="leave_time" property="leaveTime" jdbcType="INTEGER"/>
        <result column="question_disorder" property="questionDisorder" jdbcType="BOOLEAN"/>
        <result column="answer_disorder" property="answerDisorder" jdbcType="BOOLEAN"/>
        <result column="reviewer" property="reviewer" jdbcType="VARCHAR"/>
        <result column="reviewer_type" property="reviewerType" jdbcType="INTEGER"/>
        <result column="image" property="image" jdbcType="VARCHAR"/>
        <result column="exam_result_showtype" property="examResultShowtype" jdbcType="INTEGER"/>
        <result column="show_deadline" property="showDeadline" jdbcType="INTEGER"/>
        <result column="state" property="state" jdbcType="INTEGER"/>

        <association property="paper" javaType="com.ynfy.buss.exam.paper.entity.Paper">
            <id column="paper_id" property="id" jdbcType="VARCHAR"/>
            <result column="paperName" property="title" jdbcType="VARCHAR"/>
            <result column="total_score" property="totalScore" jdbcType="DOUBLE"/>
            <result column="join_type" property="joinType" jdbcType="INTEGER"/>
            <result column="create_by" property="createBy" jdbcType="VARCHAR"/>
            <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
            <result column="question_count" property="questionCount" jdbcType="INTEGER"/>
            <result column="has_subjective" property="hasSubjective" jdbcType="BOOLEAN"/>
        </association>
    </resultMap>

    <select id="selectExamList" resultType="com.ynfy.buss.exam.exam.entity.Exam">
        SELECT
        tmp1.*
        <choose>
            <when test="user!=null and user.id!=null and user.id!=''">
                ,
                tmp2.try_count,
                u.passed
            </when>
        </choose>
        FROM
        (
        SELECT
            *,
            <include refid="stateCondition"/>
        FROM exam
        WHERE
            open_type = 1
            <if test="user!=null and user.orgCode!=null and user.orgCode!=''">
                UNION
                SELECT
                *,
                <include refid="stateCondition"/>
                FROM exam
                WHERE
                open_type = 2
                AND examiner like concat('%', #{user.orgCode}, '%')
            </if>
            <if test="user!=null and user.username!=null and user.username!=''">
                UNION
                SELECT
                *,
                <include refid="stateCondition"/>
                FROM exam
                WHERE
                open_type = 3
                AND examiner like concat('%', #{user.username}, '%')
            </if>
        ) tmp1
        <choose>
            <when test="user!=null and user.id!=null and user.id!=''">
        LEFT JOIN (
            SELECT
                exam_id,
                COUNT(*) AS try_count
            FROM
                user_exam uer
            WHERE
                uer.user_id = #{user.id}
            GROUP BY
                exam_id
            ) tmp2
            ON tmp1.ID = tmp2.exam_id
            LEFT JOIN user_exam_result u on u.exam_id = tmp2.exam_id and u.user_id = #{user.id}
            </when>
        </choose>
        <where>
            <if test="exam.title!=null and exam.title!=''">
                AND tmp1.title like concat('%', #{exam.title}, '%')
            </if>
            <if test="exam.openType!=null">
                AND tmp1.open_type = #{exam.openType}
            </if>
            <if test="exam.state!=null">
                AND tmp1.state = #{exam.state}
            </if>
        </where>
    </select>

    <select id="listExamIng" resultType="String">
        SELECT id
        FROM exam
        WHERE start_time &lt; now()
          AND now() &lt; DATE_ADD(end_time, INTERVAL 6 HOUR)
    </select>

    <select id="listLatestExam" resultType="com.ynfy.buss.exam.exam.entity.Exam">
        SELECT
        e.*,
        IFNULL( update_time, create_time ) AS time_sort,
        <include refid="stateCondition"/>
        <choose>
            <when test="userId !=null and userId != ''">
                ,tmp.try_count, tmp.passed
            </when>
        </choose>
        FROM
        exam e
        <choose>
            <when test="userId !=null and userId != ''">
                LEFT JOIN (
                SELECT
                exam_id,
                try_count,
                passed
                FROM
                user_exam_result uer
                WHERE
                uer.user_id = #{userId}
                ) tmp ON e.id = tmp.exam_id
            </when>
        </choose>
        ORDER BY
        time_sort DESC
        <if test="limit !=null">
            LIMIT #{limit}
        </if>
    </select>
    <sql id="stateCondition">
        CASE
        WHEN NOW() &lt; START_TIME THEN 2
        WHEN NOW() &gt; end_TIME THEN 3
        WHEN NOW() &gt; START_TIME AND NOW() &lt; end_TIME THEN 0
        ELSE NULL
        END
        AS state
    </sql>
</mapper>