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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.statis.CuBonusStatis">
        <id column="PK_ID" property="pkId"/>
        <result column="STATIS_TYPE" property="statisType"/>
        <result column="PERIOD" property="period"/>
        <result column="START_DATE" property="startDate"/>
        <result column="END_DATE" property="endDate"/>
        <result column="MAIN_REG_AMOUNT" property="mainRegAmount"/>
        <result column="MAIN_UP_AMOUNT" property="mainUpAmount"/>
        <result column="MAIN_REPUR_AMOUNT" property="mainRepurAmount"/>
        <result column="AGENT_REG_AMOUNT" property="agentRegAmount"/>
        <result column="AGENT_UP_AMOUNT" property="agentUpAmount"/>
        <result column="AGENT_REPUR_AMOUNT" property="agentRepurAmount"/>
        <result column="HIFANS_REG_AMOUNT" property="hifansRegAmount"/>
        <result column="TOTAL_AMOUNT" property="totalAmount"/>
        <result column="INTRODUCE_BONUS" property="introduceBonus"/>
        <result column="ORG_BONUS" property="orgBonus"/>
        <result column="LEADER_BONUS" property="leaderBonus"/>
        <result column="SHARE_BONUS" property="shareBonus"/>
        <result column="SERVICE_BONUS" property="serviceBonus"/>
        <result column="SEABEAN" property="seabean"/>
        <result column="REPUR_RANGE_BONUS" property="repurRangeBonus"/>
        <result column="SEABEAN_REPUR_RANGE" property="seabeanRepurRange"/>
        <result column="REPUR_ORG_BONUS" property="repurOrgBonus"/>
        <result column="AGENT_INTRODUCE_BONUS" property="agentIntroduceBonus"/>
        <result column="AGENT_AREA_BONUS" property="agentAreaBonus"/>
        <result column="MAIN_BONUS" property="mainBonus"/>
        <result column="REPUR_BONUS" property="repurBonus"/>
        <result column="TOTAL_BONUS" property="totalBonus"/>
        <result column="TOTAL_BONUS_REAL" property="totalBonusReal"/>
        <result column="PK_BD_VERTEX" property="pkBdVertex"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="HI_FUN_INCOME" property="hiFunIncome"/>
        <result column="GIFT_ORDER" property="giftOrder"/>
        <result column="SEE_POINT_INCOME" property="seePointIncome"/>
        <result column="SHARE_INCOME" property="shareIncome"/>
        <result column="REPUR_RANGE_INCOME" property="repurRangeIncome"/>
        <result column="SAME_LEVEL_INCOME" property="sameLevelIncome"/>
    </resultMap>

    <!--   查询当天团队统计数据-->
    <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="queryForeignCountryCuBonusStatis" 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.MAIN_REG_AMOUNT mainRegAmount,
        cbs.MAIN_UP_AMOUNT mainUpAmount,
        cbs.FICTITIOUS_ORDER fictitiousOrder,
        cbs.MAIN_REPUR_AMOUNT mainRepurAmount,
        cbs.SEE_POINT_INCOME seePointIncome,
        cbs.SHARE_INCOME shareIncome,
        cbs.INTRODUCE_BONUS introduceBonus,
        cbs.ORG_BONUS orgBonus,
        cbs.LEADER_BONUS leaderBonus,
        cbs.REPUR_RANGE_INCOME repurRangeIncome,
        cbs.SAME_LEVEL_INCOME sameLevelIncome,
        cbs.CREATION_TIME creationTime,
        cbs.PK_COUNTRY pkCountry,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*cbs.SEE_POINT_INCOME/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end seePointIncomeRate,
        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*cbs.SHARE_INCOME/(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end shareIncomeRate,

        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,

        ( cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT) AS firstPurchaseAll,

        (cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME ) AS mainBonus,

        CASE WHEN (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT) = 0
        THEN 0 ELSE ROUND (#{perCent}*#{backRatio}*(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME)
        / (cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end AS mainBonusRate,

        ( cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME ) AS repurBonus,

        CASE WHEN (cbs.MAIN_REPUR_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*#{backRatio}*(cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME) / (cbs.MAIN_REPUR_AMOUNT),4) end AS repurBonusRate,

        cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME+
        cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME AS totalBonus,

        CASE WHEN ( cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT)=0 THEN 0
        ELSE ROUND (#{perCent}*#{backRatio}*(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME+
        cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME) /
        (cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT),2) 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 asc

    </select>
    <!--     查询国外奖金拨比合计-->
    <select id="queryForeignCountryCuBonusStatisSum" 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.FICTITIOUS_ORDER) fictitiousOrderSum,
        sum(cbs.MAIN_REPUR_AMOUNT) mainRepurAmountSum,
        sum(cbs.SEE_POINT_INCOME) seePointIncomeSum,
        sum(cbs.SHARE_INCOME) shareIncomeSum,
        sum(cbs.INTRODUCE_BONUS) introduceBonusSum,
        sum(cbs.ORG_BONUS) orgBonusSum,
        sum(cbs.LEADER_BONUS) leaderBonusSum,
        sum(cbs.REPUR_RANGE_INCOME) repurRangeIncomeSum,
        sum(cbs.SAME_LEVEL_INCOME) sameLevelIncomeSum,
        CASE WHEN sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(cbs.SEE_POINT_INCOME)/sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end seePointIncomeRateSum,
        CASE WHEN sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(cbs.SHARE_INCOME)/sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end shareIncomeRateSum,

        CASE WHEN sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*sum(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(#{perCent}*sum(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(#{perCent}*sum(cbs.LEADER_BONUS)/sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end leaderBonusRateSum,

        sum(cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT) AS firstPurchaseAllSum,

        sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME ) AS mainBonusSum,

        CASE WHEN sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT) = 0
        THEN 0 ELSE ROUND (#{perCent}*#{backRatio}*sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME)
        / sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4) end AS mainBonusRateSum,

        sum( cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME ) AS repurBonusSum,

        CASE WHEN sum(cbs.MAIN_REPUR_AMOUNT)=0 THEN 0
        ELSE ROUND(#{perCent}*#{backRatio}*sum(cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME) / sum(cbs.MAIN_REPUR_AMOUNT),4) end AS repurBonusRateSum,

        sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME+
        cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME) AS totalBonusSum,

        CASE WHEN sum( cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT)=0 THEN 0
        ELSE ROUND (#{perCent}*#{backRatio}*sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS+cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME+
        cbs.REPUR_RANGE_INCOME+cbs.SAME_LEVEL_INCOME) /
        sum(cbs.MAIN_REG_AMOUNT + cbs.MAIN_UP_AMOUNT+cbs.MAIN_REPUR_AMOUNT),2) 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
        +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 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,30,20) 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="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="countrylist" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">
        select pk_id as pkCountry
        from BD_COUNTRY
        where pk_id!=1 and del_flag = 0 and EFFECTIVE_DATE &lt;= sysdate
    </select>

    <!--     查询国外奖金-->
    <select id="queryForeignCountryBonus" parameterType="com.hzs.member.statis.vo.CuBonusStatisVO"
            resultType="com.hzs.member.statis.vo.CuBonusStatisVO">

        SELECT
        #{pkCountry} pkCountry,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime,
        mbp.real_income,
        bbi.BONUS_VALUE bonusType
        FROM
        (
        SELECT
        pk_order,
        sum( real_income ) real_income,
        PK_BONUS_ITEMS
        FROM
        ( SELECT pk_order, real_income,PK_BONUS_ITEMS FROM cu_member_bonus_push WHERE del_flag = 0 AND income_status = 0 )
        GROUP BY
        pk_order,PK_BONUS_ITEMS
        ) mbp
        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
        WHERE
        so.PK_COUNTRY = #{pkCountry}
        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>
        UNION ALL
        SELECT
        #{pkCountry} pkCountry,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime,
        mbe.real_income,
        bbi.BONUS_VALUE bonusType
        FROM
        (
        SELECT
        pk_order,
        sum( real_income ) real_income,
        PK_BONUS_ITEMS
        FROM
        ( SELECT pk_order, real_income,PK_BONUS_ITEMS FROM CU_MEMBER_BONUS_EXPAND WHERE del_flag = 0 AND income_status = 0 )
        GROUP BY
        pk_order,PK_BONUS_ITEMS
        ) mbe
        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
        WHERE
        so.PK_COUNTRY = #{pkCountry}
        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>
        UNION ALL
        SELECT
        #{pkCountry} pkCountry,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime,
        mbc.real_income,
        bbi.BONUS_VALUE bonusType
        FROM
        (
        SELECT
        pk_order,
        sum( real_income ) real_income,
        PK_BONUS_ITEMS
        FROM
        ( SELECT pk_order, real_income,PK_BONUS_ITEMS FROM CU_MEMBER_BONUS_COACH WHERE del_flag = 0 AND income_status = 0 )
        GROUP BY
        pk_order,PK_BONUS_ITEMS
        ) mbc
        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
        WHERE
        so.PK_COUNTRY = #{pkCountry}
        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>
        UNION ALL
        SELECT
        #{pkCountry} pkCountry,
        to_date(to_char(so.PAY_TIME, 'yyyy-mm-dd'),'yyyy-mm-dd') AS creationTime,
        mbr.real_income,
        bbi.BONUS_VALUE bonusType
        FROM
        (
        SELECT
        pk_order,
        sum( real_income ) real_income,
        PK_BONUS_ITEMS
        FROM
        ( SELECT pk_order, real_income,PK_BONUS_ITEMS FROM cu_member_bonus_range mbr WHERE del_flag = 0 AND income_status = 0 )
        GROUP BY
        pk_order,PK_BONUS_ITEMS
        ) mbr
        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
        WHERE
        so.PK_COUNTRY = #{pkCountry}
        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>
    </select>


</mapper>
