<?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.ruixuan.live.mapper.KuaishouLiveUserAccountPartMapper">

    <resultMap type="com.ruixuan.live.entity.KuaishouLiveUserAccountPart" id="KuaishouLiveUserAccountPartResult">
        <result property="ksId" column="ks_id"/>
        <result property="ksName" column="ks_name"/>
        <result property="accountId" column="account_id"/>
        <result property="accountName" column="account_name"/>
        <result property="collaboratorId" column="collaborator_id"/>
        <result property="collaborator" column="collaborator"/>
        <result property="createrId" column="creater_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectKuaishouLiveUserAccountPartVo">
        select ks_id,
               ks_name,
               account_id,
               account_name,
               collaborator_id,
               collaborator,
               creater_id,
               create_time,
               update_time
        from kuaishou_live_user_account_part
    </sql>


    <select id="queryCollaboratorsById" parameterType="Long" resultType="com.alibaba.fastjson.JSONObject">
        select collaborator_id as 'userId', collaborator as 'name'
        from kuaishou_live_user_account_part
        where ks_id = #{ksId}
          and collaborator_id is not null
          and account_id = #{accountId}
        group by collaborator_id
    </select>

    <select id="queryAccountCollaborators" parameterType="Long" resultType="com.alibaba.fastjson.JSONObject">
        select 'collaborator' as 'role','协作人' as 'roleName', collaborator_id as 'userId', collaborator as 'name'
        from kuaishou_live_user_account_part
        where ks_id = #{ksId}
          and collaborator_id is not null
          and account_id = #{accountId}
        group by collaborator_id
    </select>

    <select id="selectKuaishouLiveUserAccountPartById" parameterType="Long"
            resultMap="KuaishouLiveUserAccountPartResult">
        <include refid="selectKuaishouLiveUserAccountPartVo"/>
        where ks_id = #{ksId}
    </select>


    <delete id="deleteKuaishouLiveUserAccountPartById" parameterType="Long">
        delete
        from kuaishou_live_user_account_part
        where ks_id = #{ksId}
    </delete>


    <delete id="deleteByAccountId" parameterType="Long">
        delete
        from kuaishou_live_user_account_part
        where ks_id = #{ksId}
        AND account_id = #{accountId}
        <if test="userId != null">
            AND collaborator_id = #{userId}
        </if>

    </delete>


    <insert id="replaceBatch">
        replace into kuaishou_live_user_account_part(
        ks_id,
        ks_name,
        account_id,
        account_name,
        collaborator_id,
        collaborator,
        creater_id)
        values
        <foreach collection="collaborators" item="coll" separator=",">
            (
            #{coll.ksId},
            #{coll.ksName},
            #{coll.accountId},
            #{coll.accountName},
            #{coll.userId},
            #{coll.nickName},
            #{coll.createrId}
            )
        </foreach>
    </insert>
</mapper>