<?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.xingxi.master.tag.mapper.TagMapper">
    <resultMap type="Tag" id="TagResult">
        <result property="tagId"    column="tagId"    />
        <result property="tagName"    column="tagName"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectTag">
        select tagId, tagName, delFlag, createUser, createTime, updateUser, updateTime from m_tag
    </sql>

    <select id="selectTagList" parameterType="Tag" resultMap="TagResult">
        <include refid="selectTag"/>
        <where>  
            <if test="tagName != null  and tagName != ''"> and tagName like concat('%', #{tagName}, '%')</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectTagByTagId" parameterType="Long" resultMap="TagResult">
        <include refid="selectTag"/>
        where tagId = #{tagId}
    </select>
        
    <insert id="insertTag" parameterType="Tag" useGeneratedKeys="true" keyProperty="tagId">
        insert into m_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tagName != null">tagName,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tagName != null">#{tagName},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertTag">
        insert into m_tag( tagName, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.tagName}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateTag" parameterType="Tag">
        update m_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="tagName != null">tagName = #{tagName},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where tagId = #{tagId}
    </update>

    <delete id="deleteTagByTagId" parameterType="Long">
        delete from m_tag where tagId = #{tagId}
    </delete>

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

    <update id="logicDeleteTagByTagId" parameterType="Long">
        update m_tag
            set delFlag = '1'
        where tagId = #{tagId}
    </update>

    <update id="logicDeleteTagByTagIds" parameterType="String">
        update m_tag
        set delFlag = '1'
        where tagId in
        <foreach item="tagId" collection="array" open="(" separator="," close=")">
            #{tagId}
        </foreach>
    </update>

    <update id="logicDeleteTagByCondition" parameterType="Tag">
        update m_tag
        set delFlag = '1'
        <where>
            <if test="tagName != null  and tagName != ''"> and tagName like concat('%', #{tagName}, '%')</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>