<?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.ruoyi.ie.mapper.QuestionsMapper">

    <resultMap type="Questions" id="QuestionsResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="title1"    column="title1"    />
        <result property="title0"    column="title0"    />
        <result property="options0"    column="options0"    />
        <result property="answer0"    column="answer0"    />
        <result property="parse0"    column="parse0"    />

        <result property="options"    column="options"    />
        <result property="optionA"    column="option_a"    />
        <result property="optionB"    column="option_b"    />
        <result property="optionC"    column="option_c"    />
        <result property="optionD"    column="option_d"    />
        <result property="optionE"    column="option_e"    />
        <result property="optionF"    column="option_f"    />
        <result property="optionG"    column="option_g"    />
        <result property="answers"    column="answers"    />
        <result property="answer1"    column="answer1"    />
        <result property="answer2"    column="answer2"    />
        <result property="parse"    column="parse"    />
        <result property="qtpye"    column="qtpye"    />
        <result property="diff"    column="diff"    />
        <result property="md5"    column="md5"    />
        <result property="subjectId"    column="subjectId"    />
        <result property="gradeId"    column="gradeId"    />
        <result property="knowledges"    column="knowledges"    />
        <result property="area"    column="area"    />
        <result property="year"    column="year"    />
        <result property="paperTpye"    column="paperTpye"    />
        <result property="source"    column="source"    />
        <result property="fromSite"    column="fromSite"    />
        <result property="isSub"    column="isSub"    />
        <result property="isSubType"    column="isSubType"    />
        <result property="isNormal"    column="isNormal"    />
        <result property="isKonw"    column="isKonw"    />
        <result property="tiid"    column="tiid"    />
        <result property="similarity"    column="Similarity"    />
        <result property="isunique"    column="isunique"    />
        <result property="md52"    column="md52"    />
        <result property="score"    column="score"    />
        <result property="paperTitle"    column="paperTitle"    />
        <result property="collect"    column="collect"    />
        <result property="seq"    column="seq"    />
        <result property="paperId"    column="paper_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectQuestionsVo">
        select id, title, title1, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2, parse, qtpye, diff, md5, subjectId, gradeId, knowledges, area, year, paperTpye, source, fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52 from questions
    </sql>

    <select id="getQuestionsCountByChapterIds"  resultType="int">
        select COUNT(*) num from chapter_question_id where 1 = 1
        and chapter_id in
        <foreach item="id" collection="chapterIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>


    <select id="getPaperByKnowledgeLevelId"  resultMap="QuestionsResult">
        select ques.* from questions ques where 1=1
        <if test="difficulty == 0">and ques.diff in (1,2)</if>
        <if test="difficulty == 1">and  ques.diff = 3 </if>
        <if test="difficulty == 2">and  ques.diff in (4,5)</if>
         and ques.id in (
        select question_id from question_knowledge_basic_id
        where knowledge_basic_id in
        <foreach item="id" collection="ids" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
        )
        and ques.qtpye = #{qtype}
        order by rand() limit 0,#{number}
    </select>

    <select id="getPaperByChapterId" resultMap="QuestionsResult">
        select * from questions  where 1=1
        <if test="difficulty == 0">and diff in (1,2)</if>
        <if test="difficulty == 1">and  diff = 3 </if>
        <if test="difficulty == 2">and  diff in (4,5)</if>
        and id in (
        select question_id from chapter_question_id where chapter_id in
        <foreach item="id" collection="ids" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
        )
        and qtpye = #{qtype}
        order by rand() limit 0,#{number}
    </select>


    <select id="selectQuestionsByMxjbPaperId" parameterType="map" resultMap="QuestionsResult">
      SELECT p.paper_id, q.knowledges, pq.knowledge_id, pq.seq, q.id, q.qtpye, q.title, q.title1, q.source, p.subjectId, q.isSub, pq.answers, q.diff,
      IF(pq.parse IS NULL, q.parse, pq.parse) parse,
      IF(pq.answer1 IS NULL, q.answer1, pq.answer1) answer1,
      IF(pq.answer2 IS NULL, q.answer2, pq.answer2) answer2,
      q.option_a, q.option_b, q.option_c, q.option_d, q.option_e, q.option_f, q.option_g, pq.score, q.isSubType
      <if test="userId != null">
        , if(qc.user_id is null, 0, 1) collect
      </if>
    FROM mxjb_paper p
    JOIN mxjb_paper_question pq ON pq.paper_id = p.paper_id
    JOIN questions q ON q.id = pq.question_id
    <if test="userId != null">
    LEFT JOIN mxjb_question_collection qc ON qc.question_id = pq.question_id AND qc.user_id = #{userId}
    </if>
    <where>
        <if test="paperId != null"> AND p.paper_id = #{paperId}</if>
        <if test="paperIds != null"> AND p.paper_id in <foreach item="id" collection="paperIds" open="(" separator="," close=")">#{id}</foreach></if>
    </where>
    ORDER BY pq.seq
    </select>

    <select id="selectQuestionsByAiPaperId" parameterType="map" resultMap="QuestionsResult">
        SELECT p.paper_id, q.knowledges, pq.knowledge_id, pq.seq, q.id, q.qtpye, q.title, q.title1, q.source, p.subjectId, q.isSub, pq.answers, q.diff,
        IF(pq.parse IS NULL, q.parse, pq.parse) parse,
        IF(pq.answer1 IS NULL, q.answer1, pq.answer1) answer1,
        IF(pq.answer2 IS NULL, q.answer2, pq.answer2) answer2,
        q.option_a, q.option_b, q.option_c, q.option_d, q.option_e, q.option_f, q.option_g, pq.score, q.isSubType
        <if test="userId != null">
            , if(qc.user_id is null, 0, 1) collect
        </if>
        FROM mxjb_ai_paper p
        JOIN mxjb_ai_paper_question pq ON pq.paper_id = p.paper_id
        JOIN questions q ON q.id = pq.question_id
        <if test="userId != null">
            LEFT JOIN mxjb_question_collection qc ON qc.question_id = pq.question_id AND qc.user_id = #{userId}
        </if>
        <where>
            <if test="paperId != null"> AND p.paper_id = #{paperId}</if>
            <if test="paperIds != null"> AND p.paper_id in <foreach item="id" collection="paperIds" open="(" separator="," close=")">#{id}</foreach></if>
        </where>
        ORDER BY pq.seq
    </select>

    <select id="selectQuestionsByHomeworkPaperId" parameterType="map" resultMap="QuestionsResult">
        SELECT p.paper_id, q.knowledges, pq.knowledge_id, pq.seq, q.id, q.qtpye, q.title, q.title1, q.source, p.subjectId, q.isSub, pq.answers, q.diff,
        IF(pq.parse IS NULL, q.parse, pq.parse) parse,
        IF(pq.answer1 IS NULL, q.answer1, pq.answer1) answer1,
        IF(pq.answer2 IS NULL, q.answer2, pq.answer2) answer2,
        q.option_a, q.option_b, q.option_c, q.option_d, q.option_e, q.option_f, q.option_g, pq.score, q.isSubType
        <if test="userId != null">
            , if(qc.user_id is null, 0, 1) collect
        </if>
        FROM mxjb_customer_paper p
        JOIN mxjb_customer_paper_question pq ON pq.paper_id = p.paper_id
        JOIN questions q ON q.id = pq.question_id
        <if test="userId != null">
            LEFT JOIN mxjb_question_collection qc ON qc.question_id = pq.question_id AND qc.user_id = #{userId}
        </if>
        <where>
            <if test="paperId != null"> AND p.paper_id = #{paperId}</if>
            <if test="paperIds != null"> AND p.paper_id in <foreach item="id" collection="paperIds" open="(" separator="," close=")">#{id}</foreach></if>
        </where>
        ORDER BY pq.seq
    </select>

    <select id="selectQuestionsByZtPaperId" parameterType="map" resultMap="QuestionsResult">
        SELECT q.* FROM `paperques` pq
        LEFT JOIN questions q ON q.id = pq.`question_id`
        <where>
            <if test="type != null"> AND pq.type  = #{type}</if>
            <if test="paperId != null"> AND pq.paperId  = #{paperId}</if>
            <if test="paperIds != null"> AND pq.paperId in <foreach item="paperId" collection="paperIds" open="(" separator="," close=")">#{paperId}</foreach></if>
        </where>
        ORDER BY pq.`id`
    </select>

    <select id="selectQuestionsByPaperId" parameterType="map" resultMap="QuestionsResult">
    SELECT q.knowledges, 0 seq, q.id, q.qtpye, q.title, q.title1, q.source, NULL, q.`subjectId`, q.isSub, q.parse, q.answer1, q.answer2, q.option_a, q.option_b, q.option_c, q.option_d, q.option_e, if(pq.sorces is null, q.score, pq.sorces) score, q.knowledgeId, qc.question_id > 0 collect, q.isSubType
    FROM papers p
    JOIN paperques pq ON pq.paperId = p.id
    JOIN questions q ON q.id = pq.question_id
    LEFT JOIN mxjb_question_collection qc on qc.question_id = q.id and qc.user_id = #{userId}
    WHERE p.id = #{paperId} AND (q.isSub is null or q.isSub = 0 or q.id = q.isSubType)
    ORDER BY pq.seq, IF(pq.`type` = 'paper', q.id, pq.id)
    </select>

  <select id="listQuestionsByPaperId"  resultMap="QuestionsResult">
    SELECT q.*
    FROM papers p
    left JOIN paperques pq ON pq.paperId = p.id
    left JOIN questions q ON q.id = pq.question_id
    WHERE p.id = ${paperId}
    and q.id is not null
    ORDER BY q.id
    </select>

    <select id="selectSimilarQuestions" parameterType="map" resultMap="QuestionsResult">
      SELECT n.id
        FROM questions n
        LEFT JOIN mxjb_wrong_book w ON w.question_id = n.id AND w.customer_code = #{customerCode}
      <where> n.id != #{questionId} AND w.question_id IS NULL AND n.qtpye = #{qtpye} AND n.gradeId = #{gradeId} and n.subjectId = #{subjectId}
          <if test="isSub != null"> AND n.isSub = #{isSub}</if>
          <if test="known != null"> AND n.knowledges = #{known}</if>
      </where>
      LIMIT 500
    </select>


    <select id="selectTestQuestionsByMapCount" parameterType="map" resultType="int">
      SELECT COUNT(*)
        FROM `paperques` pq
        JOIN questions q ON q.id = pq.`question_id`
        LEFT JOIN `mxjb_paper_answers` a ON a.`question_id` = q.`id`
        LEFT JOIN `mxjb_paper_examinee` e ON e.`customer_code` = #{userId} AND a.`examinee_id` = e.`examinee_id`
        <where> a.`answer_id` IS NULL AND (q.isSub is null or q.id = q.isSubType)
            <if test="type != null"> AND pq.type  = #{type}</if>
            <if test="paperId != null"> AND pq.paperId  = #{paperId}</if>
            <if test="incTypes != null"> and qtpye in <foreach item="o" collection="incTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="excTypes != null"> and qtpye not in <foreach item="o" collection="excTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="diff != null "> and diff = #{diff}</if>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="gradeId != null "> and gradeId = #{gradeId}</if>
            <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="similarity != null "> and Similarity = #{similarity}</if>
        </where>
    </select>

    <select id="selectTestQuestionsByMap" parameterType="map" resultMap="QuestionsResult">
        select distinct q.id, title, title1, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2,
        parse, qtpye, diff, md5, subjectId,
        gradeId, knowledges, area, year, paperTpye, source,
        fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52, isSubType
        FROM `paperques` pq JOIN questions q ON q.id = pq.`question_id`
        LEFT JOIN (SELECT question_id FROM `mxjb_paper_examinee` e JOIN `mxjb_paper_answers` a ON a.`examinee_id` = e.`examinee_id` WHERE e.`customer_code` = #{userId}) T ON T.question_id = q.id
        <where>T.`question_id` IS NULL AND (q.isSub is null or q.id = q.isSubType)
            <if test="type != null"> AND pq.type  = #{type}</if>
            <if test="paperId != null"> AND pq.paperId  = #{paperId}</if>
            <if test="incTypes != null"> and qtpye in <foreach item="o" collection="incTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="excTypes != null"> and qtpye not in <foreach item="o" collection="excTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="diff != null "> and diff = #{diff}</if>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="gradeId != null "> and gradeId = #{gradeId}</if>
            <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="similarity != null "> and Similarity = #{similarity}</if>
        </where>
        limit #{startPos}, 100
    </select>

    <select id="selectQuestionsByMapCount" parameterType="map" resultType="int">
        select count(*)
        from questions q
        <where>
            <if test="incTypes != null"> and qtpye in <foreach item="o" collection="incTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="excTypes != null"> and qtpye not in <foreach item="o" collection="excTypes" open="(" separator="," close=")">#{o}</foreach></if>
            <if test="diff != null "> and diff = #{diff}</if>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="gradeId != null "> and gradeId = #{gradeId}</if>
            <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="similarity != null "> and Similarity = #{similarity}</if>
        </where>
    </select>

    <select id="selectQuestionsByMap" parameterType="map" resultMap="QuestionsResult">
        select id, title, title1, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2,
        parse, qtpye, diff, md5, subjectId,
        gradeId, knowledges, area, year, paperTpye, source,
        fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52, q.isSubType
        from questions q
        <where>
        <if test="incTypes != null"> and qtpye in <foreach item="o" collection="incTypes" open="(" separator="," close=")">#{o}</foreach></if>
        <if test="excTypes != null"> and qtpye not in <foreach item="o" collection="excTypes" open="(" separator="," close=")">#{o}</foreach></if>
        <if test="diff != null "> and diff = #{diff}</if>
        <if test="subjectId != null "> and subjectId = #{subjectId}</if>
        <if test="gradeId != null "> and gradeId = #{gradeId}</if>
        <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
        <if test="area != null  and area != ''"> and area = #{area}</if>
        <if test="year != null "> and year = #{year}</if>
        <if test="similarity != null "> and Similarity = #{similarity}</if>
        limit #{startPos}, 100
        </where>
    </select>

    <select id="selectQuestionsForAiSubject" parameterType="map" resultMap="QuestionsResult">
SELECT pq.`create_time`, p.paper_id, q.knowledges, pq.knowledge_id, e.`examinee_id` seq, q.id, q.qtpye, q.title, q.source, p.subjectId, q.isSub, pq.answers, q.diff,
        IF(pq.parse IS NULL, q.parse, pq.parse) parse,
        IF(pq.answer1 IS NULL, q.answer1, pq.answer1) answer1,
        IF(pq.answer2 IS NULL, q.answer2, pq.answer2) answer2,
        q.option_a, q.option_b, q.option_c, q.option_d, q.option_e, pq.score, q.isSubType
      FROM mxjb_ai_paper p
      JOIN `mxjb_ai_examinee` e ON e.`paper_id` = p.`paper_id`
      JOIN `mxjb_ai_paper_question` pq ON pq.`paper_id` = p.`paper_id`
      JOIN questions q ON q.id = pq.`question_id`
     WHERE p.`creator_id` = #{creatorId} and p.subjectId = #{subjectId}
     ORDER BY pq.`create_time` DESC
    </select>

    <select id="selectQuestionsList" parameterType="Questions" resultMap="QuestionsResult">
        select id, title, title1, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2,
        parse, qtpye, diff, md5, subjectId, (select subjectName from subject s where s.subjectId=q.subjectId) subjectName,
        gradeId, knowledges, area, year, paperTpye, source,
        fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52, q.isSubType
        from questions q
        where 1=1
            <if test="id != null "> and id = #{id}</if>
            <if test="title != null  and title != ''"> and title like concat('%',#{title},'%')</if>
            <if test="optionA != null  and optionA != ''"> and option_a = #{optionA}</if>
            <if test="optionB != null  and optionB != ''"> and option_b = #{optionB}</if>
            <if test="optionC != null  and optionC != ''"> and option_c = #{optionC}</if>
            <if test="optionD != null  and optionD != ''"> and option_d = #{optionD}</if>
            <if test="optionE != null  and optionE != ''"> and option_e = #{optionE}</if>
            <if test="answer1 != null  and answer1 != ''"> and answer1 = #{answer1}</if>
            <if test="answer2 != null  and answer2 != ''"> and answer2 = #{answer2}</if>
            <if test="parse != null  and parse != ''"> and parse = #{parse}</if>
           <if test="qtpye != null  and qtpye != ''"> and qtpye = #{qtpye}</if>
            <if test="diff != null "> and diff = #{diff}</if>
            <if test="md5 != null  and md5 != ''"> and md5 = #{md5}</if>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="gradeId != null "> and gradeId = #{gradeId}</if>
            <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="paperTpye != null  and paperTpye != ''"> and paperTpye = #{paperTpye}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="fromSite != null  and fromSite != ''"> and fromSite = #{fromSite}</if>
            <if test="isSub != null "> and isSub = #{isSub}</if>
            <if test="isSubType != null "> and isSubType = #{isSubType}</if>
            <if test="isNormal != null "> and isNormal = #{isNormal}</if>
            <if test="isKonw != null "> and isKonw = #{isKonw}</if>
            <if test="tiid != null  and tiid != ''"> and tiid = #{tiid}</if>
            <if test="similarity != null "> and Similarity = #{similarity}</if>
            <if test="isunique != null "> and isunique = #{isunique}</if>
            <if test="md52 != null  and md52 != ''"> and md52 = #{md52}</if>
            <if test="subjectName != null  and subjectName != ''">
                and subjectId = (select subjectId from subject where subjectName=#{subjectName})
            </if>
        order by id desc
    </select>

    <!--解决分页慢 https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Changelog.md  更新时间2017-08-01 -->
    <select id="selectQuestionsList_COUNT" resultType="Long">
        select count(*)
        from questions q
        where 1=1 and q.id &gt;=1 and q.id &lt;=999999999
        <if test="id != null "> and id = #{id}</if>
        <if test="title != null  and title != ''"> and title like concat('%',#{title},'%')</if>
        <if test="optionA != null  and optionA != ''"> and option_a = #{optionA}</if>
        <if test="optionB != null  and optionB != ''"> and option_b = #{optionB}</if>
        <if test="optionC != null  and optionC != ''"> and option_c = #{optionC}</if>
        <if test="optionD != null  and optionD != ''"> and option_d = #{optionD}</if>
        <if test="optionE != null  and optionE != ''"> and option_e = #{optionE}</if>
        <if test="answer1 != null  and answer1 != ''"> and answer1 = #{answer1}</if>
        <if test="answer2 != null  and answer2 != ''"> and answer2 = #{answer2}</if>
        <if test="parse != null  and parse != ''"> and parse = #{parse}</if>
        <if test="qtpye != null  and qtpye != ''"> and qtpye = #{qtpye}</if>
        <if test="diff != null "> and diff = #{diff}</if>
        <if test="md5 != null  and md5 != ''"> and md5 = #{md5}</if>
        <if test="subjectId != null "> and subjectId = #{subjectId}</if>
        <if test="gradeId != null "> and gradeId = #{gradeId}</if>
        <if test="knowledges != null  and knowledges != ''"> and knowledges = #{knowledges}</if>
        <if test="area != null  and area != ''"> and area = #{area}</if>
        <if test="year != null "> and year = #{year}</if>
        <if test="paperTpye != null  and paperTpye != ''"> and paperTpye = #{paperTpye}</if>
        <if test="source != null  and source != ''"> and source = #{source}</if>
        <if test="fromSite != null  and fromSite != ''"> and fromSite = #{fromSite}</if>
        <if test="isSub != null "> and isSub = #{isSub}</if>
        <if test="isNormal != null "> and isNormal = #{isNormal}</if>
        <if test="isKonw != null "> and isKonw = #{isKonw}</if>
        <if test="tiid != null  and tiid != ''"> and tiid = #{tiid}</if>
        <if test="similarity != null "> and Similarity = #{similarity}</if>
        <if test="isunique != null "> and isunique = #{isunique}</if>
        <if test="md52 != null  and md52 != ''"> and md52 = #{md52}</if>
        <if test="subjectName != null  and subjectName != ''">
            and subjectId = (select subjectId from subject where subjectName=#{subjectName})
        </if>
    </select>

    <select id="selectQuestionsFilter" parameterType="Questions" resultMap="QuestionsResult">
        select id, title, title1, title0, answer0, parse0, options0,options, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2, parse, qtpye, diff, md5, subjectId, gradeId, knowledges, area, year, paperTpye, source, fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52, isSubType from questions
        where isUpdate=0
        <if test="id != null "> and id = #{id}</if>
        <if test="params.ids != null "> and id in
            <foreach item="id" collection="params.ids" open="(" separator="," close=")">
                #{id}
            </foreach></if>
        and
        ( title like concat('%', #{title} ,'%')
            or title1 like concat('%', #{title} ,'%')
            or parse like concat('%', #{title} ,'%')
            or options like concat('%', #{title} ,'%')
            or answer1 like concat('%', #{title} ,'%')
        )
    </select>


    <select id="selectQuestionsById" parameterType="Long" resultMap="QuestionsResult">
        <include refid="selectQuestionsVo"/>
        where id = #{id}
    </select>

    <select id="selectQuestionsByPids" parameterType="Integer" resultMap="QuestionsResult">
        select id, title, title0, title1, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2,
        parse, qtpye, diff, md5,
        gradeId, knowledges, area, year, paperTpye, source,
        fromSite, isSub, isNormal, isKonw, tiid, Similarity, isunique, md52, isSubType, score
        from questions q
        where isSubType in <foreach item="id" collection="array" open="(" separator="," close=")">#{id}</foreach>
        order by q.number * 1
    </select>

    <insert id="insertQuestions" parameterType="Questions" useGeneratedKeys="true" keyProperty="id">
        insert into questions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="title1 != null">title1,</if>
            <if test="optionA != null">option_a,</if>
            <if test="optionB != null">option_b,</if>
            <if test="optionC != null">option_c,</if>
            <if test="optionD != null">option_d,</if>
            <if test="optionE != null">option_e,</if>
            <if test="answer1 != null">answer1,</if>
            <if test="answer2 != null">answer2,</if>
            <if test="parse != null">parse,</if>
            <if test="qtpye != null">qtpye,</if>
            <if test="diff != null">diff,</if>
            <if test="md5 != null">md5,</if>
            <if test="subjectId != null">subjectId,</if>
            <if test="gradeId != null">gradeId,</if>
            <if test="knowledges != null">knowledges,</if>
            <if test="area != null">area,</if>
            <if test="year != null">year,</if>
            <if test="paperTpye != null">paperTpye,</if>
            <if test="source != null">source,</if>
            <if test="fromSite != null">fromSite,</if>
            <if test="isSub != null">isSub,</if>
            <if test="isNormal != null">isNormal,</if>
            <if test="isKonw != null">isKonw,</if>
            <if test="tiid != null">tiid,</if>
            <if test="similarity != null">Similarity,</if>
            <if test="isunique != null">isunique,</if>
            <if test="md52 != null">md52,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="title1 != null">#{title1},</if>
            <if test="optionA != null">#{optionA},</if>
            <if test="optionB != null">#{optionB},</if>
            <if test="optionC != null">#{optionC},</if>
            <if test="optionD != null">#{optionD},</if>
            <if test="optionE != null">#{optionE},</if>
            <if test="answer1 != null">#{answer1},</if>
            <if test="answer2 != null">#{answer2},</if>
            <if test="parse != null">#{parse},</if>
            <if test="qtpye != null">#{qtpye},</if>
            <if test="diff != null">#{diff},</if>
            <if test="md5 != null">#{md5},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="gradeId != null">#{gradeId},</if>
            <if test="knowledges != null">#{knowledges},</if>
            <if test="area != null">#{area},</if>
            <if test="year != null">#{year},</if>
            <if test="paperTpye != null">#{paperTpye},</if>
            <if test="source != null">#{source},</if>
            <if test="fromSite != null">#{fromSite},</if>
            <if test="isSub != null">#{isSub},</if>
            <if test="isNormal != null">#{isNormal},</if>
            <if test="isKonw != null">#{isKonw},</if>
            <if test="tiid != null">#{tiid},</if>
            <if test="similarity != null">#{similarity},</if>
            <if test="isunique != null">#{isunique},</if>
            <if test="md52 != null">#{md52},</if>
         </trim>
    </insert>

    <update id="updateQuestions" parameterType="Questions">
        update questions
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="title1 != null">title1 = #{title1},</if>
            <if test="options != null">options = #{options},</if>
            <if test="optionA != null">option_a = #{optionA},</if>
            <if test="optionB != null">option_b = #{optionB},</if>
            <if test="optionC != null">option_c = #{optionC},</if>
            <if test="optionD != null">option_d = #{optionD},</if>
            <if test="optionE != null">option_e = #{optionE},</if>
            <if test="optionF != null">option_f = #{optionF},</if>
            <if test="optionG != null">option_g = #{optionG},</if>
            <if test="answer1 != null">answer1 = #{answer1},</if>
            <if test="answer2 != null">answer2 = #{answer2},</if>
            <if test="parse != null">parse = #{parse},</if>
            <if test="qtpye != null">qtpye = #{qtpye},</if>
            <if test="diff != null">diff = #{diff},</if>
            <if test="md5 != null">md5 = #{md5},</if>
            <if test="subjectId != null">subjectId = #{subjectId},</if>
            <if test="gradeId != null">gradeId = #{gradeId},</if>
            <if test="knowledges != null">knowledges = #{knowledges},</if>
            <if test="area != null">area = #{area},</if>
            <if test="year != null">year = #{year},</if>
            <if test="paperTpye != null">paperTpye = #{paperTpye},</if>
            <if test="source != null">source = #{source},</if>
            <if test="fromSite != null">fromSite = #{fromSite},</if>
            <if test="isSub != null">isSub = #{isSub},</if>
            <if test="isNormal != null">isNormal = #{isNormal},</if>
            <if test="isKonw != null">isKonw = #{isKonw},</if>
            <if test="tiid != null">tiid = #{tiid},</if>
            <if test="similarity != null">Similarity = #{similarity},</if>
            <if test="isunique != null">isunique = #{isunique},</if>
            <if test="md52 != null">md52 = #{md52},</if>
            <if test="isUpdate != null">isUpdate = #{isUpdate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQuestionsById" parameterType="Long">
        delete from questions where id = #{id}
    </delete>

    <delete id="deleteQuestionsByIds" parameterType="String">
        delete from questions where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="getPaperQuestionList" parameterType="Questions" resultMap="QuestionsResult">
        SELECT
        a.id, a.title, a.title1, a.option_a,a.option_b,a.option_c,a.option_d,a.option_e,a.option_f,
        a.option_g, a.answer1,a.answer2, a.parse,a.qtpye,a.diff,
        a.md5,a.subjectId, a.gradeId,a.knowledges,a.area, a.year, a.paperTpye,a.source,
        a.fromSite,a.isSub,a.isNormal, a.isKonw, a.tiid,a.Similarity, a.isunique,a.md52, a.isSubType
        FROM
        questions a,
        question_knowledge_basic_id b,
        chapter c
        WHERE
        c.pharseId = 3 and
        a.id = b.question_id
        AND b.knowledge_basic_id=c.knowledgeId
        <if test="gradeId != null">
            AND c.gradeId = #{gradeId}
        </if>
        <if test="subjectId != null">
            AND c.subjectId = #{subjectId}
        </if>
        <if test="editionid != null">
            AND c.editionId = #{editionid}
        </if>
        <if test="chapterIds != null">
            AND c.id in
            <foreach collection="chapterIds" item="chapterId" open="(" separator="," close=")">
                #{chapterId}
            </foreach>
        </if>
        <if test="chapterOrder != null">
            AND c.chapterOrder = #{chapterOrder}
        </if>
        <if test="sectionOrder != null">
            AND c.sectionOrder = #{sectionOrder}
        </if>
        <if test="unitOrder != null">
            AND c.unitOrder = #{unitOrder}
        </if>
        <if test="qtpye != null and qtpye !=''">
            AND a.qtpye = #{qtpye}
        </if>
        <if test="diff != null and diff == 0">and a.diff in (1,2)</if>
        <if test="diff != null and diff == 1">and  a.diff = 3 </if>
        <if test="diff != null and diff == 2">and  a.diff in (4,5)</if>

        <if test="knowledgeBasicIds != null">
            and b.knowledge_basic_id in
	        <foreach item="id" collection="knowledgeBasicIds" open="(" separator="," close=")">
			            #{id}
			</foreach>
		</if>

        <if test="number != null ">
           limit ${number}
        </if>
    </select>

    <select id="getPaperQuestionListByPaperId" parameterType="Long" resultMap="QuestionsResult">
        SELECT
        a.id, a.title, a.title1, a.option_a,a.option_b,a.option_c,a.option_d,
        a.option_e,a.option_f,a.option_g, pq.answer1,pq.answer2, pq.parse,a.qtpye,a.diff,
        a.md5,a.knowledges,a.area, a.year, a.paperTpye,a.source,
        a.fromSite,a.isSub,a.isNormal, a.isKonw, a.tiid,a.Similarity, a.isunique,a.md52,pq.score,p.subjectId, p.grade_id gradeId,
        p.name paperTitle, a.isSubType
        FROM
        questions a
        left join mxjb_paper_question pq on pq.question_id=a.id
        left join mxjb_paper p on p.paper_id=pq.paper_id
        WHERE
        p.paper_id=#{paperId}
    </select>



    <select id="getPaperQuestionListByKnowledge" parameterType="Questions" resultMap="QuestionsResult">
        SELECT
        a.id, a.title, a.title1, a.option_a,a.option_b,a.option_c,a.option_d,
        a.option_e,a.option_f,a.option_g, a.answer1,a.answer2, a.parse,a.qtpye,a.diff,
        a.md5,a.subjectId, a.gradeId,a.knowledges,a.area, a.year, a.paperTpye,a.source,
        a.fromSite,a.isSub,a.isNormal, a.isKonw, a.tiid,a.Similarity, a.isunique,a.md52, a.isSubType
        FROM
        questions a
        left join question_knowledge_basic_id b on b.question_id=a.id
        left join knowledge_level c on c.knowledgeId=b.knowledge_basic_id
        WHERE
        a.gradeId = 500
        <if test="subjectId != null">
            AND c.subjectId = #{subjectId}
        </if>
        <if test="level != null and knowledgeId != null and level == 1">
            AND c.knowledge_level_1 = #{knowledgeId}
        </if>
        <if test="level != null and knowledgeId != null and level == 2">
            AND c.knowledge_level_2 = #{knowledgeId}
        </if>
        <if test="level != null and knowledgeId != null and level == 3">
            AND c.knowledge_level_3 = #{knowledgeId}
        </if>
        <if test="qtpye != null and qtpye !=''">
            AND a.qtpye = #{qtpye}
        </if>
        <if test="diff != null and diff == 0">and a.diff in (1,2)</if>
        <if test="diff != null and diff == 1">and  a.diff = 3 </if>
        <if test="diff != null and diff == 2">and  a.diff in (4,5)</if>
        <if test="number != null ">
            limit ${number}
        </if>
    </select>

    <select id="getQuestionListByIds"  parameterType="String"  resultMap="QuestionsResult">
        <include refid="selectQuestionsVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectIdsByIdGreaterThan2"  parameterType="Long"  resultMap="QuestionsResult">
        SELECT t1.id from questions t1 where 1=1
          and t1.id not in (SELECT question_id from test_question_pic)
            order by t1.id desc
            limit 200
    </select>
    <select id="selectIdsByIdGreaterThan" parameterType="Long"  resultMap="QuestionsResult">
		select id from questions
        where id > #{id}
        order by id asc limit 1000
    </select>

	<select id="selectByChapterTreeOldids"  parameterType="Questions" resultMap="QuestionsResult">
    	SELECT q.* from questions q
    	   left join question_knowledge_basic_id qkbi on q.id = qkbi.question_id
    	   left join knowledge_basic kb on kb.id = qkbi.knowledge_basic_id
    	   left join chapter_tree ct on  ct.oldid = kb.md5
			where 1=1
        <if test="parse != null">and q.parse IS NOT NULL</if>
			<if test="chapterIds != null">
				and ct.id in
		        <foreach item="id" collection="chapterIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
			</if>
			<if test="knowledgeBasicIds != null">
				and kb.id in
		        <foreach item="id" collection="knowledgeBasicIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
			</if>
			<if test="subjectId != null">
	            AND q.subjectId = #{subjectId}
	        </if>
        <if test="qtpye != null and qtpye !=''">
	            AND q.qtpye = #{qtpye}
	        </if>
	        <if test="title != null  and title != ''"> and q.title like concat('%', #{title} ,'%') </if>
    </select>

    <select id="selectByChapterTreeChineseAndEnglish"  parameterType="Questions" resultMap="QuestionsResult">
    	SELECT q.* from questions q
    	   left join chapter_question_id cqi on cqi.question_id = q.id
    	   left join chapter_tree ct on  ct.id = cqi.chapter_id
			where 1=1
            <if test="subjectId != null">and q.subjectId = #{subjectId}</if>
            <if test="parse != null">and q.parse IS NOT NULL</if>
            <if test="chapterIds != null">
				and ct.id in
		        <foreach item="id" collection="chapterIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
			</if>
			<if test="chapterid != null">
				and ct.id = #{chapterid}
			</if>
			<if test="qtpye != null and qtpye !=''">
	            AND q.qtpye = #{qtpye}
	        </if>
	        <if test="diff != null and diff == 0">and q.diff in (1,2)</if>
	        <if test="diff != null and diff == 1">and  q.diff = 3 </if>
	        <if test="diff != null and diff == 2">and  q.diff in (4,5)</if>

	        <if test="title != null  and title != ''"> and q.title like concat('%', #{title} ,'%') </if>
    </select>

    <select id="selectByKnowledgeTreeOldids"  parameterType="Questions" resultMap="QuestionsResult">
    	SELECT q.* from questions q
    		left join question_knowledge_basic_id qkbi on q.id = qkbi.question_id
    		left join knowledge_basic kb on qkbi.knowledge_basic_id = kb.id
    		left join knowledge_tree kt on kt.oldId = kb.md5
			where 1=1
		<if test="knowledgeIds != null">
			and kt.id in
	        <foreach item="id" collection="knowledgeIds" open="(" separator="," close=")">
	            #{id}
	        </foreach>
        </if>
        <if test="qtpye != null and qtpye !=''">
            AND q.qtpye = #{qtpye}
        </if>

        <if test="title != null  and title != ''"> and q.title like concat('%', #{title} ,'%') </if>

    </select>

    <select id="getPaperQuestionListBySubject" parameterType="Questions" resultMap="QuestionsResult">
        SELECT
        a.id, a.title, a.title1, a.option_a,a.option_b,a.option_c,a.option_d,
        a.option_e, a.answer1,a.answer2, a.parse,a.qtpye,a.diff,
        a.md5,a.subjectId, a.gradeId,a.knowledges,a.area, a.year, a.paperTpye,a.source,
        a.fromSite,a.isSub,a.isNormal, a.isKonw, a.tiid,a.Similarity, a.isunique,a.md52, a.isSubType
        FROM
        questions a
        WHERE
        1=1
        <if test="subjectId != null">
            AND a.subjectId = #{subjectId}
        </if>
        <if test="qtpye != null and qtpye !=''">
            AND a.qtpye = #{qtpye}
        </if>
        <if test="diff != null and diff == 0">and a.diff in (1,2)</if>
        <if test="diff != null and diff == 1">and  a.diff = 3 </if>
        <if test="diff != null and diff == 2">and  a.diff in (4,5)</if>
        <if test="number != null ">
            limit ${number}
        </if>
    </select>

    <select id="getQuestionsByChapterForParactice"  parameterType="Questions" resultMap="QuestionsResult">
    	SELECT q.* from questions q
    	   left join question_knowledge_basic_id qkbi on q.id = qkbi.question_id
    	   left join knowledge_basic kb on kb.id = qkbi.knowledge_basic_id
    	   left join chapter_tree ct on  ct.oldid = kb.md5
			where 1=1

			<if test="chapterIds != null">
				and ct.id in
		        <foreach item="id" collection="chapterIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
	        </if>
			and q.qtpye in ('单选题' ,'单项选择题' ,'选择搭配题' ,'选择题')
			and q.option_a !='' and length(q.answer1)=1
			order by (q.parse IS NULL OR q.parse = ''), rand()
    </select>

    <select id="getQuestionsByChapterForParacticeChineseAndEnglish"  parameterType="Questions" resultMap="QuestionsResult">
    	select q.*
		from chapter_question_id cqi
		left join chapter_tree ct on cqi.chapter_id = ct.id
		left join questions q on cqi.question_id = q.id
			where 1=1

			<if test="chapterIds != null">
				and ct.id in
		        <foreach item="id" collection="chapterIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
	        </if>
			and q.qtpye in ('单选题' ,'单项选择题' ,'选择搭配题' ,'选择题')
			and q.option_a !='' and length(q.answer1)=1
			order by (q.parse IS NULL OR q.parse = ''), rand()
    </select>

    <select id="getQuestionsByKnowledgeForParactice"  parameterType="Questions" resultMap="QuestionsResult">
    	SELECT q.* from questions q
    		left join question_knowledge_basic_id qkbi on q.id = qkbi.question_id
    		left join knowledge_basic kb on qkbi.knowledge_basic_id = kb.id
    		left join knowledge_tree kt on kt.oldId = kb.md5
			where 1=1
			<if test="knowledgeIds != null">
				and kt.id in
		        <foreach item="id" collection="knowledgeIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
	        </if>
			<if test="knowledgeId != null">
				and kt.id = #{knowledgeId}
			</if>
			and q.qtpye in ('单选题' ,'单项选择题' ,'选择搭配题' ,'选择题')
			and q.option_a !='' and length(q.answer1)=1
			order by (q.parse IS NULL OR q.parse = ''), rand()
    </select>

    <select id="selectCollectedList"  parameterType="Questions" resultMap="QuestionsResult">
    	select q.* from mxjb_question_collection c
		left join questions q on c.question_id = q.id
		left join subject s on q.subjectId = s.subjectId
		where c.user_id = #{userId}
		and q.subjectId = #{subjectId}
    </select>

    <select id="selectWrongQuestions"  parameterType="Questions" resultMap="QuestionsResult">
    	select q.* from mxjb_wrong_book b
       left join questions q on q.id = b.question_id
       where b.customer_code = #{customerCode}
		and q.subjectId = #{subjectId}
		and q.qtpye = #{qtpye}
    </select>

    <select id="selectCollectInfo"  parameterType="java.util.HashMap" resultMap="QuestionsResult">
    	SELECT q.id from mxjb_question_collection c
		left join questions q on c.question_id = q.id
		where user_id = #{userId}
		and c.question_id in
		<foreach item="id" collection="ids" open="(" separator="," close=")">
		            #{id}
		</foreach>
    </select>

    <select id="selectByIds"  parameterType="java.util.HashMap" resultMap="QuestionsResult">
    	SELECT * from questions
		where id in
		<foreach item="id" collection="ids" open="(" separator="," close=")">
		            #{id}
		</foreach>
    </select>

    <select id="selectByKnowledgeIds"  parameterType="Questions" resultMap="QuestionsResult">
    	select * from questions
    	where id in (
    		select question_id
    		from question_knowledge_basic_id
    		where knowledge_basic_id in
    			<foreach item="id" collection="knowledgeIds" open="(" separator="," close=")">
		            #{id}
		        </foreach>
    		)

    	<if test="qtpye != null and qtpye !=''">
            AND qtpye = #{qtpye}
        </if>
    	<if test="number != null ">
           limit ${number}
        </if>
    </select>


    <select id="selectTotalBySubjectId" parameterType="integer" resultType="long">
        SELECT COUNT(*)
        FROM `questions` q
        WHERE q.`subjectId` = #{subjectId} AND q.`qtpye` IN ('单选题', '判断题', '单项选择题','选择题')
    </select>

    <select id="selectTotalOfCompleted" resultType="long">
       SELECT count(distinct q2.id)
         FROM `questions` q2
         JOIN `mxjb_paper_answers` a2 ON a2.`question_id` = q2.`id`
         JOIN `mxjb_paper_examinee` e2 ON e2.`customer_code` = #{userId} AND a2.`examinee_id` = e2.`examinee_id`
         JOIN `mxjb_paper_test` p2 ON e2.`paper_id` = p2.`paper_id` AND p2.`name` = '必刷题'
        WHERE q2.`subjectId` = #{subjectId} AND q2.`qtpye` IN ('单选题', '判断题', '单项选择题','选择题')

    </select>

    <select id="selectTotalOfLatestRight" resultType="long">
        SELECT COUNT(*)
         FROM `questions` q2
         JOIN `mxjb_paper_answers` a2 ON a2.`question_id` = q2.`id` AND a2.`state` = 1
         JOIN `mxjb_paper_examinee` e2 ON e2.`customer_code` = #{userId} AND a2.`examinee_id` = e2.`examinee_id`
         JOIN `mxjb_paper_test` p2 ON e2.`paper_id` = p2.`paper_id` AND p2.`name` = '必刷题'
         JOIN (SELECT q.`id`, MAX(e.`begin_time`) begin_time
               FROM `questions` q
                        JOIN `mxjb_paper_answers` a ON a.`question_id` = q.`id`
                        JOIN `mxjb_paper_examinee` e ON e.`customer_code` = #{userId} AND a.`examinee_id` = e.`examinee_id`
                        JOIN `mxjb_paper_test` p ON e.`paper_id` = p.`paper_id` AND p.`name` = '必刷题'
               WHERE q.`subjectId` = #{subjectId} AND q.`qtpye` IN ('单选题', '判断题', '单项选择题','选择题')
               GROUP BY q.`id`
        ) T ON q2.id = T.id AND T.begin_time = e2.begin_time
        WHERE q2.`subjectId` = #{subjectId} AND q2.`qtpye` IN ('单选题', '判断题', '单项选择题','选择题')

    </select>

    <select id="selectLatestRates" resultMap="QuestionsResult">
        SELECT e.`paper_id` id, e.`begin_time` create_time, round(e.`score_rate` * 100) score
          FROM `mxjb_paper_test` p
          JOIN `mxjb_paper_examinee` e ON e.`paper_id` = p.`paper_id` AND e.`customer_code` = #{userId} AND e.`state` = 6
         WHERE p.name = '必刷题' AND p.subjectId = #{subjectId}
        ORDER BY e.`begin_time` DESC LIMIT 10
    </select>

</mapper>
