<?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.learn.mapper.LearnQuestionsMapper">
    
    <resultMap type="LearnQuestions" id="LearnQuestionsResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <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="answer1"    column="answer1"    />
        <result property="answer2"    column="answer2"    />
        <result property="qtpye"    column="qtpye"    />
        <result property="subjectId"    column="subjectId"    />
        <result property="paperId"    column="paperId"    />
        <result property="knowledgeId"    column="knowledgeId"    />
        <result property="diff"    column="diff"    />
        <result property="similarity"    column="similarity"    />
        <result property="parse"    column="parse"    />
        <result property="knowId"    column="knowId"    />
        <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="isNormal"    column="isNormal"    />
        <result property="isKonw"    column="isKonw"    />
        <result property="tiid"    column="tiid"    />
        <result property="md5"    column="md5"    />
        <result property="isunique"    column="isunique"    />
        <result property="md52"    column="md52"    />
        <result property="createTime"    column="create_time"    />
        <result property="score"    column="score"    />
        <result property="options"    column="options"    />
        <result property="number"    column="number"    />
        <result property="paperTypeTitle"    column="paperTypeTitle"    />
        <result property="options0"    column="options0"    />
        <result property="title0"    column="title0"    />
        <result property="title1"    column="title1"    />
        <result property="parse0"    column="parse0"    />
        <result property="answer0"    column="answer0"    />
        <result property="isUpdate"    column="isUpdate"    />
        <result property="isSubType"    column="isSubType"    />
    </resultMap>

    <sql id="selectLearnQuestionsVo">
        select id, title, option_a, option_b, option_c, option_d, option_e, option_f, option_g, answer1, answer2, qtpye, subjectId, paperId, knowledgeId, diff, similarity, parse, knowId, gradeId, knowledges, area, year, paperTpye, source, fromSite, isSub, isNormal, isKonw, tiid, md5, isunique, md52, create_time, score, options, number, paperTypeTitle, options0, title0, title1, parse0, answer0, isUpdate, isSubType from learn_questions
    </sql>

    <select id="selectLearnQuestionsList" parameterType="LearnQuestions" resultMap="LearnQuestionsResult">
        <include refid="selectLearnQuestionsVo"/>
        <where>  
            <if test="title != null  and title != ''"> and title = #{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="optionF != null  and optionF != ''"> and option_f = #{optionF}</if>
            <if test="optionG != null  and optionG != ''"> and option_g = #{optionG}</if>
            <if test="answer1 != null  and answer1 != ''"> and answer1 = #{answer1}</if>
            <if test="answer2 != null  and answer2 != ''"> and answer2 = #{answer2}</if>
            <if test="qtpye != null  and qtpye != ''"> and qtpye = #{qtpye}</if>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="paperId != null "> and paperId = #{paperId}</if>
            <if test="knowledgeId != null "> and knowledgeId = #{knowledgeId}</if>
            <if test="diff != null "> and diff = #{diff}</if>
            <if test="similarity != null "> and similarity = #{similarity}</if>
            <if test="parse != null  and parse != ''"> and parse = #{parse}</if>
            <if test="knowId != null "> and knowId = #{knowId}</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="md5 != null  and md5 != ''"> and md5 = #{md5}</if>
            <if test="isunique != null "> and isunique = #{isunique}</if>
            <if test="md52 != null  and md52 != ''"> and md52 = #{md52}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="options != null  and options != ''"> and options = #{options}</if>
            <if test="number != null "> and number = #{number}</if>
            <if test="paperTypeTitle != null  and paperTypeTitle != ''"> and paperTypeTitle = #{paperTypeTitle}</if>
            <if test="options0 != null  and options0 != ''"> and options0 = #{options0}</if>
            <if test="title0 != null  and title0 != ''"> and title0 = #{title0}</if>
            <if test="title1 != null  and title1 != ''"> and title1 = #{title1}</if>
            <if test="parse0 != null  and parse0 != ''"> and parse0 = #{parse0}</if>
            <if test="answer0 != null  and answer0 != ''"> and answer0 = #{answer0}</if>
            <if test="isUpdate != null "> and isUpdate = #{isUpdate}</if>
            <if test="isSubType != null  and isSubType != ''"> and isSubType = #{isSubType}</if>
        </where>
    </select>
    
    <select id="selectLearnQuestionsById" parameterType="Long" resultMap="LearnQuestionsResult">
        <include refid="selectLearnQuestionsVo"/>
        where id = #{id}
    </select>

    <insert id="insertLearnQuestions" parameterType="LearnQuestions" useGeneratedKeys="true" keyProperty="id">
        insert into learn_questions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</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="optionF != null">option_f,</if>
            <if test="optionG != null">option_g,</if>
            <if test="answer1 != null">answer1,</if>
            <if test="answer2 != null">answer2,</if>
            <if test="qtpye != null">qtpye,</if>
            <if test="subjectId != null">subjectId,</if>
            <if test="paperId != null">paperId,</if>
            <if test="knowledgeId != null">knowledgeId,</if>
            <if test="diff != null">diff,</if>
            <if test="similarity != null">similarity,</if>
            <if test="parse != null">parse,</if>
            <if test="knowId != null">knowId,</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="md5 != null">md5,</if>
            <if test="isunique != null">isunique,</if>
            <if test="md52 != null">md52,</if>
            <if test="createTime != null">create_time,</if>
            <if test="score != null">score,</if>
            <if test="options != null">options,</if>
            <if test="number != null">number,</if>
            <if test="paperTypeTitle != null">paperTypeTitle,</if>
            <if test="options0 != null">options0,</if>
            <if test="title0 != null">title0,</if>
            <if test="title1 != null">title1,</if>
            <if test="parse0 != null">parse0,</if>
            <if test="answer0 != null">answer0,</if>
            <if test="isUpdate != null">isUpdate,</if>
            <if test="isSubType != null">isSubType,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</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="optionF != null">#{optionF},</if>
            <if test="optionG != null">#{optionG},</if>
            <if test="answer1 != null">#{answer1},</if>
            <if test="answer2 != null">#{answer2},</if>
            <if test="qtpye != null">#{qtpye},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="knowledgeId != null">#{knowledgeId},</if>
            <if test="diff != null">#{diff},</if>
            <if test="similarity != null">#{similarity},</if>
            <if test="parse != null">#{parse},</if>
            <if test="knowId != null">#{knowId},</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="md5 != null">#{md5},</if>
            <if test="isunique != null">#{isunique},</if>
            <if test="md52 != null">#{md52},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="score != null">#{score},</if>
            <if test="options != null">#{options},</if>
            <if test="number != null">#{number},</if>
            <if test="paperTypeTitle != null">#{paperTypeTitle},</if>
            <if test="options0 != null">#{options0},</if>
            <if test="title0 != null">#{title0},</if>
            <if test="title1 != null">#{title1},</if>
            <if test="parse0 != null">#{parse0},</if>
            <if test="answer0 != null">#{answer0},</if>
            <if test="isUpdate != null">#{isUpdate},</if>
            <if test="isSubType != null">#{isSubType},</if>
         </trim>
    </insert>

    <update id="updateLearnQuestions" parameterType="LearnQuestions">
        update learn_questions
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</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="qtpye != null">qtpye = #{qtpye},</if>
            <if test="subjectId != null">subjectId = #{subjectId},</if>
            <if test="paperId != null">paperId = #{paperId},</if>
            <if test="knowledgeId != null">knowledgeId = #{knowledgeId},</if>
            <if test="diff != null">diff = #{diff},</if>
            <if test="similarity != null">similarity = #{similarity},</if>
            <if test="parse != null">parse = #{parse},</if>
            <if test="knowId != null">knowId = #{knowId},</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="md5 != null">md5 = #{md5},</if>
            <if test="isunique != null">isunique = #{isunique},</if>
            <if test="md52 != null">md52 = #{md52},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="score != null">score = #{score},</if>
            <if test="options != null">options = #{options},</if>
            <if test="number != null">number = #{number},</if>
            <if test="paperTypeTitle != null">paperTypeTitle = #{paperTypeTitle},</if>
            <if test="options0 != null">options0 = #{options0},</if>
            <if test="title0 != null">title0 = #{title0},</if>
            <if test="title1 != null">title1 = #{title1},</if>
            <if test="parse0 != null">parse0 = #{parse0},</if>
            <if test="answer0 != null">answer0 = #{answer0},</if>
            <if test="isUpdate != null">isUpdate = #{isUpdate},</if>
            <if test="isSubType != null">isSubType = #{isSubType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLearnQuestionsById" parameterType="Long">
        delete from learn_questions where id = #{id}
    </delete>

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

    <select id="selectQuestionByPaperId" parameterType="Long" resultMap="LearnQuestionsResult">
        select q.`id`, pq.`seq`, pq.`score`, title, option_a, option_b, option_c, option_d, option_e, option_f, option_g, qtpye, diff, similarity, isSub, paperTypeTitle, title0
          from learn_paper_question pq join `learn_questions` q on q.`id` = pq.`question_id`
          where pq.`paper_id` = #{paperId}
          order by pq.`seq`, q.`id`
    </select>

    <select id="statByKnowledgeType" parameterType="map" resultMap="LearnQuestionsResult">
       SELECT q.`knowledgeId`, q.`qtpye`, COUNT(*) number
         FROM `learn_knowledge_question` kq
         <if test="studentId != null">
             LEFT JOIN `learn_answer` a ON kq.`question_id` = a.`question_id` AND a.`student_id` = #{studentId}
         </if>
         JOIN `learn_questions` q ON q.`id` = kq.`question_id`
        <where>
            <if test="knowIds != null"> and kq.`knowledge_id` in <foreach item="id" collection="knowIds" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="types != null"> and q.`qtpye` in <foreach item="id" collection="types" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="studentId != null"> AND a.`student_id` IS NULL</if>
        </where>
        GROUP BY q.`knowledgeId`, q.`qtpye`
    </select>

    <select id="selectQuestionsForPaper" parameterType="LearnQuestions" resultMap="LearnQuestionsResult">
        SELECT q.`id`, q.`knowledgeId`
        FROM `learn_knowledge_question` kq
        <if test="id != null">
            LEFT JOIN `learn_answer` a ON kq.`question_id` = a.`question_id` AND a.`student_id` = #{id}
        </if>
        JOIN `learn_questions` q ON q.`id` = kq.`question_id`
        <where>
            <if test="qtpye != null  and qtpye != ''"> and qtpye = #{qtpye}</if>
            <if test="id != null ">  AND a.`student_id` IS NULL</if>
            <if test="knowledgeId != null "> and knowledgeId = #{knowledgeId}</if>
        </where>
        limit #{number}, 500
    </select>

</mapper>