<?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.sexam.mapper.paperstudent.EScanbatchMapper">

    <!--获取指定设备试卷批次数量-->
    <select id="getBatchNameLikeNum" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(*) from e_scanbatch where batchname like concat(#{batchname,jdbcType=VARCHAR},'%')
    </select>

    <!--更改扫描数量-->
    <update id="updateScannum">
        update e_scanbatch set scannum=scannum+1 where batchid=#{batchid}
    </update>
    <!--更改缺考数量-->
    <update id="updateQknum">
        update e_scanbatch set qknum=(select count(*) from e_paper_student ps where ps.sstate=3 and ps.batchid=#{batchid})
        where batchid=#{batchid}
    </update>
    <!--更改违规数量-->
    <update id="updateWgnum">
        update e_scanbatch set wgnum=(select count(*) from e_paper_student ps where ps.hasbad=1 and ps.batchid=#{batchid})
        where batchid=#{batchid}
    </update>
    <!--更改出错数量-->
    <update id="updateErrornum">
        update e_scanbatch set errornum=
    (select count(*) from e_scanerror e where e.batchid=#{batchid} and e.errorstate=0) where batchid=#{batchid}
    </update>
    <!--更改扫描和出错数量-->
    <update id="updateScanAndErrorNum">
        update e_scanbatch set scannum=scannum+1,errornum=(select count(*) from e_scanerror e where e.batchid=#{batchid})
        where batchid=#{batchid}
    </update>

    <!--扫描答题卡列表-->
    <select id="listScanSubject" resultType="java.util.Map">
        select a.*,ifnull(truncate(a.smnum*100/a.examnum,2),0)smrate,count(distinct er.serrorid)wclnum
        ,ifnull(truncate(count(distinct case when epsq.checked=2 then epsq.epsqid else null end)*100/
        count(distinct epsq.epsqid),2),0)yjrate
        from(select es.esid,es.esstate,es.subjectname
                  ,count(eps.epid)examnum,ep.epid
                  ,count(case when eps.sstate!=0 then eps.epsid else null end)smnum
                  ,count(case when eps.sstate=3 then eps.epsid else null end)qknum
             from e_subject es left join e_paper ep on es.esid = ep.esid
                               left join e_paper_student eps on ep.epid = eps.epid
             where es.examid=#{examid}
             group by es.esid)a left join e_paper_qtype_question epqq on a.epid = epqq.epid
                                left join e_paper_student_question epsq on epsq.eptqid = epqq.eptqid and epsq.answered=1
                                left join e_scanbatch b on b.epid=a.epid left join e_scanerror er on b.batchid=er.batchid and er.errorstate=0
        group by a.epid
    </select>

    <!--试卷扫描批次信息-->
    <select id="listScanBatchPapers" resultType="java.util.Map">
        select b.*,count(es.serrorid) wclnum
        from e_scanbatch b left join e_scanerror es on b.batchid=es.batchid and es.errorstate=0
        where b.epid=#{epid} group by b.batchid order by b.batchid desc
    </select>

    <!--获取答题卡扫描结果-->
    <select id="getScanResult" resultType="com.xhkjedu.sexam.model.paperstudent.EScanbatch">
        select ifnull(sum(batchnum),0)batchnum,ifnull(sum(scannum),0)scannum
        ,ifnull(sum(errornum),0)errornum,ifnull(sum(qknum),0)qknum
        from e_scanbatch where epid=#{epid}
    </select>
    <!--扫描答题卡详情-学生列表-->
    <select id="listScanStu" resultType="java.util.Map">
        select eps.epsid,u.username,u.examno,ec.classname,eps.batchid,eps.sstate,eps.stupic
        from e_paper_student eps left join e_class ec on eps.examid = ec.examid and eps.classid = ec.classid
        left join t_user u on eps.studentid = u.userid
        where eps.epid=#{epid}
        <if test="classid!=null and classid!=0">
            and eps.classid=#{classid}
        </if>
        <if test="sstate==1">
            and eps.batchid is null
        </if>
        <if test="sstate==2">
            and eps.batchid is not null
        </if>
        <if test="username!=null and username!=''">
            and (u.username like '%${username}%' or u.examno like '%${username}%')
        </if>
    </select>
    <!--试卷基础信息-->
    <select id="listByEpId" resultType="java.util.Map">
        select ifnull(ep.scantronnum,1)scantronnum,eb.examname,eb.examid,eb.gradeid,es.subjectname
        from e_paper ep left join e_base eb on ep.examid = eb.examid
        left join e_subject es on ep.esid = es.esid
        where ep.epid=#{epid}
    </select>

    <!--扫描对应学校id-->
    <select id="getScanBatchSchool" resultType="java.util.Map">
        select b.epid,b.batchid,b.examid, e.schoolid,(select cloudcode from t_cloud limit 1) cloudcode
        from e_scanbatch b left join e_base e on b.examid=e.examid
            left join t_school s on e.schoolid=s.schoolid  where b.batchid=#{batchid}
    </select>
    <!--扫描答题卡详情-->
    <select id="getScanDetail" resultType="java.util.Map">
        select eb.examname,eb.examid,eb.gradeid,es.subjectname
        ,count(distinct case when eps.sstate=3 then eps.epsid else null end)qknum
        ,count(distinct case when eps.sstate!=0 then eps.epsid else null end)ysnum
        from e_paper_student eps left join e_base eb on eps.examid = eb.examid
        left join e_subject es on eps.esid = es.esid
        where eps.epid=#{epid}
    </select>

    <!--修改处理后批次数量信息-->
    <update id="updateBatchErrornum">
        <foreach collection="list" item="batchid" separator=";" open="" close="">
            update e_scanbatch set errornum=(select count(*) from e_scanerror e where e.batchid=#{batchid} and e.errorstate=0)
            where batchid=#{batchid}
        </foreach>
    </update>
</mapper>
