<?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.qmth.teachcloud.mark.mapper.MarkStudentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.MarkStudent">
        <id column="id" property="id"/>
        <result column="exam_id" property="examId"/>
        <result column="course_code" property="courseCode"/>
        <result column="course_name" property="courseName"/>
        <result column="paper_number" property="paperNumber"/>
        <result column="course_paper_id" property="coursePaperId"/>
        <result column="secret_number" property="secretNumber"/>
        <result column="student_code" property="studentCode"/>
        <result column="student_name" property="studentName"/>
        <result column="package_code" property="packageCode"/>
        <result column="exam_place" property="examPlace"/>
        <result column="exam_room" property="examRoom"/>
        <result column="remark" property="remark"/>
        <result column="batch_code" property="batchCode"/>
        <result column="sheet_count" property="sheetCount"/>
        <result column="answers" property="answers"/>
        <result column="is_upload" property="upload"/>
        <result column="is_absent" property="absent"/>
        <result column="is_manual_absent" property="manualAbsent"/>
        <result column="is_breach" property="breach"/>
        <result column="upload_time" property="uploadTime"/>
        <result column="check_time" property="checkTime"/>
        <result column="check_user_id" property="checkUserId"/>
        <result column="objective_score" property="objectiveScore"/>
        <result column="objective_score_list" property="objectiveScoreList"/>
        <result column="subjective_status" property="subjectiveStatus"/>
        <result column="subjective_score" property="subjectiveScore"/>
        <result column="subjective_score_list" property="subjectiveScoreList"/>
        <result column="college" property="college"/>
        <result column="class_name" property="className"/>
        <result column="teacher" property="teacher"/>
        <result column="scan_status" property="scanStatus"/>
        <result column="omr_absent_checked" property="omrAbsentChecked"/>
        <result column="question_filled" property="questionFilled"/>
        <result column="assigned" property="assigned"/>
        <result column="absent_suspect" property="absentSuspect"/>
        <result column="incomplete" property="incomplete"/>
        <result column="breach_code" property="breachCode"/>
        <result column="card_number" property="cardNumber"/>
    </resultMap>
    <select id="pageStudentScore" resultType="com.qmth.teachcloud.mark.dto.mark.score.StudentScoreDetailDto">
        SELECT
        ms.id studentId,
        ms.exam_id examId,
        ms.course_code courseCode,
        ms.course_name courseName,
        ms.paper_number paperNumber,
        ms.student_name studentName,
        ms.student_code studentCode,
        ms.secret_number secretNumber,
        ms.college,
        ms.major_name majorName,
        ms.class_name className,
        ms.objective_score objectiveScore,
        ms.subjective_score subjectiveScore,
        ifnull(ms.objective_score, 0) + ifnull(ms.subjective_score, 0) totalScore,
        ms.objective_score_list objectiveScoreList,
        ms.subjective_score_list subjectiveScoreList,
        ms.check_user_id checkUserId,
        su.login_name checkUserLoginName,
        su.real_name checkUserName,
        ms.check_time checkTime,
        ms.sheet_path sheetPath,
        ms.exam_start_time examStartTime,
        ms.exam_end_time examEndTime,
        ms.scan_status scanStatus,
        ms.subjective_status subjectiveStatus,
        ms.is_absent absent,
        ms.omr_absent omrAbsent,
        ms.is_upload upload
        FROM
        mark_student ms
        left join sys_user su on ms.check_user_id = su.id
        where ms.exam_id = #{examId}
        AND ms.paper_number = #{paperNumber}
        <if test="college != null and college != ''">
            AND ms.college LIKE CONCAT('%', #{college}, '%')
        </if>
        <if test="majorName != null and majorName != ''">
            AND ms.major_name LIKE CONCAT('%', #{majorName}, '%')
        </if>
        <if test="className != null and className != ''">
            AND ms.class_name LIKE CONCAT('%', #{className}, '%')
        </if>
        <if test="teacher != null and teacher != ''">
            AND ms.teacher LIKE CONCAT('%', #{teacher},'%')
        </if>
        <if test="filter != null">
            <choose>
                <when test="filter == 1">
                    AND ms.objective_score = 0
                </when>
                <when test="filter == 2">
                    AND ms.objective_score = 0 AND ms.subjective_score > 0
                </when>
                <when test="filter == 3">
                    AND ms.objective_score > 0 AND ms.subjective_score = 0
                </when>
            </choose>
        </if>
        <if test="status != null">
            <choose>
                <when test="status == 'ABSENT'">
                    AND (ms.is_absent = 1 OR ms.omr_absent = 1 OR ms.scan_status = 'MANUAL_ABSENT')
                </when>
                <when test="status == 'NORMAL'">
                    AND (ms.is_absent = 0 and ms.omr_absent = 0 and ms.scan_status = 'SCANNED' AND ms.is_upload = 1)
                </when>
                <otherwise>
                    AND (ms.scan_status = 'UNEXIST')
                </otherwise>
            </choose>
        </if>
        <if test="breach != null">
            AND ms.is_breach = #{breach}
        </if>
        <if test="startScore != null">
            <choose>
                <when test="startScore == 0">
                    AND (ms.is_absent = 1 OR ms.is_breach = 1 OR ((ifnull(ms.subjective_score, 0) +
                    ifnull(ms.objective_score, 0)) &gt;= #{startScore} AND (ifnull(ms.subjective_score,0) +
                    ifnull(ms.objective_score,0)) &lt;= #{endScore}))
                </when>
                <otherwise>
                    AND (ms.is_absent = 0 AND ms.is_breach = 0 AND ((ifnull(ms.subjective_score,0) +
                    ifnull(ms.objective_score,0)) &gt;= #{startScore} AND (ifnull(ms.subjective_score,0) +
                    ifnull(ms.objective_score,0)) &lt;= #{endScore}))
                </otherwise>
            </choose>
        </if>
        <if test="subjectiveStartScore != null">
            <choose>
                <when test="subjectiveStartScore == 0">
                    AND (ms.is_absent = 1 OR ms.is_breach = 1
                             OR ((ifnull(ms.subjective_score, 0) &gt;= #{subjectiveStartScore}
                             AND ifnull(ms.subjective_score, 0) &lt;= #{subjectiveEndScore})))
                </when>
                <otherwise>
                    AND (ms.is_absent = 0 AND ms.is_breach = 0
                             AND ((ifnull(ms.subjective_score, 0) &gt;= #{subjectiveStartScore}
                            AND ifnull(ms.subjective_score, 0) &lt;= #{subjectiveEndScore})))
                </otherwise>
            </choose>
        </if>
        <if test="objectiveStartScore != null">
            <choose>
                <when test="objectiveStartScore == 0">
                    AND (ms.is_absent = 1 OR ms.is_breach = 1
                        OR ((ifnull(ms.objective_score, 0) &gt;= #{objectiveStartScore}
                        AND ifnull(ms.objective_score, 0) &lt;= #{objectiveEndScore})))
                </when>
                <otherwise>
                    AND (ms.is_absent = 0 AND ms.is_breach = 0
                        AND ((ifnull(ms.objective_score, 0) &gt;= #{objectiveStartScore}
                        AND ifnull(ms.objective_score, 0) &lt;= #{objectiveEndScore})))
                </otherwise>
            </choose>
        </if>
        <if test="subScore != null">
            AND exists (SELECT 1 FROM mark_subjective_score mss WHERE ms.id = mss.student_id AND mss.score =
            #{subScore})
        </if>
        <if test="objectiveScoreLt != null">
            AND ms.objective_score &lt; #{objectiveScoreLt}
        </if>
        <if test="studentName != null and studentName != ''">
            AND ms.student_name like concat(#{studentName}, '%')
        </if>
        <if test="studentCode != null and studentCode != ''">
            AND ms.student_code = #{studentCode}
        </if>
        <if test="orderType != null and orderType != '' and orderField != null and orderField != ''">
            <choose>
                <when test="orderField == 'totalScore'">
                    <if test="orderType == 'ASC'">
                        order by (ifnull(ms.objective_score, 0) + ifnull(ms.subjective_score, 0)) ASC
                    </if>
                    <if test="orderType == 'DESC'">
                        order by (ifnull(ms.objective_score, 0) + ifnull(ms.subjective_score, 0)) DESC
                    </if>
                </when>
                <otherwise>
                    order by #{orderField} #{orderType}
                </otherwise>
            </choose>
        </if>
        <if test="orderType == null or orderType == '' or orderField == null or orderField == ''">
            order by ms.student_code
        </if>
    </select>
    <select id="listAbsentOrBreachMarkTaskStudent" resultMap="BaseResultMap">
        SELECT *
        FROM mark_student ms
        WHERE ms.exam_id = #{examId}
          AND ms.paper_number = #{paperNumber}
          AND ms.is_upload = TRUE
          AND (is_absent = TRUE OR is_breach = TRUE OR omr_absent = TRUE)
          AND EXISTS(SELECT 1
                     FROM mark_task mt
                     WHERE ms.id = mt.student_id)
    </select>
    <select id="listUnMarkTaskStudent" resultMap="BaseResultMap">
        SELECT *
        FROM mark_student ms
        WHERE ms.exam_id = #{examId}
          AND ms.paper_number = #{paperNumber}
          AND ms.is_upload = TRUE
          AND is_absent = FALSE
          AND is_breach = FALSE
          AND omr_absent = FALSE
          AND NOT EXISTS(SELECT 1
                         FROM mark_task mt
                         WHERE ms.id = mt.student_id
                           AND mt.group_number = #{groupNumber})
    </select>
    <sql id="queryWhereAndOrder">
        where t.exam_id=#{query.examId}
        <if test="query.studentId != null and query.studentId !=''">
            and t.id=#{query.studentId}
        </if>
        <if test="query.status != null and query.status.size>0">
            AND t.scan_status in
            <foreach collection="query.status" index="index" item="st"
                     open="(" separator="," close=")">
                #{st}
            </foreach>
        </if>
        <if test="query.studentCode != null and query.studentCode !=''">
            and t.student_code=#{query.studentCode}
        </if>
        <if test="query.studentName != null and query.studentName !=''">
            and t.student_name=#{query.studentName}
        </if>
        <if test="query.packageCode != null and query.packageCode !=''">
            and t.package_code=#{query.packageCode}
        </if>
        <if test="query.courseCode != null and query.courseCode !=''">
            and t.course_code=#{query.courseCode}
        </if>
        <if test="query.coursePaperId != null and query.coursePaperId !=''">
            and t.course_paper_id=#{query.coursePaperId}
        </if>
        <if test="query.coursePaperNumber != null and query.coursePaperNumber !=''">
            and t.paper_number=#{query.coursePaperNumber}
        </if>
        <if test="query.examPlace != null and query.examPlace !=''">
            and t.exam_place=#{query.examPlace}
        </if>
        <if test="query.examRoom != null and query.examRoom !=''">
            and t.exam_room=#{query.examRoom}
        </if>

        <if test="query.absentSuspect != null">
            and t.absent_suspect=#{query.absentSuspect}
        </if>
        <if test="query.omrAbsent != null">
            and t.omr_absent=#{query.omrAbsent}
        </if>
        <if test="query.omrAbsentChecked != null">
            and t.omr_absent_checked=#{query.omrAbsentChecked}
        </if>
        <if test="query.assigned != null">
            and t.assigned=#{query.assigned}
        </if>
        <if test="query.assignConfirmed != null">
            and t.assign_confirmed=#{query.assignConfirmed}
        </if>
        <if test="query.invalid != null">
            and t.invalid=#{query.invalid}
        </if>
        <if test="query.incomplete != null">
            and t.incomplete=#{query.incomplete}
        </if>
        <if test="query.questionFilled != null">
            and t.question_filled=#{query.questionFilled}
        </if>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND t.create_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND su.org_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        <if test="query.markPaperStatus != null">
            and s.status = #{query.markPaperStatus}
        </if>
    </sql>
    <select id="queryPage"
            resultType="com.qmth.teachcloud.mark.bean.scananswer.AnswerQueryVo">
        select t.*,t.scan_status status,t.paper_number coursePaperNumber,c.paper_count cardPaperCount
        from mark_student t
        left join scan_answer_card c on t.exam_id=c.exam_id and t.card_number=c.number
        left join mark_paper s on s.course_paper_id=t.course_paper_id and s.exam_id=t.exam_id
        left join sys_user su on t.create_id = su.id
        <include refid="queryWhereAndOrder"/>
        order by t.id
    </select>
    <select id="querySummary" resultType="java.lang.String">
        select
        t.id
        from mark_student t
        left join mark_paper s on s.course_paper_id=t.course_paper_id and s.exam_id=t.exam_id
        left join sys_user su on t.create_id = su.id
        <include refid="queryWhereAndOrder"/>
        order by t.id
    </select>
    <select id="findIdByExamIdAndPaperNumber" resultType="java.lang.Long">
        select id
        from mark_student
        where exam_id = #{examId}
          and paper_number = #{paperNumber}
    </select>
    <select id="studentList"
            resultType="com.qmth.teachcloud.mark.bean.archivescore.ArchiveStudentVo">
        select s.*,s.id studentId, ifnull(s.objective_score,0)+ifnull(s.subjective_score,0) totalScore
        from mark_student s left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
        <include refid="archiveQuery"/>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND s.teacher_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND bc.teaching_room_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        <if test="req.orderType != null and req.orderType != '' and req.orderField != null and req.orderField != ''">
            <choose>
                <when test="req.orderField == 'totalScore'">
                    <if test="req.orderType == 'ASC'">
                        order by (ifnull(s.objective_score, 0) + ifnull(s.subjective_score, 0)) ASC
                    </if>
                    <if test="req.orderType == 'DESC'">
                        order by (ifnull(s.objective_score, 0) + ifnull(s.subjective_score, 0)) DESC
                    </if>
                </when>
                <otherwise>
                    order by #{req.orderField} #{req.orderType}
                </otherwise>
            </choose>
        </if>
        <if test="req.orderType == null or req.orderType == '' or req.orderField == null or req.orderField == ''">
            order by s.student_code
        </if>
    </select>
    <sql id="archiveQuery">
        where s.exam_id=#{req.examId} and s.paper_number=#{req.paperNumber}
        <if test="req.studentCode != null and req.studentCode !=''">
            and s.student_code=#{req.studentCode}
        </if>
        <if test="req.startStudentCode != null and req.startStudentCode !=''">
            and s.student_code &gt;= #{req.startStudentCode}
        </if>
        <if test="req.endStudentCode != null and req.endStudentCode !=''">
            and s.student_code &lt;= #{req.endStudentCode}
        </if>
        <if test="req.studentName != null and req.studentName !=''">
            and s.student_name=#{req.studentName}
        </if>
        <if test="req.college != null and req.college !=''">
            and s.college like concat(#{req.college}, '%')
        </if>
        <if test="req.majorName != null and req.majorName !=''">
            and s.major_name like concat(#{req.majorName}, '%')
        </if>
        <if test="req.className != null and req.className !=''">
            and s.class_name like concat(#{req.className}, '%')
        </if>
        <if test="req.startScore != null and req.startScore !=''">
            and (ifnull(s.objective_score,0)+ifnull(s.subjective_score,0)) &gt;= #{req.startScore}
        </if>
        <if test="req.endScore != null and req.endScore !=''">
            and (ifnull(s.objective_score,0)+ifnull(s.subjective_score,0)) &lt;= #{req.endScore}
        </if>
        <if test="req.objectiveStartScore != null and req.objectiveStartScore !=''">
            and ifnull(s.objective_score,0) &gt;= #{req.objectiveStartScore}
        </if>
        <if test="req.objectiveEndScore != null and req.objectiveEndScore !=''">
            and ifnull(s.objective_score,0) &lt;= #{req.objectiveEndScore}
        </if>
        <if test="req.subjectiveStartScore != null and req.subjectiveStartScore !=''">
            and ifnull(s.subjective_score,0) &gt;= #{req.subjectiveStartScore}
        </if>
        <if test="req.subjectiveEndScore != null and req.subjectiveEndScore !=''">
            and ifnull(s.subjective_score,0) &lt;= #{req.subjectiveEndScore}
        </if>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND s.teacher_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND bc.teaching_room_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
    </sql>
    <select id="overview" resultType="com.qmth.teachcloud.mark.bean.archivescore.OverViewVo">
        SELECT count(*)                                                                                 studentCount,
               sum(case
                       when s.is_absent = 0 and s.omr_absent = 0 and s.is_breach = 0 and s.is_upload = 1 then 1
                       else 0 end)                                                                      actualCount,
               sum(case when s.is_absent = 1 or s.omr_absent = 1 then 1 else 0 end)                     absentCount,
               sum(case when s.is_breach = 1 then 1 else 0 end)                                         breachCount,
               avg(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) avgScore,
               max(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) maxScore,
               min(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) minScore,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.pass_score / 100 then 1
                       else 0 end)                                                                      passCount,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.excellent_score / 100 then 1
                       else 0 end)                                                                      excellentCount
        FROM mark_student s
                 left join mark_paper t on s.exam_id = t.exam_id
            and s.paper_number = t.paper_number
            and s.paper_type = t.paper_type
        left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
        <include refid="archiveQuery"/>
    </select>
    <select id="getCountByScoreRange" resultType="int">
        SELECT count(*)
        FROM mark_student s
        WHERE s.exam_id = #{examId}
          and s.paper_number = #{paperNumber}
          and s.is_absent !=1 and s.objective_score+s.subjective_score&gt;=#{start}
          and s.objective_score+s.subjective_score&lt;=#{end}
    </select>
    <select id="college" resultType="com.qmth.teachcloud.mark.bean.archivescore.CollegeVo">
        SELECT s.college,
               count(*)                                                                                 studentCount,
               sum(case when s.is_absent = 1 then 1 else 0 end)                                         absentCount,
               avg(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) avgScore,
               max(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) maxScore,
               min(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) minScore,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.pass_score / 100 then 1
                       else 0 end)                                                                      passCount,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.excellent_score / 100 then 1
                       else 0 end)                                                                      excellentCount
        FROM mark_student s
                 left join mark_paper t on s.exam_id = t.exam_id
            and s.paper_number = t.paper_number
            and s.paper_type = t.paper_type
        left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
            <include refid="archiveQuery"/>
            and s.college is not null
        group by s.college
    </select>
    <select id="classData" resultType="com.qmth.teachcloud.mark.bean.archivescore.ClassVo">
        SELECT s.class_name,
               count(*)                                                                                 studentCount,
               sum(case when s.is_absent = 1 then 1 else 0 end)                                         absentCount,
               avg(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) avgScore,
               max(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) maxScore,
               min(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) minScore,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.pass_score / 100 then 1
                       else 0 end)                                                                      passCount,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.excellent_score / 100 then 1
                       else 0 end)                                                                      excellentCount
        FROM mark_student s
                 left join mark_paper t on s.exam_id = t.exam_id
            and s.paper_number = t.paper_number
            and s.paper_type = t.paper_type
        left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
            <include refid="archiveQuery"/>
            and s.class_name is not null
        group by s.class_name
    </select>
    <select id="teacher" resultType="com.qmth.teachcloud.mark.bean.archivescore.TeacherVo">
        SELECT s.teacher,
               count(*)                                                                                 studentCount,
               sum(case when s.is_absent = 1 then 1 else 0 end)                                         absentCount,
               avg(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) avgScore,
               max(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) maxScore,
               min(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) minScore,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.pass_score / 100 then 1
                       else 0 end)                                                                      passCount,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.excellent_score / 100 then 1
                       else 0 end)                                                                      excellentCount
        FROM mark_student s
                 left join mark_paper t on s.exam_id = t.exam_id
            and s.paper_number = t.paper_number
            and s.paper_type = t.paper_type
        left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
            <include refid="archiveQuery"/>
            and s.teacher is not null
        group by s.teacher
    </select>
    <select id="findOne" resultType="com.qmth.teachcloud.mark.bean.student.StudentVo"
            parameterType="com.qmth.teachcloud.mark.bean.student.StudentQuery">
        select *
        from mark_student
        where exam_id = #{query.examId}
          and course_paper_id = #{query.coursePaperId}
          and student_code = #{query.studentCode}
    </select>
    <select id="listUnexistStudentByExamIdAndCoursePaperId"
            resultType="com.qmth.teachcloud.mark.dto.UnexistStudentDto">
        SELECT
        t.course_code courseCode,
        t.course_name courseName,
        t.paper_number paperNumber,
        t.student_code studentCode,
        t.student_name studentName
        FROM
        mark_student t left join sys_user su on t.create_id = su.id
        WHERE
        t.exam_id = #{examId}
        AND t.scan_status = 'UNEXIST'
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        <if test="coursePaperId != null and coursePaperId != ''">
            and t.course_paper_id = #{coursePaperId}
        </if>
        <if test="status != null and status != ''">
            and exists(select 1 from mark_paper mp where t.exam_id = mp.exam_id and t.course_paper_id =
            mp.course_paper_id and t.paper_type = mp.paper_type and mp.status = #{status})
        </if>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND t.create_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND su.org_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        order by t.student_code
    </select>
    <select id="teacherClass" resultType="com.qmth.teachcloud.mark.bean.archivescore.TeacherClassVo">
        SELECT s.teacher,
               s.class_name,
               count(*)                                                                                 studentCount,
               sum(case when s.is_absent = 1 then 1 else 0 end)                                         absentCount,
               avg(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) avgScore,
               max(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) maxScore,
               min(case when s.is_absent !=1 then ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) else null end) minScore,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.pass_score / 100 then 1
                       else 0 end)                                                                      passCount,
               sum(case
                       when ifnull(s.objective_score,0) + ifnull(s.subjective_score,0) >= t.total_score * t.excellent_score / 100 then 1
                       else 0 end)                                                                      excellentCount
        FROM mark_student s
                 left join mark_paper t on s.exam_id = t.exam_id
            and s.paper_number = t.paper_number
            and s.paper_type = t.paper_type
        left join sys_user su on s.teacher_id = su.id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
            <include refid="archiveQuery"/>
            and s.class_name is not null
        group by s.teacher, s.class_name
    </select>
    <select id="selectCountByQuery" resultType="java.lang.Integer">
        select count(1) from mark_student ms left join sys_user su on ms.create_id = su.id
        <where>
            and ms.exam_id = #{markStudent.examId}
            <if test="markStudent.courseCode != null and markStudent.courseCode != ''">
                and ms.course_code = #{markStudent.courseCode}
            </if>
            <if test="markStudent.paperNumber != null and markStudent.paperNumber != ''">
                and ms.paper_number = #{markStudent.paperNumber}
            </if>
            <if test="markStudent.paperType != null and markStudent.paperType != ''">
                and ms.paper_type = #{markStudent.paperType}
            </if>
            <if test="markStudent.coursePaperId != null and markStudent.coursePaperId != ''">
                and ms.course_paper_id = #{markStudent.coursePaperId}
            </if>
            <if test="markStudent.scanStatus != null">
                and ms.scan_status = #{markStudent.scanStatus}
            </if>
            <if test="markStudent.omrAbsent != null">
                and ms.omr_absent = #{markStudent.omrAbsent}
            </if>
            <if test="markStudent.omrAbsentChecked != null">
                and ms.omr_absent_checked = #{markStudent.omrAbsentChecked}
            </if>
            <if test="markStudent.incomplete != null">
                and ms.incomplete = #{markStudent.incomplete}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND ms.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            <if test="markStudent.markPaperStatus != null">
                AND exists(select 1 from mark_paper mp where ms.exam_id = mp.exam_id and ms.paper_number =
                mp.paper_number and ms.paper_type = mp.paper_type and mp.status = #{markStudent.markPaperStatus})
            </if>
        </where>
    </select>
    <select id="countAssigned" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        mark_student ms
        LEFT JOIN sys_user su ON ms.create_id = su.id
        <where>
            ms.exam_id = #{markStudent.examId}
            <if test="markStudent.courseCode != null">
                AND ms.course_code = #{markStudent.courseCode}
            </if>
            <if test="markStudent.coursePaperId != null">
                AND ms.course_paper_id = #{markStudent.coursePaperId}
            </if>
            <if test="markStudent.assigned != null">
                AND ms.assigned = #{markStudent.assigned}
            </if>
            <if test="markStudent.assignConfirmed != null">
                AND ms.assign_confirmed = #{markStudent.assignConfirmed}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND ms.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            <if test="markStudent.markPaperStatus != null">
                AND exists(select 1 from mark_paper mp where ms.exam_id = mp.exam_id and ms.paper_number =
                mp.paper_number and ms.paper_type = mp.paper_type and mp.status = #{markStudent.markPaperStatus})
            </if>
        </where>
    </select>
    <select id="listScanCollegeByExamIdAndCourseCodeAndCoursePaperId"
            resultType="com.qmth.teachcloud.mark.entity.MarkStudent">
        SELECT
        distinct ms.exam_place, ms.exam_room
        FROM
        mark_student ms
        LEFT JOIN sys_user su ON ms.create_id = su.id
        <where>
            ms.exam_id = #{examId}
            <if test="courseCode != null and courseCode != ''">
                AND ms.course_code = #{courseCode}
            </if>
            <if test="coursePaperId != null and coursePaperId != ''">
                AND ms.course_paper_id = #{coursePaperId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND ms.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            <if test="status != null">
                AND exists(select 1 from mark_paper mp where ms.exam_id = mp.exam_id and ms.paper_number =
                mp.paper_number and ms.paper_type = mp.paper_type and mp.status = #{status})
            </if>
        </where>
    </select>
    <select id="getBasicTeachClazzById" resultType="com.qmth.teachcloud.common.entity.BasicTeachClazz"
            parameterType="java.lang.Long">
        select * from basic_teach_clazz where id = #{clazzId}
    </select>
    <select id="maxCardNumber" resultType="java.lang.Integer">
        select max(card_number) from mark_student where exam_id = #{examId} and paper_number = #{paperNumber} and paper_type = #{paperType} and card_number is not null
    </select>

</mapper>
