<?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.store.mapper.FsUserCompanyUserMapper">

    <resultMap type="FsUserCompanyUser" id="FsUserCompanyUserResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="isRepeatFans"    column="is_repeat_fans"    />
    </resultMap>

    <sql id="selectFsUserCompanyUserVo">
        select id, user_id, company_user_id, company_id, is_repeat_fans from fs_user_company_user
    </sql>

    <select id="selectFsUserCompanyUserList" parameterType="FsUserCompanyUser" resultMap="FsUserCompanyUserResult">
        <include refid="selectFsUserCompanyUserVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isRepeatFans != null "> and is_repeat_fans = #{isRepeatFans}</if>
        </where>
    </select>

    <select id="selectFsUserCompanyUserById" parameterType="Long" resultMap="FsUserCompanyUserResult">
        <include refid="selectFsUserCompanyUserVo"/>
        where id = #{id}
    </select>

    <insert id="insertFsUserCompanyUser" parameterType="FsUserCompanyUser" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_company_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isRepeatFans != null">is_repeat_fans,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isRepeatFans != null">#{isRepeatFans},</if>
         </trim>
    </insert>

    <update id="updateFsUserCompanyUser" parameterType="FsUserCompanyUser">
        update fs_user_company_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isRepeatFans != null">is_repeat_fans = #{isRepeatFans},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="transfer">
        update fs_user_company_user set company_user_id=#{param.targetCompanyUserId}
        <where>
            <if test="param.userIds != null and param.userIds.size() > 0">
                user_id in
                <foreach collection="param.userIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </update>

    <delete id="deleteFsUserCompanyUserById" parameterType="Long">
        delete from fs_user_company_user where id = #{id}
    </delete>

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

    <select id="selectRepeatCompanyUserName" resultType="FsUserCompanyUser">
        SELECT
            GROUP_CONCAT( company_user.nick_name ) AS repeatCompanyUserName,
            fs_user_company_user.user_id
        FROM
            fs_user_company_user
                LEFT JOIN company_user ON company_user.user_id = fs_user_company_user.company_user_id
        <where>
            fs_user_company_user.user_id IN
            <foreach item="userId" collection="userIds" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </where>
        GROUP BY
            fs_user_company_user.user_id
    </select>
</mapper>
