<?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.HomeworkMapper">
    <update id="deleteWrongQuestionKnowledgeCount">
        update scon_knowledge_wrong_question_count
        set mastered = 1
        where homework_id = #{homeworkId}
          and how_detail_id = #{knowHowDetailId}
          and knowledge_version = #{knowledgeVersion}
    </update>

    <update id="updateNeedUpdateWrongQuestionKnowledgeCount">
        update scon_homework set scon_homework.need_update_wrong_question_knowledge_count = #{newNeedUpdateWrongQuestionKnowledgeCount}
        where id in
          <foreach collection="homeworkIdSet" open="(" item="homeworkId" separator="," close=")">
              #{homeworkId}
          </foreach>
    </update>

    <delete id="deleteWrongQuestionKnowledgeCountByHomeworkId">
        delete kwqc from scon_knowledge_wrong_question_count kwqc
        where kwqc.homework_id = #{homeworkId}
    </delete>

    <delete id="deleteWrongQuestionKnowledgeCountWhichNeedUpdate">
        delete kwqc
        from scon_knowledge_wrong_question_count kwqc, scon_homework h
        where kwqc.homework_id = h.id and h.need_update_wrong_question_knowledge_count = 1
    </delete>

    <select id="findMinCreateTime" resultType="java.util.Date">
        select min(create_time) from scon_homework
    </select>

    <select id="countHomeworkData" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select scon_homework.id                                                                               homework_id,
               scon_homework.vip_id,
               scon_homework.grade,
               scon_homework.subject,
               scon_homework.used_time,
               scon_homework.create_time                                                                      homework_time,
               sum(if(scon_homework_how_details.id is not null and scon_homework_how_details.wrong_question_id is null,
                      ifnull(scon_homework_how_details.detail_count, 0), 0)) /
               if(count(distinct kmt_homework_wrong_question.id) = 0, 1, count(distinct kmt_homework_wrong_question.id)) +
               count(distinct if(kmt_homework_wrong_question.type = 1, kmt_homework_wrong_question.id, null)) wrong_count,
               sum(if(scon_homework_how_details.id is not null and scon_homework_how_details.wrong_question_id is null,
                      ifnull(scon_homework_how_details.total_count, 1), 0)) /
               if(count(distinct kmt_homework_wrong_question.id) = 0, 1, count(distinct kmt_homework_wrong_question.id)) +
               count(distinct kmt_homework_wrong_question.id)                                                 total_count
        from scon_homework
                 left join scon_homework_how_details on scon_homework_how_details.homework_id = scon_homework.id
                 left join kmt_homework_wrong_question on kmt_homework_wrong_question.homework_id = scon_homework.id and
                                                          kmt_homework_wrong_question.is_delete = 0
        where scon_homework.create_time between #{beginDateTimeStr} and #{endDateTimeStr}
        group by scon_homework.create_time, scon_homework.id
    </select>

    <select id="countHomeworkDataByHomeworkId" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select scon_homework.id                                                                               homework_id,
               scon_homework.vip_id,
               scon_homework.grade,
               scon_homework.subject,
               scon_homework.used_time,
               scon_homework.create_time                                                                      homework_time,
               sum(if(scon_homework_how_details.id is not null and scon_homework_how_details.wrong_question_id is null,
                      ifnull(scon_homework_how_details.detail_count, 0), 0)) /
               if(count(distinct kmt_homework_wrong_question.id) = 0, 1, count(distinct kmt_homework_wrong_question.id)) +
               count(distinct if(kmt_homework_wrong_question.type = 1, kmt_homework_wrong_question.id, null)) wrong_count,
               sum(if(scon_homework_how_details.id is not null and scon_homework_how_details.wrong_question_id is null,
                      ifnull(scon_homework_how_details.total_count, 1), 0)) /
               if(count(distinct kmt_homework_wrong_question.id) = 0, 1, count(distinct kmt_homework_wrong_question.id)) +
               count(distinct kmt_homework_wrong_question.id)                                                 total_count
        from scon_homework
                 left join scon_homework_how_details on scon_homework_how_details.homework_id = scon_homework.id
                 left join kmt_homework_wrong_question on kmt_homework_wrong_question.homework_id = scon_homework.id and
                                                          kmt_homework_wrong_question.is_delete = 0
        where scon_homework.id = #{homeworkId}
        group by scon_homework.id
    </select>

    <select id="findHomeworkAccuracyRateList" resultType="com.ssj.service.conch.parents.dto.HomeworkAccuracyRateDto">
        select scon_homework_count.homework_time date,
               scon_homework_count.accuracy_rate rate,
               scon_homework_count.wrong_count,
               scon_homework_count.total_count
        from scon_homework_count
        where scon_homework_count.vip_id = #{vipId}
          and scon_homework_count.homework_time > #{beginTimeStr}
          and #{endTimeStr} > scon_homework_count.homework_time
    </select>

    <!--查询近七天的作业记录-->
    <select id="findConchHomeworkRecord" resultType="com.ssj.bean.conch.conch.domain.HomeworkRecord">
        select homework_id, subject, date(create_time) date, sum(if(is_feedback = 0 and is_mark in (0, 2), 1, 0)) not_corrected_count,
               if(count(if(is_feedback = 1 or is_mark = 1 or is_mark = 3, 1, 0)) = sum(if(is_feedback = 1 or is_mark = 1 or is_mark = 3, 1, 0)), 5, 0) type,
               min(file_key) homework_picture_url
        from scon_homework_picture where vip_id=#{vipId}
            and create_time between #{beginTime} and #{endTime}
            and is_delete = 0
        group by homework_id, subject, date
        order by date desc
    </select>

    <!--查询某个作业记录-->
    <select id="findOneConchHomeworkRecord" resultType="com.ssj.bean.conch.conch.domain.HomeworkRecord">
        select homework_id, subject, date(create_time) date,
               if(count(if(is_feedback = 1 or is_mark = 1 or is_mark = 3, 1, 0)) = sum(if(is_feedback = 1 or is_mark = 1 or is_mark = 3, 1, 0)), 5, 0) type,
               min(file_key) homework_picture_url
        from scon_homework_picture where homework_id=#{homeworkId}
        group by homework_id, subject, date
    </select>

    <!--查询近两天待处理记录-->
    <select id="findConchHandleTask" resultType="com.ssj.bean.conch.conch.domain.HomeworkRecord">
        select id, file_key homework_picture_url, subject, homework_id, date(create_time) date, is_mark type from scon_homework_picture
        where create_time between  #{beginTime} and #{endTime} and vip_id=#{vipId} and is_mark in (1, 3)
    </select>

    <select id="countMonthlyHomeworkData" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select vip_id,
               homework_id,
               grade,
               ifnull(sum(used_time), 0)                              used_time,
               count(distinct date_format(homework_time, '%Y-%m-%d')) homework_days,
               ifnull(sum(wrong_count), 0)                            wrong_count,
               ifnull(sum(total_count), 0)                            total_count
        from scon_homework_count
        where data_type = 1
          and homework_time > #{beginDateTimeStr}
          and #{endDateTimeStr} > homework_time
        group by vip_id, grade
    </select>

    <select id="findByVipIdAndMonth" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select * from scon_homework_count where vip_id = #{vipId} and grade = #{grade} and data_type = 2 and homework_month = #{month}
    </select>

    <select id="findMinHomeworkMonth" resultType="java.lang.String">
        select homework_month from scon_homework_count where data_type = 1 order by homework_time limit 1
    </select>

    <select id="findVipIdNeedPushWeeklyNotify" resultType="java.lang.String">
        select vip_id
        from scon_homework_count
        where homework_time > #{beginDateTimeStr}
          and #{endDateTimeStr} > homework_time
          and data_type = 1
        group by vip_id
    </select>

    <select id="findVipIdNeedPushMonthlyNotify" resultType="java.lang.String">
        select vip_id from scon_homework_count where data_type = 2 and homework_month = #{month}
    </select>

    <select id="findByVipId" resultType="com.ssj.service.conch.conch.dto.RankDTO">
        select user_name, img_url, ranking_num, rowno as `index`, vip_id
        from (select a.child_name      as     user_name,
                     a.img_url,
                     a.vip_id,
                     a.ranking_num,
                     (@rowno := @rowno + 1) as rowno
              from (select tb_lib_vip.child_name,
                           tb_lib_vip.img_url,
                           tb_lib_vip.id vip_id,
                           scon_homework_ranking.ranking_num,
                           scon_homework_ranking.update_time
                    from scon_homework_ranking,
                         tb_lib_vip
                    where scon_homework_ranking.vip_id = tb_lib_vip.id) as a,
                   (select @rowno := 0) as row
              order by a.ranking_num desc, a.update_time desc) as temp
        where temp.vip_id = #{vipId}
    </select>

    <!--查询批改老师作业列表的质检中状态-->
    <select id="findHomeworkCheckStatus" resultType="com.ssj.bean.conch.conch.dto.HomeworkCheckStatusDto">
        select hpc.homework_id, count(hpc.check_start_time) count_check_start_time, count(hpc.check_end_time) count_check_end_time, count(1) total_count
        from scon_homework_picture_correct hpc, scon_homework_picture hp where hpc.homework_picture_id=hp.id
        <if test="type == 8">
            and hpc.type=8 and hp.teacher_id=#{teacherId}
        </if>
        <if test="type == 13">
            and hpc.type=13 and hp.know_teacher_id=#{teacherId}
        </if>
        and hp.homework_id in
        <foreach collection="homeworkIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        group by hpc.homework_id
    </select>

    <select id="countWeeklyHomeworkData" resultType="com.ssj.bean.sys.homework.domain.StudentWeeklyReport">
        select vip_id, subject, sum(wrong_count) wrong_count, sum(total_count) total_count
        from scon_homework_count
        where data_type = 1
          and homework_time > #{beginDateTime}
          and #{endDateTime} > homework_time
        group by vip_id, subject
    </select>

    <select id="findWeeklyReportHistory" resultType="com.ssj.service.conch.parents.v2.dto.StudentWeeklyReportDTO">
        select vip_id,
               group_concat(subject)              subject_str,
               group_concat(accuracy_rate)        accuracy_rate_str,
               sum(total_knowledge_num)           total_knowledge_num,
               sum(master_well_knowledge_num)     master_well_knowledge_num,
               sum(master_infirmly_knowledge_num) master_infirmly_knowledge_num,
               sum(not_mastered_knowledge_num)    not_mastered_knowledge_num,
               begin_date begin_time,
               end_date end_time
        from scon_student_weekly_report
        where vip_id = #{vipId}
        group by vip_id, begin_date
        order by begin_date desc
        limit 30
    </select>

    <select id="findMonthlyReportHistory" resultType="com.ssj.service.conch.parents.v2.dto.MonthlyReportDTO">
        select accuracy_rate learning_score,
               homework_month,
               transcended_student_proportion,
               increased_score
        from scon_homework_count
        where data_type = 2
          and vip_id = #{vipId}
        order by homework_month desc
        limit 12
    </select>

    <select id="countWrongQuestion" resultType="java.lang.Integer">
        select count(*)
        from scon_homework_how_details, scon_homework
        where scon_homework_how_details.homework_id = scon_homework.id
          and scon_homework_how_details.is_wrong_ques_delete = 0
          <if test="type == 2">
              and scon_homework_how_details.wrong_question_id is not null
          </if>
          <if test="type != 2">
              and scon_homework_how_details.detail_count > 0
          </if>
          and scon_homework_how_details.how_detail_id in
          <foreach collection="knowledgeIdList" open="(" item="knowledgeId" separator="," close=")">
              #{knowledgeId}
          </foreach>
          and knowledge_version = #{version}
          and scon_homework.vip_id = #{vipId}
          <if test="beginDateTimeStr != ''">
              and scon_homework.create_time > #{beginDateTimeStr}
          </if>
          <if test="endDateTimeStr != ''">
              and #{endDateTimeStr} > scon_homework.create_time
          </if>
    </select>

    <select id="findWrongQuestionId" resultType="java.lang.String">
        <foreach collection="knowHowDetailList" open="" item="knowHowDetail" separator=" union " close="">
            select distinct kmt_homework_wrong_question.question_id
            from scon_homework,
                 scon_homework_how_details,
                 kmt_homework_wrong_question
            where scon_homework.id = scon_homework_how_details.homework_id
              and scon_homework_how_details.wrong_question_id = kmt_homework_wrong_question.id
              and kmt_homework_wrong_question.is_delete = 0
              and scon_homework.vip_id = #{vipId}
              <if test="beginDateTimeStr != ''">
                  and scon_homework.create_time > #{beginDateTimeStr}
              </if>
              <if test="endDateTimeStr != ''">
                  and #{endDateTimeStr} > scon_homework.create_time
              </if>
              and scon_homework_how_details.how_detail_id = #{knowHowDetail.knowHowDetailId}
              and scon_homework_how_details.knowledge_version = #{knowHowDetail.knowledgeVersion}
        </foreach>
    </select>

    <select id="findWeeklyReportByVipIdAndSubjectAndBeginDate" resultType="com.ssj.bean.sys.homework.domain.StudentWeeklyReport">
        select *
        from scon_student_weekly_report
        where vip_id = #{vipId}
            and subject = #{subject}
            and begin_date = #{beginDate}
    </select>

    <select id="findWrongQuestionHowDetailKdId" resultType="java.lang.String">
        select kmt_exercise_question_know.how_details_kd_id
        from kmt_exercise_question_know
                 inner join (select kmt_homework_wrong_question.question_id
                             from scon_homework,
                                  scon_homework_how_details,
                                  kmt_homework_wrong_question
                             where scon_homework.id = scon_homework_how_details.homework_id
                               and scon_homework_how_details.wrong_question_id = kmt_homework_wrong_question.id
                               and scon_homework.vip_id = #{vipId}
                               and scon_homework.create_time between #{beginTime} and #{endTime}
                               and scon_homework_how_details.how_detail_id in
                               <foreach collection="knowHowDetailIdSet" open="(" item="knowHowDetailId" separator="," close=")">
                                   #{knowHowDetailId}
                               </foreach>
                               ) temp
                            on kmt_exercise_question_know.question_id = temp.question_id
                                and how_details_kd_id is not null
    </select>

    <select id="findMonthlyReport" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select * from scon_homework_count where data_type = 2
    </select>

    <select id="countWrongQuestionGroupByKnowledge" resultType="com.ssj.bean.sys.homework.domain.KnowledgeWrongQuestionCount">
        select homework.id homework_id,
               homework.vip_id,
               homework.subject,
               homework.create_time homework_time,
               scon_homework_how_details.how_detail_id,
               scon_homework_how_details.knowledge_version,
               scon_homework_how_details.grade,
               scon_homework_how_details.semester,
               zyb_know_how_details_sort.module,
               zyb_know_how_details_sort.two_module,
               zyb_know_how_details_sort.alias knowledge_name,
               count(distinct scon_homework_how_details.homework_picture_id) wrong_count,
               count(distinct scon_homework_how_details.wrong_question_id) question_status,
               scon_homework_how_details.is_wrong_ques_delete mastered
        from (select id, vip_id, subject, create_time
              from scon_homework
              where
                <choose>
                    <when test="homeworkId != null and homeworkId != ''">
                        id = #{homeworkId}
                        limit 1
                    </when>
                    <otherwise>
                        need_update_wrong_question_knowledge_count = 1
                        limit #{pageSize}
                    </otherwise>
                </choose>
              ) homework
                 join scon_homework_how_details on homework.id = scon_homework_how_details.homework_id
                 join zyb_know_how_details_sort
                     on scon_homework_how_details.how_detail_id = zyb_know_how_details_sort.how_details_id
                        and scon_homework_how_details.grade = zyb_know_how_details_sort.grade
                        and scon_homework_how_details.semester = zyb_know_how_details_sort.semester
                        and scon_homework_how_details.knowledge_version = zyb_know_how_details_sort.type
                        and zyb_know_how_details_sort.stat = 2
                        and zyb_know_how_details_sort.lib_id = 'SSJ'
        where scon_homework_how_details.detail_count > 0
        group by homework.vip_id, scon_homework_how_details.how_detail_id, scon_homework_how_details.knowledge_version,
                 scon_homework_how_details.grade,
                 scon_homework_how_details.semester
    </select>

    <!--单科批改详情作业图片列表-->
    <select id="findHomeworkCorrectDetailByHomeworkId" resultType="com.ssj.bean.sys.homework.domain.HomeworkPicture">
        select id, file_key, is_feedback, is_mark, teacher_id, corrected_picture_url, is_composition,homework_comment, create_time from scon_homework_picture
        where homework_id=#{homeworkId} AND is_delete=0 ORDER BY create_time ASC
    </select>

    <select id="countOthersAndAccuracyRateLessThan" resultType="java.util.Map">
        select sum(if(#{accuracyRate} > others.accuracy_rate, 1, 0)) accuracyRateLessThanCount, count(*) othersCount
        from scon_homework_count others
        where others.homework_month = #{targetMonthStr}
          and others.data_type = 2
          and others.vip_id &lt;> #{vipId}
          and others.grade = #{grade}
    </select>

    <select id="findMonthlyReportByMonth" resultType="com.ssj.bean.sys.homework.domain.HomeworkCount">
        select * from scon_homework_count where data_type = 2
            <if test="targetMonthStr != null and targetMonthStr.length > 0">
                and homework_month = #{targetMonthStr}
            </if>
    </select>

    <select id="findWrongQuestionKnowledgeCount" resultType="com.ssj.bean.sys.homework.domain.KnowledgeWrongQuestionCount">
        select * from scon_knowledge_wrong_question_count where 1 > question_status
    </select>

    <select id="findHomeworkInfoYesterday" resultType="java.util.Map">
        select homework.vip_id, homework.subject, homework.grade
        from (select vip_id, subject, grade
              from scon_homework
              where scon_homework.create_time > #{yesterdayDate}) homework
                 join tb_lib_vip on homework.vip_id = tb_lib_vip.id and tb_lib_vip.stat = '1'
    </select>

    <!--查询没有作业采集到的知识点-->
    <select id="findNoLearnKnowledgeList" resultType="java.lang.String">
        select detail.how_details_id from scon_homework_how_details t1
            left join scon_homework h on t1.homework_id=h.id
            right join zyb_know_how_details_sort detail
            on t1.how_detail_id=detail.how_details_id and t1.grade=detail.grade
            and t1.semester=2 and t1.knowledge_version=detail.type and h.vip_id=#{vipId}
        where detail.stat=2 and detail.grade=#{grade} and detail.course_name=#{subject} and detail.semester=#{semester}
         and detail.type=#{versionType} and t1.id is null
    </select>

    <select id="findStudentTermTrainDetailIdByVipId" resultType="com.ssj.bean.conch.conch.domain.SconStudentTermtainData">
        select * from scon_student_termtain_data where vip_id=#{vipId} and type=#{type} and storage_data_type = 0
    </select>

</mapper>