<?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.PaperMapper">
    <resultMap id="typeResult" type="com.xhkjedu.sstudy.vo.paper.PaperQtypeVo">
        <result property="qtypeid" column="qtypeid"/>
        <result property="qtypename" column="qtypename"/>
        <result property="ptorder" column="ptorder"/>
        <result property="ptscore" column="ptscore"/>
        <result property="ptnum" column="ptnum"/>
        <result property="ctype" column="pctype"/>
        <collection property="questions" ofType="com.xhkjedu.sstudy.vo.question.QuestionVo">
            <result property="questionid" column="questionid"/>
            <result property="questionpid" column="questionpid"/>
            <result property="qlevel" column="qlevel"/>
            <result property="score" column="score"/>
            <result property="ptqorder" column="ptqorder"/>
            <result property="ctype" column="ctype"/>
            <result property="qstem" column="qstem"/>
            <result property="qoption" column="qoption"/>
            <result property="qanswer" column="qanswer"/>
            <result property="qanalyze" column="qanalyze"/>
            <result property="hashear" column="hashear"/>
            <result property="hearfile" column="hearfile"/>
            <result property="qn" column="qn"/>
            <result property="complexity" column="complexity"/>
            <result property="count" column="count"/>
            <result property="qtypename" column="typename"/>
            <result property="qtypeid" column="typeid"/>
        </collection>
    </resultMap>
    <!--获取作业题型列表-->
    <select id="listPaperType" resultMap="typeResult">
        select pt.qtypeid,pt.qtypename,pt.ptorder,pt.ptscore,pt.ptnum,ptq.questionid,ptq.ptqscore as score,ptq.ptqorder,ptq.qn
        ,q.questionid,ifnull(q.questionpid,q.questionid)as questionpid,q.qlevel,q.ctype,q.qstem,q.qoption,q.qanswer,q.qanalyze
        ,q.hashear,q.hearfile,q.complexity,q.count,q.qtypeid typeid,q.qtypename typename,pt.ctype pctype
        from t_paper_qtype pt left join t_paper_qtype_question ptq on pt.ptid = ptq.ptid
        left join t_question q on ptq.questionid = q.questionid
        where pt.paperid=#{paperid} order by pt.ptorder,ptq.ptqid
    </select>
    <!-- 修改作业状态 -->
    <update id="updatePaperState">
        UPDATE t_paper SET paperstate=#{pg.paperstate},hashb=#{pg.hashb},committype=#{pg.committype},timetype=#{pg.timetype}
        ,aftercommit=ifnull(#{pg.aftercommit},0)
        <if test="ctypecount==0">,checktype=4</if>
        WHERE paperid= #{pg.paperid}
    </update>
    <!-- 修改作业信息 -->
    <update id="updatePaper" parameterType="com.xhkjedu.sstudy.model.paper.TPaper">
        UPDATE t_paper SET papername=#{paper.papername}, papernum=#{paper.papernum},
       paperscore=#{paper.paperscore},papertype=#{paper.papertype},paperclass=#{paper.paperclass}
        ,hasfile=#{paper.hasfile},directorid=#{paper.directorid}
       WHERE paperid=#{paper.paperid}
    </update>

    <!--详情-->
    <select id="findById" resultType="java.util.Map">
        select p.paperid,p.papername,p.papernum,p.paperscore,p.createtime,p.createid,u.username createname,
        p.papertype,p.comm,p.paperclass,p.directorid,p.paperstate,p.hearfile,p.committype,p.timetype
        ,p.checktype,p.hasfile,p.hasreport,p.schoolid
        ,(select showtype from t_paper_class pg where pg.paperid=p.paperid limit 1)showtype
        ,p.lsbid,s.subjectname,v.versionname,sb.lsbname,d.directorname
        from t_paper p left join t_user u on u.userid=p.createid
        left JOIN t_subject_book sb on p.lsbid=sb.lsbid
        left join t_version v on sb.versionid=v.versionid
        left join t_subject s on sb.subjectid=s.subjectid
        left join t_director d on p.directorid=d.directorid
        where p.paperid=#{paperid} limit 1
    </select>

    <!--作业列表-->
    <resultMap id="paperResult" type="com.xhkjedu.vo.paper.PaperListVo">
        <result property="paperid" column="paperid"/>
        <result property="papername" column="papername"/>
        <result property="papertype" column="papertype"/>
        <result property="paperclass" column="paperclass"/>
        <result property="paperstate" column="paperstate"/>
        <result property="checktype" column="checktype"/>
        <result property="createid" column="createid"/>
        <result property="createtime" column="createtime"/>
        <result property="createname" column="createname"/>
        <result property="starttime" column="starttime"/>
        <result property="stoptime" column="stoptime"/>
        <result property="papernum" column="papernum"/>
        <result property="timetype" column="timetype"/>
        <result property="showtype" column="showtype"/>
        <result property="schoolid" column="schoolid"/>
        <result property="logschoolid" column="logschoolid"/>
        <result property="classnum" column="classnum"/>
        <result property="ytjnum" column="ytjnum"/>
        <result property="ypgnum" column="ypgnum"/>
        <result property="hashb" column="hashb"/>
        <result property="aftercommit" column="aftercommit"/>
        <collection property="schools" ofType="java.util.Map" javaType="java.util.List"
                    select="listClass" column="{paperid=paperid,createid=createid,checkid=checkid,checktype=checktype,logschoolid=logschoolid,classids=classids}">
        </collection>
        <collection property="checklist" ofType="java.util.Map" javaType="java.util.List"
                    select="listPaperCheckStu" column="{paperid=paperid,checktype=checktype,logschoolid=logschoolid,classids=classids}">
            <result property="userid" column="userid"/>
            <result property="username" column="username"/>
            <result property="classid" column="classid"/>
            <result property="classname" column="classname"/>
            <result property="gradeid" column="gradeid"/>
        </collection>
    </resultMap>
    <resultMap id="classResult" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <collection property="classes" ofType="com.xhkjedu.vo.paper.PaperClassVo" javaType="java.util.List">
            <result property="schoolid" column="schoolid"/>
            <result property="classid" column="classid"/>
            <result property="classname" column="classname"/>
            <result property="gradeid" column="gradeid"/>
            <result property="classnum" column="classnum"/>
            <result property="ytjnum" column="ytjnum"/>
            <result property="ypgnum" column="ypgnum"/>
            <result property="studentname" column="studentname"/>
            <result property="checkid" column="checkid"/>
            <result property="teachername" column="teachername"/>
            <result property="checked" column="checked"/>
        </collection>
    </resultMap>
    <!--<select id="listClass" resultMap="classResult">
        select count(*)classnum,ps.classid,c.classname,c.gradeid,pg.checked
        ,count(distinct case when ps.psstate in(1,2,4) then ps.psid else null end)ytjnum
        ,count(distinct case when ps.pschecked=1 then ps.psid else null end)ypgnum
        <if test="checktype==3">
        ,(select group_concat(distinct u.username) from t_paper_student ps1 left join t_user u on ps1.checkid=u.userid
        where ps1.paperid=ps.paperid and ps1.classid=ps.classid)studentname
        </if>
        <if test="checktype==1">
        ,pg.checkid,(select group_concat(distinct u.username) from t_user u where pg.checkid=u.userid)teachername
        </if>
        from t_paper_class pg left join t_class c on pg.classid=c.classid
        left join t_paper_student ps on ps.paperid=pg.paperid and ps.classid=pg.classid
        where pg.paperid=#{paperid}
        <if test="checkid!=null and checkid!=0 and checkid!=createid">
            and pg.checkid=#{checkid}
        </if>
        group by pg.classid order by c.gradeid,c.classtype,c.classorder
    </select>-->
    <!--本校作业班级-->
    <select id="listClass" resultMap="classResult">
        select count(*) as classnum,ps.classid,c.classname,c.gradeid,c.schoolid,s.schoolname
        ,count(distinct if(ps.psstate in(1,2,4), ps.psid, null))ytjnum
        ,count(distinct if(ps.pschecked=1, ps.psid, null))ypgnum
        from t_paper_student ps left join t_class c on ps.classid=c.classid
        left join t_school s on c.schoolid = s.schoolid
        where ps.paperid=#{paperid}
        <if test="classids!=null and classids!=''">
            and ps.classid in(${classids})
        </if>
        group by ps.classid
        order by if(s.schoolid=#{logschoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder
    </select>
    <!--本校作业班级批阅人-->
    <resultMap id="checkUserResult" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <result property="checktype" column="checktype"/>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result property="schoolid" column="schoolid"/>
            <result property="classid" column="classid"/>
            <result property="classname" column="classname"/>
            <result property="gradeid" column="gradeid"/>
            <collection property="users" ofType="java.util.Map" javaType="java.util.List">
                <result property="userid" column="userid"/>
                <result property="username" column="username"/>
            </collection>
        </collection>
    </resultMap>
    <select id="listPaperCheckStu" resultMap="checkUserResult">
         <if test="checktype==1">
           select pg.checkid as userid,u.username,c.classid,c.classname,c.gradeid,pg.schoolid,s.schoolname
           from t_paper_class pg left join t_school s on pg.schoolid=s.schoolid
           left join t_user u on pg.checkid=u.userid
           left join t_class c on pg.classid=c.classid
           where pg.paperid=#{paperid}
             <if test="classids!=null and classids!=''">
                 and pg.classid in(${classids})
             </if>
             order by if(s.schoolid=#{logschoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder,convert(u.username using gbk)
         </if>
       <if test="checktype==3">
           select ps.checkid as userid,u.username,c.classid,c.classname,c.gradeid,ps.schoolid,s.schoolname
           from t_paper_student ps left join t_school s on ps.schoolid=s.schoolid
           left join t_user u on ps.checkid=u.userid
           left join t_class c on ps.classid=c.classid
           where ps.paperid=#{paperid}
           <if test="classids!=null and classids!=''">
               and ps.classid in(${classids})
           </if>
           group by ps.classid,ps.checkid
           order by if(s.schoolid=#{logschoolid},0,s.schoolid),c.gradeid,c.classtype,c.classorder,convert(u.username using gbk)
       </if>
        <if test="checktype!=1 and checktype!=3">
            select checktype from t_paper where paperid=#{paperid}
        </if>
    </select>

    <!--教师作业列表-->
    <select id="listAll" resultMap="paperResult">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.schoolid,p.hasreport,p.timetype,p.hashb,
        #{paper.schoolid} logschoolid,u.username createname,pg.starttime,pg.stoptime,pg.showtype,p.papernum,p.createid,#{paper.createid} as checkid
        ,group_concat(pg.classid) classids,p.aftercommit
        from t_paper p left join t_user u on p.createid=u.userid
        left join t_paper_class pg on p.paperid=pg.paperid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and (d.disabled=0 or p.directorid is null)
        <if test="userVo.usertype!=null and userVo.usertype==0">
            and p.schoolid=#{userVo.schoolid}
        </if>
        <if test="userVo.usertype!=0">
            and (p.createid=#{paper.createid} or pg.checkid=#{paper.createid} or
            (p.hashb=1 and p.schoolid=#{paper.schoolid} and pg.classid in (${paper.classids})) or
            (pg.classid in (${paper.bxclassids})))
        </if>
        <if test="paper.lsbid!=null and paper.lsbid!='0'.toString() and paper.lsbid!=''">
            and p.lsbid=#{paper.lsbid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==1">
            and p.directorid=#{paper.directorid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==0">
            and d.directorall like concat('%',#{paper.directorall},'%')
        </if>
        <if test="paper.papertype!=0 and paper.papertype!=null">
            and p.papertype=#{paper.papertype}
        </if>
        <if test="paper.papername!=null and paper.papername!=''">
            and p.papername like '%${paper.papername}%'
        </if>
        <if test="paper.starttime!=null and paper.starttime!=0">
            and p.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and p.createtime&lt;=#{paper.stoptime}
        </if>
        group by p.paperid
        <if test="paper.psstate==0 || paper.psstate==1">
            having min(pg.checked)=#{paper.psstate}
        </if>
        order by p.createtime desc,p.paperid desc
    </select>
    <select id="listAllMy" resultMap="paperResult">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.schoolid,p.hasreport,p.timetype,p.hashb,
        #{paper.schoolid} logschoolid,u.username createname,pg.starttime,pg.stoptime,pg.showtype,p.papernum,p.createid,#{paper.createid} as checkid
        ,group_concat(pg.classid) classids,p.aftercommit
        from t_paper p left join t_user u on p.createid=u.userid
        left join t_paper_class pg on p.paperid=pg.paperid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.createid=#{paper.createid} and (d.disabled=0 or p.directorid is null)
        <if test="paper.lsbid!=null and paper.lsbid!='0'.toString() and paper.lsbid!=''">
            and p.lsbid=#{paper.lsbid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==1">
            and p.directorid=#{paper.directorid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==0">
            and d.directorall like concat('%',#{paper.directorall},'%')
        </if>
        <if test="paper.papertype!=0 and paper.papertype!=null">
            and p.papertype=#{paper.papertype}
        </if>
        <if test="paper.papername!=null and paper.papername!=''">
            and p.papername like '%${paper.papername}%'
        </if>
        <if test="paper.starttime!=null and paper.starttime!=0">
            and p.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and p.createtime&lt;=#{paper.stoptime}
        </if>
        group by p.paperid
        <if test="paper.psstate==0 || paper.psstate==1">
            having min(pg.checked)=#{paper.psstate}
        </if>
        order by p.createtime desc,p.paperid desc
    </select>
    <!--教师作业列表(未发布)-->
    <select id="listWfbPapers" resultType="com.xhkjedu.vo.paper.PaperListVo">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.createid,p.hasreport,p.schoolid
        ,(select u.username from t_user u where u.userid=p.createid)createname
        ,p.aftercommit
        from t_paper p left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.paperstate=21 and (d.disabled=0 or p.directorid is null)
        <if test="userVo.usertype!=null and userVo.usertype==0">
            and p.schoolid=#{userVo.schoolid}
        </if>
        <if test="userVo.usertype!=0">
            and p.createid=#{paper.createid}
        </if>
        <if test="paper.lsbid!=null and paper.lsbid!='0'.toString() and paper.lsbid!=''">
            and p.lsbid=#{paper.lsbid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==1">
            and p.directorid=#{paper.directorid}
        </if>
        <if test="paper.directorid!=null and paper.directorid!='0'.toString() and paper.directorid!='' and paper.directorLeaf==0">
            and d.directorall like concat('%',#{paper.directorall},'%')
        </if>
        <if test="paper.papertype!=0 and paper.papertype!=null">
            and p.papertype=#{paper.papertype}
        </if>
        <if test="paper.papername!=null and paper.papername!=''">
            and p.papername like '%${paper.papername}%'
        </if>
        <if test="paper.starttime!=null and paper.starttime!=0">
            and p.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and p.createtime&lt;=#{paper.stoptime}
        </if>
        group by p.paperid order by p.createtime desc,p.paperid desc
    </select>
    <!--教师批阅列表-->
    <!--<select id="correctList" resultMap="paperResult">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.createid,#{checkid} as checkid,p.schoolid as logschoolid
        ,u.username createname,pg.starttime,pg.stoptime,p.papernum
        ,group_concat(pg.classid) classids
        from t_paper p left join t_user u on p.createid=u.userid
        left join t_paper_class pg on p.paperid=pg.paperid
        left join t_paper_student ps on p.paperid=ps.paperid and pg.classid=ps.classid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and p.checktype=1 and ps.checkid=#{checkid} and ps.psstate in(1,2,4) and ps.pschecked!=1
        group by p.paperid order by p.createtime desc
    </select>-->
    <select id="correctList" resultMap="paperResult">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.schoolid,p.hasreport,p.timetype,p.hashb
        ,#{schoolid} as logschoolid,u.username createname,pg.starttime,pg.stoptime,pg.showtype,p.papernum,p.createid,#{checkid} as checkid
        ,group_concat(pg.classid) classids,p.aftercommit
        from t_paper p left join t_user u on p.createid=u.userid
        left join t_paper_class pg on p.paperid=pg.paperid
        left join t_paper_student ps on p.paperid=ps.paperid and pg.classid=ps.classid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and (d.disabled=0 or p.directorid is null)
        and (p.createid=#{checkid} or ps.checkid=#{checkid}) and ps.psstate in(1,2,4) and ps.pschecked!=1
        group by p.paperid order by p.createtime desc
    </select>
    <!--设置批改方式-->
    <update id="updateCheckType">
        update t_paper set checktype=#{checktype} where paperid=#{paperid}
    </update>
    <!--获取批改学生列表-->
    <resultMap id="checkResult" type="com.xhkjedu.sstudy.vo.paper.CheckClassVo">
        <result property="classid" column="classid"/>
        <result property="classname" column="classname"/>
        <result property="classnum" column="classnum"/>
        <result property="ytjnum" column="ytjnum"/>
        <result property="ypynum" column="ypynum"/>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.paperstudent.StudentVo"
                    javaType="java.util.List" select="listStudent" column="{paperid=paperid,classid=classid}">
        </collection>
    </resultMap>
    <resultMap id="studentResult" type="com.xhkjedu.sstudy.vo.paperstudent.StudentVo">
        <result property="psid" column="psid"/>
        <result property="studentid" column="studentid"/>
        <result property="studentname" column="studentname"/>
        <result property="pschecked" column="pschecked"/>
    </resultMap>
    <select id="listStudent" resultMap="studentResult">
        select ps.psid,ps.studentid,u.username studentname,ps.pschecked
        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(1,2,4) and ps.autocheck=0
        order by ps.pschecked,convert(u.username using gbk),u.userid
    </select>
    <select id="listStudentsByPaperIdCheckId" resultMap="checkResult">
        select ps.classid,c.classname,c.gradeid,count(*) as classnum,ps.paperid
        ,count(distinct IF(ps.psstate in(1,2,4), ps.psid, null)) ytjnum
        ,count(distinct IF(ps.pschecked = 1, ps.psid, null)) ypynum
        from t_paper_student ps left join t_class c on ps.classid = c.classid
        where ps.paperid = #{paperid} and ps.classid = #{classid}
    </select>
    <!--作业附件列表-->
    <select id="listPaperFile" resultType="com.xhkjedu.sstudy.model.paper.TPaperFile">
        SELECT * FROM t_paper_file WHERE paperid=#{paperid}
        <if test="fileclass!=null and fileclass!=0">
            and fileclass=#{fileclass}
        </if>
        ORDER BY fileorder
    </select>
    <!--获取年级下教师发布作业列表-->
    <select id="listPaper" resultMap="paperResult">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.timetype
        ,pg.starttime,pg.stoptime,p.papernum,p.createid,#{paper.teacherid} as checkid,p.schoolid,#{paper.schoolid} as logschoolid
        ,pg.showtype,p.hasreport,p.hashb,group_concat(pg.classid) classids,p.aftercommit
        ,(select u.username from t_user u where u.userid=p.createid)createname
        from t_paper p left join t_paper_class pg on p.paperid=pg.paperid
        left join t_class c on pg.classid=c.classid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.createid=#{paper.createid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        and pg.schoolid=#{paper.schoolid} and (d.disabled=0 or p.directorid is null)
        <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.paperid order by p.createtime desc
    </select>

    <!--作业删除-->
    <delete id="delPaper">
        DELETE qe.*,qc.*,qs.*,p.*
        FROM t_paper p LEFT JOIN t_question_class qc ON p.paperid=qc.paperid
        LEFT JOIN t_question_explain qe ON qc.questionid=qe.questionid AND qc.qctype=qe.qctype
        LEFT JOIN t_question_student qs ON p.paperid=qs.paperid
        WHERE p.paperid=#{paperid}
    </delete>

    <!--修改作业删除状态-->
    <update id="updatePaperDeleted">
        update t_paper set deleted=#{deleted} where paperid=#{paperid}
    </update>
    <!--修改补交状态-->
    <update id="updateAfterCommit">
        update t_paper set aftercommit=#{paper.aftercommit} where paperid=#{paper.paperid}
    </update>

    <!--获取作业原文件保存地址列表-->
    <select id="listSourcePath" resultType="java.lang.String">
        select distinct sourcepath from t_paper_file where paperid=#{paperid} and fileclass=1
    </select>
    <!--获取今天布置作业数量-->
    <select id="getPaperNum" resultType="java.lang.Integer">
        select count(*)+1 from t_paper where subjectid=#{subjectid} and createid=#{createid}
        and FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()
    </select>
    <!--获取作业状态-->
    <select id="getPaperState" resultType="java.lang.Integer">
        select paperstate from t_paper where paperid=#{paperid}
    </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"/>
            <result property="checked" column="checked"/>
        </collection>
    </resultMap>
    <select id="listPaperClass" resultMap="schoolMap">
        select pg.classid,c.classname,c.gradeid,pg.checked,c.schoolid,s.schoolname
        from t_paper_class pg left join t_class c on pg.classid = c.classid
        left join t_paper_student ps on pg.paperid=ps.paperid and pg.classid=ps.classid
        left join t_school s on c.schoolid = s.schoolid
        where pg.paperid=#{paperid} and ps.psstate in(1,2,4)
        <if test="checkid!=null and checkid!=0">
            and pg.checkid=#{checkid}
        </if>
        group by pg.classid
        order by ps.pschecked,if(c.schoolid = #{schoolid}, 0, c.schoolid)
        ,c.gradeid,c.classtype,c.classorder
    </select>
    <!--根据作业id获取作业分类-->
    <select id="getPaperTypeById" resultType="java.lang.Integer">
        select papertype from t_paper where paperid=#{paperid}
    </select>
    <!--作业母题列表-->
    <select id="listQuestionByQuestionIds" resultType="com.xhkjedu.sstudy.vo.question.QuestionVo">
        select questionid,ctype,qstem,qoption,qanswer,qanalyze,q.hashear,q.hearfile,q.count
        from t_question q where questionid in
        <foreach collection="questionids" item="questionid" open="(" separator="," close=")">
            #{questionid}
        </foreach>
    </select>
    <!--试题知识点列表-->
    <select id="listQuestionPoints" resultType="java.util.Map">
        select qp.questionid,qp.pointid,p.pointname from t_question_point qp
        left join t_point p on qp.pointid=p.pointid
        where qp.questionid in
        <foreach collection="questionids" item="questionid" open="(" separator="," close=")">
            #{questionid}
        </foreach>
        group by qp.questionid,qp.pointid order by p.pointorder
    </select>
    <!--根据试卷id获取科目id-->
    <select id="getSubjectIdByPid" resultType="java.lang.String">
        select subjectid from t_paper p where p.paperid=#{paperid}
    </select>

    <!--获取所有试卷用于重新生成分析-->
    <select id="listPapersForAnalyze" resultType="com.xhkjedu.sstudy.model.paper.TPaper">
        select * from t_paper
        <if test="paperid!=null and paperid!=''">
            where paperid=#{paperid}
        </if>
        order by paperid
    </select>
    <!--获获取未生成知识点靶向试卷用于重新生成分析-->
    <select id="listPapersForAnalyze2" resultType="com.xhkjedu.sstudy.model.paper.TPaper">
        select p.* from t_paper_analyze a left join t_paper p on a.paperid=p.paperid
        where p.papertype=3 and (pointjson is null or pointjson='')
        <if test="paperid!=null and paperid!=''">
            and p.paperid=#{paperid}
        </if>
        order by p.paperid
    </select>

    <!--获取作业听力文件-->
    <select id="getHearfileByPaperid" resultType="java.lang.String">
        select hearfile from t_paper where paperid=#{paperid}
    </select>

    <!--作业发布班级和学校-->
    <select id="listSchoolClassByPaperid" resultType="java.util.Map">
        select s.schoolid,s.schoolname,pg.classid,c.classname,c.gradeid,c.year,p.subjectid
        from t_paper_class pg
        left join t_class c on pg.classid=c.classid
        left join t_school s on s.schoolid=c.schoolid
        left join t_paper p on pg.paperid=p.paperid
        where pg.paperid=#{paperid}
        order by if(s.schoolid=#{schoolid},0,s.schoolid),c.classtype,c.classorder
    </select>
    <!--作业发布班级对应科目下所有教师和教师任教班级-->
    <select id="listTeacherClassByPaperid" resultType="java.util.Map">
        select ct.teacherid,u.username as teachername,u.loginname,u.schoolid,c.classid,c.classname from t_class_teacher ct
        left join t_teacher_powersb tp on ct.teacherid=tp.teacherid
        left join t_user u on u.userid=ct.teacherid
        left join t_class c on ct.classid=c.classid
        left join t_paper_class pg on u.schoolid=pg.schoolid
        where tp.subjectid=#{param.subjectid} and u.userstate=1 and u.usertype=1 and pg.paperid=#{param.paperid}
        and c.classstate!=10 and c.year=#{param.year} and c.classtype in (1,2) and c.gradeid=#{param.gradeid}
        group by ct.teacherid order by c.classtype,c.classorder, convert(u.username using gbk)
    </select>
    <resultMap id="paperStudentList" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <collection property="classlist" 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="classnum" column="classnum"/>
            <collection property="students" ofType="java.util.Map" javaType="java.util.List">
                <result property="studentid" column="studentid"/>
                <result property="studentname" column="studentname"/>
                <result property="headpic" column="headpic"/>
            </collection>
        </collection>
    </resultMap>
    <!--获取作业学生列表-->
    <select id="listPaperStudentsByPaperid" resultMap="paperStudentList">
        select u.schoolid,s.schoolname, pg.classid,c.classname,c.gradeid,c.classnum,cs.studentid,u.username studentname
        from t_paper_class pg left join t_class c on pg.classid=c.classid
        left join t_paper_student cs on pg.paperid=cs.paperid and pg.classid=cs.classid
        left join t_user u on cs.studentid=u.userid
        left join t_school s on s.schoolid=u.schoolid
        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>

    <!--获取所有作业，处理学生试题关联关系-->
    <select id="listPapersForAllFb" resultType="com.xhkjedu.sstudy.model.paper.TPaper">
        select * from t_paper where paperstate=22 order by paperid
    </select>
    <!--获取已过截止时间作业班级数量-->
    <select id="countEndPaperClassById" resultType="java.lang.Integer">
        select count(*) from t_paper_class where paperid=#{paperid} and #{stoptime}>=stoptime
    </select>
    <!--获取批阅班级数量-->
    <select id="getCheckClassNum" resultType="java.lang.Integer">
        select count(distinct ps.classid)
        from t_paper_student ps
        <if test="checkid!=null and checkid!=0">
            left join t_paper_class pg on ps.paperid=pg.paperid and ps.classid=pg.classid
        </if>
        where ps.paperid=#{paperid} and ps.psstate in(1,2,4) and ps.autocheck=0
        <if test="checkid!=null and checkid!=0">
            and pg.checkid=#{checkid}
        </if>
    </select>


</mapper>
