<?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.company.mapper.CompanyTagUserMapper">

    <resultMap type="CompanyTagUser" id="CompanyTagUserResult">
        <result property="id"    column="id"    />
        <result property="tagIds"    column="tag_ids"    />
        <result property="userId"    column="user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCompanyTagUserVo">
        select id, tag_ids, user_id, company_id, company_user_id, create_time from company_tag_user
    </sql>

    <select id="selectCompanyTagUserList" parameterType="CompanyTagUser" resultMap="CompanyTagUserResult">
        <include refid="selectCompanyTagUserVo"/>
        <where>
            <if test="tagIds != null  and tagIds != ''"> and tag_ids = #{tagIds}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
    </select>

    <select id="selectCompanyTagUserById" parameterType="Long" resultMap="CompanyTagUserResult">
        <include refid="selectCompanyTagUserVo"/>
        where id = #{id}
    </select>

    <insert id="insertCompanyTagUser" parameterType="CompanyTagUser" useGeneratedKeys="true" keyProperty="id">
        insert into company_tag_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tagIds != null">tag_ids,</if>
            <if test="userId != null">user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tagIds != null">#{tagIds},</if>
            <if test="userId != null">#{userId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateCompanyTagUser" parameterType="CompanyTagUser">
        update company_tag_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="tagIds != null">tag_ids = #{tagIds},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyTagUserById" parameterType="Long">
        delete from company_tag_user where id = #{id}
    </delete>

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

    <!-- 删除用户标签 -->
    <delete id="deleteCompanyTagUserByMap">
        delete from company_tag_user
        where user_id = #{params.userId} and company_id = #{params.companyId} and company_user_id = #{params.companyUserId}
    </delete>

    <select id="getTagList" resultType="com.fs.store.vo.h5.CompanyUserTagListVO">
        SELECT distinct
        company_tag.tag as tagName,
        company_tag.tag_id
        FROM
        company_tag_user
        right JOIN company_tag ON FIND_IN_SET( company_tag.tag_id, company_tag_user.tag_ids ) > 0
        WHERE 1 = 1
        <if test="userIds != null and userIds.size() > 0 ">
        AND company_tag_user.company_user_id in
        <foreach item="item" collection="userIds" open="(" separator="," close=")">
              #{item}
        </foreach>
        </if>
        <if test="keywords != null and keywords.length > 0 ">
            and
            <foreach item="item" collection="keywords" open="(" separator="or" close=")">
                company_tag.tag like concat('%',#{item},'%')
            </foreach>
        </if>
    </select>

    <!-- 根据条件查询标签下用户 -->
    <select id="selectUserListByMap" resultType="com.fs.company.vo.CompanyTagUserVO">
        select
            fu.user_id,
            fu.nickname as userName
        from company_tag_user ctu
        inner join fs_user fu on fu.user_id = ctu.user_id
        where ctu.company_id = #{params.companyId} and
        <foreach collection="params.tagIds" item="tagId" open="(" separator="or" close=")">
            find_in_set(#{tagId}, ctu.tag_ids)
        </foreach>
    </select>

</mapper>
