<?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.MsPaperCheckTeacherMapper">
    <!--批量保存-->
    <insert id="batchSave">
        <foreach collection ="list" item="s" index= "index" separator =";">
            insert into ms_paper_check_teacher (mpid, mblockid, teacherid, tasknum, createid, createtime, deleted)
            values (#{s.mpid},#{s.mblockid},#{s.teacherid},#{s.tasknum},#{s.createid},#{s.createtime},#{s.deleted})
            on duplicate key update tasknum=#{s.tasknum},deleted=#{s.deleted}
        </foreach>
    </insert>
    <!--更新批阅教师任务数量-->
    <update id="updateTaskNum">
        update ms_paper_check_teacher set tasknum = if(teacherid=#{teacherid},tasknum-#{tasknum},tasknum+#{tasknum})
        where mblockid = #{mblockid} and (teacherid = #{teacherid} or teacherid = #{addTeacherId})
    </update>
    <!--题块阅卷教师列表-->
    <select id="listCheckTeacher" resultType="java.util.Map">
        select pct.mpid,pct.mblockid,pct.teacherid,count(pctt.mpcttid) as tasknum,u.username as teachername
        ,count(if(pctt.checkstate=3,1,null)) as checknum
        from ms_paper_check_teacher pct left join t_user u on pct.teacherid = u.userid
        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
    </select>
    <!--根据题块id查询批阅教师任务信息-->
    <select id="findByBlockId" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        select pct.*,max(ifnull(pctt.taskorder,0)) as taskorder
        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.mblockid = #{mblockid} and pct.teacherid = #{teacherid} limit 1
    </select>
    <!--获取教师题块批阅信息-->
    <select id="getTeacherBlockCheck" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        select pct.mpctid,pct.mpid,pct.mblockid,pct.teacherid,count(pctt.mpcttid) as tasknum,pct.deleted
        ,count(if(pctt.checkstate in(2,3),1,null)) as checkednum
        ,group_concat(if(pctt.checkstate=1,pctt.mpcttid,null)) as mpcttids
        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.mblockid = #{mblockid} and pct.teacherid = #{teacherid}
    </select>
    <!--题块阅卷教师列表（复制用）-->
    <select id="listCheckTeacher2" resultType="com.xhkjedu.smarking.model.papercheck.MsPaperCheckTeacher">
        select * from ms_paper_check_teacher where mpid = #{mpid} and mblockid = #{mblockid}
    </select>

    <!--根据试卷ID删除阅卷教师-->
    <delete id="deleteByMpid">
        delete from ms_paper_check_teacher where mpid = #{mpid}
    </delete>
</mapper>
