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

    <resultMap type="com.ruixuan.live.entity.KuaishouLiveUserAccount" id="KuaishouLiveUserAccountResult">
        <result property="id" column="id"/>
        <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="accountType" column="account_type"/>
        <result property="saleLeaderId" column="sale_leader_id"/>
        <result property="saleLeader" column="sale_leader"/>
        <result property="operatorLeaderId" column="operator_leader_id"/>
        <result property="operatorLeader" column="operator_leader"/>
        <result property="designerLeaderId" column="designer_leader_id"/>
        <result property="designerLeader" column="designer_leader"/>
        <result property="createrId" column="creater_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="count" column="count"/>
    </resultMap>

    <sql id="selectKuaishouLiveUserAccountVo">
        select id,
               project_id,
               ks_id,
               ks_name,
               account_id,
               account_name,
               account_type,
               payment_method,
               sale_leader_id,
               sale_leader,
               operator_leader_id,
               operator_leader,
               designer_leader_id,
               designer_leader,
               enterprise_name,
               first_industry,
               secondary_industry,
               qualification_status,
               account_status,
               port_name
        from kuaishou_live_user_account
    </sql>

    <select id="selectKuaishouLiveUserAccountList" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount"
            resultMap="KuaishouLiveUserAccountResult">
        select id,
        ks_id,
        ks_name,
        account_id,
        account_name,
        account_type,
        sale_leader_id,
        sale_leader,
        operator_leader_id,
        operator_leader,
        designer_leader_id,
        designer_leader,
        IFNULL(t2.count,0) as 'count'
        from kuaishou_live_user_account t1
        left join (
        select ksId,accountName, IFNULL(SUM(1),0) as 'count'
        from (
        SELECT ks_id as 'ksId',account_name as 'accountName', collaborator FROM kuaishou_live_user_account_part
        WHERE collaborator_id is not null
        GROUP BY ks_id,account_name, collaborator) tt
        GROUP BY ksId ,accountName
        ) t2 ON t2.accountName = t1.account_name AND t2.ksId = t1.ks_id
        where account_type in ('cljn','cljnydd' ,'cp')
        <if test="ksId != null ">and ks_id = #{ksId}</if>
        <if test="ksName != null  and ksName != ''">and ks_name like concat('%', #{ksName}, '%')</if>
        <if test="accountId != null ">and account_id = #{accountId}</if>
        <if test="accountName != null  and accountName != ''">and account_name like concat('%', #{accountName}, '%')
        </if>
        <if test="accountType != null  and accountType != ''">and account_type = #{accountType}
        </if>
        <if test="saleLeaderId != null  and saleLeaderId != ''">and sale_leader_id = #{saleLeaderId}</if>
        <if test="saleLeader != null  and saleLeader != ''">and sale_leader = #{saleLeader}</if>
        <if test="operatorLeaderId != null  and operatorLeaderId != ''">and operator_leader_id =
            #{operatorLeaderId}
        </if>
        <if test="operatorLeader != null  and operatorLeader != ''">and operator_leader = #{operatorLeader}</if>
        <if test="designerLeaderId != null  and designerLeaderId != ''">and designer_leader_id =
            #{designerLeaderId}
        </if>
        <if test="designerLeader != null  and designerLeader != ''">and designer_leader = #{designerLeader}</if>
        <if test="createrId != null  and createrId != ''">and creater_id = #{createrId}</if>

        ORDER BY create_time desc
    </select>


    <select id="selectAllListByAccountIds" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount"
            resultMap="KuaishouLiveUserAccountResult">
        select id,
        ks_id,
        ks_name,
        account_id,
        account_name,
        account_type,
        sale_leader_id,
        sale_leader,
        operator_leader_id,
        operator_leader,
        designer_leader_id,
        designer_leader,
        IFNULL(t2.count,0) as 'count'
        from kuaishou_live_user_account t1
        left join (
        select ksId,accountName, IFNULL(SUM(1),0) as 'count'
        from (
        SELECT ks_id as 'ksId',account_name as 'accountName', collaborator FROM kuaishou_live_user_account_part
        GROUP BY ks_id,account_name, collaborator) tt
        GROUP BY ksId ,accountName
        ) t2 ON t2.accountName = t1.account_name AND t2.ksId = t1.ks_id
        <where>
            <if test="accountIds != null and accountIds.size() > 0">
                and account_id in
                <foreach item="id" index="index" collection="accountIds"
                         open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="ksId != null ">and ks_id = #{ksId}</if>
            <if test="ksName != null  and ksName != ''">and ks_name like concat('%', #{ksName}, '%')</if>
            <if test="accountId != null ">and account_id = #{accountId}</if>
            <if test="accountName != null  and accountName != ''">and account_name like concat('%', #{accountName}, '%')
            </if>
            <if test="accountType != null  and accountType != ''">and account_type = #{accountType}
            </if>
            <if test="saleLeaderId != null  and saleLeaderId != ''">and sale_leader_id = #{saleLeaderId}</if>
            <if test="saleLeader != null  and saleLeader != ''">and sale_leader = #{saleLeader}</if>
            <if test="operatorLeaderId != null  and operatorLeaderId != ''">and operator_leader_id =
                #{operatorLeaderId}
            </if>
            <if test="operatorLeader != null  and operatorLeader != ''">and operator_leader = #{operatorLeader}</if>
            <if test="designerLeaderId != null  and designerLeaderId != ''">and designer_leader_id =
                #{designerLeaderId}
            </if>
            <if test="designerLeader != null  and designerLeader != ''">and designer_leader = #{designerLeader}</if>
            <if test="createrId != null  and createrId != ''">and creater_id = #{createrId}</if>
            <if test="accountStatus != null  and accountStatus != ''">and account_status = #{accountStatus}</if>
        </where>
        ORDER BY create_time desc
    </select>


    <select id="queryAllList" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount"
            resultMap="KuaishouLiveUserAccountResult">
        <include refid="selectKuaishouLiveUserAccountVo"/>
        <where>
            <if test="ksId != null ">and ks_id = #{ksId}</if>
            <if test="ksName != null  and ksName != ''">and ks_name like concat('%', #{ksName}, '%')</if>
            <if test="accountId != null ">and account_id = #{accountId}</if>
            <if test="accountName != null  and accountName != ''">and account_name like concat('%', #{accountName}, '%')
            </if>
            <if test="accountType != null  and accountType != ''">and account_type= #{accountType}</if>
            <if test="saleLeaderId != null  and saleLeaderId != ''">and sale_leader_id = #{saleLeaderId}</if>
            <if test="saleLeader != null  and saleLeader != ''">and sale_leader = #{saleLeader}</if>
            <if test="operatorLeaderId != null  and operatorLeaderId != ''">and operator_leader_id =
                #{operatorLeaderId}
            </if>
            <if test="operatorLeader != null  and operatorLeader != ''">and operator_leader = #{operatorLeader}</if>
            <if test="designerLeaderId != null  and designerLeaderId != ''">and designer_leader_id =
                #{designerLeaderId}
            </if>
            <if test="designerLeader != null  and designerLeader != ''">and designer_leader = #{designerLeader}</if>
            <if test="createrId != null  and createrId != ''">and creater_id = #{createrId}</if>
            <if test="accountStatus != null  and accountStatus != ''">and account_status = #{accountStatus}</if>
        </where>
        ORDER BY create_time desc
    </select>

    <select id="queryAllListByAccountIds" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount"
            resultMap="KuaishouLiveUserAccountResult">
        <include refid="selectKuaishouLiveUserAccountVo"/>
        <where>
            <if test="accountIds != null and accountIds.size() > 0">
                and account_id in
                <foreach item="id" index="index" collection="accountIds"
                         open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>

            <if test="ksId != null ">and ks_id = #{ksId}</if>
            <if test="ksName != null  and ksName != ''">and ks_name like concat('%', #{ksName}, '%')</if>
            <if test="accountId != null ">and account_id = #{accountId}</if>
            <if test="accountName != null  and accountName != ''">and account_name like concat('%', #{accountName}, '%')
            </if>
            <if test="accountType != null  and accountType != ''">and account_type= #{accountType}</if>
            <if test="saleLeaderId != null  and saleLeaderId != ''">and sale_leader_id = #{saleLeaderId}</if>
            <if test="saleLeader != null  and saleLeader != ''">and sale_leader = #{saleLeader}</if>
            <if test="operatorLeaderId != null  and operatorLeaderId != ''">and operator_leader_id =
                #{operatorLeaderId}
            </if>
            <if test="operatorLeader != null  and operatorLeader != ''">and operator_leader = #{operatorLeader}</if>
            <if test="designerLeaderId != null  and designerLeaderId != ''">and designer_leader_id =
                #{designerLeaderId}
            </if>
            <if test="designerLeader != null  and designerLeader != ''">and designer_leader = #{designerLeader}</if>
            <if test="createrId != null  and createrId != ''">and creater_id = #{createrId}</if>
            <if test="accountStatus != null  and accountStatus != ''">and account_status = #{accountStatus}</if>
        </where>
        ORDER BY create_time desc
    </select>


    <select id="getOne" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount"
            resultType="com.ruixuan.live.entity.KuaishouLiveUserAccount">
        <include refid="selectKuaishouLiveUserAccountVo"/>
        <where>
            <if test="ksId != null ">and ks_id = #{ksId}</if>
            <if test="accountId != null ">and account_id = #{accountId}</if>
        </where>
        LIMIT 1
    </select>

    <select id="queryAccountIdsByUserId" parameterType="Long" resultType="Long">
        SELECT account_id
        FROM (
                 SELECT account_id
                 FROM `kuaishou_live_user_account`
                 WHERE sale_leader_id = #{userId}
                    OR operator_leader_id = #{userId}
                    OR designer_leader_id = #{userId}
                 UNION ALL
                 SELECT account_id
                 FROM `kuaishou_live_user_account_part`
                 WHERE collaborator_id = #{userId}
             ) t
        GROUP BY account_id
    </select>

    <select id="selectKuaishouLiveUserAccountById" parameterType="String" resultMap="KuaishouLiveUserAccountResult">
        <include refid="selectKuaishouLiveUserAccountVo"/>
        where id = #{id}
    </select>

    <select id="queryNicknameList" resultType="com.alibaba.fastjson.JSONObject">
        select ks_id as 'ksId', ks_name as 'ksName'
        from kuaishou_live_user_account
        GROUP BY ks_id
    </select>

    <select id="queryAccountListByKsId" parameterType="Long" resultType="com.alibaba.fastjson.JSONObject">
        select ks_id as 'ksId', ks_name as 'ksName', account_id as 'accountId', account_name as 'accountName'
        from kuaishou_live_user_account
        where ks_id = #{ksId}
    </select>


    <insert id="insertKuaishouLiveUserAccount" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount">
        insert into kuaishou_live_user_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="ksId != null">ks_id,</if>
            <if test="ksName != null">ks_name,</if>
            <if test="accountId != null">account_id,</if>
            <if test="accountName != null">account_name,</if>
            <if test="accountType != null">account_type,</if>
            <if test="paymentMethod != null">payment_method,</if>
            <if test="saleLeaderId != null">sale_leader_id,</if>
            <if test="saleLeader != null">sale_leader,</if>
            <if test="operatorLeaderId != null">operator_leader_id,</if>
            <if test="operatorLeader != null">operator_leader,</if>
            <if test="designerLeaderId != null">designer_leader_id,</if>
            <if test="designerLeader != null">designer_leader,</if>
            <if test="enterpriseName != null">enterprise_name,</if>
            <if test="firstIndustry != null">first_industry,</if>
            <if test="secondaryIndustry != null">secondary_industry,</if>
            <if test="qualificationStatus != null">qualification_status,</if>
            <if test="accountStatus != null">account_status,</if>
            <if test="portName != null">port_name,</if>
            <if test="createrId != null">creater_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="ksId != null">#{ksId},</if>
            <if test="ksName != null">#{ksName},</if>
            <if test="accountId != null">#{accountId},</if>
            <if test="accountName != null">#{accountName},</if>
            <if test="accountType != null">#{accountType},</if>
            <if test="paymentMethod != null">#{paymentMethod},</if>
            <if test="saleLeaderId != null">#{saleLeaderId},</if>
            <if test="saleLeader != null">#{saleLeader},</if>
            <if test="operatorLeaderId != null">#{operatorLeaderId},</if>
            <if test="operatorLeader != null">#{operatorLeader},</if>
            <if test="designerLeaderId != null">#{designerLeaderId},</if>
            <if test="designerLeader != null">#{designerLeader},</if>
            <if test="enterpriseName != null">#{enterpriseName},</if>
            <if test="firstIndustry != null">#{firstIndustry},</if>
            <if test="secondaryIndustry != null">#{secondaryIndustry},</if>
            <if test="qualificationStatus != null">#{qualificationStatus},</if>
            <if test="accountStatus != null">#{accountStatus},</if>
            <if test="portName != null">#{portName},</if>
            <if test="createrId != null">#{createrId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateKuaishouLiveUserAccount" parameterType="com.ruixuan.live.entity.KuaishouLiveUserAccount">
        update kuaishou_live_user_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            ks_id = #{ksId},
            ks_name = #{ksName},
            account_id = #{accountId},
            account_name = #{accountName},
            account_type = #{accountType},
            <if test="paymentMethod != null">payment_method = #{paymentMethod},</if>
            sale_leader_id = #{saleLeaderId},
            <if test="saleLeader != null">sale_leader = #{saleLeader},</if>
            operator_leader_id = #{operatorLeaderId},
            <if test="operatorLeader != null">operator_leader = #{operatorLeader},</if>
            designer_leader_id = #{designerLeaderId},
            <if test="designerLeader != null">designer_leader = #{designerLeader},</if>
            <if test="enterpriseName != null">enterprise_name = #{enterpriseName},</if>
            <if test="firstIndustry != null">first_industry = #{firstIndustry},</if>
            <if test="secondaryIndustry != null">secondary_industry = #{secondaryIndustry},</if>
            <if test="qualificationStatus != null">qualification_status = #{qualificationStatus},</if>
            <if test="accountStatus != null">account_status = #{accountStatus},</if>
            <if test="portName != null">port_name = #{portName},</if>
            <if test="createrId != null">creater_id = #{createrId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKuaishouLiveUserAccountById" parameterType="String">
        delete
        from kuaishou_live_user_account
        where id = #{id}
    </delete>

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


    <insert id="replaceBatch">
        replace into kuaishou_live_user_account(
        id,
        project_id,
        ks_id,
        ks_name,
        account_id,
        account_name,
        account_type,
        payment_method,
        sale_leader_id,
        sale_leader,
        operator_leader_id,
        operator_leader,
        designer_leader_id,
        designer_leader,
        enterprise_name,
        first_industry,
        secondary_industry,
        qualification_status,
        account_status,
        port_name,
        creater_id
        )
        values
        <foreach collection="accountList" item="account" separator=",">
            (
            #{account.id},
            #{account.projectId},
            #{account.ksId},
            #{account.ksName},
            #{account.accountId},
            #{account.accountName},
            #{account.accountType},
            #{account.paymentMethod},
            #{account.saleLeaderId},
            #{account.saleLeader},
            #{account.operatorLeaderId},
            #{account.operatorLeader},
            #{account.designerLeaderId},
            #{account.designerLeader},
            #{account.enterpriseName},
            #{account.firstIndustry},
            #{account.secondaryIndustry},
            #{account.qualificationStatus},
            #{account.accountStatus},
            #{account.portName},
            #{account.createrId}
            )
        </foreach>
    </insert>


</mapper>
