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

    <!--获取区平台信息-->
    <select id="getCloudInfo" resultType="com.xhkjedu.sapi.vo.region.CloudVo">
        select * from t_cloud order by cloudid limit 1
    </select>

    <!--获取资源、试题总量-->
    <select id="getResourceQuestionNum" resultType="java.util.Map">
        select (select count(distinct r.resourceid)
        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
        where d.disabled=0 and r.resourcestate=1 and (r.level=1 or r.level=4) and r.belong!=4)resourcenum
        ,(select count(distinct q.questionid)
        from t_question q left join t_question_director qd on q.questionid=qd.questionid
        left join t_director d on qd.directorid=d.directorid
        where d.disabled=0 and q.qstate=1 and q.belong!=4 and q.qlevel!=3)questionnum
    </select>
    <!--获取资源类型统计-->
    <select id="listResourceTypeNum" resultType="com.xhkjedu.sapi.vo.region.ResourceTypeVo">
        select s.level,r.resourcetype rtype,count(distinct r.resourceid)rnum
        ,count(distinct if(r.belong in (1,2),r.resourceid,null))ptnum
        ,count(distinct if(r.belong=3,r.resourceid,null))xbnum
        from t_resource r left join t_subject s on r.subjectid=s.subjectid
        left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1 and (r.level=1 or r.level=4) and r.belong!=4
        group by s.level,r.resourcetype order by s.level
    </select>
    <!--获取试题科目统计-->
    <select id="listQuestionSubjectNum" resultType="com.xhkjedu.sapi.vo.region.RSubjectVo">
        select s.level,s.subjectname,s.subjectid,count(distinct q.questionid) as snum
        ,count(distinct if(q.belong in(1,2),q.questionid,null))ptnum
        ,count(distinct if(q.belong=3,q.questionid,null))xbnum
        from t_question q left join t_subject s on q.subjectid=s.subjectid
        left join t_question_director qd on q.questionid=qd.questionid
        left join t_director d on qd.directorid=d.directorid
        where d.disabled=0 and q.qstate=1 and q.belong!=4 and q.qlevel!=3
        group by q.subjectid order by s.level
    </select>
    <!--获取学校列表-->
    <select id="listSchool" resultType="java.util.Map">
        select s.schoollevel,s.schoolname,s.shortname,s.coordinate,count(c.classid)bjnum
        ,(select cloudmap from t_cloud limit 1)cloudmap
        ,(select count(*) from t_user u where u.schoolid=c.schoolid and u.usertype=1 and u.userstate=1)jsnum
        ,sum((select count(distinct cs.studentid) from t_class_student cs where cs.classid=c.classid))xsnum
        from t_school s left join t_class c on c.schoolid=s.schoolid and c.classstate=1 and c.classtype=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        where s.schoolstate=1
        group by s.schoolid order by s.schoolid
    </select>
    <!--获取学段班级、教师、学生总量-->
    <select id="listClassTeacherStudentNum" resultType="java.util.Map">
        select s.schoollevel,count(c.classid)bjnum
        ,(select count(*) from t_user u where u.schoolid=c.schoolid and u.usertype=1 and u.userstate=1)jsnum
        ,sum((select count(distinct cs.studentid) from t_class_student cs where cs.classid=c.classid))xsnum
        from t_school s right join t_class c on s.schoolid=c.schoolid
        where s.schoolstate=1 and c.classstate=1 and c.classtype=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        group by s.schoollevel
    </select>

    <!--教学统计-->
    <resultMap id="workMap" type="com.xhkjedu.sapi.vo.region.WorkVo">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <result property="shortname" column="shortname"/>
        <collection property="details" ofType="com.xhkjedu.sapi.vo.region.WorkDetailVo">
            <result property="wid" column="wid"/>
            <result property="rid" column="rid"/>
            <result property="stunum" column="stunum"/>
            <result property="commitnum" column="commitnum"/>
            <result property="checknum" column="checknum"/>
        </collection>
    </resultMap>
    <!--作业提交批阅-->
    <select id="listPaperCommitMonth" resultMap="workMap">
        select s.schoolid,s.schoolname,s.shortname, pg.paperid as wid,
        count(distinct ps.psid)stunum,
        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
        right join t_school s on p.schoolid=s.schoolid and p.paperstate=22
        left join t_paper_class pg on p.paperid = pg.paperid and pg.schoolid=s.schoolid
        and pg.starttime between #{param.begintime} and #{param.endtime}
        left join t_paper_student ps on ps.paperid=pg.paperid and ps.classid=pg.classid
        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.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        group by s.schoolid,pg.paperid order by s.schoolid
    </select>
    <!--预习-->
    <select id="listGuideCommitMonth" resultMap="workMap">
        select s.schoolid,s.schoolname,s.shortname,g.guideid as rid,
        count(distinct gs.rgsid) stunum,
        count(distinct if(gs.readtime>0,gs.rgsid,null)) commitnum
        from t_guide g right join t_school s on g.schoolid=s.schoolid
        and g.createtime between #{param.begintime} and #{param.endtime}
        left join t_guide_group_student gs on gs.guideid=g.guideid 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.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        group by s.schoolid,g.guideid order by g.schoolid
    </select>

    <!--背诵-->
    <select id="listReciteCommitMonth" resultMap="workMap">
        select s.schoolid,s.schoolname,s.shortname,rg.reciteid as wid,
        count(distinct rs.rsid) stunum,
        count(distinct if(rs.rsstate!=0,rs.rsid,null)) commitnum
        from t_recite r right join t_school s on r.schoolid=s.schoolid
        left join t_recite_group rg on r.reciteid=rg.reciteid and rg.schoolid=s.schoolid
        and rg.starttime between #{param.begintime} and #{param.endtime}
        left join t_recite_student rs on rs.reciteid=rg.reciteid and rs.classid=rg.classid
        left join t_class c on c.classid=rg.classid
        where s.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        group by s.schoolid,rg.reciteid order by s.schoolid
    </select>
    <!--课堂出勤率-->
    <select id="listRoomCommitMonth" resultType="java.util.Map">
        select s.schoolid,s.schoolname,s.shortname,ifnull(SUM(c.classnum),0) stunum,ifnull(SUM(c.realstunum),0) commitnum
        from t_classroom c left join t_class c2 on c.classid=c2.classid
        right join t_school s on c.schoolid=s.schoolid and c.roomstate=2
        and c.createtime between #{param.begintime} and #{param.endtime}
        left join t_director d on c.directorid=d.directorid
        where d.disabled=0 and s.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c2.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c2.year=s.year
        </if>
        group by s.schoolid order by s.schoolid
    </select>

    <!--布置作业统计_布置作业次数-->
    <select id="listPapernumMonth" resultType="com.xhkjedu.sapi.vo.region.WorkVo">
        select s.schoolid,s.schoolname,s.shortname,count(distinct pg.paperid) num
        from t_paper p
        right join t_school s on p.schoolid=s.schoolid and p.paperstate=22
        left join t_paper_class pg on p.paperid=pg.paperid and s.schoolid=pg.schoolid
        and pg.starttime between #{param.begintime} and #{param.endtime}
        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.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c.year=s.year
        </if>
        group by s.schoolid order by s.schoolid

    </select>

    <!--课堂使用情况统计——使用次数-->
    <select id="listRoomNumMonth" resultType="com.xhkjedu.sapi.vo.region.WorkVo">
        select s.schoolid,s.schoolname,s.shortname,count(c.roomid) num from t_classroom c
        right join t_school s on c.schoolid=s.schoolid
        and c.createtime between #{param.begintime} and #{param.endtime}
        left join t_class c2 on c.classid=c2.classid
        left join t_director d on c.directorid=d.directorid
        where d.disabled=0 and s.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c2.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c2.year=s.year
        </if>
        group by s.schoolid order by s.schoolid
    </select>
    <resultMap id="roomAsk" type="com.xhkjedu.sapi.vo.region.WorkVo">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <result property="shortname" column="shortname"/>
        <collection property="asks" ofType="com.xhkjedu.sapi.vo.region.WorkTypeVo">
            <result property="asktype" column="asktype"/>
            <result property="asknum" column="asknum"/>
        </collection>
    </resultMap>

    <!--课堂使用情况统计——课堂互动-->
    <select id="listRoomAskNumMonth2" resultType="java.util.Map">
        select s.schoolname,s.shortname,count(distinct c.roomid)num
        ,count(distinct if(ca.asktype between 30 and 33,ca.asktid,null))twnum
        ,count(distinct if(ca.asktype=40,ca.asktid,null))tcnum
        ,count(distinct if(ca.asktype=21,ca.asktid,null))dmnum
        ,count(distinct if(ca.asktype=20,ca.asktid,null))qdnum
        from t_classroom c right join t_school s on c.schoolid=s.schoolid
        and c.createtime between #{param.begintime} and #{param.endtime}
        left join t_classroom_askteacher ca on ca.roomid=c.roomid
        left join t_class c2 on c.classid=c2.classid
        left join t_director d on c.directorid=d.directorid
        where d.disabled=0 and s.schoolstate=1
        <if test="param.year!=null and param.year!=0">
            and c2.year=#{param.year}
        </if>
        <if test="param.year==null or param.year==0">
            and c2.year=s.year
        </if>
        group by s.schoolid order by s.schoolid
    </select>
</mapper>
