<?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.PaperLibraryMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.paper.library.business.entity.PaperLibrary">
        <result column="id" property="id"/>
        <result column="paper_scan_task_id" property="paperScanTaskId"/>
        <result column="path" property="path"/>
        <result column="student_id" property="studentId"/>
        <result column="sequence" property="sequence"/>
        <result column="bind_result" property="bindResult"/>
        <result column="recognize_result" property="recognizeResult"/>
        <result column="create_id" property="createId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_id" property="updateId"/>
        <result column="update_time" property="updateTime"/>
        <result column="batch_no" property="batchNo"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        select id,
               `path`,
               paper_scan_task_id,
               student_id,
               `sequence`,
               bind_result,
               recognize_result,
               create_id,
               create_time,
               update_id,
               update_time
        from paper_library
    </sql>
    <select id="countScanStudentCount" resultType="java.lang.Integer">
        SELECT COUNT(distinct pl.student_id)
        FROM paper_library pl
        where pl.paper_scan_task_id = #{paperScanTaskId}
    </select>
    <select id="listByStudentCode" resultType="com.qmth.paper.library.business.bean.result.TaskStudentResult">
        SELECT
            es.id studentId,
            es.course_code courseCode,
            es.course_name courseName,
            es.student_name studentName,
            es.student_code studentCode,
            es.bind_count bindCount
        FROM
            exam_student es
        <where>
            <if test="examId != null">
                and es.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and es.course_code = #{courseCode}
            </if>
            <if test="studentCode != null and studentCode != ''">
                and es.student_code like concat(#{studentCode}, '%')
            </if>
        </where>
    </select>
    <select id="listMaxSequenceByPaperScanTaskId" resultType="java.util.Map">
        select es.id studentId, es.student_code studentCode, max(pl.sequence) maxSequence
        from paper_library pl
                 join exam_student es on pl.student_id = es.id
        where pl.paper_scan_task_id = #{paperScanTaskId}
        group by es.id, es.student_code
    </select>

    <select id="listScanStat" resultType="com.qmth.paper.library.business.bean.result.ScanStatResult">
        SELECT
            p.create_id createId,
            p.create_date scanDateStr,
            count( 1 ) pictureCount
        FROM
            paper_library p
        where p.school_id = #{schoolId}
        <if test="userId != null">
            and p.create_id=#{userId}
        </if>
        <if test="scanDate != null and scanDate != ''">
            and p.create_date=#{scanDate}
        </if>
        GROUP BY
            p.create_id,
            p.create_date
        ORDER BY
            p.create_date
    </select>

    <select id="countScanStudentByScannerAndDate" resultType="java.lang.Integer">
        SELECT
            COUNT( DISTINCT pl.student_id )
        FROM
            paper_library pl
        WHERE
            pl.school_id=#{schoolId}  and pl.create_id = #{createId} and pl.create_date=#{scanDate}
    </select>

    <select id="listScanStatExport" resultType="com.qmth.paper.library.business.bean.result.ScanStatExportResult">
        SELECT
        p.create_id createId,
        p.create_date scanDateStr,
        count( 1 ) pictureCount
        FROM
        paper_library p
        where p.school_id = #{schoolId}
        <if test="userId != null">
            and p.create_id=#{userId}
        </if>
        <if test="scanDate != null and scanDate != ''">
            and p.create_date=#{scanDate}
        </if>
        GROUP BY
        p.create_id,
        p.create_date
        ORDER BY
        p.create_date
    </select>

    <select id="listBatchNo" resultType="java.lang.String">
        select distinct pl.batch_no from paper_library pl where student_id=#{studentId}
    </select>

    <select id="getStudentMaxSequence" resultType="java.lang.Integer">
        select ifnull(max(sequence),0) from paper_library where paper_scan_task_id=#{paperScanTaskId} and student_id=#{studentId} and batch_no=#{batchNo}
    </select>

</mapper>