<?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_group pg left join t_paper p on pg.paperid=p.paperid
        LEFT JOIN t_school s on p.schoolid=s.schoolid
        left join t_class c on c.classid=pg.classid
        where s.schoollevel=#{param.level} and c.year=s.year and pg.starttime between #{param.begintime} and #{param.endtime}
    </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
        left join t_class c on c.classid=gg.classid
        where s.schoollevel=#{param.level} and c.year=s.year and g.createtime between #{param.begintime} and #{param.endtime}
    </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
        left join t_class c on c.classid=rg.classid
        where s.schoollevel=#{param.level} and c.year=s.year and rg.starttime between #{param.begintime} and #{param.endtime}
    </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=qe.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 c.year=s.year and qe.createtime between #{param.begintime} and #{param.endtime}
    </select>

    <!--作业各类率统计-->
    <resultMap id="rpaperMap" type="com.xhkjedu.sapi.vo.region.RSchoolVo">
        <result column="schoolid" property="schoolid"></result>
        <result column="schoolname" property="schoolname"></result>
        <result column="shortname" property="shortname"></result>
        <collection property="paperlist" ofType="com.xhkjedu.sapi.vo.region.RPaperVo">
            <result column="paperid" property="paperid"></result>
            <result column="resourceid" property="resourceid"></result>
            <result column="stunum" property="stunum"></result>
            <result column="commitnum" property="commitnum"></result>
            <result column="commitasnum" property="commitasnum"></result>
            <result column="checknum" property="checknum"></result>
            <result column="paperscore" property="paperscore"></result>
            <result column="stuscore" property="stuscore"></result>
        </collection>

    </resultMap>
    <!--作业各类率统计——作业-->
    <select id="listRegionSchoolRatePaper" resultMap="rpaperMap">
        select s.schoolid,s.schoolname,s.shortname,pg.paperid,
        count(ps.psid)stunum,sum(ps.userscore)stuscore,sum(ps.paperscore)paperscore,
        count(case when ps.psstate!=0 then ps.psid else null end) commitnum,
        count(case when ps.psstate=1 then ps.psid else null end) commitasnum,
        count(case when ps.psstate!=0 and ps.pschecked=1 then ps.psid else null end) checknum
        from t_paper p left join t_paper_group 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
        left join t_class c on c.classid=pg.classid
        where s.schoollevel=#{param.level} and c.year=s.year and pg.starttime between #{param.begintime} and #{param.endtime}
        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(case when rs.rsstate!=0 then rs.rsid else null end)commitnum,
        count(case when rs.rsstate=1 then rs.rsid else null end)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
        left join t_class c on c.classid=rg.classid
        where s.schoollevel=#{param.level} and c.year=s.year  and rg.starttime between #{param.begintime} and #{param.endtime}
        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(case when ggs.readtime>0 then ggs.rgsid else null end)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
        left join t_class c on c.classid=gg.classid
        where s.schoollevel=#{param.level} and c.year=s.year and g.createtime between #{param.begintime} and #{param.endtime}
        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) costtime
         from t_paper_student ps
        left join t_school s on ps.schoolid=s.schoolid
        left join t_class c on c.classid=ps.classid
        where s.schoollevel=#{param.level} and c.year=s.year and ps.stoptime between #{param.begintime} and #{param.endtime}
        group by ps.schoolid order by ps.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) 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
        left join t_class c on c.classid=gs.classid
        where s.schoollevel=#{param.level} and c.year=s.year and gs.readtime between #{param.begintime} and #{param.endtime} and gs.readtime>0
        group by s.schoolid order by s.schoolid
    </select>

</mapper>
