<?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.performanceappraisal.mapper.PerformanceMapper">
    <resultMap id="OptimizerTotalCostDetail" type="cn.com.ctop.performanceappraisal.vo.OptimizerCostDetailVO">
        <result column="realName" property="realName"/>
        <result column="totalCost" property="totalCost"/>
        <result column="year" property="year"/>
        <result column="quarter" property="quarter"/>
        <result column="userId" property="userId"/>
        <result column="appType" property="appType"/>
        <result column="projectId" property="projectId"/>
        <result column="projectName" property="projectName"/>
        <result column="accountName" property="accountName"/>
    </resultMap>
    <select id="getKuaishouOptimizerTotalCostDetailGroupByUserId" resultMap="OptimizerTotalCostDetail">
        SELECT
        sysUser.id as userId,sysUser.realname as realName,SUM(charge) as totalCost,#{year} AS year,#{quarter} as
        quarter,2 as appType
        FROM
        ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        <!-- userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771' -->
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt;= #{endDate}
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        GROUP BY
        sysUser.id
        ORDER BY
        SUM(charge) DESC
        <if test="startIndex != null and startIndex != '' and pageSize !=null and pageSize!=''">
        limit #{startIndex},#{pageSize}
        </if>
    </select>

    <select id="getBytedanceOptimizerTotalCostDetailGroupByUserId" resultMap="OptimizerTotalCostDetail">
        SELECT
        sysUser.id as userId,sysUser.realname as realName,SUM(dailiAccount.cost) as totalCost,
        #{year} AS year,#{quarter} as quarter,1 as appType
        FROM
        ctop_bytedance_report_advertiser_daily dailyAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailyAccount.advertiser_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        <!-- userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771' -->
        dailyAccount.stat_datetime &gt;= #{startDate}
        AND
        dailyAccount.stat_datetime &lt; #{endDate}
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        GROUP BY
        sysUser.id
        ORDER BY
        SUM(dailyAccount.cost) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getOptimizerTotalCostDetailGroupByAccountId" resultMap="OptimizerTotalCostDetail">
        SELECT
        dailiAccount.account_id,sysUser.id as userId,sysUser.realname as realName,SUM(charge) as totalCost,#{year} AS
        year,#{quarter} as quarter,2 as appType
        FROM
        ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        AND
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt; #{endDate}
        GROUP BY
        dailiAccount.account_id
        ORDER BY
        SUM(charge) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getKuaishouUserOptimizerTotalCostGroupByProjectId" resultMap="OptimizerTotalCostDetail">
        SELECT
        allocation.project_id as projectId,allocation.project_name as projectName,
        allocation.auth_name as accountName,sysUser.id as userId,sysUser.realname as realName,
        SUM(charge) as totalCost,#{year} AS year,#{quarter} as quarter,2 as appType
        FROM
        ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        <if test="projectId != null">
            and allocation.project_id = #{projectId}
        </if>
        AND
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt; #{endDate}
        GROUP BY
        allocation.project_id
        ORDER BY
        SUM(charge) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getBytedanceUserOptimizerTotalCostGroupByProjectId" resultMap="OptimizerTotalCostDetail">
        SELECT
        allocation.project_id as projectId,allocation.project_name as projectName,
        allocation.auth_name as accountName,sysUser.id as userId,sysUser.realname as realName,
        SUM(dailyAccount.cost) as totalCost,#{year} AS year,#{quarter} as quarter,1 as appType
        FROM
        ctop_bytedance_report_advertiser_daily dailyAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailyAccount.advertiser_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        <if test="projectId != null">
            and allocation.project_id = #{projectId}
        </if>
        AND
        dailyAccount.stat_datetime &gt;= #{startDate}
        AND
        dailyAccount.stat_datetime &lt; #{endDate}
        GROUP BY
        allocation.project_id
        ORDER BY
        SUM(dailyAccount.cost) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getKuaishouUserOptimizerTotalCostGroupByAccountId" resultMap="OptimizerTotalCostDetail">
        SELECT
        allocation.project_id as projectId,allocation.project_name as projectName,allocation.auth_name as accountName
        ,sysUser.id as
        userId,sysUser.realname as realName,SUM(charge) as totalCost,#{year} AS year,#{quarter} as quarter,2 as appType
        FROM
        ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        1=1
        and
        userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        <if test="projectId != null">
            and allocation.project_id = #{projectId}
        </if>
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        AND
        dailiAccount.stat_date &gt;= #{startDate}
        AND
        dailiAccount.stat_date &lt; #{endDate}
        GROUP BY
        allocation.account_id
        ORDER BY
        SUM(charge) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getBytedanceUserOptimizerTotalCostGroupByAccountId" resultMap="OptimizerTotalCostDetail">
        SELECT
        allocation.project_id as projectId,allocation.project_name as projectName,
        allocation.auth_name as accountName,sysUser.id as userId,sysUser.realname as realName,
        SUM(dailyAccount.cost) as totalCost,#{year} AS year,#{quarter} as quarter,1 as appType
        FROM
        ctop_bytedance_report_advertiser_daily dailyAccount
        LEFT JOIN
        ctop_user_allocation allocation ON dailyAccount.advertiser_id = allocation.account_id
        LEFT JOIN
        sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
        sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
        1=1
        and
        userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        <if test="projectId != null">
            and allocation.project_id = #{projectId}
        </if>
        <if test="userId != null">
            and sysUser.id = #{userId}
        </if>
        AND
        dailyAccount.stat_datetime &gt;= #{startDate}
        AND
        dailyAccount.stat_datetime &lt; #{endDate}
        GROUP BY
        allocation.account_id
        ORDER BY
        SUM(dailyAccount.cost) DESC
        limit #{startIndex},#{pageSize}
    </select>

    <select id="getKuaishouOptimizerTotalCost" resultMap="OptimizerTotalCostDetail">
        SELECT
            '账户总计' as realname,SUM(charge) as totalCost,#{year} AS year,#{quarter} as quarter,2 as appType
        FROM
            ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
            ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
            sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
            sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
            userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        AND
            dailiAccount.stat_date &gt;= #{startDate}
        AND
            dailiAccount.stat_date &lt; #{endDate}
        GROUP BY userRole.role_id
    </select>

    <select id="getBytedanceOptimizerTotalCost" resultMap="OptimizerTotalCostDetail">
        SELECT
            '账户总计' as realname,SUM(dailyAccount.cost) as totalCost,#{year} AS year,#{quarter} as quarter,1 as appType
        FROM
            ctop_bytedance_report_advertiser_daily dailyAccount
        LEFT JOIN
            ctop_user_allocation allocation ON dailyAccount.advertiser_id = allocation.account_id
        LEFT JOIN
            sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
            sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
            userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
        AND
            dailyAccount.stat_datetime &gt;= #{startDate}
        AND
            dailyAccount.stat_datetime &lt; #{endDate}
        GROUP BY userRole.role_id
    </select>

    <select id="getBytedanceSeasonTotalCost" resultType="java.math.BigDecimal">
        SELECT
            SUM(cost)
        FROM
            ctop_bytedance_report_advertiser_daily
        WHERE
            stat_datetime &gt;= #{startDate}
        AND
            stat_datetime &lt; #{endDate}
    </select>
    <select id="getKuaishouSeasonTotalCost" resultType="java.math.BigDecimal">
        SELECT
            SUM(charge)
        FROM
            ctop_kuaishou_report_daily_account
        WHERE
            stat_date &gt;= #{startDate}
        AND
            stat_date &lt; #{endDate}
    </select>

    <select id="getKuaishouMemberPerformanceByOrgCode" resultMap="OptimizerTotalCostDetail">
        SELECT
            sysUser.id as userId,sysUser.realname as realName,SUM(charge) as totalCost,#{year} AS year,#{quarter} as quarter,2 as appType
        FROM
            ctop_kuaishou_report_daily_account dailiAccount
        LEFT JOIN
            ctop_user_allocation allocation ON dailiAccount.account_id = allocation.account_id
        LEFT JOIN
            sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
            sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
            userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
		AND
			sysUser.org_code = #{orgCode}
        GROUP BY
            sysUser.id
        ORDER BY
            SUM(charge) DESC
    </select>

    <select id="getBytedanceMemberPerformanceByOrgCode" resultMap="OptimizerTotalCostDetail">
        SELECT
            sysUser.id as userId,sysUser.realname as realName,SUM(dailyAccount.cost) as totalCost,#{year} AS year,#{quarter} as quarter,2 as appType
        FROM
            ctop_bytedance_report_advertiser_daily dailyAccount
        LEFT JOIN
            ctop_user_allocation allocation ON dailyAccount.advertiser_id = allocation.account_id
        LEFT JOIN
            sys_user_role userRole ON userRole.user_id = allocation.user_id
        LEFT JOIN
            sys_user sysUser ON sysUser.id = userRole.user_id
        WHERE
            userRole.role_id = '79fba4f45f1b8e284af9c68311c6c771'
		AND
			sysUser.org_code = #{orgCode}
        GROUP BY
            sysUser.id
        ORDER BY
            SUM(dailyAccount.cost) DESC
    </select>
    <select id="getBytedanceSalesSeasonTotalCostByParams" resultType="java.math.BigDecimal">
        SELECT
        SUM(daily.cost)
        FROM
        ctop_bytedance_report_advertiser_daily daily
        LEFT JOIN
        ctop_user_allocation allocation ON daily.advertiser_id = allocation.account_id
        LEFT JOIN
        ctop_advertiser advertiser ON advertiser.id = allocation.advertiser_id
        WHERE
        1=1
        <if test="saleId != null">
            and advertiser.sale_id = #{saleId}
        </if>
        <if test="advertiserId != null">
            and advertiser.id = #{advertiserId}
        </if>
        <if test="accountId != null">
            and allocation.account_id = #{accountId}
        </if>
        AND
        daily.stat_datetime &gt;= #{startDate}
        AND
        daily.stat_datetime &lt; #{endDate}
    </select>
    <select id="getKuaishouSalesSeasonTotalCostByParams" resultType="java.math.BigDecimal">
        SELECT
            SUM(charge)
        FROM
        ctop_kuaishou_report_daily_account daily
        LEFT JOIN
        ctop_user_allocation allocation ON daily.account_id = allocation.account_id
        LEFT JOIN
            ctop_advertiser advertiser ON advertiser.id = allocation.advertiser_id
        WHERE
        1=1
        <if test="saleId != null">
            and advertiser.sale_id = #{saleId}
        </if>
        <if test="advertiserId != null">
            and advertiser.id = #{advertiserId}
        </if>
        <if test="accountId != null">
            and allocation.account_id = #{accountId}
        </if>
        AND
        daily.stat_date &gt;= #{startDate}
        AND
        daily.stat_date &lt; #{endDate}
    </select>
</mapper>
