<?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.exam.EBaseMapper">
    <!--考试删除-->
    <update id="deleteById">
        update e_base set deleted=10 where examid=#{examid}
    </update>
    <!--删除关联的班级-->
    <delete id="deleteClassById">
        delete c.*,cs.* from e_class c left join e_class_student cs on cs.examid=c.examid and cs.classid=c.classid
        where c.examid=#{examid}
    </delete>
    <!--考试列表-->
    <select id="listAll" resultType="java.util.Map">
        select b.examid,b.examname,b.examdate,b.gradeid,b.examtype,b.exammode,b.monitored,b.teashowrank,b.stushowrank,b.stuzero
        ,b.examstate,u.username createname,b.hashb,group_concat(distinct es.subjectname order by es.esid separator '|')subjectname
        ,count(distinct case when es.begintime is null or es.begintime='' then es.esid else null end)esnum
        ,min(es.esstate)esstate,min(es.pstate)pstate,min(ifnull(ep.correcttype,0))correcttype
        ,(case when b.exammode=1 then (count(distinct es.esid)-count(distinct ei.esid))
        else (select count(ps.epsid) from e_paper_student ps
        where ps.examid=b.examid and ps.sstate in(0,1)) end)papernum
        from e_base b left join e_subject es on es.examid=b.examid
        left join e_paper ep on ep.esid = es.esid
        left join e_subject_invigilate ei on ei.esid = es.esid
        left join t_user u on u.userid=b.createid
        where b.schoolid=#{base.schoolid} and b.deleted=1
        <if test="base.gradeid!=null and base.gradeid!=0">
            and b.gradeid=#{base.gradeid}
        </if>
        <if test="base.examstate!=null and base.examstate!=-1">
            and b.examstate=#{base.examstate}
        </if>
        <if test="base.createid!=null and base.createid!=0">
            and b.createid=#{base.createid}
        </if>
        <if test="base.begindate!=null and base.begindate!=''">
            and b.examdate between #{base.begindate} and #{base.enddate}
        </if>
        group by b.examid order by b.examid desc
    </select>
    <!--修改考试状态-->
    <update id="updateExamState">
        update e_base set examstate=#{examstate} where examid=#{examid}
    </update>
    <!--结束考试更改状态和报告状态-->
    <update id="updateExamStateAndReportState">
        update e_base set examstate=#{examstate},reportstate=1 where examid=#{examid}
    </update>
    <!--更改pdf报告生成状态-->
    <update id="updateExamReportState">
        update e_base set reportstate=#{reportstate} where examid=#{examid}
    </update>
    <!--修改报告pdf后-->
    <update id="updateExamReportSchoolState">
        update e_base set reportstate=#{reportstate},reportschool=#{reportschool} where examid=#{examid}
    </update>

    <!--发布考试-->
    <update id="postExam">
        update e_base set examcomm=#{examcomm},examstate=1 where examid=#{examid}
    </update>
    <!--考试基本信息-->
    <select id="findById" resultType="java.util.Map">
        select examid,examname,gradeid,examdate,examtype,exammode,monitored,examstate,teashowrank,stushowrank,stuzero,examcomm
        ,reportstate,reportschool,schoolid,hashb
        from e_base where examid=#{examid}
    </select>
    <!--考试基本信息(带考试人数)-->
    <select id="findById2" resultType="java.util.Map">
        select b.examid,b.examname,b.examdate,b.examtype,b.gradeid,b.exammode,b.monitored,b.examstate,b.examcomm,b.schoolid
        ,b.teashowrank,b.stushowrank,b.stuzero
        ,(select sum(c.classnum) from e_class c where c.examid=b.examid)examnum
        from e_base b where b.examid=#{examid}
    </select>
    <!--根据考试id获取名称-->
    <select id="getNameByExamId" resultType="java.lang.String">
        select examname from e_base where examid=#{examid}
    </select>
    <!--根据考试id获取考试状态-->
    <select id="getExamStateById" resultType="com.xhkjedu.sexam.vo.exam.EBaseVo">
        select eb.examid,eb.examname,eb.gradeid,eb.examstate,eb.schoolid,eb.exammode
        ,count(case when es.esstate!=1 then es.esid else null end)wwcnum
        ,(case when eb.exammode=1 then min(ep.answered) else 1 end)answered
        ,min(concat(es.sdate,' ',es.begintime,':00'))strtime
        from e_base eb left join e_subject es on eb.examid = es.examid
        left join e_paper ep on es.esid = ep.esid
        where eb.examid=#{examid}
    </select>
    <!--考试设置状态-->
    <select id="getSetState" resultType="java.util.Map">
        select count(distinct case when es.begintime is null or es.begintime='' then es.esid else null end)esnum
        ,min(es.esstate)esstate,min(es.pstate)pstate,min(ifnull(ep.correcttype,0))correcttype
        ,(case when b.exammode=1 then (count(distinct es.esid)-count(distinct ei.esid))
        else (select count(ps.epsid) from e_paper_student ps
        where ps.examid=b.examid and ps.sstate in(0,1)) end)papernum
        from e_base b left join e_subject es on es.examid=b.examid
        left join e_paper ep on ep.esid = es.esid
        left join e_subject_invigilate ei on ei.esid = es.esid
        where b.examid=#{examid}
    </select>
    <!--根据考试id获取考试状态-->
    <select id="getExamState" resultType="java.lang.Integer">
        select examstate from e_base where examid=#{examid}
    </select>
    <!--根据考试科目id获取考试状态-->
    <select id="getExamStateByEsId" resultType="java.lang.Integer">
        select examstate from e_subject es left join e_base b on b.examid = es.examid where es.esid=#{esid}
    </select>
    <!--获取考试状态和是否设置答案-->
    <select id="getExamStateAnseredById" resultType="com.xhkjedu.sexam.vo.exam.EBaseVo">
        select eb.examstate,eb.exammode,eb.reportstate
        ,(case when eb.exammode=2 then (select min(ep.answered) from e_paper ep where eb.examid = ep.examid)
        else (select count(*) from e_subject es where es.examid=eb.examid and es.pstate=2) end)answered
        from e_base eb where eb.examid=#{examid}
    </select>
    <!--考试未批阅试题数量-->
    <select id="getNoCheckNum" resultType="java.lang.Integer">
        select count(*) from e_paper_student_question
        where examid=#{examid} and answered=1 and checked!=2
    </select>
    <!--根据考试id获取状态和模式-->
    <select id="getExamStateAndMode" resultType="com.xhkjedu.sexam.vo.exam.EBaseVo">
        select eb.examstate,eb.exammode,eb.gradeid
        from e_base eb where eb.examid=#{examid}
    </select>
    <!--根据考试id获取显示排名-->
    <select id="getShowRankById" resultType="com.xhkjedu.sexam.model.exam.EBase">
        select eb.teashowrank,eb.stushowrank,eb.stuzero
        from e_base eb where eb.examid=#{examid}
    </select>
    <!--根据考试id获取pdf报告状态-->
    <select id="getReportState" resultType="java.lang.Integer">
        select reportstate from e_base where examid=#{examid}
    </select>

    <!--根据考试id获取pdf报告状态和校级报告地址-->
    <select id="getReportSchool" resultType="java.util.Map">
        select reportstate,reportschool from e_base where examid=#{examid}
    </select>
    <!--清空考试报告-->
    <update id="clearExamPdf">
        UPDATE e_base b LEFT JOIN e_class c ON b.examid=c.examid
        LEFT JOIN e_class_student s ON c.examid=s.examid AND s.classid=c.classid
        LEFT JOIN e_class_subject ecs ON b.examid = ecs.examid
        SET b.reportstate=1,b.reportschool=NULL,c.reportclass=NULL,c.reportstu=NULL,s.reportstu=NULL,ecs.reporturl=NULL
        WHERE b.examid=#{examid}
    </update>

    <!--获取学校当前年份-->
    <select id="getYearByUserid" resultType="java.lang.Integer">
        select s.year from t_user u left join t_school s on u.schoolid=s.schoolid
        where u.userid=#{userid}
    </select>

    <!--获得发起考试学校id-->
    <select id="getSchoolidByExamid" resultType="java.lang.Integer">
        select schoolid from e_base where examid=#{examid}
    </select>
    <!--获取0分学生是否参与统计-->
    <select id="getStuZeroByExamId" resultType="java.lang.Integer">
        select stuzero from e_base where examid=#{examid}
    </select>
</mapper>
