<?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.xhkjedu.smarking.mapper.scan.MsScanbatchPaperMapper">
    <!--获取批次下所有试卷-->
    <select id="listPaperByBatchid" resultType="com.xhkjedu.smarking.model.scan.MsScanbatchPaper">
        select * from ms_scanbatch_paper where batchid = #{batchid} order by mspid
    </select>

    <!--查看原卷-试卷信息-->
    <select id="getScanPaperByMspid" resultType="java.util.Map">
        select * from ms_scanbatch_paper where mspid = #{mspid}
    </select>

    <!--试卷标记为异常-->
    <update id="updatePaperErrortype">
        update ms_scanbatch_paper set errortype = #{errortype} and haserror=1 and mspstate=1 where mspid = #{mspid}
    </update>

    <!--指定试卷和考号下未处理异常数量-->
    <select id="getErrorNumByMpidAndExamno" resultType="java.lang.Integer">
        select count(p.mspid) from ms_scanbatch_paper p left join ms_scanbatch b on p.batchid=b.batchid
        where b.mpid=#{mpid} and p.examno=#{examno} and p.haserror=1 and p.mspstate=0
    </select>

    <!--扫描异常-异常数量-->
    <select id="getScanErrorNum" resultType="com.xhkjedu.smarking.model.scan.MsScanbatch">
        select b.batchid,count(*)scannum
        from ms_scanbatch_paper p left join ms_scanbatch b on p.batchid=b.batchid
        where b.mpid=#{mpid} group by b.batchid
    </select>

    <!--扫描异常-列表-->
    <select id="listScanPaperErrors" resultType="java.util.Map">
        select b.batchid,b.batchname,p.mspid,p.batchorder,p.paperfront,p.paperback,p.createtime,p.examno
        from ms_scanbatch b right join ms_scanbatch_paper p on b.batchid=p.batchid
        where b.mpid=#{p.mpid} and p.haserror=1
        <if test="p.errortype!=null and p.errortype!=0">
            and p.errortype=#{p.errortype}
        </if>
        <if test="p.mspstate!=null and p.mspstate!=0">
            and p.mspstate=#{p.mspstate}
        </if>
        order by p.mspid
    </select>

    <!--扫描异常-列表-异常数量-->
    <select id="listScanPaperErrorsNum" resultType="java.util.Map">
        select p.errortype,count(*) errornum
        from ms_scanbatch_paper p left join ms_scanbatch b on p.batchid=b.batchid
        where b.mpid=#{mpid} and p.haserror=1 group by p.mspid
    </select>

    <!--扫描异常-更改状态-->
    <update id="updateScanPaperMsstate">
        update ms_scanbatch_paper set mspstate = #{mspstate} where mspid = #{mspid}
    </update>
    <!--扫描异常-更改状态和考号-->
    <update id="updateScanPaperMsstateAndExamno">
        update ms_scanbatch_paper set mspstate = #{mspstate},examno=#{examno} where mspid = #{mspid}
    </update>

    <!--扫描异常-已删除数量-->
    <select id="getDeleteScanPaperCount" resultType="java.lang.Integer">
        select count(mspid) from ms_scanbatch_paper where mpid=#{mpid} and mspstate=10
    </select>

    <!--扫描异常-缺考异常-异常处理-->
    <update id="updateBatchScanPaperMspstate">
        <foreach collection="list" item="p" separator=";">
            update ms_scanbatch_paper set mspstate=#{p.mspstate} where mspid=#{p.mspid}
        </foreach>
    </update>

    <!--重新识别-获取重新识别数据-->
    <select id="listScanPapersForReidentify" resultType="com.xhkjedu.smarking.model.scan.MsScanbatchPaper">
        select * from ms_scanbatch_paper where mpid=#{param.mpid}
        <if test="param.batchid!=null and param.batchid!=0"> and batchid=#{param.batchid}</if>
        <if test="param.hasidentify!=null and param.hasidentify==1"> and identified=1</if>
        <if test="param.hasidentify!=null and param.hasidentify==2"> and identified=0</if>
        order by mspid
    </select>
    <!--重新识别-删除需要重新识别试卷识别数据-->
    <delete id="deleteScanPaperForReidentify">
        delete b from ms_scanbatch_paper_block b left join ms_scanbatch_paper p on b.mspid=p.mspid
        where p.mpid=#{param.mpid}
        <if test="param.batchid!=null and param.batchid!=0"> and p.batchid=#{param.batchid}</if>
        <if test="param.hasidentify!=null and param.hasidentify==1"> and p.identified=1</if>
        <if test="param.hasidentify!=null and param.hasidentify==2"> and p.identified=0</if>
    </delete>

    <!--重新识别-修改识别试卷数据-->
    <update id="updateScanPaperForReidentified">
        update ms_scanbatch_paper set paperfront=#{p.paperfront},paperback=#{p.paperback},identified=#{p.identified},examno=#{p.examno},
        examnopic=#{p.examnopic},missexam=#{p.missexam},violated=#{p.violated},haserror=#{p.haserror},errortype=#{p.errortype},mspstate=#{p.mspstate}
        where mspid=#{p.mspid}
    </update>

</mapper>
