<?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.stupaper.MsPaperStudentMapper">
    <!--批量保存学生试卷-->
    <insert id="batchSave">
        insert ignore into ms_paper_student (examid,subjectid,classid,studentid)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.examid},#{item.subjectid},#{item.classid},#{item.studentid})
        </foreach>
    </insert>
    <!--获取学生科目试卷状态-->
    <select id="getStateByStudentIdAndSubjectId" resultType="java.lang.Integer">
        select sstate
        from ms_paper_student where examid=#{cs.examid} and subjectid=#{cs.subjectid} and studentid=#{cs.studentid}
    </select>

    <!--获取试卷学生信息-->
    <select id="getPaperStudentByStudentidAndMpid" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudent">
        select * from ms_paper_student where studentid=#{studentid} and mpid=#{mpid} order by mpsid limit 1
    </select>

    <!--学生考试列表-待进行-->
    <select id="listExamPrePapersForStudent" resultType="java.util.Map">
        select eps.mpsid, es.msid,es.examid,es.subjectid,es.subjectname,es.sdate,es.begintime,
        es.endtime,es.msstate,e.examname,e.gradeid,e.examtype,ep.mpid,ep.ptype,eps.sstate
        from ms_paper_student eps left join ms_subject es on eps.msid=es.msid
        left join ms_paper ep on eps.mpid=ep.mpid left join ms_exam e on eps.examid=e.examid
        where e.deleted=1 and e.exammode=3 and eps.studentid=#{studentid}
        and (e.examstate=1 or (e.examstate>1 and es.msstate!=2))
        GROUP BY eps.mpsid
        order by FIELD(es.pstate,1,0,2),
        abs(timestampdiff(MINUTE,concat(es.sdate,' ',es.begintime),#{nowtime})),es.msid desc
    </select>
    <!--学生考试列表-已结束-->
    <resultMap id="examsEndForStu" type="java.util.Map">
        <result property="examid" column="examid"></result>
        <result property="examname" column="examname"></result>
        <result property="examdate" column="examdate"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="examtype" column="examtype"></result>
        <result property="exammode" column="exammode"></result>
        <result property="examstate" column="examstate"></result>
        <result property="examcomm" column="examcomm"></result>
        <collection property="subjects" ofType="java.util.Map" javaType="java.util.List"
                    select="listExamSubjects" column="{examid=examid,studentid=studentid}">
        </collection>
    </resultMap>
    <!--学生已结束考试列表-->
    <select id="listExamEndPapersForStudent" resultMap="examsEndForStu">
        select e.examid,e.examname,e.examdate,e.gradeid,e.examtype,e.exammode,e.examstate,e.examcomm,#{studentid} as studentid
        from ms_exam e left join ms_paper_student eps on e.examid=eps.examid
        where e.deleted=1 and eps.studentid=#{studentid} and e.examstate>=2
        group by e.examid order by e.examdate desc
    </select>
    <select id="listExamSubjects" resultType="java.util.Map">
        select es.msid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,eps.mpsid
        ,(select ep.ptype from ms_paper ep where ep.msid=es.msid)ptype
        from ms_subject es left join ms_paper_student eps on es.msid=eps.msid
        where eps.studentid=#{studentid} and es.examid=#{examid} and es.pstate=2 order by es.begintime,es.msid
    </select>

    <!--学生待考-考试科目状态和学生试卷状态-->
    <select id="getSubjectStateAndSstageByMpsid" resultType="com.xhkjedu.smarking.vo.stupaper.PaperStuExamVo">
        select eps.sstate,s.msstate,s.msid,eps.studentid,s.sdate,s.begintime from ms_paper_student eps                                                                        left join ms_subject s on eps.msid=s.msid where eps.mpsid=#{mpsid}
    </select>
    <!--学生待考-考试科目时间信息-->
    <select id="getExamPaperSubjectByMpsid" resultType="java.util.Map">
        select ps.mpsid,ps.examid,ps.mpid,e.examname,e.examcomm,e.monitored,e.gradeid,es.msid,
        es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,p.ptype,p.pnum,p.pscore,
        p.hearfile,p.hearnum,cs.classid,c.classname
        from ms_paper_student ps left join ms_exam e on ps.examid=e.examid
        left join ms_subject es on ps.msid=es.msid and ps.examid=es.examid
        left join ms_paper p on ps.mpid=p.mpid
        left join ms_class_student cs on cs.studentid=ps.studentid and cs.examid=ps.examid and cs.subjectid=ps.subjectid
        left join ms_class c on cs.classid=c.classid and cs.examid=c.examid and cs.subjectid=c.subjectid
        where ps.mpsid=#{mpsid} order by c.classtype
    </select>
    <!--学生题库试卷试题-->
    <resultMap id="qtypesQuestion" type="java.util.Map">
        <result column="mptid" property="mptid"></result>
        <result column="mptname" property="mptname"></result>
        <result column="mptnum" property="mptnum"></result>
        <result column="mptorder" property="mptorder"></result>
        <result column="mptscore" property="mptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result column="mpsqid" property="mpsqid"></result>
            <result column="questionid" property="questionid"></result>
            <result column="qscore" property="qscore"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="answered" property="answered"></result>
            <result column="answertype" property="answertype"></result>
            <result column="stuanswer" property="stuanswer"></result>
            <result column="mptqid" property="mptqid"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="qctype" property="ctype"></result>
            <result column="qstem" property="qstem"></result>
            <result column="hearfile" property="hearfile"></result>
            <result column="qoption" property="qoption"></result>
            <result column="qanalyze" property="qanalyze"></result>
            <result column="qlevel" property="qlevel"></result>
            <result column="sorder" property="sorder"></result>
            <result column="snum" property="snum"></result>
            <result column="questionpid" property="questionpid"></result>
            <result column="mquestionid" property="mquestionid"></result>
            <result column="mqstem" property="mqstem"></result>
            <result column="mqtypeid" property="mqtypeid"></result>
            <result column="mqtypename" property="mqtypename"></result>
            <result column="mctype" property="mctype"></result>
            <result column="mhearfile" property="mhearfile"></result>
        </collection>
    </resultMap>
    <!--学生待考-Web题库试卷-->
    <select id="listStuPaperQuestionsForWebTk" resultMap="qtypesQuestion">
        select pt.mptid,pt.mptname,pt.mptnum,pt.mptorder,pt.mptscore,psq.mpsqid,
        psq.questionid,psq.qorder,psq.qn,psq.qscore,psq.answered,psq.answertype,psq.stuanswer,
        psq.mptqid,q.qstem,q.qoption,q.ctype qctype,q.hearfile,q.qtypeid,q.qtypename,q.qlevel,
        q.snum,q.sorder,q.questionpid,mq.questionid mquestionid,mq.qstem mqstem,mq.qoption mqoption,
        mq.ctype mctype,mq.qtypeid mqtypeid,mq.qtypename mqtypename,mq.hearfile mhearfile
        from ms_paper_student_question psq left join ms_paper_qtype_question ptq on psq.mptqid=ptq.mptqid
        left join ms_paper_qtype pt on pt.mptid=ptq.mptid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.mpsid=#{mpsid} order by pt.mptorder,psq.qorder,q.sorder
    </select>
    <!--学生附件试卷试题-->
    <resultMap id="fjtypeQuestions" type="java.util.Map">
        <result column="mptid" property="mptid"></result>
        <result column="mptname" property="mptname"></result>
        <result column="mptnum" property="mptnum"></result>
        <result column="mptorder" property="mptorder"></result>
        <result column="mptscore" property="mptscore"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.Map">
            <result column="stuscore" property="stuscore"></result>
            <result column="mpsqid" property="mpsqid"></result>
            <result column="qscore" property="qscore"></result>
            <result column="qorder" property="qorder"></result>
            <result column="qn" property="qn"></result>
            <result column="mptqid" property="mptqid"></result>
            <result column="qtypeid" property="qtypeid"></result>
            <result column="qtypename" property="qtypename"></result>
            <result column="ctype" property="ctype"></result>
            <result column="optionnum" property="optionnum"></result>
            <result column="answered" property="answered"></result>
            <result column="answertype" property="answertype"></result>
            <result column="stuanswer" property="stuanswer"></result>
        </collection>
    </resultMap>
    <!--学生待考-Web附件试卷-->
    <select id="listStuPaperQuestionsForWebFj" resultMap="fjtypeQuestions">
        select pt.mptid,pt.mptname,pt.mptnum,pt.mptorder,pt.mptscore,
        psq.mpsqid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,
        psq.mptqid,psq.qscore,pq.optionnum,pq.ctype,pq.qtypeid,pq.qtypename
        from ms_paper_student_question psq left join ms_paper_qtype_question pq on psq.mptqid=pq.mptqid
        left join ms_paper_qtype pt on pq.mptid=pt.mptid
        where psq.mpsid=#{mpsid} order by pt.mptorder, psq.qorder
    </select>
    <!--学生待考-Android题库试卷-->
    <select id="listStuPaperQuestionsForAndroidTk" resultType="java.util.Map">
        select distinct psq.mpsqid,psq.questionid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,psq.mptqid,pq.qscore,q.qstem,q.qoption,q.ctype,q.hashear,q.hearfile,q.qtypeid,
        q.qtypename,q.qlevel,q.snum,q.sorder,q.questionpid,mq.questionid mquestionid,mq.qstem mqstem,
        mq.qoption mqoption,mq.ctype mctype,mq.qtypeid mqtypeid,mq.qtypename mqtypename,mq.hashear mhashear,mq.hearfile mhearfile
        from ms_paper_student_question psq left join ms_paper_qtype_question pq on psq.mptqid=pq.mptqid
        left join t_question q on psq.questionid=q.questionid
        left join t_question mq on q.questionpid=mq.questionid
        where psq.mpsid=#{mpsid} order by psq.qorder,q.sorder
    </select>
    <!--学生待考-Android附件试卷-->
    <select id="listStuPaperQuestionsForAndroidFj" resultType="java.util.Map">
        select distinct psq.mpsqid,psq.qn,psq.qorder,psq.answered,psq.answertype,psq.stuanswer,psq.mptqid,pq.qscore,pq.optionnum,pq.ctype,pq.qtypeid,pq.qtypename
        from ms_paper_student_question psq left join ms_paper_qtype_question pq on psq.mptqid=pq.mptqid
        where psq.mpsid=#{mpsid} order by psq.qorder
    </select>
    <!--学生待考-进入考试-修改学生考试状态-->
    <update id="updatePaperStuStartime">
        update ms_paper_student set starttime=#{starttime},sstate=1 where mpsid=#{mpsid}
    </update>
    <!--学生待考-进入考试-获取考试结束时间-->
    <select id="getSubjectEndtime" resultType="com.xhkjedu.smarking.vo.stupaper.PaperStuExamVo">
        select concat(es.sdate,' ',es.endtime,':00')sdatetime,(right(#{studentid},2)+0)/100 userbl
        from ms_subject es where es.msid=#{msid}
    </select>

    <!--学生考试-获取单科状态-->
    <select id="getSubjectStateByMpsid" resultType="com.xhkjedu.smarking.vo.stupaper.PaperStuExamVo">
        select s.msstate,eps.mpid.eps.msid from ms_paper_student eps left join ms_subject s on eps.msid=s.msid where eps.mpsid=#{mpsid}
    </select>
    <!--学生考试-获取开始考试学生数量-->
    <select id="getNumByExamIdAndSubjectIdAndStuId" resultType="java.lang.Integer">
        select count(*) from ms_paper_student
        where examid=#{student.examid} and subjectid=#{student.subjectid} and studentid=#{student.studentid}
        <if test="student.exammode!=null and student.exammode==3">
            and starttime is not null
        </if>
    </select>
    <!--学生考试-获取开始考试学生数量-->
    <select id="getStartNumByExamIdExamMode" resultType="java.lang.Integer">
        select count(*) from ms_paper_student where examid=#{examid}
        <if test="exammode!=null and exammode==3">
            and starttime is not null
        </if>
    </select>
    <!--学生考试-提交试卷-->
    <update id="updateStudentPaper">
        update ms_paper_student set endtime=#{ps.endtime},sstate=#{ps.sstate},stuscoretype=1
        ,stuscore=(SELECT SUM(IFNULL(q.stuscore,0)) FROM ms_paper_student_question q WHERE q.mpsid=#{ps.mpsid})
        <if test="ps.costtime==null and ps.costtime==0">
            ,costtime=(SELECT SUM(IFNULL(q.costtime,0)) FROM ms_paper_student_question q WHERE q.mpsid=#{ps.mpsid})
        </if>
        <if test="ps.costtime!=null">
            ,costtime=#{ps.costtime}
        </if>
        <if test="ps.submittype!=null and ps.submittype!='0'">
            ,submittype = #{ps.submittype}
        </if>
        <if test="ps.device!=null and ps.device!=''">
            ,device = #{ps.device}
        </if>
        where mpsid=#{ps.mpsid}
    </update>

    <!--试卷答案-客观题-批量更新学生试卷分值-->
    <update id="updateBathStudentPaperScoreByMpsid">
        <foreach collection="mpsids" item="mpsid" separator=";">
            update ms_paper_student ps set ps.stuscore=(SELECT SUM(IFNULL(q.stuscore,0)) FROM ms_paper_student_question q
            WHERE q.mpid=#{mpid} and q.mpsid=#{mpsid}) where ps.mpid=#{mpid} and ps.mpsid=#{mpsid}
        </foreach>
    </update>

    <!--清空试卷学生信息-->
    <delete id="deletePaperStuByMpid">
        delete from ms_paper_student where mpid=#{mpid};
    </delete>
    <!--删除考试学生信息-->
    <delete id="deleteByExamId">
        delete from ms_paper_student where examid=#{examid}
    </delete>

    <!--扫描异常-清空学生扫描试卷信息-->
    <update id="updateStuPaperForScanErrorClear">
        update ms_paper_student set sstate=0,stuscore=0.0,stuscoretype=0,hasbad=0,batchid=null,mspid=null,
                                    paperfront=null,paperback=null,pagenum=0 where mpsid=#{mpsid}
    </update>
    <!--扫描异常-缺考异常-更改状态-->
    <update id="updateBatchStuPaperSstate">
        <foreach collection="list" item="p" separator=";">
            update ms_paper_student set sstate=#{p.sstate} where mspid=#{p.mspid} and mpid=#{p.mpid}
        </foreach>
    </update>
    <!--扫描-清空学生试卷扫描信息-->
    <update id="updateStuPaperForScanClearByMpid">
        update ms_paper_student set sstate=0,stuscore=0.0,stuscoretype=0,hasbad=0,batchid=null,mspid=null,
                                    paperfront=null,paperback=null,pagenum=0 where mpid=#{mpid}
    </update>

    <!--补录成绩-学生科目成绩-->
    <select id="listPaperStudentForFillScore" resultType="com.xhkjedu.smarking.model.stupaper.MsPaperStudent">
        select * from ms_paper_student where examid=#{examid} and studentid=#{studentid}
    </select>
    <!--考试学生成绩列表-->
    <select id="listPaperStudentByExamId" resultType="com.xhkjedu.smarking.model.report.reportstu.MsrStudent">
        select mpid,subjectid,studentid,stuscore,sstate,pscore as fullscore
        from ms_paper_student where examid=#{examid}
        <foreach collection="mpids" item="mpid" open="and mpid in (" separator="," close=")">
            #{mpid}
        </foreach>
    </select>

    <!--补录成绩-补录单科-->
    <update id="updateStuScoreForFillScore">
        update ms_paper_student set stuscore=#{psc.fillscore},stuscoretype=2 where mpsid=#{psc.mpsid} and stuscoretype!=1
    </update>
    <!--补录成绩-补录多科-->
    <update id="updateBatchStuScoreForFillScore">
        <foreach collection="list" item="p" separator=";">
            update ms_paper_student set stuscore=#{p.fillscore},stuscoretype=2 where mpsid=#{p.mpsid} and stuscoretype!=1
        </foreach>
    </update>
    <!--补录成绩-清空学生补录分数-->
    <update id="updateStuScoreForRestoreFillScore">
        update ms_paper_student set stuscore=0.0,stuscoretype=0 where examid=#{examid} and studentid=#{studentid}
    </update>
    <!--更新学生总分-->
    <update id="updateStudentPaperScoreByMpsId">
        update ms_paper_student set stuscore=(select sum(ifnull(q.stuscore,0)) from ms_paper_student_question q where q.mpsid=#{mpsid})
        where mpsid=#{mpsid}
    </update>

    <!--清空试卷下所有学生识别作答及批阅信息-->
    <update id="updateStudentBlockQuestionForReIdentify">
        update ms_paper_student_block b join ms_paper_student_question q
        on b.mpsid=q.mpsid and b.mblockid=q.mblockid
        left join ms_paper_student ps on ps.mpsid=b.mpsid
        left join ms_scanbatch_paper sp on ps.mspid=sp.mspid
        set b.stuanswer=null,b.converted=0,b.checked=0,b.hasgood=0,b.hasbad=0,b.firstcid=null,b.firstcime=null,b.firstccore=0,
        b.secondcid=null,b.secondctime=null,b.secondcscore=0,b.arbitrateid=null,b.arbitratetime=null,b.arbitratescore=0,
        b.hasproblem=0,b.problemtype=0,b.problemcomm=null,b.hasfinal=0,b.finalreason=0,
        q.answered=0,q.answertype=0,q.stuanswer=null,q.converted=0,q.answertime=0,q.stuscore=0,
        q.checked=0,q.firstcid=null,q.firstcime=null,q.firstccore=0,q.secondcid=null,q.secondctime=null,q.secondcscore=0,
        q.arbitrateid=null,q.arbitratetime=null,q.arbitratescore=0,q.hasgood=0,
        q.hasbad=0,q.hasproblem=0,q.problemtype=0,q.problemcomm=null,
        ps.sstate=0,ps.stuscore=0.0,ps.stuscoretype=0,ps.hasbad=0,ps.batchid=null,ps.mspid=null,
        ps.paperfront=null,ps.paperback=null,ps.pagenum=0
        where b.mpid = #{param.mpid}
        <if test="param.batchid!=null and param.batchid!=0"> and sp.batchid=#{param.batchid}</if>
        <if test="param.hasidentify!=null and param.hasidentify==1"> and sp.identified=1</if>
        <if test="param.hasidentify!=null and param.hasidentify==2"> and sp.identified=0</if>
    </update>

</mapper>
