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

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

    <!--更新试卷答案设置状态-->
    <update id="updatePaperAnswer">
        update ms_paper set answered=#{answered} where mpid=#{mpid}
    </update>

    <!--获取试卷详细信息-->
    <select id="getPaperInfoByMsid" resultType="java.util.Map">
        select s.examid,s.msid,s.subjectid,s.subjectname,s.sdate,s.begintime,s.endtime,s.pstate,
        e.gradeid,e.examtype,e.examname,e.exammode,e.examstate,p.mpid,p.ptype,p.pnum,
        p.pscore,p.hasfile,p.hearnum,p.hearfile,p.yjstate,u.username as teachername,u.username as handlename
        from ms_subject s left join ms_paper p on s.msid=p.msid
        left join ms_exam e on s.examid=e.examid
        left join t_user u on s.teacherid=u.userid
        left join t_user u2 on p.handleid=u2.userid
        where s.msid=#{msid}
    </select>


    <!--获取考试科目对应考试状态信息-->
    <select id="getExamSubjectForPaper" resultType="com.xhkjedu.smarking.vo.paper.PExamSubjectVo">
        select s.subjectid,s.pstate,e.exammode,e.examstate
        from ms_subject s left join ms_exam e on s.examid=e.examid
        left join ms_paper p on s.msid = p.msid
        where p.mpid=#{mpid}
    </select>

    <!--删除试卷对应模板、题块、批阅设置-->
    <delete id="deleteTemplageBlockByMpid">
        delete from ms_template where mpid=#{mpid};
        delete from ms_paper_block where mpid=#{mpid}
    </delete>

    <!--获取考试试卷题量和分值-->
    <select id="listNumAndScoreByExamId" resultType="com.xhkjedu.smarking.model.paper.MsPaper">
        select msid,mpid,pnum,pscore,subjectid,examid
        from ms_paper where examid=#{examid}
    </select>

    <!--教师出卷列表-->
    <select id="listExamPapersByTeacherid" resultType="java.util.Map">
        select es.msid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,u2.username as teachername,
        es.pstate,ep.mpid,ep.ptype,ep.pnum,ep.pscore,ep.answered,ep.handleid,u.username as handlename,
        es.examid,e.examname,e.examtype,e.exammode,e.examstate,e.hashb,e.schoolid
        from ms_subject es left join ms_paper ep on es.msid=ep.msid
        left join ms_exam 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="pstate == 1">
            and es.pstate=2
        </if>
        <if test="pstate != 1">
            and es.pstate!=2
        </if>
        group by es.msid
        <if test="pstate == 1">
            order by es.sdate desc,es.endtime desc
        </if>
        <if test="pstate != 1">
            order by abs(timediff(concat(es.sdate,' ',es.begintime),#{strtime}))
        </if>
    </select>

    <!--根据试卷id获取考试状态-->
    <select id="getExamStateByMpid" resultType="java.lang.Integer">
        select eb.examstate from ms_paper ep left join ms_exam eb on ep.examid = eb.examid where ep.mpid=#{mpid}
    </select>

    <!--获取试卷ID及考试模式-->
    <select id="getPaperAndMxammodeByMpid" resultType="com.xhkjedu.smarking.vo.paper.PExamSubjectVo">
        select p.mpid,e.exammode from ms_paper p left join ms_exam e on p.examid=e.examid where p.mpid=#{mpid}
    </select>
    <!--根据考试id和科目id获取试卷信息-->
    <select id="getPaperByExamIdAndSubjectId" resultType="com.xhkjedu.smarking.model.paper.MsPaper">
        select mpid, examid, msid, subjectid, ptype, pnum, pscore, answered, hasfile, hearnum, hearfile,yjstate
        from ms_paper where examid=#{examid} and subjectid=#{subjectid}
    </select>

    <!--根据试卷ID获取考试ID-->
    <select id="getExamidByMpid" resultType="java.lang.Integer">
        select examid from ms_paper where mpid=#{mpid}
    </select>
    <!--根据考试科目ID获取试卷ID-->
    <select id="getMpIdByMsId" resultType="java.lang.Integer">
        select mpid from ms_paper where msid=#{msid}
    </select>

    <!--修改题块设置后试卷中试题数量-->
    <update id="updaePaperMeregpnumByMpid">
        update ms_paper set mergepnum=#{mergepnum} where mpid=#{mpid}
    </update>
    <!--获取试卷对应科目状态-->
    <select id="getSubjectStateByMpid" resultType="com.xhkjedu.smarking.vo.paper.PExamSubjectVo">
        select s.msid,s.msstate from ms_subject s left join ms_paper p on s.msid=p.msid where p.mpid=#{mpid}
    </select>
    <!--获取试卷批阅设置-->
    <select id="getCorrectTypeByMpId" resultType="java.lang.Integer">
        select correcttype from ms_paper where mpid=#{mpid}
    </select>

    <!--获取试卷类型-->
    <select id="getPaperTypeByMpsid" resultType="java.lang.Integer">
        select p.ptype from ms_paper p left join ms_paper_student ps on p.mpid=ps.mpid where ps.mpsid=#{mpsid}
    </select>

    <!--修改试卷切割状态-->
    <update id="updatePaperCutState">
        update ms_paper set cutstate=#{cutstate} where mpid=#{mpid}
    </update>
    <!--获取试卷切割状态-->
    <select id="getPaperCutStateByMpid" resultType="java.lang.Integer">
        select cutstate from ms_paper where mpid=#{mpid}
    </select>
    <!--根据试卷ID获取科目名称和考试名称-->
    <select id="getSubjectAndExamByMpid" resultType="java.util.Map">
        select distinct s.msid,s.subjectname,e.examid,e.examname,e.exammode
        from ms_subject s left join ms_exam e on s.examid=e.examid
        left join ms_paper p on s.msid=p.msid
        where p.mpid=#{mpid}
    </select>

</mapper>
