<?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.CuBonusStatisMapper">

    <!--   查询当天团队统计数据-->
    <select id="getCuBonusStatis" parameterType="com.hzs.common.domain.member.statis.CuBonusStatis"
            resultType="com.hzs.common.domain.member.statis.CuBonusStatis">
        SELECT *
        FROM CU_BONUS_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>

    <!--    奖金拨比列表-->
    <select id="queryCuBonusStatis" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        SELECT
        cbs.PK_BD_VERTEX pkVertex,
        cbs.PK_TEAM_CODE pkTeamCode,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cbs.PK_ID pkId,
        cbs.PERIOD period,
        cbs.CREATION_TIME creationTime,
        cbs.PK_COUNTRY pkCountry,
        <!-- PV字段 -->
        cbs.MAIN_REG_PV mainRegPv,
        cbs.MAIN_UP_PV mainUpPv,
        cbs.MAIN_REPUR_PV mainRepurPv,
        cbs.SPECIAL_REGISTER_PV specialRegisterPv,
        cbs.SPECIAL_UPGRADE_PV specialUpgradePv,
        cbs.SPECIAL_REPURCHASE_PV specialRepurchasePv,
        cbs.FICTITIOUS_PV fictitiousPv,
        cbs.MALL_PV mallPv,
        cbs.COOPERATE_PV cooperatePv,
        cbs.WELFARE_ORDER_PV welfareOrderPv,
        <!-- 金额字段 -->
        cbs.main_reg_amount mainRegAmount,
        cbs.main_up_amount mainUpAmount,
        cbs.main_repur_amount mainRepurAmount,
        cbs.special_register_amount,
        cbs.special_upgrade_amount,
        cbs.special_repurchase_amount,
        <!-- 奖金字段 -->
        cbs.INTRODUCE_BONUS introduceBonus,
        cbs.ORG_BONUS orgBonus,
        cbs.LEADER_BONUS leaderBonus,
        cbs.SERVICE_BONUS serviceBonus,
        cbs.REPUR_RANGE_BONUS repurRangeBonus,
        cbs.REPUR_ORG_BONUS repurOrgBonus,
        cbs.repur_leader_bonus repurLeaderBonus,
        cbs.pur_real_subtotal purRealSubtotal,
        cbs.repur_real_subtotal repurRealSubtotal,
        <!-- 首购金额 = 注册 + 升级 + 特殊注册 + 特殊升级 + 虚拟订单（空单） -->
        (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT)
        AS firstPurchaseAll,
        <!-- 复购金额 = 复购 + 直播 + 合作 + 特殊复购 + 福利订单 -->
        (cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER)
        AS repurchaseAllAmount,
        <!-- 首购拨出金额 = 直推收益 + 拓展收益 + 辅导收益 + 服务补贴  -->
        (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS)
        AS mainBonus,
        <!-- 首购拨出比例 = 首购金额为0，首购拨出比例 = 0；否则，首购拨出比例 = 100 * 首购拨出金额 / 首购金额 -->
        case
        WHEN (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} * (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS))
        / (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT), 4)
        end AS mainBonusRate,
        <!-- 首购PV拨出比例 = 首购PV为0，首购PV拨出比例 = 0；否则，首购PV拨出比例 = 100 * 首购拨出金额 / 汇率 / 首购PV -->
        case
        WHEN (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} * (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS))
        / #{rate} / (cbs.MAIN_REG_PV + cbs.MAIN_UP_PV + cbs.SPECIAL_REGISTER_PV + cbs.SPECIAL_UPGRADE_PV + cbs.FICTITIOUS_PV), 4)
        end AS mainBonusPvRate,
        <!-- 复购拨出金额 = 复购级差收益 + 复购拓展收益 + 复购推荐收益 + 复购辅导收益 -->
        (cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        AS repurBonus,
        <!-- 复购拨出比例 = 复购金额为0，复购拨出比例 = 0；否则，复购拨出比例 = 100 * 复购拨出金额 / 复购金额 -->
        case
        WHEN (cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER) = 0
        THEN 0
        ELSE ROUND(#{perCent} * (cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        / (cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER), 4)
        end AS repurBonusRate,
        <!-- 复购PV拨出比例 = 复购PV为0，复购PV拨出比例 = 0；否则，复购PV拨出比例 = 100 * 首购拨出金额 / 汇率 / 首购PV -->
        case
        WHEN (cbs.MAIN_REPUR_PV + cbs.MALL_PV + cbs.COOPERATE_PV + cbs.SPECIAL_REPURCHASE_PV + cbs.WELFARE_ORDER_PV) = 0
        THEN 0
        ELSE ROUND(#{perCent} * (cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        / #{rate} / (cbs.MAIN_REPUR_PV + cbs.MALL_PV +cbs.COOPERATE_PV + cbs.SPECIAL_REPURCHASE_PV + cbs.WELFARE_ORDER_PV), 4)
        end AS repurBonusPvRate,
        <!-- 总拨出金额 = 直推收益 + 拓展收益 + 辅导收益 + 服务补贴 + 复购级差收益 + 复购拓展收益 + 复购推荐收益 + 复购辅导收益 -->
        (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS
        + cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        AS totalBonus,
        <!-- 总拨出比例 = 首购 + 复购金额为0，总拨出比例 = 0；否则，总拨出比例 = 100 * 首购 + 复购拨出金额 / 首购 + 复购金额 -->
        CASE
        WHEN (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT
        + cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT +cbs.WELFARE_ORDER) = 0
        THEN 0
        ELSE ROUND (#{perCent} * (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS
        + cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        / (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT
        + cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER), 4)
        end AS totalBonusRate
        FROM CU_BONUS_STATIS cbs
        left join bd_vertex ver on cbs.pk_bd_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cbs.pk_team_code
        WHERE 1 = 1
        and ver.DEL_FLAG = 0
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkBdVertexList != null ">
            and cbs.PK_BD_VERTEX in
            <foreach collection="pkBdVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </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="queryOrderAchieveSum" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select
        nvl(sum(ORDER_ACHIEVE),0) orderAchieve
        from
        SA_ORDER so
        inner join cu_member cm on so.pk_member=cm.pk_id and cm.DEL_FLAG=0
        WHERE
        so.DEL_FLAG=0
        and ORDER_STATUS=1
        <if test="orderTypeList != null ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkBdVertexList != null ">
            and cm.PK_VERTEX in
            <foreach collection="pkBdVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkVertex != null ">
            and cm.PK_VERTEX =#{pkVertex}
        </if>
        <if test="pkCountry != null">
            and so.PK_COUNTRY=#{pkCountry}
        </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>
    </select>

    <!-- 奖金拨比列表统计小计（最后一行）-->
    <select id="queryCuBonusSumStatis" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        SELECT
        <!-- 金额字段 -->
        sum (cbs.main_reg_amount) mainRegAmountSum,
        sum (cbs.main_up_amount) mainUpAmountSum,
        sum (cbs.main_repur_amount) mainRepurAmountSum,
        sum (cbs.special_register_amount) specialRegisterAmountSum,
        sum (cbs.special_upgrade_amount) specialUpgradeAmountSum,
        sum (cbs.special_repurchase_amount) specialRepurchaseAmountSum,
        <!-- 奖金字段 -->
        sum (cbs.INTRODUCE_BONUS) introduceBonusSum,
        sum (cbs.ORG_BONUS) orgBonusSum,
        sum (cbs.LEADER_BONUS) leaderBonusSum,
        sum (cbs.SERVICE_BONUS) serviceBonusSum,
        sum (cbs.REPUR_RANGE_BONUS) repurRangeBonusSum,
        sum (cbs.REPUR_ORG_BONUS) repurOrgBonusSum,
        sum (cbs.REPUR_LEADER_BONUS) repurLeaderBonusSum,
        sum (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT)
        AS firstPurchaseAllSum,
        sum (cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER)
        AS repurchaseAllAmountSum,
        sum (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS)
        AS mainBonusSum,
        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)
        / sum(cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT + cbs.SPECIAL_REGISTER_AMOUNT + cbs.SPECIAL_UPGRADE_AMOUNT + cbs.FICTITIOUS_AMOUNT), 4)
        end AS mainBonusRateSum,
        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)
        / #{rate} / sum(cbs.MAIN_REG_PV + cbs.MAIN_UP_PV + cbs.SPECIAL_REGISTER_PV + cbs.SPECIAL_UPGRADE_PV + cbs.FICTITIOUS_PV), 4)
        end AS mainBonusPvRateSum,
        sum(cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS) AS repurBonusSum,
        case
        WHEN sum(cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER) = 0
        THEN 0
        ELSE ROUND(#{perCent} * sum(cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        / sum(cbs.MAIN_REPUR_AMOUNT + cbs.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER), 4)
        end AS repurBonusRateSum,
        case
        WHEN sum(cbs.MAIN_REPUR_PV + cbs.MALL_PV + cbs.COOPERATE_PV + cbs.SPECIAL_REPURCHASE_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.REPUR_LEADER_BONUS)
        / #{rate} / sum(cbs.MAIN_REPUR_PV + cbs.MALL_PV +cbs.COOPERATE_PV + cbs.SPECIAL_REPURCHASE_PV + cbs.WELFARE_ORDER_PV), 4)
        end AS repurBonusPvRateSum,
        sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS
        + cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        AS totalBonusSum,
        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.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT +cbs.WELFARE_ORDER) = 0
        THEN 0
        ELSE ROUND (#{perCent} * sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS + cbs.LEADER_BONUS + cbs.SERVICE_BONUS
        + cbs.REPUR_RANGE_BONUS + cbs.REPUR_ORG_BONUS + cbs.REPUR_PUSH_INCOME + cbs.REPUR_LEADER_BONUS)
        / 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.MALL_AMOUNT + cbs.COOPERATE_AMOUNT + cbs.SPECIAL_REPURCHASE_AMOUNT + cbs.WELFARE_ORDER), 4)
        end AS totalBonusRateSum
        FROM CU_BONUS_STATIS cbs
        left join bd_vertex ver on cbs.pk_bd_vertex = ver.pk_id
        WHERE ver.DEL_FLAG = 0
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkBdVertexList != null ">
            and cbs.PK_BD_VERTEX in
            <foreach collection="pkBdVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </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="queryCuBonusStatisChart" parameterType="com.hzs.member.statis.vo.CuBonusStatisChartVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisChartVO">
        SELECT
        cbs.PK_BD_VERTEX pkVertex,
        cbs.PK_TEAM_CODE pkTeamCode,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cbs.PK_ID pkId,
        cbs.PERIOD period,
        cbs.MAIN_REG_AMOUNT mainRegAmount,
        cbs.MAIN_UP_AMOUNT mainUpAmount,
        cbs.MAIN_REPUR_AMOUNT mainRepurAmount,
        cbs.AGENT_UP_AMOUNT agentUpAmount,
        cbs.AGENT_REPUR_AMOUNT agentRepurAmount,
        cbs.HIFANS_REG_AMOUNT hifansRegAmount,
        cbs.TOTAL_AMOUNT totalAmount,
        cbs.INTRODUCE_BONUS introduceBonus,
        cbs.ORG_BONUS orgBonus,
        cbs.LEADER_BONUS leaderBonus,
        cbs.SHARE_BONUS shareBonus,
        cbs.SERVICE_BONUS serviceBonus,
        cbs.HI_FUN_INCOME hiFunIncome,
        cbs.REPUR_RANGE_BONUS repurRangeBonus,
        cbs.REPUR_ORG_BONUS repurOrgBonus,
        cbs.AGENT_INTRODUCE_BONUS agentIntroduceBonus,
        cbs.AGENT_REG_AMOUNT agentRegAmount,
        cbs.gift_order giftOrder,
        cbs.CREATION_TIME creationTime,
        cbs.PK_COUNTRY pkCountry,
        cbs.pur_real_subtotal purRealSubtotal,
        cbs.REPUR_REAL_SUBTOTAL repurRealSubtotal,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.INTRODUCE_BONUS/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end as introduceBonusRate,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.ORG_BONUS/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end as orgBonusRate,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.LEADER_BONUS/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end leaderBonusRate,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.SHARE_BONUS/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end shareBonusRate,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.SERVICE_BONUS/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end serviceBonusRate,
        CASE WHEN (cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.AGENT_INTRODUCE_BONUS/(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT),4) end as
        agentIntroduceBonusRate,
        CASE WHEN (cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.AGENT_REG_AMOUNT/(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT),4) end
        as agentFirstAmountRate,
        CASE WHEN (cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT)=0 THEN 0 ELSE
        ROUND(#{perCent}*cbs.AGENT_REPUR_AMOUNT/(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT),4)
        end as agentRepurAmountRate,
        ( cbs.MAIN_REG_AMOUNT +
        cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT ) AS
        firstPurchaseAll,
        ( 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,
        ( cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT ) AS agentFirstAmount,

        (cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        (#{orderAchieve})+cbs.MAKER_DIRECT+cbs.MAKER_SHARE) AS mainBonus,

        CASE WHEN
        (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}*(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        (#{orderAchieve})+cbs.MAKER_DIRECT+cbs.MAKER_SHARE)
        /(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT),4)
        end AS mainBonusRate,

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

        CASE WHEN
        (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}*(((cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME))+cbs.HI_FUN_INCOME) /
        (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,

        (cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        (#{orderAchieve})+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
        (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}*(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS+
        (#{orderAchieve})+cbs.MAKER_DIRECT+cbs.MAKER_SHARE+
        cbs.REPUR_RANGE_BONUS+cbs.REPUR_ORG_BONUS+cbs.REPUR_PUSH_INCOME+cbs.HI_FUN_INCOME) /
        (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_STATIS cbs
        left join bd_vertex ver on cbs.pk_bd_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cbs.pk_team_code
        WHERE 1=1
        <if test="pkCountry != null and pkCountry ==1 ">
            and ver.DEL_FLAG=0
        </if>
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkTeamCodeList != null ">
            and cbs.PK_TEAM_CODE in
            <foreach collection="pkTeamCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkBdVertexList != null ">
            and cbs.PK_BD_VERTEX in
            <foreach collection="pkBdVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </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 DESC
    </select>

    <!-- 奖金拨比图表统计合计 最后一行 -->
    <select id="queryCuBonusSumStatisChart" parameterType="com.hzs.member.statis.vo.CuBonusStatisChartVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisChartVO">
        SELECT
        sum (cbs.MAIN_REG_AMOUNT) mainRegAmountSum,
        sum (cbs.MAIN_UP_AMOUNT) mainUpAmountSum,
        sum (cbs.MAIN_REPUR_AMOUNT) mainRepurAmountSum,
        sum (cbs.AGENT_UP_AMOUNT) agentUpAmountSum,
        sum (cbs.AGENT_REPUR_AMOUNT) agentRepurAmountSum,
        sum (cbs.HIFANS_REG_AMOUNT) hifansRegAmountSum,
        sum (cbs.INTRODUCE_BONUS) introduceBonusSum,
        sum (cbs.ORG_BONUS) orgBonusSum,
        sum (cbs.LEADER_BONUS) leaderBonusSum,
        sum (cbs.SHARE_BONUS) shareBonusSum,
        sum (cbs.SERVICE_BONUS) serviceBonusSum,
        sum (cbs.HI_FUN_INCOME) hiFunIncomeSum,
        sum (cbs.REPUR_RANGE_BONUS) repurRangeBonusSum,
        sum (cbs.SEABEAN_REPUR_RANGE) seabeanRepurRangeSum,
        sum (cbs.REPUR_ORG_BONUS) repurOrgBonusSum,
        sum (cbs.AGENT_INTRODUCE_BONUS) agentIntroduceBonusSum,
        sum (cbs.AGENT_AREA_BONUS) agentAreaBonusSum,
        sum (cbs.AGENT_REG_AMOUNT) agentRegAmountSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.INTRODUCE_BONUS)/(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)),4) end as
        introduceBonusRateSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.ORG_BONUS)/(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)),4) end as orgBonusRateSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.LEADER_BONUS)/(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)),4) end leaderBonusRateSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.SHARE_BONUS)/(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)),4) end shareBonusRateSum,
        CASE WHEN (sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.SERVICE_BONUS)/(sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)),4) end
        serviceBonusRateSum,
        CASE WHEN (sum(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.AGENT_INTRODUCE_BONUS)/(sum(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT)),4) end as
        agentIntroduceBonusRateSum,
        CASE WHEN (sum(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*cbs.AGENT_REG_AMOUNT)/(sum(cbs.AGENT_REG_AMOUNT +
        cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT)),4) end as agentFirstAmountRateSum,
        CASE WHEN (sum(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT))=0 THEN 0 ELSE
        ROUND(sum(#{perCent}*AGENT_REPUR_AMOUNT)/(sum(cbs.AGENT_REG_AMOUNT +
        cbs.AGENT_UP_AMOUNT+cbs.AGENT_REPUR_AMOUNT)),4) end as agentRepurAmountRateSum,
        ( sum(cbs.MAIN_REG_AMOUNT +
        cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT) ) AS
        firstPurchaseAllSum,
        ( 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 repurchaseAllAmountSum,
        ( sum(cbs.AGENT_REG_AMOUNT + cbs.AGENT_UP_AMOUNT) ) AS agentFirstAmountSum,

        sum(cbs.INTRODUCE_BONUS+cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SERVICE_BONUS
        +cbs.MAKER_DIRECT+cbs.MAKER_SHARE)+(#{orderAchieve}) AS mainBonusSum,

        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)+(#{orderAchieve}))
        /sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT+cbs.SPECIAL_REGISTER_AMOUNT+cbs.SPECIAL_UPGRADE_AMOUNT+cbs.FICTITIOUS_AMOUNT),4)
        end AS mainBonusRateSum,

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

        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 repurBonusRateSum,

        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)+(#{orderAchieve}) AS totalBonusSum,

        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)+(#{orderAchieve})) /
        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 totalBonusRateSum
        FROM
        CU_BONUS_STATIS cbs
        left join bd_vertex ver on cbs.pk_bd_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cbs.pk_team_code
        WHERE 1=1
        <if test="pkCountry != null and pkCountry ==1 ">
            and ver.DEL_FLAG=0
        </if>
        <if test="pkCountry != null">
            and cbs.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkTeamCodeList != null ">
            and cbs.PK_TEAM_CODE in
            <foreach collection="pkTeamCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkBdVertexList != null ">
            and cbs.PK_BD_VERTEX in
            <foreach collection="pkBdVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </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="queryYearData" parameterType="com.hzs.member.statis.vo.CuBonusStatisColumnarVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisColumnarVO">
        select sum(MAIN_REG_AMOUNT) MAIN_REG_AMOUNT,
        sum(MAIN_UP_AMOUNT) MAIN_UP_AMOUNT,
        sum(MAIN_REPUR_AMOUNT) MAIN_REPUR_AMOUNT,
        sum(HIFANS_REG_AMOUNT) HIFANS_REG_AMOUNT
        from CU_BONUS_STATIS
        WHERE
        PK_COUNTRY=#{pkCountry}
        <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}, 'yyyy-mm-dd')
        </if>
    </select>

    <!-- 查询奖金拨比 圆饼图 -->
    <select id="queryDiallingRatio" parameterType="com.hzs.member.statis.vo.CuBonusStatisRoundVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisRoundVO">
        select
        sum(MAIN_REG_AMOUNT + MAIN_UP_AMOUNT+SPECIAL_REGISTER_AMOUNT+
        SPECIAL_UPGRADE_AMOUNT+MAKER_GIFT_AMOUNT) firstPurchaseAll,
        sum(MAIN_REPUR_AMOUNT + HIFANS_REG_AMOUNT+HAI_FUN_UPGRADE_AMOUNT+CLOUD_COLLEGE_AMOUNT+MALL_AMOUNT
        +EMPOWERMENT_GIFT_AMOUNT+COOPERATE_AMOUNT+SPECIAL_REPURCHASE_AMOUNT) repurchaseAllAmount,
        sum(REAL_INCOME_TOTAL) totalBonus,
        sum(INTRODUCE_BONUS) introduceBonus,
        sum(ORG_BONUS) orgBonus,
        sum(LEADER_BONUS) leaderBonus,
        sum(SHARE_BONUS) shareBonus,
        sum(SERVICE_BONUS) serviceBonus,
        sum(REPUR_RANGE_BONUS) repurRangeBonus,
        sum(REPUR_ORG_BONUS) repurOrgBonus,
        sum(AGENT_INTRODUCE_BONUS) agentIntroduceBonus,
        sum(AGENT_REG_AMOUNT+AGENT_UP_AMOUNT) agentFirstAmount,
        sum(AGENT_REPUR_AMOUNT) agentRepurAmount
        from CU_BONUS_STATIS
        WHERE
        PK_COUNTRY=#{pkCountry}
        <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}, 'yyyy-mm-dd')
        </if>

    </select>

    <!-- 查询小于当天全部已支付或撤单订单信息-->
    <select id="querySaOrderByLessDay" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select * from (
        select
        cm.pk_vertex pkVertex,
        cm.pk_team_code pkTeamCode,
        so.PK_COUNTRY pkCountry,
        so.PK_MEMBER pkMember,
        so.ORDER_TYPE orderType,
        (so.ORDER_AMOUNT - nvl(so.CONSUME_AMOUNT,0)) orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'), 'yyyy-mm-dd') AS creationTime,
        so.MODIFIED_TIME modifiedTime
        from sa_order so
        inner JOIN CU_MEMBER cm on so.PK_MEMBER = cm.pk_id
        where so.ORDER_TYPE in (1, 2, 3, 13, 20, 24, 25, 26) and cm.DEL_FLAG = 0
        <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>
        <if test="modifiedTimeStr != null and modifiedTimeStr!='' ">
            AND so.MODIFIED_TIME = to_date(#{modifiedTimeStr}, 'YYYY-MM-dd')
        </if>
        )
    </select>

    <!-- 删除奖金拨比统计表数据-->
    <delete id="delStatis">
        delete from CU_BONUS_STATIS
        where 1=1
        <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="bonusDeductlist" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select a.PK_ID,
        b.PK_ID as pkId,
        (a.BACK_RATIO + a.PLATFORM_RATIO) / 100 backRatio
        from BD_BONUS_ITEMS b
        left JOIN BD_BONUS_DEDUCT a on a.PK_BONUS_ITEMS=b.PK_ID
        where b.DEL_FLAG = 0
        and a.BACK_RATIO > 0
        and a.PLATFORM_RATIO > 0
        <if test="pkCountry != null ">
            AND b.PK_COUNTRY = #{pkCountry}
        </if>
        ORDER BY a.BACK_RATIO, a.PLATFORM_RATIO DESC
    </select>

    <!-- 查询个人奖金拨比 -->
    <select id="bonusSingleSum" parameterType="com.hzs.member.statis.vo.CuBonusSinglePersonStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusSinglePersonStatisVO">
        select
        cm.MEMBER_CODE,cm.MEMBER_NAME,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        (select
        sum(so.ORDER_AMOUNT) orderAchieve
        from sa_order so
        inner JOIN CU_MEMBER cm on so.PK_MEMBER=cm.pk_id
        where
        so.ORDER_TYPE in (1,2,24,25)
        and cm.DEL_FLAG=0
        and so.pk_country=1
        AND so.DEL_FLAG=0
        AND so.order_status=1
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )) firstPurchaseAll,
        (select
        sum(so.ORDER_AMOUNT - nvl(so.consume_amount, 0)) orderAmount
        from sa_order so
        inner JOIN CU_MEMBER cm on so.PK_MEMBER=cm.pk_id
        where
        so.ORDER_TYPE in (3,12,7, 14, 18 ,19, 22, 26, 28, 13)
        and cm.DEL_FLAG=0
        and so.pk_country=1
        AND so.DEL_FLAG=0
        AND so.order_status=1
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )) repurchaseAllAmount,
        (
        select sum(so.ORDER_ACHIEVE)*#{dividendRatio} from sa_order so
        INNER JOIN CU_MEMBER cm on cm.pk_id=so.pk_member
        where cm.del_flag = 0
        and cm.CATEGORY=0
        and so.pk_country=#{pkCountry}
        AND so.DEL_FLAG=0
        AND so.order_status=1
        AND so.ORDER_TYPE in (1,2,24,25)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        ) shareBonus,
        (
        select sum(realIncome)*#{rate} from (
        select
        sum(mbp.REAL_INCOME) realIncome,
        mbp.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_push mbp
        INNER JOIN CU_MEMBER_BONUS cmb on mbp.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbp.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbp.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbp.DEL_FLAG=0
        and mbp.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (8,9,12,24,20,21,1)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbp.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbe.REAL_INCOME) realIncome,
        mbe.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_EXPAND mbe
        INNER JOIN CU_MEMBER_BONUS cmb on mbe.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbe.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbe.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbe.DEL_FLAG=0
        and mbe.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (8,9,12,24,20,21,1)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbe.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbc.REAL_INCOME) realIncome,
        mbc.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_COACH mbc
        INNER JOIN CU_MEMBER_BONUS cmb on mbc.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbc.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbc.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbc.DEL_FLAG=0
        and mbc.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (8,9,12,24,20,21,1)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbc.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbr.REAL_INCOME) realIncome,
        mbr.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_range mbr
        INNER JOIN CU_MEMBER_BONUS cmb on mbr.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbr.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbr.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbr.DEL_FLAG=0
        and mbr.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (8,9,12,24,20,21,1)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY bbi.BONUS_VALUE,
        mbr.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbd.REAL_INCOME) realIncome,
        mbd.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_detail mbd
        INNER JOIN CU_MEMBER_BONUS cmb on mbd.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbd.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbd.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbd.DEL_FLAG=0
        and mbd.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (8,9,12,24,20,21,1)
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        <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>
        GROUP BY bbi.BONUS_VALUE,
        mbd.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        )
        )
        repurBonus,
        (
        select sum(realIncome)*#{rate} from (
        select
        sum(mbp.REAL_INCOME) realIncome,
        mbp.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_push mbp
        INNER JOIN CU_MEMBER_BONUS cmb on mbp.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbp.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbp.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbp.DEL_FLAG=0
        and mbp.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (3,4,5,7)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbp.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbe.REAL_INCOME) realIncome,
        mbe.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_EXPAND mbe
        INNER JOIN CU_MEMBER_BONUS cmb on mbe.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbe.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbe.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbe.DEL_FLAG=0
        and mbe.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (3,4,5,7)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbe.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbc.REAL_INCOME) realIncome,
        mbc.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_COACH mbc
        INNER JOIN CU_MEMBER_BONUS cmb on mbc.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbc.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbc.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbc.DEL_FLAG=0
        and mbc.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (3,4,5,7)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY mbc.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbr.REAL_INCOME) realIncome,
        mbr.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_range mbr
        INNER JOIN CU_MEMBER_BONUS cmb on mbr.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbr.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbr.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbr.DEL_FLAG=0
        and mbr.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (3,4,5,7)
        <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>
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        GROUP BY bbi.BONUS_VALUE,
        mbr.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        sum(mbd.REAL_INCOME) realIncome,
        mbd.PK_COUNTRY con,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_detail mbd
        INNER JOIN CU_MEMBER_BONUS cmb on mbd.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbd.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id=mbd.pk_order and so.PK_COUNTRY=1 and so.del_flag=0
        where mbd.DEL_FLAG=0
        and mbd.INCOME_STATUS=0
        and bbi.BONUS_VALUE in (3,4,5,7)
        and cm.pk_id in (
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and cm.CATEGORY=0
        START WITH cm.pk_parent =#{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        <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>
        GROUP BY bbi.BONUS_VALUE,
        mbd.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        )
        ) mainBonus
        from CU_MEMBER cm
        left join bd_vertex ver on cm.PK_VERTEX = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.PK_TEAM_CODE
        where cm.DEL_FLAG=0 and cm.PK_ID=#{pkMember}
    </select>

    <!-- 查询分红比例-->
    <select id="bonusDividendRatiolist" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select
        ba.PK_ID as pkId,
        (ba.ABONUS_RATIO) dividendRatio
        from BD_AWARDS ba
        where
        ba.DEL_FLAG =0
        and ba.ENABLE_STATE=0
        and ba.ABONUS_RATIO>0
        <if test="pkCountry != null ">
            AND ba.PK_COUNTRY = #{pkCountry}
        </if>
        ORDER BY ba.ABONUS_RATIO DESC
    </select>

    <!-- 查询有效体系-->
    <select id="vertexList" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select pk_id as pkVertex
        from BD_VERTEX
        where del_flag = 0
    </select>

    <!-- 查询中国奖金-->
    <select id="queryChinaCountryBonus" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        <!-- 直推收益 -->
        select
        #{pkCountry} pkCountry, mbp.PK_COUNTRY con,
        sum(mbp.REAL_INCOME) realIncome,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_push mbp
        inner join CU_MEMBER_BONUS cmb on mbp.PK_BONUS = cmb.pk_id
        inner join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id = mbp.PK_BONUS_ITEMS
        inner join sa_order so on so.pk_id = mbp.pk_order
        and so.PK_COUNTRY = #{pkCountry} and so.del_flag = 0
        where mbp.DEL_FLAG = 0
        and mbp.INCOME_STATUS = 0
        and bbi.BONUS_VALUE in (1, 3, 4, 5, 7, 8, 9, 12, 14, 15, 16, 20, 21, 24, 25)
        <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>
        GROUP BY mbp.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        <!-- 拓展收益、消费拓展收益，收益状态：正常，延缓发放 -->
        select
        #{pkCountry} pkCountry, mbe.PK_COUNTRY con,
        sum(mbe.REAL_INCOME) realIncome,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_EXPAND mbe
        INNER JOIN CU_MEMBER_BONUS cmb on mbe.PK_BONUS = cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id = mbe.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id = mbe.pk_order
        and so.PK_COUNTRY = #{pkCountry} and so.del_flag = 0
        where mbe.DEL_FLAG = 0
        and mbe.INCOME_STATUS in (0, 4)
        and mbe.pk_order != 0
        and bbi.BONUS_VALUE in (1, 3, 4, 5, 7, 8, 9, 12, 14, 15, 16, 20, 21, 24, 25)
        <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>
        GROUP BY mbe.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        <!-- 辅导收益，收益状态：正常，延缓发放 -->
        select
        #{pkCountry} pkCountry, mbc.PK_COUNTRY con,
        sum(mbc.REAL_INCOME) realIncome,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from CU_MEMBER_BONUS_COACH mbc
        inner join CU_MEMBER_BONUS cmb on mbc.PK_BONUS = cmb.pk_id
        inner join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id = mbc.PK_BONUS_ITEMS
        inner JOIN sa_order so on so.pk_id = mbc.pk_order
        and so.PK_COUNTRY = #{pkCountry} and so.del_flag = 0
        where mbc.DEL_FLAG = 0
        and mbc.INCOME_STATUS in (0, 4)
        and mbc.pk_order != 0
        and bbi.BONUS_VALUE in (1, 3, 4, 5, 7, 8, 9, 12, 14, 15, 16, 20, 21, 24, 25)
        <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>
        GROUP BY mbc.PK_COUNTRY,
        bbi.BONUS_VALUE,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        <!-- 复购级差 -->
        select
        #{pkCountry} pkCountry, mbr.PK_COUNTRY con,
        sum(mbr.REAL_INCOME) realIncome,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_range mbr
        INNER JOIN CU_MEMBER_BONUS cmb on mbr.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id=mbr.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id = mbr.pk_order
        and so.PK_COUNTRY = #{pkCountry} and so.del_flag = 0
        where mbr.DEL_FLAG = 0
        and mbr.INCOME_STATUS = 0
        and bbi.BONUS_VALUE in (1, 3, 4, 5, 7, 8, 9, 12, 14, 15, 16, 20, 21, 24, 25)
        <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>
        GROUP BY bbi.BONUS_VALUE,
        mbr.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        <!-- 报单服务费 -->
        select
        #{pkCountry} pkCountry, mbd.PK_COUNTRY con,
        sum(mbd.REAL_INCOME) realIncome,
        bbi.BONUS_VALUE bonusType,
        cm.PK_VERTEX pkVertex,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime
        from cu_member_bonus_detail mbd
        INNER JOIN CU_MEMBER_BONUS cmb on mbd.PK_BONUS=cmb.pk_id
        INNER join cu_member cm on cm.pk_id = cmb.pk_member
        inner join BD_BONUS_ITEMS bbi on bbi.pk_id = mbd.PK_BONUS_ITEMS
        INNER JOIN sa_order so on so.pk_id = mbd.pk_order
        and so.PK_COUNTRY = #{pkCountry} and so.del_flag = 0
        where mbd.DEL_FLAG = 0
        and mbd.INCOME_STATUS = 0
        and bbi.BONUS_VALUE in (1, 3, 4, 5, 7, 8, 9, 12, 14, 15, 16, 20, 21, 24, 25)
        <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>
        GROUP BY bbi.BONUS_VALUE,
        mbd.PK_COUNTRY,
        cm.PK_VERTEX,
        to_char(so.PAY_TIME, 'yyyy-mm-dd')
    </select>

</mapper>
