<?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.paper.EPaperMapper">

    <!--获取考试下有科目试卷信息-->
    <select id="listExamPapers" resultType="java.util.Map">
        select es.esid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,u2.username as teachername,
               es.esstate,ep.epid,ep.ptype,ep.pnum,ep.pscore,ep.answered,ep.handleid,u.username as handlename,ep.scantronpdf
        from e_subject es left join e_paper ep on es.esid=ep.esid
        left join t_user u on ep.handleid=u.userid
        left join t_user u2 on es.teacherid=u2.userid
        where es.examid=#{examid} order by es.esid
    </select>

    <!--教师出卷列表-->
    <select id="listExamPapersByTeacherid" resultType="java.util.Map">
        select es.esid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,u2.username as teachername,
        es.esstate ,ep.epid,ep.ptype,ep.pnum,ep.pscore,ep.answered,ep.handleid,u.username as handlename,
        ep.scantronpdf,es.examid,e.examname,e.examtype,e.exammode,e.examstate,e.hashb,e.schoolid
        from e_subject es left join e_paper ep on es.esid=ep.esid
        left join e_base e on es.examid=e.examid
        left join t_user u on ep.handleid=u.userid
        left join t_user u2 on es.teacherid=u2.userid
        where e.deleted=1 and es.teacherid=#{teacherid}
        <if test="esstate == 1">
            and es.esstate=1
        </if>
        <if test="esstate != 1">
            and es.esstate!=1
        </if>
        group by es.esid
        <if test="esstate == 1">
            order by es.sdate desc,es.endtime desc
        </if>
        <if test="esstate != 1">
            order by abs(timediff(concat(es.sdate,' ',es.begintime),#{strtime}))
        </if>
    </select>

    <!--试卷详情-->
    <select id="getExamPaperDetailByEsid" resultType="java.util.Map">
        select e.examid,e.examname,e.gradeid,e.examtype,e.exammode,e.examstate,e.monitored,es.esid,
               u2.username teachername,es.sdate,es.esstate,es.begintime,es.endtime,es.subjectid,es.subjectname,ep.epid,
               ep.ptype,ep.pnum,ep.pscore,ep.answered,ep.hearnum,ep.hearfile,ep.hasfile,ep.handleid,ep.handletime,u.username as handlename
        from e_base e left join e_subject es on e.examid=es.examid
        left join e_paper ep on es.esid=ep.esid
        left join t_user u on ep.handleid=u.userid
        left join t_user u2 on es.teacherid=u2.userid where es.esid=#{esid}
    </select>

    <!--获取试卷考试名科目名称-->
    <select id="getExamPaperAndSubject" resultType="java.util.Map">
        select ep.epid,ep.esid,ep.ptype,e.examname,e.examstate,s.subjectname,ep.scantronfile,ep.scantronnote,ep.scantronwh
        ,ep.scantroncol,ep.cantronmiss,ep.cantronbad,ep.codetype,ep.cantroncode,ep.pnum,ep.pscore,ep.cantronbarcode,
               ep.cantronqrcode,ep.cantronpage,ep.cantronloc,ep.scantronspace,ep.codenum,ep.scantronpdf
        from e_paper ep left join e_base e on ep.examid=e.examid
        left join t_subject s on ep.subjectid=s.subjectid where ep.epid=#{epid}
    </select>

    <!--删除试卷信息-->
    <select id="deletePaperByEpid">
        delete from e_paper where epid=#{epid}
    </select>

    <!--修改试题设置答案-->
    <update id="updateEPaperAnswered">
        update e_paper set answered=#{paper.answered} where epid=#{paper.epid}
    </update>

    <!--根据试卷id获取考试id-->
    <select id="getExamIdByEpId" resultType="java.lang.Integer">
        select examid from e_paper where epid=#{epid} limit 1
    </select>
    <!--根据试卷id获取考试科目基本信息-->
    <select id="getESubjectByEpId" resultType="java.util.Map">
        select es.sdate,es.begintime,es.endtime,eb.examname,eb.schoolid
        from e_paper ep left join e_subject es on ep.esid = es.esid
        left join e_base eb on es.examid = eb.examid
        where ep.epid=#{epid}
    </select>
    <!--获取考试试卷题量和分值-->
    <select id="listNumAndScoreByExamId" resultType="com.xhkjedu.sexam.model.paper.EPaper">
        select esid,epid,pnum,pscore,subjectid,examid
        from e_paper where examid=#{examid}
    </select>
    <!--修改批阅设置-->
    <update id="updateCorrectType">
        update e_paper set correcttype=#{correcttype} where epid=#{epid}
    </update>

    <!--修改设置答题卡-->
    <update id="updatePaperScantron">
        update e_paper set scantronfile=#{paper.scantronfile},scantronnote=#{paper.scantronnote},scantronnum=#{paper.scantronnum}
        ,scantronwh=#{paper.scantronwh},scantroncol=#{paper.scantroncol},cantronmiss=#{paper.cantronmiss},
        cantronbad=#{paper.cantronbad},codetype=#{paper.codetype},codenum=#{paper.codenum},
        cantroncode=#{paper.cantroncode},cantronpage=#{paper.cantronpage},cantronbarcode=#{paper.cantronbarcode},
        cantronqrcode=#{paper.cantronqrcode},cantronloc=#{paper.cantronloc},scantronspace=#{paper.scantronspace}
        where epid=#{paper.epid}
    </update>

    <!--考试考试获取试卷信息-->
    <select id="getPaperParamsByEpid" resultType="com.xhkjedu.sexam.vo.paper.EExamSubjectPaperVo">
        select es.esid,es.subjectid,es.examid,e.examstate,e.exammode,ep.epid,ep.correcttype
        from e_subject es left join e_paper ep on es.esid=ep.esid
        left join e_base e on es.examid=e.examid where ep.epid=#{epid}
    </select>

    <!--设置试卷中试题数量-->
    <update id="updatePaperInfo">
        update e_paper set ptype=#{paper.ptype},pnum=#{paper.pnum},pscore=#{paper.pscore},answered=#{paper.answered},
         hearfile=#{paper.hearfile},hearnum=#{paper.hearnum},hasfile=#{paper.hasfile},handleid=#{paper.handleid},
         handletime=#{paper.handletime},correcttype=#{paper.correcttype} where epid=#{paper.epid}
    </update>

    <!--保存答题卡pdf-->
    <update id="updatePaperScantronpdf">
        update e_paper set scantronpdf=#{paper.scantronpdf} where epid=#{paper.epid}
    </update>

    <!--获取试卷中答题卡文件-->
    <select id="getPaperScantronfile" resultType="java.util.Map">
        select p.epid,p.examid,p.scantronfile,p.scantroncol,e.schoolid,e.examname,s.subjectname,
        (select cloudcode from t_cloud limit 1) cloudcode
        from e_paper p left join e_base e on p.examid=e.examid
        left join e_subject s on p.esid=s.esid
        where p.epid=#{epid}
    </select>

    <!--根据试卷id获取考试科目信息-->
    <select id="getExamPaperSubjectForScan" resultType="java.util.Map">
        select e.examname,e.gradeid,s.subjectname,p.pnum,p.ptype,a.alljson,ifnull(p.scantronnum,1)scantronnum,
               (select count(*) from e_paper_student ps where ps.epid=p.epid) ysrnum
        from e_paper p left join e_subject s on p.esid=s.esid
        left join e_base e on s.examid=e.examid left join e_paper_analyze a on p.epid=a.epid
        where p.epid=#{epid} limit 1
    </select>

    <!--根据考试id和科目id获取试卷信息-->
    <select id="getPaperByExamidAndSubjectid" resultType="java.util.Map">
        select epid,pnum,pscore,ptype,hearfile,hearfile from e_paper where examid=#{examid} and subjectid=#{subjectid}
    </select>
    <!--根据试卷id获取考试状态-->
    <select id="getExamStateByEpId" resultType="java.lang.Integer">
        select eb.examstate from e_paper ep left join e_base eb on ep.examid = eb.examid where ep.epid=#{epid}
    </select>

    <!--获取试卷关联考试及发起学校id-->
    <select id="getExamSchoolidByEpId" resultType="java.util.Map">
        select p.examid,e.schoolid from e_paper p
        left join e_base e on p.examid=e.examid where p.epid=#{epid} limit 1
    </select>
</mapper>
