<?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="cn.iocoder.yudao.module.knowledge.mapper.TQuestionStemMapper">

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TQuestionStem" id="tQuestionStemMaps">
        <result property="id" column="id"/>
        <result property="questionBankId" column="question_bank_id"/>
        <result property="questionBankName" column="question_bank_name"/>
        <result property="tqsConntent" column="content"/>
        <result property="type" column="type"/>
        <result property="level" column="level"/>
        <result property="tqsFileUrl" column="file_url"/>
        <result property="analysisFileUrl" column="analysis_file_url"/>
        <result property="analysis" column="analysis"/>
        <result property="rightAnswers" column="right_answers"/>
    </resultMap>

    <resultMap id="tQuestionStemMap"
               type="cn.iocoder.yudao.module.knowledge.controller.responsevo.TQuestionStemvo">
        <!--column通过SQL语句查询出来的字段,property对应的实体属性-->
        <id column="id" property="id"/>
        <result property="questionBankId" column="question_bank_id"/>
        <result property="questionBankName" column="question_bank_name"/>
        <result property="content" column="tqsConntent"/>
        <result property="type" column="type"/>
        <result property="keyContent" column="keyContent"/>
        <result property="level" column="level"/>
        <result property="fileUrl" column="tqsFileUrl"/>
        <result property="analysisFileUrl" column="analysisFileUrl"/>
        <result property="analysis" column="analysis"/>
        <result property="rightAnswers" column="right_answers"/>
    </resultMap>
    <update id="deleteBatchBy">
        update t_question_stem set deleted = 0 where question_bank_id in
        <foreach collection="questionBankIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="findAll"
            parameterType="cn.iocoder.yudao.module.knowledge.controller.requestdto.TQuestionStemRequest"
            resultMap="tQuestionStemMap">
        SELECT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        <where>
            tqs.deleted = 0
            <if test="param.content != null  and param.content != ''">and tqs.content like
                concat('%', #{param.content}, '%')
            </if>
            <if test="param.questionBankName != null  and param.questionBankName != ''">and tqs.question_bank_name like
                concat('%', #{param.questionBankName}, '%')
            </if>
            <if test="param.questionBankId != null ">and tqs.question_bank_id = #{param.questionBankId}</if>
            <if test="param.type != null">and tqs.type = #{param.type}</if>
            <if test="param.level != null">and tqs.level = #{param.level}</if>
        </where>
        order by tqs.create_time desc
    </select>


    <select id="getStemRandom"
            resultMap="tQuestionStemMap">
        SELECT DISTINCT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        inner join t_question_bank tqb on tqs.question_bank_id = tqb.id and tqb.deleted = 0
        <where>
            tqs.deleted = 0
            <if test="code != null ">and tqb.id_chain like concat(#{code},'%') </if>
            <if test="type != null and type != 5">and tqs.type = #{type}</if>
        </where>
        ORDER BY RAND()
        LIMIT #{questionSum}
    </select>

    <select id="getAppStemRandom"
            resultMap="tQuestionStemMap">
        SELECT DISTINCT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        inner join t_question_bank tqb on tqs.question_bank_id = tqb.id and tqb.deleted = 0
        <where>
            tqs.deleted = 0
            <if test="code != null ">and tqb.id_chain like concat(#{code},'%') </if>
            <if test="content != null ">and tqs.content like concat(#{content},'%') </if>
            <if test="type != null ">and tqs.type = #{type}</if>
        </where>
        ORDER BY RAND()
    </select>

    <select id="getOneById"
            resultMap="tQuestionStemMap">
        SELECT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        <where>
            tqs.deleted = 0
            and id = #{id}
        </where>
    </select>


    <select id="findOne"
            parameterType="cn.iocoder.yudao.module.knowledge.controller.requestdto.TQuestionStemRequest"
            resultMap="tQuestionStemMap">
        SELECT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.key_content keyContent,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        <where>
            tqs.deleted = 0
            <if test="id != null ">and tqs.id = #{id}</if>
        </where>
    </select>

    <select id="getByQuestionBankId"
            parameterType="cn.iocoder.yudao.module.knowledge.controller.requestdto.TQuestionStemRequest"
            resultMap="tQuestionStemMap">
        SELECT
        tqs.id,
        tqs.question_bank_id,
        tqs.question_bank_name,
        tqs.content tqsConntent,
        tqs.type,
        tqs.level,
        tqs.file_url tqsFileUrl,
        tqs.analysis_file_url analysisFileUrl,
        tqs.analysis,
        tqs.right_answers,
        tqs.code
        FROM
        t_question_stem tqs
        <where>
            <if test="id != null ">and tqs.question_bank_id = #{id}</if>
        </where>
    </select>


    <select id="selectQuestionStemList"
            parameterType="Long"
            resultType="cn.iocoder.yudao.module.knowledge.entity.TQuestionStem">
        SELECT
        *
        FROM
        t_question_stem tqs
        <where>
            <if test="id != null ">and question_bank_id = #{id}</if>
        </where>
    </select>
    <select id="queryPage" resultType="cn.iocoder.yudao.module.knowledge.entity.TQuestionStem">
        select * from t_question_stem
        <where>
            deleted = 0
            <if test="params.bankIdList != null and params.bankIdList.size > 0">
                and question_bank_id in
                <foreach collection="params.bankIdList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="params.content != null and params.content != ''">
                and content like concat ('%',#{params.content}, '%' )
            </if>
            <if test="params.type != null and params.type != '' and params.type != 5">
                and type = #{params.type}
            </if>
            <if test="params.level != null and params.level != '' and params.level != 5">
                and level = #{params.level}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="queryList" resultType="cn.iocoder.yudao.module.knowledge.entity.TQuestionStem">
        select * from t_question_stem
        <where>
            deleted = 0
            <if test="params.bankIdList != null and params.bankIdList.size > 0">
                and question_bank_id in
                <foreach collection="params.bankIdList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="params.content != null and params.content != ''">
                and content like concat ('%',#{params.content}, '%' )
            </if>
            <if test="params.type != null and params.type != '' and params.type != 5">
                and type = #{params.type}
            </if>
            <if test="params.level != null and params.level != '' and params.level != 5">
                and level = #{params.level}
            </if>
        </where>
        order by create_time desc
    </select>

    <update id="deleteByTquestionbankId" parameterType="Long">
        update t_question_stem
        set deleted = 1
        where question_bank_id = #{childrenId}
    </update>
    <update id="updateBankNameByBankId">
        update t_question_stem
        set question_bank_name = #{bankName}
        where question_bank_id = #{bankId}
          and deleted = 0
    </update>

</mapper>
