<?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.flossom.common.core.mapper.WxUserTagMapper">

    <resultMap type="WxUserTag" id="WxUserTagResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="tagName"    column="tag_name"    />
        <result property="tagId"    column="tag_id"    />
        <result property="type"    column="type"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectWxUserTagVo">
        select id, user_id, tag_name, tag_id, type, status, create_by, create_time, update_by, update_time from wx_user_tag
    </sql>

    <select id="selectWxUserTagList" parameterType="WxUserTag" resultMap="WxUserTagResult">
        <include refid="selectWxUserTagVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="tagName != null  and tagName != ''"> and tag_name like concat('%', #{tagName}, '%')</if>
            <if test="tagId != null "> and tag_id = #{tagId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectWxUserTagById" parameterType="Long" resultMap="WxUserTagResult">
        <include refid="selectWxUserTagVo"/>
        where id = #{id}
    </select>

    <select id="selectWxUserTagByTagId" resultType="java.lang.Integer">
        SELECT
            user_id
        FROM
            `wx_user_tag`
        WHERE
            tag_id = #{tagId} AND type = #{type} AND status = 0
    </select>

    <insert id="insertWxUserTag" parameterType="WxUserTag" useGeneratedKeys="true" keyProperty="id">
        insert into wx_user_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="tagName != null">tag_name,</if>
            <if test="tagId != null">tag_id,</if>
            <if test="type != null">type,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="tagName != null">#{tagName},</if>
            <if test="tagId != null">#{tagId},</if>
            <if test="type != null">#{type},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        insert into wx_user_tag (user_id, tag_name, tag_id, `type`, create_time)
        values
        <foreach collection="list" item="userTag" separator="," >
            (
                #{userTag.userId},
                #{userTag.tagName},
                #{userTag.tagId},
                #{userTag.type},
                #{userTag.createTime}
            )
        </foreach>
    </insert>

    <update id="updateWxUserTag" parameterType="WxUserTag">
        update wx_user_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="tagName != null">tag_name = #{tagName},</if>
            <if test="tagId != null">tag_id = #{tagId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWxUserTagById" parameterType="Long">
        delete from wx_user_tag where id = #{id}
    </delete>

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

    <delete id="deleteBatch">
        DELETE
        FROM
            wx_user_tag
        WHERE
            tag_id IN
                <foreach item="tagId" collection="tagIdList" open="(" separator="," close=")">
                    #{tagId}
                </foreach>
            AND user_id IN
                <foreach item="userId" collection="userIdList" open="(" separator="," close=")">
                    #{userId}
                </foreach>
    </delete>

    <delete id="deleteWxUserTagByUserId">
        delete from wx_user_tag where user_id = #{userId}
    </delete>

    <delete id="deleteWxUserTagByTagId">
        delete from wx_user_tag where tag_id = #{tagId}
    </delete>
</mapper>
