<?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.paper.library.business.mapper.PaperScanTaskMapper">
    <update id="updateScanCount" parameterType="java.lang.Long">
        update paper_scan_task set scan_student_count = (SELECT COUNT(distinct student_id) FROM paper_library  where paper_scan_task_id = #{paperScanTaskId}),
                                    scan_count = (SELECT COUNT(1) FROM paper_library  where paper_scan_task_id = #{paperScanTaskId})
            where id = #{paperScanTaskId}
    </update>
    <select id="findPaperScanTaskPage"
            resultType="com.qmth.paper.library.business.bean.result.PaperScanTaskResult">
        SELECT
            pst.id AS paperScanTaskId,
            pst.school_id AS schoolId,
            bs.name semesterName,
            be.name examName,
            pst.course_code AS courseCode,
            pst.course_name AS courseName,
            pst.recognition_type recognitionType,
            pst.store_type storeType,
            pst.student_count AS studentCount,
            pst.scan_count AS scanCount,
            pst.scan_student_count AS scanStudentCount
        FROM
            paper_scan_task pst
                left join
            basic_exam be on pst.exam_id = be.id
                left join
            basic_semester bs on be.semester_id = bs.id
        <where>
            <if test="examId != null">
                AND pst.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND pst.course_code = #{courseCode}
            </if>
            order by pst.course_code
        </where>
    </select>

    <select id="findPaperScanTaskDetailPageByScanTaskId"
            resultType="com.qmth.paper.library.business.bean.result.PaperScanTaskDetailResult">
        SELECT
            es.student_name,
            es.student_code,
            es.course_code,
            es.course_name,
            es.teacher,
            es.teach_class,
            es.bind_count,
            es.id studentId
        FROM
            paper_scan_task pst
                 JOIN
            exam_student es ON pst.exam_id = es.exam_id and pst.course_code = es.course_code
        <where>
            <if test="paperScanTaskId != null">
                AND pst.id = #{paperScanTaskId}
            </if>
        </where>
        ORDER BY es.student_code
    </select>
</mapper>