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

    <resultMap type="CompanyTag" id="CompanyTagResult">
        <result property="tagId"    column="tag_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="tag"    column="tag"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCompanyTagVo">
        select tag_id, company_id, tag, create_time from company_tag
    </sql>

    <select id="selectCompanyTagList" parameterType="CompanyTag" resultMap="CompanyTagResult">
        <include refid="selectCompanyTagVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="tag != null  and tag != ''"> and tag = #{tag}</if>
        </where>
    </select>

    <select id="selectCompanyTagById" parameterType="Long" resultMap="CompanyTagResult">
        <include refid="selectCompanyTagVo"/>
        where tag_id = #{tagId}
    </select>

    <!-- 查询标签列表 -->
    <select id="selectCompanyTagListByMap" resultType="com.fs.company.domain.CompanyTag">
        select ct.* from company_tag ct
        <where>
            <if test="params.keyword != null and params.keyword.length > 0">
                and
                <foreach collection="params.keyword" item="item"  open="(" separator="or" close=")">
                    ct.tag like concat('%',#{item},'%')
                </foreach>
            </if>
            <if test="params.companyId != null">
                and ct.company_id = #{params.companyId}
            </if>
        </where>
    </select>

    <!-- 查询用户标签列表 -->
    <select id="selectCompanyTagListByUserId" resultType="com.fs.company.domain.CompanyTag">
        select ct.* from company_tag ct
                             inner join company_tag_user ctu on ctu.company_id = ct.company_id and find_in_set(ct.tag_id, ctu.tag_ids) > 0
        where ctu.user_id = #{userId}
    </select>
    <select id="findUserTagByUserId" resultType="java.lang.String">
        select tag_ids from company_tag_user where user_id = ${userId} limit 1
    </select>
    <select id="queryAllTagMap" resultType="java.util.Map">
        select tag_id,tag from company_tag
    </select>

    <insert id="insertCompanyTag" parameterType="CompanyTag" useGeneratedKeys="true" keyProperty="tagId">
        insert into company_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="tag != null">tag,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="tag != null">#{tag},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateCompanyTag" parameterType="CompanyTag">
        update company_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="tag != null">tag = #{tag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where tag_id = #{tagId}
    </update>

    <delete id="deleteCompanyTagById" parameterType="Long">
        delete from company_tag where tag_id = #{tagId}
    </delete>

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

</mapper>
