<?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.sstudy.mapper.resource.GuideMapper">

    <resultMap id="guidesForMap" type="com.xhkjedu.sstudy.vo.resource.GuideVo">
        <result column="guideid" property="guideid"/>
        <result column="guidename" property="guidename"/>
        <result column="createname" property="createname"/>
        <result column="createtime" property="createtime"/>
        <result column="costtime" property="costtime"/>
        <result column="subjectname" property="subjectname"/>
        <result column="createid" property="createid"/>
        <result column="hashb" property="hashb"/>
        <collection property="resources" ofType="java.util.Map"
                    javaType="java.util.List" select="listResourcesByGuideid" column="{guideid=guideid}">
        </collection>
    </resultMap>

    <!--学生预习列表教师网页端-->
    <select id="listGuidesForTeacherWeb" resultMap="guidesForMap">
        select distinct g.guideid,g.guidename,g.createtime,g.schoolid,g.createid,g.hashb
        ,(select u.username from t_user u where g.createid=u.userid)createname
        from t_guide g left join t_guide_resource gr on g.guideid=gr.guideid
        left join t_guide_group gg on g.guideid = gg.guideid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and g.subjectid=#{guide.subjectid}
        <if test="userVo.usertype!=null and userVo.usertype==0">
            and g.schoolid=#{userVo.schoolid}
        </if>
        <if test="userVo.usertype!=0">
            and ((g.hashb=1 and g.schoolid=#{guide.schoolid} and gg.classid in (${guide.classids})) or
            (gg.classid in (${guide.bxclassids})) or g.createid=#{guide.createid})
        </if>
        <if test="guide.lsbid!=null and guide.lsbid!='' and guide.lsbid!='0'.toString()">
            and g.lsbid = #{guide.lsbid}
        </if>
        <if test="guide.directorid!=null and guide.directorid!='' and guide.directorid!='0'.toString()">
            and g.directorid = #{guide.directorid}
        </if>
        <if test="guide.guidename!=null and guide.guidename!=''">
            and g.guidename like '%${guide.guidename}%'
        </if>
        group by g.guideid order by g.createtime desc,g.guidename
    </select>
    <select id="listResourcesByGuideid" resultType="java.util.Map">
        select r.resourceid, r.resourcename,r.resourceurl,r.resourcecover,r.converted,r.pdfurl,
        r.suffix,r.mp4code,r.resourcesize,r.duration,r.resourceclass
        from t_resource r left join t_guide_resource gr on r.resourceid=gr.resourceid
        where gr.guideid=#{guideid} order by gr.grid
    </select>
    <!--学生预习列表教师app-->
    <select id="listGuidesForTeacherApp" resultType="java.util.Map">
        select g.guideid,g.guidename,g.createtime,
        g.schoolid,(select u.username from t_user u where g.createid=u.userid)createname,
        count(DISTINCT gr.resourceid)as filenum,r.suffix,r.resourcecover,r.resourceclass,r.converted,
        sum(r.resourcesize) as resourcesize
        from t_guide g left join t_guide_resource gr on g.guideid=gr.guideid
        left join t_resource r on gr.resourceid=r.resourceid
        left join t_guide_group gg on g.guideid = gg.guideid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and g.subjectid=#{guide.subjectid}
        and ((g.hashb=1 and g.schoolid=#{guide.schoolid} and gg.classid in (${guide.classids})) or
        (gg.classid in (${guide.bxclassids})) or g.createid=#{guide.createid})
        <if test="guide.lsbid!=null and guide.lsbid!='' and guide.lsbid!='0'.toString()">
            and g.lsbid = #{guide.lsbid}
        </if>
        <if test="guide.directorid!=null and guide.directorid!='' and guide.directorid!='0'.toString()">
            and g.directorid = #{guide.directorid}
        </if>
        <if test="guide.guidename!=null and guide.guidename!=''">
            and g.guidename like '%${guide.guidename}%'
        </if>
        group by g.guideid order by g.createtime desc,g.guidename limit  #{guide.page},#{guide.pageSize}
    </select>

    <!--学生预习列表学生网页端-->
    <select id="listGuidesForStuWeb" resultMap="guidesForMap">
        select g.guideid,g.guidename,g.createtime,g.schoolid,u.username as createname,ggs.costtime,
        s.subjectname,count(DISTINCT gr.resourceid)as filenum
        from t_guide_group_student ggs
        left join t_guide g on ggs.guideid=g.guideid
        left join t_guide_resource gr on g.guideid=gr.guideid
        left join t_resource r on gr.resourceid=r.resourceid
        left join t_user u on g.createid=u.userid
        left join t_subject s on g.subjectid=s.subjectid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and ggs.studentid=#{guide.studentid}
        <if test="guide.subjectid!=null and guide.subjectid!='' and guide.subjectid!='0'.toString()">
            and g.subjectid=#{guide.subjectid}
        </if>
        <if test="guide.lsbid!=null and guide.lsbid!='' and guide.lsbid!='0'.toString()">
            and g.lsbid = #{guide.lsbid}
        </if>
        <if test="guide.directorid!=null and guide.directorid!='' and guide.directorid!='0'.toString()">
            and g.directorid = #{guide.directorid}
        </if>
        group by ggs.rgsid
        <if test="guide.costtime==1">
            having ggs.costtime=0
        </if>
        <if test="guide.costtime==2">
            having ggs.costtime>0
        </if>
        order by g.createtime desc,g.guidename asc
    </select>

    <!--学生预习列表学生app-->
    <select id="listGuidesForStuApp" resultType="java.util.Map">
        select g.guideid,g.guidename,g.createtime,g.schoolid,ggs.costtime,
        count(DISTINCT gr.resourceid)as filenum,r.suffix,r.resourcesize,r.resourcecover,r.resourceclass,r.converted,
        u.username as createname,ggs.costtime from t_guide_group_student ggs
        left join t_guide g on ggs.guideid=g.guideid
        left join t_guide_resource gr on g.guideid=gr.guideid
        left join t_resource r on gr.resourceid=r.resourceid
        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 ggs.studentid=#{guide.studentid}
        <if test="guide.subjectid!=null and guide.subjectid!='' and guide.subjectid!='0'.toString()">
            and g.subjectid=#{guide.subjectid}
        </if>
        <if test="guide.lsbid!=null and guide.lsbid!='' and guide.lsbid!='0'.toString()">
            and g.lsbid = #{guide.lsbid}
        </if>
        <if test="guide.directorid!=null and guide.directorid!='' and guide.directorid!='0'.toString()">
            and g.directorid = #{guide.directorid}
        </if>
        group by ggs.rgsid
        <if test="guide.costtime==1">
            having ggs.costtime=0
        </if>
        <if test="guide.costtime==2">
            having ggs.costtime>0
        </if>
        order by g.createtime desc,g.guidename asc limit #{guide.page},#{guide.pageSize}
    </select>

    <!--删除预习-->
    <delete id="deleteByGuideid">
        delete from t_guide where guideid=#{guideid}
    </delete>

    <!--根据预习id获取预习详情-->
    <select id="getGuideByGuideid" resultMap="guidesForMap">
      select g.guideid,g.guidename,g.createtime,g.schoolid,g.createid,u.username as createname
        from t_guide g
        left join t_user u on g.createid=u.userid
        where g.guideid=#{guideid}
    </select>


    <!--预习统计：年级下教师列表-->
    <select id="listUser" resultType="com.xhkjedu.sstudy.vo.resource.GuideAnalyzeVo">
        select u.userid as teacherid,u.username teachername,s.subjectname,c.gradeid
        from t_class_teacher ct inner join t_class c on ct.classid=c.classid
        inner join t_teacher_powersb tp on tp.teacherid=ct.teacherid
        inner join t_subject_book sb on sb.lsbid=tp.lsbid
        inner join t_subject s on s.subjectid=sb.subjectid
        inner join t_user u on ct.teacherid=u.userid
        where u.schoolid=#{paper.schoolid} and c.classtype in(1,2) and c.gradeid=#{paper.gradeid}
        and c.classstate=1 and c.year=#{paper.year} and u.userstate=1
        <if test="paper.subjectid!=null and paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and sb.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and c.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and c.classid in (${paper.classids})
        </if>
        group by u.userid
    </select>
    <!--预习统计：教师预习统计-->
    <select id="getPreviewAnalyze" resultType="com.xhkjedu.sstudy.vo.resource.GuideAnalyzeVo">
        select g.createid as teacherid,g.hashb,count(distinct ggs.guideid)fbnum,count(distinct ggs.rgsid)yyxnum,
        count(distinct if(ggs.costtime!=0, ggs.rgsid, null))yxnum
        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_director d on g.directorid=d.directorid
        where d.disabled=0 and g.schoolid=#{paper.schoolid} and ggs.schoolid=#{paper.schoolid}
        and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and c.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and c.classid in (${paper.classids})
        </if>
        group by g.createid
    </select>
    <!--获取年级下教师发布学生预习列表-->
    <select id="listPreview" resultMap="guidesForMap">
        select g.guideid,g.guidename,g.createtime,g.schoolid,g.createid,count(distinct gr.grid)as filenum
        ,(select u.username from t_user u where u.userid=g.createid)createname
        from t_guide g
        left join t_guide_group gg on gg.guideid=g.guideid
        left join t_class c on gg.classid=c.classid
        left join t_guide_resource gr on g.guideid=gr.guideid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        and g.createid=#{paper.createid} and gg.schoolid=#{paper.schoolid}
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and gg.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and gg.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
        group by g.guideid order by g.createtime desc,g.guidename,g.guideid
    </select>
    <!--预习统计-按学生列表-->
    <!--<select id="listPreviewStuGrade1" resultType="java.util.Map">
        select a.*,ifnull(truncate(a.commitnum*100/a.sdnum,2),0)commitrate
        from(select cs.studentid,u.username studentname
        ,count(distinct if(g.guideid is not null, ggs.rgsid, null))sdnum
        ,count(distinct if(ggs.costtime!=0 and g.guideid is not null, ggs.rgsid, null))commitnum
        from t_class_student cs left join t_class c on cs.classid=c.classid
        left join t_guide_group_student ggs on c.classid=ggs.classid and ggs.studentid=cs.studentid
        left join t_guide g on ggs.guideid=g.guideid
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
        left join t_user u on cs.studentid=u.userid
        where c.schoolid=#{paper.schoolid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year} and u.userstate=1
        <if test="paper.classid!=0">
            and c.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and c.classid in (${paper.classids})
        </if>
        group by cs.studentid)a
        order by a.commitnum desc,a.studentid
    </select>-->
    <select id="listPreviewStuGrade" resultType="java.util.Map">
        select a.*,ifnull(truncate(a.commitnum*100/a.sdnum,2),0)commitrate
        from(select cs.*
        ,count(distinct if(g.guideid is not null and d.disabled=0, ggs.rgsid, null))sdnum
        ,count(distinct if(ggs.costtime!=0 and g.guideid is not null and d.disabled=0, ggs.rgsid, null))commitnum
        from (select cs2.studentid,u.username studentname
        from t_class_student cs2,t_class c,t_user u
        where c.schoolid=#{paper.schoolid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        and c.classtype in(1,2) and c.classstate=1
        and u.userstate=1 and cs2.classid=c.classid and cs2.studentid=u.userid
        <if test="paper.classid!=null and paper.classid!=0">
            and cs2.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and cs2.classid in (${paper.classids})
        </if>
        group by cs2.studentid)cs
        left join t_guide_group_student ggs on ggs.studentid=cs.studentid
        left join t_guide g on ggs.guideid=g.guideid
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
        <if test="paper.classid!=null and paper.classid!=0">
            and ggs.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ggs.classid in (${paper.classids})
        </if>
        left join t_director d on g.directorid=d.directorid
        group by cs.studentid)a
        order by a.commitnum desc,a.studentid
    </select>
    <!--预习统计-按学生详情-->
    <select id="listPreviewStuDetail" resultType="com.xhkjedu.sstudy.vo.paper.AStudentPaperVo">
        select a.*,ifnull(truncate(a.commitnum*100/a.sdnum,2),0)commitrate
        from(select s.subjectname
        ,count(distinct ggs.rgsid)sdnum,count(distinct if(ggs.costtime!=0, ggs.rgsid, null))commitnum
        from t_guide_group_student ggs left join t_class c on ggs.classid=c.classid
        left join t_guide g on ggs.guideid=g.guideid
        left join t_subject s on g.subjectid=s.subjectid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and ggs.studentid=#{paper.studentid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=0">
            and ggs.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ggs.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
        group by g.subjectid order by s.subjectorder)a
    </select>
    <!--预习统计-按学生列表-->
    <select id="listPreviewStuExport" resultType="com.xhkjedu.sstudy.vo.paper.AStudentPaperVo">
        select ggs.studentid,g.subjectid,ggs.costtime psstate
        from t_guide_group_student ggs left join t_class c on ggs.classid=c.classid
        left join t_guide g on ggs.guideid=g.guideid
        left join t_director d on g.directorid=d.directorid
        where d.disabled=0 and ggs.studentid=#{paper.studentid} and c.gradeid=#{paper.gradeid} and c.year=#{paper.year}
        <if test="paper.starttime!=null and paper.starttime!=0">
            and g.createtime>=#{paper.starttime}
        </if>
        <if test="paper.stoptime!=null and paper.stoptime!=0">
            and g.createtime&lt;=#{paper.stoptime}
        </if>
        <if test="paper.classid!=0">
            and ggs.classid=#{paper.classid}
        </if>
        <if test="paper.classids!=null and paper.classids!=''">
            and ggs.classid in (${paper.classids})
        </if>
        <if test="paper.subjectid!='0'.toString() and paper.subjectid!=''">
            and g.subjectid=#{paper.subjectid}
        </if>
    </select>

    <!--预习关联班级-->
    <select id="listGuideClassByGuide" resultType="java.util.Map">
        select rs.classid,c.classname,c.gradeid,s.schoolid,s.schoolname,count(distinct rs.rgsid)stunum
        ,count(distinct if(rs.readtime>0, rs.rgsid, null))commitnum
        from t_guide_group_student rs left join t_class c on rs.classid = c.classid
        left join t_guide g on rs.guideid=g.guideid
        left join t_school s on rs.schoolid=s.schoolid
        where rs.guideid=#{guide.guideid}
        <if test="guide.classids!=null and guide.classids!=''">
            and rs.classid in (${guide.classids})
        </if>
        group by rs.classid
        order by if(s.schoolid=#{guide.schoolid},0,s.schoolid),c.classtype,c.classorder
    </select>
    <!--获取预习年份-->
    <select id="getYearById" resultType="java.lang.Integer">
        select year from t_guide where guideid=#{guideid}
    </select>
</mapper>
