<?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.exam.MsSubjectMapper">
    <!--设置考试时间-->
    <update id="setExamDate">
        <foreach collection="subjects" item="s" separator=";">
            update ms_subject set sdate=#{s.sdate},begintime=#{s.begintime},endtime=#{s.endtime},teacherid=#{s.teacherid}
            ,handleid=#{handleid},handletime=#{timestamp}
            where examid=#{s.examid} and subjectid=#{s.subjectid}
        </foreach>
    </update>
    <!--考试进度总览-->
    <select id="examOverview" resultType="java.util.Map">
        select s.msid,s.subjectid,s.subjectname,s.sdate,s.begintime,s.endtime,s.msstate,s.pstate,s.ptstate,
        s.pbstate,s.checkset,s.checkstate,s.invigilatestate,p.answered,p.ptype,s.teacherid,
        u.username as teachername,p.mpid,p.yjstate,e.exammode,e.examstate,e.hashb,e.gradeid,s.leaderid,u2.username as leadername
        ,if(ps.batchid is not null,ps.mpsid,null) scannum,count(ps.mpsid) stunum
        from ms_subject s left join t_user u on s.teacherid=u.userid
        left join t_user u2 on s.leaderid=u2.userid
        left join ms_paper_student ps on s.msid = ps.msid
        left join ms_paper p on p.msid = s.msid
        left join ms_exam e on s.examid=e.examid
        where s.examid=#{examid} group by s.msid order by s.msid
    </select>
    <!--考试进度总览-批阅仲裁疑难数量-线上-->
    <select id="examOverviewProblemForOnline" resultType="java.util.Map">
        select s.msid,count(if(q.arbitratestate=1,q.mpsqid,null)) as arbitratenum
        ,count(if(q.hasproblem=1,q.mpsqid,null)) as problemnum
        ,count(if(q.checked in (1,2),q.mpsqid,null)) as tjnum
        ,count(if(q.checked=3,q.mpsqid,null)) as pynum
        from ms_subject s
        left join ms_paper_student_question q on s.examid=q.examid and s.subjectid=q.subjectid
        where s.examid=#{examid} group by s.msid order by s.msid
    </select>
    <!--考试进度总览-批阅仲裁疑难数量-线下-->
    <select id="examOverviewProblemForOffline" resultType="java.util.Map">
        select s.msid,count(if(q.arbitratestate=1,q.mpsbid,null)) as arbitratenum
        ,count(if(q.hasproblem=1,q.mpsbid,null)) as problemnum
        ,count(if(q.checked in (1,2),q.mpsbid,null)) as tjnum
        ,count(if(q.checked=3,q.mpsbid,null)) as pynum
        from ms_subject s
        left join ms_paper_student_block q on s.examid=q.examid and s.subjectid=q.subjectid
        where s.examid=#{examid} group by s.msid order by s.msid
    </select>

    <!--根据考试科目id获取考试科目基本信息-->
    <select id="getESubjectByMsId" resultType="java.util.Map">
        select s.sdate,s.begintime,s.endtime,s.subjectname
        ,e.examname,e.schoolid,e.examid,e.gradeid,e.examtype,e.createid,e.schoolid,e.hashb
        from ms_subject s left join ms_exam e on s.examid = e.examid
        where s.msid=#{msid}
    </select>

    <!--更改科目试卷设置状态-->
    <update id="updatePstateByMpid">
        update ms_subject s inner join ms_paper p on s.msid=p.msid set s.pstate=#{pstate} where p.mpid=#{mpid}
    </update>

    <!--获取考试下学科组长权限科目-->
    <resultMap id="examSubjectByLeader" type="java.util.Map">
        <id property="examid" column="examid"/>
        <id property="examname" column="examname"/>
        <collection property="subjects" ofType="java.util.Map" javaType="java.util.List"/>
    </resultMap>
    <select id="getExamLeadSubjects" resultMap="examSubjectByLeader">
        select e.examid,e.examname,s.msid,s.subjectid,s.subjectname
        from ms_subject s left join ms_exam e on s.examid=e.examid
        where s.examid=#{examid} and s.leaderid=#{leaderid} order by s.msid
    </select>
    <!--获取考试下所有科目名称-->
    <select id="listSubjectNameByExamId" resultType="java.util.Map">
        select distinct s.subjectid,s.subjectname,p.mpid from ms_subject s
        left join ms_paper p on s.msid=p.msid where s.examid=#{examid} order by s.msid
    </select>
    <!--根据考试科目id获取阅卷状态-->
    <select id="getCheckStateByMsId" resultType="java.lang.Integer">
        select s.checkstate from ms_subject s where s.msid=#{msid}
    </select>

    <!--更改科目试卷模板设置状态-->
    <update id="updatePtstateByMpid">
        update ms_subject s,ms_paper p set s.ptstate=#{ptstate} where s.msid=p.msid and p.mpid=#{mpid}
    </update>
    <!--更改科目试卷题块设置状态-->
    <update id="updatePbstateByMpid">
        update ms_subject s,ms_paper p set s.pbstate=#{pbstate} where s.msid=p.msid and p.mpid=#{mpid}
    </update>
    <!--重置试卷模板和题块状态-->
    <update id="updateTempAndBlockStateByMpid">
        update ms_subject s,ms_paper p set s.ptstate=0,s.pbstate=0 where s.msid=p.msid and p.mpid=#{mpid}
    </update>

    <!--更改考试科目监考设置状态-->
    <update id="updateInvigilateStateByMsId">
        update ms_subject set invigilatestate=#{invigilatestate} where msid=#{msid}
    </update>
    <!--更改考试科目阅卷设置状态-->
    <update id="updateCheckSetByMsId">
        update ms_subject s inner join ms_paper p on s.msid=p.msid
        set s.checkset=#{checkset},p.correcttype=#{correcttype}
        where s.msid=#{msid}
    </update>
    <!--更改阅卷设置状态-->
    <update id="updateCheckSetByMpId">
        update ms_subject s inner join ms_paper p on s.msid=p.msid
            set s.checkset=#{checkset} where p.mpid=#{mpid}
    </update>

    <!--考试监控-考试基本信息-->
    <select id="getExamSubjectByMsid" resultType="java.util.Map">
        select ms.sdate,ms.begintime,ms.endtime,ms.subjectname
        ,e.examname,e.schoolid,e.examid,e.gradeid,e.examtype,e.createid,e.schoolid,e.hashb
        from ms_subject ms left join ms_exam e on ms.examid = e.examid
        where ms.msid=#{msid}
    </select>

    <!--定时任务-科目开始考试-->
    <update id="updateStartSubject">
        update ms_exam e left join ms_subject ms on e.examid = ms.examid
        set ms.msstate=1
        where e.examstate>=1 and e.deleted=1 and e.exammode=3
        and ms.msstate=0 and concat(ms.sdate,' ',ms.begintime)&lt;=#{strtime}
    </update>
    <!--定时任务-科目结束考试-获取待结束考试-->
    <select id="listMsidsForEndSubject" resultType="java.lang.Integer">
        select ms.msid from ms_subject ms left join ms_exam e on ms.examid=e.examid
        where e.examstate>=1 and e.deleted=1 and e.exammode=3 and ms.msstate=1
        and adddate(concat(ms.sdate,' ',ms.endtime),interval 5 minute) &lt;#{strtime}
    </select>
    <!--定时任务-科目对应考试下所有科目-->
    <select id="listSubjectAndStateByMsids" resultType="com.xhkjedu.smarking.model.exam.MsSubject">
        select distinct s.msid,s.msstate,s.examid from ms_subject s where s.examid in
        (select t.examid from ms_subject t where t.msid in
        <foreach collection="list" item="msid" open="(" close=")" separator=",">#{msid}</foreach>
    </select>
    <!--获取考试科目ids-->
    <select id="listSubjectIdsByExamId" resultType="java.lang.String">
        select subjectid from ms_subject where examid=#{examid}
    </select>
    <!--获取考试科目信息-->
    <select id="listByExamId" resultType="java.util.Map">
        select s.msid,s.subjectid,s.subjectname,s.sdate,s.begintime,s.endtime,s.teacherid,u.username as teachername
        from ms_subject s left join t_user u on s.teacherid=u.userid
        where examid=#{examid}
    </select>
    <!--单科设置信息-->
    <select id="getSet" resultType="java.util.Map">
        <if test="exammode!=3">
            select s.msid,s.subjectid,s.subjectname,s.sdate,s.begintime,s.endtime,s.pstate,s.ptstate,s.pbstate,s.checkset,s.checkstate,s.invigilatestate,p.mpid,p.yjstate,3 as correcttype
            ,count(distinct ps.mpsid) as stunum
            ,count(distinct if(ps.batchid is not null,ps.mpsid,null)) as scannum
            ,count(distinct pb.mblockid) as blocknum
            ,count(distinct if(tb.localcoor is null or tb.localcoor='',tb.mblockid,null)) as wkxnum
            ,count(distinct if(pc.dispenseway!=0,pc.mblockid,null)) as ysznum
            ,count(distinct if(pb.blocktype=2,pb.mblockid,null))-count(distinct if(pc.dispenseway!=0,pc.mblockid,null)) as wsznum
            ,count(distinct if(psb.arbitratestate in(1,2),psb.mpsbid,null)) as arbitratenum
            ,count(distinct if(psb.hasproblem in(1,2),psb.mpsbid,null)) as problemnum
            from ms_subject s left join ms_paper_student ps on s.msid = ps.msid
            left join ms_paper p on p.msid = s.msid
            left join ms_paper_block pb on pb.mpid = ps.mpid
            left join ms_template t on t.mpid=ps.mpid
            left join ms_template_block tb on tb.mtid=t.mtid
            left join ms_paper_check pc on pc.mpid=ps.mpid
            left join ms_paper_student_block psb on psb.mpsid=ps.mpsid and psb.mblockid=pb.mblockid
            where s.examid=#{examid} and s.subjectid=#{subjectid}
        </if>
        <if test="exammode==3">
            select s.msid,s.subjectid,s.subjectname,s.sdate,s.begintime,s.endtime,s.pstate,s.ptstate,s.pbstate,s.checkset,s.checkstate,s.invigilatestate,p.mpid,p.correcttype
            ,count(distinct if(pc.dispenseway!=0,pc.mblockid,null)) as ysznum
            ,if(p.correcttype=1,(select count(*) from ms_class c where c.examid=s.examid and c.subjectid=s.subjectid)
            ,count(distinct if(pb.qtype=2,pb.mptqid,null)))-count(distinct if(pc.dispenseway!=0,pc.mblockid,null)) as wsznum
            ,count(distinct if(psb.arbitratestate in(1,2),psb.mpsqid,null)) as arbitratenum
            ,count(distinct if(psb.hasproblem in(1,2),psb.mpsqid,null)) as problemnum
            from ms_subject s left join ms_paper p on s.msid = p.msid
            left join ms_paper_qtype_question pb on pb.mpid = p.mpid
            left join ms_paper_check pc on pc.mpid=p.mpid
            left join ms_paper_student_question psb on psb.mpid=p.mpid
            where s.examid=#{examid} and s.subjectid=#{subjectid}
        </if>
    </select>
    <!--考试报告科目列表-->
    <select id="listReportSubject" resultType="com.xhkjedu.smarking.model.paper.MsPaper">
        select s.subjectid,s.subjectname,s.msid,p.mpid,p.pnum,p.mergepnum,p.pscore,p.correcttype,p.ptype
        from ms_subject s inner join ms_paper p on s.msid=p.msid
        where s.examid=#{examid} and s.msid=2
        <if test="exammode!=3">
            and s.msstate=2
        </if>
    </select>
    <!--定时任务-科目结束考试-->
    <update id="updateEndSubject">
        update ms_subject ms
        left join ms_paper_student mps on ms.msid=mps.msid and mps.sstate in(0,1)
        set ms.msstate=2,mps.sstate=(case when mps.sstate=0 then 3 else 2 end)
        ,mps.endtime=(case when mps.sstate=1 then #{timestamp} else mps.endtime end)
        ,mps.stuscoretype=(case when mps.sstate=1 then 1 else 0 end)
        ,mps.stuscore=(case when mps.sstate=1 then
        (SELECT SUM(IFNULL(q.stuscore,0)) FROM ms_paper_student_question q WHERE q.mpsid=mps.mpsid)
        else 0 end)
        ,mps.costtime=(case where mps.sstate=1 then
        (SELECT SUM(IFNULL(q.costtime,0)) FROM ms_paper_student_question q WHERE q.mpsid=mps.mpsid)
        else 0 end)
        where ms.msstate=1 and ms.msid in
        <foreach collection="msids" item="msid" separator="," open="(" close=")">#{msid}</foreach>
    </update>
    <!--修改试卷结构后更改后续操作状态-->
    <update id="updatePaperFollowup">
        update ms_subject s inner join ms_paper p on s.msid=p.msid
            set s.ptstate=0,s.pbstate=0,s.checkset=0 where p.mpid=#{mpid}
    </update>

</mapper>
