<?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.rc.mapper.TQuestionnaireTemplateMapper">
    
    <resultMap type="TQuestionnaireTemplate" id="TQuestionnaireTemplateResult">
        <result property="id"    column="id"    />
        <result property="chapterId"    column="chapter_id"    />
        <result property="type"    column="type"    />
        <result property="directory"    column="directory"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="chapterName"    column="chapter_name"    />
    </resultMap>

    <sql id="selectTQuestionnaireTemplateVo">
        select qt.id, qt.chapter_id, qt.type, qt.directory, qt.code, qt.name, qt.dept_id,
        SUBSTR(qt.directory, 1, INSTR(qt.directory, '.') - 1) as directorySubStr,
        ct.name as chapter_name
        from t_questionnaire_template qt
        left join t_chapter_template ct
        on qt.chapter_id=ct.id
    </sql>

    <select id="selectTQuestionnaireTemplateList" parameterType="TQuestionnaireTemplate" resultMap="TQuestionnaireTemplateResult">
        <include refid="selectTQuestionnaireTemplateVo"/>
        <where>  
            <if test="chapterId != null "> and chapter_id = #{chapterId}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="directory != null  and directory != ''"> and directory = #{directory}</if>
            <if test="code != null "> and code = #{code}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="deptId != null  and deptId != ''"> and dept_id = #{deptId}</if>
        </where>
        order by (SUBSTR(qt.directory, 1, INSTR(qt.directory, '.') - 1) + 0) asc, code asc
    </select>
    
    <select id="selectTQuestionnaireTemplateById" parameterType="Long" resultMap="TQuestionnaireTemplateResult">
        <include refid="selectTQuestionnaireTemplateVo"/>
        where qt.id = #{id}
    </select>

    <insert id="insertTQuestionnaireTemplate" parameterType="TQuestionnaireTemplate" useGeneratedKeys="true" keyProperty="id">
        insert into t_questionnaire_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chapterId != null">chapter_id,</if>
            <if test="type != null">type,</if>
            <if test="directory != null">directory,</if>
            <if test="code != null">code,</if>
            <if test="name != null">name,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chapterId != null">#{chapterId},</if>
            <if test="type != null">#{type},</if>
            <if test="directory != null">#{directory},</if>
            <if test="code != null">#{code},</if>
            <if test="name != null">#{name},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTQuestionnaireTemplate" parameterType="TQuestionnaireTemplate">
        update t_questionnaire_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="chapterId != null">chapter_id = #{chapterId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="directory != null">directory = #{directory},</if>
            <if test="code != null">code = #{code},</if>
            <if test="name != null">name = #{name},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTQuestionnaireTemplateById" parameterType="Long">
        delete from t_questionnaire_template where id = #{id}
    </delete>

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