<?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="jnpf.cultivate.mapper.CultivateExamMapper">

    <!-- 批量更新老版本考试信息 -->
    <update id="updateOldExamBatch">
        UPDATE ftb_cultivate_exam e
        JOIN (
            SELECT * FROM (
                VALUES
                <foreach collection="examList" item="item" separator=",">
                    ROW(#{item.examId}, #{item.examTime}, #{item.drawMode}, #{item.drawDataBase}, #{item.needUpdate})
                </foreach>
            ) AS tmp(F_Id, F_ExamTime, F_DrawMode, F_DrawDataBase, F_NeedUpdate)
        ) v ON e.F_Id = v.F_Id
        SET
        e.F_DrawMode = v.F_DrawMode,
        e.F_ExamTime = v.F_ExamTime,
        e.F_DrawDataBase = v.F_DrawDataBase,
        e.F_NeedUpdate = v.F_NeedUpdate
    </update>
    <!-- 查询考试列表 -->
    <select id="selectListV2" resultType="jnpf.model.cultivate.v2.exam.vo.V2ExamListVo">
        select ce.F_Id id,
        ce.F_ExamId examId,
        ce.F_ExamName examName,
        ce.F_ExamLimitation examLimitation,
        ce.F_EndTime endTime,
        ce.F_ExamType `examType`,
        ce.F_Status `status`,
        ce.F_NeedUpdate needUpdate,
        ifnull(sum(dr.F_PickCount), 0) questionNum, ifnull(sum(dr.F_Score * dr.F_PickCount), 0) totalScore
        from ftb_cultivate_exam ce
        left join ftb_cultivate_exam_draw_rule dr on ce.F_Id = dr.F_ExamId
        where ce.F_EnabledMark = 1
        <if test="null != examType">
            and ce.F_ExamType = #{examType}
        </if>
        <if test="null != examName and '' != examName">
            and instr(ce.F_ExamName, #{examName}) > 0
        </if>
        <if test="null != needUpdate">
            and ce.F_NeedUpdate = #{needUpdate}
        </if>
        <if test="null != limitedDate and limitedDate == 0">
            and ce.F_StartTime is null and ce.F_EndTime is null
        </if>
        group by ce.F_Id, ce.F_ExamId, ce.F_ExamName, ce.F_ExamLimitation
        order by ce.F_CreatorTime desc
    </select>
    <!-- 查询受影响的抽题规则(删题后) -->
    <select id="getAffectedExamRule" resultType="jnpf.model.cultivate.v2.exam.vo.ExamDrawRuleVo">
        SELECT
            ce.F_Id AS examId,
            ce.F_DrawDataBase drawDataBase,
            dr.F_Id AS ruleId,
            dr.F_QuestionType,
            dr.F_PickCount
        FROM ftb_cultivate_exam ce
        INNER JOIN ftb_cultivate_exam_draw_rule dr ON ce.F_Id = dr.F_ExamId
        WHERE ce.F_EnabledMark = 1
        AND ce.F_Status IN (0,1)
        AND ce.F_ExamType = 0
        AND FIND_IN_SET(#{bankId}, ce.F_DrawDataBase)
        AND dr.F_QuestionType IN
        <foreach collection="typeList" item="type" open="(" separator="," close=")">
            #{type}
        </foreach>
    </select>
    <!-- 查询老版本考试列表 -->
    <select id="getOldExamList" resultType="jnpf.model.cultivate.resp.ExamPaperVo">
        select ce.F_Id examId, ce.F_PaperId paperId, cast(ce.F_ExamTime / 60 as UNSIGNED) examTime,
               tp.F_QuestionNumber questionNumber, tp.F_TotalScore totalScore, tp.F_Type drawMode, tp.F_RelationQuestionBankId drawDataBase,
               case tp.F_Type when 2 then 1 else tp.F_NeedUpdate end needUpdate,
               case ce.F_PassType when 1 then ce.F_PassMark else ROUND(tp.F_TotalScore * ce.F_PassMark / 100, 2) end passScore,
               case ce.F_ExcellentType when 1 then ce.F_ExcellentMark else ROUND(tp.F_TotalScore * ce.F_ExcellentMark / 100, 2) end excellentScore
        from ftb_cultivate_exam ce
        left join ftb_cultivate_test_paper tp on tp.F_Id = ce.F_PaperId
        where ce.F_EnabledMark = 1 and ce.F_PaperId is not null
        order by ce.F_CreatorTime desc
    </select>
    <!-- 查询我的考试列表[web] -->
    <select id="getMyExamWebList" resultType="jnpf.model.cultivate.v2.exam.vo.MyExamWebVo">
        select
            eu.F_Id userExamId,
            eu.F_ExamId examId,
            e.F_ExamName examName,
            eu.F_ExamTime examTime,
            eu.F_UserId userId,
            eu.F_Duration duration,
            eu.F_QuestionNumber questionNum,
            eu.F_TotalScore totalScore,
            eu.F_PassScore passScore,
            eu.F_Score score,
            DATE_FORMAT(eu.F_FinishTime, '%Y-%m-%d %H:%i:%s') submitTime,
            eu.F_Status myExamStatus,
            case when eu.F_VersionBatch = e.F_VersionBatch then 1 else 0 end currentVersion
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on e.F_Id = eu.F_ExamId
        where eu.F_EnabledMark = 1 and F_UserId = #{userId}
        order by eu.F_CreatorTime desc
    </select>
</mapper>