<?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.mapper.libraryteacher.LibraryTeacherMapper">
    <resultMap id="FeedbackDTOResultMap" type="com.ssj.service.conch.teacher.library.dto.FeedbackDTO">
        <id column="push_id" property="pushId"/>
        <result column="suggest" property="suggest"/>
        <result column="push_time" property="pushTime"/>
        <collection property="homeworkPictureList" ofType="String">
            <constructor>
                <arg column="file_key"/>
            </constructor>
        </collection>
    </resultMap>

    <sql id="tbLibVip">
        t1.id,
        t1.child_name childName,
        t1.img_url imgUrl
    </sql>

    <select id="findVipList" resultType="com.ssj.bean.mybatis.LibVipMBDto" parameterType="java.util.Map">
        select
        <include refid="tbLibVip"/>
        ,t2.id pushId,t2.type,t2.push_time pushTime
        from tb_lib_vip t1
        join zyb_student_push t2 on t1.id = t2.vip_id and to_days(t2.push_time) = to_days( #{map.strdate})
        where
        <!--这里兼容注销逻辑-->


        t2.teacher_id = #{map.teacherId}
        <!--小孩的馆必须和签到的馆是一个馆才能显示 add by Mount 20200422 16:00-->
        and t1.lib_id = t2.lib_id
        and to_days(t2.push_time) = to_days( #{map.strdate})
        and t1.user_id is not null
        <if test="map.type != null and map.type != '' ">
            and t2.type = #{map.type}
        </if>
        order by t2.push_time,t1.id
    </select>

    <select id="findTodayHomeworkByVipId" resultType="com.ssj.bean.mybatis.HomeworkPictureMBDto" parameterType="String">
      		 select
             t.course_name subject,
             t.type workType,

        	sum(case when e.is_delete = 0  then 1 else 0 end)   homeworkNum,
				( sum(case when e.is_delete = 0 and e.is_feedback = 1  then 1 else 0 end ) +
				sum(case when e.is_delete = 0 and e.is_feedback = 0 and e.is_mark in(1,3) then 1 else 0 end ) )    feedbackNum
       from
	( SELECT  a.id  ,a.course_name ,  a.type from  scon_no_homework_record a where  a.vip_id =#{vipId}
	 and to_days(a.create_time) = to_days( #{strdate} ) ) t
       left join  scon_homework_picture e on e.homework_record_id = t.id and e.is_delete=0
        GROUP BY  t.course_name
    </select>

    <select id="findTodayBadHomeworkByVipId" resultType="com.ssj.bean.mybatis.HomeworkPictureMBDto"
            parameterType="String">
      select t.isMark,t.fileKey,t.picNum,t.remindTime,t.subject
        from (select t.is_mark isMark,
                     t.file_key fileKey,
                     count(t.id)        picNum,
                     min(t.create_time) remindTime,
                     t.subject
              from scon_homework_picture t
              where t.is_mark in (1, 3)
                and to_days(t.create_time) = to_days(now())
                and t.vip_id = #{vipId}
              group by t.vip_id, t.is_mark, t.subject) t
        order by t.remindTime
    </select>

    <select id="countHomeworkDataByClass" resultType="java.util.Map">
        select count(distinct kmt_homework_wrong_question.id) totalCount,
        sum(if(kmt_homework_wrong_question.type = 1, 1, 0)) wrongCount
        from tb_class_student,
        scon_homework,
        kmt_homework_wrong_question
        where tb_class_student.vip_id = scon_homework.vip_id
        and scon_homework.id = kmt_homework_wrong_question.homework_id
        and tb_class_student.status = 1
        and scon_homework.subject in
        <foreach collection="subjectList" open="(" item="subject" separator="," close=")">
            #{subject}
        </foreach>
        and tb_class_student.class_id = #{classId}
        and scon_homework.create_time > #{beginDateTimeStr}
        and #{endDateTimeStr} > scon_homework.create_time
    </select>

    <select id="countSubmittedStudentNum" resultType="java.lang.Integer">
        select count(distinct scon_homework_picture.vip_id)
        from (select vip_id
        from tb_class_student
        where class_id = #{classId}
        and status = 1) student
        join scon_homework_picture on student.vip_id = scon_homework_picture.vip_id and
        scon_homework_picture.create_time between #{beginDateTimeStr} and #{endDateTimeStr} and
        scon_homework_picture.subject in
        <foreach collection="subjectList" open="(" item="subject" separator="," close=")">
            #{subject}
        </foreach>
        <if test="corrected">
            and scon_homework_picture.is_feedback = 1
            and scon_homework_picture.is_mark in (0, 2)
        </if>
    </select>

    <select id="findHomeworkIdYesterdayByClass" resultType="java.lang.String">
        select distinct scon_homework_picture.homework_id
        from tb_class_student,
        scon_homework_picture
        where tb_class_student.class_id = #{classId}
        and tb_class_student.status = 1
        and tb_class_student.vip_id = scon_homework_picture.vip_id
        and scon_homework_picture.create_time > #{beginDateTimeStr}
        and #{endDateTimeStr} > scon_homework_picture.create_time
        and scon_homework_picture.subject in
        <foreach collection="subjectList" open="(" item="subject" separator="," close=")">
            #{subject}
        </foreach>
        and scon_homework_picture.is_feedback = 1
        and scon_homework_picture.is_mark in (0, 2)
    </select>

    <select id="findTypicalWrongQuestionList"
            resultType="com.ssj.service.conch.teacher.library.dto.TypicalWrongQuestionDTO">
        select kmt_homework_wrong_question.question_id,
        kmt_exercise_book_course.book_name,
        kmt_exercise_book_course.unit,
        kmt_exercise_book_course.period,
        kmt_exercise_questions.big_question_index bigIndex,
        kmt_exercise_questions.small_question_name smallIndex,
        count(kmt_homework_wrong_question.homework_id) studentCount
        from kmt_homework_wrong_question,
        kmt_exercise_questions,
        kmt_exercise_book_course
        where kmt_homework_wrong_question.homework_id in
        <foreach collection="homeworkIdList" open="(" item="homeworkId" separator="," close=")">
            #{homeworkId}
        </foreach>
        and kmt_homework_wrong_question.type = 1
        and kmt_homework_wrong_question.question_id = kmt_exercise_questions.id
        and kmt_exercise_questions.book_course_id = kmt_exercise_book_course.id
        group by kmt_homework_wrong_question.question_id
    </select>

    <select id="countHomeworkDataByGrade" resultType="java.util.Map">
        select sum(IF(type = 2, 1, 0)) rightCount, count(DISTINCT homework_id) totalCount
        from kmt_homework_wrong_question, scon_homework
        where kmt_homework_wrong_question.homework_id = scon_homework.id
          and kmt_homework_wrong_question.question_id = #{questionId}
          and scon_homework.grade = #{grade}
          and scon_homework.create_time > #{beginDateTimeStr}
          and #{endDateTimeStr} > scon_homework.create_time
    </select>

    <select id="findDependentKnowledge" resultType="com.ssj.service.conch.teacher.library.dto.KnowledgeDTO">
        select zyb_know_how_details_sort.alias name
        from kmt_exercise_question_know, zyb_know_how_details_sort
        where kmt_exercise_question_know.how_details_id = zyb_know_how_details_sort.how_details_id
          and zyb_know_how_details_sort.stat = 2
          and kmt_exercise_question_know.question_id = #{questionId}
        group by kmt_exercise_question_know.how_details_id
    </select>

    <select id="findSubmittedStudentList" resultType="com.ssj.service.conch.teacher.library.dto.StudentDTOV2">
        select tb_lib_vip.img_url photo,
        tb_lib_vip.child_name name,
        picture.vip_id,
        time
        from (select scon_homework_picture.vip_id, max(scon_homework_picture.create_time) time
        from (select vip_id
        from tb_class_student
        where class_id = #{classId}
        and status = 1) student
        join scon_homework_picture on student.vip_id = scon_homework_picture.vip_id and
        scon_homework_picture.create_time between #{beginDateTimeStr} and #{endDateTimeStr} and
        scon_homework_picture.subject in
        <foreach collection="subjectList" open="(" item="subject" separator="," close=")">
            #{subject}
        </foreach>
        group by scon_homework_picture.vip_id
        order by time desc) picture
        join tb_lib_vip on picture.vip_id = tb_lib_vip.id
    </select>

    <select id="findWrongQuestionDetail" resultType="com.ssj.service.conch.teacher.library.dto.WrongQuestionDetailDTO">
        select ifnull(classStudentPicture.corrected_picture_url, classStudentPicture.file_key) homeworkPictureUrl,
               tb_lib_vip.img_url                                                              studentPhoto,
               tb_lib_vip.child_name                                                           studentName
        from (select picture.*
              from (select scon_homework_picture.corrected_picture_url,
                           scon_homework_picture.file_key,
                           scon_homework_picture.vip_id
                    from (select homework_picture_id
                          from kmt_homework_wrong_question
                          where question_id = #{questionId}) wrongQuestion
                             join scon_homework_picture on wrongQuestion.homework_picture_id = scon_homework_picture.id and
                                                           scon_homework_picture.create_time between #{beginDateTimeStr} and #{endDateTimeStr}) picture
                       join tb_class_student on picture.vip_id = tb_class_student.vip_id and tb_class_student.status = 1 and
                                                tb_class_student.class_id = #{classId}) classStudentPicture
                 join tb_lib_vip on classStudentPicture.vip_id = tb_lib_vip.id
    </select>

    <select id="findClassList" resultType="com.ssj.service.conch.teacher.v2.dto.ClassDto">
        select tb_class.id                                  classId,
               tb_class.grade                               grade,
               tb_class.class_name                          className
         from  tb_class_teacher left join
               tb_class on tb_class_teacher.class_id=tb_class.id
         where  tb_class_teacher.status=1
         and   tb_class.status in(1,2)
         and   tb_class_teacher.teacher_id=#{teacherId}
         order by tb_class.create_time
    </select>

    <select id="findNewClassList" resultType="com.ssj.service.conch.teacher.v2.dto.NewClassDto">
        select ct.class_id, c.class_name, lj.league_name
        from tb_class_teacher ct
                 join tb_lib_manager lm
                      on ct.teacher_id = lm.user_id and lm.type = 10 and ct.lib_id = lm.lib_id and lm.state = 1
                 join tb_class c on ct.class_id = c.id and c.status in (1, 2)
                 join tb_lib_join lj on ct.lib_id = lj.id
        where ct.teacher_id = #{teacherId}
          and ct.status = 1
        order by c.create_time
    </select>

    <select id="findStudentList" resultType="com.ssj.service.conch.teacher.v2.dto.ClassStudentDto">
        select  t2.img_url     photo,
                t2.child_name   name,
                t2.school       school,
                t2.grade        grade,
                t2.id       vipId
         from   tb_class_student t1  join
                tb_lib_vip       t2  on t1.vip_id=t2.id  and   t2.stat=1
         where  t1.vip_id=t2.id
          and   t1.status=1
          and   t1.class_id = #{classId}


    </select>

    <select id="countAllByClassId" resultType="java.lang.Integer">
        select count(distinct vip_id) from tb_class_student where class_id = #{classId} and status = 1
    </select>

    <select id="fingClassTeacherList" resultType="com.ssj.bean.mybatis.ManagerMBDto" parameterType="java.util.Map">
        select t.user_id userId,
        t.course_name courseName,
        t.name,
        t.t_photo tPhoto,
        t.telephone,
        sum(t.type) typeSum
        from tb_lib_manager t
        where t.type in (2, 10, 15)
        and t.state = 1
        <!--在本班的老师-->
        <if test="map.classId != null and map.classId != '' ">
            and exists(select 1 from tb_class_teacher where t.user_id=teacher_id and status=1 and
            class_id=#{map.classId})
        </if>
        <!--不在此班级的其他老师-->
        <if test="map.notThisClassId != null and map.notThisClassId != '' ">
            and t.user_id not in (select teacher_id from tb_class_teacher where class_id = #{map.notThisClassId} and
            status = 1)
        </if>
        <if test="map.libId != null and map.libId != '' ">
            and t.lib_id=#{map.libId}
        </if>
        group by t.user_id
        order by t.name
        <if test="map.nolimit != null and map.nolimit != '' ">
            limit 1000
        </if>
    </select>

    <select id="findFeedbackHistory" resultMap="FeedbackDTOResultMap">
        select push.*, scon_homework_picture.file_key
        from (select id        push_id,
                     vip_id,
                     suggest,
                     push_time push_time
              from zyb_student_push
              where vip_id = #{vipId}
                and #{endDate} > push_time
                and type > 0
              order by push_time desc
              limit #{offset}, #{pageSize}) push
                 left join scon_homework_picture
                           on push.vip_id = scon_homework_picture.vip_id
                               and to_days(push.push_time) = to_days(scon_homework_picture.create_time)
                               and scon_homework_picture.is_delete = 0
        order by push.push_time desc, field(scon_homework_picture.subject, '语文', '数学', '英语')
    </select>

    <select id="countFeedbackHistory" resultType="java.lang.Integer">
        select count(*)
        from zyb_student_push
        where vip_id = #{vipId}
          and #{endDate} > push_time
              and type > 0
    </select>
</mapper>