<?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.analyze.SchoolResourceMapper">
    <!-- 校本资源总数量 -->
    <select id="getSchoolResourceNum" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceNumVo">
        SELECT COUNT(r.resourceid) resourcenum,SUM(resourcesize) as resourcesize,SUM(downcount) as downnum
        ,sum(r2.collectnum) as collectnum
        from(select distinct r.resourceid,count(distinct rc.rcid)collectnum
        FROM t_resource r left join t_resource_collect rc on rc.resourceid=r.resourceid
        left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong in(1,2) or (r.belong=3 and r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid})r2
        left join t_resource r on r2.resourceid=r.resourceid
    </select>

    <!--资源分布-分资源类统计-->
    <select id="listSchoolResourceTypeNum" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceTypeNumVo">
        SELECT r.resourcetype,COUNT(distinct r.resourceid)AS resourcenum
         FROM t_resource r left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong in(1,2) or (r.belong=3 and r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid}
         GROUP BY r.resourcetype
    </select>

    <!--资源分布-资源格式统计-->
    <select id="listSchoolResourceClassNum" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceClassNumVo">
        SELECT r.resourceclass,COUNT(distinct r.resourceid)AS resourcenum
         FROM t_resource r left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong in(1,2) or (r.belong=3 and r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid}
         GROUP BY r.resourceclass
    </select>

    <resultMap id="sResourceUpload" type="com.xhkjedu.sapi.vo.analyze.SchoolResourceUploadNumVo">
        <result property="uploadtime" column="uploadtime"/>
        <collection property="rtypelist" ofType="com.xhkjedu.sapi.vo.analyze.SchoolResourceTypeNumVo">
            <result property="resourcetype" column="resourcetype"/>
            <result property="resourcenum" column="resourcenum"/>
        </collection>
    </resultMap>
    <!--资源上传量-->
    <select id="listSchoolResourceUploadNum" resultMap="sResourceUpload">
        SELECT FROM_UNIXTIME(r.createtime,'%Y-%m') AS uploadtime,r.resourcetype,COUNT(distinct r.resourceid) AS resourcenum
        FROM t_resource r left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong in(1,2) or (r.belong=3 and r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid}
        AND r.createtime >= #{starttime} AND r.createtime <![CDATA[ <= ]]>#{endtime}
        GROUP BY uploadtime,r.resourcetype
    </select>

    <resultMap id="sResourceRank" type="com.xhkjedu.sapi.vo.analyze.SchoolResourceRankVo">
        <result property="userid" column="userid"/>
        <result property="username" column="username"/>
        <collection property="rtypelist" ofType="com.xhkjedu.sapi.vo.analyze.SchoolResourceTypeNumVo">
            <result property="resourcetype" column="resourcetype"/>
            <result property="resourcenum" column="resourcenum"/>
        </collection>
    </resultMap>
    <!--资源排行-->
    <select id="listSchoolResourceRanks" resultMap="sResourceRank">
        SELECT r.createid as userid,u.username,r.resourcetype,COUNT(DISTINCT r.resourceid) AS resourcenum
        FROM t_resource r LEFT JOIN t_user u ON r.createid=u.userid
        left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        <if test="gradeid>0">
            left join t_class_teacher ct on r.createid=ct.teacherid
            LEFT JOIN t_class c ON ct.classid=c.classid
        </if>
        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.belong IN(1,2) OR (r.belong=3 AND r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid} and c.year=ts.year
        <if test="subjectid!=null and subjectid!='0'.toString() and subjectid!=''">
            AND r.subjectid=#{subjectid}
        </if>
        <if test="gradeid>0">
            AND c.gradeid=#{gradeid} and c.year=ts.year
        </if>
        AND r.createtime >= #{starttime} AND r.createtime <![CDATA[ <= ]]>#{endtime}
        GROUP BY r.createid,r.resourcetype

    </select>

    <!-- 结构性试题统计 学科试题数量-->
    <select id="listSchoolResourceQuestion" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceQuestionVo">
         SELECT q.subjectid,s.subjectname,COUNT(DISTINCT q.questionid) AS questionnum
          FROM t_question q LEFT JOIN t_subject s ON q.subjectid=s.subjectid
          left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{params.schoolid})) and ts.schoolid=#{params.schoolid}
        <if test="params.type!=null and params.type!=0">
          AND q.createtime >= #{params.starttime} AND q.createtime <![CDATA[ <= ]]>#{params.stoptime}
        </if>
          GROUP BY q.subjectid ORDER BY s.subjectorder
    </select>
    <!-- 结构性试题统计 年级试题数量-->
    <select id="listSchoolResourceQuestionGrade" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceQuestionVo">
      SELECT c.gradeid,COUNT(DISTINCT q.questionid) AS questionnum
      FROM t_question q left join t_class_teacher ct on q.createid=ct.teacherid
      LEFT JOIN t_class c ON ct.classid=c.classid
      left join t_subject s on q.subjectid=s.subjectid
      left join t_school ts on s.level=ts.schoollevel
      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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{params.schoolid})) and ts.schoolid=#{params.schoolid} and c.year=ts.year
      <if test="params.type!=null and params.type!=0">
          AND q.createtime >= #{params.starttime} AND q.createtime <![CDATA[ <= ]]>#{params.stoptime}
      </if>
      GROUP BY c.gradeid ORDER BY c.gradeid
    </select>

    <!--结构性试题 教师上传排行榜-->
    <select id="listSchoolResourceQuestionRank" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceQuestionRankVo">
         SELECT q.createid as userid,u.username,COUNT(DISTINCT q.questionid) AS questionnum FROM t_question q
        LEFT JOIN t_user u ON q.createid=u.userid
        <if test="params.gradeid!=null and params.gradeid!=0">
            LEFT JOIN t_class_teacher ct ON q.createid=ct.teacherid
            LEFT JOIN t_class c ON ct.classid=c.classid
        </if>
        left join t_subject s on q.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{params.schoolid})) and ts.schoolid=#{params.schoolid}
        <if test="params.type!=null and params.type!=0">
            AND q.createtime >= #{params.starttime} AND q.createtime <![CDATA[ <= ]]>#{params.stoptime}
        </if>
        <if test="params.subjectid!=null and params.subjectid!='0'.toString() and params.subjectid!=''">
            and q.subjectid=#{params.subjectid}
        </if>
        <if test="params.username!=null and params.username!=''">
            and u.username like '%${params.username}%'
        </if>
        <if test="params.gradeid!=null and params.gradeid!=0">
            and c.gradeid=#{params.gradeid} and c.year=ts.year
        </if>
        GROUP BY q.createid ORDER BY questionnum desc
    </select>
    <!--结构性试题数量统计-->
    <select id="getSchoolQuestionNum" resultType="com.xhkjedu.sapi.vo.analyze.SchoolQuestionVo">
        SELECT COUNT(DISTINCT q.questionid) AS questionnum
        ,(SELECT COUNT(*) FROM t_paper p WHERE p.papertype in(1,3) and p.paperstate=22 AND p.schoolid=#{params.schoolid})papernum
        FROM t_question q
        left join t_subject s on q.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{params.schoolid})) and ts.schoolid=#{params.schoolid}
    </select>
    <resultMap id="sQuestionUpload" type="com.xhkjedu.sapi.vo.analyze.SchoolQuestionUploadNumVo">
        <result property="uploadtime" column="uploadtime"/>
        <collection property="questions" ofType="com.xhkjedu.sapi.vo.analyze.SchoolQuestionNumVo">
            <result property="gradeid" column="gradeid"/>
            <result property="subjectname" column="subjectname"/>
            <result property="questionnum" column="questionnum"/>
        </collection>
    </resultMap>
    <!--获取按年级试题上传变化列表-->
    <select id="listSchoolGQuestionUpload" resultMap="sQuestionUpload">
        SELECT FROM_UNIXTIME(q.createtime,'%Y-%m') AS uploadtime,COUNT(distinct q.questionid) AS questionnum,c.gradeid
        FROM t_question q LEFT JOIN t_class_teacher ct ON q.createid=ct.teacherid
        LEFT JOIN t_class c ON ct.classid=c.classid
        left join t_subject s on q.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{schoolid})) and ts.schoolid=#{schoolid} and c.year=ts.year
        AND q.createtime >= #{starttime} AND q.createtime <![CDATA[ <= ]]>#{endtime}
        GROUP BY uploadtime,c.gradeid
    </select>
    <!--获取按科目试题上传变化列表-->
    <select id="listSchoolSQuestionUpload" resultMap="sQuestionUpload">
        SELECT FROM_UNIXTIME(q.createtime,'%Y-%m') AS uploadtime,COUNT(distinct q.questionid) AS questionnum
        ,s.subjectname
        FROM t_question q left join t_subject s on q.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and q.qstate=1 AND (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{schoolid})) and ts.schoolid=#{schoolid}
        AND q.createtime >= #{starttime} AND q.createtime <![CDATA[ <= ]]>#{endtime}
        GROUP BY uploadtime,q.subjectid
    </select>
    <!--本月新增情况-->
    <select id="listMonthAdd" resultType="com.xhkjedu.sapi.vo.analyze.SchoolAddNumVo">
        select 'notice' type,count(n.noticeid)num from t_notice n where n.schoolid=#{schoolid}
        and DATE_FORMAT(FROM_UNIXTIME(n.createtime),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')
        union select 'resource' type,count(distinct r.resourceid)num
        from t_resource r left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong IN(1,2) OR (r.belong=3 AND r.schoolid=#{schoolid})) and r.resourcestate=1 and ts.schoolid=#{schoolid}
        and DATE_FORMAT(FROM_UNIXTIME(r.createtime),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')
        union select 'research' type,count(r.researchid)num from t_research r where r.schoolid=#{schoolid}
        and DATE_FORMAT(FROM_UNIXTIME(r.createtime),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')
        union select 'question' type,count(distinct q.questionid)num
        from t_question q left join t_subject s on q.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.qlevel!=3 and (q.belong IN(1,2) OR (q.belong=3 AND q.schoolid=#{schoolid})) and q.qstate=1 and ts.schoolid=#{schoolid}
        and DATE_FORMAT(FROM_UNIXTIME(q.createtime),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')
    </select>

    <!--按年级资源统计列表-->
    <select id="listSchoolResourceGrade" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceGradeNumVo">
        select c.gradeid,count(distinct r.resourceid)as resourcenum
        from t_resource r left join t_class_teacher ct on r.createid=ct.teacherid
        left join t_class c on ct.classid=c.classid
        left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        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.belong IN(1,2) OR (r.belong=3 AND r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid} and c.year=ts.year
        group by c.gradeid
    </select>
    <!--按科目资源统计列表-->
    <select id="listSchoolResourceSubject" resultType="com.xhkjedu.sapi.vo.analyze.SchoolResourceSubjectNumVo">
        select s.subjectname,count(distinct r.resourceid)as resourcenum
        from t_resource r left join t_resource_director rd on r.resourceid=rd.resourceid
        left join t_subject s on r.subjectid = s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        left join t_director d on rd.directorid=d.directorid
        where d.disabled=0 and r.resourcestate=1 and (r.belong IN(1,2) OR (r.belong=3 AND r.schoolid=#{schoolid})) and ts.schoolid=#{schoolid}
        group by r.subjectid
    </select>
    <!--资源上传详情排行-->
    <select id="listSchoolResourceRank2" resultType="com.xhkjedu.sapi.vo.analyze.SchoolUserResourceNumVo">
        SELECT count(distinct r.resourceid)totalnum,u.username
        ,count(distinct if(r.resourcetype=1,r.resourceid,null))xtnum
        ,count(distinct if(r.resourcetype=2,r.resourceid,null))janum
        ,count(distinct if(r.resourcetype=3,r.resourceid,null))kjnum
        ,count(distinct if(r.resourcetype=4,r.resourceid,null))jcnum
        ,count(distinct if(r.resourcetype=5,r.resourceid,null))spnum
        ,count(distinct if(r.resourcetype=6,r.resourceid,null))xanum
        ,count(distinct if(r.resourcetype=7,r.resourceid,null))ywbnum
        ,count(distinct if(r.resourcetype=10,r.resourceid,null))qtnum
        FROM t_resource r LEFT JOIN t_user u ON r.createid=u.userid
        left join t_subject s on r.subjectid=s.subjectid
        left join t_school ts on s.level=ts.schoollevel
        <if test="params.gradeid!=null and params.gradeid!=0">
            LEFT JOIN t_class_teacher ct ON r.createid=ct.teacherid
            LEFT JOIN t_class c ON ct.classid=c.classid
        </if>
        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.belong IN(1,2) OR (r.belong=3 AND r.schoolid=#{params.schoolid})) and ts.schoolid=#{params.schoolid}
        <if test="params.type!=null and params.type!=0">
            AND r.createtime >= #{params.starttime} AND r.createtime <![CDATA[ <= ]]>#{params.stoptime}
        </if>
        <if test="params.subjectid!=null and params.subjectid!='0'.toString() and params.subjectid!=''">
            and r.subjectid=#{params.subjectid}
        </if>
        <if test="params.username!=null and params.username!=''">
            and u.username like '%${params.username}%'
        </if>
        <if test="params.gradeid!=null and params.gradeid!=0">
            and c.gradeid=#{params.gradeid} and c.year=ts.year
        </if>
        GROUP BY r.createid ORDER BY totalnum DESC
    </select>
</mapper>
