<?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.MsPaperCheckTeacherArbitrateMapper">
    <!--保存或更新单个任务-->
    <insert id="save">
        insert into ms_paper_check_teacher_arbitrate (mpid, mblockid, teacherid, taskorder,checkstate, mpsbid, createtime)
        values (#{m.mpid},#{m.mblockid},#{m.teacherid},#{m.taskorder},#{m.checkstate},#{m.mpsbid},#{m.createtime})
        on duplicate key update taskorder=#{m.taskorder},teacherid=#{m.teacherid}
    </insert>
    <!--更新仲裁结果-->
    <update id="correct">
        update ms_paper_check_teacher_arbitrate
        set checkstate=3,checkscore=#{param.checkscore}
        <if test="param.hasgood!=null and param.hasgood!=0">
            ,hasgood=#{param.hasgood}
        </if>
        <if test="param.hasbad!=null and param.hasbad!=0">
            ,hasbad=#{param.hasbad},problemtype=#{param.problemtype}
        </if>
        where mpid=#{param.mpid} and mpsbid=#{param.mpsbid} and teacherid=#{param.handleid}
    </update>
    <!--更新仲裁状态-->
    <update id="updateArbitrateState">
        <if test="param.correcttype==3">
            update ms_paper_student_block pb inner join ms_paper_student_question psq on pb.mpsid=psq.mpsid and pb.mblockid=psq.mblockid
            set pb.arbitrateid=#{param.handleid},pb.arbitratestate=2,psq.arbitrateid=#{param.handleid},psq.arbitratestate=2
            where pb.mpsbid=#{param.mpsbid}
        </if>
        <if test="param.correcttype==2">
            update ms_paper_student_question psq
            set psq.arbitrateid=#{param.handleid},psq.arbitratestate=2
            where psq.mpsqid=#{param.mpsbid}
        </if>
    </update>
    <!--教师阅卷-待/已仲裁列表-->
    <select id="listArbitrate" resultType="java.util.Map">
        select s.msid,s.subjectname,s.sdate,s.begintime,s.endtime,e.examname,e.gradeid,e.examtype,e.exammode
        ,pc.mpid,pc.checktype,pc.dispenseway,pc.numtype,pc.correcttype
        ,truncate((count(distinct if(psb.arbitratestate=2,psb.mpsbid,null)))*100/count(distinct psb.mpsbid),2) as checkrate
        from ms_subject s inner join ms_exam e on s.examid = e.examid
        inner join ms_paper_check pc on pc.msid = s.msid
        inner join ms_paper_student_block psb on psb.mblockid = pc.mblockid and psb.mpid = pc.mpid
        where pc.arbitrateid=#{param.handleid} and e.deleted=1 and pc.correcttype=3 and psb.arbitratestate>0
        group by s.msid
        <if test="param.checktype==0">having 100>checkrate</if>
        <if test="param.checktype==1">having 100=checkrate</if>
        union
        select s.msid,s.subjectname,s.sdate,s.begintime,s.endtime,e.examname,e.gradeid,e.examtype,e.exammode
        ,pc.mpid,pc.checktype,pc.dispenseway,pc.numtype,pc.correcttype
        ,truncate((count(distinct if(psb.arbitratestate=2,psb.mpsqid,null)))*100/count(distinct psb.mpsqid),2) as checkrate
        from ms_subject s inner join ms_exam e on s.examid = e.examid
        inner join ms_paper_check pc on pc.msid = s.msid
        inner join ms_paper_student_question psb on psb.mptqid = pc.mblockid and psb.mpid = pc.mpid
        where pc.arbitrateid=#{param.handleid} and e.deleted=1 and pc.correcttype=2 and psb.arbitratestate>0
        group by s.msid
        <if test="param.checktype==0">having 100>checkrate</if>
        <if test="param.checktype==1">having 100=checkrate</if>
        order by msid desc
    </select>
    <!--教师阅卷-待/已仲裁详情-->
    <select id="detailArbitrate" resultType="java.util.Map">
        <if test="param.correcttype==3">
            select pb.blockname,pb.blockorder,pb.bqnum
            ,pc.mblockid,pc.hidefirstscore
            ,count(distinct psb.mpsbid) as totalNum
            ,count(distinct if(psb.arbitratestate=2,psb.mpsbid,null)) as myCheckedNum
            ,count(distinct if(psb.arbitratestate=1,psb.mpsbid,null)) as myUncheckedNum
            ,(select group_concat(pbbq.bqn separator '、') from ms_paper_block_question pbbq where pbbq.mblockid = pb.mblockid) as bqn
            from ms_paper_check pc left join ms_paper_student_block psb on psb.mblockid = pc.mblockid and psb.mpid = pc.mpid
            inner join ms_paper_block pb on pc.mblockid = pb.mblockid and pb.mpid = pc.mpid
            where pc.mpid=#{param.mpid} and pc.arbitrateid=#{param.handleid} and psb.arbitratestate!=0
            group by pb.mblockid
            order by pb.blockorder
        </if>
        <if test="param.correcttype==2">
            select pb.qorder,pb.qn
            ,pc.mblockid,pc.hidefirstscore
            ,count(distinct psb.mpsqid) as totalNum
            ,count(distinct if(psb.arbitratestate=2,psb.mpsqid,null)) as myCheckedNum
            ,count(distinct if(psb.arbitratestate=1,psb.mpsqid,null)) as myUncheckedNum
            from ms_paper_check pc left join ms_paper_student_question psb on psb.mptqid = pc.mblockid and psb.mpid = pc.mpid
            inner join ms_paper_qtype_question pb on pc.mblockid = pb.mptqid and pb.mpid = pc.mpid
            where pc.mpid=#{param.mpid} and pc.arbitrateid=#{param.handleid} and psb.arbitratestate!=0
            group by pb.mptqid
            order by pb.mptqid
        </if>
    </select>
    <!--获取待仲裁任务-->
    <select id="getCorrectArbitrate" resultType="java.util.Map">
        <if test="param.correcttype==3">
            select pc.mpid,pc.mblockid,pc.correcttype,psb.stuanswer,psb.stumerge,psb.mpsbid,psb.arbitratescore,2 as answertype
            from ms_paper_check pc inner join ms_paper_student_block psb on psb.mblockid = pc.mblockid and psb.mpid = pc.mpid
            where pc.mpid=#{param.mpid} and pc.arbitrateid=#{param.handleid}
            and (psb.arbitratestate=1 or (psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid}))
            order by psb.mpsbid limit 1
        </if>
        <if test="param.correcttype==2">
            select pc.mpid,pc.mblockid,pc.correcttype,psb.stuanswer,psb.stumerge,psb.mpsqid as mpsbid,psb.arbitratescore,psb.answertype
            from ms_paper_check pc inner join ms_paper_student_question psb on psb.mptqid = pc.mblockid and psb.mpid = pc.mpid
            where pc.mpid=#{param.mpid} and pc.arbitrateid=#{param.handleid}
            and (psb.arbitratestate=1 or (psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid}))
            order by psb.mpsqid limit 1
        </if>
    </select>
    <!--获取仲裁数量-->
    <select id="getACheckNum" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        <if test="param.correcttype==3">
            select count(psb.mpsbid) as tasknum
            ,count(if(psb.arbitratestate=3 and psb.arbitrateid=#{param.handleid},psb.mpsbid,null)) as checkednum
            from ms_paper_student_block psb
            where psb.mpid=#{param.mpid} and psb.mblockid=#{param.mblockid} and psb.arbitratestate in(1,2,3)
        </if>
        <if test="param.correcttype==2">
            select count(psb.mpsqid) as tasknum
            ,count(if(psb.arbitratestate=3 and psb.arbitrateid=#{param.handleid},psb.mpsqid,null)) as checkednum
            from ms_paper_student_question psb
            where psb.mpid=#{param.mpid} and psb.mptqid=#{param.mblockid} and psb.arbitratestate in(1,2,3)
        </if>
    </select>
    <!--教师阅卷-待仲裁-上一份-->
    <select id="getPreArbitrate" resultType="java.util.Map">
        <if test="param.correcttype==3">
            select pcta.mpid,pcta.mblockid,#{param.correcttype} as correcttype,pcta.taskorder,psb.stuanswer,psb.stumerge,psb.mpsbid,psb.arbitratescore,2 as answertype
            from ms_paper_student_block psb inner join ms_paper_check_teacher_arbitrate pcta on pcta.mpid=psb.mpid and pcta.mpsbid=psb.mpsbid
            where pcta.mpid=#{param.mpid} and pcta.teacherid=#{param.handleid} and pcta.mblockid=#{param.mblockid}
            and pcta.checkstate=3 and #{param.taskorder}>pcta.taskorder
            order by pcta.taskorder desc limit 1
        </if>
        <if test="param.correcttype==2">
            select pcta.mpid,pcta.mblockid,#{param.correcttype} as correcttype,pcta.taskorder,psb.stuanswer,psb.stumerge,pcta.mpsbid,psb.arbitratescore,psb.answertype
            from ms_paper_student_question psb inner join ms_paper_check_teacher_arbitrate pcta on pcta.mpid=psb.mpid and pcta.mpsbid=psb.mpsqid
            where pcta.mpid=#{param.mpid} and pcta.teacherid=#{param.handleid} and pcta.mblockid=#{param.mblockid}
            and pcta.checkstate=3 and #{param.taskorder}>pcta.taskorder
            order by pcta.taskorder desc limit 1
        </if>
    </select>
    <!--待仲裁题块列表-->
    <resultMap id="arbitrateBlockResultMap" type="java.util.Map">
        <result property="mblockid" column="mblockid"/>
        <result property="blockname" column="blockname"/>
        <result property="blockorder" column="blockorder"/>
        <result property="bqnum" column="bqnum"/>
        <result property="hidefirstscore" column="hidefirstscore"/>
        <result property="correcttype" column="correcttype"/>
        <collection property="questions" select="listQuestion" column="mblockid"/>
    </resultMap>
    <select id="listQuestion" resultType="java.util.Map">
        select pbq.mbqid,pbq.mptqid,pbq.mbqtype,pbq.bqscore,pbq.bqorder,pbq.bqn
        ,pbq.mergeqid,pbq.mergeqn,pbq.mergescore,pbq.scorestepway,pbq.scorestep
        from ms_paper_block_question pbq where pbq.mblockid=#{mblockid}
        order by pbq.bqn
    </select>
    <select id="listBlockArbitrate" resultMap="arbitrateBlockResultMap">
        select pb.blockname,pb.blockorder,pb.bqnum
        ,pc.mblockid,pc.hidefirstscore,pc.correcttype
        from ms_paper_check pc left join ms_paper_student_block psb on psb.mblockid = pc.mblockid and psb.mpid = pc.mpid
        inner join ms_paper_block pb on pc.mblockid = pb.mblockid and pb.mpid = pc.mpid
        where pc.mpid=#{param.mpid} and (psb.arbitratestate=1 or psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid})
        <if test="param.usertype!=null and param.usertype==1">
            and pc.arbitrateid=#{param.handleid}
        </if>
        group by pb.mblockid
        order by pb.blockorder
    </select>
    <!--待仲裁试题列表-->
    <select id="listQuestionArbitrate" resultType="java.util.Map">
        select psb.qorder,psb.qn,psb.qscore
        ,pc.mblockid,pc.hidefirstscore,pc.correcttype
        from ms_paper_check pc left join ms_paper_student_question psb on psb.mptqid = pc.mblockid and psb.mpid = pc.mpid
        where pc.mpid=#{param.mpid} and (psb.arbitratestate=1 or psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid})
        <if test="param.usertype!=null and param.usertype==1">
            and pc.arbitrateid=#{param.handleid}
        </if>
        group by psb.mptqid
        order by psb.mptqid
    </select>
    <!--学科组长-获取待仲裁任务-->
    <select id="getCorrectLeaderArbitrate" resultType="java.util.Map">
        <if test="param.correcttype==3">
            select psb.mpid,psb.mblockid,#{param.correcttype} as correcttype,psb.stuanswer,psb.stumerge,psb.mpsbid,psb.arbitratescore,2 as answertype
            from ms_paper_student_block psb
            where psb.mpid=#{param.mpid} and (psb.arbitratestate=1 or (psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid}))
            order by psb.mpsbid limit 1
        </if>
        <if test="param.correcttype==2">
            select psb.mpid,psb.mpsqid as mblockid,#{param.correcttype} as correcttype,psb.stuanswer,psb.stumerge,psb.mpsqid as mpsbid,psb.arbitratescore,psb.answertype
            from ms_paper_student_question psb
            where psb.mpid=#{param.mpid} and (psb.arbitratestate=1 or (psb.arbitratestate=2 and psb.arbitrateid=#{param.handleid}))
            order by psb.mpsqid limit 1
        </if>
    </select>
    <!--仲裁历史-->
    <select id="listArbitrateHistory" resultType="java.util.Map">
        select pcta.mpctaid,pcta.mpsbid,pcta.checkscore,pcta.taskorder
        ,(case when pcta.hasgood=1 then '优' when pcta.hasbad=1 then '违' when pcta.hasproblem=1 then '疑' else '' end) as comm
        from ms_paper_check_teacher_arbitrate pcta
        where pcta.mpid=#{param.mpid} and pcta.teacherid=#{param.handleid} and pcta.mblockid=#{param.mblockid} and pcta.checkstate=3
        <if test="param.taskorder!=null and param.taskorder!=0">
            and pcta.taskorder=#{param.taskorder}
        </if>
        order by pcta.mpsbid desc
    </select>
    <!--仲裁历史详情-->
    <select id="detailHistory" resultType="java.util.Map">
        select pcta.mpid,pcta.mblockid,#{param.correcttype} as correcttype,pcta.taskorder,psq.stuanswer,psq.stumerge,pcta.mpsbid,psq.arbitratescore
        ,if(#{param.correcttype}=3,2,psq.answertype) as answertype
        ,pcta.hasgood,pcta.hasbad,pcta.hasproblem,pcta.problemtype,pcta.problemcomm,pcta.checkscore
        from ms_paper_check_teacher_arbitrate pcta
        <if test="param.correcttype==3">
            inner join ms_paper_student_block psq on pcta.mpsbid=psq.mpsbid
        </if>
        <if test="param.correcttype==2">
            inner join ms_paper_student_question psq on pcta.mpsbid=psq.mpsqid
        </if>
        where pcta.mpctaid=#{param.mpctaid}
    </select>
    <!--初评分-->
    <select id="getScore" resultType="java.util.Map">
        select psq.firstccore,psq.secondcscore,psq.qscore,psq.qorder,psq.qn
        ,pc.diffscore,pc.scoreway
        <if test="param.correcttype==3">
            from ms_paper_student_block psb inner join ms_paper_student_question psq on psq.mpsid=psb.mpsid and psq.mblockid=psb.mblockid
        </if>
        <if test="param.correcttype==2">
            from ms_paper_student_question psq
        </if>
        inner join ms_paper_check pc on psq.mpid=pc.mpid
        <if test="param.correcttype==3">
            where psb.mpsbid=#{param.mpsbid} and psb.mblockid=pc.mblockid
        </if>
        <if test="param.correcttype==2">
            where psq.mpsqid=#{param.mpsbid} and psq.mptqid=pc.mblockid
        </if>
        order by psq.mpsqid
    </select>
</mapper>
