<?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="org.jeecg.ctop.material.mapper.ManagerSaleValueMapper">

    <select id="getRoleCodeByUserId" resultType="java.lang.String">
        select c.role_code
        from `jeecg-boot`.sys_user a
                 left join `jeecg-boot`.sys_user_role b on a.id = b.user_id
                 left join `jeecg-boot`.sys_role c on b.role_id = c.id
        where a.id = #{userId} LIMIT 1
    </select>


    <select id="queryUserCompany" resultType="com.alibaba.fastjson.JSONObject">
        select company_id as 'companyId',company_name as 'companyName'
        from `jeecg-boot`.user_company
        where user_id = #{userId}
    </select>

    <select id="getUserNameById" resultType="String">
        select realname
        from `jeecg-boot`.sys_user
        where id = #{userId}
    </select>

    <select id="queryList" resultType="com.alibaba.fastjson.JSONObject">
        select company_id as 'companyId',company_name as 'companyName'
        from `jeecg-boot`.sys_manager_company
    </select>

    <select id="querySaleDirctorItsTeamMember" resultType="String">
        select user_id as 'userId'
        from `application`.app_user_manage
        where manage_id = #{userId}
    </select>


    <select id="getCompanyAccountIds" resultType="Long">
        SELECT t1.account_id
        FROM `jeecg-boot`.ctop_user_allocation t1
        LEFT JOIN `jeecg-boot`.user_company t2 ON t1.user_id = t2.user_id
        WHERE t2.company_id = #{companyId}
        <if test='mediaId ==2'>
            AND (media_id = 2 or media_id = 4)
        </if>
        <if test='mediaId ==1'>
            AND (media_id = 1 or media_id = 3)
        </if>
        and t1.account_status = 0
        GROUP BY t1.account_id
    </select>


    <select id="queryKsTodayCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT IFNULL(t.charge, 0.00) as 'totalCharge', CONCAT(
        CAST(IFNULL(ROUND((t.charge - y.charge) / y.charge * 100, 2), 0) as CHAR), '%') as 'chargeRoi'
        FROM (
        SELECT sum(charge) as 'charge'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_date = DATE_FORMAT(#{statDay}, '%Y%m%d')
        <if test="hour !=null">
            AND hour &lt;= #{hour}
        </if>
        ) t
        LEFT JOIN
        (
        SELECT sum(charge) as 'charge'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_date = DATE_FORMAT(DATE_ADD(#{statDay},INTERVAL -1 day), '%Y%m%d')
        <if test="hour !=null">
            AND hour &lt;= #{hour}
        </if>
        ) y on 1 = 1
    </select>

    <select id="queryTtTodayCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT IFNULL(t.charge, 0.00) as 'totalCharge', CONCAT(
        CAST(IFNULL(ROUND((t.charge - tt.charge) / tt.charge * 100, 2), 0) as CHAR), '%') as 'chargeRoi'
        FROM (
        SELECT sum(cost) as 'charge'
        FROM application.bytedance_advertiser_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_datetime = DATE_FORMAT(#{statDay}, '%Y%m%d')
        <if test="hour !=null">
            AND hour &lt;= #{hour}
        </if>
        ) t
        LEFT JOIN
        (
        SELECT sum(cost) as 'charge'
        FROM application.bytedance_advertiser_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_datetime = DATE_FORMAT(DATE_ADD(#{statDay},INTERVAL -1 day), '%Y%m%d')
        <if test="hour !=null">
            AND hour &lt;= #{hour}
        </if>
        ) tt on 1 = 1
    </select>

    <select id="queryTtManydaysTotalCharge" resultType="String">
        SELECT IFNULL(sum(cost), 0.00)
        FROM application.bytedance_advertiser_report_daily_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_datetime >= DATE_FORMAT(#{startDay}, '%Y%m%d')
        and stat_datetime &lt; DATE_FORMAT(#{endDay}, '%Y%m%d')
    </select>

    <select id="queryTtManydaysCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DATE_FORMAT(stat_datetime,'%Y-%m-%d') as 'statDate', IFNULL(sum(cost), 0.00) as 'charge'
        FROM application.bytedance_advertiser_report_daily_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_datetime >= DATE_FORMAT(#{startTime}, '%Y%m%d')
        and stat_datetime &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d')
        GROUP BY stat_datetime
        order by stat_datetime
    </select>

    <select id="queryKsManydaysCharge" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DATE_FORMAT(stat_date,'%Y-%m-%d') as 'statDate', IFNULL(sum(charge), 0.00) as 'charge'
        FROM application.kuaishou_account_report_daily_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_date >= DATE_FORMAT(#{startTime}, '%Y%m%d')
        and stat_date &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d')
        GROUP BY stat_date
        order by stat_date
    </select>

    <select id="queryKsManydaysTotalCharge" resultType="String">
        SELECT IFNULL(sum(charge), 0.00)
        FROM application.kuaishou_account_report_daily_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_date >= DATE_FORMAT(#{startDay}, '%Y%m%d')
        and stat_date &lt; DATE_FORMAT(#{endDay}, '%Y%m%d')
    </select>

    <select id="queryKsRealCharged" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL( thisMonth.realCharged, 0.00) AS 'realCharged',
        CONCAT( CAST( IFNULL( ROUND(( thisMonth.realCharged - lastMonth.realCharged ) / lastMonth.realCharged *
        100,2),0) AS CHAR),'%') AS 'realChargedRoi'
        FROM
        (
        SELECT
        IFNULL( sum( real_charged_in_yuan ), 0.00 ) AS 'realCharged'
        FROM
        application.app_kuaishou_agent_report_d
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT( #{monthFirstday}, '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT( #{statDay}, '%Y%m%d' )
        ) thisMonth
        LEFT JOIN (
        SELECT
        IFNULL( sum( real_charged_in_yuan ), 0.00 ) AS 'realCharged'
        FROM
        application.app_kuaishou_agent_report_d
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT(#{lastMonthFirstday}, '%Y%m%d' )
        AND stat_date &lt; DATE_FORMAT(#{monthFirstday}, '%Y%m%d' )
        ) lastMonth ON 1=1
    </select>

    <select id="queryTtRealCharged" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL( thisMonth.realCharged, 0.00) AS 'realCharged',
        CONCAT( CAST( IFNULL( ROUND(( thisMonth.realCharged - lastMonth.realCharged ) / lastMonth.realCharged *
        100,2),0) AS CHAR),'%') AS 'realChargedRoi'
        FROM
        (
        SELECT
        IFNULL( sum( cash_cost ), 0.00 ) AS 'realCharged'
        FROM
        application.app_bytedance_agent_report_d
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT( #{monthFirstday}, '%Y%m%d' )
        AND stat_datetime &lt;= DATE_FORMAT( #{statDay}, '%Y%m%d' )
        ) thisMonth
        LEFT JOIN (
        SELECT
        IFNULL( sum( cash_cost ), 0.00 ) AS 'realCharged'
        FROM
        application.app_bytedance_agent_report_d
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT(#{lastMonthFirstday}, '%Y%m%d' )
        AND stat_datetime &lt; DATE_FORMAT(#{monthFirstday}, '%Y%m%d' )
        ) lastMonth ON 1=1
    </select>


    <select id="getWeekly" resultType="Long">
        SELECT IFNULL(weekly_goal_value, 0)
        FROM `jeecg-boot`.sys_manager_sale_value
        WHERE manage_id = #{userId}
          AND media_id = #{mediaId}
          AND company_id = #{companyId}
    </select>
    <select id="getMonthly" resultType="Long">
        SELECT IFNULL(monthly_goal_value, 0)
        FROM `jeecg-boot`.sys_manager_sale_value
        WHERE manage_id = #{userId}
          AND media_id = #{mediaId}
          AND company_id = #{companyId}
    </select>

    <select id="querySalesDirectorAccountIds" resultType="Long">
        SELECT
        t1.account_id
        FROM
        `jeecg-boot`.ctop_user_allocation t1
        LEFT JOIN `jeecg-boot`.user_company t2 ON t1.user_id = t2.user_id
        WHERE
        t1.account_status = 0
        AND t2.company_id = #{companyId}
        AND project_id IN (
        SELECT DISTINCT
        t1.project_id AS projectId
        FROM
        `jeecg-boot`.ctop_project_member t1
        LEFT JOIN `jeecg-boot`.ctop_project t2 ON t1.project_id = t2.id
        WHERE t1.user_id IN
        <foreach collection="userIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        <if test='mediaId ==2'>
            AND (t2.media_id = 2 or t2.media_id = 4)
        </if>
        <if test='mediaId ==1'>
            AND (t2.media_id = 1 or t2.media_id = 3)
        </if>
        AND t1.project_name IS NOT NULL
        AND t1.project_name != ''
        )
        GROUP BY
        t1.account_id
    </select>

    <select id="querySalesDirectorProjectIds" resultType="Long">
        SELECT DISTINCT
        t1.project_id
        FROM
        `jeecg-boot`.ctop_project_member t1
        LEFT JOIN `jeecg-boot`.ctop_project t2 ON t1.project_id = t2.id
        WHERE t2.company_id =#{companyId}
        <if test='userIds !=null and userIds.size() > 0'>
            AND t1.user_id IN
            <foreach collection="userIds" item="id" separator=","
                     open="(" close=")">
                #{id}
            </foreach>
        </if>
        <if test='mediaId ==2'>
            AND (t2.media_id = 2 or t2.media_id = 4)
        </if>
        <if test='mediaId ==1'>
            AND (t2.media_id = 1 or t2.media_id = 3)
        </if>
        AND t1.project_name IS NOT NULL
        AND t1.project_name != ''
    </select>


    <select id="queryKsHourList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT hour,sum(charge) as 'charge'
        FROM application.kuaishou_account_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_date = DATE_FORMAT(#{statDay}, '%Y%m%d' )
        GROUP BY hour
        ORDER BY hour
    </select>

    <select id="queryTtHourList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT hour,sum(cost) as 'charge'
        FROM application.bytedance_advertiser_report_hourly_dw
        WHERE account_id in
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and stat_datetime = DATE_FORMAT(#{statDay}, '%Y%m%d' )
        GROUP BY hour
        ORDER BY hour
    </select>


    <select id="queryTtManydaysChargeAndRoi" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL( this.charge, 0.00) AS 'charge',
        CONCAT( CAST( IFNULL( ROUND(( this.charge - last.charge ) / last.charge* 100,2),0) AS CHAR),'%') AS
        'chargeRoi'
        FROM
        (
        SELECT
        IFNULL( sum(cost), 0.00 ) AS 'charge'
        FROM
        application.bytedance_advertiser_report_daily_dw
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND stat_datetime &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        ) this
        LEFT JOIN (
        SELECT
        IFNULL( sum(cost), 0.00 ) AS 'charge'
        FROM
        application.bytedance_advertiser_report_daily_dw
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT( #{lastTimeStart}, '%Y%m%d' )
        AND stat_datetime &lt;= DATE_FORMAT( #{lastTimeEnd}, '%Y%m%d' )
        ) last ON 1=1
    </select>


    <select id="queryKsManydaysChargeAndRoi" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL( this.charge, 0.00) AS 'charge',
        CONCAT( CAST( IFNULL( ROUND(( this.charge - last.charge ) / last.charge* 100,2),0) AS CHAR),'%') AS
        'chargeRoi'
        FROM
        (
        SELECT
        IFNULL( sum(charge), 0.00 ) AS 'charge'
        FROM
        application.kuaishou_account_report_daily_dw
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        ) this
        LEFT JOIN (
        SELECT
        IFNULL( sum(charge), 0.00 ) AS 'charge'
        FROM
        application.kuaishou_account_report_daily_dw
        WHERE
        account_id IN
        <foreach collection="accountIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT( #{lastTimeStart}, '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT( #{lastTimeEnd}, '%Y%m%d' )
        ) last ON 1=1
    </select>


    <select id="queryKsHourProjectTopTotal" resultType="Long">
        SELECT count(1)
        FROM
        (select 1 from application.kuaishou_account_report_hourly_dw t1
        where t1.project_id in
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and t1.stat_date >= DATE_FORMAT(#{startTime}, '%Y%m%d')
        and t1.stat_date &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d')
        GROUP BY project_id
        ) t
    </select>

    <select id="queryTtHourProjectTopTotal" resultType="Long">
        SELECT count(1)
        FROM
        (select 1 from application.bytedance_advertiser_report_hourly_dw t1
        where t1.project_id in
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        and t1.stat_datetime >= DATE_FORMAT(#{startTime}, '%Y%m%d')
        and t1.stat_datetime &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d')
        GROUP BY project_id
        ) t
    </select>

    <select id="queryKsHourProjectTop" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.project_id as 'projectId',
        t1.project_name as 'projectName',
        ( SELECT realname FROM `jeecg-boot`.sys_user us WHERE us.id = t1.sale_id ) AS 'saleName',
        SUM( t1.charge ) AS 'charge',
        CONCAT( CAST( IFNULL( ROUND(( t1.charge - t3.charge ) / t3.charge * 100, 2 ), 0.00 ) AS CHAR ), '%' ) AS
        'chargeRoi',
        IFNULL(t2.realCharge,0.00) AS 'realCharge',
        IFNULL(t2.jlCharge,0.00) AS 'jlCharge'
        FROM
        application.kuaishou_account_report_hourly_dw t1
        LEFT JOIN(
        SELECT project_id,
        SUM(real_charged_in_yuan) as 'realCharge',
        SUM(direct_rebate_real_charged_in_yuan) as 'jlCharge'
        FROM application.app_kuaishou_agent_report_d
        WHERE
        project_id IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        GROUP BY project_id
        )t2 ON t1.project_id = t2.project_id
        LEFT JOIN (
        SELECT project_id, SUM( charge ) AS 'charge' FROM application.kuaishou_account_report_hourly_dw WHERE project_id
        IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT(DATE_ADD(#{startTime}, INTERVAL - ${days} DAY ), '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT(DATE_ADD(#{endTime}, INTERVAL - ${days} DAY ), '%Y%m%d' )
        GROUP BY project_id ) t3 ON
        t1.project_id = t3.project_id
        WHERE
        t1.project_id IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_date >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND stat_date &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        GROUP BY
        t1.project_id
        ORDER BY
        SUM( t1.charge ) DESC

    </select>


    <select id="queryTtHourProjectTop" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.project_id as 'projectId',
        t1.project_name as 'projectName',
        ( SELECT realname FROM `jeecg-boot`.sys_user us WHERE us.id = t1.sale_id ) AS 'saleName',
        SUM( t1.cost ) AS 'charge',
        CONCAT( CAST( IFNULL( ROUND(( t1.cost - t3.charge ) / t3.charge * 100, 2 ), 0 ) AS CHAR ), '%' ) AS
        'chargeRoi',
        IFNULL(t2.realCharge,0.00) AS 'realCharge',
        IFNULL(t2.jlCharge,0.00) AS 'jlCharge'
        FROM
        application.bytedance_advertiser_report_hourly_dw t1
        LEFT JOIN(
        SELECT project_id,
        SUM(cash_cost) as 'realCharge',
        SUM(grants_cost) AS 'jlCharge'
        FROM application.app_bytedance_agent_report_d
        WHERE
        project_id IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND stat_datetime &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        GROUP BY project_id
        )t2 ON t1.project_id = t2.project_id
        LEFT JOIN (
        SELECT project_id, SUM( cost ) AS 'charge' FROM application.bytedance_advertiser_report_hourly_dw WHERE
        project_id
        IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND stat_datetime >= DATE_FORMAT(DATE_ADD(#{startTime}, INTERVAL - ${days} DAY ), '%Y%m%d' )
        AND stat_datetime &lt;= DATE_FORMAT(DATE_ADD(#{endTime}, INTERVAL - ${days} DAY ), '%Y%m%d' )
        GROUP BY project_id ) t3 ON
        t1.project_id = t3.project_id
        WHERE
        t1.project_id IN
        <foreach collection="projectIds" item="id" separator=","
                 open="(" close=")">
            #{id}
        </foreach>
        AND t1.stat_datetime >= DATE_FORMAT(#{startTime}, '%Y%m%d' )
        AND t1.stat_datetime &lt;= DATE_FORMAT(#{endTime}, '%Y%m%d' )
        GROUP BY
        t1.project_id
        ORDER BY
        SUM( t1.cost ) DESC

    </select>


</mapper>