<?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.region.RPaperAnalyzeMapper">

    <!--作业数量统计——作业数量-->
    <select id="getRegionPaperNumPaper" resultType="java.lang.Integer">
      select count(DISTINCT pg.paperid) from t_paper_class pg left join t_paper p on pg.paperid=p.paperid
        LEFT JOIN t_school s on p.schoolid=s.schoolid and pg.schoolid=s.schoolid
        left join t_class c on c.classid=pg.classid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and s.schoollevel=#{param.level} and s.schoolstate=1 and p.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and pg.starttime between #{param.begintime} and #{param.endtime}
        </if>
    </select>
    <!--作业数量统计——预习数量-->
    <select id="getRegionPaperNumPreview" resultType="java.lang.Integer">
        select count(DISTINCT g.guideid) from t_guide_group gg left join t_guide g on gg.guideid=g.guideid
        LEFT JOIN t_school s on g.schoolid=s.schoolid and gg.schoolid=s.schoolid
        left join t_class c on c.classid=gg.classid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and s.schoollevel=#{param.level} and s.schoolstate=1 and g.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and g.createtime between #{param.begintime} and #{param.endtime}
        </if>
    </select>
    <!--作业数量统计——背诵数量-->
    <select id="getRegionPaperNumRecite" resultType="java.lang.Integer">
       select count(DISTINCT rg.reciteid) from t_recite_group rg left join t_recite r on rg.reciteid=r.reciteid
        LEFT JOIN t_school s on r.schoolid=s.schoolid and rg.schoolid=s.schoolid
        left join t_class c on c.classid=rg.classid
        where s.schoollevel=#{param.level} and s.schoolstate=1 and r.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and rg.starttime between #{param.begintime} and #{param.endtime}
        </if>
    </select>
    <!--作业数量统计——错题视频数量-->
    <select id="getRegionPaperNumQueexp" resultType="java.lang.Integer">
        select count(distinct qe.qcpid) from t_question_explain qe left join t_question_class qc on qe.questionid=qc.questionid
        left JOIN t_school s on qc.schoolid=s.schoolid
        left join t_class c on c.classid=qc.classid
        where s.schoollevel=#{param.level} and s.schoolstate=1 and qe.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and qe.createtime between #{param.begintime} and #{param.endtime}
        </if>
    </select>

    <!--作业各类率统计-->
    <resultMap id="rpaperMap" type="com.xhkjedu.sapi.vo.region.RSchoolVo">
        <result column="schoolid" property="schoolid"/>
        <result column="schoolname" property="schoolname"/>
        <result column="shortname" property="shortname"/>
        <collection property="paperlist" ofType="com.xhkjedu.sapi.vo.region.RPaperVo">
            <result column="paperid" property="paperid"/>
            <result column="resourceid" property="resourceid"/>
            <result column="stunum" property="stunum"/>
            <result column="commitnum" property="commitnum"/>
            <result column="commitasnum" property="commitasnum"/>
            <result column="checknum" property="checknum"/>
            <result column="paperscore" property="paperscore"/>
            <result column="stuscore" property="stuscore"/>
        </collection>
    </resultMap>
    <!--作业各类率统计——作业-->
    <select id="listRegionSchoolRatePaper" resultMap="rpaperMap">
        select s.schoolid,s.schoolname,s.shortname,pg.paperid,
        count(ps.psid)stunum,sum(ps.userscore) as stuscore,sum(ps.paperscore) as paperscore,
        count(distinct if(ps.psstate in(1,2),ps.psid,null)) commitnum,
        count(distinct if(ps.psstate=1,ps.psid,null)) commitasnum,
        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=p.paperid and ps.classid=pg.classid
        left join t_school s on p.schoolid=s.schoolid and pg.schoolid=s.schoolid
        left join t_class c on c.classid=pg.classid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and s.schoollevel=#{param.level} and s.schoolstate=1 and p.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and pg.starttime between #{param.begintime} and #{param.endtime}
        </if>
        group by p.schoolid,p.paperid order by p.schoolid
    </select>
    <!--作业各类率统计——背诵-->
    <select id="listRegionSchoolRateRecite" resultMap="rpaperMap">
        select s.schoolid,s.schoolname,s.shortname,r.reciteid as paperid,
        count(rs.rsid)stunum,sum(rs.userscore)stuscore,
        count(distinct if(rs.rsstate!=0,rs.rsid,null))commitnum,
        count(distinct if(rs.rsstate=1,rs.rsid,null))commitasnum
        from t_recite r left join t_recite_group rg on r.reciteid=rg.reciteid
        left join t_recite_student rs on rs.reciteid=r.reciteid and rs.classid=rg.classid
        left join t_school s on r.schoolid=s.schoolid and rg.schoolid=s.schoolid
        left join t_class c on c.classid=rg.classid
        where s.schoollevel=#{param.level} and s.schoolstate=1 and r.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and rg.starttime between #{param.begintime} and #{param.endtime}
        </if>
        group by r.schoolid,r.reciteid order by r.schoolid
    </select>
    <!--作业各类率统计——预习-->
    <select id="listRegionSchoolRatePreview" resultMap="rpaperMap">
        select s.schoolid,s.schoolname,s.shortname, g.guideid,
        count(ggs.rgsid)stunum,
        count(distinct if(ggs.readtime>0,ggs.rgsid,null))commitnum
        from t_guide g
        left join t_guide_group gg on gg.guideid=g.guideid
        left join t_guide_group_student ggs on ggs.guideid=g.guideid and ggs.classid=gg.classid
        left join t_school s on s.schoolid=g.schoolid and s.schoolid=gg.schoolid
        left join t_class c on c.classid=gg.classid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and s.schoollevel=#{param.level} and s.schoolstate=1 and g.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and g.createtime between #{param.begintime} and #{param.endtime}
        </if>
        group by s.schoolid, g.guideid order by s.schoolid
    </select>

    <!--人均用时——作业-->
    <select id="listRegionSchoolUsetimePaper" resultType="java.util.Map">
        select s.schoolid,s.schoolname,s.shortname,
        count(ps.psid) as stunum,
        ifnull(SUM(ps.costtime),0) as costtime
        from t_paper p left join t_paper_class pg on p.paperid=pg.paperid
        left join t_school s on p.schoolid=s.schoolid and pg.schoolid=s.schoolid
        left join t_paper_student ps on pg.paperid=ps.paperid and pg.classid=ps.classid and ps.psstate in(1,2)
        left join t_class c on c.classid=pg.classid
        left join t_director d on p.directorid=d.directorid
        where (d.disabled=0 or p.directorid is null) and s.schoollevel=#{param.level} and s.schoolstate=1 and p.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and pg.starttime between #{param.begintime} and #{param.endtime}
        </if>
        group by s.schoolid order by s.schoolid
    </select>
    <!--人均用时——预习-->
    <select id="listRegionSchoolUsetimePreview" resultType="java.util.Map">
        select s.schoolid,s.schoolname,s.shortname,
        count(distinct gs.rgsid) as stunum,
        sum(gs.costtime) as costtime
        from t_guide_group_student gs
        left join t_guide g on gs.guideid=g.guideid
        left join t_school s on g.schoolid=s.schoolid and gs.schoolid=s.schoolid
        left join t_class c on c.classid=gs.classid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and s.schoollevel=#{param.level} and s.schoolstate=1 and gs.readtime>0 and g.year=#{param.year}
        <if test="param.begintime!=null and param.begintime!=0">
            and g.createtime between #{param.begintime} and #{param.endtime}
        </if>
        group by s.schoolid order by s.schoolid
    </select>

</mapper>
