<?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.MsExamMapper">
    <resultMap id="examResultMap" type="java.util.Map">
        <id property="examid" column="examid"/>
        <collection property="subjects" select="listSubject" column="{examid=examid,exammode=exammode}"/>
    </resultMap>
    <!--发布考试-->
    <update id="postExam">
        update ms_exam set examcomm=#{examcomm},examstate=1 where examid=#{examid}
    </update>
    <!--删除-->
    <update id="deleteById">
        update ms_exam set deleted=10 where examid=#{examid}
    </update>
    <!--更新考试状态-->
    <update id="updateExamState">
        update ms_exam set examstate=#{examstate} where examid=#{examid}
    </update>
    <!--删除关联的班级-->
    <delete id="deleteClassById">
        delete c.*,cs.* from ms_class c inner join ms_class_student cs on cs.examid=c.examid and cs.classid=c.classid
        where c.examid=#{examid} and c.subjectid=cs.subjectid
    </delete>
    <!--考试科目列表-->
    <select id="listSubject" resultType="java.util.Map">
        select subjectid,subjectname
        ,if(pstate=1 and ptstate=2 and pbstate=2 and checkstate!=0 and checkset=1
        and (#{exammode}!=3 or invigilatestate=2)
        and (select count(ps.mpsid) from ms_paper_student ps where ps.msid=s.msid and ps.sstate=2 and ps.checked!=2)=0
        ,1,0) as state
        from ms_subject s where s.examid=#{examid} order by msid
    </select>
    <!--考试列表-->
    <select id="listAll" resultMap="examResultMap">
        select e.examid,e.examname,e.examdate,e.gradeid,e.examtype,e.exammode
        ,e.examstate,u.username createname,e.hashb
        from ms_exam e left join t_user u on u.userid=e.createid
        where e.schoolid=#{exam.schoolid} and e.deleted=1
        <if test="exam.gradeid!=null and exam.gradeid!=0">
            and e.gradeid=#{exam.gradeid}
        </if>
        <if test="exam.examstate!=null and exam.examstate!=-1">
            and e.examstate=#{exam.examstate}
        </if>
        <if test="exam.createid!=null and exam.createid!=0">
            and e.createid=#{exam.createid}
        </if>
        <if test="exam.begindate!=null and exam.begindate!=''">
            and e.examdate between #{exam.begindate} and #{exam.enddate}
        </if>
        group by e.examid order by e.examid desc
    </select>
    <!--根据考试id获取考试状态-->
    <select id="getExamStateById" resultType="com.xhkjedu.smarking.vo.exam.EBaseVo">
        select e.examid,e.examname,e.gradeid,e.examstate,e.schoolid,e.exammode
        ,count(if(ms.pstate!=2,ms.msid,null)) as wwcnum
        ,min(concat(ms.sdate,' ',ms.begintime,':00')) as strtime
        ,ms.pbstate
        from ms_exam e left join ms_subject ms on e.examid = ms.examid
        where e.examid=#{examid}
    </select>
    <!--考试基本信息-->
    <select id="findById" resultType="java.util.Map">
        select examid,examname,gradeid,examdate,examtype,exammode,monitored,examstate,teashowrank,stushowrank,stuzero,examcomm
        ,fixedscore,schoolid,hashb,year
        from ms_exam where examid=#{examid}
    </select>

    <!--考试状态-->
    <select id="getExamstate" resultType="java.lang.Integer">
        select examstate from ms_exam where examid=#{examid}
    </select>
    <!--获取考试学校id-->
    <select id="getSchoolIdByExamId" resultType="java.lang.Integer">
        select schoolid from ms_exam where examid=#{examid}
    </select>

    <!--批量结束考试-->
    <update id="updateBatchExamState">
        update ms_exam set examstate=2 where examid in
        <foreach collection="examids" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
</mapper>
