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

	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap"
		type="com.qmth.teachcloud.mark.entity.ScanPackage">
		<id column="id" property="id" />
		<result column="exam_id" property="examId" />
		<result column="course_paper_id" property="coursePaperId" />
		<result column="package_code" property="packageCode" />
		<result column="package_no" property="packageNo" />
		<result column="path" property="path" />
		<result column="md5" property="md5" />
		<result column="assigned" property="assigned" />
		<result column="creator_id" property="creatorId" />
		<result column="updater_id" property="updaterId" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
	</resultMap>
	<select id="page"
		resultType="com.qmth.teachcloud.mark.bean.scanpackage.ScanPackageVo">
		select t.*,t.paper_number coursePaperNumber
		FROM mark_paper_package t
        where t.exam_id=#{req.examId}
        <if test="req.courseCode != null and req.courseCode != ''">
			and t.course_code = #{req.courseCode}
		</if>
		<if test="req.coursePaperId != null and req.coursePaperId != ''">
			and t.course_paper_id= #{req.coursePaperId}
		</if>
        <if test="req.status != null">
			<if test="req.status ==@com.qmth.teachcloud.common.enums.ScanStatus@SCANNED">
				and exists (
				select 1 from scan_package f where t.exam_id=f.exam_id and t.course_paper_id=f.course_paper_id
				)
			</if>
			<if test="req.status ==@com.qmth.teachcloud.common.enums.ScanStatus@UNEXIST">
				and not exists (
				select 1 from scan_package f where t.exam_id=f.exam_id and t.course_paper_id=f.course_paper_id
				)
			</if>
		</if>
		<if test="dpr != null">
          	and exists (select 1 from mark_paper mp left join sys_user su on mp.user_id = su.id 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 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>
		ORDER BY t.package_code
	</select>
    <select id="countPackageCode" resultType="java.lang.Integer">
		SELECT
			count(distinct sp.package_code)
		FROM
			scan_package sp
				JOIN
			mark_paper_package mpp ON sp.exam_id = mpp.exam_id
			    AND sp.course_paper_id = mpp.course_paper_id
				AND sp.package_code = mpp.package_code
		WHERE
			sp.exam_id = #{examId}
			<if test="courseCode != null and courseCode != ''">
				AND mpp.course_code = #{courseCode}
			</if>
			<if test="coursePaperId != null and coursePaperId != ''">
				AND mpp.course_paper_id = #{coursePaperId}
			</if>
			<if test="dpr != null or status != null">
		  			and exists (select 1 from (select * from mark_paper where status = #{status}) mp
		  			    			left join sys_user su on mp.user_id = su.id where
		  			            		mpp.exam_id = mp.exam_id and mpp.course_paper_id = mp.course_paper_id
				<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>
	</select>
</mapper>
