<?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.MsPaperCheckTeacherProblemMapper">
    <!--保存或更新单个任务-->
    <insert id="save">
        insert into ms_paper_check_teacher_problem (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="updateHasProblem">
        <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.problehid=#{param.handleid},pb.hasproblem=2,psq.problehid=#{param.handleid},psq.hasproblem=2
            where pb.mpsbid=#{param.mpsbid}
        </if>
        <if test="param.correcttype==2">
            update ms_paper_student_question psq
            set psq.problehid=#{param.handleid},psq.hasproblem=2
            where psq.mpsqid=#{param.mpsbid}
        </if>
    </update>
    <!--处理疑难卷-->
    <update id="correct">
        update ms_paper_check_teacher_problem
        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>
    <!--删除单个任务-->
    <delete id="deleteByMpsbidAndMpid">
        delete from ms_paper_check_teacher_problem where mpsbid=#{mpsbid} and mpid=#{mpid}
    </delete>
    <!--疑难卷题块列表-->
    <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"/>
        <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="listBlockProblem" resultMap="arbitrateBlockResultMap">
        select pb.mblockid,pb.blockname,pb.blockorder,pb.bqnum
        from ms_paper_student_block psb inner join ms_paper_block pb on psb.mblockid = pb.mblockid
        where psb.mpid=#{param.mpid} and (psb.hasproblem=1 or (psb.hasproblem=2 and psb.problehid=#{param.handleid}))
        group by pb.mblockid
        order by pb.blockorder
    </select>
    <!--疑难卷试题列表-->
    <select id="listQuestionProblem" resultType="java.util.Map">
        select psb.mptqid as mblockid,psb.qorder,psb.qn,psb.qscore
        from ms_paper_student_question psb
        where psb.mpid=#{param.mpid} and (psb.hasproblem=1 or (psb.hasproblem=2 and psb.problehid=#{param.handleid}))
        group by psb.mptqid
        order by psb.mptqid
    </select>
    <!--获取疑难卷任务-->
    <select id="getCorrectProblem" resultType="java.util.Map">
        select psb.stuanswer,psb.stumerge,psb.hasproblem,psb.problemtype,psb.problemcomm
        <if test="param.correcttype==3">
            ,psb.mblockid,psb.mpsbid,2 as answertype
        </if>
        <if test="param.correcttype!=3">
            ,psb.mptqid as mblockid,psb.mpsqid as mpsbid,psb.answertype
        </if>
        ,u.username as studentname,u.examno,u2.username as teachername
        <if test="param.correcttype==3">
            from ms_paper_student_block psb
        </if>
        <if test="param.correcttype!=3">
            from ms_paper_student_question psb
        </if>
        inner join t_user u on psb.studentid = u.userid
        inner join t_user u2 on psb.probleid = u2.userid
        where psb.mpid=#{param.mpid}
        and (psb.hasproblem=1 or (psb.hasproblem=2 and psb.problehid=#{param.handleid}))
        <if test="param.correcttype==3">
            order by psb.mpsbid limit 1
        </if>
        <if test="param.correcttype==2">
            order by psb.mpsqid limit 1
        </if>
    </select>
    <!--获取疑难卷数量-->
    <select id="getPCheckNum" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        <if test="param.correcttype==3">
            select count(psb.mpsbid) as tasknum
            ,count(if(psb.hasproblem=3 and psb.problehid=#{param.handleid},psb.mpsbid,null)) as checkednum
            from ms_paper_student_block psb
            where psb.mpid=#{param.mpid} and psb.hasproblem in(1,2,3)
        </if>
        <if test="param.correcttype==2">
            select count(psb.mpsqid) as tasknum
            ,count(if(psb.hasproblem=3 and psb.problehid=#{param.handleid},psb.mpsqid,null)) as checkednum
            from ms_paper_student_question psb
            where psb.mpid=#{param.mpid} and psb.hasproblem in(1,2,3)
        </if>
    </select>
    <!--获取上一份疑难卷-->
    <select id="getPreProblem" resultType="java.util.Map">
        select psq.stuanswer,psq.stumerge,psq.hasproblem,psq.problemtype,psq.problemcomm
        ,pctp.mpid,pctp.mblockid,pctp.mpsbid,pctp.taskorder,pctp.checkscore
        ,if(#{param.correcttype}=3,2,psq.answertype) as answertype
        ,u.username as studentname,u.examno,u2.username as teachername
        from ms_paper_check_teacher_problem pctp
        <if test="param.correcttype==3">
            inner join ms_paper_student_block psq on mpcta.mpsbid=psq.mpsbid
        </if>
        <if test="param.correcttype==2">
            inner join ms_paper_student_question psq on pctp.mpsbid=psq.mpsqid
        </if>
        inner join t_user u on psq.studentid = u.userid
        inner join t_user u2 on psq.probleid = u2.userid
        where pctp.mpid=#{param.mpid} and pctp.teacherid=#{param.handleid} and pctp.mblockid=#{param.mblockid} and pctp.checkstate=3
        and #{param.taskorder}>pctp.taskorder
        order by pctp.taskorder desc limit 1
    </select>
    <!--历史记录列表-->
    <select id="listHistory" resultType="java.util.Map">
        select pctp.mpctpid,pctp.mpsbid,pctp.checkscore,pctp.taskorder
        ,(case when pctp.hasgood=1 then '优' when pctp.hasbad=1 then '违' else '' end) as comm
        from ms_paper_check_teacher_problem pctp
        where pctp.mpid=#{param.mpid} and pctp.teacherid=#{param.handleid} and pctp.mblockid=#{param.mblockid} and pctp.checkstate=3
        <if test="param.taskorder!=null and param.taskorder!=0">
            and pctp.taskorder=#{param.taskorder}
        </if>
        order by pctp.mpsbid desc
    </select>
    <!--历史记录详情-->
    <select id="detailHistory" resultType="java.util.Map">
        select psq.stuanswer,psq.stumerge,psq.hasproblem,psq.problemtype,psq.problemcomm
        ,pctp.mpid,pctp.mblockid,pctp.mpsbid,pctp.taskorder,pctp.checkscore
        ,if(#{param.correcttype}=3,2,psq.answertype) as answertype
        ,u.username as studentname,u.examno,u2.username as teachername
        from ms_paper_check_teacher_problem pctp
        <if test="param.correcttype==3">
            inner join ms_paper_student_block psq on mpcta.mpsbid=psq.mpsbid
        </if>
        <if test="param.correcttype==2">
            inner join ms_paper_student_question psq on pctp.mpsbid=psq.mpsqid
        </if>
        inner join t_user u on psq.studentid = u.userid
        inner join t_user u2 on psq.probleid = u2.userid
        where pctp.mpctpid=#{param.mpctpid}
    </select>
    <!--打回重阅-->
    <select id="backCorrect">
        update ms_paper_check_teacher_task pctt
        <if test="param.correcttype==3">
            inner join ms_paper_student_block psb on pctt.mpsbid=psb.mpsbid
            inner join ms_paper_student_question psq on psb.mblockid=psq.mblockid
        </if>
        <if test="param.correcttype==2">
            inner join ms_paper_student_question psq on pctt.mpsbid=psq.mpsqid
        </if>
        set pctt.checkstate=1,psq.checked=1,psq.hasproblem=0,psq.problemtype=0,psq.problemcomm='',psq.firstcime=null,psq.secondctime=null
        <if test="param.correcttype==3">
            ,psb.checked=1,psb.hasproblem=0,psb.problemtype=0,psb.problemcomm='',psb.firstcime=null,psb.secondctime=null
        </if>
        where pctt.mpid=#{param.mpid} and pctt.mpsbid=#{param.mpsbid}
    </select>
    <!--根据题块/试题id获取学生试卷id-->
    <select id="getMpsIdByMpsbid" resultType="java.lang.Integer">
        <if test="param.correcttype==3">
            select mpsid from ms_paper_student_block where mpsbid=#{mpsbid}
        </if>
        <if test="param.correcttype!=3">
            select mpsid from ms_paper_student_question where mpsqid=#{mpsbid}
        </if>
    </select>
</mapper>
