<?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.paperreport.PaperReportMapper" >
    <!--删除作业报告-->
    <delete id="deletePaperReport">
        delete from t_paper_rclass where paperid=#{paperid};
        delete from t_paper_rclass_point where paperid=#{paperid};
        delete from t_paper_rclass_question where paperid=#{paperid};
        delete from t_paper_rclass_rank where paperid=#{paperid};
        delete from t_paper_rstudent where paperid=#{paperid};
        delete from t_paper_rstudent_question where paperid=#{paperid};
        delete from t_paper_rstudent_point where paperid=#{paperid};
        delete from t_question_class where paperid=#{paperid};
        delete from t_question_student where paperid=#{paperid} and errornum=1;
        update t_paper set hasreport=0 where paperid=#{paperid};
    </delete>
    <!--更改作业是否生成报告-->
    <update id="updateHasreport">
        update t_paper set hasreport=#{hasreport} where paperid=#{paperid}
    </update>
    <!--作业概况-班级概况-->
    <select id="getPaperClass" resultType="java.util.Map">
        select * from t_paper_rclass where paperid=#{paperid} and classid=#{classid}
    </select>
    <!--作业概况-成绩概况-->
    <select id="listPaperClassRank" resultType="java.util.Map">
        select * from t_paper_rclass_rank where paperid=#{paperid} and classid=#{classid}
    </select>
    <!--作业概况-试题错误率-->
    <select id="listPaperClassQuestionRate" resultType="java.util.Map">
        select q.classid,c.gradeid,c.classname,q.qorder,q.classlostrate,q.schoollostrate
        from t_paper_rclass_question q left join t_class c on q.classid = c.classid
        where paperid=#{paperid} and q.classid=#{classid}
        order by qorder
    </select>
    <!--作业概况-知识点错误率-->
    <select id="listPaperClassPointRate" resultType="java.util.Map">
        select pointname,classlostrate
        from t_paper_rclass_point where paperid=#{paperid} and classid=#{classid}
    </select>
    <!--作业分析-->
    <select id="getPaperAnalyze" resultType="java.util.Map">
        select p.paperid,p.papertype,p.papername,pa.pointjson
        from t_paper p left join t_paper_analyze pa on p.paperid = pa.paperid
        where p.paperid=#{paperid}
    </select>
    <resultMap id="typeResult" type="java.util.Map">
        <result property="ptid" column="ptid"></result>
        <result property="ptorder" column="ptorder"></result>
        <result property="qtypename" column="qtypename"></result>
        <collection property="questions" ofType="java.util.Map" javaType="java.util.List">
            <result property="ptqid" column="ptqid"></result>
            <result property="qn" column="qn"></result>
            <result property="qorder" column="qorder"></result>
            <result property="qscore" column="qscore"></result>
            <result property="answerjson" column="answerjson"></result>
            <result property="ctype" column="ctype"></result>
            <result property="questionid" column="questionid"></result>
            <result property="classavgscore" column="classavgscore"></result>
            <result property="classscorerate" column="classscorerate"></result>
            <result property="schoolavgscore" column="schoolavgscore"></result>
            <result property="schoolscorerate" column="schoolscorerate"></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="hashear" column="hashear"></result>
            <result property="hearfile" column="hearfile"></result>
            <result property="qlevel" column="qlevel"></result>
            <result property="optionnum" column="optionnum"></result>
            <result property="ptqid" column="ptqid"></result>
            <result property="stunum" column="stunum"></result>
            <result property="qtypename" column="qqtypename"></result>
        </collection>
    </resultMap>
    <!--题库作业试题-->
    <select id="listTkSt" resultMap="typeResult">
        <!--select q.qn,q.qorder,q.qscore,q.qtypename,q.answerjson,q.ctype,q.questionid
        ,q.classavgscore,q.classscorerate,q.schoolavgscore,q.schoolscorerate
        ,q.ptid,q.ptorder
        ,tq.qstem,tq.qoption,tq.qanswer,tq.qanalyze,tq.hashear,tq.hearfile,tq.qlevel
        <if test="papertype==3">
            ,(select count(*) from t_paper_student ps where ps.paperid=q.paperid and ps.classid=q.classid
            and locate(concat('"',q.questionid,'"'),ps.questionids)>0)stunum
        </if>
        from t_paper_rclass_question q left join t_question tq on q.questionid = tq.questionid
        where q.paperid=#{paperid} and q.classid=#{classid}
        order by q.qorder-->
        select pq.qtypename,pq.ptid,pq.ptorder,pqq.qn,pqq.ptqorder qorder,pqq.ptqscore qscore,tq.ctype,
        pqq.questionpid questionid,tq.qtypename qqtypename,q.answerjson
        ,q.classavgscore,q.classscorerate,q.schoolavgscore,q.schoolscorerate
        ,tq.qstem,tq.qoption,tq.qanswer,tq.qanalyze,tq.hashear,tq.hearfile,tq.qlevel
        <if test="papertype==3">
            ,(select count(*) from t_paper_student ps where ps.paperid=#{paperid} and ps.classid=#{classid}
            and locate(concat('"',pqq.questionpid,'"'),ps.questionids)>0)stunum
        </if>
        from t_paper_qtype_question pqq left join t_paper_qtype pq on pqq.ptid=pq.ptid
        left join t_question tq on pqq.questionpid=tq.questionid
        left join t_paper_rclass_question q
        on pqq.paperid=q.paperid and pqq.questionpid=q.questionid and q.classid=#{classid}
        where pqq.paperid=#{paperid} group by pqq.questionpid  order by pqq.ptqorder
    </select>
    <!--附件作业试题-->
    <select id="listFjSt" resultMap="typeResult">
        select pq.qtypename,pq.psqtypeid ptid,pq.pstorder ptorder,ps.qn,ps.psorder qorder,ps.psscore qscore,ps.ctype,
         ps.psanswer qanswer,ps.optionnum,ps.pscaid ptqid,ps.qtypename qqtypename,
        q.classavgscore,q.classscorerate,q.schoolavgscore,q.schoolscorerate,q.answerjson
        from t_paper_scantron ps
        left join t_paper_scantron_qtype pq on ps.psqtypeid=pq.psqtypeid
        left join t_paper_rclass_question q
        on ps.paperid=q.paperid and q.ptqid = ps.pscaid and q.classid=#{classid}
        where ps.paperid=#{paperid}
        order by ps.psorder
    </select>
    <!--作业题型-->
    <select id="listPaperType" resultType="com.xhkjedu.sstudy.vo.paperreport.PaperRStuQuestionVo">
        <if test="papertype==1 or papertype==3">
            select ptq.questionpid questionid,q.ctype,pt.ptid,pt.qtypename,pt.ptorder
            from t_paper_qtype_question ptq left join t_paper_qtype pt on ptq.ptid = pt.ptid
            left join t_question q on ptq.questionpid = q.questionid
            where ptq.paperid=#{paperid} group by ptq.questionpid
        </if>
        <if test="papertype==2">
            select ptq.pscaid questionid,ptq.ctype,pt.psqtypeid ptid,pt.qtypename,pt.pstorder ptorder
            from t_paper_scantron ptq left join t_paper_scantron_qtype pt on ptq.psqtypeid = pt.psqtypeid
            where ptq.paperid=#{paperid} group by ptq.pscaid
        </if>
    </select>
    <!--题库作业子题-->
    <select id="listZtQuestion" resultType="java.util.Map">
        select tq.questionid,tq.questionpid,tq.sorder,tq.qstem,tq.qoption,tq.qanswer,tq.qanalyze,tq.ctype,q.ptqscore as qscore,
        q.qn,tq.qtypename
        from t_question tq left join t_paper_qtype_question q on tq.questionid=q.questionid
        where q.paperid=#{paperid} and tq.questionpid in
        <foreach collection="questionids" item="questionid" open="(" separator="," close=")">#{questionid}</foreach>
        order by tq.sorder
    </select>
    <!--未提交学生列表-->
    <select id="listWtjStu" resultType="java.util.Map">
        select ps.studentid,u.username stuname,u.headpic
        from t_paper_student ps left join t_user u on ps.studentid = u.userid
        where ps.paperid=#{paperid} and ps.classid=#{classid} and ps.psstate in(0,3)
        order by convert(u.username using gbk),u.userid
    </select>
    <!--已提交学生列表-->
    <select id="listTjStu" resultType="java.util.Map">
        select ps.psid,u.username studentname,u.headpic,ps.stoptime answertime,ps.costtime
        ,ps.psstate,ps.pschecked,ps.papernum,ps.paperscore
        ,rs.stuscore,rs.stuscorerate,rs.stuzgtscore,rs.stukgtscore
        ,(select u2.username from t_user u2 where u2.userid=ps.checkid)checkname
        from t_paper_student ps left join t_paper_rstudent rs on ps.paperid=rs.paperid
        and ps.studentid=rs.studentid and ps.classid=rs.classid
        left join t_user u on ps.studentid = u.userid
        where ps.paperid=#{paper.paperid} and ps.classid=#{paper.classid} and ps.psstate in(1,2)
        <choose>
            <when test="paper.fieldname!=null and paper.fieldname!=''">order by ${paper.fieldname} ${paper.fieldorder}</when>
            <otherwise>order by rs.stuscorerate desc</otherwise>
        </choose>
        ,convert(u.username using gbk)
    </select>
    <!--作业基本信息-->
    <select id="getPaper" resultType="java.util.Map">
        select p.papername,p.papertype,p.paperclass,p.createtime,p.hasfile,p.committype,p.timetype,p.hearfile,p.createid
        ,pg.starttime,pg.stoptime,pg.showtype
        from t_paper p left join t_paper_class pg on p.paperid = pg.paperid
        where p.paperid=#{paperid} limit 1
    </select>

    <!--优秀作答班级-->
    <resultMap id="classResult" 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>
        </collection>
    </resultMap>
    <select id="listGoodAnswerClass" resultMap="classResult">
        select q.classid,c.classname,c.gradeid,c.schoolid,s.schoolname
        <if test="papertype!=2">from t_paper_student_question q</if>
        <if test="papertype==2">from t_paper_student_scantron q</if>
        left join t_class c on q.classid=c.classid
        left join t_school s on c.schoolid = s.schoolid
        where q.paperid=#{paperid} and q.good=1
        group by q.classid
        order by if(s.schoolid=#{schoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder
    </select>
</mapper>