<?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.paperstudent.PaperClassMapper">
    <!--详情-->
    <select id="selectByPaperAndClassId" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperClass">
        select pg.paperid,pg.classid,pg.stoptime,pg.showtype
        from t_paper_class pg
        where pg.paperid=#{paperid} and pg.classid=#{classid} limit 1
    </select>
    <resultMap id="groupClass" type="java.util.Map">
        <result property="schoolid" column="schoolid"></result>
        <result property="schoolname" column="schoolname"></result>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result property="classid" column="classid"></result>
            <result property="classname" column="classname"></result>
            <result property="gradeid" column="gradeid"></result>
            <result property="starttime" column="starttime"></result>
            <result property="stoptime" column="stoptime"></result>
            <collection property="students" ofType="com.xhkjedu.sstudy.vo.gradeclass.CStudentVo" javaType="java.util.List">
                <result property="studentid" column="studentid"></result>
                <result property="studentname" column="studentname"></result>
            </collection>
        </collection>
    </resultMap>
    <!--获取参与作业学生列表-->
    <select id="listPaperStudent" resultMap="groupClass">
        select pg.schoolid,s.schoolname,ps.classid,c.classname,c.gradeid,ps.studentid,u.username studentname,pg.starttime,pg.stoptime
        from t_paper_class pg left join t_paper_student ps on ps.paperid = pg.paperid and pg.classid=ps.classid
        left join t_school s on pg.schoolid=s.schoolid
        left join t_user u on ps.studentid=u.userid
        left join t_class c on ps.classid=c.classid
        where pg.paperid=#{paperid}
        order by if(s.schoolid=#{schoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder,convert(u.username using gbk)
    </select>
    <!--批阅完成-->
    <update id="updateChecked">
        update t_paper_class set checked=1 where paperid=#{paperid} and classid=#{classid}
    </update>
    <!--根据学生作业id获取作业是否完成批阅-->
    <select id="getCheckedByPsId" resultType="java.lang.Integer">
        select pg.checked from t_paper_student ps
        left join t_paper_class pg on ps.paperid=pg.paperid and ps.classid=pg.classid
        where ps.psid=#{psid}
    </select>

    <!--作业班级下未批阅学生作业数量-->
    <select id="getWpyNum" resultType="java.lang.Integer">
        select count(*) from t_paper_student where paperid=#{pg.paperid} and classid=#{pg.classid} and pschecked!=1
    </select>
    <!--获取班级作业是否结束批阅-->
    <select id="getPaperClassChecked" resultType="java.lang.Integer">
        select pg.checked from t_paper_class pg where pg.paperid=#{paperid} and pg.classid=#{classid}
    </select>
    <!--教师权限分组-->
    <select id="listGroupByTeacherId" resultType="java.util.Map">
        select c.classid,c.classname,c.gradeid,g.groupid,g.groupname,g.groupnum,g.grouptype,gs.studentid
        from t_group g left join t_class_teacher ct on ct.classid = g.classid
        left join t_class c on ct.classid = c.classid
        left join t_school s on c.schoolid=s.schoolid
        left join t_group_student gs on gs.groupid=g.groupid
        where ct.teacherid = #{teacherid} and c.year = s.year and c.schoolid=#{schoolid}
        and c.classstate = 1 and c.classnum!=0 and g.groupnum!=0
        and c.classtype <![CDATA[ < ]]>5 and (g.createid = #{teacherid} or g.grouptype = 1)
        order by c.classtype, c.classorder, g.groupid
    </select>
    <!--作业对象集合-->
    <select id="listUserByIds" resultType="java.util.Map">
        select userid,username,headpic from t_user where userid in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            ${userid}
        </foreach>
    </select>
    <!--作业试题列表-->
    <resultMap id="questionResult" type="com.xhkjedu.sstudy.vo.question.QuestionVo">
        <result property="questionid" column="questionpid"></result>
        <result property="score" column="score"></result>
        <result property="ptqorder" column="ptqorder"></result>
        <result property="complexity" column="complexity"></result>
        <result property="ctype" column="ctype"></result>
        <result property="qstem" column="qstem"></result>
        <result property="qoption" column="qoption"></result>
        <result property="qanswer" column="qanswer"></result>
        <result property="qanalyze" column="qanalyze"></result>
        <result property="createtime" column="createtime"></result>
        <result property="count" column="count"></result>
        <result property="qtypeid" column="qtypeid"></result>
        <result property="qtypename" column="qtypename"></result>
        <result property="qstemtxt" column="qstemtxt"></result>
    </resultMap>
    <select id="listPaperQuestions" resultMap="questionResult">
        select q.qtypeid,q.qtypename,ptq.questionpid,ptq.ptqscore score,ptq.ptqorder
        ,q.complexity,q.ctype,q.qstem,q.qoption,q.qanswer,q.qanalyze,qp.pointid,p.pointname,q.createtime,q.count
        ,(case when ptq.questionid!=ptq.questionpid then (select concat('[',group_concat(
        json_object('questionid',ptq2.questionid,'qn',ptq2.qn,'ptqorder',ptq2.ptqorder,'score',ptq2.ptqscore,'ctype',ptq.ctype
        ,'qstem',q2.qstem,'qoption',q2.qoption,'qanswer',q2.qanswer,'qanalyze',q2.qanalyze)),']')
        from t_paper_qtype_question ptq2 left join t_question q2 on ptq2.questionid = q2.questionid
        where ptq2.questionpid=ptq.questionpid and ptq2.paperid=ptq.paperid) else '' end)qstemtxt
        ,(select concat('[',group_concat(json_object('pointid',qp.pointid,'pointname',p.pointname)),']')
        from t_question_point qp left join t_point p on qp.pointid = p.pointid
        where qp.questionid = ptq.questionpid)source
        from t_paper_qtype_question ptq left join t_question q on ptq.questionpid = q.questionid
        left join t_question_point qp on q.questionid = qp.questionid
        left join t_point p on qp.pointid = p.pointid
        where ptq.paperid=#{paperid} group by ptq.ptqorder order by ptq.ptqorder
    </select>
    <!--学生满分试题-->
    <select id="listStuQuestion" resultType="java.util.Map">
        select q.studentid,q.questionpid questionid,(case when q.questionid=q.questionpid then q.qscore=q.stuscore
        else (select sum(q2.qscore)=sum(q2.stuscore) from t_paper_student_question q2
        where q2.paperid=q.paperid and q2.questionpid=q.questionpid and q2.studentid=q.studentid) end)score
        from(select max(psqid)psqid from t_paper_student_question
        where questionpid in
        <foreach collection="questionids" item="questionid" index="index" open="(" separator="," close=")">
            #{questionid}
        </foreach>
        and studentid in
        <foreach collection="studentids" item="studentid" index="index" open="(" separator="," close=")">
            ${studentid}
        </foreach>
        group by studentid,questionpid)a left join t_paper_student_question q on q.psqid=a.psqid having score=1
    </select>
    <!--分组学生列表-->
    <select id="listGroupStudent" resultType="java.util.Map">
        select gs.studentid,u.username studentname,u.headpic
        from t_group_student gs left join t_user u on gs.studentid = u.userid
        where gs.groupid=#{groupid} order by convert(u.username using gbk)
    </select>
    <!--获取作业母题列表-->
    <select id="listPaperPQuestions" resultType="com.xhkjedu.sstudy.vo.question.QuestionVo">
        select q.questionpid questionid,q.mscore score
        from t_paper_qtype_question q
        where q.paperid=#{paperid}
        group by q.questionpid
    </select>

    <!--获取班级设置作业时间-->
    <select id="getPaperClassTimes" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperClass">
        select starttime,stoptime from t_paper_class where paperid=#{paperid} limit 1
    </select>
</mapper>
