<?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.PaperAnalyzeMapper">

    <!--作业分析-->
    <select id="getAnalyzeByPaperid" resultType="com.xhkjedu.sstudy.model.paper.TPaperAnalyze">
        select * from t_paper_analyze where paperid=#{paperid} limit 1
    </select>

    <!-- 根据作业id删除分析 -->
    <delete id="deleteByPaperid">
        DELETE FROM t_paper_analyze WHERE paperid=#{paperid}
    </delete>

    <!--获取作业分析-->
    <resultMap id="paperAnalyzeResult" type="com.xhkjedu.sstudy.vo.paper.PaperAnalyzeVo">
        <result property="papername" column="papername"/>
        <result property="papernum" column="pnum"/>
        <result property="paperscore" column="pscore"/>
        <result property="papertype" column="papertype"/>
        <result property="stuscore" column="stuscore"/>
        <result property="fbnum" column="fbnum"/>
        <result property="avgscore" column="avgscore"/>
        <result property="maxscore" column="maxscore"/>
        <result property="minscore" column="minscore"/>
        <result property="stoptime" column="stoptime"/>
        <result property="checkrate" column="checkrate"/>
        <result property="checktype" column="checktype"/>
        <result property="createtime" column="createtime"/>
        <result property="starttime" column="starttime"/>
        <result property="stoptime" column="stoptime"/>
        <result property="showtype" column="showtype"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paper.AnalyzeStudentVo">
            <result property="pschecked" column="pschecked"/>
            <result property="papernum" column="papernum"/>
            <result property="paperscore" column="paperscore"/>
            <result property="userscore" column="userscore"/>
            <result property="psid" column="psid"/>
        </collection>
    </resultMap>
    <select id="getPaperAnalyze" resultMap="paperAnalyzeResult">
        select p.paperid,p.papername,p.papertype,p.checktype
        ,p.papernum pnum,p.paperscore pscore,ps.papernum,ps.paperscore,ps.userscore,ps.psid
        ,p.createtime,pg.starttime,pg.stoptime,pg.showtype
        ,(case when ps.psstate in(0,3) then 0 when ps.pschecked=1 then 2 else 1 end) as pschecked
        from t_paper p left join t_paper_student ps on p.paperid = ps.paperid
        left join t_paper_class pg on pg.paperid=p.paperid and pg.classid=#{classid}
        where p.paperid=#{paperid} and ps.classid=#{classid}
    </select>
    <select id="getPeopleAnalyze" resultType="com.xhkjedu.sstudy.vo.paper.AnalyzeStudentVo">
        select ps.psid,u.username studentname,ps.userscore,ps.answertime,ps.costtime
        ,ps.psstate,ps.pschecked,ps.zgscore,ps.kgscore,ps.papernum,ps.paperscore
        ,(select u2.username from t_user u2 where u2.userid=ps.checkid)checkname
        from(select ps.psid,ps.studentid,ps.checkid,ps.userscore,ps.stoptime answertime,ps.costtime,ps.psstate,ps.pschecked
        ,ps.papernum,ps.paperscore
        ,sum(if(q.ctype in(3,13,14,15),psq.stuscore,0)) as zgscore
        ,sum(if(q.ctype not in(3,13,14,15),psq.stuscore,0)) as kgscore
        from t_paper_student ps left join
        <if test="paper.papertype==1 or paper.papertype==3">
            t_paper_student_question psq on ps.psid=psq.psid left join t_question q on psq.questionid=q.questionid
        </if>
        <if test="paper.papertype==2">
            t_paper_student_scantron psq on ps.psid=psq.psid  left join t_paper_scantron q on psq.pscaid=q.pscaid
        </if>
        where ps.paperid=#{paper.paperid}
        <if test="paper.classid!=null and paper.classid!=0">
            and ps.classid=#{paper.classid}
        </if>
        <if test="paper.paperstate==1">
            and ps.psstate in(0,3)
        </if>
        <if test="paper.paperstate==2">
            and ps.psstate in(1,2) and ps.pschecked=0
        </if>
        <if test="paper.paperstate==3">
            and ps.pschecked=1
        </if>
        group by ps.psid)ps left join t_user u on ps.studentid = u.userid
        <choose>
            <when test="paper.fieldname!=null and paper.fieldname!=''">order by ${paper.fieldname} ${paper.fieldorder}</when>
            <otherwise>order by ps.pschecked desc,ps.psstate desc,ps.userscore desc</otherwise>
        </choose>
        ,convert(u.username using gbk)
    </select>
    <!--作业统计按题-->
    <select id="getQuestionAnalyze" resultType="com.xhkjedu.sstudy.vo.paper.AnalyzeQuestionVo">
        select ptq.paperid,ptq.questionid,ptq.ptqorder,ptq.qn,ptq.questionpid
        ,group_concat(distinct concat(ptq.qn,'_',ptq.questionid))qns
        ,ptq.mqtypename as qtypename,ptq.mscore as totalscore,truncate(avg(psq.stuscore),2)avgscore
        ,truncate(avg(psq.costtime),2)avgtime,min(psq.costtime)mintime,count(ps.psid)stunum
        ,truncate(sum(if(psq.answered=1,1,0))*100/count(ps.psid),2)commitrate
        ,truncate(SUM(IFNULL(psq.stuscore,0))*100/(ptq.ptqscore*count(ps.psid)),2)scorerate
        from t_paper_qtype_question ptq left join t_question q on ptq.questionid=q.questionid
        left join t_paper_student ps on ptq.paperid=ps.paperid and ps.classid=#{classid}
        left join t_paper_student_question psq on psq.psid=ps.psid and ptq.questionid=psq.questionid
        where ptq.paperid=#{paperid}
        <if test="papertype==3">
            and locate(concat('"',ptq.questionpid,'"'),ps.questionids)>0
        </if>
        group by ptq.ptqorder order by ptq.ptqorder
    </select>
    <!--答题卡作业统计按题-->
    <select id="getSQuestionAnalyze" resultType="com.xhkjedu.sstudy.vo.paper.AnalyzeQuestionVo">
        SELECT ps.paperid,ps.psorder ptqorder,ps.qn,ps.qtypename,ps.psscore totalscore,ps.psanswer qanswer,ps.ctype,truncate(avg(pss.stuscore),2)avgscore
        ,truncate(avg(pss.costtime),2)avgtime,min(pss.costtime)mintime
        ,truncate(sum(if(pss.answered=1,1,0))*100/count(ps1.psid),2)commitrate
        ,truncate(SUM(IFNULL(pss.stuscore,0))*100/(ps.psscore*count(ps1.psid)),2)scorerate
        FROM t_paper_scantron ps left join t_paper_student ps1 on ps1.paperid=ps.paperid and ps1.classid=#{classid}
        LEFT JOIN t_paper_student_scantron pss ON ps.pscaid=pss.pscaid
        AND pss.psid=ps1.psid
        WHERE ps.paperid=#{paperid}
        GROUP BY ps.pscaid ORDER BY ps.psorder
    </select>
    <!--获取作业人数-->
    <select id="getStuNum" resultType="java.lang.Integer">
        select count(*) from t_paper_student where paperid=#{paperid} and classid=#{classid}
    </select>

    <select id="getQuestionDetailAnalyze" resultType="com.xhkjedu.sstudy.vo.paper.QuestionAnalyzeVo">
        select q.qtypename,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype
        ,(select ptq.mscore from t_paper_qtype_question ptq
        where ptq.paperid=#{question.paperid} and ptq.questionpid=#{question.questionpid} limit 1) as score
        ,(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_question q where q.questionid=#{question.questionpid}
    </select>
    <!--作业统计-按题详情-学生列表-->
    <select id="listQuestionStu" resultType="com.xhkjedu.sstudy.vo.paper.AnalyzeStudentVo">
        select psq.psqid,psq.stuscore userscore,psq.answertime,psq.costtime,u.username studentname
        ,psq.useranswer,psq.converted,psq.useranswertext,psq.useranswertxt,psq.answertype
        from t_paper_student_question psq left join t_user u on psq.studentid = u.userid
        where psq.questionid=#{questionid} and psq.paperid=#{paperid} and psq.answered=1 and psq.classid=#{classid}

        <choose>
            <when test="od!=null and od==1">
                order by psq.stuscore,convert(u.username using gbk)
            </when>
            <otherwise>
                order by psq.stuscore desc,convert(u.username using gbk)
            </otherwise>
        </choose>
    </select>
    <!--答题卡作业统计按题详情-->
    <select id="getSQuestionDetailAnalyze" resultType="com.xhkjedu.sstudy.vo.paper.AnalyzeStudentVo">
        SELECT pss.pssid psqid,pss.stuscore userscore
        ,pss.answertime,pss.costtime,u.username studentname
        ,pss.useranswer,pss.converted,pss.useranswertext,pss.useranswertxt,pss.answertype
        FROM t_paper_student_scantron pss LEFT JOIN t_user u ON pss.studentid = u.userid
        WHERE pss.paperid=#{paperid} AND pss.qorder=#{ptqorder} AND pss.answered=1 and pss.classid=#{classid}
        <choose>
            <when test="od!=null and od==1">
                order by pss.stuscore,convert(u.username using gbk)
            </when>
            <otherwise>
                order by pss.stuscore desc,convert(u.username using gbk)
            </otherwise>
        </choose>
    </select>
    <!--作业统计按人详情-->
    <select id="getStudentPaperAnalyze" resultType="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperVo">
        select p.paperid,p.papername,p.papertype,p.hearfile,ps.papernum,ps.paperscore,ps.userscore,u.username studentname,ps.classid
        ,ps.questionids,(select username from t_user where userid=ps.checkid)checkname
        ,ps.stoptime,ps.costtime,p.committype
        from t_paper_student ps left join t_paper p on ps.paperid = p.paperid
        LEFT JOIN t_user u ON ps.studentid=u.userid
        where ps.psid=#{psid} limit 1
    </select>
    <!--作业统计按人详情试题列表-->
    <resultMap id="typeQuestion" type="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperTypeVo">
        <result property="qtypename" column="qtypename"/>
        <result property="ptorder" column="ptorder"/>
        <result property="ptscore" column="ptscore"/>
        <result property="ptnum" column="ptnum"/>
        <collection property="questions" ofType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
            <result property="qtypename" column="qtypename"/>
            <result property="psqid" column="psqid"/>
            <result property="paperid" column="paperid"/>
            <result property="studentid" column="studentid"/>
            <result property="answered" column="answered"/>
            <result property="answertime" column="answertime"/>
            <result property="costtime" column="costtime"/>
            <result property="answertype" column="answertype"/>
            <result property="useranswer" column="useranswer"/>
            <result property="converted" column="converted"/>
            <result property="useranswertext" column="useranswertext"/>
            <result property="useranswertxt" column="useranswertxt"/>
            <result property="answertype" column="answertype"/>
            <result property="checked" column="checked"/>
            <result property="qscore" column="qscore"/>
            <result property="stuscore" column="stuscore"/>
            <result property="qorder" column="qorder"/>
            <result property="psid" column="psid"/>
            <result property="questionid" column="questionid"/>
            <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="pointname" column="pointname"/>
            <result property="checkcomm" column="checkcomm"/>
            <result property="hashear" column="hashear"/>
            <result property="hearfile" column="hearfile"/>
            <result property="qlevel" column="qlevel"/>
            <result property="sorder" column="sorder"/>
            <result property="questionpid" column="questionpid"/>
            <result property="qn" column="qn"/>
            <result property="optionnum" column="optionnum"/>
            <result property="good" column="good"/>
        </collection>
    </resultMap>
    <!--作业统计 按人详情 题型列表-->
    <select id="listPeopleDetailAnalyze" resultMap="typeQuestion">
        SELECT t.qtypename,t.ptorder,t.ptscore,t.ptnum
        ,psq.psqid,psq.paperid,psq.studentid,psq.answered,psq.answertime,psq.costtime,psq.answertype,psq.checkcomm
        ,psq.useranswer,psq.converted,psq.useranswertext,psq.useranswertxt,psq.checked,psq.qscore,psq.stuscore
        ,psq.qorder,psq.qn,psq.psid,psq.good
        ,q.questionid,q.qstem,q.qoption,q.qanswer,q.qanalyze,q.ctype,q.complexity,q.hashear,q.hearfile,q.qlevel,q.sorder
        ,ifnull(q.questionpid,q.questionid) as questionpid
        ,(if(psq.questionid!=psq.questionpid,'',(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
        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>
    <resultMap id="typeSQuestion" type="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperTypeVo">
        <result property="qtypename" column="qtypename"/>
        <result property="ptorder" column="ptorder"/>
        <result property="ptscore" column="ptscore"/>
        <result property="ptnum" column="ptnum"/>
        <collection property="scantrons" ofType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentScantronVo">
            <result property="pssid" column="pssid"/>
            <result property="paperid" column="paperid"/>
            <result property="studentid" column="studentid"/>
            <result property="psid" column="psid"/>
            <result property="pscaid" column="pscaid"/>
            <result property="qn" column="qn"/>
            <result property="qorder" column="qorder"/>
            <result property="answered" column="answered"/>
            <result property="answertype" column="answertype"/>
            <result property="useranswer" column="useranswer"/>
            <result property="converted" column="converted"/>
            <result property="useranswertext" column="useranswertext"/>
            <result property="useranswertxt" column="useranswertxt"/>
            <result property="answertype" column="answertype"/>
            <result property="answertime" column="answertime"/>
            <result property="costtime" column="costtime"/>
            <result property="checked" column="checked"/>
            <result property="stuscore" column="stuscore"/>
            <result property="qscore" column="qscore"/>
            <result property="qtypeid" column="qtypeid"/>
            <result property="qtypename" column="qtypename"/>
            <result property="ctype" column="ctype"/>
            <result property="psanswer" column="psanswer"/>
            <result property="optionnum" column="optionnum"/>
            <result property="checkcomm" column="checkcomm"/>
        </collection>
    </resultMap>
    <!--答题卡作业统计 按人详情 题型列表-->
    <select id="listPeopleSDetailAnalyze" resultMap="typeSQuestion">
        SELECT psq.qtypename,psq.pstorder ptorder,psq.pstscore ptscore,psq.pstnum ptnum,
        pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.answertype,pss.useranswer,pss.converted,pss.useranswertext,pss.useranswertxt,
        pss.answertime,pss.costtime,pss.checked,pss.stuscore,pss.qscore,pss.pscaid,pss.checkcomm,
        psc.qtypeid,psc.ctype,psc.psanswer,psc.optionnum,pss.good
        FROM t_paper_student_scantron pss LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        LEFT JOIN t_paper_scantron_qtype psq ON psc.paperid = psq.paperid AND psc.psqtypeid=psq.psqtypeid
        WHERE pss.psid=#{psid} ORDER BY psq.pstorder,pss.qorder
    </select>
    <!--答题卡作业统计 按人详情 题型列表-->
    <select id="listStuDetailAnalyze" resultMap="typeQuestion">
        SELECT psq.qtypename,psq.pstorder ptorder,psq.pstscore ptscore,psq.pstnum ptnum,
        pss.pssid,pss.paperid,pss.studentid,pss.psid,pss.qn,pss.qorder,pss.answered,pss.answertype,pss.useranswer,pss.converted,pss.useranswertext,pss.useranswertxt,
        pss.answertime,pss.costtime,pss.checked,pss.stuscore,pss.qscore,pss.pscaid questionid,pss.checkcomm,
        psc.qtypeid,psc.ctype,psc.psanswer qanswer,psc.optionnum,pss.good
        FROM t_paper_student_scantron pss LEFT JOIN t_paper_scantron psc ON pss.pscaid = psc.pscaid
        LEFT JOIN t_paper_scantron_qtype psq ON psc.paperid = psq.paperid AND psc.psqtypeid=psq.psqtypeid
        WHERE pss.psid=#{psid} ORDER BY psq.pstorder,pss.qorder
    </select>
    <resultMap id="commitAnalyze" type="com.xhkjedu.sstudy.vo.paper.PaperCAnalyzeVo">
        <result property="paperid" column="paperid"/>
        <result property="papername" column="papername"/>
        <result property="starttime" column="starttime"/>
        <collection property="classes" ofType="com.xhkjedu.sstudy.vo.paper.PaperClassVo"
                    javaType="java.util.List" select="listClass" column="paperid=paperid">
        </collection>
    </resultMap>
    <select id="listClass" resultType="com.xhkjedu.sstudy.vo.paper.PaperClassVo">
        select count(*) as classnum,c.classname,c.gradeid,pg.classid
        ,(select count(*) from t_paper_student ps where ps.paperid=pg.paperid and ps.classid=pg.classid and ps.psstate in(1,2))ytjnum
        from t_paper_class pg left join t_paper_student ps on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_class c on pg.classid=c.classid
        where pg.paperid=#{paperid} group by pg.classid order by c.gradeid,c.classtype,c.classorder
    </select>
    <select id="listCommitAnalyze" resultMap="commitAnalyze">
        select p.paperid,p.papername,pg.starttime
        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 (d.disabled=0 or p.directorid is null) and p.createid=#{createid}
        group by p.paperid order by pg.starttime desc limit #{papernum}
    </select>
    <!--获取作业班级列表-->
    <resultMap id="schoolMap" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result property="classid" column="classid"/>
            <result property="classname" column="classname"/>
            <result property="gradeid" column="gradeid"/>
            <result property="schoolid" column="schoolid"/>
        </collection>
    </resultMap>
    <select id="listPaperClass" resultMap="schoolMap">
        select pg.classid,c.classname,c.gradeid,pg.checked,s.schoolid,s.schoolname
        from t_paper_class pg left join t_class c on pg.classid = c.classid
        left join t_school s on c.schoolid = s.schoolid
        where pg.paperid=#{paperid}
        <if test="classids!=null and classids!=''"> and pg.classid in (${classids})</if>
        group by pg.classid
        order by if(s.schoolid=#{schoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder
    </select>
    <!--作业班级学生列表-->
    <select id="listPaperClassStudent" resultType="com.xhkjedu.sstudy.vo.gradeclass.CStudentVo">
        select ps.psstate as studentid,u.username as studentname
        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 convert(u.username using gbk)
    </select>


    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <!--作业详情-->
    <select id="getStudentPaperAnalyzeWeb" resultType="com.xhkjedu.sstudy.vo.paperstudent.StudentPaperVo">
        select p.paperid,p.papername,p.hearfile,ps.papernum,ps.paperscore,pg.starttime,pg.stoptime,ps.showanswer,pg.showtype
        ,ps.userscore,ps.costtime,truncate(ps.userscore*100/ps.paperscore,2)scorerate,u.username as createname
        ,ps.psstate,ps.pschecked,count(distinct if(ps2.userscore>ps.userscore,ps2.psid,null))+1 `rank`,count(distinct ps2.psid)stunum
        from t_paper_student ps left join t_paper p on ps.paperid = p.paperid
        left join t_paper_student ps2 on ps2.paperid=ps.paperid and ps2.classid=ps.classid
        left join t_paper_class pg on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_user u on p.createid=u.userid
        where ps.psid=#{psid} limit 1
    </select>
    <!--知识点得分情况-->
    <select id="listPointScoreByPsId" resultType="com.xhkjedu.sstudy.vo.paperstudent.StudentPointScoreVo">
        select psq.qscore,truncate(psq.stuscore*100/psq.qscore,2)scorerate
        ,(select group_concat(p.pointname separator '/') from t_question_point qp
        left join t_point p on qp.pointid=p.pointid where qp.questionid=psq.questionid)groupname
        from t_paper_student_question psq
        where psq.psid=#{psid} order by psq.qorder
    </select>


    <!--作业统计：年级下教师列表-->
    <select id="listUser" resultType="com.xhkjedu.sstudy.vo.paper.PaperGAnalyzeVo">
        select u.userid as teacherid,u.username as teachername,s.subjectname,c.gradeid
        from t_class_teacher ct inner join t_class c on ct.classid=c.classid
        inner join t_teacher_powersb tp on tp.teacherid=ct.teacherid
        inner join t_subject_book sb on sb.lsbid=tp.lsbid
        inner join t_subject s on s.subjectid=sb.subjectid
        inner join t_user u on ct.teacherid=u.userid
        where u.schoolid=#{paper.schoolid} and c.classtype in(1,2) and c.gradeid=#{paper.gradeid}
        and c.classstate=1 and c.year=#{paper.year} and u.userstate=1
        <if test="paper.subjectid!=null and paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and sb.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and c.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and c.classid in (${paper.classids})
        </if>
        group by u.userid
    </select>
    <!--作业统计：教师作业统计-->
    <select id="getPaperGrade" resultType="com.xhkjedu.sstudy.vo.paper.PaperGAnalyzeVo">
        select p.createid as teacherid,count(distinct p.paperid)fbnum
        ,count(distinct if(pg.starttime&lt;=#{paper.timestamp},ps.psid,null))ytjnum,
        count(distinct if(ps.psstate in (1,2),ps.psid,null))commitnum,
        count(distinct if(ps.psstate in (1,2) and ps.pschecked=1,ps.psid,null))checknum
        from t_paper p left join t_paper_class pg on p.paperid=pg.paperid
        left join t_paper_student ps on ps.paperid=pg.paperid and ps.classid=pg.classid
        left join t_class c on pg.classid=c.classid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and p.paperstate=22 and p.schoolid=#{paper.schoolid} and pg.schoolid=#{paper.schoolid}
        and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        <if test="paper.starttime!=null and paper.starttime!=0">
            and pg.starttime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and pg.starttime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and c.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and c.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        group by p.createid
    </select>

    <select id="listPaperStuGrade" resultType="java.util.Map">
        select a.*,ifnull(truncate(a.commitnum*100/a.sdnum,2),0)commitrate
        from(select cs.*
        ,count(distinct if(p.paperid is not null and (d.disabled=0 or p.directorid is null),ps.psid,null))sdnum
        ,count(distinct if(ps.psstate in (1,2) and p.paperid is not null and (d.disabled=0 or p.directorid is null),ps.psid,null))commitnum
        ,ifnull(truncate(sum(if(p.paperid is not null and (d.disabled=0 or p.directorid is null),ps.userscore,0))*100/
        sum(if(p.paperid is not null and (d.disabled=0 or p.directorid is null),ps.paperscore,0)),2),0)scorerate
        from (select cs2.studentid,u.username studentname
        from t_class_student cs2,t_class c,t_user u
        where c.schoolid=#{paper.schoolid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        and c.classtype in(1,2) and c.classstate=1
        and u.userstate=1 and cs2.classid=c.classid and cs2.studentid=u.userid
        <if test="paper.classid!=null and paper.classid!=0">
            and cs2.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and cs2.classid in (${paper.classids})
        </if>
        group by cs2.studentid)cs
        left join t_paper_student ps on ps.studentid=cs.studentid
        left join t_paper_class pg on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_paper p on ps.paperid=p.paperid and p.paperstate=22
        <if test="paper.starttime!=null and paper.starttime!=0">
            and pg.starttime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and pg.starttime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and ps.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ps.classid in (${paper.classids})
        </if>
        left join t_director d on p.directorid=d.directorid
        group by cs.studentid)a
        order by a.commitnum desc,a.studentid
    </select>
    <!--作业统计-按学生详情-->
    <select id="listPaperStuDetail" resultType="com.xhkjedu.sstudy.vo.paper.AStudentPaperVo">
        select
        a.*,ifnull(truncate(a.commitnum*100/a.sdnum,2),0)commitrate,ifnull(truncate(a.userscore*100/a.paperscore,2),0)scorerate
        from(select s.subjectname
        ,count(distinct ps.psid)sdnum,count(distinct if(ps.psstate in (1,2),ps.psid,null))commitnum
        ,sum(ps.userscore)userscore,sum(ps.paperscore)paperscore
        from t_paper_student ps left join t_class c on ps.classid=c.classid
        left join t_paper_class pg on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_paper p on ps.paperid=p.paperid
        left join t_subject s on p.subjectid=s.subjectid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and ps.studentid=#{paper.studentid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year} and
        p.paperstate=22
        <if test="paper.starttime!=null and paper.starttime!=0">
            and pg.starttime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and pg.starttime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and ps.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ps.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        group by p.subjectid order by s.subjectorder)a
    </select>
    <!--作业统计-按学生列表-->
    <select id="listPaperStuExport" resultType="com.xhkjedu.sstudy.vo.paper.AStudentPaperVo">
        select ps.studentid,p.subjectid,ps.psstate,ps.userscore,ps.paperscore
        from t_paper_student ps left join t_class c on ps.classid=c.classid
        left join t_paper_class pg on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_paper p on ps.paperid=p.paperid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and ps.studentid=#{paper.studentid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year} and p.paperstate=22
        <if test="paper.starttime!=null and paper.starttime!=0">
            and pg.starttime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and pg.starttime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and ps.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ps.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
    </select>

    <!--年级下所有学生作业情况-->
    <select id="listPaperAllClassStu" resultType="com.xhkjedu.sstudy.vo.paper.AStudentPaperVo">
        select ps.studentid,p.subjectid,ps.psstate,ps.userscore,ps.paperscore
        from t_paper_student ps left join t_class c on ps.classid=c.classid
        left join t_paper_class pg on ps.paperid=pg.paperid and ps.classid=pg.classid
        left join t_paper p on pg.paperid=p.paperid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and c.schoolid=#{paper.schoolid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year} and p.paperstate=22
        <if test="paper.starttime!=null and paper.starttime!=0">
            and pg.starttime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and pg.starttime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and ps.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ps.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and p.subjectid=#{paper.subjectid}
        </if>
        order by ps.studentid,ps.paperid
    </select>
    <!--作业母题列表-->
    <select id="listQuestionByQuestionIds" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        select questionid,ctype,qstem,qoption,qanswer,qanalyze,q.hashear,q.hearfile,q.qlevel
        ,(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_question q where questionid in
        <foreach collection="questionids" item="questionid" open="(" separator="," close=")">
            #{questionid}
        </foreach>
    </select>
    <!--作业母题列表（无知识点）-->
    <select id="listQuestionNoPointByQuestionIds" resultType="com.xhkjedu.sstudy.vo.paperstudent.PaperStudentQuestionVo">
        select questionid,ctype,qstem,qoption,q.hashear,q.hearfile,q.qlevel
        from t_question q where questionid in
        <foreach collection="questionids" item="questionid" open="(" separator="," close=")">
            #{questionid}
        </foreach>
    </select>
</mapper>
