<?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.TChapterTemplateMapper">
    
    <resultMap type="TChapterTemplate" id="TChapterTemplateResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="deptId"    column="dept_id"    />
    </resultMap>

    <sql id="selectTChapterTemplateVo">
        select id, code, name, dept_id, IF(INSTR(code, '.') > 0, SUBSTR(code, 1, INSTR(code, '.') - 1), code) as codeSubStr from t_chapter_template
    </sql>

    <select id="selectTChapterTemplateList" parameterType="TChapterTemplate" resultMap="TChapterTemplateResult">
        <include refid="selectTChapterTemplateVo"/>
        <where>  
            <if test="code != null  and code != ''"> 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 (IF(INSTR(code, '.') > 0, SUBSTR(code, 1, INSTR(code, '.') - 1), code) + 0) asc, code asc
    </select>

    <select id="selectNameByCode" parameterType="String" resultType="String">
        select name from t_chapter_template where code = #{code}
    </select>
    
    <select id="selectTChapterTemplateById" parameterType="Long" resultMap="TChapterTemplateResult">
        <include refid="selectTChapterTemplateVo"/>
        where id = #{id}
    </select>

    <insert id="insertTChapterTemplate" parameterType="TChapterTemplate" useGeneratedKeys="true" keyProperty="id">
        insert into t_chapter_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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="code != null">#{code},</if>
            <if test="name != null">#{name},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTChapterTemplate" parameterType="TChapterTemplate">
        update t_chapter_template
        <trim prefix="SET" suffixOverrides=",">
            <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="deleteTChapterTemplateById" parameterType="Long">
        delete from t_chapter_template where id = #{id}
    </delete>

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