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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.ScanOmrTask">
        <id column="id" property="id" />
        <result column="exam_id" property="examId" />
        <result column="conditions" property="conditions" />
        <result column="student_id" property="studentId" />
        <result column="paper_index" property="paperIndex" />
        <result column="paper_id" property="paperId" />
        <result column="card_number" property="cardNumber" />
        <result column="status" property="status" />
        <result column="user_id" property="userId" />
        <result column="creator_id" property="creatorId" />
        <result column="updater_id" property="updaterId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="pages" property="pages"
			typeHandler="com.qmth.teachcloud.mark.handle.OmrTaskPageListTypeHandler" />
    </resultMap>
    
    <select id="findUnMarked" resultType="com.qmth.teachcloud.mark.entity.ScanOmrTask">
		select *
		from scan_omr_task t
		where t.exam_id=#{examId}
		and
		t.status=#{status}
		order by t.id
		limit
		#{pageNumber},#{pageSize}
	</select>
	
	<select id="listByExamIdAndStatusAndUserId" resultType="com.qmth.teachcloud.mark.dto.ScanStudentDto">
		select 
			s.id studentId,
			s.student_name,
			s.student_code
		from mark_student s left join sys_user su on s.create_id = su.id
			<where>
				<if test="examId != null">
					and s.exam_id = #{examId}
				</if>
				<if test="courseCode != null and courseCode != ''">
					and s.course_code = #{courseCode}
				</if>
				<if test="coursePaperId != null and coursePaperId != ''">
					and s.course_paper_id = #{coursePaperId}
				</if>
					and exists (select 1 from mark_paper mp where s.exam_id = mp.exam_id and s.course_paper_id = mp.course_paper_id and s.paper_type = mp.paper_type and mp.status = #{markPaperStatus})
					and exists(select 1
						from scan_omr_task t
						where s.id = t.student_id
						and t.status=#{status}
						<if test="dpr != null">
							<if test="dpr.requestUserId != null">
								AND s.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>
				)
			</where>
	</select>
	
	<select id="getStudentCountByExamAndStatusAndUserId" resultType="int">
		select
			count(1)
		from mark_student s left join sys_user su on s.create_id = su.id
		<where>
			<if test="examId != null">
				and s.exam_id = #{examId}
			</if>
			<if test="courseCode != null and courseCode != ''">
				and s.course_code = #{courseCode}
			</if>
			<if test="coursePaperId != null and coursePaperId != ''">
				and s.course_paper_id = #{coursePaperId}
			</if>
			and exists (select 1 from mark_paper mp where s.exam_id = mp.exam_id and s.course_paper_id = mp.course_paper_id and s.paper_type = mp.paper_type and mp.status = #{markPaperStatus})
			and exists(select 1
			from scan_omr_task t
			where s.id = t.student_id
			and t.status=#{status}
			<if test="dpr != null">
				<if test="dpr.requestUserId != null">
					AND s.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>
			)
		</where>
	</select>

    <select id="countOmrTask" resultType="java.lang.Integer">
		SELECT
			COUNT(distinct  sot.student_id)
		FROM
			scan_omr_task sot
		<where>
			sot.exam_id = #{scanOmrTask.examId}
			<if test="scanOmrTask.status != null">
				AND sot.status = #{scanOmrTask.status}
			</if>
			<if test="scanOmrTask.courseCode != null or scanOmrTask.coursePaperId != null or dpr != null or scanOmrTask.markPaperStatus != null">
				AND EXISTS( SELECT
						1
					FROM
						mark_student ms
							left join sys_user su on ms.create_id = su.id
					WHERE
						sot.student_id = ms.id
						<if test="scanOmrTask.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 = #{scanOmrTask.markPaperStatus})
						</if>
						<if test="scanOmrTask.courseCode != null">
							AND ms.course_code = #{scanOmrTask.courseCode}
						</if>
						<if test="scanOmrTask.coursePaperId != null">
							AND ms.course_paper_id = #{scanOmrTask.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>
		</where>
	</select>

</mapper>
