<?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.TTestPaperQuestionStemMapper">

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TTestPaperQuestionStem"
               id="tTestPaperQuestionStemMap">
        <result property="id" column="t_question_stem_id"/>
        <result property="testPaperInfoId" column="test_paper_info_id"/>
        <result property="fileUrl" column="file_url"/>
        <result property="content" column="content"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="type" column="type"/>
        <result property="level" column="level"/>
        <result property="analysis" column="analysis"/>
        <result property="rightAnswers" column="right_answers"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="code" column="code"/>
        <result property="score" column="score"/>
        <result property="orders" column="orders"/>
        <result property="usercode" column="usercode"/>
        <result property="tQuestionStemId" column="t_question_stem_id"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="columns">
        id
        ,test_paper_id,file_url,content,type,level,analysis,right_answers,code,score,orders,usercode
    </sql>
    <update id="deleteByPaperInfoIds">
        <foreach collection="paperInfoIds" item="item" separator=";">
            update t_test_paper_question_stem
            set deleted = 1
            where test_paper_info_id = #{item} and deleted = 0
        </foreach>
    </update>

    <select id="findList" resultMap="tTestPaperQuestionStemMap">
        select
        <include refid="columns"/>
        from t_test_paper_question_stem
        <where>
            <if test="id != null">
                id = #{id}
            </if>
            <if test="testPaperId != null">
                test_paper_id = #{testPaperId}
            </if>
            <if test="fileUrl != null and fileUrl != ''">
                file_url = #{fileUrl}
            </if>
            <if test="content != null and content != ''">
                content = #{content}
            </if>
            <if test="type != null and type != ''">
                type = #{type}
            </if>
            <if test="level != null">
                level = #{level}
            </if>
            <if test="analysis != null and analysis != ''">
                analysis = #{analysis}
            </if>
            <if test="rightAnswers != null and rightAnswers != ''">
                right_answers = #{rightAnswers}
            </if>
            <if test="code != null and code != ''">
                code = #{code}
            </if>
            <if test="order != null and order != ''">
                order = #{order}
            </if>
            <if test="usercode != null and usercode != ''">
                usercode = #{usercode}
            </if>
        </where>
    </select>

    <select id="selectByPaperId"
            resultType="cn.iocoder.yudao.module.knowledge.controller.responsevo.TQuestionAnswervo">
        select right_answers, type, orders
        from t_test_paper_question_stem
        where test_paper_id = #{testPaperId}
    </select>

    <select id="queryPaper" resultMap="tTestPaperQuestionStemMap">
        select id,t_question_stem_id,test_paper_info_id ,content, score,type, level, orders from t_test_paper_question_stem
        <where>
            deleted = 0
            <if test="testPaperInfoId != null">
                and test_paper_info_id = #{testPaperInfoId}
            </if>
            <if test="type != null and type == '1'.toString()">
                and user_id = #{userId}
            </if>
        </where>
        order by orders asc
    </select>

    <select id="queryAnswer" resultMap="tTestPaperQuestionStemMap">
        select id,type, right_answers,score,t_question_stem_id from t_test_paper_question_stem
        <where>
            deleted = 0
            <if test="testPaperInfoId != null">
                and test_paper_info_id = #{testPaperInfoId}
            </if>
            <if test="type != null and type == '1'.toString()">
                and usercode = #{userId}
            </if>
        </where>
    </select>

    <select id="getOneByTrainId"
            resultMap="tTestPaperQuestionStemMap">
        select *
        from t_test_paper_question_stem
        where test_paper_info_id = #{id}
        and deleted = 0
    </select>

    <update id="deleteByTrainId"
            parameterType="java.lang.Long">
        update t_test_paper_question_stem
        set deleted = 1
        where train_id = #{id}
    </update>

    <update id="deleteByPaperInfId"
            parameterType="java.lang.Long">
        update t_test_paper_question_stem
        set deleted = 1
        where test_paper_info_id = #{id}
    </update>

</mapper>
