<?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.gitee.gen.mapper.TemplateConfigMapper">
    <resultMap id="BaseResultMap" type="com.gitee.gen.entity.TemplateConfig">
        <result column="id" property="id" />
        <result column="group_id" property="groupId" />
        <result column="group_name" property="groupName" />
        <result column="name" property="name" />
        <result column="folder" property="folder" />
        <result column="file_name" property="fileName" />
        <result column="content" property="content" />
        <result column="is_deleted" property="isDeleted" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id
        , t.group_id
        , t.group_name
        , t.name
        , t.folder
        , t.file_name
        , t.content
        , t.is_deleted
        </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns" />
        FROM template_config t
        WHERE t.is_deleted = 0
    </select>

    <select id="getByName" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM template_config t
        WHERE name = #{name} AND group_id=#{groupId}
        AND is_deleted=0
        LIMIT 1
    </select>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        SELECT
        <include refid="baseColumns" />
        FROM template_config t
        WHERE id = #{id}
    </select>

    <!-- 插入全部字段 -->
    <insert id="insert" parameterType="com.gitee.gen.entity.TemplateConfig"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO template_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            group_id,
            group_name,
            name,
            folder,
            file_name,
            content,
            is_deleted,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{groupId},
            #{groupName},
            #{name},
            #{folder},
            #{fileName},
            #{content},
            #{isDeleted},
        </trim>
    </insert>

    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.gitee.gen.entity.TemplateConfig"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO template_config
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="groupId != null">
                group_id,
            </if>
            <if test="groupName != null">
                group_name,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="fileName != null">
                file_name,
            </if>
            <if test="content != null">
                content,
            </if>
            <if test="isDeleted != null">
                is_deleted,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="groupId != null" >
                #{groupId},
            </if>
            <if test="groupName != null" >
                #{groupName},
            </if>
            <if test="name != null" >
                #{name},
            </if>
            <if test="fileName != null" >
                #{fileName},
            </if>
            <if test="content != null" >
                #{content},
            </if>
            <if test="isDeleted != null" >
                #{isDeleted},
            </if>
        </trim>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.gitee.gen.entity.TemplateConfig">
        UPDATE template_config
        <set>
            group_id=#{groupId},
            group_name=#{groupName},
            name=#{name},
            file_name=#{fileName},
            content=#{content},
            is_deleted=#{isDeleted},
        </set>
        WHERE id = #{id}
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.gitee.gen.entity.TemplateConfig">
        UPDATE template_config
        <set>
            <if test="groupId != null" >
                group_id=#{groupId},
            </if>
            <if test="groupName != null" >
                group_name=#{groupName},
            </if>
            <if test="name != null" >
                name=#{name},
            </if>
            <if test="folder != null" >
                folder=#{folder},
            </if>
            <if test="fileName != null" >
                file_name=#{fileName},
            </if>
            <if test="content != null" >
                content=#{content},
            </if>
            <if test="isDeleted != null" >
                is_deleted=#{isDeleted},
            </if>
        </set>
        WHERE id = #{id}
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete" parameterType="com.gitee.gen.entity.TemplateConfig">
		UPDATE template_config
		SET is_deleted=1
		WHERE id = #{id}
	</delete>

    <select id="listByGroupId" parameterType="java.lang.String" resultType="com.gitee.gen.entity.TemplateConfig">
        SELECT
        <include refid="baseColumns" />
        FROM template_config t
        WHERE group_id = #{groupId} and is_deleted=0
    </select>

    <select id="listByGroupName" parameterType="java.lang.String" resultType="com.gitee.gen.entity.TemplateConfig">
        SELECT
        <include refid="baseColumns" />
        FROM template_config t
        WHERE group_name = #{groupName} and is_deleted=0
    </select>

    <update id="updateGroupNameByGroupId">
        UPDATE template_config
        SET group_name = #{groupName}
        WHERE group_id = #{groupId}
    </update>

    <update id="deleteByGroupId">
        UPDATE template_config
        SET is_deleted=1
        WHERE group_id = #{groupId}
    </update>

    <select id="listTemplate" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns" />
        FROM template_config t
        WHERE id in
        <foreach collection="idList" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

</mapper>
