<?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.LearnExamineeMapper">
    
    <resultMap type="LearnExaminee" id="LearnExamineeResult">
        <result property="examineeId"    column="examinee_id"    />
        <result property="studentId"    column="student_id"    />
        <result property="paperType"    column="paper_type"    />
        <result property="paperId"    column="paper_id"    />
        <result property="paperKey"    column="paper_key"    />
        <result property="paperInfo"    column="paper_info"    />
        <result property="beginTime"    column="begin_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="duration"    column="duration"    />
        <result property="state"    column="state"    />
        <result property="scoreLevel"    column="score_level"    />
        <result property="score"    column="score"    />
        <result property="scoreRate"    column="score_rate"    />
        <result property="ranking"    column="ranking"    />
        <result property="gradeRanking"    column="grade_ranking"    />
        <result property="wrongCount"    column="wrong_count"    />
        <result property="chooseCount"    column="choose_count"    />
        <result property="chooseTotal"    column="choose_total"    />
        <result property="subjectiveCount"    column="subjective_count"    />
        <result property="subjectiveTotal"    column="subjective_total"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectLearnExamineeVo">
        select examinee_id, student_id, paper_type, paper_id, paper_key, paper_info, begin_time, end_time, duration, stats, state, score_level, score, score_rate, ranking, grade_ranking, wrong_count, choose_count, choose_total, subjective_count, subjective_total, create_time, update_time from learn_examinee
    </sql>

    <select id="selectLearnExamineeList" parameterType="LearnExaminee" resultMap="LearnExamineeResult">
        <include refid="selectLearnExamineeVo"/>
        <where>  
            <if test="studentId != null "> and student_id = #{studentId}</if>
            <if test="paperType != null "> and paper_type = #{paperType}</if>
            <if test="paperId != null "> and paper_id = #{paperId}</if>
            <if test="paperKey != null "> and paper_key = #{paperKey}</if>
            <if test="beginTime != null "> and begin_time = #{beginTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="duration != null "> and duration = #{duration}</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>
            <if test="ranking != null "> and ranking = #{ranking}</if>
            <if test="gradeRanking != null "> and grade_ranking = #{gradeRanking}</if>
            <if test="wrongCount != null "> and wrong_count = #{wrongCount}</if>
            <if test="chooseCount != null "> and choose_count = #{chooseCount}</if>
            <if test="chooseTotal != null "> and choose_total = #{chooseTotal}</if>
            <if test="subjectiveCount != null "> and subjective_count = #{subjectiveCount}</if>
            <if test="subjectiveTotal != null "> and subjective_total = #{subjectiveTotal}</if>
        </where>
        order by examinee_id desc
    </select>

    <select id="selectTestStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
       SELECT COUNT(*) total, SUM(IF(ls.`major_plan_id` IS NULL, 1, 0)) undirect, SUM(IF(ls.`major_plan_id` IS NOT NULL AND ts.`id` IS NULL, 1, 0)) unsend, SUM(IF(ts.`status` IN (1,2,3), 1, 0)) undone, SUM(IF(ts.`status` >= 4, 1, 0)) done
         FROM `dz_teacher_class` tc
         JOIN `learn_student` ls ON ls.`class_id` = tc.`class_id`
         JOIN `dz_classes` c ON ls.`class_id` = c.`class_id`
         LEFT JOIN `learn_test_student` ts ON ts.`batch_id` = #{batchId} AND ts.`student_id` = ls.`student_id` AND ts.`build_type` = #{buildType} AND ts.`subject_id` = #{subjectId}
        WHERE tc.`teacher_id` = #{teacherId} and tc.`class_id` = #{classId}
    </select>

    <select id="selectTestStudents" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
       SELECT ls.`student_id` studentId, c.`name` className, u.`nick_name` nickName,
               case when ls.`major_plan_id` is null then '未组卷未定向' when ls.`major_plan_id` IS NOT NULL AND ts.`id` IS NULL then '未组卷定向' when ts.`status` IN (1,2,3) then '组卷未完成' else '组卷完成'  end as state
         FROM `dz_teacher_class` tc
         JOIN `learn_student` ls ON ls.`class_id` = tc.`class_id`
         JOIN `dz_classes` c ON ls.`class_id` = c.`class_id`
         LEFT JOIN `learn_test_student` ts ON ts.`batch_id` = #{batchId} AND ts.`student_id` = ls.`student_id` AND ts.`build_type` = #{buildType} AND ts.`subject_id` = #{subjectId}
         LEFT JOIN `sys_user` u ON u.`user_id` = ls.`student_id`
        WHERE tc.`teacher_id` = #{teacherId} and tc.`class_id` = #{classId}
         AND #{buildStatus} = case when ls.`major_plan_id` is null then 10 when ls.`major_plan_id` IS NOT NULL AND ts.`id` IS NULL then 20 when ts.`status` IN (1,2,3) then 30 else 40 end
    </select>

    <select id="selectSimulatedStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT ls.`class_id` `id`, c.`name`, COUNT(DISTINCT ls.`student_id`) `total`, COUNT(DISTINCT e2.`student_id`) `value`
          FROM `dz_teacher_class` tc
          JOIN `learn_student` ls ON ls.`class_id` = tc.`class_id`
          JOIN `dz_classes` c ON ls.`class_id` = c.`class_id`
          LEFT JOIN `learn_examinee` e2 ON ls.`student_id` = e2.`student_id` AND e2.`paper_type` = 20
        WHERE tc.`teacher_id` = #{teacherId}
        GROUP BY ls.`class_id`, c.`name`
        ORDER BY `value` DESC
    </select>

    <select id="selectSimulatedStudentStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT u.`user_id` `id`, u.`nick_name` `name`, COUNT(*) `total`
          FROM `learn_student` ls
          JOIN `sys_user` u ON ls.`student_id` = u.`user_id`
          JOIN `learn_examinee` e2 ON ls.`student_id` = e2.`student_id` AND e2.`paper_type` = 20
         WHERE ls.`class_id` = #{classId}
         GROUP BY u.`user_id`, u.`user_name`
         ORDER BY `total` DESC
    </select>

    <select id="selectSimulatedStuSubjectStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT e.`examinee_id` `id`, s.`subject_name` `name`, e.`score` `value`, p.`fenshu` `total`
          FROM `learn_examinee` e
          JOIN `learn_paper` p ON e.`paper_id` = p.`id`
          JOIN `dz_subject` s ON p.`subjectId` = s.`subject_id`
         WHERE e.`student_id` = #{studentId} AND e.`paper_type` = 20
    </select>

    <select id="selectExamRankingStats" parameterType="Map" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT e2.`paper_id`, COUNT(*) totalCount, AVG(e2.`score`) maxScore, AVG(e2.`score`) avgScore, COUNT(IF(e2.`score` &lt; #{stuScore}, 1, 0)) lowCount  FROM `learn_examinee` e2
        WHERE e2.`paper_type` = #{paperType} and e2.paper_id in <foreach item="id" collection="paperIds" open="(" separator="," close=")">#{id}</foreach>
        GROUP BY e2.`paper_id`
    </select>

    <select id="selectStudentPractices" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT e.`examinee_id` examineeId, e.`end_time` endTime, SUBSTRING_INDEX(SUBSTRING_INDEX(p.`paperName`, '-', 2), '-', -1) paperName, LENGTH(SUBSTRING_INDEX(SUBSTRING_INDEX(p.`direct_key`, '_', 2), '_', -1)) > 0 AS directed
          FROM `learn_examinee` e
          JOIN `learn_paper` p ON e.`paper_id` = p.`id`
         WHERE e.student_id = #{studentId} and e.`paper_type` = #{paperType} AND e.`state` &gt;= 4
        ORDER BY e.`end_time` DESC
    </select>

    <select id="selectLearnExamineeByExamineeId" parameterType="Long" resultMap="LearnExamineeResult">
        <include refid="selectLearnExamineeVo"/>
        where examinee_id = #{examineeId}
    </select>

    <insert id="insertLearnExaminee" parameterType="LearnExaminee" useGeneratedKeys="true" keyProperty="examineeId">
        insert into learn_examinee
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="studentId != null">student_id,</if>
            <if test="paperType != null">paper_type,</if>
            <if test="paperId != null">paper_id,</if>
            <if test="paperKey != null ">paper_key,</if>
            <if test="paperInfo != null ">paper_info,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="duration != null">duration,</if>
            <if test="stats != null">stats,</if>
            <if test="state != null">state,</if>
            <if test="scoreLevel != null">score_level,</if>
            <if test="score != null">score,</if>
            <if test="scoreRate != null">score_rate,</if>
            <if test="ranking != null">ranking,</if>
            <if test="gradeRanking != null">grade_ranking,</if>
            <if test="wrongCount != null">wrong_count,</if>
            <if test="chooseCount != null">choose_count,</if>
            <if test="chooseTotal != null">choose_total,</if>
            <if test="subjectiveCount != null">subjective_count,</if>
            <if test="subjectiveTotal != null">subjective_total,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="studentId != null">#{studentId},</if>
            <if test="paperType != null">#{paperType},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="paperKey != null">#{paperKey},</if>
            <if test="paperInfo != null">#{paperInfo},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="duration != null">#{duration},</if>
            <if test="stats != null">#{stats},</if>
            <if test="state != null">#{state},</if>
            <if test="scoreLevel != null">#{scoreLevel},</if>
            <if test="score != null">#{score},</if>
            <if test="scoreRate != null">#{scoreRate},</if>
            <if test="ranking != null">#{ranking},</if>
            <if test="gradeRanking != null">#{gradeRanking},</if>
            <if test="wrongCount != null">#{wrongCount},</if>
            <if test="chooseCount != null">#{chooseCount},</if>
            <if test="chooseTotal != null">#{chooseTotal},</if>
            <if test="subjectiveCount != null">#{subjectiveCount},</if>
            <if test="subjectiveTotal != null">#{subjectiveTotal},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateLearnExaminee" parameterType="LearnExaminee">
        update learn_examinee
        <trim prefix="SET" suffixOverrides=",">
            <if test="studentId != null">student_id = #{studentId},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="stats != null">stats = #{stats},</if>
            <if test="state != null">state = #{state},</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="ranking != null">ranking = #{ranking},</if>
            <if test="gradeRanking != null">grade_ranking = #{gradeRanking},</if>
            <if test="wrongCount != null">wrong_count = #{wrongCount},</if>
            <if test="chooseCount != null">choose_count = #{chooseCount},</if>
            <if test="chooseTotal != null">choose_total = #{chooseTotal},</if>
            <if test="subjectiveCount != null">subjective_count = #{subjectiveCount},</if>
            <if test="subjectiveTotal != null">subjective_total = #{subjectiveTotal},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where examinee_id = #{examineeId}
    </update>

    <delete id="deleteLearnExamineeByExamineeId" parameterType="Long">
        delete from learn_examinee where examinee_id = #{examineeId}
    </delete>

    <delete id="deleteLearnExamineeByExamineeIds" parameterType="String">
        delete from learn_examinee where examinee_id in 
        <foreach item="examineeId" collection="array" open="(" separator="," close=")">
            #{examineeId}
        </foreach>
    </delete>
</mapper>