<?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.KmtExerciseMapper">
    <resultMap id="DistributionExerciseBookDTOResultMap" type="com.ssj.service.kmt.exercisesys.dto.DistributionExerciseBookDTO">
        <id column="exerciseBookId" property="exerciseBookId"/>
        <result column="importDate" property="importDate"/>
        <result column="subject" property="subject"/>
        <result column="grade" property="grade"/>
        <result column="version_name" property="versionName"/>
        <result column="semester" property="semester"/>
        <result column="book_name" property="bookName"/>
        <result column="book_type" property="bookType"/>
        <result column="unitNum" property="unitNum"/>
        <result column="courseNum" property="courseNum"/>
        <result column="handleAccuracy" property="handleAccuracy"/>
        <result column="handleAuditAccuracy" property="handleAuditAccuracy"/>
        <result column="analysisAccuracy" property="analysisAccuracy"/>
        <collection property="handleUserList" ofType="com.ssj.service.kmt.exercisesys.dto.DistributionTeacherDTO">
            <result column="handlerTeacherId" property="teacherId"/>
            <result column="handlerSalaryGiven" property="salaryGiven"/>
            <result column="handlerSalaryGivenTime" property="salaryGivenTime"/>
            <result column="handlerName" property="teacherName"/>
            <result column="handlerDistributeTime" property="createTime"/>
        </collection>
        <collection property="auditingUserList" ofType="com.ssj.service.kmt.exercisesys.dto.DistributionTeacherDTO">
            <result column="handlerAuditUserTeacherId" property="teacherId"/>
            <result column="handlerAuditUserSalaryGiven" property="salaryGiven"/>
            <result column="handlerAuditUserSalaryGivenTime" property="salaryGivenTime"/>
            <result column="handlerAuditUserName" property="teacherName"/>
            <result column="handlerAuditUserDistributeTime" property="createTime"/>
        </collection>
        <collection property="analysisUserList" ofType="com.ssj.service.kmt.exercisesys.dto.DistributionTeacherDTO">
            <result column="analyserTeacherId" property="teacherId"/>
            <result column="analyserSalaryGiven" property="salaryGiven"/>
            <result column="analyserSalaryGivenTime" property="salaryGivenTime"/>
            <result column="analyserName" property="teacherName"/>
            <result column="analyserDistributeTime" property="createTime"/>
        </collection>
        <collection property="analysisAuditerList" ofType="com.ssj.service.kmt.exercisesys.dto.DistributionTeacherDTO">
            <result column="analysisAuditUserTeacherId" property="teacherId"/>
            <result column="analysisAuditUserSalaryGiven" property="salaryGiven"/>
            <result column="analysisAuditUserSalaryGivenTime" property="salaryGivenTime"/>
            <result column="analysisAuditUserName" property="teacherName"/>
            <result column="analysisAuditUserDistributeTime" property="createTime"/>
        </collection>
    </resultMap>

    <resultMap id="ExportDistributionExerciseBookDTOResultMap" type="com.ssj.service.kmt.exercisesys.dto.ExportDistributionExerciseBookDTO">
        <id column="exercise_book_id" property="exerciseBookId"/>
        <result column="import_date" property="importDate"/>
        <result column="subject" property="subject"/>
        <result column="grade" property="grade"/>
        <result column="version_name" property="versionName"/>
        <result column="semester" property="semester"/>
        <result column="book_name" property="bookName"/>
        <result column="book_type" property="bookType"/>
        <result column="unit_count" property="unitNum"/>
        <result column="course_count" property="courseNum"/>

    </resultMap>

    <select id="findWrongQuestionKnowHowDetailIdList" resultType="java.lang.String">
        select kmt_exercise_question_know.how_details_id
        from kmt_exercise_question_know
        where kmt_exercise_question_know.question_id = #{questionId}
        union all
        select kmt_exercise_question_know.how_details_id
        from kmt_exercise_question_know,
             kmt_exercise_questions brother,
             kmt_exercise_questions
        where kmt_exercise_question_know.question_id = brother.id
          and brother.parent_id = kmt_exercise_questions.parent_id
          and kmt_exercise_questions.id = #{questionId}
          and ifnull(length(kmt_exercise_questions.parent_id), 0) > 0
    </select>

    <select id="findTempRecommendQuestion" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
        (select kmt_exercise_questions.id,
                kmt_exercise_questions.parent_id,
                kmt_exercise_questions.question,
                kmt_exercise_questions.num,
                temp_question.same_how_sum,
                temp_question.all_how_sum
         from kmt_exercise_questions,
              (select kmt_exercise_question_know.question_id,
                      count(distinct kmt_exercise_question_know.how_details_id)         same_how_sum,
                      (select count(distinct temp.how_details_id)
                       from kmt_exercise_question_know temp
                       where temp.question_id = kmt_exercise_question_know.question_id) all_how_sum
               from kmt_exercise_question_know
               where kmt_exercise_question_know.how_details_id in
                 <foreach collection="wrongQuestionKnowHowDetailIdList" open="(" item="howDetailId" separator="," close=")">
                     #{howDetailId}
                 </foreach>
                 and kmt_exercise_question_know.question_id not in
                 <foreach collection="needExcludedQuestionIdSet" open="(" item="questionId" separator="," close=")">
                     #{questionId}
                 </foreach>
                 and ifnull(length(kmt_exercise_question_know.question_parent_id), 0) > 0
               group by kmt_exercise_question_know.question_id
               order by same_how_sum desc, all_how_sum
               limit 2) temp_question
         where kmt_exercise_questions.id = temp_question.question_id)
        union all
        (select kmt_exercise_questions.id,
                kmt_exercise_questions.parent_id,
                kmt_exercise_questions.question,
                kmt_exercise_questions.num,
                temp_question.same_how_sum,
                temp_question.all_how_sum
         from kmt_exercise_questions,
              (select kmt_exercise_question_know.question_id,
                      count(distinct kmt_exercise_question_know.how_details_id)         same_how_sum,
                      (select count(distinct temp.how_details_id)
                       from kmt_exercise_question_know temp
                       where temp.question_id = kmt_exercise_question_know.question_id) all_how_sum
               from kmt_exercise_question_know
               where kmt_exercise_question_know.how_details_id in
                 <foreach collection="wrongQuestionKnowHowDetailIdList" open="(" item="howDetailId" separator="," close=")">
                     #{howDetailId}
                 </foreach>
                 and kmt_exercise_question_know.question_id not in
                 <foreach collection="needExcludedQuestionIdSet" open="(" item="questionId" separator="," close=")">
                     #{questionId}
                 </foreach>
                 and 1 > ifnull(length(kmt_exercise_question_know.question_parent_id), 0)
               group by kmt_exercise_question_know.question_id
               order by same_how_sum desc, all_how_sum
               limit 2) temp_question
         where kmt_exercise_questions.id = temp_question.question_id)
        order by same_how_sum desc, all_how_sum
        limit 2
    </select>

    <select id="findFullBigQuestionByParentId" resultType="com.ssj.service.kmt.dto.OriginalQuestionDTO">
        select bigQuestion.id, bigQuestion.parent_id, 1 as type, bigQuestion.question, bigQuestion.num, bigQuestion.book_id,
            question_type, difficulty_level
        from kmt_exercise_questions bigQuestion
        where bigQuestion.id in
        <foreach collection="parentQuestionIdList" open="(" item="parentQuestionId" separator="," close=")">
            #{parentQuestionId}
        </foreach>
        union all
        select smallQuestion.id, smallQuestion.parent_id, 1 as type, smallQuestion.question, smallQuestion.num, smallQuestion.book_id,
            question_type, difficulty_level
        from kmt_exercise_questions smallQuestion
        where smallQuestion.parent_id in
        <foreach collection="parentQuestionIdList" open="(" item="parentQuestionId" separator="," close=")">
            #{parentQuestionId}
        </foreach>
    </select>

    <!-- 查询题目列表 -->
    <select id="findKmtExerciseQuestionsByIdIn" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion"  >
        select eq.id, eq.book_id, eq.unit_id, eq.book_course_id,
        eb.auditing_user_id, eb.handle_user_id, eb.analysis_user_id, eb.analysis_auditer_id, eb.answer_correcter_id,
        eq.question_type, eq.source, eq.big_question_index, eq.analysis_submit_time, eq.page_index,
        eq.image, eq.precision_percent, eq.create_time, eq.deal_status, eq.remark, eq.analysis_submit_type,
        eq.answer_image, eq.num, eq.small_question_name,  eq.parent_id, eq.big_name, eq.real_exam_type, eq.publish_time,
        eq.update_type, eq.difficulty_level, eq.training_skill, eq.course_name, eq.is_common_flag, eq.question_year, eq.exercise_book_id, eq.es_loaded,
        eq.analysis_handler_submit_time, eq.question_class_type, eq.repulse_time,
        eq.proofread_status, eq.question_path
        from kmt_exercise_questions eq
        left join kmt_exercise_book eb on eq.exercise_book_id=eb.id where eq.id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        limit 0, 5000
    </select>

    <!-- 查询打回题目列表 -->
    <select id="findKmtExerciseQuestionRollbacks" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestionRollback">
        SELECT * FROM kmt_exercise_question_rollback where question_id in
        <foreach collection="questionIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and type=#{dealType}
    </select>

    <!-- 查询待加工审核的题目 -->
    <select id="findQuestionByBookCourseIdAndUserId" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eb.handle_user_id, eb.auditing_user_id, eb.analysis_user_id, eb.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason, eq.from_user_id, eq.to_user_id,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, eq.parent_id, eq.proofread_status, eq.question_path
        from kmt_exercise_questions eq  left join kmt_exercise_book eb on eq.exercise_book_id=eb.id
        where eq.book_course_id = #{bookCourseId} and (eq.exercise_book_id=#{exerciseBookId} or eq.is_common_flag = 0)
        <if test = "dealType == 2">
            and eq.deal_status in (3, 7)
        </if>
        <if test = "dealType == 1">
            and eq.deal_status &lt; 2
        </if>
        <if test = "dealType == 4">
            and eq.deal_status in (5, 8)
        </if>
        <if test = "dealType == 5">
            and eq.deal_status in (9, 10)
        </if>
        <if test = "dealType == 6">
            and eq.deal_status >= 4 and eq.deal_status != 7 and eb.answer_correcter_id=#{userId} and eq.proofread_status = 0
        </if>
        <if test = "dealType == 7">
            and eq.proofread_status > 0 and eb.answer_correcter_id=#{userId}
        </if>
        order by eq.num, eq.create_time
        limit 0, 5000
    </select>

    <!-- 查询待加工审核的题目 -->
    <select id="findQuestionByBookCourseId" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, eq.parent_id
        from kmt_exercise_questions eq
        where eq.book_course_id = #{bookCourseId}
        order by eq.num, eq.create_time
        limit 0, 5000
    </select>

    <!-- 查询打回理由记录 -->
    <select id="findQuestionRollbackRecord" resultType="com.ssj.service.kmt.dto.exercise.KmtExerciseQuestionRollBackInfo">
      select eqr.id, eqr.create_time, eqr.reason,eqr.user_id userId,eqr.to_user_id toUserId,user1.user_id tbUserId,user2.user_id tbToUserId, user1.name from_user, eqr.from_user_type from_type,user2.name to_user,
      eqr.to_user_type to_type from kmt_exercise_question_rollback eqr left join tb_lib_manager user1 on eqr.user_id = user1.user_id and user1.type in (17, 18)
      left join tb_lib_manager user2 on eqr.to_user_id = user2.user_id and user2.type in (17, 18)
      where eqr.question_id = #{questionId}
      order by eqr.create_time desc limit 20;
    </select>


    <!-- 查询打回理由记录老 -->
    <select id="findQuestionRollbackRecordOld" resultType="com.ssj.service.kmt.dto.exercise.ExerciseQuestionRollbacks">
    select eqr.id,eqr.create_time, eqr.reason, user1.name from_user, user1.type from_type,  user2.name to_user,
      user2.type to_type from kmt_exercise_question_rollback eqr left join kmt_sys_user user1 on eqr.user_id = user1.id
      left join kmt_sys_user user2 on eqr.to_user_id = user2.id
      where eqr.question_id = #{questionId} order by eqr.create_time desc limit 20;
    </select>

    <select id="countWrongAndTotal" resultType="java.util.Map">
        select sum(ifnull(scon_homework_how_details.detail_count, 0)) wrong_count,
               sum(ifnull(scon_homework_how_details.total_count, 1))  total_count
        from scon_homework_how_details,
             scon_homework
        where scon_homework_how_details.homework_id = scon_homework.id
          and scon_homework.vip_id = #{vipId}
          <if test="subject != null and subject != ''">
            and scon_homework.subject = #{subject}
          </if>
          and scon_homework.create_time >= #{beginDateTime}
          and  #{endDateTime} >= scon_homework.create_time
          and scon_homework_how_details.wrong_question_id is null
        union all
        select sum(if(kmt_homework_wrong_question.type = 1, 1, 0)) wrong_count, count(*) total_count
        from kmt_homework_wrong_question,
             scon_homework
        where kmt_homework_wrong_question.homework_id = scon_homework.id
          and scon_homework.vip_id = #{vipId}
          <if test="subject != null and subject != ''">
            and scon_homework.subject = #{subject}
          </if>
          and scon_homework.create_time >= #{beginDateTime}
          and  #{endDateTime} >= scon_homework.create_time
          and kmt_homework_wrong_question.is_delete = 0
    </select>

    <select id="countKnowledgeTimes" resultType="java.util.Map">
        (select zyb_know_how_details_sort.alias, count(*) count_num
         from scon_homework_how_details,
              scon_homework,
              zyb_know_how_details_sort
         where scon_homework_how_details.homework_id = scon_homework.id
           and scon_homework_how_details.how_detail_id = zyb_know_how_details_sort.how_details_id
           and scon_homework_how_details.knowledge_version = zyb_know_how_details_sort.type
           and zyb_know_how_details_sort.stat = 2
           and scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
                and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
           and scon_homework_how_details.wrong_question_id is null
         group by zyb_know_how_details_sort.alias)
        union all
        (select zyb_know_how_details_sort.alias, count(distinct scon_homework.id, zyb_know_how_details_sort.how_details_id) count_num
         from kmt_homework_wrong_question,
              scon_homework,
              kmt_exercise_question_know,
              zyb_know_how_details_sort
         where kmt_homework_wrong_question.homework_id = scon_homework.id
           and kmt_homework_wrong_question.question_id = kmt_exercise_question_know.question_id
           and kmt_exercise_question_know.how_details_id = zyb_know_how_details_sort.how_details_id
           and zyb_know_how_details_sort.type = kmt_exercise_question_know.version
           and zyb_know_how_details_sort.stat = 2
           and scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
                and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
           and kmt_homework_wrong_question.is_delete = 0
         group by zyb_know_how_details_sort.alias)
    </select>

    <select id="countKnowledgeMastery" resultType="java.util.Map">
        (select details_sort.alias,
                <if test="subject != null and subject != ''">
                    <if test="subject == '语文' || subject == '数学'">
                        details_sort.two_module module,
                    </if>
                    <if test="subject == '英语'">
                        details_sort.module module,
                    </if>
                </if>
                group_concat(how_sort.alias) how_name,
                sum(ifnull(scon_homework_how_details.detail_count, 0)) wrong_count,
                sum(ifnull(scon_homework_how_details.total_count, 1))  total_count,
                group_concat(details_sort.how_details_id) know_how_detail_id_str,
                group_concat(details_sort.type) knowledge_version_str
         from scon_homework_how_details,
              scon_homework,
              zyb_know_how_details_sort details_sort,
              zyb_know_how_details_sort how_sort
         where scon_homework_how_details.homework_id = scon_homework.id
           and scon_homework_how_details.how_detail_id = details_sort.how_details_id
           and scon_homework_how_details.knowledge_version = details_sort.type
           and details_sort.stat = 2
           and how_sort.how_id = details_sort.how_id
           and how_sort.stat = 1
           and how_sort.grade_num = details_sort.grade_num
           and how_sort.semester = details_sort.semester
           and scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
                and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
           and scon_homework_how_details.wrong_question_id is null
         group by details_sort.alias)
        union all
        (select details_sort.alias,
                <if test="subject != null and subject != ''">
                    <if test="subject == '语文' || subject == '数学'">
                        details_sort.two_module module,
                    </if>
                    <if test="subject == '英语'">
                        details_sort.module module,
                    </if>
                </if>
                group_concat(how_sort.alias) how_name,
                sum(if(kmt_homework_wrong_question.type = 1, 1, 0)) wrong_count,
                count(*)                                            total_count,
                group_concat(details_sort.how_details_id) know_how_detail_id_str,
                group_concat(details_sort.type) knowledge_version_str
         from kmt_homework_wrong_question,
              scon_homework,
              kmt_exercise_questions,
              kmt_exercise_book_course,
              kmt_exercise_question_know,
              zyb_know_how_details_sort details_sort,
              zyb_know_how_details_sort how_sort
         where kmt_homework_wrong_question.homework_id = scon_homework.id
           and kmt_homework_wrong_question.question_id = kmt_exercise_questions.id
           and kmt_exercise_questions.book_course_id = kmt_exercise_book_course.id
           and kmt_homework_wrong_question.question_id = kmt_exercise_question_know.question_id
           and kmt_exercise_question_know.how_details_id = details_sort.how_details_id
           and details_sort.type = kmt_exercise_book_course.version
           and details_sort.stat = 2
           and how_sort.how_id = details_sort.how_id
           and how_sort.stat = 1
           and how_sort.grade_num = details_sort.grade_num
           and how_sort.semester = details_sort.semester
           and scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
                and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
           and kmt_homework_wrong_question.is_delete = 0
         group by details_sort.alias)
    </select>

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

    <!--查询题目列表关联的知识点id列表-->
    <select id="findHowDetailIdsByQuestionIds" resultType="java.lang.String">
        select how_details_id from kmt_exercise_question_know where question_id in
        <foreach collection="questionIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
         group by how_details_id
    </select>

    <!--查询相似题-->
    <select id="findSimilarQuestionByIds" resultType="com.ssj.bean.conch.conch.dto.KmtExerciseQuestionProblemDto">
        select id question_id, question, answer_json, big_question_index from kmt_exercise_questions where id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        order by num
    </select>

    <select id="countKnowledgeMasteryV2" resultType="java.util.Map">
        (select scon_homework_how_details.how_detail_id,
                scon_homework_how_details.knowledge_version version,
                scon_homework_how_details.grade,
                scon_homework_how_details.semester,
                sum(ifnull(scon_homework_how_details.detail_count, 0)) wrong_count,
                sum(ifnull(scon_homework_how_details.total_count, 1))  total_count
         from scon_homework_how_details,
              scon_homework,
              zyb_know_how_details_sort
         where scon_homework_how_details.homework_id = scon_homework.id
               and 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 scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
               and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
           and scon_homework_how_details.wrong_question_id is null
         group by zyb_know_how_details_sort.alias)
        union all
        (select kmt_exercise_question_know.how_details_id how_detail_id,
                kmt_exercise_question_know.version,
                zyb_know_how_details_sort.grade,
                zyb_know_how_details_sort.semester,
                sum(if(kmt_homework_wrong_question.type = 1, 1, 0)) wrong_count,
                count(*)                                            total_count
         from kmt_homework_wrong_question,
             scon_homework,
             kmt_exercise_question_know,
             zyb_know_how_details_sort
         where kmt_homework_wrong_question.homework_id = scon_homework.id
           and kmt_homework_wrong_question.question_id = kmt_exercise_question_know.question_id
           and kmt_exercise_question_know.how_detail_sort_id = zyb_know_how_details_sort.id
           and scon_homework.vip_id = #{vipId}
           <if test="subject != null and subject != ''">
               and scon_homework.subject = #{subject}
           </if>
           and scon_homework.create_time > #{beginDateTimeStr}
           and #{endDateTimeStr} > scon_homework.create_time
         group by zyb_know_how_details_sort.alias)
    </select>

    <!--查询未解析的题目-->
    <select id="findUnAnalysisBookCourse" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBookCourse">
        select distinct t1.id, t1.book_id, t1.course_name, t1.version, t1.grade,
         t1.semester, t1.book_name, t1.unit, t1.period, t1.unit_id,
         t1.period_sort, t1.unit_sort from kmt_exercise_book_course t1 left join kmt_exercise_questions t2
         on t1.id = t2.book_course_id where t1.analysis_user_id=#{analysisUserId} and t2.analysis_submit_type=0
         order by t1.unit_sort, t1.period_sort limit 0, #{maxCount}
    </select>

    <select id="findData2EsByQuestionList" resultType="java.util.Map">
        select k.id,
               k.question_id,
               k.how_details_id,
               k.how_details_kd_id,
               k.how_detail_sort_id,
               k.how_detail_kd_sort_id,
               k.version,
               date_format(k.create_time, '%Y-%m-%dT%H:%i:%s.000Z')        create_time,
               book_id,
               unit_id,
               book_course_id,
               question_type,
               source,
               big_question_index,
               date_format(analysis_submit_time, '%Y-%m-%dT%H:%i:%s.000Z') analysis_submit_time,
               page_index,
               image,
               precision_percent,
               auditing_user_id,
               handle_user_id,
               deal_status,
               remark,
               analysis_submit_type,
               answer_image,
               t.num,
               small_question_name,
               ifnull(parent_id, '') parent_id,
               big_name,
               real_exam_type,
               date_format(publish_time, '%Y-%m-%dT%H:%i:%s.000Z')         publish_time,
               update_type,
               analysis_user_id,
               ifnull(difficulty_level, 0) difficulty_level,
               t.course_name,
               training_skill,
               concise_id,
               analysis_auditer_id,
               date_format(handler_submit_time, '%Y-%m-%dT%H:%i:%s.000Z')         handler_submit_time,
               date_format(analysis_handler_submit_time, '%Y-%m-%dT%H:%i:%s.000Z')         analysis_handler_submit_time,
               question_class_type,
               ifnull(khds.alias, '') knowledge_name,
               khds.course_name,
               khds.semester,
               khds.grade,
               date_format(t.repulse_time, '%Y-%m-%dT%H:%i:%s.000Z')        repulse_time,
               t.question_path
        from (select *
              from kmt_exercise_question_know
              where question_id in
                <foreach collection="questionIdList" open="(" item="questionId" separator="," close=")">
                    #{questionId}
                </foreach>
              limit 1000) k join kmt_exercise_questions t on k.question_id = t.id
                            left join zyb_know_how_details_sort khds on k.how_detail_sort_id = khds.id
    </select>

    <select id="findAll2Es" resultType="java.util.Map">
        select k.id,
               k.question_id,
               k.how_details_id,
               k.how_details_kd_id,
               k.how_detail_sort_id,
               k.how_detail_kd_sort_id,
               k.version,
               date_format(k.create_time, '%Y-%m-%dT%H:%i:%s.000Z')        create_time,
               book_id,
               unit_id,
               book_course_id,
               question_type,
               source,
               big_question_index,
               date_format(analysis_submit_time, '%Y-%m-%dT%H:%i:%s.000Z') analysis_submit_time,
               page_index,
               image,
               precision_percent,
               auditing_user_id,
               handle_user_id,
               deal_status,
               remark,
               analysis_submit_type,
               answer_image,
               t.num,
               small_question_name,
               ifnull(parent_id, '') parent_id,
               big_name,
               real_exam_type,
               date_format(publish_time, '%Y-%m-%dT%H:%i:%s.000Z')         publish_time,
               update_type,
               analysis_user_id,
               ifnull(difficulty_level, 0) difficulty_level,
               t.course_name,
               training_skill,
               concise_id,
               analysis_auditer_id,
               date_format(handler_submit_time, '%Y-%m-%dT%H:%i:%s.000Z')         handler_submit_time,
               date_format(analysis_handler_submit_time, '%Y-%m-%dT%H:%i:%s.000Z')         analysis_handler_submit_time,
               question_class_type,
               ifnull(khds.alias, '') knowledge_name,
               khds.course_name,
               khds.semester,
               khds.grade,
               date_format(t.repulse_time, '%Y-%m-%dT%H:%i:%s.000Z')        repulse_time,
               t.question_path
        from (select * from kmt_exercise_question_know where kmt_exercise_question_know.loaded = 0 limit #{pageSize}) k
                 join kmt_exercise_questions t on k.question_id = t.id
                 left join zyb_know_how_details_sort khds on k.how_detail_sort_id = khds.id
    </select>

    <select id="countQuestionByKnowledgeAndVersion" resultType="java.lang.Integer">
        select count(distinct question_id)
        from kmt_exercise_question_know
        where how_details_id in
        <foreach collection="knowledgeIdList" open="(" item="knowledgeId" separator="," close=")">
            #{knowledgeId}
        </foreach>
        and kmt_exercise_question_know.version = #{version}
    </select>

    <!--查询未加工练习册  where eb.had_handle = 0 and eb.handle_user_id=#{handleUserId}
            left join kmt_exercise_questions eq on eb.id=eq.book_id
        where eq.deal_status in (0, 1) and eb.handle_user_id=#{handleUserId}
        group by eb.id
    -->
    <select id="findUnHandleBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.id exercise_book_id, eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag != 3 and eb.had_handle=0 and  eb.handle_user_id=#{handleUserId}
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <!--查询未分配加工人练习册-->
    <select id="findUndistributedHandlerBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.book_id id, group_concat(distinct eb.book_year order by eb.book_year asc) book_year_list, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.part_flag
        from kmt_exercise_book eb
        where eb.course_name=#{subject} and eb.semester=#{semester} and eb.version=#{versionType} and eb.grade=#{grade}
        and eb.handle_user_id is null
        group by eb.book_id
    </select>

    <!--查询未审核练习册 where eb.had_auditing=0 and eb.auditing_user_id=#{auditingUserId}
    left join kmt_exercise_questions eq on eb.id=eq.book_id
        where eq.deal_status in (3, 7) and eb.auditing_user_id=#{auditingUserId}
        group  by eb.id
    -->
    <select id="findUnAuditingBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.id exercise_book_id, eb.book_id id,eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag != 3 and eb.had_auditing=0 and eb.auditing_user_id=#{auditingUserId}
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <!--查询未分配审核练习册-->
    <select id="findUndistributedAuditerBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.book_id id, group_concat(distinct eb.book_year order by eb.book_year asc) book_year_list, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.part_flag
        from kmt_exercise_book eb
        where eb.course_name=#{subject} and eb.semester=#{semester} and eb.version=#{versionType} and eb.grade=#{grade}
        and eb.auditing_user_id is null
        group by eb.book_id
    </select>

    <!--查询未加工解析的练习册-->
    <select id="findUnAnalysisHandleBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.id exercise_book_id, eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag != 3 and eb.analysis_handled=0 and eb.analysis_user_id=#{analysisUserId}
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <!--查询未审核解析的练习册-->
    <select id="findUnAnalysisAuditingBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.id exercise_book_id,  eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag != 3 and eb.analysis_audited=0 and eb.analysis_auditer_id=#{analysisAuditerId}
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <select id="findUnCorrectAnswerBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select  eb.id exerciseBookId, eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag !=3 and eb.answer_correcter_id=#{userId} and eb.answer_corrected_status = 0
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <select id="findCorrectedAnswerBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select  eb.id exerciseBookId, eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag !=3 and eb.answer_correcter_id=#{userId} and eb.proofread_status > 0
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <!--查询未校对的练习册-->
    <select id="findExerciseBookListByIds" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select  eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type
        from kmt_exercise_book eb
        where id in
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <!--查询所有练习册练习册-->
    <select id="findAllExerciseBooks" resultType="com.ssj.service.kmt.dto.ExerciseBookDto">
        select eb.id exercise_book_id, eb.book_id id, eb.book_year, eb.book_name name, eb.grade, eb.course_name subject, eb.version version_type, eb.semester, eb.book_type,eb.ai_rollback_import aiRollbackImport
        from kmt_exercise_book eb
        where eb.part_flag != 3 and eb.course_name=#{subject} and eb.semester=#{semester} and eb.version=#{versionType} and eb.grade=#{grade}
        <if test="bookName != null and bookName != ''">
            and eb.book_name like concat('%',#{bookName},'%')
        </if>
    </select>

    <!--查询待加工审核的练习册单元 -->
    <select id="findUnHandleExerciseBookCourse" resultType="com.ssj.service.kmt.dto.exercise.ExerciseUnitBookcourseDto">
        select ebc.unit_id id, ebc.unit name, ebc.id book_course_id, ebc.period book_course from kmt_exercise_book_course ebc
        left join kmt_exercise_questions eq on ebc.id=eq.book_course_id
        left join kmt_exercise_book eb on eq.exercise_book_id = eb.id
        where eq.exercise_book_id=#{bookId}
        <if test = "dealType == 1">
            and eq.deal_status in (0, 1) and eb.handle_user_id=#{userId}
        </if>
        <if test = "dealType == 2">
            and eq.deal_status in (3, 7) and eb.auditing_user_id=#{userId}
        </if>
        <if test = "dealType == 4">
            and eq.deal_status in (5, 8) and eb.analysis_user_id=#{userId}
        </if>
        <if test = "dealType == 5">
            and eq.deal_status in (9, 10) and eb.analysis_auditer_id=#{userId}
        </if>
        <if test = "dealType == 6">
            and eq.deal_status >= 4 and eq.deal_status != 7 and eb.answer_correcter_id=#{userId} and eq.proofread_status = 0
        </if>
        <if test = "dealType == 7">
            and eq.proofread_status > 0 and eb.answer_correcter_id=#{userId}
        </if>
        group by ebc.id
        order by ebc.period_sort

    </select>

    <!--查询待解析加工审核的练习册单元-->
    <select id="findAnalysisUnHandleExerciseBookCourse" resultType="com.ssj.service.kmt.dto.exercise.ExerciseUnitBookcourseDto">
        select ebu.id, ebu.unit name, ebc.id book_course_id, ebc.period book_course from kmt_exercise_book_unit ebu
        left join  kmt_exercise_book_course ebc on ebu.id=ebc.unit_id
        left join kmt_exercise_questions eq on ebc.id=eq.book_course_id
        where ebu.book_id=#{bookId}
        <if test = "dealType == 2">
            and ebc.had_handle = 0
        </if>
        <if test = "dealType == 1">
            and ebc.had_auditing = 0
        </if>
        <if test = "dealType == 4">
            and eq.deal_status in (5, 8)
        </if>
        <if test = "dealType == 5">
            and eq.deal_status in (9, 10)
        </if>
        group by ebc.id
        order by ebu.num, ebc.period_sort
    </select>

    <!--查询练习册下所有单元课时-->
    <select id="findAllExerciseBookCourse" resultType="com.ssj.service.kmt.dto.exercise.ExerciseUnitBookcourseDto">
        select ebc.unit_id id, ebc.unit name, ebc.id book_course_id, ebc.period book_course from kmt_exercise_book_course ebc
        where (ebc.book_id = #{bookId} and ebc.is_common_flag = 0) or (ebc.exercise_book_id=#{exerciseBookId} and ebc.is_common_flag = 1)
        order by ebc.period_sort
    </select>

    <!--查询练习册下所有单元课时-->
    <select id="findAllExerciseBookCourseByExerciseBookId" resultType="com.ssj.service.kmt.dto.exercise.ExerciseUnitBookcourseDto">
        select ebc.unit_id id, ebc.unit name, ebc.id book_course_id, ebc.period book_course from kmt_exercise_book_course ebc
        where ebc.exercise_book_id=#{exerciseBookId}
        order by ebc.period_sort
    </select>

    <!--查询未加工/未审核的题目数量-->
    <select id ="findUnHandleQuestionCount" resultType="java.lang.Integer">
        select count(1) from kmt_exercise_questions where book_course_id=#{bookCourseId}
        and id not in
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        <if test = "dealType == 2">
            and deal_status in (3, 7)
        </if>
        <if test = "dealType == 1">
            and deal_status &lt; 2
        </if>
    </select>

    <!--查询未加工/未审核的课时数量-->
    <select id="findUnHandleBookCourseCount" resultType="java.lang.Integer">
        select count(1) from kmt_exercise_book_course where unit_id=#{unitId}
        and id != #{bookCourseId}
        <if test = "dealType == 2">
            and had_auditing = 0
        </if>
        <if test = "dealType == 1">
            and had_handle = 0
        </if>
    </select>

    <!--查询未加工/未审核的单元数量-->
    <select id="findUnHandleUnitCount" resultType="java.lang.Integer">
        select count(1) from kmt_exercise_book_unit where book_id=#{bookId}
        and id != #{unitId}
        <if test = "dealType == 2">
            and had_auditing = 0
        </if>
        <if test = "dealType == 1">
            and had_handle = 0
        </if>
    </select>

    <!--查询搜索的题目-->
    <select id="findKmtQuestionsById" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.num small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, parent_id, image question_image, eq.update_type
        from kmt_exercise_questions eq
        where (eq.id=#{questionId} or eq.parent_id=#{questionId}) and eq.question_class_type in (0, 2) and eq.book_id is null
        <choose>
            <when test="dealType == 1">
                and eq.handle_user_id = #{userId} and eq.deal_status &lt; 2
            </when>
            <when test="dealType == 2">
                and eq.auditing_user_id = #{userId} and eq.deal_status in (3, 7)
            </when>
            <otherwise>
                and eq.deal_status != 6
            </otherwise>
        </choose>
        order by eq.num
    </select>

    <!--根据知识点分页查询题目-->
    <select id="findKmtQuestionByDetailId" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, parent_id, image question_image
        from kmt_exercise_questions eq
        left join kmt_exercise_question_know eqk on eq.id = eqk.question_id
        where eqk.how_detail_sort_id = #{howDetailsId}
        <if test = "howDetailsKdId != null and howDetailsKdId != ''">
          and eqk.how_detail_kd_sort_id= #{howDetailsKdId}
        </if>
        and eq.book_id is null  and (((eq.parent_id is null or eq.parent_id='') and not exists (select 1 from kmt_exercise_questions tmpeq where tmpeq.parent_id = eq.id))
        or (eq.parent_id is not null and eq.parent_id != ''))
        <choose>
            <when test="dealType == 1">
                and eq.handle_user_id = #{userId} and eq.deal_status &lt; 2
            </when>
            <when test="dealType == 2">
                and eq.auditing_user_id = #{userId} and eq.deal_status in (3, 7)
            </when>
            <otherwise>
                and eq.deal_status != 6
            </otherwise>
        </choose>
        <if test="userIdList.size() > 0">
            and (
            eq.handle_user_id in
            <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            OR
            eq.auditing_user_id in
            <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            )
        </if>
        group by eq.id
        order by eq.handler_submit_time, eq.parent_id, eq.num, eq.id
    </select>

    <!--根据年级，科目，版本，上下册分页查询题目-->
    <select id="findKmtQuestionByGradeSubjectVersionSemester" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, parent_id, image question_image
        from kmt_exercise_questions eq
        left join zyb_know_how_details_sort khds on eq.concise_id=khds.id
        where eq.book_id is null  and eq.question_class_type in (0, 2)
        <if test = "grade != null and grade != ''">
            and khds.grade = #{grade}
        </if>
        <if test = "courseName != null and courseName != ''">
            and khds.course_name = #{courseName}
        </if>
        <if test = "type > 0">
            and khds.type = #{type}
        </if>
        <if test = "semester > 0">
            and khds.semester = #{semester}
        </if>
        <choose>
            <when test="dealType == 1">
                and eq.handle_user_id = #{userId} and eq.deal_status &lt; 2
            </when>
            <when test="dealType == 2">
                and eq.auditing_user_id = #{userId} and eq.deal_status in (3, 7)
            </when>
            <otherwise>
                and eq.deal_status != 6
            </otherwise>
        </choose>
        <if test="userIdList.size() > 0">
            and (
            eq.handle_user_id in
            <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            OR
            eq.auditing_user_id in
            <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            )
        </if>
        group by eq.id
        order by eq.handler_submit_time, eq.parent_id, eq.num, eq.id
    </select>

    <!--分页查询所有题目-->
    <select id="findAllKmtQuestions" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, eq.parent_id, image question_image
        from kmt_exercise_questions eq
        where eq.book_id is null and (((eq.parent_id is null or eq.parent_id='') and not exists (select 1 from kmt_exercise_questions tmpeq where tmpeq.parent_id = eq.id))
        or (eq.parent_id is not null and eq.parent_id != ''))
        <choose>
            <when test="dealType == 1">
                and eq.handle_user_id = #{userId} and eq.deal_status &lt; 2
            </when>
            <when test="dealType == 2">
                and eq.auditing_user_id = #{userId} and eq.deal_status in (3, 7)
            </when>
            <otherwise>
                and eq.deal_status != 6
            </otherwise>
        </choose>
        <if test="userIdList.size() > 0">
            and (
                eq.handle_user_id in
                <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
                OR
                eq.auditing_user_id in
                <foreach collection="userIdList" index="i" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            )
        </if>
        group by eq.id
        order by eq.handler_submit_time, eq.parent_id, eq.num, eq.id
    </select>

    <!--查询小题-->
    <select id="findSmallQuestionByParentIds"  resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, eq.parent_id, image question_image
        from kmt_exercise_questions eq
        where eq.parent_id in
        <foreach collection="parentIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and eq.deal_status != 6
        order by eq.parent_id, eq.num
    </select>

    <!--查询大题-->
    <select id="findBigQuestionById"  resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id, eq.analysis_auditer_id, eq.book_course_id course_id,
        eq.big_question_index, eq.small_question_name small_question_index,
        eq.deal_status question_status, eq.create_time, eq.remark reason,
        eq.question_type question_types, eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
        eq.training_skill training_skills, eq.parent_id, image question_image,eq.update_type
        from kmt_exercise_questions eq
        where eq.id = #{id}
    </select>

    <!--查询题目的知识点-->
    <select id="findQuestionKnowByQuestionIds" resultType="com.ssj.bean.kmt.kmt.model.QuestionListKnowModel">
        select eqk.question_id, hds.id how_details_id, hds.alias detail_name, hdks.id how_details_kd_id, hdks.alias kd_name,
        hds.grade, hds.course_name, hds.type version_type, hds.semester, khd.is_zt module_type, hs.alias how_name
        from kmt_exercise_question_know eqk
        left join zyb_know_how_details_sort hds on eqk.how_detail_sort_id=hds.id
        left join zyb_know_how_details_sort hs on hds.how_id=hs.how_id and hds.type=hs.type and hds.semester=hs.semester and hds.grade=hs.grade and hs.stat=1
        left join zyb_know_how_details_sort hdks on eqk.how_detail_kd_sort_id=hdks.id
        left join zyb_know_how_details khd on hds.how_details_id=khd.id
        where question_id in
        <foreach collection="questionIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        order by eqk.question_id, hds.num, hdks.num
    </select>

    <!--查询kmt题目的知识点-->
    <select id="findKmtQuestionKnowByQuestionIds" resultType="com.ssj.bean.kmt.kmt.model.QuestionListKnowModel">
        select eqk.question_id, hds.id how_details_id, hds.alias detail_name, hdks.id how_details_kd_id, hdks.alias kd_name,
        hds.grade, hds.course_name, hds.type version_type, hds.semester, khd.is_zt module_type, hs.alias how_name
        from kmt_quality_question_know eqk
        left join zyb_know_how_details_sort hds on eqk.how_detail_sort_id=hds.id
        left join zyb_know_how_details_sort hs on hds.how_id=hs.how_id and hds.type=hs.type and hds.semester=hs.semester and hds.grade=hs.grade and hs.stat=1
        left join zyb_know_how_details_sort hdks on eqk.how_detail_kd_sort_id=hdks.id
        left join zyb_know_how_details khd on hds.how_details_id=khd.id
        where question_id in
        <foreach collection="questionIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        order by eqk.question_id, hds.num, hdks.num
    </select>

    <select id="findFullBigQuestion" resultType="com.ssj.service.kmt.dto.OriginalQuestionDTO">
        select bigQuestion.id, bigQuestion.parent_id, bigQuestion.book_id, 2 as type, bigQuestion.question, bigQuestion.num
        from kmt_exercise_questions bigQuestion,
             kmt_exercise_questions
        where kmt_exercise_questions.parent_id = bigQuestion.id
          and length(kmt_exercise_questions.parent_id) > 0
          and kmt_exercise_questions.id = #{questionId}
        union all
        select smallQuestion.id, smallQuestion.parent_id, smallQuestion.book_id, 1 as type, smallQuestion.question, smallQuestion.num
        from kmt_exercise_questions smallQuestion,
             kmt_exercise_questions
        where smallQuestion.parent_id = kmt_exercise_questions.parent_id
          and length(kmt_exercise_questions.parent_id) > 0
          and kmt_exercise_questions.id = #{questionId}
        union all
        select bigQuestion.id, bigQuestion.parent_id, bigQuestion.book_id, 2 as type, bigQuestion.question, bigQuestion.num
        from kmt_exercise_questions bigQuestion
        where bigQuestion.id = #{questionId}
          and 1 > length(bigQuestion.parent_id)
        union all
        select child.id, child.parent_id, child.book_id, 1 as type, child.question, child.num
        from kmt_exercise_questions child
        where child.parent_id = #{questionId}
    </select>

    <!--通过子题目id查询父题目-->
    <select id="findPrentQuestionByChildQuestionId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
        select parent.* from kmt_exercise_questions child left join kmt_exercise_questions parent on child.parent_id=parent.id
        where child.id=#{questionId} and parent.id is not null
    </select>

    <select id="findByParentIdLimitSmallSum"
            resultType="com.ssj.service.conch.parents.v2.dto.TrainRecommendQuestionDTO">
        select bigQuestion.id, bigQuestion.parent_id, 1 as type, bigQuestion.question, bigQuestion.num,
               bigQuestion.book_id, bigQuestion.question_type optionTypeInt, bigQuestion.difficulty_level difficulty,
               bigQuestion.course_name
        from kmt_exercise_questions bigQuestion
        where bigQuestion.id = #{parentId}
        union all
        (select smallQuestion.id, smallQuestion.parent_id, 1 as type, smallQuestion.question, smallQuestion.num,
                smallQuestion.book_id, smallQuestion.question_type optionTypeInt, smallQuestion.difficulty_level difficulty,
                smallQuestion.course_name
         from kmt_exercise_questions smallQuestion
         where smallQuestion.parent_id = #{parentId}
         <if test="smallQuestionSum != null">
             limit #{smallQuestionSum}
         </if>
        )
        order by num
    </select>

    <!--分页查询作业批改带学校的练习册-->
    <select id="findHomeworkCorrectFilterBooksBySchool" resultType="com.ssj.service.sys.homework.vo.HomeworkExerciseBookDto">
        select eb.id book_id, eb.book_name, eb.part_flag, eb.course_name, eb.version version_type, eb.semester from kmt_exercise_book eb
        left join  scon_homework_exercise_except hee on eb.id=hee.except_book_id
        and hee.school=#{school}
        where hee.id is null and eb.course_name=#{subject}
        <if test="versionType > 0">
            and version=#{versionType}
        </if>
        and eb.semester=#{semester} and eb.grade=#{grade}
        and eb.book_type in (0, 1, 3)
        order by eb.id
    </select>

    <!--分页查询作业批改的练习册-->
    <select id="findHomeworkCorrectFilterBooks" resultType="com.ssj.service.sys.homework.vo.HomeworkExerciseBookDto">
        select eb.id book_id, eb.book_name, eb.part_flag, eb.course_name, eb.version version_type, eb.semester from kmt_exercise_book eb where  eb.course_name=#{subject}
        and eb.semester=#{semester} and eb.grade=#{grade}
        <if test="versionType > 0">
            and version=#{versionType}
        </if>
        and eb.book_type in (0, 1, 3)
        order by eb.id
    </select>

    <!--    查询练习册记录-->
    <select id="findExerciseBookById" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
        select * from kmt_exercise_book where id=#{bookId}
    </select>

    <select id="findExerciseBookByBookIdAndYearAndType" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
        select id, book_id from kmt_exercise_book where book_id=#{bookId} and book_year=#{bookYear}
            and book_type=#{bookType}
            limit 1
    </select>


    <!--    查询练习册单元课时-->
    <select id="findByBookIds" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBookCourse">
        select eb.id book_id,  ebc.book_name, ebc.unit_id, ebc.unit, ebc.id,  ebc.period, ebc.grade,
        ebc.course_name, ebc.version, ebc.semester, eb.part_flag remark from kmt_exercise_book_course ebc
        left join kmt_exercise_book eb on ebc.book_id=eb.book_id  where eb.id in
        <foreach collection="bookIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        order by eb.id,ebc.unit_sort,ebc.period_sort limit 5000
    </select>


    <!-- 删除加工的知识点 -->
    <delete id="deleteKmtExerciseQuestionKnowByQuestionIds">
        delete from kmt_exercise_question_know where question_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteKmtExerciseBookStatusCountByExerciseBookIds">
        delete from kmt_exercise_book_status_count where exercise_book_id in
        <foreach collection="exerciseBookIdSet" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 题目提交审核 -->
    <update id="updateKmtExerciseQuestion2Review">
        UPDATE kmt_exercise_questions set deal_status=3 WHERE id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="updateLoadStatus">
        update kmt_exercise_question_know set loaded = 1 where loaded = 0 and id in
        <foreach collection="kmtExerciseQuestionKnowIdList" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!--更新练习册为未加工-->
    <update id="updateKmtExerciseBookNotHandle">
        update kmt_exercise_book set had_handle=0 where id=#{id}
    </update>
    <!--更新练习册为未审核-->
    <update id="updateKmtExerciseBookNotAuditing">
        update kmt_exercise_book set had_auditing=0 where id=#{id}
    </update>

    <!--更新练习册为解析未加工-->
    <update id="updateKmtExerciseBookAnalysisNotHandle">
        update kmt_exercise_book set analysis_handled=0 where id=#{id}
    </update>
    <!--更新练习册为解析未审核-->
    <update id="updateKmtExerciseBookAnalysisNotAuditing">
        update kmt_exercise_book set analysis_audited=0 where id=#{id}
    </update>

    <!--更新练习册课时为未加工-->
    <update id="updateKmtExerciseBookCourseNotHandle">
        update kmt_exercise_book_course set had_handle=0, had_auditing=0 where id=#{id}
    </update>
    <!--更新练习册课时为未审核-->
    <update id="updateKmtExerciseBookCourseNotAuditing">
        update kmt_exercise_book_course set had_auditing=0 where id=#{id}
    </update>

    <update id="updateExerciseQuestionKnowEsLoadStatus">
        update kmt_exercise_question_know set loaded = #{newLoaded}
        where id in
        <foreach collection="kmtExerciseQuestionKnowIdList" open="(" item="kmtExerciseQuestionKnowId" separator="," close=")">
            #{kmtExerciseQuestionKnowId}
        </foreach>
    </update>

    <select id="getBookId" resultType="String" parameterType="java.util.Map">
        select id
        from kmt_exercise_book eb
        where eb.course_name = #{map.subject}
        and eb.grade = #{map.grade}
        and eb.semester = #{map.semester}
        and eb.book_type in (0, 1, 3)
        and not exists (
             select 1 from scon_homework_exercise_except t1
             where t1.school = #{map.school}
             and t1.grade = #{map.grade}
             and t1.course_name = #{map.subject}
             and eb.book_id = t1.except_book_id
        )
        <if test='map.version != null and map.version > 0'>
            and version = #{map.version}
        </if>
    </select>

    <select id="findAllChild" resultType="com.ssj.service.conch.parents.v2.dto.TrainRecommendQuestionDTO">
        select
            id,
            difficulty_level difficulty,
            question_type    optionTypeInt,
            course_name,
            num
        from kmt_exercise_questions
        where parent_id = #{parentId}
        order by num
    </select>
    <sql id="exercisePublic" >
        t.id exerciseBookId,
        t.url1,
        t.url2,
        t.book_name bookName,
        t.press,
        t.course_name subject,
        t.grade,
        t.version,
        t.version_name versionName,
        t.semester,
        t.print_year printYear,
        t.book_year bookYear,
        t.book_year useYear,
        t.status,
        t.org_nums orgNums,
        t.progress,
        t.total_grade_priority priority,
        t.identified_nums identifiedNums,
        t.answer_correcter_id answerCorrecterId,
        t.book_type,
        t.check_time checkTime,
        t.ai_rollback_import aiRollbackImport
    </sql>
    <select id="findExerciseList" resultType="com.ssj.service.kmt.exercisesys.dto.Exercise1Dto" parameterType="java.util.Map">
        select
        <include refid="exercisePublic" />
        from kmt_exercise_book t
        where 1=1
        <!--未校对-->
        <if test='map.proofreadStatus == "0" '>
            and not exists (
                select 1 from kmt_exercise_book_status_count t1
                where t.id = t1.exercise_book_id
                and t1.data_status = 1
                and t1.status > 0
                and t1.status &lt; 5
            )
            and t.status is not null
            and t.proofread_status = 0
            and t.answer_correcter_id is null
            and t.part_flag != 3
        </if>
        <if test="map.bookType != null and map.bookType != '' ">
            and t.book_type = ${map.bookType}
        </if>
        <if test="map.progress != null and map.progress != '' ">
            and t.progress = #{map.progress}
        </if>
        <if test="map.subject != null and map.subject != '' ">
            and t.course_name = #{map.subject}
        </if>
        <if test="map.grade != null and map.grade != '' ">
            and t.grade = #{map.grade}
        </if>
        <if test="map.bookName != null and map.bookName != '' ">
            and t.book_name regexp #{map.bookName}
        </if>
        <if test="map.semester != null and map.semester != '' ">
            and t.semester = #{map.semester}
        </if>
        <if test="map.versionName != null and map.versionName != '' ">
            and t.version_name = #{map.versionName}
        </if>
        <if test="map.bookYear != null and map.bookYear != '' ">
            and t.book_year = #{map.bookYear}
        </if>
        order by t.updated_time desc,t.book_name
    </select>

    <select id="findTeacherExerciseList" resultType="com.ssj.service.kmt.exercisesys.dto.Exercise1Dto" parameterType="java.util.Map">
        select
        <include refid="exercisePublic" />
        ,t1.name teacherName
        from kmt_exercise_book t
            left join tb_lib_manager t1 on t.answer_correcter_id = t1.user_id
                and t1.type = 17
                and t1.authority = 8
                and t1.state = 1
        where 1=1
        <if test="map.teacherName != null and map.teacherName != '' ">
            and t1.name regexp #{map.teacherName}
        </if>
        <if test="map.exerciseName != null and map.exerciseName != '' ">
            and t.book_name regexp #{map.exerciseName}
        </if>
        <if test="map.beginTime != null and map.beginTime != '' ">
            and t.check_time >= #{map.beginTime}
        </if>
        <if test="map.endTime != null and map.endTime != '' ">
            and t.check_time &lt;= #{map.endTime}
        </if>
        <!--校对中-->
        <if test='map.type == "1" '>
            and t.part_flag != 3
            and t.answer_corrected_status = 0
            and t.answer_correcter_id is not null
            order by t1.name,t1.user_id,t.book_name
        </if>
        <!--已校对-->
        <if test='map.type == "2" '>
            and t.part_flag != 3
            and t.answer_corrected_status = 1
            order by t1.name,t1.user_id,t.book_name,t.create_time asc
        </if>
    </select>

    <select id="findLastExerciseRollbackByQuestionId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestionRollback">
        select * from kmt_exercise_question_rollback where question_id=#{questionId} order by create_time desc limit 1
    </select>

    <select id="findLibList" resultType="com.ssj.bean.weixin.libmy.domain.TbLibJoin" parameterType="java.util.Map">
        select t1.*
        from tb_lib_join t1
        where 1=1
        and t1.type = 1
        <if test="map.name != null and map.name != '' ">
            and t1.league_name regexp #{map.name}
        </if>
        and exists (
            select 1 from fx_merchant t2 where t1.id = t2.lib_id and t2.sign_service > 0
        )
        order by t1.league_name
    </select>

    <select id="findExerciseBookCount" resultType="com.ssj.service.kmt.exercisesys.dto.ExerciseBookCountDTO">
        select status,
               count(*)                           totalCount,
               sum(if(course_name = '语文', 1, 0)) chineseCount,
               sum(if(course_name = '数学', 1, 0)) mathCount,
               sum(if(course_name = '英语', 1, 0)) englishCount
        from kmt_exercise_book_status_count
        where data_status = 1
        group by status
    </select>


    <select id="findAllLib" resultType="java.util.Map">
        select distinct (t1.lib_id) libId,t3.league_lat leagueLet,t3.league_lng leagueLng,t3.league_name leagueName, ifNull(t4.sign_service,0) signService
        from kmt_exercise_book_lib t1
        left join kmt_exercise_book t2 on t1.exercise_book_id =t2.id and t1.is_delete=0
        left join tb_lib_join t3 on t1.lib_id=t3.id
        left join fx_merchant t4 on t1.lib_id=t4.lib_id
        where t1.lib_id is not null and t1.lib_id !=''
        <if test="subject != null and subject != '' ">
            and t2.course_name=#{subject}
        </if>
         and t1.is_delete=0
         and t4.sign_service!=0
         and t4.sign_service is not null

   </select>

    <select id="findAllExerciseBook" resultType="com.ssj.service.kmt.dto.exercise.KmtExerciseBookLibDto">
        select distinct t1.book_id bookId, t1.book_name exerciseName,t1.version_name version,t1.total_grade_priority priority,t1.semester,t1.grade,t1.progress progress,t1.total_grade_priority totalPriority
        from kmt_exercise_book_lib t
        left join kmt_exercise_book t1  on t.exercise_book_id =t1.id
        where t.lib_id=#{libId}
        and t1.course_name=#{subject}
        and t.is_delete=0

   </select>

    <select id="findAllExportLib" resultType="java.util.Map">
        select distinct t1.book_id bookId, t1.lib_id libId,t3.league_lat leagueLet,t3.league_lng leagueLng,t3.league_name leagueName,ifNull(t4.sign_service,0)signService
        from kmt_exercise_book_lib t1
        left join kmt_exercise_book t2 on t1.exercise_book_id =t2.id
        left join tb_lib_join t3 on t1.lib_id=t3.id
        left join fx_merchant t4 on t1.lib_id=t4.lib_id
        where t1.lib_id is not null
        <if test="subject != null and subject != '' ">
            and t2.course_name=#{subject}
        </if>
       and t1.is_delete=0

    </select>


    <select id="findQuestionByParentId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
        select * from kmt_exercise_questions where (id in
        <foreach collection="parentIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>or parent_id in
        <foreach collection="parentIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        ) and deal_status != 6
        order by num
        limit 2000
    </select>

    <select id="findKmtExerciseQuestionRollbackListByQuestionId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestionRollback">
        select * from kmt_exercise_question_rollback where question_id= #{questionId}
    </select>

    <select id="findByUserIdAndTypeIn" resultType="com.ssj.bean.weixin.libmy.domain.TbLibManager">
        select * from tb_lib_manager where user_id= #{userId}
        and type in(17,18) limit 1
    </select>

    <select id="findLibSignCount" resultType="com.ssj.service.kmt.exercisesys.dto.ExercisePriorityDto">
        select t1.id id,t1.exercise_book_id exerciseBookId,t1.lib_id libId,t2.sign_service signService from kmt_exercise_book_lib t1 left join fx_merchant t2
             on t1.lib_id=t2.lib_id
             where t1.exercise_book_id is not null
             and t1.is_delete=0
             limit 10000
    </select>

    <select id="findExerciseQuestionByExerciseBookId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
        select eq.id, eq.book_id, eq.unit_id, eq.book_course_id,
               eb.auditing_user_id, eb.handle_user_id, eb.analysis_user_id, eb.analysis_auditer_id, eb.answer_correcter_id,
               eq.question_type, eq.source, eq.big_question_index, eq.analysis_submit_time, eq.page_index,
               eq.image, eq.precision_percent, eq.create_time, eq.deal_status, eq.remark, eq.analysis_submit_type,
               eq.answer_image, eq.num, eq.small_question_name,  eq.parent_id, eq.big_name, eq.real_exam_type, eq.publish_time,
               eq.update_type, eq.difficulty_level, eq.training_skill, eq.course_name, eq.is_common_flag, eq.question_year, eq.exercise_book_id, eq.es_loaded,
               eq.analysis_handler_submit_time, eq.question_class_type, eq.repulse_time,
               eq.proofread_status, eq.question_path
        from kmt_exercise_questions eq
         left join kmt_exercise_book eb on eq.exercise_book_id=eb.id where eq.exercise_book_id=#{exerciseBookId} limit 10000
    </select>

    <select id="findExerciseQuestionByIds" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
        select eq.id, eq.book_id, eq.unit_id, eq.book_course_id,
        eb.auditing_user_id, eb.handle_user_id, eb.analysis_user_id, eb.analysis_auditer_id, eb.answer_correcter_id,
        eq.question_type, eq.source, eq.big_question_index, eq.analysis_submit_time, eq.page_index,
        eq.image, eq.precision_percent, eq.create_time, eq.deal_status, eq.remark, eq.analysis_submit_type,
        eq.answer_image, eq.num, eq.small_question_name,  eq.parent_id, eq.big_name, eq.real_exam_type, eq.publish_time,
        eq.update_type, eq.difficulty_level, eq.training_skill, eq.course_name, eq.is_common_flag, eq.question_year, eq.exercise_book_id, eq.es_loaded,
        eq.analysis_handler_submit_time, eq.question_class_type, eq.repulse_time,
        eq.proofread_status, eq.question_path
        from kmt_exercise_questions eq
        left join kmt_exercise_book eb on eq.exercise_book_id=eb.id where eq.id in
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        limit 10000
    </select>

    <!--更新练习册涉及到的机构签约数-->
    <update id="updateKmtExerciseBookByExerciseBookId">
        update kmt_exercise_book set exercise_relevancy_num=#{exerciseRelevancyNum} where id=#{exerciseBooId}
    </update>

    <select id="findAllExerciseBookBySignNum"  resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
         select * from kmt_exercise_book
         where
         exercise_relevancy_num !=0
         and exercise_relevancy_num is not null
         order by exercise_relevancy_num desc
         limit 10000
    </select>

    <select id="findAnswerProofreadExerciseBookList"
            resultType="com.ssj.service.kmt.exercisesys.dto.AnswerProofreadExerciseBookDTO">
        select distinct t1.id exerciseBookId,
               t1.book_name,
               t1.course_name subject,
               t1.grade,
               t1.version,
               t1.semester,
               t1.proofread_status,
               t1.book_type,
               t1.latest_answer_wrong_date_time proofreadDate,
               t3.name
        from kmt_exercise_book t1  left join tb_lib_manager t3
                 on t1.answer_correcter_id=t3.user_id
         and t3.type=17 and t3.authority=8 and t3.state=1
        where t1.proofread_status in (2, 3)
          <if test="req.subject != null and req.subject != ''">
              and t1.course_name = #{req.subject}
          </if>
          <if test="req.grade != null and req.grade != ''">
              and t1.grade = #{req.grade}
          </if>
          <if test="req.bookName != null and req.bookName != ''">
              and t1.book_name regexp #{req.bookName}
          </if>
        <if test="req.proofreadDate != null and req.proofreadDate != ''">
            and date(t1.latest_answer_wrong_date_time) = #{req.proofreadDate}
        </if>
        order by t1.proofread_status
    </select>

    <select id="findQuestionListAnswerWrong" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id question_id, eq.course_name, eq.handle_user_id, eq.auditing_user_id, eq.analysis_user_id,
               eq.analysis_auditer_id, eq.book_course_id course_id, eq.big_question_index,
               eq.small_question_name small_question_index,
               eq.deal_status question_status, eq.create_time, eq.remark reason, eq.question_type question_types,
               eq.difficulty_level difficulty_levels, eq.real_exam_type real_exam_types,
               eq.training_skill training_skills, eq.parent_id
        from kmt_exercise_questions eq
        where eq.exercise_book_id = #{req.exerciseBookId}
            and eq.book_course_id = #{req.periodId}
            and eq.proofread_status = 2
        order by eq.num, eq.create_time
    </select>

    <select id="findRepulsedQuestion" resultType="com.ssj.service.kmt.dto.ExerciseQuetionDto">
        select eq.id                  question_id,
               eq.course_name,
               eq.handle_user_id,
               eq.auditing_user_id,
               eq.analysis_user_id,
               eq.analysis_auditer_id,
               eq.book_course_id      course_id,
               eq.big_question_index,
               eq.small_question_name small_question_index,
               eq.deal_status         question_status,
               eq.create_time,
               eq.remark              reason,
               eq.from_user_id,
               eq.to_user_id,
               eq.question_type       question_types,
               eq.difficulty_level    difficulty_levels,
               eq.real_exam_type      real_exam_types,
               eq.training_skill      training_skills,
               eq.parent_id
        from kmt_exercise_questions eq
        where eq.exercise_book_id = #{req.exerciseBookId}
          and eq.deal_status in
          <foreach collection="dealStatusList" open="(" item="dealStatus" separator="," close=")">
              #{dealStatus}
          </foreach>
        order by eq.num, eq.create_time, eq.deal_status
        limit 0, 5000
    </select>

    <update id="updateKmtExerciseBookById">
        update kmt_exercise_book set priority=#{priority} where id=#{id}
    </update>

    <update id="updateKmtExerciseBookByPriority">
        update kmt_exercise_book set total_grade_priority=#{totalGradePriority} where id=#{id}
    </update>

    <select id="findUnproofreadExerciseId" resultType="java.util.Map">
       select id from kmt_exercise_book eb where eb.answer_correcter_id is not null
    </select>


    <select id="findExerciseStatusByBookId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion">
       select eq.* from kmt_exercise_questions eq left kmt_exercise_book eb on eq.exercise_book_id=eb.id where eb.answer_correcter_id is not null and eq.book_id=#{bookId}
    </select>

    <update id="updateExerciseBookStatusByIdAndStatus">
        update kmt_exercise_book set proofread_status=#{proofreadStatus} where id=#{id}
    </update>

    <update id="updateExerciseBookProofreadResult">
        update kmt_exercise_book set proofread_status=#{type}, latest_answer_wrong_date_time = now() where id=#{exerciseBookId}
    </update>

    <select id="countAnswerWrongQuestion" resultType="java.lang.Integer">
        select count(*) from kmt_exercise_questions where exercise_book_id = #{exerciseBookId} and proofread_status = 2 and deal_status not in (2, 6)
        and id not in
        <foreach collection="excludeQuestionIdSet" open="(" item="id" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="teacherAnswerProofreadDetail" resultType="java.util.Map">
        select count(1) total,sum(answer_corrected_status) finishedNums,count(1)-sum(answer_corrected_status) unfinishedNums
        from kmt_exercise_book
        where answer_correcter_id = #{userId}
    </select>

    <select id="findUnitAndPeriodList" resultType="com.ssj.service.kmt.dto.exercise.ExerciseUnitBookcourseDto">
        select ebc.unit_id id, ebc.unit name, ebc.id book_course_id, ebc.period book_course
        from kmt_exercise_book_course ebc
        where ebc.id in (select distinct book_course_id
                         from kmt_exercise_questions
                         where exercise_book_id = #{request.exerciseBookId} and proofread_status = 2)
    </select>

    <select id="findKmtExerciseQuestionErrorsByQuestionIds" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestionErrors">
        select * from kmt_exercise_question_errors where question_id in
        <foreach collection="questionIds" open="(" item="id" close=")" separator=",">
            #{id}
        </foreach>
        and error_user_auth=#{beType}
    </select>
    <select id="findKmtExerciseQuestionByErrorUserId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestionErrors">
        select * from kmt_exercise_question_errors where error_user_id=#{userId} order by create_time desc
    </select>

    <select id="countQuestionErrorCountByExerciseBookId" resultType="java.lang.Integer">
        select count(1) from kmt_exercise_question_errors where exercise_book_id=#{exerciseBookId}
        and error_user_id=#{userId} and error_user_auth=#{type}
    </select>

    <select id="findSameExerciseBookList" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
        SELECT t.*
        FROM kmt_exercise_book t,
             (select book_name,
                     course_name,
                     grade,
                     version_name,
                     semester,
                     book_type,
                     book_year,
                     COUNT(book_name)    c1,
                     COUNT(course_name)  c2,
                     COUNT(grade)        c3,
                     COUNT(version_name) c4,
                     COUNT(semester)     c5,
                     COUNT(book_type)    c6,
                     COUNT(book_year)    c7
              from kmt_exercise_book
              group by book_name, course_name, grade, version_name, semester, book_type, book_year
              HAVING c1 > 1
                 and c2 > 1
                 and c3 > 1
                 and c4 > 1
                 and c5 > 1
                 and c6 > 1
                 and c7 > 1) t1
        where t.book_name = t1.book_name
          and t.course_name = t1.course_name
          and t.grade = t1.grade
          and t.version_name = t1.version_name
          and t.semester = t1.semester
          and t.book_type = t1.book_type
          and t.book_year = t1.book_year
        order by t.book_name, t.course_name, t.grade, t.version_name, t.semester, t.book_type, t.book_year, t.part_flag
    </select>

    <select id="countUnFinishQuestionsByExerciseBookId" resultType="java.lang.Integer">
        select count(1) from kmt_exercise_questions where exercise_book_id=#{exerciseBookId}
        <if test="type == 1">
            and deal_status in (0, 1)
        </if>
        <if test="type == 2">
            and deal_status in (3, 7)
        </if>
        <if test="type == 3">
            and deal_status in (5, 8)
        </if>
        <if test="type == 4">
            and deal_status in (9, 10)
        </if>
    </select>

    <select id="findDistributionCount"
            resultType="com.ssj.service.kmt.exercisesys.dto.ExerciseDistributionCountDTO">
        select sum(if(handle_user_id is null or auditing_user_id is null or analysis_user_id is null or
                      analysis_auditer_id is null, 1, 0)) notDistributedExerciseBookCount,
               count(*)                                   totalExerciseBookCount
        from kmt_exercise_book
        where part_flag &lt;> 3
    </select>

    <select id="findDistributionExerciseBookList" resultMap="DistributionExerciseBookDTOResultMap">
        select eb.exerciseBookId,
               eb.importDate,
               eb.subject,
               eb.grade,
               eb.version_name,
               eb.semester,
               eb.book_name,
               eb.book_type,
               (select count(distinct unit_id)
                from kmt_exercise_book_course ebc
                where ebc.exercise_book_id = eb.exerciseBookId) unitNum,
               (select count(*)
                from kmt_exercise_book_course ebc
                where ebc.exercise_book_id = eb.exerciseBookId) courseNum,
               ebcHandle.accuracy                               handleAccuracy,
               ebcHandleAudit.accuracy                          handleAuditAccuracy,
               ebcAnalysis.accuracy                             analysisAccuracy,
               ebtrHandle.teacher_id                            handlerTeacherId,
               ebtrHandle.teacher_name                          handlerName,
               ebtrHandle.salary_given                          handlerSalaryGiven,
               ebtrHandle.salary_given_time                     handlerSalaryGivenTime,
               ebtrHandle.create_time                           handlerDistributeTime,
               ebtrHandleAudit.teacher_id                       handlerAuditUserTeacherId,
               ebtrHandleAudit.teacher_name                     handlerAuditUserName,
               ebtrHandleAudit.salary_given                     handlerAuditUserSalaryGiven,
               ebtrHandleAudit.salary_given_time                handlerAuditUserSalaryGivenTime,
               ebtrHandleAudit.create_time                      handlerAuditUserDistributeTime,
               ebtrAnalysis.teacher_id                          analyserTeacherId,
               ebtrAnalysis.teacher_name                        analyserName,
               ebtrAnalysis.salary_given                        analyserSalaryGiven,
               ebtrAnalysis.salary_given_time                   analyserSalaryGivenTime,
               ebtrAnalysis.create_time                         analyserDistributeTime,
               ebtrAnalysisAudit.teacher_id                     analysisAuditUserTeacherId,
               ebtrAnalysisAudit.teacher_name                   analysisAuditUserName,
               ebtrAnalysisAudit.salary_given                   analysisAuditUserSalaryGiven,
               ebtrAnalysisAudit.salary_given_time              analysisAuditUserSalaryGivenTime,
               ebtrAnalysisAudit.create_time                    analysisAuditUserDistributeTime
        from (select id          exerciseBookId,
                     create_time importDate,
                     course_name subject,
                     grade,
                     version_name,
                     semester,
                     book_name,
                     book_type,
                     handle_user_id,
                     auditing_user_id,
                     analysis_user_id,
                     analysis_auditer_id
              from kmt_exercise_book
              where (3 > part_flag or part_flag > 3)
                  <if test="req.type == 1">
                      and (
                      <choose>
                          <when test="(req.handler != null and req.handler > 0) or
                                      (req.handleAuditUser != null and req.handleAuditUser > 0) or
                                      (req.analyser != null and req.analyser > 0) or
                                      (req.analysisAuditUser != null and req.analysisAuditUser > 0)">
                              <if test="req.handler != null and req.handler > 0">
                                  handle_user_id is null and
                              </if>
                              <if test="req.handleAuditUser != null and req.handleAuditUser > 0">
                                  auditing_user_id is null and
                              </if>
                              <if test="req.analyser != null and req.analyser > 0">
                                  analysis_user_id is null and
                              </if>
                              <if test="req.analysisAuditUser != null and req.analysisAuditUser > 0">
                                  analysis_auditer_id is null and
                              </if>
                              1 = 1
                          </when>
                          <otherwise>
                              handle_user_id is null
                              or auditing_user_id is null
                              or analysis_user_id is null
                              or analysis_auditer_id is null
                          </otherwise>
                      </choose>
                      )
                  </if>
                  <if test="req.type == 2">
                      and (handle_user_id is not null
                      and auditing_user_id is not null
                      and analysis_user_id is not null
                      and analysis_auditer_id is not null)
                  </if>
                  <if test="req.bookName != null and req.bookName != ''">
                      and book_name like '%${req.bookName}%'
                  </if>
                  <if test="req.subject != null and req.subject != ''">
                      and course_name = #{req.subject}
                  </if>
                  <if test="req.grade != null and req.grade != ''">
                      and grade = #{req.grade}
                  </if>
                  <if test="req.version != null and req.version > 0">
                      and version = #{req.version}
                  </if>
                  <if test="req.semester != null and req.semester > 0">
                      and semester = #{req.semester}
                  </if>
                  <if test="req.bookType != null and req.bookType > -1">
                      and book_type = #{req.bookType}
                  </if>
                  <if test="req.startTime != null and req.startTime != ''">
                      and create_time > #{req.startTime}
                  </if>
                  <if test="req.endTime != null and req.endTime != ''">
                      and '${req.endTime} 23:59:59' > create_time
                  </if>
              order by create_time desc
              limit #{offset}, #{pageSize}) eb
                 left join kmt_exercise_book_teacher_record ebtrHandle
                           on eb.exerciseBookId = ebtrHandle.exercise_book_id and ebtrHandle.authority_type = 1
                 left join kmt_exercise_book_count ebcHandle
                           on eb.exerciseBookId = ebcHandle.exercise_book_id and ebcHandle.type = 1
                 left join kmt_exercise_book_teacher_record ebtrHandleAudit
                           on eb.exerciseBookId = ebtrHandleAudit.exercise_book_id and ebtrHandleAudit.authority_type = 2
                 left join kmt_exercise_book_count ebcHandleAudit
                           on eb.exerciseBookId = ebcHandleAudit.exercise_book_id and ebcHandleAudit.type = 2
                 left join kmt_exercise_book_teacher_record ebtrAnalysis
                           on eb.exerciseBookId = ebtrAnalysis.exercise_book_id and ebtrAnalysis.authority_type = 3
                 left join kmt_exercise_book_count ebcAnalysis
                           on eb.exerciseBookId = ebcAnalysis.exercise_book_id and ebcAnalysis.type = 3
                 left join kmt_exercise_book_teacher_record ebtrAnalysisAudit
                           on eb.exerciseBookId = ebtrAnalysisAudit.exercise_book_id and ebtrAnalysisAudit.authority_type = 4
                 left join kmt_exercise_book_count ebcAnalysisAudit
                           on eb.exerciseBookId = ebcAnalysisAudit.exercise_book_id and ebcAnalysisAudit.type = 4
        order by eb.importDate desc
    </select>

    <select id="countDistributionExerciseBook" resultType="java.lang.Integer">
        select count(*)
        from kmt_exercise_book
        where part_flag &lt;> 3
            <if test="req.type == 1">
                and (
                <choose>
                    <when test="(req.handler != null and req.handler > 0) or
                                (req.handleAuditUser != null and req.handleAuditUser > 0) or
                                (req.analyser != null and req.analyser > 0) or
                                (req.analysisAuditUser != null and req.analysisAuditUser > 0)">
                        <if test="req.handler != null and req.handler > 0">
                            handle_user_id is null and
                        </if>
                        <if test="req.handleAuditUser != null and req.handleAuditUser > 0">
                            auditing_user_id is null and
                        </if>
                        <if test="req.analyser != null and req.analyser > 0">
                            analysis_user_id is null and
                        </if>
                        <if test="req.analysisAuditUser != null and req.analysisAuditUser > 0">
                            analysis_auditer_id is null and
                        </if>
                        1 = 1
                    </when>
                    <otherwise>
                        handle_user_id is null
                        or auditing_user_id is null
                        or analysis_user_id is null
                        or analysis_auditer_id is null
                    </otherwise>
                </choose>
                )
            </if>
            <if test="req.type == 2">
                and (handle_user_id is not null
                and auditing_user_id is not null
                and analysis_user_id is not null
                and analysis_auditer_id is not null)
            </if>
            <if test="req.bookName != null and req.bookName != ''">
                and book_name like '%${req.bookName}%'
            </if>
            <if test="req.subject != null and req.subject != ''">
                and course_name = #{req.subject}
            </if>
            <if test="req.grade != null and req.grade != ''">
                and grade = #{req.grade}
            </if>
            <if test="req.version != null and req.version > 0">
                and version = #{req.version}
            </if>
            <if test="req.semester != null and req.semester > 0">
                and semester = #{req.semester}
            </if>
            <if test="req.bookType != null and req.bookType > -1">
                and book_type = #{req.bookType}
            </if>
            <if test="req.startTime != null and req.startTime != ''">
                and create_time > #{req.startTime}
            </if>
            <if test="req.endTime != null and req.endTime != ''">
                and '${req.endTime} 23:59:59' > create_time
            </if>
    </select>

    <update id="updateAnswerCorrecterIdByIds">
        update kmt_exercise_book set answer_correcter_id = #{teacherId}
        where id in
        <foreach collection="exerciseBookIds" open="(" item="exerciseBookId" separator="," close=")">
            #{exerciseBookId}
        </foreach>
    </update>

    <update id="updateAnswerCorrecterIdByExerciseBookIds">
        update kmt_exercise_questions set answer_correcter_id = #{teacherId}
        where exercise_book_id in
        <foreach collection="exerciseBookIds" open="(" item="exerciseBookId" separator="," close=")">
            #{exerciseBookId}
        </foreach>
    </update>

    <select id="findExportDistributionExerciseBookList" resultType="com.ssj.service.kmt.exercisesys.dto.ExportDistributionExerciseBookDTO">
        select *
        from kmt_exercise_book_distribution_count
        <where>
          <if test="req.startTime != null and req.startTime != ''">
              and import_date > #{req.startTime}
          </if>
          <if test="req.endTime != null and req.endTime != ''">
              and '${req.endTime} 23:59:59' > import_date
          </if>
        </where>
        order by import_date desc
        limit #{offset}
    </select>

    <select id="findDistributionTeacher"
            resultType="com.ssj.service.kmt.exercisesys.dto.DistributionTeacherDTO">
        select teacher_id,
               teacher_name,
               salary_given,
               salary_given_time
               <if test="authorityType == 1">
                   , (select max(handler_submit_time)
                   from kmt_exercise_questions eq
                   where eq.exercise_book_id = #{exerciseBookId} and eq.deal_status > 0) lastSubmitTime
               </if>
               <if test="authorityType == 2">
                   , (select max(publish_time)
                   from kmt_exercise_questions eq
                   where eq.exercise_book_id = #{exerciseBookId} and eq.deal_status > 3) lastSubmitTime
               </if>
               <if test="authorityType == 3">
                   , (select max(analysis_handler_submit_time)
                   from kmt_exercise_questions eq
                   where eq.exercise_book_id = #{exerciseBookId} and eq.deal_status > 5) lastSubmitTime
               </if>
               <if test="authorityType == 4">
                   , (select max(analysis_submit_time)
                   from kmt_exercise_questions eq
                   where eq.exercise_book_id = #{exerciseBookId} and eq.deal_status = 11) lastSubmitTime
               </if>
        from kmt_exercise_book_teacher_record
        where exercise_book_id = #{exerciseBookId}
          and teacher_id = #{userId}
          and authority_type = #{authorityType}
        order by create_time desc
        limit 1
    </select>

    <select id="findAllExerciseBookPage" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
        select * from kmt_exercise_book
        where part_flag &lt;> 3
        <if test="selectedExerciseBookIdList != null and selectedExerciseBookIdList.size > 0">
            and id in
            <foreach collection="selectedExerciseBookIdList" open="(" item="selectedExerciseBookId" separator="," close=")">
                #{selectedExerciseBookId}
            </foreach>
        </if>
        limit #{offset}, #{pageSize}
    </select>


    <select id="countNotCheckAnswerCount" resultType="java.lang.Integer">
         select count(1) from kmt_exercise_questions where exercise_book_id=#{exerciseBookId}
         and deal_status != 2 and proofread_status = 0
    </select>

    <select id="countAllNotCheckAnswerCount" resultType="java.lang.Integer">
         select count(1) from kmt_exercise_questions where exercise_book_id=#{exerciseBookId}
         and proofread_status = 0
    </select>

    <select id="findExerciseBooksByBookId" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBook">
        select * from kmt_exercise_book where book_id=#{bookId}
    </select>

    <select id="findExerciseTeacherByExerciseBookIdsAndAuthType" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBookTeacherRecord">
        select * from kmt_exercise_book_teacher_record where exercise_book_id in
        <foreach collection="exerciseBookIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        and authority_type = #{authType}
    </select>

    <select id="findBookCountByExerciseBookIdAndType" resultType="com.ssj.bean.kmt.kmt.domain.KmtExerciseBookCount">
        select * from kmt_exercise_book_count where exercise_book_id=#{exerciseBookId} and type=#{type} limit 1
    </select>

    <update id="updateKmtExerciseBookCountById">
        UPDATE kmt_exercise_book_count set accuracy=#{accuracy} WHERE id = #{bookCountId}
    </update>
</mapper>