<?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.kuaishou.modules.report.mapper.KsNewPlanReportMapper">

    <select id="queryKsNewPlanGroupByAccount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        u.project_name as projectName,
        u.auth_name as authName,
        u.user_name as userName,
        t.account_id accountId,
        COUNT(t.campaign_id) new,
        COUNT(IF(t.charge > 500, TRUE, NULL)) valid,
        max(t.put_create_time) maxCreateTime
        FROM
        (
        SELECT
        t1.account_id,
        t1.campaign_id,
        t1.put_create_time,
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) AS statDate,
        (
        SELECT
        ifnull(sum(charge), 0)
        FROM
        ctop_kuaishou_report_daily_campaign t2
        WHERE
        t2.campaign_id = t1.campaign_id
        AND stat_date >= DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        )
        AND stat_date &lt; DATE_ADD(
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ),
        INTERVAL '7' DAY
        )
        ) AS charge
        FROM
        ctop_kuaishou_campaign t1
        WHERE
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) >= #{startDate}
        AND DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        account_id,
        campaign_id
        ) t LEFT JOIN ctop_user_allocation u on t.account_id=u.account_id
        GROUP BY
        t.account_id
        ORDER BY ${target} ${order}
    </select>

    <select id="queryKsNewPlanGroupByMonth" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        DATE_FORMAT(t.statDate, '%Y-%m') statDate,
        COUNT(t.campaign_id) new,
        COUNT(IF(t.charge > 500, TRUE, NULL)) valid
        FROM
        (
        SELECT
        t1.account_id,
        t1.campaign_id,
        t1.put_create_time,
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) AS statDate,
        (
        SELECT
        ifnull(sum(charge), 0)
        FROM
        ctop_kuaishou_report_daily_campaign t2
        WHERE
        t2.campaign_id = t1.campaign_id
        AND stat_date >= DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        )
        AND stat_date &lt; DATE_ADD(
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ),
        INTERVAL '7' DAY
        )
        ) AS charge
        FROM
        ctop_kuaishou_campaign t1
        WHERE
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) >= #{startDate}
        AND DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        account_id,
        campaign_id
        ) t
        GROUP BY DATE_FORMAT(t.statDate, '%Y-%m')
        ORDER BY statDate
    </select>

    <select id="queryKsNewPlanGroupByStatDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t.statDate,
        COUNT(t.campaign_id) new,
        COUNT(IF(t.charge > 500, TRUE, NULL)) valid
        FROM
        (
        SELECT
        t1.account_id,
        t1.campaign_id,
        t1.put_create_time,
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) AS statDate,
        (
        SELECT
        ifnull(sum(charge), 0)
        FROM
        ctop_kuaishou_report_daily_campaign t2
        WHERE
        t2.campaign_id = t1.campaign_id
        AND stat_date >= DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        )
        AND stat_date &lt; DATE_ADD(
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ),
        INTERVAL '7' DAY
        )
        ) AS charge
        FROM
        ctop_kuaishou_campaign t1
        WHERE
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) >= #{startDate}
        AND DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        account_id,
        campaign_id
        ) t
        GROUP BY
        statDate
        ORDER BY statDate
    </select>

    <select id="querySumNewPlanBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        COUNT(t.campaign_id) new,
        COUNT(IF(t.charge > 500, TRUE, NULL)) valid
        FROM
        (
        SELECT
        t1.account_id,
        t1.campaign_id,
        t1.put_create_time,
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) AS statDate,
        (
        SELECT
        ifnull(sum(charge), 0)
        FROM
        ctop_kuaishou_report_daily_campaign t2
        WHERE
        t2.campaign_id = t1.campaign_id
        AND stat_date >= DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        )
        AND stat_date &lt; DATE_ADD(
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ),
        INTERVAL '7' DAY
        )
        ) AS charge
        FROM
        ctop_kuaishou_campaign t1
        WHERE
        DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) >= #{startDate}
        AND DATE_FORMAT(
        t1.put_create_time,
        '%Y-%m-%d'
        ) &lt;= #{endDate}
        <if test="accountIds != null">
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        account_id,
        campaign_id
        ) t
    </select>

    <select id="queryAllOperator"  resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            DISTINCT user_id,user_name
        FROM
            ctop_project_member
        WHERE
                project_id IN (
                SELECT
                    id
                FROM
                    ctop_project
                WHERE
                    media_id = 2
            )
          and
                role_code IN (
                              'operator',
                              'kuaishouOperationManager',
                              'operationAssistant'
                )
    </select>

    <select id="queryAccountIdsByUserId" resultType="java.lang.Long">
        SELECT
            account_id
        FROM
            ctop_user_allocation
        WHERE
            user_id = #{userId}
        and account_status=0
    </select>

    <select id="queryAccountIdsByMedia" resultType="java.lang.Long">
        SELECT
            account_id
        FROM
            ctop_user_allocation
        WHERE
            media_id = 2
          and account_status=0
    </select>

</mapper>