<?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.qw.mapper.QwTagMapper">

    <resultMap type="QwTag" id="QwTagResult">
        <result property="id"    column="id"    />
        <result property="tagId"    column="tag_id"    />
        <result property="name"    column="name"    />
        <result property="groupId"    column="group_id"    />
        <result property="order"    column="order"    />
        <result property="corpId"    column="corp_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="tagFrom"    column="tag_from"    />
    </resultMap>

    <sql id="selectQwTagVo">
        select id, tag_id, name, group_id, `order`, corp_id, company_id, create_time, update_time, tag_from from qw_tag
    </sql>

    <select id="selectQwTagList" parameterType="QwTag" resultMap="QwTagResult">
        <include refid="selectQwTagVo"/>
        <where>
            <if test="tagId != null  and tagId != ''"> and tag_id = #{tagId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="groupId != null  and groupId != ''"> and group_id = #{groupId}</if>
            <if test="order != null  and order != ''"> and `order` = #{order}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="tagFrom != null "> and tag_from = #{tagFrom}</if>
        </where>
    </select>

    <select id="selectQwTagById" parameterType="Long" resultMap="QwTagResult">
        <include refid="selectQwTagVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwTag" parameterType="QwTag">
        insert into qw_tag
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="tagId != null">tag_id,</if>
            <if test="name != null">name,</if>
            <if test="groupId != null">group_id,</if>
            <if test="order != null">`order`,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="tagFrom != null">tag_from,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="tagId != null">#{tagId},</if>
            <if test="name != null">#{name},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="order != null">#{order},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="tagFrom != null">#{tagFrom},</if>
         </trim>
    </insert>

    <update id="updateQwTag" parameterType="QwTag">
        update qw_tag
        <trim prefix="SET" suffixOverrides=",">
            <if test="tagId != null">tag_id = #{tagId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="order != null">`order` = #{order},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="tagFrom != null">tag_from = #{tagFrom},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwTagById" parameterType="Long">
        delete from qw_tag where id = #{id}
    </delete>

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

    <delete id="deleteQwTagByGroupId" parameterType="String">
        delete from qw_tag where group_id =#{id}
    </delete>

    <select id="selectTagListByUserId" resultType="QwTagVO">
        SELECT DISTINCT
            qw_tag.id,
            qw_tag.tag_id,
            qw_tag.`name`,
            qw_tag.corp_id
        FROM
            qw_tag
                LEFT JOIN qw_external_contact qec ON qw_tag.corp_id = qec.corp_id
                LEFT JOIN qw_user ON qw_user.qw_user_id = qec.user_id
                LEFT JOIN company_user cu ON qw_user.company_user_id = cu.user_id
        WHERE
            cu.user_id = #{param.userId}
        <if test="keywords != null and keywords.length > 0 ">
            and
            <foreach item="item" collection="keywords" open="(" separator="or" close=")">
                qw_tag.`name` like concat('%',#{item},'%')
            </foreach>
        </if>
    </select>
</mapper>
