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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.MarkPaper">
        <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="objective_score" property="objectiveScore"/>
        <result column="subjective_score" property="subjectiveScore"/>
        <result column="total_score" property="totalScore"/>
        <result column="upload_count" property="uploadCount"/>
        <result column="remark" property="remark"/>
        <result column="sheet_config" property="sheetConfig"/>
        <result column="pass_score" property="passScore"/>
        <result column="excellent_score" property="excellentScore"/>
        <result column="card_type" property="cardType"/>
        <result column="paper_file_path" property="paperFilePath"/>
        <result column="answer_file_path" property="answerFilePath"/>
        <result column="auto_scroll" property="autoScroll"/>
        <result column="mark_mode" property="markMode"/>
        <result column="mark_start_time" property="markStartTime"/>
        <result column="mark_end_time" property="markEndTime"/>
        <result column="sheet_view" property="sheetView"/>
        <result column="show_object_score" property="showObjectScore"/>
        <result column="open_mark_class" property="openMarkClass"/>
        <result column="status" property="status"/>
    </resultMap>
    <select id="listPaperSetting" resultType="com.qmth.teachcloud.common.bean.dto.mark.MarkSettingDto">
        SELECT
        mp.exam_id examId,
        mp.course_code courseCode,
        mp.course_name courseName,
        mp.paper_number paperNumber,
        mp.mark_mode markMode,
        mp.group_status groupStatus,
        mp.paper_type paperType
        FROM
        mark_paper mp
        LEFT JOIN
        sys_user su ON mp.user_id = su.id
        <where>
            <if test="schoolId != null">
                and su.school_id = #{schoolId}
            </if>
            <if test="examId != null">
                and mp.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and mp.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and mp.paper_number = #{paperNumber}
            </if>
            <if test="groupStatus != null">
                and mp.group_status = #{groupStatus}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    and mp.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
                </if>
                <if test="dpr.orgIdSet != null">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by mp.id desc
    </select>
    <select id="listQualityMarkPaperByStatus" resultType="com.qmth.teachcloud.mark.entity.MarkPaper">
        SELECT exam_id      examId,
               paper_number paperNumber
        FROM mark_paper mp
        WHERE mp.status = #{status}
          AND upload_count > #{uploadCount}
          AND EXISTS(SELECT 1
                     FROM basic_exam be
                     WHERE be.enable = 1
                       AND mp.exam_id = be.id)
    </select>
    <select id="listStudentScoreList" resultType="com.qmth.teachcloud.mark.dto.mark.score.CheckScoreListDto">
        SELECT
        mp.exam_id examId,
        mp.course_code courseCode,
        mp.course_name courseName,
        mp.paper_number paperNumber,
        mp.student_count studentCount,
        mp.upload_count uploadCount
        FROM
        mark_paper mp
        LEFT JOIN sys_user su ON mp.user_id = su.id
        where mp.exam_id = #{examId}
        <if test="courseCode != null and courseCode != ''">
            AND mp.course_code = #{courseCode}
        </if>
        <if test="paperNumber != null and paperNumber != ''">
            AND mp.paper_number = #{paperNumber}
        </if>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND mp.user_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.courseUserId != null">
                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
            </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 mp.id desc
    </select>
    <select id="listPackage" resultType="com.qmth.teachcloud.mark.dto.mark.score.MarkPaperPackageDto">
        SELECT distinct
        sp.package_code packageCode
        FROM
        scan_package sp
        WHERE
        sp.exam_id = #{examId}
        AND sp.course_paper_id = #{coursePaperId}
        <if test="packageCode != null and packageCode != ''">
            AND sp.package_code = #{packageCode}
        </if>
        ORDER BY sp.package_code
    </select>
    <select id="scoreList" resultType="com.qmth.teachcloud.mark.bean.archivescore.ArchiveScoreVo">
        SELECT t.id,t.exam_id,t.course_code,t.course_name,t.paper_number,
        count(*) studentCount,count(distinct s.class_name) classCount,
        sum(case when s.is_absent =1 or s.omr_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_paper t
        left join mark_student s on t.exam_id=s.exam_id and t.course_paper_id=s.course_paper_id
        left join (select * from basic_course where school_id = #{schoolId}) bc on s.course_code = bc.code
        WHERE
        t.exam_id = #{req.examId}
        <if test="req.courseCode != null and req.courseCode != ''">
            AND t.course_code = #{req.courseCode}
        </if>
        AND t.status = 'FINISH'
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND s.teacher_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.courseUserId != null">
                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{req.examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = t.course_code)
            </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>
        group by t.id,t.exam_id,t.course_code,t.course_name,t.paper_number
        ORDER BY t.id desc
    </select>
    <select id="getCourseCount" resultType="java.lang.Integer">
        select count(distinct mp.course_code) from mark_paper mp left join sys_user su on mp.user_id = su.id
        <where>
            and mp.exam_id = #{examId} AND mp.status = #{status}
            <if test="courseCode != null and courseCode != ''">
                AND mp.course_code = #{courseCode}
            </if>
            <if test="coursePaperId != null and coursePaperId != ''">
                AND mp.course_paper_id = #{coursePaperId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND mp.user_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>
        </where>
    </select>
    <select id="getPaperNumberCount" resultType="java.lang.Integer">
        select count(distinct mp.paper_number) from mark_paper mp left join sys_user su on mp.user_id = su.id
        <where>
            and mp.exam_id = #{examId} AND mp.status = #{status}
            <if test="courseCode != null and courseCode != ''">
                AND mp.course_code = #{courseCode}
            </if>
            <if test="coursePaperId != null and coursePaperId != ''">
                AND mp.course_paper_id = #{coursePaperId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND mp.user_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>
        </where>
    </select>
    <select id="listByExamId" resultType="com.qmth.teachcloud.mark.entity.MarkPaper">
        select mp.*
        from mark_paper mp
        left join sys_user su on mp.user_id = su.id
        <where>
            mp.exam_id = #{examId} AND mp.status = #{status}
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND mp.user_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>
        </where>
    </select>

    <select id="documentList" resultType="com.qmth.teachcloud.mark.bean.document.ArchivePaperVo">
        SELECT t.id,t.exam_id,t.course_code,t.course_name,t.paper_number
        FROM
        mark_paper t
        left join mark_student s on t.exam_id=s.exam_id and t.course_paper_id=s.course_paper_id
        left join sys_user su on t.user_id = su.id
        WHERE
        t.exam_id = #{req.examId}
        <if test="req.courseCode != null and req.courseCode != ''">
            AND t.course_code = #{req.courseCode}
        </if>
        AND t.status = 'FINISH'
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND t.user_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.courseUserId != null">
                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{req.examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = t.course_code)
            </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>
        group by t.id,t.exam_id,t.course_code,t.course_name,t.paper_number
        ORDER BY t.id desc
    </select>
</mapper>
