<?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.KmtQuestionMapper">
    <update id="updateEsLoadStatus">
        update kmt_question_know set es_loaded = #{newEsLoaded}
        where id in
            <foreach collection="kmtQuestionKnowIdList" open="(" item="kmtQuestionKnowId" separator="," close=")">
                #{kmtQuestionKnowId}
            </foreach>
    </update>

    <select id="findTrainRecommendQuestion" resultType="java.util.Map">
        (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.easy difficulty
         from kmt_question,
              kmt_question_know
         where kmt_question.id = kmt_question_know.question_id
           and kmt_question_know.how_details_id in
           <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
               #{howDetailsId}
           </foreach>
           <if test="excludeQuestionIdSet.size() > 0">
               and kmt_question.id not in
               <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                   #{questionId}
               </foreach>
           </if>
           and kmt_question.easy = 1
           and ((1 > ifnull(length(kmt_question.parent_id), 0)
             and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
             (ifnull(length(kmt_question.parent_id), 0) > 0))
         group by kmt_question.id
         order by rand()
         <if test="type == 1">
             limit 2
         </if>
         <if test="type == 2">
             limit 1
         </if>
        )
        union all
        (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.easy difficulty
         from kmt_question,
              kmt_question_know
         where kmt_question.id = kmt_question_know.question_id
           and kmt_question_know.how_details_id in
           <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
               #{howDetailsId}
           </foreach>
           <if test="excludeQuestionIdSet.size() > 0">
               and kmt_question.id not in
               <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                   #{questionId}
               </foreach>
           </if>
           and kmt_question.easy = 2
           and ((1 > ifnull(length(kmt_question.parent_id), 0)
             and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
             (ifnull(length(kmt_question.parent_id), 0) > 0))
         group by kmt_question.id
         order by rand()
         limit 1)
        <if test="type == 2">
            union all
            (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.easy difficulty
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.easy = 3
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0))
             group by kmt_question.id
             order by rand()
             limit 1)
        </if>
    </select>

    <select id="findFullQuestion" resultType="java.util.Map">
        select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.option_content, kmt_question.easy difficulty
        from kmt_question
        where kmt_question.id = #{parentId}
           or kmt_question.parent_id = #{parentId}
        order by kmt_question.num
    </select>

    <select id="countTrainRecommendQuestionV2" resultType="java.util.Map">
        <if test="recommendNumMap.easyNum > 0">
            (select count(distinct kmt_question.id) easy_num, 0 middle_num, 0 difficult_num
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.easy = 1
               and kmt_question.state = 1
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0))
            )
        </if>
        <if test="recommendNumMap.middleNum > 0">
            <if test="recommendNumMap.easyNum > 0">
                union all
            </if>
            (select 0 easy_num, count(distinct kmt_question.id) middle_num, 0 difficult_num
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.easy = 2
               and kmt_question.state = 1
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0)))
        </if>
        <if test="recommendNumMap.difficultNum > 0">
            <if test="recommendNumMap.easyNum > 0 or recommendNumMap.middleNum > 0">
                union all
            </if>
            (select 0 easy_num, 0 middle_num, count(distinct kmt_question.id) difficult_num
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.easy = 3
               and kmt_question.state = 1
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0)))
        </if>
    </select>

    <select id="findTrainRecommendQuestionV2" resultType="java.util.Map">
        <if test="recommendNumMap.easyLimitOffset > -1 and recommendNumMap.easyNum > 0">
            (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.option_content, kmt_question.easy difficulty, kmt_question.option_type option_type_int
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.state = 1
               and kmt_question.easy = 1
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0))
             limit #{recommendNumMap.easyLimitOffset}, #{recommendNumMap.easyNum}
            )
        </if>
        <if test="recommendNumMap.middleLimitOffset > -1 and recommendNumMap.middleNum > 0">
            <if test="recommendNumMap.easyLimitOffset > -1">
                union all
            </if>
            (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.option_content, kmt_question.easy difficulty, kmt_question.option_type option_type_int
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.state = 1
               and kmt_question.easy = 2
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0))
             limit #{recommendNumMap.middleLimitOffset}, #{recommendNumMap.middleNum})
        </if>
        <if test="recommendNumMap.difficultLimitOffset > -1 and recommendNumMap.difficultNum > 0">
            <if test="recommendNumMap.easyLimitOffset > -1 or recommendNumMap.middleLimitOffset > -1">
                union all
            </if>
            (select kmt_question.id, kmt_question.parent_id, kmt_question.question, kmt_question.option_content, kmt_question.easy difficulty, kmt_question.option_type option_type_int
             from kmt_question,
                  kmt_question_know
             where kmt_question.id = kmt_question_know.question_id
               and kmt_question_know.how_details_id in
               <foreach collection="knowHowDetailIdSet" open="(" item="howDetailsId" separator="," close=")">
                   #{howDetailsId}
               </foreach>
               <if test="excludeQuestionIdSet.size() > 0">
                   and kmt_question.id not in
                   <foreach collection="excludeQuestionIdSet" open="(" item="questionId" separator="," close=")">
                       #{questionId}
                   </foreach>
               </if>
               and kmt_question.state = 1
               and kmt_question.easy = 3
               and kmt_question.type = #{recommendNumMap.type}
               and ((1 > ifnull(length(kmt_question.parent_id), 0)
                 and not exists(select id from kmt_question temp where kmt_question.id = temp.parent_id)) or
                 (ifnull(length(kmt_question.parent_id), 0) > 0))
             limit #{recommendNumMap.difficultLimitOffset}, #{recommendNumMap.difficultNum})
        </if>
    </select>

    <!--查询知识点对应的题目，知识点命中数量越多，排序越靠前-->
    <select id="findQuestionByHowDetailIds" resultType="com.ssj.bean.conch.conch.dto.KmtQuestionDto">
        select q.id question_id, q.analysis, q.question, q.option_content, count(qk.how_details_id) num from kmt_question_know qk
        left join kmt_question q on qk.question_id=q.id where qk.how_details_id in
        <foreach collection="howDetailIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        and q.type=1
        group by q.id, q.analysis, q.question, q.option_content
        order by num desc limit #{count}
    </select>

    <!--查询题目列表-->
    <select id="findQuestionByIds" resultType="com.ssj.bean.conch.conch.dto.KmtQuestionDto">
        select q.id question_id, q.analysis, q.question, q.option_content from kmt_question q where id in
        <foreach collection="list" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="findWrongQuestionKnowModuleList" resultType="java.lang.String">
        select distinct
            <if test="subject == '语文' || subject == '数学'">
                t1.two_module
            </if>
            <if test="subject == '英语'">
                t1.module
            </if>
        from
            scon_homework k1 join
            scon_homework_how_details s1 on k1.subject = #{subject} and s1.is_wrong_ques_delete = 0 and s1.detail_count > 0
                and s1.homework_id = k1.id
            join zyb_know_how_details_sort t1 on t1.stat = 2 and t1.how_details_id = s1.how_detail_id
        where t1.type = s1.knowledge_version
            and s1.is_wrong_ques_delete = 0
            and s1.detail_count > 0
            <if test="type == 2">
                and s1.wrong_question_id is not null
            </if>
            and k1.vip_id = #{vipId}
            and k1.subject = #{subject}
            <if test="beginDateTimeStr != ''">
                and k1.create_time > #{beginDateTimeStr}
            </if>
            <if test="endDateTimeStr != ''">
                and #{endDateTimeStr} > k1.create_time
            </if>
    </select>

    <select id="findWrongQuestionKnowledgePage" resultType="com.ssj.service.conch.parents.v2.dto.WrongBookKnowledgeDTO2">
        select group_concat(s1.how_detail_id) know_how_detail_id_str,
                t1.alias knowledge_alias,
                count(distinct e1.id) wrong_question_sum,
                group_concat(t1.type) knowledge_version_str,
                1 questionStatus
        from zyb_know_how_details_sort t1
            join scon_homework_how_details s1 on t1.stat = 2 and t1.how_details_id = s1.how_detail_id
            join scon_homework_picture e1 on s1.homework_picture_id = e1.id and e1.subject = #{subject}
        where s1.is_wrong_ques_delete = 0
            and s1.detail_count > 0
            and t1.type = s1.knowledge_version
            <if test="subject == '语文' || subject == '数学'">
                and t1.two_module = #{module}
            </if>
            <if test="subject == '英语'">
                and t1.module = #{module}
            </if>
            and e1.vip_id = #{vipId}
            <if test="beginDateTimeStr != ''">
                and e1.create_time > #{beginDateTimeStr}
            </if>
            <if test="endDateTimeStr != ''">
                and #{endDateTimeStr} > e1.create_time
            </if>
            and e1.subject = #{subject}
            group by knowledge_alias
    </select>

    <select id="findWrongBookQuestionPage" resultType="com.ssj.service.conch.parents.v2.dto.WrongBookQuestionDTO">
        select temp.homework_id,
               temp.create_time,
               group_concat(temp.homework_picture_id) homework_picture_id_str,
               group_concat(temp.picture_url)         picture_url_str
        from (
                 <foreach collection="req.knowHowDetailList" item="knowHowDetail" separator=" union all ">
                     (select scon_homework_picture.id                homework_picture_id,
                             scon_homework_picture.homework_id,
                             date(scon_homework_picture.create_time) create_time,
                             if(length(scon_homework_picture.corrected_picture_url) > 0,
                                scon_homework_picture.corrected_picture_url,
                                scon_homework_picture.file_key)      picture_url
                      from scon_homework_picture,
                           scon_homework_how_details
                      where scon_homework_picture.id = scon_homework_how_details.homework_picture_id
                        <if test="!includeDeleted">
                            and scon_homework_how_details.is_wrong_ques_delete = 0
                        </if>
                        and scon_homework_how_details.detail_count > 0
                        <if test="req.type == 2">
                            and scon_homework_how_details.wrong_question_id is not null
                        </if>
                        and scon_homework_picture.vip_id = #{req.vipId}
                        <if test="beginDateTimeStr != ''">
                            and scon_homework_picture.create_time > #{beginDateTimeStr}
                        </if>
                        <if test="endDateTimeStr != ''">
                            and #{endDateTimeStr} > scon_homework_picture.create_time
                        </if>
                        and scon_homework_how_details.how_detail_id = #{knowHowDetail.knowHowDetailId}
                        and scon_homework_how_details.knowledge_version = #{knowHowDetail.knowledgeVersion}
                      group by create_time, scon_homework_how_details.homework_picture_id)
                 </foreach>
             ) temp
        group by create_time, homework_id
        order by create_time desc, homework_id
    </select>

    <select id="findWrongQuestionKnowModuleListV2" resultType="java.util.Map">
        select
            <if test="subject == '语文' || subject == '数学'">
                zyb_know_how_details_sort.two_module real_module,
            </if>
            <if test="subject == '英语'">
                zyb_know_how_details_sort.module real_module,
            </if>
            group_concat(how_details_id) how_details_id_str,
            group_concat(type) version_str
        from zyb_know_how_details_sort,
            scon_homework_how_details,
            scon_homework
        where zyb_know_how_details_sort.stat = 2
            and zyb_know_how_details_sort.how_details_id = scon_homework_how_details.how_detail_id
            and zyb_know_how_details_sort.type = scon_homework_how_details.knowledge_version
            and scon_homework_how_details.is_wrong_ques_delete = 0
            and scon_homework_how_details.detail_count > 0
            and scon_homework_how_details.homework_id = scon_homework.id
            and scon_homework.vip_id = #{vipId}
            and scon_homework.subject = #{subject}
            <if test="beginDateTimeStr != ''">
                and scon_homework.create_time > #{beginDateTimeStr}
            </if>
            <if test="endDateTimeStr != ''">
                and #{endDateTimeStr} > scon_homework.create_time
            </if>
        group by real_module
    </select>

    <select id="findByHowDetailKdId" resultType="java.util.Map">
        select kmt_question.id,
               kmt_question.parent_id,
               kmt_question.question,
               kmt_question.option_content,
               kmt_question.easy difficulty,
               kmt_question.option_type
        from kmt_question,
             kmt_question_know
        where kmt_question.id = kmt_question_know.question_id
          and kmt_question_know.how_details_kd_id in
          <foreach collection="wrongQuestionHowDetailKdIdList" open="(" item="wrongQuestionHowDetailKdId" separator="," close=")">
              #{wrongQuestionHowDetailKdId}
          </foreach>
          <if test="existQuestionIdSet.size() > 0">
              and kmt_question_know.question_id not in
              <foreach collection="existQuestionIdSet" open="(" item="existQuestionId" separator="," close=")">
                  #{existQuestionId}
              </foreach>
          </if>
        group by how_details_kd_id
    </select>

    <select id="findWrongQuestionKnowModuleListV3New" resultType="java.lang.String">
        select distinct
            <if test="subject == '语文' || subject == '数学'">
                scon_knowledge_wrong_question_count.two_module
            </if>
            <if test="subject == '英语'">
                scon_knowledge_wrong_question_count.module
            </if>
        from scon_knowledge_wrong_question_count
        where mastered = 0
            and vip_id = #{vipId}
            and subject = #{subject}
            <if test="type == 2">
                and question_status > 0
            </if>
            <if test="beginDateTimeStr != ''">
                and homework_time > #{beginDateTimeStr}
            </if>
            <if test="endDateTimeStr != ''">
                and #{endDateTimeStr} > homework_time
            </if>
    </select>

    <select id="findWrongQuestionKnowledgePageV4New"
            resultType="com.ssj.service.conch.parents.v2.dto.WrongBookKnowledgeDTO2">
        select knowledge_name knowledge_alias,
            sum(wrong_count) wrong_question_sum,
            group_concat(how_detail_id) know_how_detail_id_str,
            group_concat(knowledge_version)         knowledge_version_str,
            group_concat(question_status) question_status_str
        from scon_knowledge_wrong_question_count
        where mastered = 0
            and vip_id = #{vipId}
            and subject = #{subject}
            <if test="type == 2">
                and question_status > 0
            </if>
            <if test="subject == '语文' || subject == '数学'">
                and two_module = #{module}
            </if>
            <if test="subject == '英语'">
                and module = #{module}
            </if>
            <if test="beginDateTimeStr != ''">
                and homework_time > #{beginDateTimeStr}
            </if>
            <if test="endDateTimeStr != ''">
                and #{endDateTimeStr} > homework_time
            </if>
        group by knowledge_name
    </select>

    <select id="findAll2Es" resultType="java.util.Map">
        select questionKnow.*,
               kmt_question.course_name,
               ifnull(parent_id, '') parent_id,
               question,
               option_content,
               option_answer,
               option_type,
               type,
               easy,
               explain_text,
               analysis,
               thinking,
               suggest,
               date_format(update_time, '%Y-%m-%dT%H:%i:%s.000Z') update_time,
               state,
               source,
               checkuser,
               skill,
               kmt_question.remarks,
               min_option_type,
               ifnull(kmt_question.num, 0) num,
               tag,
               adduser,
               adduser_id,
               source_name,
               is_copy
        from (select id,
                     question_id,
                     how_details_id,
                     how_id,
                     course_name,
                     how_details_kd_id,
                     tags,
                     date_format(create_time, '%Y-%m-%dT%H:%i:%s.000Z') create_time,
                     date_format(last_update_time, '%Y-%m-%dT%H:%i:%s.000Z') last_update_time,
                     remarks
              from kmt_question_know
              where es_loaded = 0
              limit #{pageSize}) questionKnow
                 join kmt_question on questionKnow.question_id = kmt_question.id
    </select>

    <!-- 查询kmt老师详情 -->
    <select id="findKmtTeacherDetails" resultType="com.ssj.service.kmt.exercisesys.dto.KmtTeacherDetailDto">
        select detail.id detail_id, detail.alias detail_name, detail.grade, detail.semester, detail.course_name, detail.type version_type,
               etd.total_count, etd.un_finish_count, etd.question_total_count, etd.question_unfinish_count, etd.rollback_question_count
        from kmt_exercise_teacher_datalist etd left join zyb_know_how_details_sort detail
        on etd.business_id=detail.id and detail.stat=2
        where 1=1
            and etd.teacher_id=#{userId}
            and etd.type = #{type}
        <if test="detailName != null and detailName != ''">
            and detail.alias regexp #{detailName}
        </if>
        order by etd.un_finish_count desc, etd.id
    </select>
</mapper>