<?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.TagMapper">

    <resultMap type="Tag" id="TagResult">
        <result property="id"        column="id"/>
        <result property="companyId" column="company_id"/>
        <result property="companyName" column="companyName"/>
        <result property="groupId"   column="group_id"/>
        <result property="groupName" column="groupName"/>
        <result property="type"      column="type"/>
        <result property="name"      column="name"/>
        <result property="sort"      column="sort"/>
        <result property="status"    column="status"/>
        <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="selectTagVo">
        select
        t.id,
        t.company_id,
        c.company_name as companyName,
        t.group_id,
        g.name as groupName,
        t.type,
        t.name,
        t.sort,
        t.status,
        t.create_time,
        t.update_time,
        t.create_by,
        t.update_by,
        t.deleted
        from tag t
        left join company c
        on c.company_id = t.company_id
        and c.is_del = 0
        left join tag_group g
        on g.id = t.group_id
        and g.deleted = 0

    </sql>

    <select id="selectTagList" parameterType="Tag" resultMap="TagResult">
        <include refid="selectTagVo"/>
        <where>
            <if test="companyId != null">
                and t.company_id = #{companyId}
            </if>
            <if test="groupId != null">
                and t.group_id = #{groupId}
            </if>
            <if test="type != null and type != ''">
                and t.type = #{type}
            </if>
            <if test="name != null and name != ''">
                and t.name like concat('%', #{name}, '%')
            </if>
            <if test="sort != null">
                and t.sort = #{sort}
            </if>
            <if test="status != null ">
                and t.status = #{status}
            </if>
                and t.deleted = 0
        </where>
        order by t.id desc
    </select>



    <select id="selectTagById" parameterType="Long" resultMap="TagResult">
        <include refid="selectTagVo"/>
        where t.id = #{id}
    </select>
    <select id="selectTagByIds" resultType="com.fs.delivery.domain.Tag">
        select * from tag where id in
        <foreach item="id" collection="ids.split(',')" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <insert id="insertTag" parameterType="Tag" useGeneratedKeys="true" keyProperty="id">
        insert into tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="type != null">type,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="sort != null">sort,</if>
            <if test="status != null">status,</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="groupId != null">#{groupId},</if>
            <if test="type != null">#{type},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="sort != null">#{sort},</if>
            <if test="status != null">#{status},</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="updateTag" parameterType="Tag">
        update tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="type != null">type = #{type},</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="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="deleteTagById" parameterType="Long">
        delete from tag where id = #{id}
    </delete>

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