<?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.distributed.print.business.mapper.GradeBatchMapper">

    <select id="findGradeBatchPage"
            resultType="com.qmth.distributed.print.business.bean.result.analyze.GradeBatchResult">
        SELECT
            gb.id AS id,
            gb.exam_id AS examId,
            be.name AS examName,
            gb.batch_name AS batchName,
            gb.batch_time AS batchTime,
            gb.status,
            gb.semester_id AS semesterId,
            bs.name AS semesterName,
            gb.progress AS progress,
            gb.report_file_path AS reportFilePath,
            gb.notify_time AS notifyTime,
            gb.result AS result,
            gb.third_semester_id AS thirdSemesterId,
            gbp.publish_status AS publishStatus
        FROM
            grade_batch gb
                LEFT JOIN
            (SELECT
                 batch_id, MAX(publish_status) AS publish_status
             FROM
                 grade_batch_paper
             GROUP BY batch_id) gbp ON gb.id = gbp.batch_id
                INNER JOIN
            basic_semester bs ON gb.semester_id = bs.id AND bs.enable
                INNER JOIN
            basic_exam be ON gb.exam_id = be.id AND be.enable
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND gb.school_id = #{schoolId}
            </if>
            <if test="semesterId != null">
                AND gb.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND gb.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND gb.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND gb.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
</mapper>
