<?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.FtbCultivateExamUserMapper">
    <sql id="Base_Column_List">
        SELECT a.F_Id                     AS id,
               a.F_ExamId                 AS examId,
               a.F_ExamType               AS examType,
               a.F_UserId                 AS userId,
               a.F_Duration               AS duration,
               a.F_Score                  AS userTotleScore,
               a.F_Status                 AS STATUS,
               a.F_PaperId                AS paperId,
               a.F_StartTime              AS startTime,
               a.F_EndTime                AS endTime,
               a.F_Content                AS content,
               a.F_Batch                  AS batch,
               a.F_UserStartTime          AS userStartTime,
               a.F_FinishTime             AS finishTime,
               a.F_ExamSource             AS examSource,
               a.F_RelationCourseExamId   AS relationCourseExamId,
               a.F_RelationPositionExamId AS relationPositionExamId,
               a.F_RelationRankId         AS relationRankId,
               a.F_RelationPositionId     AS relationPositionId,
               a.F_VersionBatch           As versionBatch,
               a.F_CreatorTime            As creatorTime,
               a.F_LastModifyTime         AS lastModifyTime,
               fa.F_ExamName              AS examName,
               fa.F_Description           AS description,
               fa.F_Examlimitation        AS examlimitation,
               fa.F_ExcellentType         AS excellentType,
               fa.F_ExcellentMark         AS excellentMark,
               fa.F_PassType              AS passType,
               fa.F_PassMark              AS passMark,
               fa.F_ExamTime              AS examTime,
               b.F_Name                   AS paperName,
               b.F_QuestionNumber         AS questionNum,
               b.F_TotalScore             AS examTotleScore
        FROM ftb_cultivate_exam_user a
                 LEFT JOIN ftb_cultivate_exam fa ON a.F_ExamId = fa.F_Id
                 LEFT JOIN ftb_cultivate_test_paper b ON a.F_PaperId = b.F_Id
    </sql>


    <sql id="Base_Sql">
        SELECT count(*)
        FROM ftb_cultivate_exam_user a
    </sql>
    <!-- 批量更新旧版本用户考试 -->
    <update id="updateOldExamUserBatch">
        UPDATE ftb_cultivate_exam_user u
        JOIN (
            SELECT * FROM (
                VALUES
                <foreach collection="examList" item="item" separator=",">
                    ROW(#{item.examId}, #{item.examTime}, #{item.pickQuestionRule}, #{item.passScore}, #{item.excellentScore})
                </foreach>
            ) AS tmp(F_ExamId, F_ExamTime, F_PickQuestionRule, F_PassScore, F_ExcellentScore)
        ) v ON u.F_ExamId = v.F_ExamId
        SET
        u.F_ExamTime = v.F_ExamTime,
        u.F_PickQuestionRule = v.F_PickQuestionRule,
        u.F_PassScore = v.F_PassScore,
        u.F_ExcellentScore = v.F_ExcellentScore
    </update>
    <!--查询我的考试列表-->
    <select id="pagingQueryMyExamList" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        SELECT a.F_Id                     AS id,
        a.F_ExamId                 AS examId,
        a.F_ExamType               AS examType,
        a.F_UserId                 AS userId,
        a.F_Duration               AS duration,
        a.F_Score                  AS userTotleScore,
        a.F_Status                 AS STATUS,
        a.F_PaperId                AS paperId,
        a.F_StartTime              AS startTime,
        a.F_EndTime                AS endTime,
        a.F_Content                AS content,
        a.F_Batch                  AS batch,
        a.F_UserStartTime          AS userStartTime,
        a.F_FinishTime             AS finishTime,
        a.F_ExamSource             AS examSource,
        a.F_RelationCourseExamId   AS relationCourseExamId,
        a.F_RelationPositionExamId AS relationPositionExamId,
        a.F_RelationRankId         AS relationRankId,
        a.F_RelationPositionId     AS relationPositionId,
        a.F_VersionBatch           As versionBatch,
        b.F_Name                   AS paperName,
        b.F_QuestionNumber         AS questionNum,
        b.F_TotalScore             AS examTotleScore
        FROM ftb_cultivate_exam_user a
        LEFT JOIN ftb_cultivate_test_paper b ON a.F_PaperId = b.F_Id
        <where>
            a.F_EnabledMark = 1
            AND a.F_UserId =#{params.userId}
            And a.F_AppShow = 0

            <if test="params.keyword!= null and params.keyword!= '' and params.innerExamIds != null and params.innerExamIds.size() >0">
                and (
                a.F_ExamId in
                <foreach collection="params.innerExamIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
                or b.F_Name like CONCAT('%',#{params.keyword},'%')
                )
            </if>

            <if test="params.keyword!= null and params.keyword!= '' and params.innerExamIds.size() ==0">
                and  b.F_Name like CONCAT('%',#{params.keyword},'%')
            </if>

            <if test="params.examType!= null">
                and a.F_ExamType =#{params.examType}
            </if>
            order by a.F_Status asc
        </where>

    </select>
    <!--查询我的等待考试列表-->
    <select id="pagingQueryWaitMyExamList" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        <include refid="Base_Column_List"></include>
        <where>
            a.F_EnabledMark = 1
            AND a.F_Status = 1
            AND a.F_IsJoinCount = 0
            <if test="params.userId!= null and params.userId!= ''">
             AND a.F_ReviewerUserIds like CONCAT('%',#{params.userId},'%')
            </if>
            <if test="managerUserIds != null and managerUserIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="managerUserIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.keyword!= null and params.keyword!= ''">
                and (
                fa.F_ExamName like CONCAT('%',#{params.keyword},'%')
                or b.F_Name like CONCAT('%',#{params.keyword},'%')
                )
            </if>

            <if test="params.examType!= null">
                and a.F_ExamType =#{params.examType}
            </if>
        </where>
    </select>
    <!--查询我的完成考试列表-->
    <select id="pagingQueryMyCompleteExamList" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        <include refid="Base_Column_List"></include>
        <where>
            a.F_EnabledMark = 1
            AND a.F_Status in (3,4,5)
            AND a.F_ReviewerUserId=#{params.userId}
            <if test="params.keyword!= null and params.keyword!= ''">
                and (
                fa.F_ExamName like CONCAT('%',#{params.keyword},'%')
                or b.F_Name like CONCAT('%',#{params.keyword},'%')
                )
            </if>

            <if test="params.examType!= null">
                and a.F_ExamType =#{params.examType}
            </if>
            order by a.F_Status asc
        </where>
    </select>

    <!--查询过期的考试列表-->
    <select id="pagingQueryExpireExamList" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        <include refid="Base_Column_List"></include>
        <where>
            a.F_EnabledMark = 1
            and a.F_ExamType = 1
            and a.F_Status=2
            And a.F_AppShow = 0
            and  a.F_ReviewerUserIds like CONCAT('%',#{params.userId},'%')
            <if test="managerUserIds != null and managerUserIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="managerUserIds" item="item" open="("  separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.keyword!= null and params.keyword!= ''">
                and (
                fa.F_ExamName like CONCAT('%',#{params.keyword},'%')
                or b.F_Name like CONCAT('%',#{params.keyword},'%')
                )
            </if>
            order by a.F_EndTime asc
        </where>

    </select>
    <!--查询考试列表-->
    <select id="pagingQueryExamList" resultType="jnpf.model.cultivate.resp.PositionExamDto">
        SELECT a.F_Id             AS innerBuinessId,
               a.F_PostLearnId    AS postLearnId,
               a.examSource       as examSource,
               b.F_Id             AS examId,
               b.F_ExamName       AS examName,
               b.F_ExamType       AS examType,
               b.F_StartTime      AS startTime,
               b.F_EndTime        AS endTime,
               b.F_ExamTime       AS examTime,
               b.F_Examlimitation AS examlimitation,
               b.F_ExcellentType  AS excellentType,
               b.F_ExcellentMark  AS excellentMark,
               b.F_PassType       AS passType,
               b.F_PassMark       AS passMark,
               b.F_PaperId        AS paperId,
               b.F_Description    AS description,
               c.F_Name           AS paperName,
               c.F_QuestionNumber AS questionNumber,
               c.F_TotalScore     AS examTotleScore
        FROM (SELECT F_Id, F_PostRankId, F_ExamId, F_PostLearnId, F_EnabledMark, "2" as examSource
              FROM ftb_cultivate_position_exam
              where F_EnabledMark = 0
                AND F_PostRankId = #{params.rankId}
              UNION ALL
              SELECT F_Id, F_PostRankId, F_ExamId, F_PostLearnId, F_EnabledMark, "1" as examSource
              FROM ftb_cultivate_position_course_exam
              where F_EnabledMark = 0
                AND F_PostRankId = #{params.rankId}) a
                 JOIN ftb_cultivate_exam b ON a.F_ExamId = b.F_Id
                 JOIN ftb_cultivate_test_paper c ON b.F_PaperId = c.F_Id
        WHERE b.F_ExamType = 0


    </select>

    <!--通过岗位查询考试列表-->
    <select id="pagingQueryExamListByPosition" resultType="jnpf.model.cultivate.resp.PositionExamDto">
        SELECT a.F_Id             AS innerBuinessId,
               a.F_PostLearnId    AS postLearnId,
               a.examSource       as examSource,
               b.F_Id             AS examId,
               b.F_ExamName       AS examName,
               b.F_ExamType       AS examType,
               b.F_StartTime      AS startTime,
               b.F_EndTime        AS endTime,
               b.F_ExamTime       AS examTime,
               b.F_Examlimitation AS examlimitation,
               b.F_ExcellentType  AS excellentType,
               b.F_ExcellentMark  AS excellentMark,
               b.F_PassType       AS passType,
               b.F_PassMark       AS passMark,
               b.F_PaperId        AS paperId,
               b.F_Description    AS description,
               c.F_Name           AS paperName,
               c.F_QuestionNumber AS questionNumber,
               c.F_TotalScore     AS examTotleScore
        FROM (SELECT F_Id, F_PostRankId, F_ExamId, F_PostLearnId, F_EnabledMark, "2" as examSource
              FROM ftb_cultivate_position_exam
              where F_EnabledMark = 0
                AND F_PostRankId = #{params.postionId}
              UNION ALL
              SELECT F_Id, F_PostRankId, F_ExamId, F_PostLearnId, F_EnabledMark, "1" as examSource
              FROM ftb_cultivate_position_course_exam
              where F_EnabledMark = 0
                AND F_PostRankId = #{params.postionId}) a
                 JOIN ftb_cultivate_exam b ON a.F_ExamId = b.F_Id
                 JOIN ftb_cultivate_test_paper c ON b.F_PaperId = c.F_Id
        WHERE b.F_ExamType = 0


    </select>

    <!--通查询待我考试的数量-->
    <select id="queryWaitMyExamNum" resultType="int">
        <include refid="Base_Sql"></include>
        <where>
            a.F_EnabledMark = 1
            and a.F_Status = 1
            AND (
            a.F_ReviewerUserIds like CONCAT('%',#{params.userId},'%')
            <foreach item="id" index="index" collection="params.roleIdList" open="" separator=" " close="">
                or a.F_ReviewerRoleId =#{id}
            </foreach>
            )
        </where>
    </select>
    <!--通查询我完成考试的数量-->
    <select id="queryMyCompleteExamNum" resultType="int">
        <include refid="Base_Sql"></include>
        <where>
            a.F_EnabledMark = 1
            AND a.F_Status in (3,4,5)
            AND a.F_ReviewerUserId=#{params.userId}
        </where>
    </select>

    <!--通查询带我批阅的过期的数量-->
    <select id="queryExpireExamNum" resultType="int">
        <include refid="Base_Sql"></include>
        <where>
            a.F_EnabledMark = 1
            and a.F_Status=2
            And a.F_AppShow = 0
            AND (
            a.F_ReviewerUserIds like CONCAT('%',#{params.userId},'%')
            <foreach item="id" index="index" collection="params.roleIdList" open="" separator=" " close="">
                or a.F_ReviewerRoleId =#{id}
            </foreach>
            )
        </where>

    </select>

    <!--查询平均成绩-->
    <select id="queryAvgScore" resultType="jnpf.model.cultivate.resp.UserRankingVo">

        SELECT
        a.F_UserId AS userId,
        AVG(a.F_Score) AS score
        from ftb_cultivate_exam_user a
        where a.F_Status in (3,5) And a.F_EnabledMark = 1 And a.F_IsJoinCount = 0
        <if test="dto.startTime!= null and dto.endTime!= null">
            and a.F_FinishTime between #{dto.startTime} and #{dto.endTime}
        </if>

        <if test="dto.selectOrgList != null and dto.selectOrgList.size() > 0 ">
            and
            <foreach collection="dto.selectOrgList" item="item" open="(" separator="or" close=")">
                a.F_UserOrgList like CONCAT('%',#{item},'%')
            </foreach>
        </if>
        <if test="dto.selectUserIds != null and dto.selectUserIds.size() > 0 ">
            and a.F_UserId in
            <foreach collection="dto.selectUserIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>


        group by a.F_UserId
        order by a.F_Score DESC limit 10

    </select>
    <!--查询指定考试的考试成绩排行榜-->
    <select id="pagingQueryRankingExamList" resultType="jnpf.model.cultivate.resp.UserRankingVo">
        SELECT a.F_UserId     AS userId,
               a.F_Status     AS status,
               max(a.F_Score) AS score
        from ftb_cultivate_exam_user a
        where a.F_ExamId = #{examId}
          and a.F_Status in (3, 5)
          And a.F_EnabledMark = 1
        group by a.F_UserId
        order by a.F_Score DESC
    </select>
    <!--查询我的考试成绩分数-->
    <select id="queryMyRank" resultType="jnpf.model.cultivate.resp.UserRankingVo">
        SELECT a.F_UserId AS userId,
               a.F_Status AS status,
               a.F_Score  AS score
        from ftb_cultivate_exam_user a
        where a.F_ExamId = #{examId}
          and a.F_UserId = #{userId}
          and a.F_Status in (1, 3, 4, 5)
          And a.F_EnabledMark = 1
    </select>
    <!--根据用户id查询考试列表-->
    <select id="pagingQueryExamListForUserId" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        <include refid="Base_Column_List"></include>
        where
        a.F_EnabledMark = 1
        AND a.F_UserId =#{userId}
        order by a.F_Status asc
    </select>

    <!--考试信息按照个人维度查询-->
    <select id="queryExamStatisticsForPerson" resultType="jnpf.model.cultivate.resp.ExamStatisticsForPersonVo">
        SELECT
        a.F_Id AS id,
        a.F_UserId AS userId,
        a.F_Duration AS duration,
        a.F_Score AS score,
        a.F_Status AS status,
        a.F_PaperId AS paperId,
        a.F_FinishTime AS finishTime,
        a.F_ExamSource AS examSource,
        a.F_RelationPositionId as relationPositionId,
        a.F_RelationCourseExamId AS relationCourseExamId,
        a.F_RelationPositionExamId AS relationPositionExamId,
        a.F_RelationRankId AS relationRankId,
        fa.F_ExamName AS examName,
        fa.F_ExamTime AS examTime,
        fa.F_ExamType AS examType,
        b.F_Name AS paperName,
        b.F_PaperType as paperType,
        roster.F_Name as userName,
        roster.F_SystemWokerId as systemWokerId
        FROM
        ftb_cultivate_exam_user a
        LEFT JOIN ftb_cultivate_exam fa ON a.F_ExamId = fa.F_Id
        LEFT JOIN ftb_cultivate_test_paper b ON a.F_PaperId = b.F_Id
        LEFT JOIN ftb_personnels_staff_roster roster ON roster.F_UserId = a.F_UserId
        <where>
            a.F_EnabledMark = 1
            And a.F_AppShow = 0
            And a.F_IsJoinCount = 0
            <if test="params.examId!= null and params.examId!= ''">
                and  a.F_ExamId = #{params.examId}
            </if>
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                roster.F_Name like CONCAT('%',#{params.keyWord},'%')
                or roster.F_SystemWokerId like CONCAT('%',#{params.keyWord},'%')
                or b.F_Name like CONCAT('%',#{params.keyWord},'%')
                )
            </if>

            <if test="params.innerUserIds != null and params.innerUserIds.size() >0 ">
                and a.F_UserId in
                <foreach collection="params.innerUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.leaverUserIds != null and params.leaverUserIds.size() >0 ">
                and a.F_UserId not in
                <foreach collection="params.leaverUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.innerStatus != null and params.innerStatus.size() > 0 ">
                and a.F_Status in
                <foreach collection="params.innerStatus" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.innerPaperIds != null and params.innerPaperIds.size() >0 ">
                and a.F_PaperId in
                <foreach collection="params.innerPaperIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="params.startTime!= null and params.endTime!= null">
                and a.F_FinishTime between #{params.startTime} and #{params.endTime}
            </if>
            <if test="params.studyPotionId != null and params.studyPotionId !='' ">
                and a.F_RelationPositionId=#{params.studyPotionId}
            </if>
            order by a.F_CreatorTime desc
        </where>
    </select>
    <!--考试信息按照组织维度查询-->
    <select id="countForOrg" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        SELECT a.F_Id AS id,
        a.F_ExamId AS examId,
        a.F_ExamType AS examType,
        a.F_UserId AS userId,
        a.F_Status AS STATUS,
        a.F_PaperId AS paperId,
        a.F_StartTime AS startTime,
        a.F_EndTime AS endTime,
        a.F_FinishTime AS finishTime,
        a.F_ExamSource AS examSource,
        a.F_UserOrgList as userOrgList,
        a.F_RelationCourseExamId AS relationCourseExamId,
        a.F_RelationPositionExamId AS relationPositionExamId,
        a.F_RelationRankId AS relationRankId,
        a.F_RelationPositionId AS relationPositionId,
        fa.F_ExamName AS examName,
        b.F_Name AS paperName,
        b.F_PaperType AS paperType
        FROM ftb_cultivate_exam_user a
        LEFT JOIN ftb_cultivate_exam fa ON a.F_ExamId = fa.F_Id
        LEFT JOIN ftb_cultivate_test_paper b ON a.F_PaperId = b.F_Id
        <where>
            a.F_EnabledMark = 1
            and a.F_Status in(1,3,4,5)
            And a.F_IsJoinCount = 0
            <if test="dto.keyWord!= null and dto.keyWord!= ''">
                and (
                fa.F_ExamName like CONCAT('%',#{dto.keyWord},'%')
                or b.F_Name like CONCAT('%',#{dto.keyWord},'%')
                )
            </if>

            <if test="dto.startTime!= null and dto.endTime!= null">
                and a.F_FinishTime between #{dto.startTime} and #{dto.endTime}
            </if>

            <if test="dto.selectUserIds != null and dto.selectUserIds.size() > 0 ">
                and a.F_UserId  in
                <foreach collection="dto.selectUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.paperIdsList != null and dto.paperIdsList.size() > 0 ">
                and a.F_PaperId in
                <foreach collection="dto.paperIdsList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            order by a.F_CreatorTime desc
        </where>

    </select>
    <!--考试信息按照组织维度查询-->
    <select id="webStatisticsForOrg" resultType="jnpf.model.cultivate.po.exam.FtbCultivateExamUser">
        select
        a.F_Id AS id,
        a.F_UserId AS userId,
        a.F_Status AS `status`
        FROM ftb_cultivate_exam_user a
        <where>
            a.F_EnabledMark = 1
            and a.F_Status in(1,3,4,5)
            And a.F_IsJoinCount = 0
            <if test="dto.startTime!= null and dto.endTime!= null">
                and a.F_FinishTime between #{dto.startTime} and #{dto.endTime}
            </if>


            <if test="dto.selectOrgList != null and dto.selectOrgList.size() > 0 ">
                and
                <foreach collection="dto.selectOrgList" item="item" open="(" separator="or" close=")">
                    a.F_UserOrgList like CONCAT('%',#{item},'%')
                </foreach>
            </if>
            <if test="dto.selectUserIds != null and dto.selectUserIds.size() > 0 ">
                AND a.F_UserId IN
                <foreach item="id" collection="dto.selectUserIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>


        </where>

    </select>

    <!--查询带我批阅的考试指定批次的待批阅数量-->
    <select id="queryWaitReadOverNum" resultType="jnpf.model.cultivate.resp.WaitReadOverNumVo">
        SELECT count(*)         AS num,
               a.F_VersionBatch AS versionBatch

        FROM ftb_cultivate_exam_user a
        where a.F_EnabledMark = 1
          and a.F_Status = '1'
          And a.F_IsJoinCount = 0
          And a.F_ExamId = #{examId}
        group by a.F_VersionBatch


    </select>
    <!--查询需要提示的岗位考试列表-->
    <select id="queryAlertPostionIdentify" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        SELECT a.F_Id                     AS id,
        a.F_ExamId                 AS examId,
        a.F_ExamType               AS examType,
        a.F_UserId                 AS userId,
        a.F_Duration               AS duration,
        a.F_Score                  AS userTotleScore,
        a.F_Status                 AS STATUS,
        a.F_PaperId                AS paperId,
        a.F_StartTime              AS startTime,
        a.F_EndTime                AS endTime,
        a.F_Content                AS content,
        a.F_Batch                  AS batch,
        a.F_UserStartTime          AS userStartTime,
        a.F_FinishTime             AS finishTime,
        a.F_ExamSource             AS examSource,
        a.F_RelationCourseExamId   AS relationCourseExamId,
        a.F_RelationPositionExamId AS relationPositionExamId,
        a.F_RelationRankId         AS relationRankId,
        a.F_RelationPositionId     AS relationPositionId,
        a.F_VersionBatch           As versionBatch

        FROM ftb_cultivate_exam_user a join ftb_cultivate_position b on a.F_RelationPositionId=b.F_PostId

        where a.F_EnabledMark = 1 AND a.F_UserId =#{userId} and a.F_ExamSource=2
            And a.F_AppShow = 0 and a.F_ExamType ='0' and a.F_Status in(3,5)
            and b.F_Qualified=1 and a.F_CompleteIdentify=0



    </select>

    <!--批量查询一个岗位的指定人员的考试信息-->
    <select id="queryTheListOfQualifiedPersons" resultType="jnpf.model.cultivate.po.exam.FtbCultivateExamUser">
        SELECT
        t.F_UserId AS userId,
        t.F_Score AS score,
        t.F_Status AS `status`,
        t.F_CreatorTime AS creatorTime
        FROM
        (
        SELECT
        u.*,
        ROW_NUMBER() OVER ( PARTITION BY u.F_UserId ORDER BY u.F_CreatorTime DESC ) AS rn
        FROM
        ftb_cultivate_exam_user u
        WHERE
         u.F_ExamSource IN ( 1, 2 )
        and  u.F_Id in
        <foreach collection="exmIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and u.F_RelationPositionId = #{positionId}
        AND u.F_Status NOT IN ( 2 )
        AND u.F_EnabledMark = 1
        AND u.F_UserId IN
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        WHERE
        t.rn = 1
    </select>
    <!--批量查询一个岗位的指定人员的考试信息-->
    <select id="queryTheListOfQualifiedPersonsNew" resultType="jnpf.model.cultivate.po.exam.FtbCultivateExamUser">
        SELECT
        t.F_UserId AS userId,
        t.F_Score AS score,
        t.F_Status AS `status`,
        t.F_CreatorTime AS creatorTime
        FROM
        (
        SELECT
        u.*,
        ROW_NUMBER() OVER ( PARTITION BY u.F_UserId ORDER BY u.F_CreatorTime DESC ) AS rn
        FROM
        ftb_cultivate_exam_user u
        WHERE
        u.F_ExamSource IN ( 1, 2 )
        and u.F_RelationPositionId = #{positionId}
        AND u.F_Status NOT IN ( 2 )
        AND u.F_EnabledMark = 1
        AND u.F_UserId IN
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        ) t
        WHERE
        t.rn = 1
    </select>
    <!--统计通过的任务考试数量-->
    <select id="groupPassCountNum" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
        SELECT
        count(*) AS num,
        a.F_RelationTaskId AS selectKey
        FROM
        ftb_cultivate_exam_user a
        WHERE
        a.F_EnabledMark = 1
        AND a.F_ExamSource = 4
        AND a.F_RelationTaskId IN
        <foreach collection="taskIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND a.F_Status IN ( 3, 5 )
        group by a.F_RelationTaskId
    </select>
    <!--批量统计所有的任务考试数量-->
    <select id="groupAllCountNum" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
        SELECT
        count(*) AS num,
        a.F_RelationTaskId AS selectKey
        FROM
        ftb_cultivate_exam_user a
        WHERE
        a.F_EnabledMark = 1
        AND a.F_ExamSource = 4
        AND a.F_RelationTaskId IN
        <foreach collection="taskIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by a.F_RelationTaskId

    </select>
    <select id="groupAllCountNumV2" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
    SELECT
    count(*) AS num,
    task_exam.F_TaskId AS selectKey
    FROM
    ftb_cultivate_exam_user a join ftb_cultivate_learn_task_exam as task_exam on a.F_ExamId = task_exam.F_ExamId
    WHERE
    a.F_EnabledMark = 1 and    task_exam.F_EnableMark=0

    AND task_exam.F_TaskId IN
    <foreach collection="taskIds" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
    group by task_exam.F_TaskId
    </select>
    <select id="storeMyExamList" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        <include refid="Base_Column_List"></include>
        <where>
        a.F_EnabledMark = 1
        And a.F_AppShow = 0
        and a.F_UserId =#{userId}
        </where>
        order by a.F_CreatorTime asc

    </select>


    <select id="queryMyWaitingExamNum" resultType="java.lang.Long">
        SELECT count(*)         AS num
        FROM ftb_cultivate_exam_user a
        where a.F_EnabledMark = 1
          and a.F_Status = 0
          and a.F_UserId = #{userId}
          And a.F_AppShow = 0
    </select>

    <select id="queryAllMyReadOver" resultType="java.lang.Long">
        SELECT count(*)
        FROM ftb_cultivate_exam_user a
        <where>
            a.F_EnabledMark = 1
            AND a.F_Status in (3,4,5)
            AND a.F_ReviewerUserId=#{userId}
            order by a.F_Status asc
        </where>
    </select>
    <select id="queryPassMyReadOver" resultType="java.lang.Long">
        SELECT count(*)
        FROM ftb_cultivate_exam_user a
        <where>
            a.F_EnabledMark = 1
            AND a.F_Status in (3,5)
            AND a.F_ReviewerUserId=#{userId}
            order by a.F_Status asc
        </where>
    </select>

    <select id="countForOrgV2" resultType="jnpf.model.cultivate.resp.AppExamListVo">
        SELECT a.F_Id AS id,
        a.F_ExamId AS examId,
        a.F_ExamType AS examType,
        a.F_UserId AS userId,
        a.F_Status AS STATUS,
        a.F_PaperId AS paperId,
        a.F_StartTime AS startTime,
        a.F_EndTime AS endTime,
        a.F_FinishTime AS finishTime,
        a.F_ExamSource AS examSource,
        fa.F_ExamName AS examName
        FROM ftb_cultivate_exam_user a
        LEFT JOIN ftb_cultivate_exam fa ON a.F_ExamId = fa.F_Id
        <where>
            a.F_EnabledMark = 1
            and a.F_Status in(1,3,4,5)
            And a.F_IsJoinCount = 0
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and fa.F_ExamName like CONCAT('%',#{params.keyWord},'%')
            </if>

            <if test="params.startTime!= null and params.endTime!= null">
                and a.F_FinishTime between #{params.startTime} and #{params.endTime}
            </if>

            <if test="userIds != null and userIds.size() > 0 ">
                and a.F_UserId in
                <foreach collection="userIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            order by a.F_CreatorTime desc
        </where>
    </select>

    <select id="queryExamStatisticsForPersonV2"
            resultType="jnpf.model.cultivate.v2.exam.vo.V2ExamStatisticsForPersonVo">
        SELECT
        a.F_Id AS id,
        a.F_UserId AS userId,
        a.F_Duration AS duration,
        a.F_Score AS score,
        a.F_Status AS status,
        a.F_PaperId AS paperId,
        a.F_FinishTime AS finishTime,
        a.F_ExamSource AS examSource,
        a.F_RelationPositionId as relationPositionId,
        a.F_RelationCourseExamId AS relationCourseExamId,
        a.F_RelationPositionExamId AS relationPositionExamId,
        a.F_RelationRankId AS relationRankId,
        fa.F_ExamName AS examName,
        fa.F_ExamTime AS examTime,
        fa.F_ExamType AS examType,
        u.F_RealName as userName,
        u.F_SystemWorkerId as systemWorkerId
        FROM
        ftb_cultivate_exam_user a
        LEFT JOIN ftb_cultivate_exam fa ON a.F_ExamId = fa.F_Id
        LEFT JOIN base_user u ON u.F_Id = a.F_UserId
        <where>
            a.F_EnabledMark = 1
            And a.F_AppShow = 0
            And a.F_IsJoinCount = 0
            <if test="params.examId!= null and params.examId!= ''">
                and a.F_ExamId = #{params.examId}
            </if>
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and (
                fa.F_ExamName like CONCAT('%',#{params.keyWord},'%')
                )
            </if>
            <if test="innerUserIds != null and innerUserIds.size() > 0">
                and a.F_UserId in
                <foreach collection="innerUserIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="innerStatus != null and innerStatus.size() > 0">
                and a.F_Status in
                <foreach collection="innerStatus" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.startTime!= null and params.endTime!= null">
                and a.F_FinishTime between #{params.startTime} and #{params.endTime}
            </if>
            <if test="params.studyPotionId != null and params.studyPotionId !='' ">
                and a.F_RelationPositionId=#{params.studyPotionId}
            </if>
            order by a.F_CreatorTime desc
        </where>
    </select>

    <select id="groupPassCountNumV2" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
        SELECT
        count(*) AS num,
        task_exam.F_TaskId AS selectKey
        FROM
        ftb_cultivate_exam_user a join ftb_cultivate_learn_task_exam as task_exam on a.F_ExamId = task_exam.F_ExamId
        WHERE
        a.F_EnabledMark = 1 and task_exam.F_EnableMark=0
        AND a.F_Status IN ( 3, 5 )
        AND task_exam.F_TaskId IN
        <foreach collection="taskIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by task_exam.F_TaskId
    </select>
    <!-- 查询用户考试列表 -->
    <select id="getExamUserList" resultType="jnpf.model.cultivate.v2.exam.vo.V2TestPaperVo">
        select
        F_Id userExamId,
        F_ExamId examId,
        F_UserId userId,
        F_Duration duration,
        F_Score score,
        F_ScoreRatio scoreRatio,
        F_UserExamCount userExamCount,
        F_FinishTime finishedTime,
        F_Status paperStatus,
        F_Remark remark,
        case when F_VersionBatch = #{versionBatch} then 1 else 0 end currentVersion
        from ftb_cultivate_exam_user
        where F_ExamId = #{id} and F_EnabledMark = 1
        <if test="null != userIds and userIds.size > 0">
            and F_UserId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by
        <if test="null != scoreSort">
            <if test="scoreSort == 1">
                F_Score desc,
            </if>
            <if test="scoreSort == 0">
                F_Score asc,
            </if>
        </if>
        F_CreatorTime desc
    </select>
    <!-- 查询我的考试列表 -->
    <select id="getMyExamList" resultType="jnpf.model.cultivate.v2.exam.vo.MyExamVo">
        select eu.F_Id userExamId, eu.F_ExamId examId, e.F_ExamName examName, e.F_Cover cover, e.F_Status `status`,
               eu.F_Duration duration, eu.F_Score score, eu.F_TotalScore totalScore, eu.F_Status myExamStatus
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on eu.F_ExamId = e.F_Id
        where eu.F_EnabledMark = 1 and e.F_EnabledMark = 1 and eu.F_UserId = #{userId}
        <if test="null != status">
            <if test="status == 3">
                and eu.F_Status in (3, 5)
            </if>
            <if test="status != 3">
                and eu.F_Status = #{status}
            </if>
        </if>
        order by eu.F_CreatorTime desc
    </select>
    <!-- 根据状态查询用户考试数 -->
    <select id="getStatusCount" resultType="java.lang.Integer">
        select count(*) countNum
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on eu.F_ExamId = e.F_Id
        where eu.F_EnabledMark = 1 and e.F_EnabledMark = 1
        and eu.F_UserId = #{userId}
        <if test="null != examStatus">
            <if test="examStatus == 3">
                and eu.F_Status in (3, 5)
            </if>
            <if test="examStatus != 3">
                and eu.F_Status = #{examStatus}
            </if>
        </if>
    </select>
    <!-- 查询用户考试详情 - 基础信息 -->
    <select id="getMyExamDetail" resultType="jnpf.model.cultivate.v2.exam.vo.MyExamDetailVo">
        select eu.F_Id userExamId, eu.F_ExamId examId, e.F_ExamName examName, eu.F_PickQuestionRule pickQuestionRule, e.F_Status examStatus, eu.F_Status myExamStatus,
               eu.F_UserExamResultValid userExamResultValid, eu.F_Remark remark,
               eu.F_Duration userExamTime, eu.F_QuestionNumber questionNum, eu.F_TotalScore totalScore, eu.F_PassScore passScore, eu.F_ExcellentScore excellentScore, eu.F_ExamTime examTime,
               case when eu.F_StartTime is null then 0 else 1 end limitedTime, eu.F_StartTime startTime, eu.F_EndTime endTime, e.F_NeedUpdate needUpdate
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on eu.F_ExamId = e.F_Id
        where eu.F_Id = #{userExamId}
    </select>
    <!-- 查询用户考试详情 - 考试结果 -->
    <select id="getExamResult" resultType="jnpf.model.cultivate.v2.exam.vo.ExamResultVo">
        select F_UserId userId, F_FinishTime submitTime, F_Status myExamStatus, F_Duration duration,
               F_TotalScore totalScore, F_Score `score`, F_QuestionNumber questionNum,
               case when F_QuestionNumber = 0 then null else F_correctCount end correctCount,
               case when F_QuestionNumber = 0 then null else (F_QuestionNumber - F_correctCount) end errorCount
        from ftb_cultivate_exam_user
        where F_Id = #{userExamId}
    </select>
    <!-- 批阅考试 - 考试列表 -->
    <select id="readOverExamList" resultType="jnpf.model.cultivate.v2.exam.vo.ReadOverExamVo">
        select eu.F_Id userExamId, eu.F_ExamId examId, e.F_ExamName examName, e.F_Cover cover, eu.F_Status myExamStatus,
               eu.F_UserId userId, eu.F_FinishTime submitTime, eu.F_TotalScore totalScore, eu.F_Score score
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on eu.F_ExamId = e.F_Id
        where eu.F_EnabledMark = 1 and e.F_EnabledMark = 1 and eu.F_Status not in (0)
        <if test="null != userIds and userIds.size > 0">
            and eu.F_UserId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="null != keyword and '' != keyword">
            and instr(e.F_ExamName, #{keyword}) > 0
        </if>
        <if test="null != status">
            <if test="status == 1">
                and eu.F_Status = #{status}
            </if>
            <if test="status == 2">
                and eu.F_Status in (3, 4, 5)
            </if>
            <if test="status == 3">
                and eu.F_Status = 2
            </if>
        </if>
        order by eu.F_CreatorTime desc
    </select>
    <!-- 查询考试下用户排名列表 -->
    <select id="getUserExamRankList" resultType="jnpf.model.cultivate.v2.exam.vo.V2AppExamRankingVo">
        SELECT
            t.F_UserId userId,
            t.F_Status `status`,
            t.F_Score score,
            t.F_ScoreRatio scoreRate,
            DENSE_RANK() OVER (ORDER BY CASE WHEN t.F_Status IN (0,1,2) THEN 1 ELSE 0 END, t.F_ScoreRatio DESC, t.F_Score DESC, F_CreatorTime ASC) AS ranking
        FROM ftb_cultivate_exam_user t
        WHERE t.F_ExamId = #{examId}
        AND t.F_EnabledMark = 1
        <if test="null != userIds and userIds.size > 0">
            AND t.F_UserId IN
            <foreach collection="userIds" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ORDER BY ranking ASC, F_FinishTime DESC, F_UserId ASC
    </select>
    <!-- 查询待批阅数量 -->
    <select id="getWaitCheckCount" resultType="java.lang.Integer">
        select count(*) countNum
        from ftb_cultivate_exam_user eu
        left join ftb_cultivate_exam e on eu.F_ExamId = e.F_Id
        where eu.F_EnabledMark = 1 and e.F_EnabledMark = 1 and eu.F_Status = #{examStatus}
        <if test="null != userIds and userIds.size > 0">
            and eu.F_UserId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by eu.F_Status
    </select>

    <!-- 根据考试ID统计考试数据 -->
    <select id="getExamStatistics" resultType="jnpf.model.cultivate.v2.exam.vo.AiHelperExamStatisticsVo">
        SELECT
            COUNT(CASE WHEN eu.F_Status IN (1, 3, 4, 5) THEN 1 END) AS totalParticipants,
            COUNT(CASE WHEN eu.F_Status IN (3, 5) THEN 1 END) AS qualifiedCount,
            CASE
                WHEN COUNT(CASE WHEN eu.F_Status IN (1, 3, 4, 5) THEN 1 END) = 0 THEN 0
                ELSE ROUND(
                    COUNT(CASE WHEN eu.F_Status IN (3, 5) THEN 1 END) * 100.0 /
                    COUNT(CASE WHEN eu.F_Status IN (1, 3, 4, 5) THEN 1 END),
                    2
                )
            END AS passRate,
            COUNT(CASE WHEN eu.F_Status = 1 THEN 1 END) AS pendingReviewCount
        FROM ftb_cultivate_exam_user eu
        WHERE eu.F_ExamId = #{examId}
          AND eu.F_EnabledMark = 1
    </select>

    <!--查询用户已完成的考试列表（关联查询考试信息）-->
    <select id="queryCompletedExamsWithInfo" resultType="jnpf.model.cultivate.v2.course.vo.UserLearningStatusVo$ExamInfo">
        SELECT
            eu.F_ExamId AS examId,
            e.F_ExamName AS examName
        FROM
            ftb_cultivate_exam_user eu
        INNER JOIN ftb_cultivate_exam e ON eu.F_ExamId = e.F_Id
        WHERE
            eu.F_EnabledMark = 1
          AND e.F_EnabledMark = 1
          AND eu.F_Status IN (3, 4, 5)
          AND eu.F_UserId = #{userId}
        ORDER BY eu.F_FinishTime DESC
    </select>
</mapper>