<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ssj.mapper.knowledge.KmtKnowledgeMapper">
    <select id="findByHowDetailIdAndTypeAndGradeAndSemester" resultType="com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetailsSort">
      select * from zyb_know_how_details_sort khds where khds.how_details_id in
      <foreach collection="howDetailIds" index="i" item="id" open="(" close=")" separator=",">
          #{id}
      </foreach>
       and grade=#{grade} and semester = #{semester} and type=#{type} and lib_id='SSJ'
  </select>

    <select id="findHowDetailsSortByHowDetailIdAndType" resultType="com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetailsSort">
        select * from zyb_know_how_details_sort where how_details_id = #{howDetailId} and type = #{type} and stat = 2 and lib_id = 'SSJ'
    </select>

    <select id="findHowSortByHowIdAndType" resultType="com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetailsSort">
        select * from zyb_know_how_details_sort where how_id = #{howId} and type = #{type} and stat = 1 and lib_id = 'SSJ'
    </select>

    <select id="findKnowledgeInfoByHowDetailIdAndType" resultType="java.util.Map">
        select detailSort.how_details_id,
               howSort.alias how_name,
               detailSort.alias knowledge_name,
               if(detailSort.course_name = '英语', detailSort.module, detailSort.two_module) module
        from zyb_know_how_details_sort detailSort,
             zyb_know_how_details_sort howSort
        where detailSort.how_id = howSort.how_id
          and howSort.type = detailSort.type
          and howSort.stat = 1
          and detailSort.how_details_id in
          <foreach collection="howDetailIdSet" open="(" item="howDetailId" separator="," close=")">
              #{howDetailId}
          </foreach>
          and detailSort.type = #{type}
          and detailSort.stat = 2
    </select>

    <select id="findKnowledgeListByModule" resultType="com.ssj.service.conch.parents.v2.dto.WrongBookKnowledgeDTO2">
        select group_concat(how_details_id) know_how_detail_id_str,
               alias knowledge_alias,
               0 wrong_question_sum,
               group_concat(#{version}) knowledge_version_str
        from zyb_know_how_details_sort
        where course_name = #{subject}
          and grade = #{grade}
          and semester = #{semester}
          and type = #{version}
          <if test="subject == '英语'">
              and module = #{module}
          </if>
          <if test="subject == '语文' || subject == '数学'">
              and two_module = #{module}
          </if>
        group by alias
    </select>

    <!--查询知识点列表-->
    <select id="findKmtKnowledgeByGradeCourseNameTypeSemester" resultType="com.ssj.bean.kmt.kmt.model.KmtKnowledgeModel">
        select t1.id know_how_id,
		       if(t3.is_zt = 1, concat('专题：', t1.alias), t1.alias) know_how_name,
		       t2.id know_how_details_id,
		       t2.alias          know_how_details_name,
		       t4.id how_details_kd_id,
		       t4.alias know_how_details_kd_name
		from zyb_know_how_details_sort t1
                   left join zyb_know_how_details_sort t2 on t1.how_id = t2.how_id  and t2.stat = 2
                   and t2.grade = #{grade}
                   and t2.course_name = #{courseName}
                   and t2.type = #{versionType}
                   and t2.semester = #{semester}
                   and t2.lib_id = 'SSJ'
                   join zyb_know_how_details t3 on t2.how_details_id = t3.id
        left join zyb_know_how_details_sort t4 on t2.how_details_id = t4.how_details_id and t4.stat = 3
                   and t4.grade = #{grade}
                   and t4.course_name = #{courseName}
                   and t4.type = #{versionType}
                   and t4.semester = #{semester}
                   and t4.lib_id = 'SSJ'
            where t1.stat = 1
              and t1.grade = #{grade}
              and t1.course_name = #{courseName}
              and t1.type = #{versionType}
              and t1.semester = #{semester}
              and t1.lib_id = 'SSJ'
        <if test="searchKey != null and searchKey != ''">
            and t2.alias REGEXP #{searchKey}
        </if>
        <if test="courseName != '数学' and moduleType != null">
          and t3.is_zt = #{moduleType}
        </if>
        order by t3.is_zt, t1.num, t1.create_time, t2.num, t3.num, t4.num
    </select>

    <!--查询知识点下的考点-->
    <select id="findDetailsListWithKd" resultType="com.ssj.bean.kmt.kmt.model.KmtKnowledgeModel">
        select kd.id how_details_kd_id, kd.alias know_how_details_kd_name, detail.id know_how_details_id, detail.alias know_how_details_name,
        detail.grade, detail.type, detail.semester, detail.course_name
        from zyb_know_how_details_sort detail
        left join zyb_know_how_details_sort kd on kd.how_details_id=detail.how_details_id and kd.stat=3
        and kd.type=detail.type and kd.semester=detail.semester and kd.grade=detail.grade and kd.course_name=detail.course_name
        where detail.id in
        <foreach collection="detailIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        order by detail.num, kd.num
    </select>

    <select id="findAllModule" resultType="java.util.Map">
        select
               <if test="subject == '语文' || subject == '数学'">
                   two_module real_module,
               </if>
               <if test="subject == '英语'">
                   module real_module,
               </if>
               group_concat(how_details_id) how_details_id_str
        from zyb_know_how_details_sort
        where stat = 2
          and grade = #{grade}
          and semester = #{semester}
          and course_name = #{subject}
          and type = #{version}
        group by real_module
    </select>

    <select id="findKnowledgeInfoByHowDetailIdAndTypeAndGradeAndSemester" resultType="java.util.Map">
        select detailSort.how_details_id,
               howSort.alias how_name,
               detailSort.alias knowledge_name,
               if(detailSort.course_name = '英语', detailSort.module, detailSort.two_module) module
        from zyb_know_how_details_sort detailSort,
             zyb_know_how_details_sort howSort
        where detailSort.how_id = howSort.how_id
              and howSort.type = detailSort.type
              and howSort.stat = 1
              and howSort.grade = #{grade}
              and howSort.semester = #{semester}
              and detailSort.how_details_id = #{howDetailId}
              and detailSort.type = #{version}
              and detailSort.stat = 2
              and detailSort.grade = #{grade}
              and detailSort.semester = #{semester}
              and detailSort.lib_id = 'SSJ'
        limit 1
    </select>

    <select id="countKnowledgeWrongAndTotalQuestion" resultType="java.util.Map">
        select
            scon_homework_how_details.how_detail_id,
            ifnull(sum(scon_homework_how_details.detail_count), 0) wrong_count,
            ifnull(sum(scon_homework_how_details.total_count), 0)  total_count
        from
            (select id
             from scon_homework
             where vip_id = #{vipId} and subject = #{subject} and grade = #{grade}
                   <if test="homeworkDate != ''">
                       and date(create_time) = #{homeworkDate}
                   </if>
                   and date(now()) > create_time
                   and date_format(create_time, '%m-%d')
                   <if test="semester == 1"> not </if> between '02-15' and '08-01') homework
            join scon_homework_how_details
                on scon_homework_how_details.homework_id = homework.id
                   and scon_homework_how_details.how_detail_id in
                   <foreach collection="knowledgeIdList" open="(" item="knowledgeId" separator="," close=")">
                       #{knowledgeId}
                   </foreach>
                   and scon_homework_how_details.grade = #{grade}
                   and scon_homework_how_details.semester = #{semester}
                   and scon_homework_how_details.knowledge_version = #{version}
        group by scon_homework_how_details.how_detail_id
    </select>

    <select id="countKnowledgeRightQuestionWithExercise" resultType="java.util.Map">
        select questionKnow.how_details_id,
               count(distinct questionKnow.homework_id, questionKnow.question_id) right_count
        from (select
                  question.question_id,
                  question.homework_id,
                  kmt_exercise_question_know.how_details_id,
                  kmt_exercise_question_know.how_detail_sort_id
              from (select kmt_homework_wrong_question.question_id,
                           kmt_homework_wrong_question.homework_id
                    from
                        (select id
                         from scon_homework
                         where vip_id = #{vipId} and subject = #{subject} and grade = #{grade}
                         <if test="homeworkDate != ''">
                             and date(create_time) = #{homeworkDate}
                         </if>
                         and date(now()) > create_time
                         and date_format(create_time, '%m-%d')
                         <if test="semester == 1"> not </if> between '02-15' and '08-01') homework
                        join kmt_homework_wrong_question
                            on kmt_homework_wrong_question.homework_id = homework.id and
                               kmt_homework_wrong_question.type = 2) question
                  join kmt_exercise_question_know
                      on question.question_id = kmt_exercise_question_know.question_id
                         and kmt_exercise_question_know.how_details_id in
                         <foreach collection="knowledgeIdList" open="(" item="knowledgeId" separator="," close=")">
                             #{knowledgeId}
                         </foreach>
             ) questionKnow
            join zyb_know_how_details_sort
                on questionKnow.how_detail_sort_id = zyb_know_how_details_sort.id
                   and zyb_know_how_details_sort.course_name = #{subject}
                   and zyb_know_how_details_sort.grade = #{grade}
                   and zyb_know_how_details_sort.semester = #{semester}
                   and zyb_know_how_details_sort.type = #{version}
        group by questionKnow.how_details_id
    </select>

    <select id="findKnowHowDetailsSortMap" resultType="java.util.Map">
        select knowHowDetail.id, knowHowDetail.how_details_id, knowHowDetail.how_id, knowHowDetail.type,
               knowHowDetail.course_name, knowHowDetail.grade, knowHowDetail.grade_num, knowHowDetail.semester,
               knowHowDetail.num, knowHowDetail.lib_id,
               date_format(knowHowDetail.create_time, '%Y-%m-%dT%H:%i:%s.000Z')        create_time,
               ifnull(knowHow.alias, '') unit_name,
               ifnull(knowHow.num, 0) unit_num,
               knowHowDetail.how_details_kd_id, knowHowDetail.stat, knowHowDetail.alias,
               knowHowDetail.before_how_details_kd_id, knowHowDetail.memorize, knowHowDetail.understanding,
               knowHowDetail.apply, knowHowDetail.two_module, knowHowDetail.module, knowHowDetail.jxmb,
               knowHowDetail.zdbx, knowHowDetail.ydsl, knowHowDetail.attr_type,
               0 question_status
        from zyb_know_how_details_sort knowHowDetail, zyb_know_how_details_sort knowHow
        where knowHow.how_id = knowHowDetail.how_id
          and knowHow.grade = knowHowDetail.grade
          and knowHow.semester = knowHowDetail.semester
          and knowHow.type = knowHowDetail.type
          and knowHow.stat = 1
          and knowHowDetail.stat = 2
          and knowHowDetail.lib_id = 'SSJ'
          and knowHowDetail.how_details_id is not null
        limit #{offset}, #{pageSize}
    </select>

    <select id="countExerciseQuestionKnow" resultType="java.lang.Integer">
        select count(*)
        from (select question_id from kmt_exercise_question_know where how_details_id = #{howDetailsId}) eqk
                 join kmt_exercise_questions eq
                      on eqk.question_id = eq.id and eq.book_id is not null and (eq.deal_status in (4, 5) or eq.deal_status > 7)
                          and (eq.real_exam_type is null or eq.real_exam_type &lt;> '4')
    </select>

    <select id="findKnowledgeIdAndNameAndModule" resultType="java.util.Map">
        select how_details_id, alias,
            <if test="subject == '语文' || subject == '数学'">
                two_module module
            </if>
            <if test="subject == '英语'">
                module
            </if>
        from zyb_know_how_details_sort
        where stat = 2 and lib_id = 'SSJ' and course_name = #{subject} and grade = #{grade}
              and semester = #{semester} and type = #{version}
        limit 1000
    </select>

</mapper>