<?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="cn.com.ctop.common.module.mapper.UserAllocationMapper">

    <select id="selectAccountListByProjectId" resultType="cn.com.ctop.common.module.entity.UserAllocation">
        select
        a.user_id as userId,
        a.user_name as userName,
        a.auth_name as authName,
        a.account_id as accountId
        from
        ctop_user_allocation a
        left join ctop_project b on a.project_id = b.id
        where b.id = #{projectId}
    </select>

    <select id="queryAdvertiserId" resultType="String">
        select
            distinct advertiser_id
        from
            ctop_user_allocation
        where account_id=#{accountId}
    </select>
    <select id="queryUserIdByAccountId" resultType="String">
        select
            distinct user_id
        from
            ctop_user_allocation
        where account_id=#{accountId}
    </select>

    <select id="getAccountListByProject" resultType="com.alibaba.fastjson.JSONObject">
       select
       t1.account_id as 'accountId',
       (select  realname  from sys_user where  id = t1.user_id ) as 'userName',
       t1.auth_name as 'authName'
       from ctop_user_allocation t1
       where t1.project_id = #{projectId}
       order by t1.create_time desc
    </select>

    <select id="getAccountListByProjectList" resultType="com.alibaba.fastjson.JSONObject">
        select
        t1.project_id as 'projectId',
        t1.account_id as 'accountId',
        (select realname from sys_user where id = t1.user_id ) as 'userName',
        t1.auth_name as 'authName',
        t1.account_status as 'accountStatus'
        from ctop_user_allocation t1
        where t1.project_id in
        <foreach collection="list" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>


        order by t1.create_time desc
    </select>


    <select id="getUserIdListByProjectId" resultType="com.alibaba.fastjson.JSONObject">
   SELECT t1.user_id as 'userId',
    t1.account_id as 'accountId',
    t1.user_name  as 'userName'
    from  ctop_kuaishou_advertiser_base_info t1  LEFT JOIN
    ctop_user_allocation  t2
    ON t1.account_id = t2.account_id
    WHERE t2.project_id = #{projectId}
    GROUP BY t1.user_id;
    </select>


    <select id="getAccountIdListByUserId" resultType="com.alibaba.fastjson.JSONObject">
    SELECT
    t1.account_id as 'accountId',
    t1.auth_name  as 'authName',
    t1.user_name as 'userName'
    from ctop_user_allocation   t1  LEFT JOIN
    ctop_kuaishou_advertiser_base_info t2
    ON t1.account_id = t2.account_id
    WHERE t2.user_id = #{userId}
    AND t1.account_status = 0
    </select>

    <select id="getAccountIdsByUserId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            allocation.account_id,
            allocation.auth_name,
            project.project_name as project_name
        FROM
            ctop_user_allocation allocation
            left join ctop_project project on allocation.project_id = project.id
        WHERE
            allocation.user_id = #{userId}
          and allocation.account_status=0
    </select>


    <select id="getAllAccountIdsByMediaId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            account_id,
            auth_name,
            project_name
        FROM
            ctop_user_allocation
        WHERE
            media_id=#{mediaId}
          and account_status=0
    </select>

    <select id="getProjectIdByAccountId" resultType="java.lang.Long">
    select  project_id  from  ctop_user_allocation
    where account_id = #{accountId}
    limit 1
    </select>

    <select id="getUserAllocation" resultType="cn.com.ctop.common.module.entity.UserAllocation">
    select  *  from  ctop_user_allocation
    where account_id=#{accountId}
    limit 1
    </select>

    <select id="getSwitchStatusByAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            monitoring_link,
            landing_page
        FROM
            ctop_user_allocation
        WHERE
            account_id = #{accountId}
    </select>

    <select id="queryAccountsByDeleteComment" resultType="java.lang.Long">
        SELECT
            account_id
        FROM
            ctop_user_allocation
        WHERE
            media_id = 2
          AND account_status = 0
          AND delete_comment = 0
    </select>
    <select id="getCompanyNameByUserId" resultType="java.lang.String">
        select company_name from user_company where user_id = #{planId} limit 1
    </select>
    <select id="listByMediaId" resultType="cn.com.ctop.common.module.entity.UserAllocation">
        select a.* from ctop_user_allocation a
        left join ctop_project p on p.id = a.project_id
        where
        1=1
        <if test="null!=accountStatus">
            and a.account_status = #{accountStatus}
        </if>
        <if test="null!=mediaId">
            and p.media_id = #{mediaId}
        </if>
    </select>


</mapper>
