<?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.SchoolTaskMapper">
    <!--常规作业统计-->

    <resultMap id="taskwork" type="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        <result property="subjectid" column="subjectid"/>
        <result property="subjectname" column="subjectname"/>
        <result property="fabunum" column="fabunum"/>
        <result property="stunum" column="stunum"/>
        <result property="commitnum" column="commitnum"/>
        <result property="checknum" column="checknum"/>
        <!--<collection property="worklist" ofType="com.xhkjedu.vo.analyze.work.SchoolWorkDetailVo">
            <result property="paperid" column="paperid"/>
        </collection>-->
    </resultMap>
    <select id="listSchoolTaskWork" resultMap="taskwork">
      SELECT p.subjectid,s.subjectname,count(distinct p.paperid)fabunum,
        count(distinct if(pg.starttime&lt;=#{sparams.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_subject s ON p.subjectid=s.subjectid
        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=#{sparams.schoolid} AND pg.schoolid=#{sparams.schoolid}
        AND c.gradeid=#{sparams.gradeid} and p.year=#{sparams.year} AND u.userstate=1 and (d.disabled=0 or p.directorid is null)
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND pg.starttime>=#{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND pg.stoptime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
        GROUP BY p.subjectid ORDER BY s.subjectorder
    </select>

    <!--常规作业 详情-->
    <select id="listSchoolTaskWorkDetail" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        select p.createid userid,count(distinct p.paperid)fabunum,
        count(distinct if(pg.starttime&lt;=#{sparams.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 ps1.paperid=pg.paperid and ps1.classid=pg.classid
        left join t_class c on c.classid=pg.classid
        left join t_director d on p.directorid=d.directorid
        where p.deleted=0 and p.schoolid=#{sparams.schoolid} and p.paperstate=22 and pg.schoolid=#{sparams.schoolid}
        and c.gradeid=#{sparams.gradeid} and p.year=#{sparams.year} and (d.disabled=0 or p.directorid is null)
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND pg.starttime>=#{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND pg.starttime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
            and p.subjectid=#{sparams.subjectid}
        </if>
        group by p.createid
    </select>

    <!--学生预习-->
    <select id="listSchoolTaskGuide" resultMap="taskwork">
          SELECT s.subjectid,s.subjectname,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_subject s ON g.subjectid=s.subjectid
         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=#{sparams.schoolid} AND ggs.schoolid=#{sparams.schoolid}
        AND c.gradeid=#{sparams.gradeid} and g.year=#{sparams.year} and u.userstate=1
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND g.createtime >= #{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND g.createtime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
         GROUP BY g.subjectid ORDER BY s.subjectorder
    </select>
    <select id="listSchoolTaskGuideDetail" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        select g.createid userid,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 c.classid=ggs.classid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and g.schoolid=#{sparams.schoolid} and ggs.schoolid=#{sparams.schoolid}
        and c.gradeid=#{sparams.gradeid} and g.year=#{sparams.year}
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND g.createtime >= #{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND g.createtime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
            and g.subjectid=#{sparams.subjectid}
        </if>
        group by g.createid
    </select>

    <!--背诵-->
    <select id="listSchoolTaskRecite" resultMap="taskwork">
          SELECT r.subjectid,s.subjectname,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_subject s ON r.subjectid=s.subjectid
         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=#{sparams.schoolid} AND rg.schoolid=#{sparams.schoolid}
        AND c.gradeid=#{sparams.gradeid} and r.year=#{sparams.year} AND u.userstate=1
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND rg.starttime>=#{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND rg.starttime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
         GROUP BY r.subjectid ORDER BY s.subjectorder
    </select>

    <!--背诵 详情-->
    <select id="listSchoolTaskReciteDetail" resultType="com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo">
        select r.createid userid,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 rs.reciteid=rg.reciteid and rs.classid=rg.classid
        left join t_class c on c.classid=rg.classid
        where r.schoolid=#{sparams.schoolid} and r.recitestate=22 and rg.schoolid=#{sparams.schoolid}
        and c.gradeid=#{sparams.gradeid} and r.year=#{sparams.year}
        <if test="sparams.starttime!=null and sparams.starttime!=0">
            AND rg.starttime>=#{sparams.starttime}
        </if>
        <if test="sparams.stoptime!=null and sparams.stoptime!=0">
            AND rg.starttime <![CDATA[ <= ]]>#{sparams.stoptime}
        </if>
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
            and r.subjectid=#{sparams.subjectid}
        </if>
        group by r.createid
    </select>

    <!--获取用户集合-->
    <select id="listUser" resultType="com.xhkjedu.sapi.vo.analyze.SchoolParams">
        select u.userid as createid,u.username
        from t_class_teacher ct left join t_class c on ct.classid=c.classid
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
        right join t_teacher_powersb tp on tp.teacherid=ct.teacherid
        left join t_subject_book sb on sb.lsbid=tp.lsbid
        </if>
        left join t_user u on ct.teacherid=u.userid
        where u.schoolid=#{sparams.schoolid} and c.gradeid=#{sparams.gradeid} and c.classstate=1 and c.year=#{sparams.year} and u.userstate=1
        <if test="sparams.subjectid!=null and sparams.subjectid!='0'.toString() and sparams.subjectid!=''">
            and sb.subjectid=#{sparams.subjectid}
        </if>
        <if test="sparams.username!=null and sparams.username!=''">
            and u.username like '%${sparams.username}%'
        </if>
        and c.classtype in (1,2)
        group by u.userid
    </select>
</mapper>
