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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.MarkTask">
        <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="group_number" property="groupNumber" />
        <result column="student_id" property="studentId" />
        <result column="student_code" property="studentCode" />
        <result column="secret_number" property="secretNumber" />
        <result column="task_number" property="taskNumber" />
        <result column="status" property="status" />
        <result column="user_id" property="userId" />
        <result column="marker_time" property="markerTime" />
        <result column="marker_score" property="markerScore" />
        <result column="marker_score_list" property="markerScoreList" />
        <result column="marker_spent" property="markerSpent" />
        <result column="header_id" property="headerId" />
        <result column="header_time" property="headerTime" />
        <result column="header_score" property="headerScore" />
        <result column="header_score_list" property="headerScoreList" />
    </resultMap>
    <select id="listPaperManage" resultType="com.qmth.teachcloud.mark.dto.mark.manage.MarkManageDto">
        SELECT
            mp.exam_id examId,
            mp.course_code courseCode,
            mp.course_name courseName,
            mp.paper_number paperNumber,
            mp.paper_type paperType,
            mp.status,
            ifnull(b.totalCount, 0) totalCount,
            ifnull(b.markedCount, 0) markedCount
        FROM mark_paper mp
            left join
            (select
                 ms.exam_id,
                 ms.paper_number ,
                 ms.paper_type,
                 count(1) totalCount,
                 sum(case ms.subjective_status when 'MARKED' then 1 else 0 end) markedCount
             from mark_student ms where ms.exam_id = #{examId}
                <if test="courseCode != null and courseCode != ''">
                    and ms.course_code = #{courseCode}
                </if>
                <if test="paperNumber != null and paperNumber != ''">
                    and ms.paper_number = #{paperNumber}
                </if>
                and ms.is_upload = true and ms.is_absent = false and ms.is_breach = false and ms.omr_absent = false
                group by ms.exam_id, ms.paper_number, ms.paper_type) b
                    on mp.exam_id = b.exam_id and mp.paper_number = b.paper_number and mp.paper_type = b.paper_type
            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 su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            <if test="progressStatus != null">
                <if test="progressStatus == true">
                    and ifnull(b.totalCount, 0) > 0 AND ifnull(b.totalCount, 0) = ifnull(b.markedCount, 0)
                </if>
                <if test="progressStatus == false">
                    and ifnull(b.totalCount, 0) = 0 OR ifnull(b.totalCount, 0) > ifnull(b.markedCount, 0)
                </if>
            </if>
        </where>
        order by mp.id desc
    </select>
    <select id="listUserMarkedCount"
            resultType="com.qmth.teachcloud.mark.dto.mark.manage.MarkerInfoDto">
        SELECT
            mt.exam_id examId,
            mt.course_code courseCode,
            mt.course_name courseName,
            mt.paper_number paperNumber,
            mt.group_number groupNumber,
            su.login_name loginName,
            su.code empno,
            su.real_name realName,
            COUNT(1) markedCount
        FROM
            mark_task mt
                LEFT JOIN
            sys_user su ON mt.user_id = su.id
        <where>
            mt.exam_id = #{examId}
            <if test="courseCode != null and courseCode != ''">
                and mt.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and mt.paper_number = #{paperNumber}
            </if>
        </where>
        GROUP BY mt.exam_id , mt.course_code , mt.course_name , mt.paper_number , mt.group_number , su.login_name , su.code , su.real_name
    </select>
    <select id="getLastOneByUserIdAndStatus" resultType="com.qmth.teachcloud.mark.entity.MarkTask">
        SELECT
            exam_id examId,
            paper_number paperNumber,
            group_number groupNumber,
            user_id userId,
            marker_time markerTime
        FROM
            mark_task
        WHERE
            exam_id = #{examId}
          AND paper_number = #{paperNumber}
          AND group_number = #{groupNumber}
          AND user_id = #{userId}
          AND status = #{status}
        ORDER BY marker_time DESC
            LIMIT 1
    </select>
	<select id="listPageHistory" resultType="com.qmth.teachcloud.mark.entity.MarkTask">
        SELECT
        	*
        FROM
            mark_task mt
        <where>
			mt.exam_id = #{examId}
		and mt.paper_number = #{paperNumber}
		and mt.group_number = #{groupNumber}
		and mt.user_id = #{userId}
		and mt.status = 'MARKED'
            <if test="secretNumber != null and secretNumber != '' ">
                and mt.secret_number = #{secretNumber}
			</if>
			<if test="markerScore != null">
                and mt.marker_score = #{markerScore}
			</if>
        </where>
    </select>
    
    <select id="findUnMarked" resultType="com.qmth.teachcloud.mark.entity.MarkTask">
        SELECT
        	*
        FROM
            mark_task mt
        <where>
        	mt.exam_id = #{examId}
		and mt.paper_number = #{paperNumber}
		and mt.group_number = #{groupNumber}
		and (mt.user_id is null or mt.user_id =#{userId})
		and (mt.status='WAITING' or mt.status='REJECTED')
		AND NOT EXISTS ( 
			SELECT t.id FROM
			mark_task t
			WHERE
			t.student_id = mt.student_id
            AND t.exam_id = mt.exam_id
            AND t.paper_number = mt.paper_number
            AND t.group_number = mt.group_number
			AND t.user_id = #{userId}
			AND t.marker_score IS NOT NULL
		)
        </where>
    </select>

    <select id="findUnMarkedFilterClass" resultType="com.qmth.teachcloud.mark.entity.MarkTask">
        SELECT
        	*
        FROM
            mark_task mt
        <where>
        	mt.exam_id = #{examId}
		and mt.paper_number = #{paperNumber}
		and mt.group_number = #{groupNumber}
		and (mt.user_id is null or mt.user_id =#{userId})
		and (mt.status='WAITING' or mt.status='REJECTED')
		AND NOT EXISTS ( 
			SELECT t.id 
			FROM
			mark_task t
			WHERE
			t.student_id = mt.student_id
            AND t.exam_id = mt.exam_id
            AND t.paper_number = mt.paper_number
            AND t.group_number = mt.group_number
			AND t.id != mt.id
			AND t.user_id = #{userId}
			AND t.marker_score IS NOT NULL
		)
		AND EXISTS (
			SELECT 1
			FROM
			(select * from mark_user_class where exam_id = #{examId} and paper_number = #{paperNumber} and user_id = #{userId}) mc
                join
			(select * from mark_student where exam_id = #{examId} and paper_number = #{paperNumber}) s on mc.class_name = s.class_name
			WHERE mt.student_id = s.id
		)
        </where>
    </select>
    <select id="listByExamIdAndPaperNumberAndGroupNumberAndUserIdAndClassName"
            resultType="com.qmth.teachcloud.mark.entity.MarkTask">
        SELECT
            *
        FROM
            mark_task mt
        WHERE
            mt.exam_id = #{examId}
          AND mt.paper_number = #{paperNumber}
          <if test="groupNumber != null">
              AND mt.group_number = #{groupNumber}
          </if>
          <if test="userId != null">
              AND mt.user_id = #{userId}
          </if>
          <if test="className != null and className != ''">
              AND EXISTS( SELECT
                1
              FROM
              (select id, exam_id, paper_number, class_name
              from mark_student
              where exam_id = #{examId}
              and paper_number = #{paperNumber}
              and class_name = #{className}) ms
              WHERE
              mt.student_id = ms.id)
          </if>
    </select>
    <select id="countByExamIdAndPaperNumberAndGroupNumberAndUserIdAndAndClassNameStatusIn" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            mark_task mt
        WHERE
            mt.exam_id = #{examId}
            AND mt.paper_number = #{paperNumber}
        <if test="groupNumber != null">
            AND mt.group_number = #{groupNumber}
        </if>
        <if test="userId != null">
            AND mt.user_id = #{userId}
        </if>
        <if test="statusList != null and statusList.length > 0">
            and mt.status in
          <foreach collection="statusList" item="status" separator="," open="(" close=")">
              #{status}
          </foreach>
        </if>
        <if test="classNames != null and classNames.size() > 0">
            AND EXISTS( SELECT
            1
            FROM
            (select id, exam_id, paper_number, class_name
            from mark_student
            where exam_id = #{examId}
            and paper_number = #{paperNumber}
            and class_name in
            <foreach collection="classNames" item="className" separator="," open="(" close=")">
                #{className}
            </foreach>) ms
            WHERE
            mt.student_id = ms.id)
        </if>
    </select>
</mapper>
