<?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.FtbCultivateQuestionMapper">

    <!--根据题库id查询题库中重复题目-->
    <select id="queryRepeatQuestion" resultType="jnpf.model.cultivate.po.question.FtbCultivateQuestion">
        SELECT F_Content AS content, COUNT(*) AS num
        FROM ftb_cultivate_question
        where F_ClassifyId = #{questionBankId}
          and F_EnabledMark = 1
        GROUP BY F_Content
        HAVING COUNT(*) > 1;
    </select>

    <!--根据题库id统计各题库的题目数量-->
    <select id="countForClassifyId" resultType="jnpf.model.cultivate.resp.QuestionCountDto">
        select F_ClassifyId as questionBankId,count(*) as num from ftb_cultivate_question where F_EnabledMark =1
        <if test="ids != null and ids.size() > 0 ">
            and F_ClassifyId in
            <foreach collection="ids" item="item" open="("  separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY F_ClassifyId
    </select>
    <!-- 在题库中根据规则随机抽题 -->
    <select id="getRandQuestionByRule" resultType="jnpf.model.cultivate.v2.exam.vo.V2AppQuestionVo">
        SELECT t.F_Id questionId, t.F_ClassifyId classifyId, t.F_Type `type`, t.F_Content content,
               t.F_Answer answer, t.F_Analysis analysis, t.F_LastModifyTime lastModifyTime, r.F_Score questionScore
        FROM (
            SELECT q.*,
                ROW_NUMBER() OVER (PARTITION BY q.F_Type, q.F_Difficulty ORDER BY RAND()) rn
            FROM ftb_cultivate_question q
            WHERE q.F_EnabledMark = 1
            AND FIND_IN_SET(q.F_ClassifyId, #{drawDataBase})
            AND q.F_Type IN
            <foreach collection="ruleList" item="rule" open="(" separator="," close=")">
                #{rule.questionType}
            </foreach>
        ) t
        JOIN (
            SELECT *
            FROM (
                VALUES
                <foreach collection="ruleList" item="rule" separator=",">
                    ROW(#{rule.questionType}, #{rule.easyCount}, #{rule.mediumCount}, #{rule.hardCount}, #{rule.score})
                </foreach>
            ) AS tmp(F_QuestionType, F_EasyCount, F_MediumCount, F_HardCount, F_Score)
        ) r
        ON t.F_Type = r.F_QuestionType
        WHERE (t.F_Difficulty = 1 AND t.rn <![CDATA[ <= ]]> r.F_EasyCount)
        OR (t.F_Difficulty = 2 AND t.rn <![CDATA[ <= ]]> r.F_MediumCount)
        OR (t.F_Difficulty = 3 AND t.rn <![CDATA[ <= ]]> r.F_HardCount)
    </select>
    <!-- 根据题目ids查询题目 -->
    <select id="selectListById" resultType="jnpf.model.cultivate.v2.exam.vo.V2AppQuestionVo">
        select t.F_Id questionId, t.F_ClassifyId classifyId, t.F_Type `type`, t.F_Content content,
               t.F_Answer answer, t.F_Analysis analysis, t.F_LastModifyTime lastModifyTime
        from ftb_cultivate_question t
        where t.F_EnabledMark = 1 and t.F_Id in
        <foreach collection="questionIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 题型统计 -->
    <select id="getTypeAnalysis" resultType="jnpf.model.cultivate.v2.exam.vo.QuestionTypeAnalysisVo">
        select t.F_Type `type`, count(*) questionCount,
        sum(case when t.F_Difficulty = 1 then 1 else 0 end) easyCount,
        sum(case when t.F_Difficulty = 2 then 1 else 0 end) mediumCount,
        sum(case when t.F_Difficulty = 3 then 1 else 0 end) hardCount
        from ftb_cultivate_question t
        where t.F_EnabledMark = 1 and t.F_ClassifyId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by F_Type
        order by F_Type asc
    </select>
    <!-- 查询题目列表 -->
    <select id="getQuestionList" resultType="jnpf.model.cultivate.v2.exam.vo.QuestionVo">
        select t.F_Id questionId, t.F_Difficulty difficulty, qb.F_BankContent bankName, t.F_Content content, t.F_Type `type`, t.F_LastModifyTime lastModifyTime
        from ftb_cultivate_question t
        left join ftb_cultivate_question_bank qb on qb.F_Id = t.F_ClassifyId
        where t.F_EnabledMark = 1
        <if test="null != keyword and '' != keyword">
            and instr(t.F_Content, #{keyword}) > 0
        </if>
        and t.F_ClassifyId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        order by t.F_LastModifyTime desc, t.F_Id desc
    </select>
    <!-- 查询题库题型数量 -->
    <select id="getBankQuestionTypeCount" resultType="jnpf.model.cultivate.v2.exam.vo.BankPickVo">
        SELECT
            F_ClassifyId AS bankId,
            F_Type AS questionType,
            COUNT(*) AS questionCount,
            SUM(CASE WHEN F_Difficulty = 1 THEN 1 ELSE 0 END) AS easyCount,
            SUM(CASE WHEN F_Difficulty = 2 THEN 1 ELSE 0 END) AS mediumCount,
            SUM(CASE WHEN F_Difficulty = 3 THEN 1 ELSE 0 END) AS hardCount
        FROM ftb_cultivate_question
        WHERE F_EnabledMark = 1
        AND (F_ClassifyId, F_Type) IN (
            <foreach collection="bankPickList" item="item" separator=",">
                (#{item.bankId}, #{item.questionType})
            </foreach>
        )
        GROUP BY F_ClassifyId, F_Type
    </select>
    <!-- 查询试卷抽题规则 -->
    <select id="getPaperDrawRuleList" resultType="jnpf.model.cultivate.resp.PaperDrawRuleVo">
        select pq.F_PaperId paperId, q.F_Type questionType, q.F_Difficulty difficulty, pq.F_QuestionId questionId, pq.F_Score score, 1 as normal, 1 as pickCount
        from ftb_cultivate_test_paper_question pq
        left join ftb_cultivate_question q on pq.F_QuestionId = q.F_Id
        where pq.F_EnabledMark = 1 and pq.F_PaperId in
        <foreach collection="paperIds" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>