<?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.fs.delivery.mapper.TagGroupMapper">
    
    <resultMap type="TagGroup" id="TagGroupResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="companyName" />
        <result property="name"    column="name"    />
        <result property="sort"    column="sort"    />
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="deleted"    column="deleted"    />
    </resultMap>

    <sql id="selectTagGroupVo">
        select
        tg.id,
        tg.company_id,
        c.company_name as companyName,
        tg.name,
        tg.sort,
        tg.status,
        tg.type,
        tg.create_time,
        tg.update_time,
        tg.create_by,
        tg.update_by,
        tg.deleted
        from tag_group tg
        left join company c
        on c.company_id = tg.company_id
        and c.is_del = 0
    </sql>


    <select id="selectTagGroupList" parameterType="TagGroup" resultMap="TagGroupResult">
        <include refid="selectTagGroupVo"/>
        <where>
            <if test="companyId != null"> and tg.company_id = #{companyId}</if>
            <if test="name != null and name != ''"> and tg.name like concat('%', #{name}, '%')</if>
            <if test="sort != null"> and tg.sort = #{sort}</if>
            <if test="status != null"> and tg.status = #{status}</if>
            <if test="type != null"> and tg.type = #{type}</if>
            <if test="deleted != null"> and tg.deleted = #{deleted}</if>
        </where>
        order by tg.sort, tg.create_time desc
    </select>
    
    <select id="selectTagGroupById" parameterType="Long" resultMap="TagGroupResult">
        <include refid="selectTagGroupVo"/>
        where tg.id = #{id}
    </select>
        
    <insert id="insertTagGroup" parameterType="TagGroup" useGeneratedKeys="true" keyProperty="id">
        insert into tag_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="sort != null">sort,</if>
            <if test="status != null">status,</if>
            <if test="type != null">type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="sort != null">#{sort},</if>
            <if test="status != null">#{status},</if>
            <if test="type != null">#{type},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateTagGroup" parameterType="TagGroup">
        update tag_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="status != null">status = #{status},</if>
            <if test="type != null">type = #{type},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTagGroupById" parameterType="Long">
        delete from tag_group where id = #{id}
    </delete>

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