<?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.project.training.spec.mapper.TStQuestionAnswerMapper">
    
    <resultMap type="TStQuestionAnswer" id="TStQuestionAnswerResult">
        <result property="id"    column="id"    />
        <result property="planId"    column="plan_id"    />
        <result property="feedbackId"    column="feedback_id"    />
        <result property="questionId"    column="question_id"    />
        <result property="question"    column="question"    />
        <result property="answer"    column="answer"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTStQuestionAnswerVo">
        select d.id, d.plan_id, d.FEEDBACK_ID, d.question_id, d.answer, b.QUESTION
        from t_st_question_answer d
        inner join T_ST_QUESTION_BANK b on d.QUESTION_ID=b.ID
    </sql>

    <select id="selectTStQuestionAnswerList" parameterType="TStQuestionAnswer" resultMap="TStQuestionAnswerResult">
        <include refid="selectTStQuestionAnswerVo"/>
        <where>  
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="feedbackId != null "> and feedback_id = #{feedbackId}</if>
            <if test="questionId != null "> and question_id = #{questionId}</if>
            <if test="answer != null  and answer != ''"> and answer = #{answer}</if>
        </where>
    </select>

    <select id="selectTStQuestionAnswerByPlanIdAndQuestionId" parameterType="TStQuestionAnswer" resultMap="TStQuestionAnswerResult">
        <include refid="selectTStQuestionAnswerVo"/>
        where plan_id = #{planId} and question_id = #{questionId}
    </select>

    <select id="selectTStQuestionAnswer" parameterType="TStQuestionAnswer" resultMap="TStQuestionAnswerResult">
        <include refid="selectTStQuestionAnswerVo"/>
        <where>
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="feedbackId != null "> and feedback_id = #{feedbackId}</if>
            <if test="questionId != null "> and question_id = #{questionId}</if>
            <if test="answer != null  and answer != ''"> and answer = #{answer}</if>
            <if test="feedbackId != null  and feedbackId != ''"> and feedback_id = #{feedbackId}</if>
        </where>
    </select>

    <select id="selectTStQuestionAnswerById" parameterType="Long" resultMap="TStQuestionAnswerResult">
        <include refid="selectTStQuestionAnswerVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTStQuestionAnswer" parameterType="TStQuestionAnswer">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_st_question_answer.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_st_question_answer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="planId != null">plan_id,</if>
            <if test="feedbackId != null">feedback_id,</if>
            <if test="questionId != null">question_id,</if>
            <if test="answer != null">answer,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="planId != null">#{planId},</if>
            <if test="feedbackId != null">#{feedbackId},</if>
            <if test="questionId != null">#{questionId},</if>
            <if test="answer != null">#{answer},</if>
         </trim>
    </insert>

    <update id="updateTStQuestionAnswer" parameterType="TStQuestionAnswer">
        update t_st_question_answer
        <trim prefix="SET" suffixOverrides=",">
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="questionId != null">question_id = #{questionId},</if>
            <if test="answer != null">answer = #{answer},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTStQuestionAnswerById" parameterType="Long">
        update t_st_question_answer set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTStQuestionAnswerByIds" parameterType="String">
        update t_st_question_answer set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
</mapper>