<?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.hzs.member.statis.mapper.CuBonusVertexStatisMapper">

    <!--    奖金拨比列表-->
    <select id="queryCuBonusStatis" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisVO">
        SELECT
        cm.MEMBER_CODE as memberCode,
        cm.MEMBER_NAME as memberName,
        cbs.PK_ID pkId,
        cbs.PERIOD period,
        cbs.CREATION_TIME creationTime,
        cbs.PK_COUNTRY pkCountry,
        cbs.MAIN_REG_AMOUNT mainRegAmount,
        cbs.MAIN_UP_AMOUNT mainUpAmount,
        cbs.MAIN_REPUR_AMOUNT mainRepurAmount,
        cbs.RETAIL_CONSUME_AMOUNT retailConsumeAmount,
        ( nvl(cbs.MAIN_REG_AMOUNT,0)+nvl(cbs.MAIN_UP_AMOUNT,0)) firstPurchaseAll,
        ( nvl(cbs.MAIN_REG_AMOUNT,0)+nvl(cbs.MAIN_UP_AMOUNT,0)+nvl(cbs.MAIN_REPUR_AMOUNT,0)+nvl(cbs.RETAIL_CONSUME_AMOUNT,0)) totalAmount,

        #{rate}*cbs.retail_range_income retailRangeIncome,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_range_income /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailRangeIncomeRate,

        #{rate}*cbs.retail_same_level_income_V1V4 retailSameLevelIncomeV1V4,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_same_level_income_V1V4 /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailSameLevelIncomeV1V4Rate,

        #{rate}*cbs.retail_same_level_income_V5 retailSameLevelIncomeV5,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_same_level_income_V5 /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailSameLevelIncomeV5Rate,

        #{rate}*cbs.retail_area_income retailAreaIncome,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_area_income /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailAreaIncomeRate,

        #{rate}*cbs.retail_benefit_avg_income retailBenefitAvgIncome,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_benefit_avg_income /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailBenefitAvgIncomeRate,

        #{rate}*cbs.retail_benefit_income retailBenefitIncome,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_benefit_income /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailBenefitIncomeRate,

        #{rate}*cbs.retail_benefit_range_income retailBenefitRangeIncome,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.retail_benefit_range_income /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailBenefitRangeIncomeRate,

        #{rate}*cbs.BACK_POINTS backPoints,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND (#{perCent}*cbs.BACK_POINTS /
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as backPointsRate,


        #{rate}*(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0)) totalBonus,


        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0))/
        (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as totalBonusRate
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX AND cm.DEL_FLAG = 0
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY cbs.CREATION_TIME asc
    </select>

    <!--    查询奖金拨比PV列表-->
    <select id="queryCuBonusPvStatis" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisPvVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisPvVO">
        SELECT
        cm.MEMBER_CODE as memberCode,
        cm.MEMBER_NAME as memberName,
        cbs.PK_ID pkId,
        cbs.PERIOD period,
        cbs.CREATION_TIME creationTime,
        cbs.PK_COUNTRY pkCountry,
        #{rate}*nvl(cbs.MAIN_REG_PV,0) mainRegPv,
        #{rate}*nvl(cbs.MAIN_UP_PV,0) mainUpPv,
        #{rate}*nvl(cbs.MAIN_REPUR_PV,0) mainRepurPv,
        #{rate}*nvl(cbs.RETAIL_CONSUME_PV,0) retailConsumePv,
        #{rate}*( nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)) firstPurchaseAll,
        #{rate}*( nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)) totalAchieve,

        #{rate}*cbs.retail_range_income retailRangeIncome,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_range_income /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailRangeIncomeRate,

        #{rate}*cbs.retail_same_level_income_V1V4 retailSameLevelIncomeV1V4,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_same_level_income_V1V4 /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailSameLevelIncomeV1V4Rate,

        #{rate}*cbs.retail_same_level_income_V5 retailSameLevelIncomeV5,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_same_level_income_V5 /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailSameLevelIncomeV5Rate,

        #{rate}*cbs.retail_area_income retailAreaIncome,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_area_income /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailAreaIncomeRate,

        #{rate}*cbs.retail_benefit_avg_income retailBenefitAvgIncome,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_benefit_avg_income /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailBenefitAvgIncomeRate,

        #{rate}*cbs.retail_benefit_income retailBenefitIncome,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_benefit_income /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailBenefitIncomeRate,

        #{rate}*cbs.retail_benefit_range_income retailBenefitRangeIncome,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.retail_benefit_range_income /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as retailBenefitRangeIncomeRate,

        #{rate}*cbs.BACK_POINTS backPoints,
        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND (100*cbs.BACK_POINTS /
        (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as backPointsRate,


        (nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0)) totalBonus,


        CASE WHEN (nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0))=0 THEN 0 ELSE
        ROUND(100*(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0))/
        ( nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)),4) end as totalBonusRate
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX AND cm.DEL_FLAG = 0
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY cbs.CREATION_TIME asc
    </select>

    <!--    奖金拨比列表统计小计（最后一行）-->
    <select id="queryCuBonusSumStatis" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisVO">
        SELECT
        sum(cbs.MAIN_REG_AMOUNT) mainRegAmountSum,
        sum(cbs.MAIN_UP_AMOUNT) mainUpAmountSum,
        sum( nvl(cbs.MAIN_REG_AMOUNT,0)+nvl(cbs.MAIN_UP_AMOUNT,0)) firstPurchaseAllSum,
        sum( nvl(cbs.MAIN_REG_AMOUNT,0)+nvl(cbs.MAIN_UP_AMOUNT,0)+nvl(cbs.MAIN_REPUR_AMOUNT,0)+nvl(cbs.RETAIL_CONSUME_AMOUNT,0)) totalAmountSum,
        sum(cbs.MAIN_REPUR_AMOUNT) mainRepurAmountSum,
        sum(cbs.RETAIL_CONSUME_AMOUNT) retailConsumeAmountSum,

        sum(#{rate}*cbs.retail_range_income) retailRangeIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_range_income) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as retailRangeIncomeRateSum,

        sum(#{rate}*cbs.retail_same_level_income_V1V4) retailSameLevelIncomeV1V4Sum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_same_level_income_V1V4) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as sameLevelIncomeV1V4RateSum,

        sum(#{rate}*cbs.retail_same_level_income_V5) retailSameLevelIncomeV5Sum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_same_level_income_V5) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as sameLevelIncomeV5RateSum,

        sum(#{rate}*cbs.retail_area_income) retailAreaIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_area_income) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as retailAreaIncomeRateSum,

        sum(#{rate}*cbs.retail_benefit_avg_income) retailBenefitAvgIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_benefit_avg_income) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as retailBenefitAvgIncomeRateSum,

        sum(#{rate}*cbs.retail_benefit_income) retailBenefitIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_benefit_income) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as retailBenefitIncomeRateSum,

        sum(#{rate}*cbs.retail_benefit_range_income) retailBenefitRangeIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.retail_benefit_range_income) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as benefitRangeIncomeRateSum,

        sum(#{rate}*cbs.BACK_POINTS) backPointsSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND (#{perCent}*sum(cbs.BACK_POINTS) /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as backPointsRateSum,


        sum(#{rate}*(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0))) totalBonusSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT))=0 THEN 0 ELSE
        ROUND(#{perCent}*sum((nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0)))/
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT)),4) end as totalBonusRateSum
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>

        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>

    </select>
    <!--    奖金拨比列表统计小计(PV)（最后一行）-->
    <select id="queryCuBonusPvSumStatis" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisPvVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisPvVO">
        SELECT
        #{rate}* sum(cbs.MAIN_REG_PV) mainRegPvSum,
        #{rate}* sum(cbs.MAIN_UP_PV) mainUpPvSum,
        #{rate}* sum(cbs.MAIN_REPUR_PV) mainRepurPvSum,
        #{rate}* sum(cbs.RETAIL_CONSUME_PV) retailConsumePvSum,
        #{rate}* sum( nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)) firstPurchaseAllSum,
        #{rate}* sum( nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)) totalAchieveSum,

        #{rate}* sum(cbs.retail_range_income) retailRangeIncomeSum,
        CASE WHEN (sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV))=0 THEN 0 ELSE
        ROUND (100* sum(cbs.retail_range_income) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as retailRangeIncomeRateSum,

        #{rate}* sum(cbs.retail_same_level_income_V1V4) retailSameLevelIncomeV1V4Sum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100* sum(cbs.retail_same_level_income_V1V4 )/
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as sameLevelIncomeV1V4RateSum,

        #{rate}* sum(cbs.retail_same_level_income_V5) retailSameLevelIncomeV5Sum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100* sum(cbs.retail_same_level_income_V5) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as sameLevelIncomeV5RateSum,

        #{rate}* sum(cbs.retail_area_income) retailAreaIncomeSum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100*sum(cbs.retail_area_income) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as retailAreaIncomeRateSum,

        #{rate}*sum(cbs.retail_benefit_avg_income) retailBenefitAvgIncomeSum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100*sum(cbs.retail_benefit_avg_income) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as retailBenefitAvgIncomeRateSum,

        #{rate}*sum(cbs.retail_benefit_income) retailBenefitIncomeSum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100*sum(cbs.retail_benefit_income) /
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT+cbs.RETAIL_CONSUME_AMOUNT),4) end as retailBenefitIncomeRateSum,

        #{rate}*sum(cbs.retail_benefit_range_income) retailBenefitRangeIncomeSum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100*sum(cbs.retail_benefit_range_income) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as benefitRangeIncomeRateSum,

        #{rate}*sum(cbs.BACK_POINTS) backPointsSum,
        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND (100*sum(cbs.BACK_POINTS) /
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as backPointsRateSum,

        #{rate}* sum(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0)) totalBonusSum,

        CASE WHEN (sum(nvl(cbs.MAIN_REG_PV,0)+nvl(cbs.MAIN_UP_PV,0)+nvl(cbs.MAIN_REPUR_PV,0)+nvl(cbs.RETAIL_CONSUME_PV,0)))=0 THEN 0 ELSE
        ROUND(100*sum(nvl(cbs.retail_range_income,0)+nvl(cbs.retail_same_level_income_V1V4,0)+nvl(cbs.retail_same_level_income_V5,0)
        +nvl(cbs.retail_area_income,0)+nvl(cbs.retail_benefit_avg_income,0)
        +nvl(cbs.retail_benefit_income,0)+nvl(cbs.retail_benefit_range_income,0)+nvl(cbs.BACK_POINTS,0))/
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.MAIN_REPUR_PV+cbs.RETAIL_CONSUME_PV),4) end as totalBonusRateSum
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX AND cm.DEL_FLAG = 0
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
    </select>

    <!--    查询顶点奖金拨比汇总-->
    <select id="queryCuBonusStatisTotal" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO">
        select * from(
        SELECT
        cbs.PK_BD_VERTEX pkVertex,
        cm.MEMBER_CODE as memberCode,
        cm.MEMBER_NAME as memberName,
        sum (cbs.MAIN_REG_AMOUNT) mainRegAmount,
        sum (cbs.MAIN_UP_AMOUNT) mainUpAmount,
        sum (cbs.MAIN_REPUR_AMOUNT) mainRepurAmount,
        sum (cbs.HIFANS_REG_AMOUNT) hifansRegAmount,
        sum (cbs.INTRODUCE_BONUS) introduceBonus,
        sum (cbs.ORG_BONUS) orgBonus,
        sum (cbs.LEADER_BONUS) leaderBonus,
        sum (cbs.SERVICE_BONUS) serviceBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.SERVICE_BONUS)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS serviceBonusRate,
        sum (cbs.HI_FUN_INCOME) hiFunIncome,
        sum (cbs.REPUR_RANGE_BONUS) repurRangeBonus,
        sum (cbs.REPUR_ORG_BONUS) repurOrgBonus,
        sum (cbs.MAKER_DIRECT) makerDirect,
        sum (cbs.MAKER_SHARE) makerShare,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS makerRate,
        ((sum(cbs.MAIN_REG_AMOUNT +
        cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT))) AS
        firstPurchaseAll,
        ( sum(cbs.MAIN_REPUR_AMOUNT +
        cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER) ) AS repurchaseAllAmount,
        sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE) AS mainBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS mainBonusRate,

        CASE WHEN
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.SPECIAL_REGISTER_PV+cbs.SPECIAL_UPGRADE_PV+cbs.FICTITIOUS_PV)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /#{rate}/sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.SPECIAL_REGISTER_PV+cbs.SPECIAL_UPGRADE_PV+cbs.FICTITIOUS_PV),4)
        end AS mainBonusPvRate,
        sum(cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME
        +cbs.HI_FUN_INCOME) AS repurBonus,

        CASE WHEN
        sum(cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(((cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME))+cbs.HI_FUN_INCOME)
        /
        sum(cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER),4) end AS repurBonusRate,

        CASE WHEN
        sum(cbs.MAIN_REPUR_PV+cbs.HIFANS_REG_PV+cbs.HAI_FUN_UPGRADE_PV+cbs.CLOUD_COLLEGE_PV+cbs.MALL_PV
        +cbs.EMPOWERMENT_GIFT_PV+cbs.COOPERATE_PV+cbs.SPECIAL_REPURCHASE_PV
        +cbs.MAKER_ORDER_PV+cbs.MAKER_GIFT_PV+cbs.WELFARE_ORDER_PV)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(((cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME))+cbs.HI_FUN_INCOME)
        /#{rate}/
        sum(cbs.MAIN_REPUR_PV+cbs.HIFANS_REG_PV+cbs.HAI_FUN_UPGRADE_PV+cbs.CLOUD_COLLEGE_PV+cbs.MALL_PV
        +cbs.EMPOWERMENT_GIFT_PV+cbs.COOPERATE_PV+cbs.SPECIAL_REPURCHASE_PV
        +cbs.MAKER_ORDER_PV+cbs.MAKER_GIFT_PV+cbs.WELFARE_ORDER_PV),4) end AS repurBonusPvRate,

        sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        cbs.MAKER_DIRECT+cbs.MAKER_SHARE+
        cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME
        +cbs.HI_FUN_INCOME) AS totalBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT+
        cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)=0 THEN 0
        ELSE ROUND ((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE+
        cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME+cbs.HI_FUN_INCOME))
        /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT+
        cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)),4) end AS totalBonusRate
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>

        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        GROUP BY cbs.PK_BD_VERTEX,cm.MEMBER_CODE,cm.MEMBER_NAME
        )
        ORDER BY firstPurchaseAll desc,repurchaseAllAmount desc


    </select>

    <!--    顶点奖金拨比汇总查询总和-->
    <select id="queryCuBonusStatisSum" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO">
        SELECT
        sum (cbs.MAIN_REG_AMOUNT) mainRegAmount,
        sum (cbs.MAIN_UP_AMOUNT) mainUpAmount,
        sum (cbs.MAIN_REPUR_AMOUNT) mainRepurAmount,
        sum (cbs.HIFANS_REG_AMOUNT) hifansRegAmount,
        sum (cbs.INTRODUCE_BONUS) introduceBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.INTRODUCE_BONUS)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS introduceBonusRate,
        sum (cbs.ORG_BONUS) orgBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.ORG_BONUS)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS orgBonusRate,

        sum (cbs.LEADER_BONUS) leaderBonus,
        sum (cbs.SERVICE_BONUS) serviceBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.SERVICE_BONUS)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS serviceBonusRate,
        sum (cbs.HI_FUN_INCOME) hiFunIncome,
        sum (cbs.REPUR_RANGE_BONUS) repurRangeBonus,
        sum (cbs.REPUR_ORG_BONUS) repurOrgBonus,
        sum (cbs.MAKER_DIRECT) makerDirect,
        sum (cbs.MAKER_SHARE) makerShare,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS makerRate,
        ((sum(cbs.MAIN_REG_AMOUNT +
        cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT))) AS
        firstPurchaseAll,
        ( sum(cbs.MAIN_REPUR_AMOUNT +
        cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER) ) AS repurchaseAllAmount,
        sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE) AS mainBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT)),4)
        end AS mainBonusRate,

        CASE WHEN
        sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.SPECIAL_REGISTER_PV+cbs.SPECIAL_UPGRADE_PV+cbs.FICTITIOUS_PV)
        = 0
        THEN 0 ELSE ROUND(((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE)))
        /#{rate}/sum(cbs.MAIN_REG_PV+cbs.MAIN_UP_PV+cbs.SPECIAL_REGISTER_PV+cbs.SPECIAL_UPGRADE_PV+cbs.FICTITIOUS_PV),4)
        end AS mainBonusPvRate,

        sum(cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME
        +cbs.HI_FUN_INCOME) AS repurBonus,

        CASE WHEN
        sum(cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(((cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME))+cbs.HI_FUN_INCOME)
        /
        sum(cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER),4) end AS repurBonusRate,

        CASE WHEN
        sum(cbs.MAIN_REPUR_PV+cbs.HIFANS_REG_PV+cbs.HAI_FUN_UPGRADE_PV+cbs.CLOUD_COLLEGE_PV+cbs.MALL_PV
        +cbs.EMPOWERMENT_GIFT_PV+cbs.COOPERATE_PV+cbs.SPECIAL_REPURCHASE_PV
        +cbs.MAKER_ORDER_PV+cbs.MAKER_GIFT_PV+cbs.WELFARE_ORDER_PV)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(((cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME))+cbs.HI_FUN_INCOME)
        /#{rate}/
        sum(cbs.MAIN_REPUR_PV+cbs.HIFANS_REG_PV+cbs.HAI_FUN_UPGRADE_PV+cbs.CLOUD_COLLEGE_PV+cbs.MALL_PV
        +cbs.EMPOWERMENT_GIFT_PV+cbs.COOPERATE_PV+cbs.SPECIAL_REPURCHASE_PV
        +cbs.MAKER_ORDER_PV+cbs.MAKER_GIFT_PV+cbs.WELFARE_ORDER_PV),4) end AS repurBonusPvRate,

        sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        cbs.MAKER_DIRECT+cbs.MAKER_SHARE+
        cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME
        +cbs.HI_FUN_INCOME) AS totalBonus,
        CASE WHEN
        sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT+
        cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)=0 THEN 0
        ELSE ROUND ((#{perCent}*sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE+
        cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME+cbs.HI_FUN_INCOME))
        /
        (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT+
        cbs.MAIN_REPUR_AMOUNT+cbs.HIFANS_REG_AMOUNT+cbs.HAI_FUN_UPGRADE_AMOUNT+cbs.CLOUD_COLLEGE_AMOUNT+cbs.MALL_AMOUNT
        +cbs.EMPOWERMENT_GIFT_AMOUNT+cbs.COOPERATE_AMOUNT+cbs.SPECIAL_REPURCHASE_AMOUNT
        +cbs.MAKER_ORDER+cbs.MAKER_GIFT_AMOUNT+cbs.WELFARE_ORDER)),4) end AS totalBonusRate
        FROM
        CU_BONUS_VERTEX_STATIS cbs
        LEFT JOIN CU_MEMBER cm ON cm.PK_ID = cbs.PK_BD_VERTEX
        WHERE 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>

        <if test="pkVertex != null ">
            and cbs.PK_BD_VERTEX =#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
    </select>
    <!--    查询时间内奖金拨比顶点会员-->
    <select id="queryCuBonusVertex" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO">
        select cbs.pk_bd_vertex pkVertex
        from cu_bonus_vertex_statis cbs where 1=1
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkVertex != null">
            and cbs.PK_BD_VERTEX=#{pkVertex}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        GROUP BY cbs.PK_BD_VERTEX
    </select>

    <!--    7天重算查询订单-->
    <select id="queryAnewSaOrderByLessDay" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisVO">
        select * from (
        select
        cm.pk_Member pkMember,
        #{pkVertex} pkVertex,
        so.PK_COUNTRY pkCountry,
        so.ORDER_TYPE orderType,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from (
        SELECT
        pk_member
        FROM
        ${settleTableName}
        WHERE category = 0
        start with PK_PARENT = #{pkVertex}
        connect by PK_PARENT = prior pk_member
        ) cm
        INNER JOIN SA_ORDER so on so.pk_member=cm.pk_member
        where so.ORDER_TYPE in (41, 42, 43, 44)
        <if test="pkCountry != null">
            and so.pk_country=#{pkCountry}
        </if>
        <if test="delFlag != null">
            AND so.DEL_FLAG=#{delFlag}
        </if>
        <if test="orderStatus != null">
            AND so.order_status=#{orderStatus}
        </if>
        <if test="startDate != null and startDate!='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate!='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="payTimeStr != null and payTimeStr!='' ">
            AND so.PAY_TIME = to_date(#{payTimeStr}, 'YYYY-MM-dd')
        </if>
        )
    </select>

    <!--    删除奖金拨比统计表数据-->
    <delete id="delStatis">
        delete from CU_BONUS_VERTEX_STATIS
        where 1=1
        <if test="pkVertexList!=null and pkVertexList.size> 0 ">
            and PK_BD_VERTEX not in
            <foreach collection="pkVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startDate != null and startDate!='' ">
            and CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate!='' ">
            and CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
    </delete>

    <!--      查询生效国外国家-->
    <select id="countrylist" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisVO">
        select pk_id as pkCountry
        from BD_COUNTRY
        where pk_id!=1 and del_flag = 0 and EFFECTIVE_DATE &lt;= sysdate
    </select>

    <!-- 查询7天重算奖金查询-->
    <select id="queryVertexChinaCountryBonus" parameterType="com.hzs.member.statis.vo.CuBonusVertexStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusVertexStatisVO">
        select
        #{pkCountry} pkCountry,
        sum(cmb.retail_range_income) retailRangeIncome,

        <!--
        (select
        sum(cmb.retail_same_level_income) retailSameLevelIncomeV1V4
        from CU_MEMBER_BONUS cmb
        INNER join (
        SELECT pk_member
        FROM (SELECT PK_MEMBER, cm.Pk_Grade
        FROM ${settleTableName} CM
        where cm.category = 0
        START WITH CM.PK_MEMBER = #{pkVertex}
        CONNECT BY CM.PK_PARENT = PRIOR CM.PK_MEMBER) t0, BD_GRADE BG
        where BG.PK_ID = t0.Pk_Grade
        AND BG.GRADE_VALUE IN (30, 40, 50, 60)
        ) cm
        on cm.pk_member = cmb.pk_member
        INNER JOIN CU_MEMBER_SETTLE_PERIOD cmsp ON cmsp.pk_id=cmb.period
        where CMB.DEL_FLAG = 0
        AND CMSP.Del_Flag = 0
        <if test="startDate!=null and startDate!='' ">
            and cmsp.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ) retailSameLevelIncomeV1V4,

        (select
        sum(cmb.retail_same_level_income) retailSameLevelIncomeV5
        from CU_MEMBER_BONUS cmb
        INNER join (SELECT pk_member
        FROM (SELECT PK_MEMBER, cm.Pk_Grade
        FROM ${settleTableName} CM
        where cm.category = 0
        START WITH CM.PK_MEMBER = #{pkVertex}
        CONNECT BY CM.PK_PARENT = PRIOR CM.PK_MEMBER) t0, BD_GRADE BG
        where BG.PK_ID = t0.Pk_Grade
        AND BG.GRADE_VALUE IN (70) ) cm
        on cm.pk_member = cmb.pk_member
        INNER JOIN CU_MEMBER_SETTLE_PERIOD cmsp ON cmsp.pk_id=cmb.period
        where CMB.DEL_FLAG = 0
        AND CMSP.Del_Flag = 0
        <if test="startDate!=null and startDate!='' ">
            and cmsp.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ) retailSameLevelIncomeV5,
        -->

        (select
        sum(cmb.retail_same_level_income) retailSameLevelIncomeV5
        from CU_MEMBER_BONUS cmb
        inner join (SELECT pk_member
        FROM (SELECT PK_MEMBER, cm.Pk_Grade
        FROM ${settleTableName} CM
        where cm.category = 0
        START WITH CM.PK_MEMBER = #{pkVertex}
        CONNECT BY CM.PK_PARENT = PRIOR CM.PK_MEMBER) t0) cm
        on cm.pk_member = cmb.pk_member
        INNER JOIN CU_MEMBER_SETTLE_PERIOD cmsp ON cmsp.pk_id=cmb.period
        where CMB.DEL_FLAG = 0
        AND CMSP.Del_Flag = 0
        <if test="startDate!=null and startDate!='' ">
            and cmsp.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ) retailSameLevelIncomeV5,

        sum(cmb.retail_area_income) retailAreaIncome,
        sum(cmb.retail_benefit_range_income) retailBenefitRangeIncome,
        sum(cmb.retail_benefit_avg_income) retailBenefitAvgIncome,
        sum(cmb.retail_benefit_income) retailBenefitIncome,
        sum(cmb.BACK_POINTS) backPoints,
        sum(cmb.real_income_total) realIncomeTotal,
        #{pkVertex} pkVertex,
        to_date(to_char(cmsp.settle_date, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS cmb
        INNER join (SELECT
        pk_member
        FROM
        ${settleTableName}
        WHERE category=0
        start with pk_member = #{pkVertex}
        connect by PK_PARENT = prior pk_member ) cm
        on cm.pk_member = cmb.pk_member
        INNER JOIN CU_MEMBER_SETTLE_PERIOD cmsp ON cmsp.pk_id=cmb.period
        where CMB.DEL_FLAG=0
        AND CMSP.Del_Flag=0
        <if test="startDate!=null and startDate!='' ">
            and cmsp.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        GROUP BY
        to_char(cmsp.settle_date, 'yyyy-mm-dd')

    </select>

    <!--   查询当天团队统计数据-->
    <select id="getCuBonusStatis" parameterType="com.hzs.common.domain.member.statis.CuBonusVertexStatis"
            resultType="com.hzs.common.domain.member.statis.CuBonusVertexStatis">
        SELECT *
        FROM
        CU_BONUS_VERTEX_STATIS a
        WHERE
        1=1
        <if test="pkBdVertex != null ">
            AND a.PK_BD_VERTEX = #{pkBdVertex}
        </if>
        <if test="pkCountry != null ">
            AND a.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="creationTime != null ">
            and to_char(a.CREATION_TIME, 'yyyy-mm-dd') = to_char(#{creationTime}, 'yyyy-mm-dd')
        </if>
    </select>

</mapper>
