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

    <!--扫描答卷-考试试卷信息-->
    <select id="getExamPaperScanInfo" resultType="java.util.Map">
        select e.examid,e.examname,e.exammode,e.gradeid,s.msid,s.subjectid,s.subjectname,p.mpid,
        (select sum(bqnum) from ms_paper_block where mpid=p.mpid and blocktype=1) objnum,
        (select sum(bqnum) from ms_paper_block where mpid=p.mpid and blocktype=2) subnum,
        (select count(mcsid) from ms_class_student where examid=e.examid and subjectid=s.subjectid) stunum
        from ms_subject s left join ms_exam e on s.examid=e.examid
        left join ms_paper p on s.msid=p.msid where p.mpid=#{mpid}
    </select>

    <!--扫描答卷-扫描信息-->
    <select id="listScanBatchForPaper" resultType="java.util.Map">
        select s.* from ms_scanbatch s where s.mpid=#{mpid} order by s.batchid
    </select>

    <!--获取批次名称模糊数量-->
    <select id="getBatchNumByBatchname" resultType="java.lang.Integer">
        select count(*) from ms_scanbatch where batchname like concat(#{batchname,jdbcType=VARCHAR},'%')
    </select>

    <!--获取试卷所有扫描批次-->
    <select id="listScanBatchByMpidForSel" resultType="java.util.Map">
        select batchid,batchname from ms_scanbatch where mpid=#{mpid} order by batchid
    </select>

    <!--扫描记录-扫描信息-->
    <select id="getScanPaperRecord" resultType="java.util.Map">
        select e.examid,e.examname,e.gradeid,e.exammode,s.subjectid,s.subjectname,s.msid,sum(b.scannum) scannum,
        (select count(*) from ms_class_student where examid=e.examid and subjectid=s.subjectid) stunum,
        (select count(*) from ms_paper_student where mpid=b.mpid and batchid is not null) identifynum
        from ms_exam e left join ms_subject s on e.examid=s.examid
        left join ms_paper p on s.msid=p.msid
        left join  ms_scanbatch b on e.examid and p.mpid=b.mpid
        where b.mpid=#{mpid} group by b.mpid
    </select>

    <!--扫描记录-按班级-->
    <select id="listRecordClassStudents" resultType="java.util.Map">
        select distinct cs.studentid,cs.classid,s.classname,u.username studentname,u.examno,
        ps.batchid,ps.mspid,ps.stuscore,b.batchname
        from ms_class_student cs left join ms_class s on cs.classid=s.classid and cs.examid=s.examid
        left join t_user u on cs.studentid=u.userid
        left join ms_paper_student ps on cs.studentid=ps.studentid and cs.subjectid=ps.subjectid
        and cs.examid=ps.examid
        left join ms_scanbatch b on ps.batchid=b.batchid and b.mpid=ps.mpid
        where cs.examid=#{params.examid} and ps.mpid=#{params.mpid}
        <if test="params.classid!=null and params.classid!=0">
            and cs.classid=#{params.classid}
        </if>
        <if test="params.hasscan!=null and params.hasscan==1">
            and ps.batchid is not null
        </if>
        <if test="params.hasscan!=null and params.hasscan==2">
            and ps.batchid is null
        </if>
        order by cs.mcsid
    </select>
    <!--扫描记录-按批次-->
    <select id="listRecordBatchStudents" resultType="java.util.Map">
        select b.batchid,b.batchname,sp.mspid,sp.examno,c.classid,c.classname,u.username studentname
        from ms_scanbatch_paper sp left join ms_scanbatch b on sp.batchid=b.batchid
        left join ms_paper_student ps on ps.batchid=b.batchid
        left join ms_class_student cs on cs.subjectid=ps.subjectid and ps.studentid=cs.studentid
        left join ms_class c on cs.examid=c.examid and cs.subjectid=c.subjectid and cs.classid=c.classid
        left join t_user u on ps.studentid=u.userid
        where b.mpid=#{params.mpid}
        <if test="params.batchid!=null and params.batchid!=0">
            and b.batchid=#{params.batchid}
        </if>
        <if test="params.hasidentify!=null and params.hasidentify==1">
            and sp.identified=1
        </if>
        <if test="params.hasidentify!=null and params.hasidentify==2">
            and sp.identified=0
        </if>
        order by b.batchid,sp.mspid
    </select>

    <!--修改扫描数量-->
    <update id="updateScannum">
        update ms_scanbatch set scannum=scannum+1 where batchid=#{batchid}
    </update>
    <!--修改扫描数量和出错数量-->
    <update id="updateScannumAndErrornum">
        update ms_scanbatch set scannum=scannum+1,
        errornum=(select count(*) from ms_scanbatch_paper where batchid=#{batchid} and haserror=1
        where batchid=#{batchid}
    </update>
    <!--修改识别数量-->
    <update id="updateIdentifynum">
        update ms_scanbatch set identified=(select count(*) from ms_scanbatch_paper where batchid=#{batchid} and identified=1) where batchid=#{batchid}
    </update>
    <!--修改缺考数量-->
    <update id="updateQknum">
        update ms_scanbatch_paper set errornum=(select count(*) from ms_scanbatch_paper where batchid=#{batchid} and haserror=1) where batchid=#{batchid}
    </update>
    <!--修改违规数量-->
    <update id="updateWgnum">
        update ms_scanbatch_paper set errornum=(select count(*) from ms_scanbatch_paper where batchid=#{batchid} and haserror=1) where batchid=#{batchid}
    </update>

    <!--根据试卷ID获取考试状态和ID-->
    <select id="getExamStateAndId" resultType="com.xhkjedu.smarking.model.exam.MsExam">
        select p.examid,e.examstate from ms_paper p left join ms_exam e on p.examid=e.examid
        where p.mpid=#{mpid}
    </select>
    <!--根据批次ID获取考试状态-->
    <select id="getExamStateByBatchid" resultType="java.lang.Integer">
        select e.examstate from ms_exam e
        right join ms_scanbatch b on e.examid=b.examid and b.batchid=#{batchid}
    </select>

</mapper>
