<?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.ssj.service.mapper.CuratorMapper">
    <select id="findMyInfo" resultType="com.ssj.service.conch.curator.dto.CuratorDTO">
        select tb_lib_manager.name      curator_name,
               tb_lib_manager.telephone phone,
               tb_lib_manager.lib_id,
               tb_lib_join.league_name  lib_name
        from tb_lib_manager,
             tb_lib_join
        where tb_lib_manager.lib_id = tb_lib_join.id
          and tb_lib_manager.user_id = #{curatorUserId}
          and tb_lib_manager.type = 4
          and tb_lib_manager.state = 1
        limit 1
    </select>

    <select id="findApplyInfo" resultType="com.ssj.service.conch.curator.dto.CuratorDTO">
        select tb_lib_manager_audit.name      curator_name,
               tb_lib_manager_audit.telephone phone,
               tb_lib_manager_audit.lib_id,
               tb_lib_join.league_name  lib_name
        from tb_lib_manager_audit,
             tb_lib_join
        where tb_lib_manager_audit.lib_id = tb_lib_join.id
          and tb_lib_manager_audit.user_id = #{curatorUserId}
          and tb_lib_manager_audit.type = 4
        limit 1
    </select>

    <!--校长端老师列表-->
    <select id="findCuratorTeacherList" resultType="com.ssj.bean.conch.curator.dto.CuratorTeacherListDto">
        select lm.user_id teacher_id, group_concat(lm.type) teacher_type_list, group_concat(distinct course_name) subject,
        lm.name,  lm.t_photo img_url, lm.telephone phone,ct.class_sum from tb_lib_manager lm
        left join (select teacher_id, count(class_id) class_sum from tb_class_teacher where  status=1 and lib_id=#{libId} group by teacher_id) ct
        on ct.teacher_id=lm.user_id
        where lm.type in
        <foreach collection="types" open="(" item="type" separator="," close=")">
            #{type}
        </foreach>
        <if test="searchKey != null and searchKey != '' ">
            and lm.name regexp #{searchKey}
        </if>
        and lm.state=1 and lm.lib_id=#{libId}
        group by lm.user_id
    </select>

    <!--老师详情-->
    <select id="findCuratorTeacherDetailData" resultType="com.ssj.bean.conch.curator.dto.CuratorTeacherListDto">
        select lm.user_id teacher_id, group_concat(lm.type) teacher_type_list, group_concat(distinct course_name) subject,
        lm.name,  lm.t_photo img_url, lm.telephone phone from tb_lib_manager lm
        where lm.state = 1 and lm.user_id=#{teacherId} and lm.type in
        <foreach collection="types" open="(" item="type" separator="," close=")">
            #{type}
        </foreach>
        group by lm.user_id
    </select>

    <!--老师详情-->
    <select id="findCuratorTeacherDetailDataV1" resultType="com.ssj.bean.conch.curator.dto.CuratorTeacherListDto">
        select lm.user_id teacher_id, group_concat(lm.type) teacher_type_list, group_concat(distinct course_name) subject,
        lm.name,  lm.t_photo img_url, lm.telephone phone from tb_lib_manager lm
        where lm.state = 1 and lm.user_id=#{teacherId} and lm.lib_id=#{libId} and lm.type in
        <foreach collection="types" open="(" item="type" separator="," close=")">
            #{type}
        </foreach>
        group by lm.user_id
    </select>

    <!--查询老师详情的班级列表-->
    <select id="findTeacherDetailClassList" resultType="com.ssj.bean.conch.curator.dto.TeacherDetailClassListDto">
        select c.id class_id, c.class_name, if(c.status=2, 1, 0) is_default from tb_class_teacher ct
        left join tb_class c on ct.class_id=c.id
        where ct.teacher_id=#{teacherId} and ct.status=1 and c.status in (1, 2)
    </select>

    <!--查询老师详情的班级列表-->
    <select id="findTeacherDetailClassListV1" resultType="com.ssj.bean.conch.curator.dto.TeacherDetailClassListDto">
        select c.id class_id, c.class_name, if(c.status=2, 1, 0) is_default from tb_class_teacher ct
        left join tb_class c on ct.class_id=c.id
        where ct.teacher_id=#{teacherId} and ct.lib_id=#{libId} and ct.status=1 and c.status in (1, 2)
    </select>

    <!--查询班级列表的学生数-->
    <select id="findClassStudentCount" resultType="com.ssj.bean.conch.curator.dto.TeacherDetailClassListDto">
        select class_id, count(1) student_sum from tb_class_student
        where class_id in
        <foreach collection="classIds" open="(" item="classId" separator="," close=")">
            #{classId}
        </foreach>
        and status = 1
        group by class_id
    </select>

    <!--查询班级列表的学生数-->
    <select id="findClassTeacherCount" resultType="com.ssj.bean.conch.curator.dto.TeacherDetailClassListDto">
        select class_id, count(1) teacher_sum from tb_class_teacher
        where class_id in
        <foreach collection="classIds" open="(" item="classId" separator="," close=")">
            #{classId}
        </foreach>
        and status = 1
        group by class_id
    </select>

    <!--查询老师不在的的班级列表-->
    <select id="findTeacherNotInClassList" resultType="com.ssj.bean.conch.curator.dto.TeacherDetailClassListDto">
        select  class.id class_id, class.class_name, if(class.status=2, 1, 0) is_default  from tb_class class
        left join tb_class_teacher teacher on class.id=teacher.class_id
        and  teacher.status=1
        and  teacher.teacher_id = #{teacherId}
        where class.lib_id=#{libId} and class.status in (1, 2) and teacher.id is null
    </select>

    <select id="findClassStudentDetail" resultType="java.util.Map">
        select t.class_name className,t.id classId, t1.vip_id vipId,t2.child_name childName,t2.img_url imgUrl,t2.grade grade,t3.mobile phone,
        (
        select IF(ts.species='S999',999,correct_type)  from tb_lib_vip_service ts
        where ts.vip_id=t1.vip_id
        and ts.stat = 1
        and ts.species in ('S003','S999')
        and to_days(ts.start_time) &lt;=to_days(now())
        and to_days(ts.end_time) >= to_days(now())
        ORDER BY ts.species,ts.service_sort,ts.correct_type,ts.start_time limit 1
        ) as correctType
        from tb_class t left join tb_class_student t1
        on t.id=t1.class_id
        left join tb_lib_vip t2
        on t1.vip_id=t2.id
        left join tb_user t3
        on t2.user_id=t3.id
        where
        <if test="classId != null and classId != '' ">
             t.id=#{classId}
        </if>
        and t1.status=1
        and t2.stat=1
        <if test="name != null and name != '' ">
            and t2.child_name regexp #{name}
        </if>

    </select>

    <select id="findClassStudentDetailByLibId" resultType="java.util.Map">
        select t.class_name className,t.id classId, t1.vip_id vipId,t2.child_name childName,t2.img_url imgUrl,t2.grade grade,t3.mobile phone,
        (
        select IF(ts.species='S999',999,correct_type)  from tb_lib_vip_service ts
        where ts.vip_id=t1.vip_id
        and ts.stat = 1
        and ts.species in ('S003','S999')
        and to_days(ts.start_time) &lt;=to_days(now())
        and to_days(ts.end_time) >= to_days(now())
        ORDER BY ts.species,ts.service_sort,ts.correct_type,ts.start_time limit 1
        ) as correctType
        from tb_class t left join tb_class_student t1
        on t.id=t1.class_id
        left join tb_lib_vip t2
        on t1.vip_id=t2.id
        left join tb_user t3
        on t2.user_id=t3.id
        where
        <if test="libId != null and libId != '' ">
            t.lib_id=#{libId}
        </if>
        and t1.status=1
        and t2.stat=1
        <if test="name != null and name != '' ">
            and t2.child_name regexp #{name}
        </if>

    </select>

    <!--查询其余的学生列表-->
    <select id="findOtherClassStudentDetail" resultType="java.util.Map">
        select t.class_name className,t.id classId, t1.vip_id vipId,t2.child_name childName,t2.img_url imgUrl,t2.grade grade,t3.mobile phone,
        (
        select IF(ts.species='S999',999,correct_type)  from tb_lib_vip_service ts
        where ts.vip_id=t1.vip_id
        and ts.stat = 1
        and ts.species in ('S003','S999')
        and to_days(ts.start_time) &lt;=to_days(now())
        and to_days(ts.end_time) >= to_days(now())
        ORDER BY ts.species,ts.service_sort,ts.correct_type,ts.start_time limit 1
        ) as correctType
        from tb_class t left join tb_class_student t1
        on t.id=t1.class_id
        left join tb_lib_vip t2
        on t1.vip_id=t2.id
        left join tb_user t3
        on t2.user_id=t3.id
        where
        <if test="classId != null and classId != '' ">
            t.id !=#{classId}
        </if>
        and t1.status=1
        and t2.stat=1
        and t.lib_id=#{libId}
        <if test="name != null and name != '' ">
            and t2.child_name regexp #{name}
        </if>

    </select>

    <select id="findClassList"  resultType="java.util.Map">
        select t1.id                             classId,
               t1.class_name                     className,
               (select count(distinct t2.vip_id)
                from tb_class_student t2,
                     tb_lib_subscribe ls
                where t2.class_id = t1.id
                  and t2.status = 1
                  and ls.vip_id = t2.vip_id
                  and to_days(ls.ryke_time) = to_days(now())
                  and ls.lib_id = #{libId}
                  and ls.stat = '1'
                  and ls.flow_stat in ('3', '4')
                  and t2.vip_id is not null)     signedStudentCount,
               (select count(distinct t2.vip_id)
                from tb_class_student t2,tb_lib_vip t3
                where t2.vip_id = t3.id
                  and t2.lib_id = t3.lib_id
                  and t2.class_id = t1.id
                  and t2.status = 1
                  and t2.vip_id is not null)     studentSum,
               (select count(distinct t3.teacher_id)
                from tb_class_teacher t3
                where t3.class_id = t1.id
                  and t3.lib_id = t1.lib_id
                  and t3.status = 1
                  and exists(
                        select t4.user_id
                        from tb_lib_manager t4
                        where t4.lib_id = t1.lib_id
                          and t4.user_id = t3.teacher_id
                          and t4.state = 1
                          and t4.type in (2, 10, 15)
                          and t4.is_cancelled = 0
                    )
                  and t3.teacher_id is not null) teacherSum
        from tb_class t1
        where t1.lib_id = #{libId}
          and t1.status in (1, 2)
        order by t1.status desc, t1.create_time
    </select>

    <!--查询学生不在的的班级列表-->
    <select id="findStudentNotInClass"  resultType="java.util.Map">

        select t1.id classId,t1.class_name className,
        (
        select count(distinct t2.vip_id) from tb_class_student t2 where
        t2.class_id=t1.id
        and t2.status=1
        and t2.vip_id is not null
        ) as studentSum,
        (
        select count(distinct t3.teacher_id) from tb_class_teacher t3
        where t3.class_id=t1.id  and t3.lib_id=t1.lib_id
        and t3.status=1
        and exists(
        select t4.user_id from tb_lib_manager t4
        where t4.lib_id=t1.lib_id
        and t4.user_id=t3.teacher_id
        and t4.state=1
        and t4.type in(2,10,15)
        and t4.is_cancelled=0
        )
        and t3.teacher_id is not null
        ) as teacherSum
        from tb_class t1
        where
        <if test="libId != null and libId != '' ">
            t1.lib_id =#{libId}
        </if>
        <if test="classId != null and classId != '' ">
          and  t1.id !=#{classId}
        </if>
        and t1.status in(1,2)
        order by t1.status desc,t1.create_time
    </select>

    <select id="getTodayStudentHomeworkSum"  resultType="java.util.Map">
        select sum(case when s.is_delete = 0  then 1 else 0 end)   homeworkNum,
				sum(case when s.is_delete = 0 and s.is_feedback = 1  then 1 else 0 end ) feedbackNum1,
				sum(case when s.is_delete = 0 and s.is_feedback = 0 and s.is_mark in(1,3) then 1 else 0 end ) feedbackNum2
                from scon_homework_picture s where s.vip_id = #{vipId} and s.is_delete = 0 and TO_DAYS(s.create_time) = TO_DAYS(NOW())
    </select>

    <select id="getUploadHomeworkSumByClassId"  resultType="java.lang.Integer">
        select count(*) from (
            select s.vip_id from scon_homework_picture s where s.vip_id in (
                select t1.vip_id from tb_class t left join tb_class_student t1
                    on t.id=t1.class_id
                    left join tb_lib_vip t2
                    on t1.vip_id=t2.id
                    where t.id=#{classId}
                    and t1.status=1
                    and t2.stat=1)
            and TO_DAYS(s.create_time) = TO_DAYS(NOW()) and s.is_delete = 0 GROUP BY s.vip_id) as sss
    </select>

    <select id="getNoCorrectSumByClassId"  resultType="java.lang.Integer">
            select count(*) from (
                select s.vip_id from scon_homework_picture s where s.vip_id in (
                    select t1.vip_id from tb_class t left join tb_class_student t1
                        on t.id=t1.class_id
                        left join tb_lib_vip t2
                        on t1.vip_id=t2.id
                        where t.id=#{classId}
                        and t1.status=1
                        and t2.stat=1)
                and TO_DAYS(s.create_time) = TO_DAYS(NOW()) and s.is_delete = 0 and s.is_feedback = 0 and is_mark in (0, 2)  GROUP BY s.vip_id) as sss
    </select>

    <select id="getStudentSumByClassId"  resultType="java.lang.Integer">
        select count(*) from (
            select  t1.vip_id from tb_class t left join tb_class_student t1
                    on t.id=t1.class_id
                    left join tb_lib_vip t2
                    on t1.vip_id=t2.id
                    where t.id=#{classId}
                    and t1.status=1
                    and t2.stat=1) as sss
    </select>

    <select id="renewStudentList" resultType="java.util.Map">
        select t.id         vipId,
        t.child_name childName,
        t.img_url    imgUrl,
        t.grade,
        (select concat(IF(ts.species = 'S999', 999, ts.correct_type), '#S', ts.end_time)
        from tb_lib_vip_service ts
        where ts.stat = 1
        and ts.species in ('S003', 'S999')
        and to_days(ts.start_time) &lt;= to_days(now())
        and to_days(ts.end_time) >= to_days(now())
        and t.id = ts.vip_id
        ORDER BY ts.species, ts.service_sort, ts.correct_type, ts.start_time
        limit 1
        )            serviceInfo,
        (select tc.class_name from tb_class_student tcs,tb_class tc
        where tcs.class_id = tc.id and t.id = tcs.vip_id and tcs.status = 1 and tc.status>0 limit 1) className
        from tb_lib_vip t
        where 1 = 1
        <if test="map.libId != null and map.libId != '' ">
            and t.lib_id=#{map.libId}
        </if>
        <if test="map.classIdList != null and map.classIdList.size() > 0">
            and exists(select 1 from tb_class_student tc where t.id = tc.vip_id and status = 1
                and tc.class_id in
                <foreach collection="map.classIdList" index="i" item="classId" open="(" close=")" separator=",">
                    #{classId}
                </foreach>
                )
        </if>
    </select>
    <select id="getUploadHomeworkSumByLibId" resultType="java.lang.Integer">
        select count(*) from (
            select s.id from scon_homework_picture s where s.vip_id in (
                select t2.id from  tb_lib_vip t2
                    where t2.lib_id=#{libId}
                    and t2.stat=1)
            and TO_DAYS(s.create_time) = TO_DAYS(NOW()) and s.is_delete = 0 GROUP BY s.vip_id) as sss
    </select>

    <select id="homeworkIndex" resultType="java.util.Map">
            select t11.vip_id,t.id,t12.child_name,t12.grade,t13.league_name,t12.school,count(1) as pictureCount,group_concat(t11.file_key) as file_keys,t11.create_time,t.`subject`
                   ,count(IF(t11.corrected_picture_url is not null,true,null)) as correctedCount,group_concat(t11.corrected_picture_url) as corrected_keys
                   ,count(IF(t.subject='语文',true,null)) as count1,count(IF(t.subject='数学',true,null)) as count2,count(IF(t.subject='英语',true,null)) as count3
                     from scon_homework t
                left join scon_homework_picture t11 on t.id = t11.homework_id
                left join tb_lib_vip t12 on t11.vip_id = t12.id
                left join tb_lib_join t13 on t12.lib_id = t13.id
                where t11.create_time regexp #{queryDate}
                  and t12.lib_id=#{libId} and t11.is_delete = 0
                <if test="searchValue != '' ">
                    and t12.child_name regexp #{searchValue}
                </if>
                group  by t11.vip_id
                order by t.subject,t11.create_time desc
    </select>

    <select id="findPageStudentList" resultType="java.util.Map">
        select distinct t.class_name className,t.id classId, t1.vip_id vipId,t2.child_name childName,t2.img_url imgUrl,t2.grade grade,t3.mobile phone
        from tb_class t left join tb_class_student t1
        on t.id=t1.class_id
        left join tb_lib_vip t2 on t1.vip_id=t2.id
        left join tb_user t3 on t2.user_id=t3.id
        left join tb_lib_vip_service t4 on t2.id = t4.vip_id
            where t.lib_id=#{map.libId}
             and t1.status=1
             and t2.stat=1
        <if test="map.searchValue != null and map.searchValue != '' ">
            and t2.child_name regexp #{map.searchValue}
        </if>
        <if test="map.startDate != null and map.startDate != '' ">
            and to_days(t4.end_time) >= to_days(#{map.startDate})
        </if>
        <if test="map.endDate != null and map.endDate != '' ">
            and to_days(#{map.endDate}) >= to_days(t4.end_time)
        </if>
        <if test="map.stat != null and map.stat != '' ">
            and t4.stat=#{map.stat}
        </if>
        <if test="map.classId != null and map.classId != '' ">
            and t.id=#{map.classId}
        </if>
        <if test="map.serviceType != null and map.serviceType != '' and map.serviceType=='S999'">
            and t4.species='S999'
        </if>
        <if test="map.serviceType != null and map.serviceType != '' and map.serviceType!='S999'">
            and t4.correct_type=#{map.serviceType}
        </if>
        group by t2.id
    </select>

    <select id="findServiceListByVipId" resultType="java.util.Map">
         select  t.start_time,t.end_time,case when t.stat=1 then '有效' else '无效' end stat,
         case when t.species='S999' then '内部用户' when t.correct_type=0 then '无服务' when  t.correct_type=1 then '平台服务(家长购买)'
              when t.correct_type=2 then '校区服务' when t.correct_type=4 then '平台服务(机构购买)' when t.correct_type=0 then '服务已关闭' else '批改服务'
              end as correct_type
         from tb_lib_vip_service t
         where t.vip_id=#{map.vipId} and t.species in ('S003', 'S999')
        <if test="map.startDate != null and map.startDate != '' ">
            and to_days(t.end_time) >= to_days(#{map.startDate})
        </if>
        <if test="map.endDate != null and map.endDate != '' ">
            and to_days(#{map.endDate}) >= to_days(t.end_time)
        </if>
        <if test="map.stat != null and map.stat != '' ">
            and t.stat=#{map.stat}
        </if>
        <if test="map.serviceType != null and map.serviceType != '' and map.serviceType=='S999'">
            and t.species='S999'
        </if>
        <if test="map.serviceType != null and map.serviceType != '' and map.serviceType!='S999'">
            and t.correct_type=#{map.serviceType}
        </if>
        order by t.end_time desc
    </select>

    <select id="findClassListBySys" resultType="java.util.Map">
        select t1.id                             classId,
               t1.class_name                     className,
               (select count(distinct t2.vip_id)
                from tb_class_student t2,
                     tb_lib_subscribe ls
                where t2.class_id = t1.id
                  and t2.status = 1
                  and ls.vip_id = t2.vip_id
                  and to_days(ls.ryke_time) = to_days(now())
                  and ls.lib_id = #{libId}
                  and ls.stat = '1'
                  and ls.flow_stat in ('3', '4')
                  and t2.vip_id is not null)     signedStudentCount,
               (select count(distinct t2.vip_id)
                from tb_class_student t2,tb_lib_vip t3
                where t2.vip_id = t3.id
                  and t2.lib_id = t3.lib_id
                  and t2.class_id = t1.id
                  and t2.status = 1
                  and t2.vip_id is not null)     studentSum,
               (select count(distinct t3.teacher_id)
                from tb_class_teacher t3
                where t3.class_id = t1.id
                  and t3.lib_id = t1.lib_id
                  and t3.status = 1
                  and exists(
                        select t4.user_id
                        from tb_lib_manager t4
                        where t4.lib_id = t1.lib_id
                          and t4.user_id = t3.teacher_id
                          and t4.state = 1
                          and t4.type in (2, 10, 15)
                          and t4.is_cancelled = 0
                    )
                  and t3.teacher_id is not null) teacherSum
        from tb_class t1
        where t1.lib_id = #{libId}
          and t1.status in (1, 2)
        <if test="searchValue != null and searchValue != '' ">
            and t1.class_name regexp #{searchValue}
        </if>
        <if test="teacherId != null and teacherId != '' ">
            and t1.id in (
                select class_id from tb_class_teacher where status = 1
                and teacher_id=#{teacherId}
            )
        </if>
        order by t1.status desc, t1.create_time
    </select>

    <select id="findCuratorTeacherListBySys" resultType="com.ssj.bean.conch.curator.dto.CuratorTeacherListDto">
        select lm.user_id teacher_id, group_concat(distinct  lm.type) teacher_type_list, group_concat(distinct lm.course_name) subject,
        lm.name,  lm.t_photo img_url, lm.telephone phone,count(distinct ct.class_id) as class_sum from tb_lib_manager lm
        left join tb_class_teacher ct on ct.teacher_id=lm.user_id and lm.lib_id = ct.lib_id and ct.status = 1
        where lm.type in
        <foreach collection="types" open="(" item="type" separator="," close=")">
            #{type}
        </foreach>
        <if test="searchKey != null and searchKey != '' ">
            and lm.name regexp #{searchKey}
        </if>
        <if test="classId != null and classId != '' ">
            and ct.class_id = #{classId}
        </if>
        and lm.state=1 and lm.lib_id=#{libId} and  lm.is_cancelled = 0
        group by lm.user_id
    </select>

    <select id="allLibIndex" resultType="java.util.Map">
        select count(1) as totolCount,count(if(t.subject='语文',true,null)) as chineseCount,count(if(t.subject='数学',true,null)) as shuxueCount,count(if(t.subject='英语',true,null)) as englishCount from
        scon_homework_picture t
        left join tb_lib_vip t1 on t.vip_id = t1.id
        where t.is_delete = 0 and t1.lib_id is not null
        <if test="queryDate != null and queryDate != '' ">
            and t.create_time regexp #{queryDate}
        </if>
    </select>
    <select id="searchHomeworkAllTop" resultType="java.util.Map">
        SELECT sum(totolCount) as totolCount,a.lib_id,b.league_name from (
            select count(1) as totolCount,t1.lib_id
            from scon_homework_picture t
            left join tb_lib_vip t1 on t.vip_id = t1.id
            where is_delete = 0 and t1.lib_id is not null
            <if test="startDate != null and startDate != '' ">
                and t.create_time >= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and #{endDate} >= t.create_time
            </if>
            <if test="subject != null and subject != '' ">
                and t.subject = #{subject}
            </if>
            group  by t1.lib_id
            order by totolCount desc)a join tb_lib_join b on a.lib_id = b.id
            GROUP BY a.lib_id
            order by totolCount desc
    </select>
    <select id="searchActiveTop" resultType="java.util.Map">
        select  t2.league_name,t1.lib_id ,
        (select count(1) from tb_lib_vip a where a.lib_id = t1.lib_id) as all_vips,
        count(distinct  t.vip_id) as active_count
        from scon_homework t
        left join tb_lib_vip t1 on t.vip_id = t1.id
        join tb_lib_join t2 on t1.lib_id = t2.id
        where  t1.lib_id is not null
        <if test="startDate != null and startDate != '' ">
            and t.create_time >= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and #{endDate} >= t.create_time
        </if>
        group  by t1.lib_id
    </select>
</mapper>