<?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.ruoyi.learn.mapper.LearnAnswerMapper">
    
    <resultMap type="LearnAnswer" id="LearnAnswerResult">
        <result property="answerId"    column="answer_id"    />
        <result property="examineeId"    column="examinee_id"    />
        <result property="studentId"    column="student_id"    />
        <result property="questionId"    column="question_id"    />
        <result property="seq"    column="seq"    />
        <result property="knowledgeId"    column="knowledge_id"    />
        <result property="answer"    column="answer"    />
        <result property="correct"    column="correct"    />
        <result property="duration"    column="duration"    />
        <result property="count"    column="count"    />
        <result property="state"    column="state"    />
        <result property="scoreLevel"    column="score_level"    />
        <result property="score"    column="score"    />
        <result property="scoreRate"    column="score_rate"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectLearnAnswerVo">
        select answer_id, examinee_id, student_id, question_id, seq, knowledge_id, answer, correct, duration, count, state, score_level, score, score_rate, isMark, isNotKnow, create_time, update_time from learn_answer
    </sql>

    <select id="selectSimulatedKnowledgeStats" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT a.`knowledge_id` `id`, kt.`name`, COUNT(*) `total`, SUM(IF(a.`state` = 1, 1, 0)) `value`, ROUND(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
          FROM `learn_examinee` e
          JOIN `learn_answer` a ON a.`examinee_id` = e.`examinee_id` AND a.`state` &gt; 0
          JOIN `learn_knowledge_tree` kt ON a.`knowledge_id` = kt.`id`
         WHERE e.`examinee_id` = #{examineeId}
         GROUP BY  a.`knowledge_id`, kt.`name`
         ORDER BY rate DESC
    </select>

    <select id="selectClassKnowledgeHeader" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT ROUND(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*)) rate
          FROM `learn_answer` a
          JOIN `learn_student` ls ON ls.`student_id` = a.`student_id`
         WHERE a.`state` > 0 AND ls.`class_id` = #{classId}
    </select>

    <select id="selectClassKnowledgeDetail" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT ls.`student_id` id, u.`nick_name` `name`
             , COUNT(*) total, SUM(IF(a.`state` = 1, 1, 0)) `value`, ROUND(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
        FROM `learn_answer` a
        JOIN `learn_student` ls ON ls.`student_id` = a.`student_id`
        JOIN `sys_user` u ON u.`user_id` = ls.`student_id`
        WHERE a.`state` > 0 AND ls.`class_id` = #{classId}
        GROUP BY ls.`student_id`, u.`nick_name`
        ORDER BY rate DESC, a.`student_id`
    </select>


    <select id="selectKnowledgeStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT a.`knowledge_id` id , kt.`name`, a.`knowledge_id` IN <foreach item="id" collection="knowIds" open="(" separator="," close=")">#{id}</foreach> AS directed
        , COUNT(*) total, SUM(IF(a.`state` = 1, 1, 0)) `value`, round(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
        FROM `learn_answer` a
        JOIN `learn_knowledge_tree` kt ON a.`knowledge_id` = kt.`id`
        WHERE a.`student_id` = #{studentId} and a.`state` > 0
        GROUP BY a.`knowledge_id`, directed
        order by directed desc, rate desc, a.`knowledge_id`
    </select>

    <select id="selectPaperKnowledgeStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT a.`knowledge_id` knowledgeId , kt.`name` knowledgeName
        , COUNT(*) num, SUM(IF(a.`state` = 1, 1, 0)) correct, round(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
        FROM `learn_answer` a
        JOIN `learn_knowledge_tree` kt ON a.`knowledge_id` = kt.`id`
        WHERE a.`examinee_id` = #{examineeId} and a.`state` > 0
        GROUP BY a.`knowledge_id`
        order by rate desc, a.`knowledge_id`
    </select>

    <select id="selectClassQuestionStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT ls.`student_id` id, u.`nick_name` `name`, COUNT(*) total , COUNT(DISTINCT DATE(a.create_time)) `time`, SUM(IF(a.`state` = 1, 1, 0)) `value`, ROUND(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
          FROM `learn_answer` a
          JOIN `learn_student` ls ON ls.`student_id` = a.`student_id`
          JOIN `sys_user` u ON ls.`student_id` = u.`user_id`
         WHERE ls.`class_id` = #{classId}
         GROUP BY ls.`student_id`
         ORDER BY <choose><when test="sortField='total'">total</when><when test="sortField='time'">`time`</when><otherwise>rate</otherwise></choose> <if test="null != desc and desc">DESC</if>
    </select>

    <select id="selectQuestionStatsHeader" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT COUNT(*) total, SUM(IF(a.`state` = 1, 1, 0)) `value`, round(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*)) rate, COUNT(DISTINCT DATE(a.create_time)) `time`
          FROM `learn_answer` a
          WHERE a.`student_id` = #{studentId} and a.`state` > 0 and a.`create_time` between #{beginDate} and #{endDate}
    </select>

    <select id="selectQuestionStatsDetail" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT DATE(a.create_time) `time`, COUNT(*) total, SUM(IF(a.`state` = 1, 1, 0)) `value`, round(SUM(IF(a.`state` = 1, 1, 0)) * 100 / COUNT(*), 1) rate
          FROM `learn_answer` a
         WHERE a.`student_id` = #{studentId} and a.`state` > 0 and a.`create_time` between #{beginDate} and #{endDate}
         GROUP BY DATE(a.create_time)
         order by DATE(a.create_time) desc
    </select>

    <select id="selectLearnAnswerList" parameterType="LearnAnswer" resultMap="LearnAnswerResult">
        <include refid="selectLearnAnswerVo"/>
        <where>  
            <if test="examineeId != null "> and examinee_id = #{examineeId}</if>
            <if test="studentId != null "> and student_id = #{studentId}</if>
            <if test="questionId != null "> and question_id = #{questionId}</if>
            <if test="seq != null "> and seq = #{seq}</if>
            <if test="knowledgeId != null "> and knowledge_id = #{knowledgeId}</if>
            <if test="answer != null  and answer != ''"> and answer = #{answer}</if>
            <if test="correct != null  and correct != ''"> and correct = #{correct}</if>
            <if test="duration != null "> and duration = #{duration}</if>
            <if test="count != null "> and count = #{count}</if>
            <if test="state != null "> and state = #{state}</if>
            <if test="scoreLevel != null  and scoreLevel != ''"> and score_level = #{scoreLevel}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="scoreRate != null "> and score_rate = #{scoreRate}</if>
        </where>
    </select>
    
    <select id="selectLearnAnswerByAnswerId" parameterType="Long" resultMap="LearnAnswerResult">
        <include refid="selectLearnAnswerVo"/>
        where answer_id = #{answerId}
    </select>

    <insert id="insertLearnAnswer" parameterType="LearnAnswer" useGeneratedKeys="true" keyProperty="answerId">
        insert into learn_answer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="examineeId != null">examinee_id,</if>
            <if test="studentId != null">student_id,</if>
            <if test="questionId != null">question_id,</if>
            <if test="seq != null">seq,</if>
            <if test="knowledgeId != null">knowledge_id,</if>
            <if test="answer != null">answer,</if>
            <if test="correct != null">correct,</if>
            <if test="duration != null">duration,</if>
            <if test="count != null">count,</if>
            <if test="state != null">state,</if>
            <if test="isMark != null">isMark,</if>
            <if test="isNotKnow != null">isNotKnow,</if>
            <if test="scoreLevel != null">score_level,</if>
            <if test="score != null">score,</if>
            <if test="scoreRate != null">score_rate,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="examineeId != null">#{examineeId},</if>
            <if test="studentId != null">#{studentId},</if>
            <if test="questionId != null">#{questionId},</if>
            <if test="seq != null">#{seq},</if>
            <if test="knowledgeId != null">#{knowledgeId},</if>
            <if test="answer != null">#{answer},</if>
            <if test="correct != null">#{correct},</if>
            <if test="duration != null">#{duration},</if>
            <if test="count != null">#{count},</if>
            <if test="state != null">#{state},</if>
            <if test="isMark != null">#{isMark},</if>
            <if test="isNotKnow != null">#{isNotKnow},</if>
            <if test="scoreLevel != null">#{scoreLevel},</if>
            <if test="score != null">#{score},</if>
            <if test="scoreRate != null">#{scoreRate},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateLearnAnswer" parameterType="LearnAnswer">
        update learn_answer
        <trim prefix="SET" suffixOverrides=",">
            <if test="examineeId != null">examinee_id = #{examineeId},</if>
            <if test="studentId != null">student_id = #{studentId},</if>
            <if test="questionId != null">question_id = #{questionId},</if>
            <if test="seq != null">seq = #{seq},</if>
            <if test="knowledgeId != null">knowledge_id = #{knowledgeId},</if>
            <if test="answer != null">answer = #{answer},</if>
            <if test="correct != null">correct = #{correct},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="count != null">count = #{count},</if>
            <if test="state != null">state = #{state},</if>
            <if test="isMark != null">isMark = #{isMark},</if>
            <if test="isNotKnow != null">isNotKnow = #{isNotKnow},</if>
            <if test="scoreLevel != null">score_level = #{scoreLevel},</if>
            <if test="score != null">score = #{score},</if>
            <if test="scoreRate != null">score_rate = #{scoreRate},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where answer_id = #{answerId}
    </update>

    <delete id="deleteLearnAnswerByAnswerId" parameterType="Long">
        delete from learn_answer where answer_id = #{answerId}
    </delete>

    <delete id="deleteLearnAnswerByAnswerIds" parameterType="String">
        delete from learn_answer where answer_id in 
        <foreach item="answerId" collection="array" open="(" separator="," close=")">
            #{answerId}
        </foreach>
    </delete>
</mapper>