<?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.PaperStudentQuestionMapper">
    <!--获取学生作业详情-->
    <select id="getPaperQuestion" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        SELECT psq.psqid,psq.paperid,psq.studentid,psq.answered,psq.answertime,psq.costtime,psq.checkcomm
        ,psq.useranswer,psq.answertype,psq.converted,psq.useranswertext,psq.useranswertxt,psq.checked,psq.qscore
        ,psq.stuscore,psq.qorder,psq.psid,psq.qn,psq.good
        ,q.questionid,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype,q.complexity,q.qtypename,q.hashear,q.hearfile,q.qlevel,q.sorder
        ,(CASE WHEN psq.checked=1 AND psq.stuscore=psq.qscore THEN 1 ELSE 2 END)corrected
        ,(CASE WHEN q.questionpid IS NULL THEN ''
        ELSE (SELECT json_object('qstem',fq.qstem,'qoption',fq.qoption,'qanswer',fq.qanswer,'qanalyze',fq.qanalyze
            ,'qtypename',fq.qtypename,'hashear',fq.hashear,'hearfile',fq.hearfile,'qlevel',fq.qlevel,'ctype',fq.ctype)
        FROM t_question fq WHERE fq.questionid=q.questionpid) END)mqstem
        ,(case when q.questionpid is null then (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)
            else (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.questionpid) end)pointname
        FROM t_paper_student_question psq LEFT JOIN t_question q ON psq.questionid=q.questionid
        WHERE psq.psid=#{psid} ORDER BY psq.psqid
    </select>
    <!--获取学生作业详情（无答案）-->
    <select id="getPaperQuestionNoAnswer" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        SELECT psq.psqid,psq.paperid,psq.studentid,psq.answered
        ,0 costtime,0 stuscore,'[]' qanswer
        ,psq.useranswer,psq.answertype,psq.converted,psq.useranswertxt,psq.qscore
        ,psq.qorder,psq.psid,psq.qn
        ,q.questionid,q.qstem,q.qoption,q.ctype,q.complexity,q.qtypename,q.hashear,q.hearfile,q.qlevel,q.sorder
        ,(CASE WHEN q.questionpid IS NULL THEN ''
        ELSE (SELECT json_object('qstem',fq.qstem,'qoption',fq.qoption
            ,'qtypename',fq.qtypename,'hashear',fq.hashear,'hearfile',fq.hearfile,'qlevel',fq.qlevel,'ctype',fq.ctype)
        FROM t_question fq WHERE fq.questionid=q.questionpid) END)mqstem
        FROM t_paper_student_question psq LEFT JOIN t_question q ON psq.questionid=q.questionid
        WHERE psq.psid=#{psid} ORDER BY psq.psqid
    </select>
    <!--获取学生待批改试题列表-->
    <select id="listCorrectQuestion" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        SELECT psq.psqid,psq.paperid,psq.studentid,psq.answered,psq.answertime,psq.costtime,psq.checkcomm
        ,psq.useranswer,psq.answertype,psq.converted,psq.useranswertext,psq.useranswertxt,psq.checked,psq.qscore
        ,psq.stuscore,psq.qorder,psq.psid,psq.qn,psq.good
        ,q.questionid,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype,q.complexity,q.qtypeid,q.qtypename
        ,(CASE WHEN psq.checked=1 AND psq.stuscore=psq.qscore THEN 1 ELSE 2 END)corrected
        FROM t_paper_student_question psq LEFT JOIN t_question q ON psq.questionid=q.questionid
        WHERE psq.psid=#{psid} and psq.answered=1 and q.ctype in(3,13,14,15)
        ORDER BY psq.qorder
    </select>
    <!--作业关联试题列表-->
    <select id="listPaperQuestion" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion">
        SELECT ps.paperid,ps.classid,ps.studentid,ptq.questionid,ptq.ptqscore AS qscore,ptq.ptqorder qorder
        ,ps.schoolid,ps.psid,ptq.qn,ptq.questionpid
        FROM t_paper_student ps LEFT JOIN t_paper_qtype_question ptq ON ps.paperid=ptq.paperid WHERE ps.psid=#{psid}
    </select>
    <!--批量保存-->
    <insert id="batchSave" parameterType="java.util.List">
        INSERT INTO t_paper_student_question (paperid,classid,studentid,questionpid,questionid,qscore,qorder,qn,schoolid,psid)
        VALUES
        <foreach collection ="list" item="r" index= "index" separator =",">
            (#{r.paperid},#{r.classid},#{r.studentid},#{r.questionpid},#{r.questionid}
            ,#{r.qscore},#{r.qorder},#{r.qn},#{r.schoolid},#{r.psid})
        </foreach>
    </insert>
    <!--获取学生未提交试题集合-->
    <select id="listPSQByNotDo" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion">
        SELECT psq.questionid,psq.qscore,q.qtypeid
        FROM t_paper_student_question psq left join t_question q on psq.questionid = q.questionid
        WHERE psq.psid=#{psid} AND psq.checked=0 AND psq.answered=0
    </select>
    <!--自动批改学生未做试题-->
    <update id="updatePSQByNotDo">
        UPDATE t_paper_student_question SET costtime=0,checked=1,stuscore=0,checkid=0,checktime=#{checktime}
        WHERE psid=#{psid} AND answered=0
    </update>
    <!--获取该作业该学生未批改作业数量-->
    <select id="getNotCorrect" resultType="java.lang.Integer">
        SELECT count(*) FROM t_paper_student_question
        WHERE psid=#{psid} AND checked!=1
    </select>

    <!--计算所有试题用时-->
    <select id="getSumQuesitonsCosttime" resultType="java.lang.Integer">
         SELECT SUM(costtime) FROM t_paper_student_question WHERE psid=#{psid}
    </select>

    <!--修改学生答案图片，添加教师批阅-->
    <update id="updateUseranswer">
        UPDATE t_paper_student_question SET useranswer=#{useranswer} WHERE psqid=#{psqid}
    </update>

    <!--题库作业 按题批阅列表-->
    <resultMap id="questionResult" type="java.util.Map">
        <result property="qscore" column="qscore"></result>
        <result property="qorder" column="qorder"></result>
        <result property="qn" column="qn"></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>
        <collection property="students" ofType="java.util.Map" javaType="java.util.List">
            <result property="stuscore" column="stuscore"></result>
            <result property="answered" column="answered"></result>
            <result property="checked" column="checked"></result>
            <result property="checkcomm" column="checkcomm"></result>
            <result property="psid" column="psid"></result>
            <result property="psqid" column="psqid"></result>
            <result property="answertype" column="answertype"></result>
            <result property="useranswer" column="useranswer"></result>
            <result property="converted" column="converted"></result>
            <result property="useranswertext" column="useranswertext"></result>
            <result property="useranswertxt" column="useranswertxt"></result>
            <result property="studentname" column="username"></result>
            <result property="good" column="good"></result>
        </collection>
    </resultMap>
    <select id="qcorrectList" resultMap="questionResult">
        select psq.qscore,psq.qorder,psq.qn,q.qstem,q.qoption,q.qanswer,q.qanalyze
        ,psq.stuscore,psq.answered,psq.checked,psq.checkcomm,psq.psid,psq.psqid,psq.good
        ,psq.useranswer,psq.converted,psq.answertype,psq.useranswertext,psq.useranswertxt,u.username
        from t_paper_student_question psq left join t_question q on psq.questionid = q.questionid
        left join t_user u on psq.studentid = u.userid
        where psq.psid in
        <foreach collection="psids" item="psid" index="index" open="(" separator="," close=")">${psid}</foreach>
        and q.ctype in(3,13,14,15)
        order by psq.checked,psq.psqid,convert(u.username using gbk),u.userid
    </select>
    <!--获取学生作业未批改试题数量-->
    <select id="getNoCheckNumByPsId" resultType="integer">
        select count(*) from t_paper_student_question where psid=#{psid} and answered=1 and checked!=1
    </select>
    <!--获取未合并的学生答案-->
    <select id="listNoMergeUserAnswer" resultType="com.xhkjedu.sstudy.model.paperstudent.TPaperStudentQuestion">
        select psqid,useranswer from t_paper_student_question where converted=0
        <if test="psid!=0">and psid=#{psid}</if>
    </select>

    <!--获取作业所有学生试题作答情况-->
    <select id="listAllStuAnswers" resultType="com.xhkjedu.sstudy.vo.paperreport.PaperRStuQuestionVo">
        select sq.psqid,sq.paperid,sq.classid,sq.studentid,sq.psid,sq.questionpid,sq.questionid,sq.qn,sq.qorder,sq.answered,sq.answertype,
        sq.useranswer,sq.useranswertext,sq.useranswertxt,sq.costtime,sq.stuscore,sq.qscore,sq.schoolid,sq.checked,qq.ptqid,qq.ctype
        from t_paper_student_question sq
        left join t_paper_qtype_question qq on sq.paperid=qq.paperid and sq.questionid=qq.questionid
        where sq.paperid=#{paperid} order by sq.studentid,sq.qorder,qq.ptqid
    </select>
    <!--获取作业中所有试题-->
    <select id="listAllQuesByPaperid" resultType="com.xhkjedu.sstudy.vo.paperreport.PaperRQuestionVo">
        select tq.ptqid,tq.ctype,tq.questionid,tq.questionpid,tq.qn,tq.ptqorder qorder,
               tq.ptqscore qscore,q.qtypename,q.ctype pctype
        from t_paper_qtype_question tq left join t_question q on tq.questionpid=q.questionid
        where tq.paperid=#{paperid} ORDER BY tq.ptqorder,tq.ptqid
    </select>

    <!--清空指定作业所有优秀作答-->
    <update id="updatePaperStudentGoodByPsid">
        update t_paper_student_question set good=0 where psid=#{psid} and good=1
    </update>

    <!--标记单题是否优秀作答-->
    <update id="updatePaperStudentGoodByPsqid">
        update t_paper_student_question set good=#{good} where psid=#{psid} and questionpid=#{questionpid}
    </update>
    <!--学生作业题型列表-->
    <resultMap id="typeQuestion" type="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperTypeVo">
        <result property="qtypename" column="qtypename"></result>
        <result property="ptorder" column="ptorder"></result>
        <result property="ptscore" column="ptscore"></result>
        <result property="ptnum" column="ptnum"></result>
        <collection property="questions" ofType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
            <result property="qtypename" column="qtypename"></result>
            <result property="psqid" column="psqid"></result>
            <result property="paperid" column="paperid"></result>
            <result property="studentid" column="studentid"></result>
            <result property="answered" column="answered"></result>
            <result property="answertype" column="answertype"></result>
            <result property="useranswer" column="useranswer"></result>
            <result property="converted" column="converted"></result>
            <result property="useranswertxt" column="useranswertxt"></result>
            <result property="answertype" column="answertype"></result>
            <result property="qscore" column="qscore"></result>
            <result property="qorder" column="qorder"></result>
            <result property="psid" column="psid"></result>
            <result property="questionid" column="questionid"></result>
            <result property="qstem" column="qstem"></result>
            <result property="qoption" column="qoption"></result>
            <result property="ctype" column="ctype"></result>
            <result property="complexity" column="complexity"></result>
            <result property="hashear" column="hashear"></result>
            <result property="hearfile" column="hearfile"></result>
            <result property="qlevel" column="qlevel"></result>
            <result property="sorder" column="sorder"></result>
            <result property="questionpid" column="questionpid"></result>
            <result property="qn" column="qn"></result>
        </collection>
    </resultMap>
    <select id="listPaperType" resultMap="typeQuestion">
        select t.qtypename,t.ptorder,t.ptscore,t.ptnum
        ,psq.psqid,psq.paperid,psq.studentid,psq.answered,psq.answertype,psq.answertime
        ,psq.useranswer,psq.converted,psq.useranswertxt,psq.checked,psq.qscore
        ,psq.qorder,psq.qn,psq.psid
        ,q.questionid,q.qstem,q.qoption,q.ctype,q.complexity,q.hashear,q.hearfile,q.qlevel,q.sorder
        ,ifnull(q.questionpid,q.questionid)questionpid
        from t_paper_student_question psq left join t_question q on psq.questionid=q.questionid
        left join t_paper_qtype_question tq on psq.questionid=tq.questionid and psq.paperid=tq.paperid
        left join t_paper_qtype t on tq.ptid=t.ptid
        where psq.psid=#{psid} order by t.ptorder,psq.qorder
    </select>
</mapper>
