<?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.sapi.mapper.component.ComponentMapper">

    <!--作业-->
    <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>
    <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="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>
    <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="listCorrectPages" resultType="java.util.Map">
        select p.paperid,p.papername,p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,
        p.hasreport,p.timetype,u.username createname,pg.starttime,pg.stoptime,pg.showtype,p.aftercommit
        ,count(DISTINCT ps.psid) classnum,count(DISTINCT(if(ps.psstate>0,ps.psid,null))) ytjnum
        ,count(DISTINCT(if(ps.pschecked=1,ps.psid,null))) ypgnum
        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=#{userid} or ps.checkid=#{userid}) and ps.psstate in(1,2,4) and ps.pschecked!=1
        group by p.paperid order by p.createtime desc limit #{limitSize}

    </select>

    <!--作业-我的-->
    <select id="listPaperForMy" resultType="java.util.Map">
        select p.paperid,p.papername,
        p.papertype,p.paperclass,p.paperstate,p.checktype,p.createtime,p.hasreport,p.timetype,
        pg.starttime,pg.stoptime,p.papernum,p.aftercommit
        ,count(DISTINCT ps.psid) classnum,count(DISTINCT(if(ps.psstate>0,ps.psid,null))) ytjnum
        ,count(DISTINCT(if(ps.pschecked=1,ps.psid,null))) ypgnum
        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
        left join t_paper_student ps on p.paperid=ps.paperid
        where p.deleted=0 and p.createid=#{params.userid} and (d.disabled=0 or p.directorid is null)
        group by p.paperid order by p.createtime desc,p.paperid desc limit #{params.limitSize}
    </select>

    <!--通知公告-->
    <select id="listNotices" resultType="com.xhkjedu.sapi.vo.notice.NoticeVo">
        select n.noticeid,n.noticetitle,n.noticetxt,n.createtime,s.subjectname,u.username createname,IFNULL(nt.readed,0) as readed
        from t_notice n left join t_user u on n.createid=u.userid
        left join t_notice_user nt on n.noticeid = nt.noticeid
        left join t_subject s on n.subjectid=s.subjectid
        where nt.userid=#{userid}
        group by n.noticeid order by nt.readed,n.createtime desc limit #{limitSize}
    </select>

    <!--教学教研-->
    <select id="listResearchs" resultType="java.util.Map">
        SELECT r.researchid,r.researchname,r.researchcover,r.starttime,r.stoptime,
        r.createid,r.createtime,u.username AS createname,r.readnum,ru.rustate
        FROM t_research_user ru LEFT JOIN t_research r ON ru.researchid=r.researchid
        LEFT JOIN t_user u ON r.createid=u.userid
        WHERE ru.userid=#{userid} and r.starttime&lt;=#{timestamp}
        GROUP BY r.researchid ORDER BY r.createtime desc limit #{limitSize}
    </select>
    
    <!--我的空间-->
    <select id="listMySpaces" resultType="java.util.Map">
        select r.resourceid,r.resourcename,r.resourceclass,r.suffix,r.resourcesize,r.resourcecover,r.resourceurl,
        r.createtime from t_resource r
        left join t_resource_director d on r.resourceid=d.resourceid
        left join t_teacher_powersb p on p.teacherid=r.createid and p.lsbid=d.lsbid
        where r.createid=#{userid} and r.`level`=2 and r.resourcestate=1
        group by r.resourceid order by r.createtime desc limit #{limitSize}
    </select>

    <!--校本资源库-->
    <select id="listSchoolResources" resultType="java.util.Map">
        select r.resourceid,r.resourcename,r.resourceclass,r.suffix,r.resourcesize,r.resourcecover,resourceurl,
        r.createtime from t_resource r
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        left join t_teacher_powersb p on p.teacherid=r.createid and p.lsbid=d.lsbid
        where r.resourcestate=1 and r.`level`=1 and d.disabled=0
        and (r.belong in(1,2) or (r.belong in(3,10) and r.schoolid=#{schoolid}) or (r.belong=4 and r.createid=#{userid}))
        group by r.resourceid order by r.createtime desc limit #{limitSize}
    </select>

    <!--同步视频-->
    <select id="listVideos" resultType="java.util.Map">
        select r.resourceid,r.resourcename,r.resourceclass,r.suffix,r.resourcesize,r.resourcecover,
        r.createtime,r.duration,u.username createname from t_resource r
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        left join t_teacher_powersb p on p.teacherid=r.createid and p.lsbid=d.lsbid
        left join t_user u on r.createid=u.userid
        where r.resourcestate=1 and r.`level`=4 and d.disabled=0 and r.resourcetype=5
        and (r.belong in(1,2) or (r.belong in(3,10) and r.schoolid=#{schoolid}) or (r.belong=4 and r.createid=#{userid}))
        group by r.resourceid order by r.createtime desc limit #{limitSize}
    </select>

    <resultMap id="guidesForMap" type="java.util.Map">
        <result column="guideid" property="guideid"/>
        <result column="guidename" property="guidename"/>
        <result column="createid" property="createid"/>
        <result column="createtime" property="createtime"/>
        <result column="totalnum" property="totalnum"/>
        <result column="commitnum" property="commitnum"/>
        <collection property="resources" ofType="java.util.Map"
                    javaType="java.util.List" select="listResourcesByGuideid" column="guideid">
        </collection>
    </resultMap>

    <!--预习-->
    <select id="listGuides" resultMap="guidesForMap">
        select g.guideid,g.guidename,g.createtime,g.createid
        ,count(DISTINCT gs.rgsid) totalnum,
        count(DISTINCT(if(gs.readtime is not null,gs.rgsid,null))) commitnum
        from t_guide g
        left join t_guide_group_student gs on g.guideid=gs.guideid
        left join t_director d on g.directorid=d.directorid
        left join t_teacher_powersb p on p.teacherid=g.createid and p.lsbid=g.lsbid
        where d.disabled=0 and g.createid=#{userid}
        group by g.guideid order by g.createtime desc,g.guidename LIMIT #{limitSize}
    </select>
    <select id="listResourcesByGuideid" resultType="java.util.Map">
        select r.resourceid, r.resourcename,r.resourceurl,r.resourcecover,r.converted,r.pdfurl,
        r.suffix,r.mp4code,r.resourcesize,r.duration,r.resourceclass
        from t_resource r left join t_guide_resource gr on r.resourceid=gr.resourceid
        where gr.guideid=#{guideid} order by gr.grid
    </select>

    <!--背诵-->
    <select id="listReciteds" resultType="java.util.Map">
        select r.reciteid,r.recitetitle,r.subjectid,r.createid,r.createtime,r.recitestate,r.recitecont
        ,r.schoolid,r.hashb,rg.starttime,rg.stoptime,count(DISTINCT rs.rsid) totalnum,
        count(DISTINCT(if (rs.rsstate>0,rs.rsid,null))) commitnum
        from t_recite r left join t_recite_group rg on r.reciteid=rg.reciteid
        left join t_recite_student rs on r.reciteid=rs.reciteid
        where r.createid=#{userid} group by r.reciteid order by r.createtime desc limit #{limitSize}
    </select>

    <!--课堂回顾-->
    <select id="listClassrooms" resultType="java.util.Map">
        SELECT c.roomid,c.roomname,c.classid,g.classname,g.gradeid,c.createtime,c.endtime,
        c.realstunum,u.username createname,c.classnum,d.directorname,
        (select rfpath from t_classroom_file where roomid=c.roomid order by rfid limit 1) rfpath,
        (select count(rs.asksid)+(select count(ps.psid) from t_classroom_paper p
        left join t_classroom_paper_student ps on p.paperid=ps.paperid and ps.pschecked!=1 where p.roomid=c.roomid)
        from t_classroom_askteacher rt
        left join t_classroom_askstudent rs on rt.asktid=rs.asktid and rs.radiocorrect=0
        where rt.roomid=c.roomid and rt.asktype between 32 and 33)wpynum
        FROM t_classroom c LEFT JOIN t_class g ON c.classid=g.classid
        left join t_user u on c.teacherid=u.userid
        left join t_director d on c.directorid=d.directorid
        WHERE d.disabled=0 and c.teacherid=#{userid}
        ORDER BY c.createtime DESC limit #{limitSize}
    </select>

    <!--预习统计-班级-->
    <select id="listGuidesStatisticClass" resultType="com.xhkjedu.sapi.vo.component.StudyStatisticVo">
        select g.guideid as id,g.createtime,gg.classid,c.gradeid,c.classname
        ,count(DISTINCT(gs.rgsid)) totalnum
        ,count(DISTINCT(if(gs.readtime is not null,gs.rgsid,null))) commitnum
        from t_guide_group gg left join t_guide g on gg.guideid=g.guideid and g.schoolid=gg.schoolid
        left join t_class c on gg.classid=c.classid
        left join t_guide_group_student gs on g.guideid=gs.guideid and gg.classid=gs.classid
        where g.schoolid=#{params.schoolid} and g.createtime BETWEEN #{params.starttime} and #{params.stoptime}
        <if test="params.usertype == 1"> and gg.classid in (${params.classids})</if>
        group by gg.rgid order by c.gradeid,c.classtype,c.classorder
    </select>
    <!--预习统计-科目-->
    <select id="listGuidesStatisticSubject" resultType="com.xhkjedu.sapi.vo.component.StudyStatisticVo">
        select g.guideid as id,g.createtime,g.subjectid,s.subjectname
        ,count(DISTINCT(gs.rgsid)) totalnum
        ,count(DISTINCT(if(gs.readtime is not null,gs.rgsid,null))) commitnum
        from t_guide_group_student gs left join t_guide g on gs.guideid=g.guideid and gs.schoolid=g.schoolid
        left join t_subject s on g.subjectid=s.subjectid
        where g.schoolid=#{params.schoolid} and g.createtime BETWEEN #{params.starttime} and #{params.stoptime}
        <if test="params.usertype == 1"> and g.createid=#{params.userid}</if>
        group by g.guideid order by s.subjectorder
    </select>

    <!--作业统计-班级-->
    <select id="listPapersStatisticClass" resultType="com.xhkjedu.sapi.vo.component.StudyStatisticVo">
        select p.paperid as id,pc.createtime,pc.classid,c.gradeid,c.classname
        ,count(DISTINCT(ps.psid)) totalnum
        ,count(DISTINCT(if(ps.psstate>0,ps.psid,null))) commitnum
        ,count(DISTINCT(if(ps.pschecked=1,ps.psid,null))) checknum
        from t_paper_class pc left join t_paper p on pc.paperid=p.paperid and p.schoolid=pc.schoolid
        left join t_class c on pc.classid=c.classid
        left join t_paper_student ps on ps.paperid=p.paperid and ps.classid=pc.classid
        where p.schoolid=#{params.schoolid} and pc.pgstate=22 and pc.createtime BETWEEN #{params.starttime} and #{params.stoptime}
        <if test="params.usertype == 1"> and pc.classid in (${params.classids})</if>
        group by pc.pcid order by c.gradeid,c.classtype,c.classorder
    </select>

    <!--作业统计-科目-->
    <select id="listPapersStatisticSubject" resultType="com.xhkjedu.sapi.vo.component.StudyStatisticVo">
        select p.paperid as id,pc.createtime,p.subjectid,s.subjectname
        ,count(DISTINCT(ps.psid)) totalnum
        ,count(DISTINCT(if(ps.psstate>0,ps.psid,null))) commitnum
        ,count(DISTINCT(if(ps.pschecked=1,ps.psid,null))) checknum
        from t_paper_student ps left join t_paper p on ps.paperid=p.paperid and ps.schoolid=p.schoolid
        left join t_paper_class pc on pc.paperid=p.paperid and pc.schoolid=p.schoolid
        left join t_subject s on p.subjectid=s.subjectid
        where p.schoolid=#{params.schoolid} and pc.pgstate=22 and pc.createtime BETWEEN #{params.starttime} and #{params.stoptime}
        <if test="params.usertype == 1"> and p.createid=#{params.userid}</if>
        group by p.paperid order by s.subjectorder
    </select>

    <!--直播巡课-->
    <select id="listLivePatrol" resultType="java.util.Map">
        select cs.sectiondate as timestr,count(distinct cs.sectionid)as num,
        count(distinct case when cs.sectionstate=0 then cs.sectionid else null end)wksnum,
        count(distinct case when cs.sectionstate=2 then cs.sectionid else null end)jxznum,
        count(distinct case when cs.sectionstate=3 then cs.sectionid else null end)yjsnum,
        count(distinct case when cs.sectionstate=4 then cs.sectionid else null end)ygqnum
        from l_course_section cs left join l_course c on cs.courseid=c.courseid
        <if test="params.usertype == 1 and params.classids!=null and params.classids!=''">
            left join l_course_class cc on cc.courseid=c.courseid
        </if>
        where c.deleted=1 and c.schoolid=#{params.schoolid}
        and cs.sectiondate=#{params.todayDate}
        <if test="params.usertype == 1 and params.classids!=null and params.classids!=''">
            and cc.classid in (${params.classids})
        </if>
        <if test="params.usertype==1">
            and cs.teacherid=#{params.userid}
        </if>
        group by cs.sectiondate
    </select>

    <!--教师直播-->
    <select id="listLiveTeachers" resultType="java.util.Map">
        select c.courseid,c.gradeid,c.stunum,c.comm,c.classid,s.subjectname,u.username teachername,u.headpic,
        (select t.typename from l_course_type t where t.ctypeid=c.ctypeid)typename,
        cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.stunum cynum ,cs.planbegintime,cs.planendtime,cs.sectionstate,
        cs.begintime,cs.endtime,
        timestampdiff(second,CONCAT(cs.sectiondate,' ',cs.planbegintime),CONCAT(cs.sectiondate,' ',cs.planendtime)) totaltime,
        (select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        left join t_subject s on c.subjectid=s.subjectid
        left join t_user u on cs.teacherid=u.userid
        where cs.teacherid=#{params.userid} and c.deleted=1 and cs.sectiondate=#{params.selectDate}
        order by cs.sectionstate,abs(timestampdiff(second,concat(cs.sectiondate,' ',cs.planbegintime),#{params.todayDatetime})),
        cs.sectionid limit #{params.limitSize}
    </select>

    <!--班级错题集-最近一次作业错题数量-->
    <select id="getClassErrorQuesLastPaper" resultType="java.lang.Integer">
        select count(qc.qcid) lastnum
        from t_question_class qc left join t_paper p on qc.paperid=p.paperid
        where p.deleted=0 and p.hasreport=1 and p.createid=#{params.userid} and qc.classid=#{params.classid} and qc.qctype!=3
        group by p.paperid order by qc.createtime desc limit 1
    </select>
    <!--班级做提及-错题数量和错题讲解数量-->
    <select id="getClassErrorQuesAndExplain" resultType="java.util.Map">
        select count(DISTINCT qc.qcid) quesnum,count(DISTINCT qe.qcpid) explainnum
        from t_question_class qc left join t_paper p on qc.paperid=p.paperid
        left join t_question_explain qe on qc.questionid=qe.questionid
        where p.deleted=0 and p.hasreport=1 and p.createid=#{params.userid} and qc.classid=#{params.classid}
        and qc.createtime BETWEEN #{params.starttime} and #{params.stoptime}
        order by qc.createtime desc
    </select>

    <!--我的班级-权限班级-->
    <select id="listClassidsForTeacher" resultType="java.lang.Integer">
        select c.classid
        from t_class_teacher ct left join t_class c on ct.classid=c.classid
        left join t_school s on c.schoolid=s.schoolid and c.`year`=s.`year`
        where s.schoolid=#{params.schoolid} and ct.teacherid=#{params.userid} and c.classstate=1 and c.classtype in (1,2,3)
        and c.classnum > 0
        order by c.classtype,c.classorder
    </select>
    <!--我的班级-学生数量分组数量-->
    <select id="listClassStuAndGroup" resultType="java.lang.Integer">
        select count(DISTINCT studentid) stunum from t_class_student where classid in (${classidsStr})
        UNION
        select count(distinct groupid) groupnum from t_group where
            (grouptype=1 or (grouptype=2 and createid=#{userid})) and
            classid in (${classidsStr})
    </select>
    
    <!--校本题库-试题数量-->
    <select id="getQuestionStatistic" resultType="java.util.Map">
        select count(DISTINCT q.questionid) totalnum,
        count(DISTINCT(if(DATE(FROM_UNIXTIME(q.createtime)) = CURDATE(),q.questionid,null))) todaynum
        from t_question q
        left join t_subject s on q.subjectid=s.subjectid and s.`level`=#{params.schoollevel}
        where q.belong and (q.belong in(1,2) or (q.belong in(3,10) and q.schoolid=#{params.schoolid}) or (q.belong=4 and q.createid=#{params.userid}))
        and q.qlevel &lt; 3
    </select>
    <!--校本题库-当前学科试题数量-->
    <select id="getQuestionStatisticSubejct" resultType="java.lang.Integer">
        select count(DISTINCT q.questionid) subjectnum
        from t_question q
        left join t_teacher_powersb tp on q.subjectid = tp.subjectid
        where tp.teacherid=#{params.userid}
        and q.belong and (q.belong in(1,2) or (q.belong in(3,10) and q.schoolid=#{params.schoolid}) or (q.belong=4 and q.createid=#{params.userid})) and q.qlevel &lt; 3
    </select>

    <!--教学统计-班级学生教师数量-->
    <select id="getTeachingSchoolClassNum" resultType="java.util.Map">
        SELECT COUNT(DISTINCT c.classid) AS classnum,count(DISTINCT s.studentid) studentnum,
        (SELECT COUNT(*) FROM t_user WHERE schoolid=c.schoolid AND usertype=1 AND userstate=1) AS teachernum
        FROM t_class c LEFT JOIN t_class_student s ON c.classid=s.classid
        WHERE c.schoolid=#{params.schoolid} AND c.year=#{params.year} and c.classtype=1 and c.classstate=1
    </select>
    <!--教学统计-作业-->
    <select id="listTeachingPapers" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        SELECT p.subjectid,count(distinct p.paperid)fabunum,
        count(distinct if(pg.starttime&lt;=#{params.timestamp},ps1.psid,null))stunum,
        count(distinct if(ps1.psstate in(1,2,4),ps1.psid,null)) commitnum,
        count(distinct if(ps1.psstate in(1,2,4) and ps1.pschecked=1,ps1.psid,null)) checknum
        FROM t_paper p
        left join t_paper_class pg on p.paperid=pg.paperid
        LEFT JOIN t_paper_student ps1 ON p.paperid=ps1.paperid and ps1.classid=pg.classid
        LEFT JOIN t_class c ON pg.classid=c.classid
        LEFT JOIN T_USER U ON p.createid=u.userid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.paperstate=22 AND p.schoolid=#{params.schoolid} AND pg.schoolid=#{params.schoolid}
        and p.year=#{params.year} AND u.userstate=1 and (d.disabled=0 or p.directorid is null)
        AND pg.starttime>=#{params.starttime} AND pg.stoptime <![CDATA[ <= ]]>#{params.stoptime}
        GROUP BY p.subjectid
    </select>
    <!--教学统计-预习-->
    <select id="listTeachingGuides" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        SELECT g.subjectid,count(distinct g.guideid)fabunum,
        count(distinct ggs.rgsid)stunum,
        count(distinct if(ggs.costtime!=0,ggs.rgsid,null)) commitnum
        FROM t_guide g left join t_guide_group_student ggs on ggs.guideid=g.guideid
        LEFT JOIN t_class c ON ggs.classid=c.classid
        LEFT JOIN T_USER U ON g.createid=u.userid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and g.schoolid=#{params.schoolid} AND ggs.schoolid=#{params.schoolid}
        and g.year=#{params.year} and u.userstate=1 AND g.createtime >= #{params.starttime} AND g.createtime <![CDATA[ <= ]]>#{params.stoptime}
        GROUP BY g.subjectid
    </select>
    <!--教学统计-背诵-->
    <select id="listTeachingRecites" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        SELECT r.subjectid,count(distinct r.reciteid)fabunum,
        count(distinct rs.rsid) stunum,
        count(distinct if(rs.rsstate!=0,rs.rsid,null)) commitnum
        FROM t_recite r left join t_recite_group rg on rg.reciteid=r.reciteid
        LEFT JOIN t_recite_student rs ON rg.reciteid=rs.reciteid and rg.classid=rs.classid
        LEFT JOIN t_class c ON rg.classid=c.classid
        LEFT JOIN T_USER U ON r.createid=u.userid
        WHERE r.recitestate=22 AND r.schoolid=#{params.schoolid} AND rg.schoolid=#{params.schoolid}
        and r.year=#{params.year} AND u.userstate=1 AND rg.starttime>=#{params.starttime} AND rg.starttime <![CDATA[ <= ]]>#{params.stoptime}
        GROUP BY r.subjectid
    </select>

    <!--常用应用-->
    <select id="listUserModules" resultType="java.util.Map">
        SELECT um.umid, um.moduleid,m.modulename,m.moduleurl,m.moduleico,m.minout,m.mabbrevname
        FROM t_user_module um LEFT JOIN t_module m ON um.moduleid=m.moduleid
        WHERE um.userid=#{params.userid} and m.display=1
        <if test="params.belongcode!=null and params.belongcode==1">and um.belongcode in (1,3)</if>
        <if test="params.belongcode!=null and params.belongcode==2">and um.belongcode in (2,3)</if>
        ORDER BY um.umorder
    </select>

    <!--考试-待出卷-->
    <select id="listExamPaperGiv" resultType="java.util.Map">
        select es.esid,es.subjectid,es.subjectname,es.sdate,es.begintime,es.endtime,es.teacherid,u2.username as teachername,
        es.esstate ,ep.epid,ep.ptype,ep.pnum,ep.pscore,ep.answered,ep.handleid,u.username as handlename,
        ep.scantronpdf,es.examid,e.examname,e.examtype,e.exammode,e.examstate,e.gradeid,e.hashb,e.schoolid
        from e_subject es left join e_paper ep on es.esid=ep.esid
        left join e_base e on es.examid=e.examid
        left join t_user u on ep.handleid=u.userid
        left join t_user u2 on es.teacherid=u2.userid
        where e.deleted=1 and es.teacherid=#{params.userid}
        group by es.esid
        order by abs(timediff(concat(es.sdate,' ',es.begintime),#{params.todayDatetime})) limit #{params.limitSize}
    </select>
    <!--考试-待批阅-->
    <select id="listExamPaperCheck" resultType="java.util.Map">
        select a.*,eb.examname,eb.gradeid,eb.examtype,eb.examstate,eb.hashb,eb.schoolid,es.sdate,es.begintime,es.endtime,es.subjectname
        ,truncate(a.pynum*100/a.tjnum,2)pgrate
        from(select p.esid,p.epid,group_concat(distinct concat(psq.qn,'_',psq.eptqid) order by psq.eptqid)qns
        ,group_concat(distinct pc.classid)classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctclass pc on p.epid = pc.epid
        left join e_paper_student_question psq on psq.classid=pc.classid and psq.epid=p.epid
        where pc.teacherid=#{params.userid} and psq.ctype in(3,13,14,15) group by p.epid
        union
        select p.esid,p.epid,group_concat(distinct concat(pq.qn,'_',pq.eptqid) order by pq.eptqid)qns
        ,'' classids
        ,count(case when psq.answered=1 then psq.epsqid else null end)tjnum
        ,count(case when psq.answered=1 and psq.checked=2 then psq.epsqid else null end)pynum
        from e_paper p left join e_paper_correctquestion pq on p.epid = pq.epid
        left join e_paper_student_question psq on psq.eptqid=pq.eptqid
        where pq.teacherid=#{params.userid} group by p.epid)a left join e_subject es on a.esid = es.esid
        left join e_base eb on es.examid = eb.examid
        where a.tjnum!=a.pynum and eb.deleted=1 limit #{params.limitSize}
    </select>
    <!--考试-待监考-->
    <select id="listExamInvigilate" resultType="java.util.Map">
        select es.*,count(eps.epsid)examnum
--         ,count(if(eps.sstate in(0,3), eps.epsid, null)) wjcnum
--         ,count(if(eps.sstate in(1,2), eps.epsid, null))jcnum
        ,truncate(count(if(eps.sstate in(1,2), eps.epsid, null))*100/count(eps.epsid),2) jcrate
--         ,count(if(eps.sstate=2, eps.epsid, null))tjnum
--         ,count(if(eps.sstate=3, eps.epsid, null))qknum
        from(select es.esid,es.sdate,es.begintime,es.endtime,es.esstate,es.subjectname,es.pstate
        ,eb.examname,eb.gradeid,eb.examtype,eb.examstate,eb.hashb,eb.schoolid,max(ei.majored)majored
        ,(if(max(ei.majored)=0, group_concat(distinct ei.classid), group_concat(distinct ec.classid)))classids
        ,concat('[',group_concat(distinct json_object('classid',ec.classid,'classname',ec.classname
        ,'schoolid',ec.schoolid,'schoolname',s.schoolname)),']')classnames
        from e_subject_invigilate ei left join e_subject es on ei.esid=es.esid
        left join e_base eb on eb.examid = es.examid
        left join e_class ec on eb.examid = ec.examid
        left join t_school s on ec.schoolid = s.schoolid
        where eb.deleted=1 and eb.examstate!=0 and eb.exammode=1 and eb.monitored=1
        and ei.teacherid=#{params.userid} and es.pstate!=2
        group by es.esid having classids is not null
        )es left join e_paper_student eps on es.esid = eps.esid and find_in_set(eps.classid,es.classids)
        group by es.esid
        order by abs(timediff(concat(es.sdate,' ',es.begintime),#{params.todayDatetime})) limit #{params.limitSize}
    </select>
    <!--考试-校级报告-获取最近一次生成报告的考试-->
    <select id="getExamReportSchoolBase" resultType="java.util.Map">
        select re.classnum,re.stunum,re.missnum,re.subjectnum,re.maxscore,re.avgscore,re.mixscore,
        e.examid,e.teashowrank,e.examstate,e.examname from e_base e left join e_rbase re on e.examid=re.examid
        where e.schoolid=#{schoolid} and e.deleted=1 order by examdate desc limit 1
    </select>
    <!--考试-校级报告-成绩概况-->
    <select id="listExamReportSchoolStatistic" resultType="java.util.Map">
        select rs.subjectid, rs.subjectname, score, maxscore, minscore, avgscore, stunum, missnum, yxrate, lhrate, jgrate, dfrate, bzc
        from e_rsubject rs left join e_subject es on rs.examid = es.examid and rs.subjectid=es.subjectid
        where rs.examid=#{examid}
        order by es.begintime,es.esid
    </select>

    <!--考试-考试报告-班级参与班级-->
    <select id="listExamReportClass" resultType="java.util.Map">
        select ec.classid,c.gradeid,c.classname from e_class ec left join t_class c on ec.classid=c.classid
        left join t_class_teacher ct on ec.classid = ct.classid
        where examid=#{examid} and ct.teacherid=#{userid} group by ec.classid order by c.classorder,c.classid
    </select>

    <!--考试-班级报告-考试信息-->
    <select id="getExamReportClassInfo" resultType="com.xhkjedu.sapi.vo.component.ExamInfoVo">
        select e.examid,e.examname,e.teashowrank,e.examstate,c.classid,c.classname
        from e_base e left join e_class c on e.examid=c.examid
        left join t_class_teacher ct on c.classid=ct.classid
        where e.deleted=1 and e.schoolid=#{params.schoolid} and ct.teacherid=#{params.userid}
        group by e.examid order by e.examdate desc,e.examid desc,c.ecid limit 1
    </select>
    <!--考试-班级报告-学科成绩对比-->
    <select id="listExamReportClassScore" resultType="java.util.Map">
        select er.allscore,er.classmaxscore,er.classminscore,er.classavgscore,er.schoolmaxscore,er.schoolavgscore
        ,er.stunum,er.missnum,er.yxrate,er.lhrate,er.jgrate,er.dfrate,er.bzc,er.qkbzc
        ,er.classid,(case when er.classid=0 then '全部班级' else ec.classname end)classname
        ,(case when er.subjectid='zf' then '总分' else (select s.subjectname from t_subject s where
        er.subjectid=s.subjectid) end)subjectname
        from e_rclass er left join e_class ec on er.examid = ec.examid and er.classid = ec.classid
        left join e_subject es on ec.examid = es.examid and er.subjectid=es.subjectid
        where er.examid=#{params.examid} and er.classid=#{params.classid}
        order by es.begintime,es.esid
    </select>
    <!--考试-单科报告-单科考试信息-->
    <select id="getExamReportSingleInfo" resultType="com.xhkjedu.sapi.vo.component.ExamInfoVo">
        select e.examid,e.examname,e.teashowrank,e.stushowrank,e.examstate,s.subjectid,c.classid from e_base e
        left join e_subject s on e.examid=s.examid
        left join t_teacher_powersb tp on tp.teacherid=#{params.userid} and tp.subjectid=s.subjectid
        left join e_class c on e.examid=c.examid
        left join t_class_teacher ct on c.classid=ct.classid and tp.teacherid=ct.teacherid
        where e.deleted=1 and e.schoolid=#{params.schoolid} and ct.teacherid=#{params.userid}
        group by e.examid order by e.examdate desc,e.examid desc,c.ecid limit 1
    </select>
    <!--考试-单科报告-单科概况-->
    <select id="getExamReportSingleBase" resultType="java.util.Map">
        select er.stunum,er.yxrate,er.jgrate,er.lhrate,er.dfrate
        <if test="params.showrank==0">
            ,0 classmaxscore,0 classavgscore,0 classminscore,0 classavgscorerank,0 schoolmaxscore,0 schoolavgscore
        </if>
        <if test="params.showrank==1">
            ,er.classmaxscore,er.classavgscore,er.classminscore,0 classavgscorerank,er.schoolmaxscore,er.schoolavgscore
        </if>
        <if test="params.showrank==2 or params.showrank==3">
            ,er.classmaxscore,er.classavgscore,er.classminscore,er.classavgscorerank,er.schoolmaxscore,er.schoolavgscore
        </if>
        from e_rclass er
        where er.examid=#{params.examid} and er.classid=#{params.classid} and er.subjectid=#{params.subjectid}
        limit 1
    </select>
    <!--考试-单科报告-成绩等级分布-->
    <select id="listExamReportSingleRank" resultType="java.util.Map">
        select err.ranktype,err.rankname,err.stunum,err.stujson,err.sturate,er.minscore,er.maxscore
        ,(case when er.linescore is null then '' else concat(er.linescore,'-',er.floatscore) end)rankrange
        from e_rclass_rank err left join e_rrank er on err.examid = er.examid and err.subjectid = er.subjectid
        and er.ranktype=1 and er.rankname=err.rankname
        where err.examid=#{params.examid} and err.classid=#{params.classid}
        and err.subjectid=#{params.subjectid} and err.ranktype=1
    </select>
    <!--考试-阅卷进度-考试-->
    <select id="getExamCheckProgressBase" resultType="com.xhkjedu.sapi.vo.component.ExamInfoVo">
        select e.examid,e.examname,(select sum(c.classnum) from e_class c where c.examid=e.examid)examnum from e_base e
        where e.deleted=1 and e.schoolid=#{params.schoolid}  and e.examstate>0
        <if test="params.usertype == 1">
            and e.createid=#{params.userid}
        </if>
        group by e.examid order by e.examdate desc limit 1
    </select>
    <!--考试-阅卷进度-试卷-->
    <select id="listExamCheckProgressPaper" resultType="com.xhkjedu.vo.exam.EPaperVo">
        select ep.epid,ep.pnum,es.subjectid,es.subjectname,ep.correcttype
        ,(case when ep.correcttype=1
        then (select concat('[',group_concat(json_object('classid',epc.classid,'teacherid',epc.teacherid)),']')
        from e_paper_correctclass epc where epc.epid=ep.epid)
        else (select concat('[',group_concat(json_object('eptqid',epc.eptqid,'teacherid',epc.teacherid)),']')
        from e_paper_correctquestion epc where epc.epid=ep.epid) end)correctJson
        from e_paper ep left join e_subject es on ep.esid = es.esid
        where ep.examid=#{examid}
    </select>
    <!--考试-阅卷进度-学生试题-->
    <select id="listExamCheckProgressStuQuestion" resultType="com.xhkjedu.sapi.vo.component.PaperStudentQuestionVo">
        select studentid,epid,eptqid,qn,answered,checked,checkid,ctype,qorder
        from e_paper_student_question where epid in(${epids})
    </select>

</mapper>
