<?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.question.QuestionStudentMapper">
    <!--获取学生错题信息-->
    <select id="getQuestionStudent" resultType="com.xhkjedu.sstudy.model.question.TQuestionStudent">
        SELECT * FROM t_question_student
        WHERE questionid = #{qs.questionid} AND studentid = #{qs.studentid} and qctype=#{qs.qctype}
        ORDER BY qsid DESC LIMIT 1
    </select>
    <!--列表-->
    <select id="findAll" resultType="com.xhkjedu.sstudy.vo.question.QuestionStudentVo">
        select qs.qsid,qs.questionid,qs.qscore,qs.stuscore,qs.scorerate,qs.qctype,qs.qorder,qs.qn,qs.qtypeid,qs.paperid
        from t_question_student qs left join t_paper p on qs.paperid=p.paperid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and qs.studentid = #{qs.studentid} and delstate=0 and qs.subjectid=#{qs.subjectid}
        <if test="qs.starttime!=null and qs.starttime!=0">
            and qs.handletime>=#{qs.starttime}
        </if>
        <if test="qs.stoptime!=null and qs.stoptime!=0">
            and qs.handletime&lt;=#{qs.stoptime}
        </if>
        <if test="qs.lsbid!=null and qs.lsbid!='0'.toString() and qs.lsbid!=''">
            and p.lsbid=#{qs.lsbid}
        </if>
        <if test="qs.directorid!=null and qs.directorid!='0'.toString() and qs.directorid!=''">
            and p.directorid=#{qs.directorid}
        </if>
        <if test="qs.qtypeid!=null and qs.qtypeid!='0'.toString() and qs.qtypeid!=''">
            <if test="qs.qtypeid!='tlt'">and qs.hashear=0 and qs.qtypeid=#{qs.qtypeid}</if>
            <if test="qs.qtypeid=='tlt'">and qs.hashear=1</if>
        </if>
        order by qs.handletime desc,qs.qorder desc limit #{qs.page},#{qs.pageSize}
    </select>
    <!--删除-->
    <update id="deleteById">
        update t_question_student set errornum=0,delstate=1 where qsid=#{qsid}
    </update>
    <!--更新错误次数-->
    <update id="updateErrorNum">
        update t_question_student set errornum=errornum+1,delstate=0,handletime=#{checktime} where qsid=#{qsid}
    </update>

    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <select id="listForWeb" resultType="com.xhkjedu.sstudy.vo.question.QuestionStudentVo">
        select qs.qsid,qs.questionid,qs.qscore,qs.stuscore,qs.scorerate,qs.qctype,qs.qorder,qs.qn
        ,qs.qtypeid,concat(p.papername,'-第',qs.qn,'题')qstem
        from t_question_student qs left join t_paper p on qs.paperid=p.paperid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and qs.studentid=#{question.studentid} and qs.delstate=0
        <if test="question.starttime!=null and question.starttime!=0">
            and qs.handletime>=#{question.starttime}
        </if>
        <if test="question.stoptime!=null and question.stoptime!=0">
            and qs.handletime&lt;=#{question.stoptime}
        </if>
        <if test="question.subjectid!=null and question.subjectid!='0'.toString() and question.subjectid!=''">
            and qs.subjectid=#{question.subjectid}
        </if>
        <if test="question.lsbid!=null and question.lsbid!='0'.toString() and question.lsbid!=''">
            and p.lsbid=#{question.lsbid}
        </if>
        <if test="question.directorid!=null and question.directorid!='0'.toString() and question.directorid!=''">
            and p.directorid=#{question.directorid}
        </if>
        <if test="question.qtypeid!=null and question.qtypeid!='0'.toString() and question.qtypeid!=''">
            <if test="question.qtypeid!='tlt'">and qs.hashear=0 and qs.qtypeid=#{question.qtypeid}</if>
            <if test="question.qtypeid=='tlt'">and qs.hashear=1</if>
        </if>
        group by qs.questionid,qs.qctype order by qs.handletime desc,qs.qorder desc
    </select>
    <!--更新错题信息-->
    <update id="updateQuestion">
        update t_question_student set qtypeid=#{qs.qtypeid},qorder=#{qs.qorder},qn=#{qs.qn}
        ,qscore=#{qs.qscore},stuscore=#{qs.stuscore},scorerate=#{qs.scorerate},paperid=#{qs.paperid}
        ,errornum=errornum+1,delstate=0,handletime=#{qs.handletime},stujson=#{qs.stujson}
        where qsid=#{qs.qsid}
    </update>
    <!--结构试题详情-->
    <resultMap id="questionResult" type="com.xhkjedu.sstudy.vo.question.QuestionStudentVo">
        <result property="qsid" column="qsid"/>
        <result property="qstem" column="qstem"/>
        <result property="qoption" column="qoption"/>
        <result property="qanswer" column="qanswer"/>
        <result property="qanalyze" column="qanalyze"/>
        <result property="ctype" column="ctype"/>
        <result property="complexity" column="complexity"/>
        <result property="qscore" column="qscore"/>
        <result property="stuscore" column="stuscore"/>
        <result property="stujson" column="stujson"/>
        <collection property="answers" ofType="com.xhkjedu.sstudy.vo.question.StudentAnswerVo" javaType="java.util.List"
                    select="listAnswer" column="{questionid=questionid,paperid=paperid,studentid=studentid}">
        </collection>
    </resultMap>
    <select id="listAnswer" resultType="com.xhkjedu.sstudy.vo.question.StudentAnswerVo">
        select useranswer,answertype,useranswertxt,stuscore,answertime,q.ctype,q.sorder
        from t_paper_student_question psq left join t_question q on psq.questionid=q.questionid
        where paperid!=#{paperid} and studentid=#{studentid} and answered=1 and checked=1 and psq.qscore!=psq.stuscore
        and psq.questionpid=#{questionid}
        order by answertime desc
    </select>
    <select id="detail" resultMap="questionResult">
        select q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype,q.complexity,q.questionid,q.hashear,q.hearfile
        ,qs.qsid,qs.qscore,qs.stuscore,qs.stujson,qs.paperid,qs.studentid,qs.qorder,qs.qn
        ,(select group_concat(p.pointname separator '  ') from t_question_point qp
        left join t_point p on qp.pointid = p.pointid where qp.questionid=qs.questionid)pointname
        from t_question_student qs left join t_question q on qs.questionid=q.questionid
        where qs.questionid=#{questionid} and qs.studentid=#{studentid} and qs.qctype=#{qctype} limit 1
    </select>
    <!--附件试题详情-->
    <select id="sdetail" resultType="com.xhkjedu.sstudy.vo.question.QuestionStudentVo">
        select concat(p.papername,'-第',qs.qn,'题')qstem,p.hearfile
        ,ps.psanswer qanswer,ps.ctype,qs.qsid,qs.qscore,qs.stuscore,qs.stujson,qs.paperid
        from t_question_student qs left join t_paper_scantron ps on qs.questionid=ps.pscaid
        left join t_paper p on p.paperid=qs.paperid
        where qs.questionid=#{questionid} and qs.studentid=#{studentid} and qs.qctype=2 limit 1
    </select>

    <!--已批阅未得满分的试题-->
    <select id="listQuestions" resultType="com.xhkjedu.sstudy.model.question.TQuestionStudent">
        <if test="qctype==1 or qctype==3">
            select q.studentid,ptq.mscore as qscore,sum(q.stuscore) as stuscore,ptq.questionpid as questionid
            ,ptq.mqtypeid as qtypeid,ptq.ptqorder as qorder,q.qn,max(q.checked) as checked
            ,concat('[',group_concat(json_object('questionid',q.questionid,'qn',q.qn,'qorder',q.qorder,'answertype',q.answertype
            ,'useranswer',q.useranswer,'useranswertxt',q.useranswertxt,'converted',q.converted
            ,'qscore',q.qscore,'stuscore',q.stuscore,'ctype',ptq.ctype) order by ptq.ptqid),']')stujson
            ,(select tq.hashear from t_question tq where tq.questionid=ptq.questionpid)hashear
            from t_paper_student_question q left join t_paper_qtype_question ptq on q.questionid=ptq.questionid and q.paperid=ptq.paperid
            where q.paperid=#{paperid} and q.classid=#{classid}
            group by ptq.questionpid,q.studentid,q.classid HAVING checked=1 and qscore!=stuscore
        </if>
        <if test="qctype==2">
            select q.studentid,q.qscore,q.stuscore,q.pscaid questionid,ps.qtypeid,ps.psorder as qorder,q.qn
            ,concat('[',group_concat(json_object('questionid',q.pscaid,'qn',q.qn,'qorder',q.qorder,'answertype',q.answertype
            ,'useranswer',q.useranswer,'useranswertxt',q.useranswertxt,'converted',q.converted
            ,'qscore',q.qscore,'stuscore',q.stuscore,'ctype',ps.ctype) order by ps.pscaid),']')stujson
            from t_paper_student_scantron q left join t_paper_scantron ps on q.pscaid=ps.pscaid
            where q.paperid=#{paperid} and q.classid=#{classid} and q.checked=1 and q.stuscore!=q.qscore
            group by q.pscaid,q.studentid,q.classid
        </if>
    </select>
    <!--题型列表-->
    <select id="listQtype" resultType="com.xhkjedu.sstudy.vo.subjectbook.SubjectQuestiontypeVo">
        select st.qtypeid,st.qtypename,st.qtypeorder,st.ctype
        from t_question_student qs left join t_subject_questiontype st on qs.qtypeid=st.qtypeid
        where qs.studentid=#{qs.studentid} and st.subjectid=#{qs.subjectid}
        group by st.qtypeid order by st.qtypeorder
    </select>
    <!--根据作业id获取作业名称-->
    <select id="getPaperNameById" resultType="java.util.Map">
        select paperid,papername from t_paper where
        <foreach collection="paperids" item="paperid" index="index" separator="or">
            paperid=#{paperid}
        </foreach>
    </select>
    <!--保存学生错题-->
    <insert id="saveQuestionStudent">
        insert into t_question_student(qctype,questionid,studentid,hashear,subjectid
        ,qtypeid,qorder,qn,qscore,stuscore,scorerate,paperid,errornum,delstate,handletime,stujson)
        values(#{qs.qctype},#{qs.questionid},#{qs.studentid},#{qs.hashear},#{qs.subjectid}
        ,#{qs.qtypeid},#{qs.qorder},#{qs.qn},#{qs.qscore},#{qs.stuscore},#{qs.scorerate},#{qs.paperid}
        ,1,0,#{qs.handletime},#{qs.stujson})
        on duplicate key update qtypeid=#{qs.qtypeid},qorder=#{qs.qorder},qn=#{qs.qn}
        ,qscore=#{qs.qscore},stuscore=#{qs.stuscore},scorerate=#{qs.scorerate},paperid=#{qs.paperid}
        ,errornum=errornum+1,delstate=0,handletime=#{qs.handletime},stujson=#{qs.stujson}
    </insert>
</mapper>
