<?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.LibManagerMapper">
    <select id="findOrgMyInfo" resultType="com.ssj.service.weixin.library.dto.SelfCorrectTeacherInfoDTO">
        select tb_lib_manager.name,
               tb_lib_manager.t_photo  photo,
               tb_lib_manager.lib_id,
               tb_lib_join.league_name org_name,
               tb_lib_manager.telephone
        from tb_lib_manager,
             tb_lib_join
        where tb_lib_manager.lib_id = tb_lib_join.id
          and tb_lib_manager.state = 1
          and tb_lib_manager.user_id = #{teacherId}
          and tb_lib_manager.type = #{type}
        limit 1
    </select>

    <select id="findOrgMyInfoV1" resultType="com.ssj.service.weixin.library.dto.SelfCorrectTeacherInfoDTO">
        select tb_lib_manager.name,
               tb_lib_manager.t_photo  photo,
               tb_lib_manager.lib_id,
               tb_lib_join.league_name org_name,
               tb_lib_manager.telephone
        from tb_lib_manager,
             tb_lib_join
        where tb_lib_manager.lib_id = tb_lib_join.id
          and tb_lib_manager.state = 1
          and tb_lib_manager.user_id = #{teacherId}
          and tb_lib_manager.type = #{type}
    </select>

    <select id="findSubjectMyInfo" resultType="com.ssj.service.weixin.library.dto.SubjectMyInfoDto">
        select tb_lib_manager.name,
               tb_lib_manager.t_photo  photo,
               tb_lib_manager.course_name courseName,
               tb_lib_join.league_name leagueName,
               tb_lib_manager.telephone
        from tb_lib_manager,
             tb_lib_join
        where tb_lib_manager.lib_id = tb_lib_join.id
          and tb_lib_manager.state = 1
          and tb_lib_manager.user_id = #{teacherId}
          and tb_lib_manager.type = #{type}
    </select>

    <select id="findSchoolmasterInfo" resultType="com.ssj.service.conch.curator.dto.SchoolmasterInfoDTO">
        select schoolmaster.t_photo                                                    photo,
               schoolmaster.lib_id,
               tb_lib_join.league_name                                                 orgName,
               schoolmaster.telephone                                                  phone,
               schoolmaster.name     schoolName,
               (select count(distinct tb_lib_vip.id)
                from tb_lib_vip
                where tb_lib_vip.lib_id = schoolmaster.lib_id and tb_lib_vip.stat = 1) studentNum,
               (select count(distinct tb_lib_manager.user_id)
                from tb_lib_manager
                where tb_lib_manager.lib_id = schoolmaster.lib_id
                  and tb_lib_manager.type in (2, 10, 15)
                  and tb_lib_manager.state = 1)                                        teacherNum,
               tb_lib_join.lib_service,
               tb_lib_join.platform_service
        from tb_lib_manager schoolmaster,
             tb_lib_join
        where schoolmaster.lib_id = tb_lib_join.id
          and schoolmaster.user_id = #{schoolmasterUserId}
          and schoolmaster.type = 4
          and schoolmaster.state = 1
    </select>

    <select id="findExerciseTeacherList" resultType="com.ssj.service.kmt.exercisesys.dto.ExerciseTeacherDTO">
        select lm.*,
               sum(if((lm.teacherId = eb.handle_user_id and eb.had_handle = 0)
                          or
                      (lm.teacherId = eb.auditing_user_id and (eb.had_handle = 0 or eb.had_auditing = 0))
                          or
                      (lm.teacherId = eb.analysis_user_id and eb.analysis_handled = 0)
                          or
                      (lm.teacherId = eb.analysis_auditer_id and (eb.analysis_handled = 0 or eb.analysis_audited = 0)), 1, 0)
                   )      unFinishCount,
               sum(if(eb.id is null, 0, 1))   totalCount
        from (select user_id teacherId, t_photo image, name, course_name subject, authority, is_banned, is_cancelled,
                     is_formal
              from tb_lib_manager
              where state = 1
                and type = 17
                <if test="req.subject != null and req.subject != ''">
                    and course_name regexp #{req.subject}
                </if>
                <choose>
                    <when test="req.type != null and req.type != ''">
                        and authority regexp #{req.type}
                    </when>
                    <otherwise>
                        and authority regexp '[12348]'
                    </otherwise>
                </choose>
                and #{req.maxCreateTime} > create_time) lm
                 left join
             kmt_exercise_book eb
             on (lm.teacherId = eb.handle_user_id or
                 lm.teacherId = eb.auditing_user_id or
                 lm.teacherId = eb.analysis_user_id or
                 lm.teacherId = eb.analysis_auditer_id)
        <where>
            <if test="req.teacherName != null and req.teacherName != ''">
                lm.name regexp #{req.teacherName}
            </if>
        </where>
        group by lm.teacherId, lm.is_banned, lm.is_cancelled
        order by lm.is_banned, lm.is_cancelled, unFinishCount desc
    </select>

    <!-- 查询kmt老师列表 -->
    <select id="findKmtTeacherList" resultType="com.ssj.service.kmt.exercisesys.dto.ExerciseTeacherDTO">
        select m.user_id teacher_id, m.t_photo image, m.name, m.is_banned, m.is_cancelled, m.is_formal, m.authority, etd.total_count, etd.un_finish_count,m.course_name subject,
               etd.question_total_count, etd.question_unfinish_count from tb_lib_manager m
            left join  kmt_exercise_teacher_datalist etd on m.user_id=etd.teacher_id and etd.type=#{type}
        where m.state=1
        <if test="subject != null and subject != ''">
            and m.course_name regexp #{subject}
        </if>
        <choose>
            <when test="authority != null and authority != '' and authority != 0">
                and m.authority regexp #{authority}
            </when>
            <otherwise>
                and m.authority regexp '[56]'
            </otherwise>
        </choose>
        <if test="teacherName != null and teacherName != ''">
            and m.name regexp #{teacherName}
        </if>
        and m.type = 17
        order by m.is_banned, m.is_cancelled, etd.question_unfinish_count desc, etd.id
    </select>

    <select id="findExerciseTeacherDetails"
            resultType="com.ssj.service.kmt.exercisesys.dto.ExerciseTeacherDetailDTO">
        select book_id, exercise_book_id, book_name, grade, semester, course_name, version, book_type, book_status,
               unfinished_unit_count, total_unit_count, accuracy
        from kmt_exercise_book_count
        where user_id = #{req.teacherId}
          and #{req.maxCreateTime} > create_time
          <if test="req.bookName != null and req.bookName != ''">
                and book_name like concat('%',#{req.bookName},'%')
          </if>
          and book_status > 0
        order by book_status
    </select>

    <select id="findTeacherStatusDetail"
            resultType="com.ssj.service.kmt.exercisesys.dto.TeacherInfoStatusDetailDTO">
        select user_id teacherId,
               t_photo image,
               name,
               school,
               sex,
               create_time,
               major,
               telephone,
               authority,
               is_banned,
               is_cancelled,
               is_formal,
               course_name subject
        from tb_lib_manager
        where user_id = #{teacherId}
          and state = 1
          and type = 17
          order by create_time desc
        limit 1
    </select>

    <select id="findTbLibManagerByUserIdsAndType" resultType="com.ssj.bean.weixin.libmy.domain.TbLibManager">
        select * from tb_lib_manager where type in
        <foreach collection="types" open="(" item="type" separator="," close=")">
            #{type}
        </foreach>
        and user_id in
        <foreach collection="userIds" open="(" item="userId" separator="," close=")">
            #{userId}
        </foreach>
    </select>

    <select id="findExerciseTeacherByName" resultType="String">
        select user_id from tb_lib_manager
        where state=1
        and type=17
        and is_cancelled=0
        and name = #{name}
        union
        select user_id from tb_lib_manager_audit
        where type=17
        and status=3
        and name = #{name}
        limit 1
    </select>

    <select id="findManagerByNameAndType" resultType="com.ssj.bean.weixin.libmy.domain.TbLibManager">
        select * from tb_lib_manager where name regexp #{name} and type=#{type} and state=1
    </select>
    
    <select id="findRandManagerByTypeAndSubject" resultType="com.ssj.bean.weixin.libmy.domain.TbLibManager">
        select * from tb_lib_manager m where type=17 and authority regexp '6' and state=1
        <if test="subject != null and subject != ''">
            and course_name regexp #{subject}
        </if>
        order by rand() limit 1;
    </select>

    <select id="findCheckTeacherList"
            resultType="com.ssj.service.kmt.exercisesys.dto.FindCheckTeacherListDto">
        select user_id teacherId,
               name teacherName,
               is_banned,
               is_cancelled,
               is_formal,
               course_name subject
        from tb_lib_manager
        where type = 17
        and state = 1
        and authority regexp #{authorityType}
        and is_cancelled = 0
        and is_banned = 0
        <if test="subjects != null and subjects.size()>0" >
            <foreach collection="subjects" item="subject" index="index" open="" close=" " separator="">
                and course_name regexp #{subject}
            </foreach>
        </if>
        <if test="teacherName != null and teacherName != ''">
            and name regexp #{teacherName}
        </if>
    </select>

    <select id="findCheckTeacherDetailsList" resultType="java.util.Map">
        select t.id, t.book_id bookId, t.book_name bookName, t.grade grade, t.semester semester, t.course_name subject,
         t.version_name version, book_type bookType, t.answer_corrected_status bookStatus,t.check_time checkTime
         from kmt_exercise_book t
         where t.answer_correcter_id = #{teacherId}
         and t.create_time &lt;=#{maxCreateTime}
         order by t.answer_corrected_status,t.check_time desc
    </select>

    <select id="findCheckTeacherDetails" resultType="java.util.Map">
     select
      sum(case when t.answer_corrected_status=0 then 1 else 0 end) uncheckCount,
      sum(case when t.answer_corrected_status=1 then 1 else 0 end) checkCount
      from kmt_exercise_book t
      where t.answer_correcter_id = #{teacherId}
      and t.create_time &lt;=#{maxCreateTime}
   </select>
</mapper>