<?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.papercheck.MsPaperCheckMapper">
    <!--更新题块阅卷状态-->
    <update id="updateCheckStateByMsId">
        update ms_paper_check set checkstate = #{checkstate} where msid = #{msid}
    </update>
    <!--根据mpid和mblockids删除阅卷方式-->
    <delete id="deleteByMpIdAndBlockIds">
        delete from ms_paper_check where mpid = #{mpid} and mblockid in
        <foreach collection="mblockids" item="mblockid" open="(" separator="," close=")">
            #{mblockid}
        </foreach>
    </delete>
    <!--根据题块id获取阅卷方式id-->
    <select id="getMpcIdByMBlockId" resultType="java.lang.Integer">
        select mpcid from ms_paper_check where mpid = #{mpid} and mblockid = #{mblockid}
    </select>

    <resultMap id="paperCheckResultMap" type="java.util.Map">
        <result property="mpid" column="mpid"/>
        <result property="msid" column="msid"/>
        <result property="mblockid" column="mblockid"/>
        <result property="blockname" column="blockname"/>
        <result property="blockorder" column="blockorder"/>
        <result property="bqnum" column="bqnum"/>
        <result property="bqscore" column="bqscore"/>
        <result property="mpcid" column="mpcid"/>
        <result property="checktype" column="checktype"/>
        <result property="dispenseway" column="dispenseway"/>
        <result property="arbitratename" column="arbitratename"/>
        <collection property="teachers" select="listCheckTeacher" column="mblockid"/>
    </resultMap>
    <!--题块阅卷教师列表-->
    <select id="listCheckTeacher" resultType="java.util.Map">
        select mct.teacherid,u.username as teachername
        from t_user u,ms_paper_check_teacher mct
        where u.userid = mct.teacherid and mct.mblockid = #{mblockid}
    </select>
    <!--阅卷分配-按题块列表（线下）-->
    <select id="listPaperCheck" resultMap="paperCheckResultMap">
        select pb.mpid,p.msid,pb.mblockid,pb.blockname,pb.blockorder,pb.bqnum,pb.bqscore
        ,pc.mpcid,pc.checktype,pc.dispenseway
        ,if(pc.checktype=2,(select u.username from t_user u where u.userid=pc.arbitrateid),'') as arbitratename
        from ms_paper_check pc right join ms_paper_block pb on pc.mblockid = pb.mblockid
        inner join ms_paper p on pb.mpid = p.mpid
        where pb.mpid = #{mpid} and pb.blocktype = 2
        order by pb.blockorder
    </select>
    <!--获取试卷分配总数-->
    <select id="getDispenseNum" resultType="java.util.Map">
        select count(distinct ps.studentid) as signupnum
        <if test="correcttype!=null and correcttype==3">
            ,(select count(*) from ms_paper_student_block psb where psb.mpid=#{mpid} and psb.mblockid=#{mblockid} and psb.checked!=0) as commitnum
        </if>
        <if test="correcttype!=null and correcttype!=3">
            ,(select count(*) from ms_paper_student_question psb where psb.mpid=#{mpid} and psb.mptqid=#{mblockid} and psb.checked!=0) as commitnum
        </if>
        ,(select count(*) from ms_paper_check_teacher_task t WHERE t.mpid=#{mpid} AND t.mblockid=#{mblockid}) as tasknum
        from ms_paper_student ps
        where ps.mpid = #{mpid}
    </select>
    <!--获取题块分配教师列表-->
    <select id="listTeacherByMBlockId" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        select pct.mpctid, pct.teacherid, count(pctt.mpcttid) as tasknum,pct.mpid,pct.mblockid
        ,count(if(pctt.checkstate in(2,3),1,null)) as checkednum,count(pctt.mpcttid) as assignednum
        from ms_paper_check_teacher pct left join ms_paper_check_teacher_task pctt on pct.mblockid = pctt.mblockid and pct.teacherid = pctt.teacherid
        where pct.mpid = #{mpid} and pct.mblockid = #{mblockid}
        group by pct.teacherid
        order by assignednum desc
    </select>
    <!--根据题块id获取阅卷信息-->
    <select id="findByMBlockId" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheck">
        select mpcid,mpid,mblockid,checktype,arbitrateid,diffscore,hidefirstscore,scoreway,finalscore,roundvalue,dispenseway
        ,checkstate,correcttype
        from ms_paper_check where mpid = #{mpid} and mblockid = #{mblockid}
    </select>
    <!--获取学生作答及批阅信息（按题块）-->
    <select id="listStudentByMBlockId" resultType="com.xhkjedu.smarking.vo.papercheck.MsPaperCheckVo">
        select psb.mpsbid,psb.mblockid,psb.checked,psb.firstcid,psb.firstcime,psb.secondcid,psb.secondctime
        from ms_paper_student_block psb
        where psb.mblockid=#{mblockid}
        <if test="numtype!=null and numtype==1">
            and psb.stuanswer is not null and psb.stuanswer!=''
        </if>
        group by psb.mpsbid
        order by psb.mpsbid
    </select>
    <!--获取学生作答及批阅信息（按试题）-->
    <select id="listStudentByMpsqId" resultType="com.xhkjedu.smarking.vo.papercheck.MsPaperCheckVo">
        select psq.mpsqid as mpsbid,psq.mptqid as mblockid,psq.checked,psq.firstcid,psq.firstcime,psq.secondcid,psq.secondctime
        from ms_paper_student_question psq
        where psq.mptqid=#{mblockid}
        <if test="numtype!=null and numtype==1">
            and psq.stuanswer is not null and psq.stuanswer!=''
        </if>
        group by psq.mpsqid
        order by psq.mpsqid
    </select>
    <!--阅卷分配-按班级列表（线上）-->
    <resultMap id="classResultMap" type="java.util.Map">
        <result property="gradeid" column="gradeid"/>
        <result property="mblockid" column="mblockid"/>
        <result property="classname" column="classname"/>
        <result property="mpid" column="mpid"/>
        <result property="msid" column="msid"/>
        <result property="mpcid" column="mpcid"/>
        <collection property="teachers" select="listCheckTeacher" column="mblockid"/>
    </resultMap>
    <select id="listSubjectClass" resultMap="classResultMap">
        select c.gradeid,mc.classid as mblockid,mc.classname,p.mpid,p.msid,pc.mpcid
        from ms_paper p inner join ms_class mc on p.examid=mc.examid and mc.subjectid=p.subjectid
        inner join t_class c on mc.classid=c.classid
        left join ms_paper_check pc on p.mpid=pc.mpid and pc.mblockid=mc.classid
        where p.mpid=#{mpid}
        order by c.classtype,c.classorder,mc.mcid
    </select>
    <!--阅卷分配-按试题列表（线上）-->
    <resultMap id="questionResultMap" type="java.util.Map">
        <result property="qorder" column="qorder"/>
        <result property="qn" column="qn"/>
        <result property="mblockid" column="mblockid"/>
        <result property="mpid" column="mpid"/>
        <result property="qscore" column="qscore"/>
        <result property="msid" column="msid"/>
        <result property="mpcid" column="mpcid"/>
        <result property="checktype" column="checktype"/>
        <result property="dispenseway" column="dispenseway"/>
        <result property="arbitratename" column="arbitratename"/>
        <collection property="teachers" select="listCheckTeacher" column="mblockid"/>
    </resultMap>
    <select id="listQuestion" resultMap="questionResultMap">
        select q.qorder,q.qn,q.mptqid as mblockid,q.mpid,q.qscore,p.msid,pc.mpcid,pc.checktype,pc.dispenseway
        ,if(pc.checktype=2,(select u.username from t_user u where u.userid=pc.arbitrateid),'') as arbitratename
        from ms_paper_qtype_question q left join ms_paper_check pc on q.mpid=pc.mpid
        inner join ms_paper p on q.mpid=p.mpid
        where q.mpid=#{mpid} and q.qtype=2
    </select>
    <!--获取需要批阅的数量-->
    <select id="getNeedCheckNum" resultType="java.lang.Integer">
        <if test="correcttype!=null and correcttype==2">
            select count(distinct psq.mpsqid) as needchecknum
            from ms_paper_student_question psq
            where psq.answered=1 and psq.mptqid=#{mblockid}
        </if>
        <if test="correcttype!=null and correcttype==3">
            select count(distinct psb.mpsbid) as needchecknum
            from ms_paper_student_block psb
            where psb.mblockid=#{mblockid}
        </if>
    </select>
    <!--获取阅卷设置状态-->
    <select id="getCheckSetById" resultType="java.lang.Integer">
        select if((checktype=1 or (checktype=2 and arbitrateid is not null)) and dispenseway in(1,2,3) and count(pct.mpctid)>0,2,1)
        from ms_paper_check pc left join ms_paper_check_teacher pct on pc.mblockid=pct.mblockid and pc.mpid=pct.mpid
        where pc.mpcid=#{mpcid}
    </select>
</mapper>
