<?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.sstudy.mapper.paper.PaperTeaAppMapper">
    <resultMap id="paperResult" type="com.xhkjedu.sstudy.vo.paper.PaperTeacherAppVo">
        <result property="paperid" column="paperid"/>
        <result property="papername" column="papername"/>
        <result property="papernum" column="papernum"/>
        <result property="paperscore" column="paperscore"/>
        <result property="papertype" column="papertype"/>
        <result property="paperclass" column="paperclass"/>
        <result property="starttime" column="starttime"/>
        <result property="stoptime" column="stoptime"/>
        <result property="schoolid" column="schoolid"/>
        <result property="logschoolid" column="logschoolid"/>
        <result property="createname" column="createname"/>
        <result property="hashb" column="hashb"/>
        <result property="aftercommit" column="aftercommit"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo"
                    select="listPaperStu" column="{paperid=paperid,createid=createid,checkid=checkid,logschoolid=logschoolid,classids=classids}">
        </collection>
    </resultMap>
    <select id="listPaperStu" resultType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo">
        select (case when ps.psstate in(0,3) then 0 when ps.pschecked=1 then 2 else 1 end) as pschecked
        from t_paper_student ps
        where ps.paperid=#{paperid} and ps.classid in(${classids})
        <!--<if test="checkid!=createid">
            and ps.checkid=#{checkid}
        </if>-->
    </select>
    <select id="listTeacherApp" resultMap="paperResult">
        select p.paperid,p.papername,p.papernum,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createid,#{paper.teacherid} as checkid
        ,min(pg.starttime) as starttime,min(pg.stoptime) as stoptime,pg.checked,p.subjectid,p.paperscore,p.schoolid,#{paper.schoolid} logschoolid
        ,group_concat(pg.classid) classids,p.hashb,p.aftercommit
        ,(select u.username from t_user u where u.userid=p.createid)createname
        from t_paper p left join t_paper_class pg on p.paperid=pg.paperid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and (d.disabled=0 or p.directorid is null) and (p.createid=#{paper.teacherid} or pg.checkid=#{paper.teacherid} or
        (p.hashb=1 and p.schoolid=#{paper.schoolid} and pg.classid in (${paper.classids})) or
        (pg.classid in (${paper.bxclassids})))
        <if test="paper.lsbid!=null and paper.lsbid!='0'.toString() and paper.lsbid!=''">
            and p.lsbid=#{paper.lsbid}
        </if>
        <if test="paper.subjectid!=null and paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!=''">
            and p.directorid=#{paper.directorid}
        </if>
        group by p.paperid
        having min(pg.checked)=#{paper.psstate}
        order by p.createtime desc,p.paperid desc
    </select>

    <!--作业列表-我的-->
    <select id="listMyTeacherApp" resultMap="paperResult">
        select p.paperid,p.papername,p.papernum,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createid,#{paper.teacherid} as checkid
        ,min(pg.starttime) as starttime,min(pg.stoptime) as stoptime,pg.checked,p.subjectid,p.paperscore,p.schoolid,#{paper.schoolid} logschoolid
        ,group_concat(pg.classid) classids,p.hashb,p.aftercommit
        ,(select u.username from t_user u where u.userid=p.createid)createname
        from t_paper p left join t_paper_class pg on p.paperid=pg.paperid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.createid=#{paper.teacherid} and (d.disabled=0 or p.directorid is null)
        <if test="paper.lsbid!=null and paper.lsbid!='0'.toString() and paper.lsbid!=''">
            and p.lsbid=#{paper.lsbid}
        </if>
        <if test="paper.subjectid!=null and paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!=''">
            and p.directorid=#{paper.directorid}
        </if>
        group by p.paperid
        having min(pg.checked)=#{paper.psstate}
        order by p.createtime desc,p.paperid desc
    </select>
    <!--未布置作业列表-->
    <select id="listWbz" resultType="com.xhkjedu.sstudy.vo.paper.PaperTeacherAppVo">
        select p.paperid,p.papername,p.papernum,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createid
        ,p.subjectid,p.paperscore,p.schoolid,p.aftercommit
        ,(select u.username from t_user u where u.userid=p.createid)createname
        from t_paper p left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.createid=#{paper.teacherid} and p.paperstate=#{paper.psstate} and (d.disabled=0 or p.directorid is null)
        order by p.createtime desc,p.paperid desc
    </select>
    <!--作业情况-->
    <resultMap id="paperDetailResult" type="com.xhkjedu.sstudy.vo.paper.PClassVo">
        <result property="classid" column="classid"/>
        <result property="classname" column="classname"/>
        <result property="gradeid" column="gradeid"/>
        <result property="checked" column="checked"/>
        <result property="schoolid" column="gschoolid"/>
        <result property="checkid" column="checkid"/>
        <result property="checkname" column="checkname"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo"
                    select="listPaperClassStu" column="{paperid=paperid,classid=classid,schoolid=schoolid}">
        </collection>
    </resultMap>
    <select id="listPaperClassStu" resultType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo">
        select (case when ps.psstate in(0,3) then 0 when ps.pschecked=1 then 2 else 1 end) as pschecked
        from t_paper_student ps where ps.paperid=#{paperid} and ps.classid=#{classid}
    </select>
    <select id="getPaperDetail" resultMap="paperDetailResult">
        select pg.classid,c.classname,c.gradeid,pg.checked,pg.paperid,pg.schoolid gschoolid,#{schoolid} as schoolid
        ,pg.checkid,u.username checkname
        from t_paper_class pg left join t_class c on pg.classid=c.classid
        left join t_user u on pg.checkid=u.userid
        where pg.paperid=#{paperid}
        <if test="classids!=null and classids!=''">
            and pg.classid in(${classids})
        </if>
        <!--<if test="checkid!=null and checkid!=0">
            and pg.checkid=#{checkid}
        </if>-->
        group by pg.classid
        order by if(c.schoolid=#{schoolid},0,c.schoolid),c.classtype,c.classorder,c.classid
    </select>

    <!--作业报告-->
    <resultMap id="reportResult" type="com.xhkjedu.sstudy.vo.paper.PaperTeacherReportVo">
        <result property="qorder" column="qorder"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo">
            <result property="studentid" column="studentid"/>
            <result property="pschecked" column="pschecked"/>
        </collection>
    </resultMap>
    <!--20220421修改，不再判断学生试题是否提交只要作业提交了，就参与统计-->
    <!--<select id="getReport" resultMap="reportResult">
        select q.qorder,q.studentid
        ,(case when q.stuscore=q.qscore then 1 else 0 end)pschecked
        <if test="paper.papertype==1 or paper.papertype==3">
            from t_paper_student_question q
        </if>
        <if test="paper.papertype==2">
            from t_paper_student_scantron q
        </if>
        left join t_paper_student ps on q.paperid=ps.paperid and q.studentid=ps.studentid
        where q.paperid=#{paper.paperid} and q.classid=#{paper.classid} and ps.psstate>0 group by q.qorder,q.studentid
        order by q.qorder
    </select>-->
    <!--20230406修改，不再判断学生试题是否提交只要收到作业，就参与统计-->
    <select id="getReport" resultType="com.xhkjedu.sstudy.vo.paper.PaperTeacherReportVo">
        <if test="paper.papertype==1 or paper.papertype==3">
            select ptq.ptqorder as qorder,ptq.qn
            ,truncate(SUM(IFNULL(psq.stuscore,0))*100/(ptq.ptqscore*count(ps.psid)),2)correctrate
            from t_paper_qtype_question ptq
            left join t_paper_student ps on ptq.paperid=ps.paperid and ps.classid=#{paper.classid}
            left join t_paper_student_question psq on psq.psid=ps.psid and ptq.questionid=psq.questionid
            where ptq.paperid=#{paper.paperid}
            <if test="paper.papertype==3">
                and locate(concat('"',ptq.questionpid,'"'),ps.questionids)>0
            </if>
            group by ptq.ptqorder order by ptq.ptqorder
        </if>
        <if test="paper.papertype==2">
            select ptq.psorder as qorder,ptq.qn
            ,truncate(SUM(IFNULL(psq.stuscore,0))*100/(ptq.psscore*count(ps.psid)),2)correctrate
            from t_paper_scantron ptq
            left join t_paper_student ps on ptq.paperid=ps.paperid and ps.classid=#{paper.classid}
            left join t_paper_student_scantron psq on ptq.pscaid=psq.pscaid and psq.psid=ps.psid
            where ptq.paperid=#{paper.paperid}
            group by ptq.pscaid order by ptq.psorder
        </if>
    </select>

    <!--未提交学生列表-->
    <select id="listWtjStudent" resultType="com.xhkjedu.sstudy.vo.paper.PStudentVo">
        select u.username studentname,u.headpic
        from t_paper_student ps left join t_user u on ps.studentid=u.userid
        where ps.paperid=#{paperid} and ps.psstate in(0,3) and ps.classid=#{classid}
        order by convert(u.username using gbk)
    </select>
    <!--已提交学生列表-->
    <select id="listYtjStudent" resultType="com.xhkjedu.sstudy.vo.paper.PStudentVo">
        select u.username studentname,ps.psid,ps.stoptime,ps.costtime,ps.pschecked,#{paper.papertype} papertype
        ,u.headpic,ps.paperscore,ps.userscore,ps.autocheck
        ,(select count(ps2.psid) from t_paper_student ps2 where ps2.paperid=#{paper.paperid} and ps2.classid=#{paper.classid})classnum
        from t_paper_student ps left join t_user u on ps.studentid=u.userid
        where ps.paperid=#{paper.paperid} and ps.psstate in(1,2,4) and ps.classid=#{paper.classid}
        order by ps.pschecked,ps.userscore ${paper.fieldorder},convert(u.username using gbk)
    </select>
    <!--获取学生答题卡-->
    <resultMap id="scantronResult" type="java.util.Map">
        <result property="papername" column="papername"/>
        <result property="papernum" column="papernum"/>
        <result property="scorerate" column="scorerate"/>
        <result property="papertype" column="papertype"/>
        <result property="psid" column="psid"/>
        <result property="stoptime" column="stoptime"/>
        <result property="costtime" column="costtime"/>
        <result property="rank" column="rank"/>
        <result property="paperscore" column="paperscore"/>
        <result property="userscore" column="userscore"/>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result property="qorder" column="qorder"/>
            <result property="qn" column="qn"/>
            <result property="correctrate" column="correctrate"/>
            <result property="stuscore" column="stuscore"/>
            <result property="qscore" column="qscore"/>
            <result property="qlevel" column="qlevel"/>
            <result property="questionid" column="questionid"/>
            <result property="questionpid" column="questionpid"/>
        </collection>
    </resultMap>
    <select id="getStudentScantron" resultMap="scantronResult">
        select p.papername,ps.papernum,truncate(ps.userscore*100/ps.paperscore,2)scorerate,#{paper.papertype} as papertype
        ,ps.psid,ps.stoptime,ps.costtime,q.qorder,q.qn,truncate(q.stuscore*100/q.qscore,2)correctrate,q.stuscore,q.qscore
        ,(select count(*)+1 from t_paper_student ps1
        where ps1.paperid=ps.paperid and ps1.classid=ps.classid and ps1.userscore>ps.userscore)'rank',ps.paperscore,ps.userscore
        <if test="paper.papertype==1 or paper.papertype==3">
            ,tq.qlevel,tq.questionid,tq.questionpid
        </if>
        from t_paper_student ps left join t_paper p on ps.paperid = p.paperid
        <if test="paper.papertype==1 or paper.papertype==3">
            left join t_paper_student_question q on ps.psid=q.psid
            left join t_question tq on q.questionid=tq.questionid
        </if>
        <if test="paper.papertype==2">
            left join t_paper_student_scantron q on ps.psid=q.psid
            left join t_paper_scantron tq on q.pscaid=tq.pscaid
        </if>
        where ps.psid=#{paper.psid}
    </select>
    <!--学生答题卡单题详情-->
    <select id="getStudentQuestion" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        SELECT psq.qorder,psq.useranswer,psq.converted,psq.useranswertext,psq.useranswertxt,psq.answertype,psq.qscore,psq.stuscore
        ,q.questionid,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype
        ,(select group_concat(p.pointname separator '  ') from t_question_point qp
        left join t_point p on qp.pointid=p.pointid where qp.questionid=q.questionid)pointname
        FROM t_paper_student_question psq LEFT JOIN t_question q ON psq.questionid=q.questionid
        WHERE psq.psid=#{paper.psid} and psq.qorder=#{paper.qorder} limit 1
    </select>
    <!--试题详情-->
    <resultMap id="questionDetailResult" type="com.xhkjedu.sstudy.vo.paper.PQuestionVo">
        <result property="commitrate" column="commitrate"/>
        <result property="scorerate" column="scorerate"/>
        <result property="qtypename" column="qtypename"/>
        <result property="qscore" column="qscore"/>
        <result property="avgtime" column="avgtime"/>
        <result property="mintime" column="mintime"/>
        <result property="mqtypename" column="mqtypename"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paper.PStudentVo">
            <result property="studentid" column="studentid"/>
            <result property="studentname" column="studentname"/>
            <result property="stoptime" column="answertime"/>
            <result property="costtime" column="costtime"/>
            <result property="scorerate" column="stuscore"/>
            <result property="pschecked" column="answered"/>
        </collection>
    </resultMap>
    <!--20220421修改获取提交作业的学生未基准-->
    <select id="getQuestionDetail" resultMap="questionDetailResult">
        select tq.qtypename,q.qscore,q.answertime,q.costtime,q.stuscore,q.answered,q.studentid,u.username studentname
        <if test="question.qctype==1 or question.qctype==3">
            ,mq.qtypename mqtypename
            from t_paper_student_question q left join t_question tq on q.questionid=tq.questionid
            left join t_question mq on tq.questionpid=mq.questionid
        </if>
        <if test="question.qctype==2">
            from t_paper_student_scantron q left join t_paper_scantron tq on q.pscaid=tq.pscaid
        </if>
        left join t_user u on q.studentid=u.userid
        left join t_paper_student ps on q.paperid=ps.paperid and q.studentid=ps.studentid
        where q.paperid=#{question.paperid} and q.qorder=#{question.qorder} and q.classid=#{question.classid} and ps.psstate>0
        order by q.stuscore desc,convert(u.username using gbk)
    </select>

    <!--<resultMap id="questionDetailResult" type="java.util.Map">
        <result property="qtypename" column="qtypename"/>
        <result property="qscore" column="qscore"/>
        <result property="mqtypename" column="mqtypename"/>
        <collection property="students" ofType="java.util.Map" javaType="java.util.List">
            <result property="studentid" column="studentid"/>
            <result property="studentname" column="studentname"/>
            <result property="stoptime" column="answertime"/>
            <result property="costtime" column="costtime"/>
            <result property="scorerate" column="stuscore"/>
            <result property="pschecked" column="answered"/>
        </collection>
    </resultMap>
    &lt;!&ndash;20220421修改获取提交作业的学生未基准&ndash;&gt;
    <select id="getQuestionDetail" resultMap="questionDetailResult">
        select tq.qtypename,q.qscore,q.studentid,q.answertime,q.costtime,q.stuscore,q.answered,u.username studentname
        <if test="question.qctype==1 or question.qctype==3">
            ,mq.qtypename mqtypename
            from t_paper_student_question q left join t_question tq on q.questionid=tq.questionid
            left join t_question mq on tq.questionid=mq.questionid
        </if>
        <if test="question.qctype==2">
            from t_paper_student_scantron q left join t_paper_scantron tq on q.pscaid=tq.pscaid
        </if>
        left join t_user u on q.studentid=u.userid
        left join t_paper_student ps on q.paperid=ps.paperid and q.studentid=ps.studentid
        where q.paperid=#{question.paperid} and q.qorder=#{question.qorder} and q.classid=#{question.classid} and ps.psstate>0
        order by q.stuscore desc,convert(u.username using gbk)
    </select>-->

    <!--作业发布的人数-->
    <select id="getPaperstudentNum" resultType="java.lang.Integer">
        select count(*) from t_paper_student where paperid=#{paperid} and classid=#{classid}
    </select>
    <!--作业学生列表-->
    <select id="listPaperStudent" resultType="com.xhkjedu.sstudy.vo.paper.PStudentVo">
        select u.username studentname,ps.psid,ps.stoptime,ps.costtime
        ,(case when ps.psstate in(0,3) then 0 when ps.pschecked=1 then 2 else 1 end) as pschecked
        ,u.headpic,ps.paperscore,ps.userscore,ps.papernum
        from t_paper_student ps left join t_user u on ps.studentid=u.userid
        where ps.paperid=#{paper.paperid} and ps.classid=#{paper.classid}
        order by ps.pschecked,ps.userscore ${paper.fieldorder},convert(u.username using gbk)
    </select>
</mapper>
