<?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.bonus.bonus.mapper.CuMemberBonusMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberBonus" type="com.hzs.common.domain.member.ext.CuMemberBonusExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PERIOD" property="period"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="out_exchange_rate" property="outExchangeRate"/>
        <result column="DIRECT_INCOME" property="directIncome"/>
        <result column="EXPAND_INCOME" property="expandIncome"/>
        <result column="EXPAND_NO_CAPPING" property="expandNoCapping"/>
        <result column="EXPAND_CAPPING" property="expandCapping"/>
        <result column="COACH_INCOME" property="coachIncome"/>
        <result column="SHARE_INCOME" property="shareIncome"/>
        <result column="SERVICE_INCOME" property="serviceIncome"/>
        <result column="BACK_POINTS" property="backPoints"/>
        <result column="SERVICE_SPEND" property="serviceSpend"/>
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal"/>
        <result column="REPUR_PUSH_INCOME" property="repurPushIncome"/>
        <result column="REPUR_RANGE_INCOME" property="repurRangeIncome"/>
        <result column="REPUR_EXPAND_INCOME" property="repurExpandIncome"/>
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal"/>
        <result column="CLOUD_DIRECT_INCOME" property="cloudDirectIncome"/>
        <result column="CLOUD_PUR_INCOME" property="cloudPurIncome"/>
        <result column="CLOUD_REPUR_INCOME" property="cloudRepurIncome"/>
        <result column="CLOUD_REAL_SUBTOTAL" property="cloudRealSubtotal"/>
        <result column="REPUR_COUPON" property="repurCoupon"/>
        <result column="REPUR_COUPON_SHARE" property="repurCouponShare"/>
        <result column="REPUR_COUPON_SUBTOTAL" property="repurCouponSubtotal"/>
        <result column="GLOBAL_POINTS" property="globalPoints"/>
        <result column="CAR_AWARD_POINTS" property="carAwardPoints"/>
        <result column="HI_FUN_INCOME" property="hiFunIncome"/>
        <result column="MAKER_DIRECT_INCOME" property="makerDirectIncome"/>
        <result column="MAKER_SHARE_INCOME" property="makerShareIncome"/>
        <result column="MAKER_REAL_SUBTOTAL" property="makerRealSubtotal"/>
        <result column="REAL_INCOME_TOTAL" property="realIncomeTotal"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="GRANT_STATUS" property="grantStatus"/>
        <result column="SETTLE_DATE" property="settleDate"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="TEAM_NAME" property="teamName"/>
        <result column="STORE_INCOME" property="storeIncome"/>
        <result column="HAI_FUN_INCOME" property="haiFunIncome"/>
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal"/>
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal"/>
        <result column="CLOUD_REAL_SUBTOTAL" property="cloudRealSubtotal"/>
        <result column="MAKER_REAL_SUBTOTAL" property="makerRealSubtotal"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="SERVICE_CODE" property="serviceCode"/>
        <result column="NICK_NAME" property="nickName"/>

        <result column="RETAIL_RANGE_INCOME" property="retailRangeIncome"/>
        <result column="RETAIL_SAME_LEVEL_INCOME" property="retailSameLevelIncome"/>
        <result column="RETAIL_AREA_INCOME" property="retailAreaIncome"/>
        <result column="RETAIL_BENEFIT_RANGE_INCOME" property="retailBenefitRangeIncome"/>
        <result column="RETAIL_MONTH_REPURCHASE_INCOME" property="retailMonthRepurchaseIncome"/>
        <result column="RETAIL_BENEFIT_AVG_INCOME" property="retailBenefitAvgIncome"/>
        <result column="RETAIL_BENEFIT_INCOME" property="retailBenefitIncome"/>
    </resultMap>

    <update id="updateCuMemberBonusStatus">
        update cu_member_bonus
        set grant_status = #{grantStatus}, pk_modified = 100000000, modified_time = sysdate
        where del_flag = 0
        and grant_status = #{oldGrantStatus}
        and period between #{startPeriod} and #{endPeriod}
        <if test="pkCountry != null">
            and pk_country = #{pkCountry}
        </if>
    </update>

    <update id="mergeBackRangeBonusIncome">
        merge into cu_member_bonus a
        using (
        select b.* from bd_bonus_items bt
        inner join (
        select pk_bonus, pk_bonus_items,
        sum(pretax_income) pretax_income,
        sum(income_tax) income_tax,
        sum(real_income) real_income
        from
        <choose>
            <when test="bonusValue == 27">cu_member_bonus_detail</when>
            <otherwise>cu_member_bonus_range</otherwise>
        </choose>
        where del_flag = 0
        and pk_order= #{pkOrder}
        group by pk_bonus, pk_bonus_items
        ) b
        on bt.pk_id = b.pk_bonus_items
        where bt.bonus_value = #{bonusValue}
        ) b
        on (a.pk_id = b.pk_bonus)
        when matched then
        update set a.retail_real_subtotal = a.retail_real_subtotal - b.real_income,
        a.back_points = a.back_points - b.income_tax,
        a.real_income_total = a.real_income_total - b.real_income
        <if test="bonusValue == 8">
            ,a.repur_range_income = a.repur_range_income - b.pretax_income
        </if>
        <if test="bonusValue == 25">
            ,a.retail_range_income = a.retail_range_income - b.pretax_income
        </if>
        <if test="bonusValue == 26">
            ,a.retail_same_level_income = a.retail_same_level_income - b.pretax_income
        </if>
        <if test="bonusValue == 27">
            ,a.retail_area_income = a.retail_area_income - b.pretax_income
        </if>
    </update>

    <delete id="deleteCuMemberBonus">
        delete
        from cu_member_bonus
        where period = #{period}
          and grant_status &lt;= 2
    </delete>

    <insert id="batchInsertCuMemberBonus">
        merge into cu_member_bonus cb
        using(
        select pk_id, pk_member, period, pk_rate, direct_income,
        expand_income, expand_no_capping, expand_capping, coach_income,
        share_income, service_income, back_points, service_spend,
        pur_real_subtotal, repur_push_income, repur_range_income, repur_expand_income,
        repur_real_subtotal, repur_coupon, repur_coupon_share, repur_coupon_subtotal,
        store_income, real_income_total, pk_country, pk_creator, grant_status, income_status,
        retail_range_income, retail_same_level_income, retail_area_income,
        retail_benefit_range_income, retail_month_repurchase_income,
        retail_benefit_avg_income, retail_benefit_income, retail_real_subtotal
        from (
        <foreach item="item" index="index" collection="cuMemberBonusList" separator=" union ">
            select #{item.pkId} pk_id, #{item.pkMember} pk_member, #{item.period} period, #{item.pkRate,jdbcType=INTEGER} pk_rate,
            #{item.directIncome} direct_income, #{item.expandIncome} expand_income, #{item.expandNoCapping} expand_no_capping,
            #{item.expandCapping} expand_capping, #{item.coachIncome} coach_income, #{item.shareIncome} share_income,
            #{item.serviceIncome} service_income, #{item.backPoints} back_points, #{item.serviceSpend} service_spend,
            #{item.purRealSubtotal} pur_real_subtotal, #{item.repurPushIncome} repur_push_income, #{item.repurRangeIncome} repur_range_income,
            #{item.repurExpandIncome} repur_expand_income, #{item.repurRealSubtotal} repur_real_subtotal,
            #{item.repurCoupon} repur_coupon, #{item.repurCouponShare} repur_coupon_share,
            #{item.repurCouponSubtotal} repur_coupon_subtotal, #{item.storeIncome} store_income, #{item.realIncomeTotal} real_income_total,
            #{item.pkCountry} pk_country,#{item.pkCreator} pk_creator,#{item.grantStatus} grant_status,#{item.incomeStatus} income_status,
            #{item.retailRangeIncome} retail_range_income, #{item.retailSameLevelIncome} retail_same_level_income, #{item.retailAreaIncome} retail_area_income,
            #{item.retailBenefitRangeIncome} retail_benefit_range_income, #{item.retailMonthRepurchaseIncome} retail_month_repurchase_income,
            #{item.retailBenefitAvgIncome} retail_benefit_avg_income, #{item.retailBenefitIncome} retail_benefit_income,
            #{item.retailRealSubtotal} retail_real_subtotal
            from dual
        </foreach>) )b
        on (cb.pk_member = b.pk_member and cb.period = b.period)
        WHEN NOT MATCHED THEN
        insert (
        pk_id, pk_member, period, pk_rate, direct_income,expand_income,expand_no_capping,
        expand_capping,coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_push_income,repur_range_income,repur_expand_income,repur_real_subtotal,
        repur_coupon,repur_coupon_share,repur_coupon_subtotal,
        store_income, real_income_total,
        pk_country, pk_creator, grant_status, income_status,
        retail_range_income, retail_same_level_income,
        retail_area_income, retail_benefit_range_income, retail_month_repurchase_income,
        retail_benefit_avg_income, retail_benefit_income, retail_real_subtotal)
        values (
        b.pk_id, b.pk_member, b.period, b.pk_rate, b.direct_income,b.expand_income,b.expand_no_capping,
        b.expand_capping,b.coach_income,b.share_income,b.service_income,b.back_points,b.service_spend,
        b.pur_real_subtotal,b.repur_push_income,b.repur_range_income,b.repur_expand_income,b.repur_real_subtotal,
        b.repur_coupon,b.repur_coupon_share,b.repur_coupon_subtotal,
        b.store_income, b.real_income_total,
        b.pk_country, b.pk_creator, b.grant_status, b.income_status,
        b.retail_range_income, b.retail_same_level_income,
        b.retail_area_income, b.retail_benefit_range_income, b.retail_month_repurchase_income,
        b.retail_benefit_avg_income, b.retail_benefit_income, b.retail_real_subtotal)
    </insert>

    <insert id="insertOrUpdateCuMemberBonus">
        merge into cu_member_bonus a
        using(
        select pk_id,pk_member,period,pk_rate,direct_income,expand_income,expand_no_capping,
        expand_capping,coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_push_income,
        repur_range_income,
        repur_expand_income,repur_real_subtotal,
        cloud_direct_income,cloud_pur_income,cloud_repur_income,cloud_real_subtotal,
        repur_coupon,repur_coupon_subtotal,global_points,
        car_award_points,hi_fun_income,hai_fun_income,
        retail_range_income, retail_same_level_income, retail_area_income,
        retail_benefit_range_income, retail_benefit_avg_income, retail_benefit_income,
        retail_real_subtotal, maker_direct_income,maker_share_income, maker_real_subtotal, real_income_total,
        pk_country, pk_creator, grant_status, income_status
        from (
        <foreach item="item" index="index" collection="cuMemberBonusList" separator=" union ">
            select #{item.pkId} pk_id,#{item.pkMember} pk_member,#{item.period} period,#{item.pkRate} pk_rate,
            #{item.directIncome} direct_income,#{item.expandIncome} expand_income,#{item.expandNoCapping} expand_no_capping,
            #{item.expandCapping} expand_capping,#{item.coachIncome} coach_income,#{item.shareIncome} share_income,
            #{item.serviceIncome} service_income,#{item.backPoints} back_points,#{item.serviceSpend} service_spend,
            #{item.purRealSubtotal} pur_real_subtotal,#{item.repurPushIncome} repur_push_income,
            #{item.repurRangeIncome} repur_range_income,
            #{item.repurExpandIncome} repur_expand_income,
            #{item.repurRealSubtotal} repur_real_subtotal,#{item.cloudDirectIncome} cloud_direct_income,
            #{item.cloudPurIncome} cloud_pur_income,#{item.cloudRepurIncome} cloud_repur_income,
            #{item.cloudRealSubtotal} cloud_real_subtotal,#{item.repurCoupon} repur_coupon,
            #{item.repurCouponSubtotal} repur_coupon_subtotal,#{item.globalPoints} global_points,
            #{item.carAwardPoints} car_award_points,#{item.hiFunIncome} hi_fun_income,
            #{item.haiFunIncome} hai_fun_income,#{item.makerDirectIncome} maker_direct_income,
            #{item.makerShareIncome} maker_share_income,#{item.makerRealSubtotal} maker_real_subtotal,
            #{item.realIncomeTotal} real_income_total,#{item.pkCountry} pk_country,#{item.pkCreator} pk_creator,
            #{item.grantStatus} grant_status, #{item.incomeStatus} income_status,
            #{item.retailRangeIncome} retail_range_income, #{item.retailSameLevelIncome} retail_same_level_income,
            #{item.retailAreaIncome} retail_area_income, #{item.retailBenefitRangeIncome} retail_benefit_range_income,
            #{item.retailBenefitAvgIncome} retail_benefit_avg_income, #{item.retailBenefitIncome} retail_benefit_income,
            #{item.retailRealSubtotal} retail_real_subtotal
            from dual
        </foreach>)
        ) b
        on (a.pk_member = b.pk_member and a.period = b.period)
        when matched then
        update set
        a.direct_income = a.direct_income + b.direct_income,a.expand_income=a.expand_income+b.expand_income,
        a.expand_no_capping=a.expand_no_capping+b.expand_no_capping,a.expand_capping=a.expand_capping+b.expand_capping,
        a.coach_income=a.coach_income+b.coach_income,a.share_income=a.share_income+b.share_income,
        a.service_income=a.service_income+b.service_income,a.back_points=a.back_points+b.back_points,a.service_spend=a.service_spend+b.service_spend,
        a.pur_real_subtotal=a.pur_real_subtotal+b.pur_real_subtotal,a.repur_push_income=a.repur_push_income+b.repur_push_income,
        a.repur_range_income = a.repur_range_income + b.repur_range_income,
        a.repur_expand_income=a.repur_expand_income+b.repur_expand_income,
        a.repur_real_subtotal=a.repur_real_subtotal+b.repur_real_subtotal,a.cloud_direct_income=a.cloud_direct_income+b.cloud_direct_income,
        a.cloud_pur_income=a.cloud_pur_income+b.cloud_pur_income,a.cloud_repur_income=a.cloud_repur_income+b.cloud_repur_income,
        a.cloud_real_subtotal=a.cloud_real_subtotal+b.cloud_real_subtotal,a.repur_coupon=a.repur_coupon+b.repur_coupon,
        a.global_points=a.global_points+b.global_points,a.car_award_points=a.car_award_points+b.car_award_points,
        a.hi_fun_income=a.hi_fun_income+b.hi_fun_income,a.real_income_total=a.real_income_total+b.real_income_total,
        a.retail_range_income = a.retail_range_income + b.retail_range_income,
        a.retail_same_level_income = a.retail_same_level_income + b.retail_same_level_income,
        a.retail_area_income = a.retail_area_income + b.retail_area_income,
        a.retail_benefit_range_income = a.retail_benefit_range_income + b.retail_benefit_range_income,
        a.retail_benefit_avg_income = a.retail_benefit_avg_income + b.retail_benefit_avg_income,
        a.retail_benefit_income = a.retail_benefit_income + b.retail_benefit_income,
        a.retail_real_subtotal = a.retail_real_subtotal + b.retail_real_subtotal
        when not matched then
        insert (
        pk_id,pk_member,period,pk_rate,direct_income,expand_income,expand_no_capping,
        expand_capping,coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_push_income,
        repur_range_income,
        repur_expand_income,repur_real_subtotal,
        cloud_direct_income,cloud_pur_income,cloud_repur_income,cloud_real_subtotal,
        repur_coupon,global_points,car_award_points,hi_fun_income,real_income_total,
        pk_country,pk_creator,grant_status,income_status,
        retail_range_income, retail_same_level_income, retail_area_income,
        retail_benefit_range_income, retail_benefit_avg_income, retail_benefit_income,
        retail_real_subtotal)
        values (
        b.pk_id,b.pk_member,b.period,b.pk_rate,b.direct_income,b.expand_income,b.expand_no_capping,
        b.expand_capping,b.coach_income,b.share_income,b.service_income,b.back_points,b.service_spend,
        b.pur_real_subtotal,b.repur_push_income,
        b.repur_range_income,
        b.repur_expand_income,b.repur_real_subtotal,
        b.cloud_direct_income,b.cloud_pur_income,b.cloud_repur_income,b.cloud_real_subtotal,
        b.repur_coupon,b.global_points,b.car_award_points,b.hi_fun_income,b.real_income_total,
        b.pk_country,b.pk_creator,b.grant_status,b.income_status,
        b.retail_range_income, b.retail_same_level_income, b.retail_area_income,
        b.retail_benefit_range_income, b.retail_benefit_avg_income, b.retail_benefit_income,
        b.retail_real_subtotal)
    </insert>

    <select id="queryMemberBonusByCondition" resultMap="CuMemberBonus">
        select cp.settle_date,
        cb.direct_income,
        cb.expand_income,
        cb.expand_no_capping,
        cb.expand_capping,
        cb.coach_income,
        cb.share_income,
        cb.service_income,
        cb.back_points,
        cb.service_spend,
        cb.pur_real_subtotal,
        cb.repur_push_income,
        cb.repur_expand_income,
        cb.repur_real_subtotal,
        cb.repur_coupon,
        cb.repur_coupon_share,
        cb.repur_coupon_subtotal,
        cb.store_income,
        cb.global_points, cb.car_award_points,
        cb.repur_range_income,
        cb.retail_range_income,
        cb.retail_same_level_income,
        cb.retail_area_income,
        cb.retail_benefit_range_income,
        cb.retail_month_repurchase_income,
        cb.retail_benefit_avg_income,
        cb.retail_benefit_income,
        cb.retail_real_subtotal,
        cb.real_income_total
        from cu_member_bonus cb
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        where cb.del_flag = 0 and cp.del_flag = 0
        and cb.pk_member = #{pkMember}
        <if test="grantStatus != null">
            and cb.grant_status >= #{grantStatus}
        </if>
        <if test="startDate != null">
            and cp.settle_date >=#{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and cp.settle_date &lt; #{endDate, jdbcType=DATE}
        </if>
        order by cp.settle_date desc
    </select>

    <select id="queryMemberBonusByConditionForServer" resultMap="CuMemberBonus">
        select cm.member_code, cm.member_name,
        cm.pk_settle_grade, cm.pk_awards,
        bv.vertex_name, ct.team_name,
        cb.period,cp.settle_date,cb.direct_income,cb.expand_income,cb.expand_no_capping,
        cb.expand_capping,cb.share_income,cb.service_income,cb.back_points,cb.service_spend,
        cb.pur_real_subtotal,cb.repur_push_income,cb.repur_expand_income,cb.repur_real_subtotal,
        cb.repur_coupon,cb.repur_coupon_share,cb.repur_coupon_subtotal,cb.store_income,
        cb.global_points,cb.car_award_points,cb.pk_rate,bc.out_exchange_rate,
        cb.coach_income,
        cb.repur_range_income,
        cb.retail_range_income, cb.retail_same_level_income, cb.retail_area_income,
        cb.retail_benefit_range_income, cb.retail_month_repurchase_income,
        cb.retail_benefit_avg_income, cb.retail_benefit_income,cb.retail_real_subtotal,
        cb.real_income_total,
        cm.pk_vertex, cm.service_code, cm.nick_name
        from cu_member_bonus cb
        inner join cu_member cm
        on cb.pk_member = cm.pk_id
        and cm.del_flag = 0
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        and cp.del_flag = 0
        left join bd_vertex bv
        on bv.pk_id = cm.pk_vertex
        left join cu_member_team ct
        on ct.pk_id = cm.pk_team_code
        left join bd_currency bc
        on bc.pk_country = cb.pk_country
        where cb.del_flag = 0
        <if test="bonusParam.pkCountry != null">
            and cm.pk_settle_country = #{bonusParam.pkCountry}
        </if>
        <if test="bonusParam.systemType != null">
            and cm.system_type = #{bonusParam.systemType}
        </if>
        <if test="bonusParam.memberCode != null and bonusParam.memberCode != ''">
            and cm.member_code = #{bonusParam.memberCode}
        </if>
        <if test="bonusParam.memberName != null and bonusParam.memberName != ''">
            and cm.member_name = #{bonusParam.memberName}
        </if>
        <if test="bonusParam.pkVertex != null and bonusParam.pkVertex != ''">
            and cm.pk_vertex = #{bonusParam.pkVertex}
        </if>
        <if test="bonusParam.pkTeamCode != null and bonusParam.pkTeamCode != ''">
            and cm.pk_team_code = #{bonusParam.pkTeamCode}
        </if>
        <if test="bonusParam.startDate != null">
            and cp.settle_date >=#{bonusParam.startDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.endDate != null">
            and cp.settle_date &lt; #{bonusParam.endDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.period != null">
            and cb.period >=#{bonusParam.period}
        </if>
        <if test="bonusParam.pkVertexList!=null and bonusParam.pkVertexList.size> 0 ">
            and cm.PK_VERTEX in
            <foreach collection="bonusParam.pkVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="bonusParam.vertexList != null and bonusParam.vertexList.size >0">
            and exists(
            select 1 from (
            <foreach collection="bonusParam.vertexList" item="item" separator=" union ">
                select pk_id from cu_member
                where del_flag=0 and PAY_STATUS=1
                start with pk_id = #{item}
                connect by pk_parent = prior pk_id
            </foreach>
            <if test="bonusParam.teamList != null and bonusParam.teamList.size >0">
                union
                <foreach collection="bonusParam.teamList" item="item" separator=" union ">
                    select pk_id from cu_member
                    where del_flag=0 and PAY_STATUS=1
                    start with pk_id = #{item}
                    connect by pk_parent = prior pk_id
                </foreach>
            </if>
            ) a
            where a.pk_id = cb.pk_member
            )
        </if>
        order by cb.period desc
    </select>

    <select id="queryMemberBonusByConditionForServerSum" resultMap="CuMemberBonus">
        select
        nvl(sum(cb.direct_income),0) direct_income,
        nvl(sum(cb.expand_income),0) expand_income,
        nvl(sum(cb.expand_no_capping),0) expand_no_capping,
        nvl(sum(cb.expand_capping),0) expand_capping,
        nvl(sum(cb.coach_income),0) coach_income,
        nvl(sum(cb.share_income),0) share_income,
        nvl(sum(cb.service_income),0) service_income,
        nvl(sum(cb.back_points),0) back_points,
        nvl(sum(cb.service_spend),0) service_spend,
        nvl(sum(cb.pur_real_subtotal),0) pur_real_subtotal,
        nvl(sum(cb.repur_push_income),0) repur_push_income,
        nvl(sum(cb.repur_expand_income),0) repur_expand_income,
        nvl(sum(cb.repur_real_subtotal),0) repur_real_subtotal,
        nvl(sum(cb.repur_coupon),0) repur_coupon,
        nvl(sum(cb.repur_coupon_share),0) repur_coupon_share,
        nvl(sum(cb.repur_coupon_subtotal),0) repur_coupon_subtotal,
        nvl(sum(cb.store_income),0) store_income,
        nvl(sum(cb.global_points),0) global_points,
        nvl(sum(cb.car_award_points),0) car_award_points,
        bc.out_exchange_rate,
        nvl(sum(cb.repur_range_income),0) repur_range_income,
        nvl(sum(cb.retail_range_income),0) retail_range_income,
        nvl(sum(cb.retail_same_level_income),0) retail_same_level_income,
        nvl(sum(cb.retail_area_income),0) retail_area_income,
        nvl(sum(cb.retail_benefit_range_income),0) retail_benefit_range_income,
        nvl(sum(cb.retail_month_repurchase_income),0) retail_month_repurchase_income,
        nvl(sum(cb.retail_benefit_avg_income),0) retail_benefit_avg_income,
        nvl(sum(cb.retail_benefit_income),0) retail_benefit_income,
        nvl(sum(cb.retail_real_subtotal),0) retail_real_subtotal,
        nvl(sum(cb.real_income_total),0) real_income_total
        from cu_member_bonus cb
        inner join cu_member cm
        on cb.pk_member = cm.pk_id
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        left join bd_vertex bv
        on bv.pk_id = cm.pk_vertex
        left join cu_member_team ct
        on ct.pk_id = cm.pk_team_code
        left join bd_currency bc
        on bc.pk_country = cb.pk_country
        where cb.del_flag = 0 and cm.del_flag = 0
        and cp.del_flag = 0
        <if test="bonusParam.pkCountry != null">
            and cm.pk_settle_country = #{bonusParam.pkCountry}
        </if>
        <if test="bonusParam.memberCode != null and bonusParam.memberCode != ''">
            and cm.member_code = #{bonusParam.memberCode}
        </if>
        <if test="bonusParam.memberName != null and bonusParam.memberName != ''">
            and cm.member_name = #{bonusParam.memberName}
        </if>
        <if test="bonusParam.pkVertex != null and bonusParam.pkVertex != ''">
            and cm.pk_vertex = #{bonusParam.pkVertex}
        </if>
        <if test="bonusParam.pkTeamCode != null and bonusParam.pkTeamCode != ''">
            and cm.pk_team_code = #{bonusParam.pkTeamCode}
        </if>
        <if test="bonusParam.startDate != null">
            and cp.settle_date >=#{bonusParam.startDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.endDate != null">
            and cp.settle_date &lt; #{bonusParam.endDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.period != null">
            and cb.period >=#{bonusParam.period}
        </if>
        <if test="bonusParam.pkVertexList!=null and bonusParam.pkVertexList.size> 0 ">
            and cm.PK_VERTEX in
            <foreach collection="bonusParam.pkVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="bonusParam.vertexList != null and bonusParam.vertexList.size >0">
            and exists(
            select 1 from (
            <foreach collection="bonusParam.vertexList" item="item" separator=" union ">
                select pk_id from cu_member
                where del_flag=0 and PAY_STATUS=1
                start with pk_id = #{item}
                connect by pk_parent = prior pk_id
            </foreach>
            <if test="bonusParam.teamList != null and bonusParam.teamList.size >0">
                union
                <foreach collection="bonusParam.teamList" item="item" separator=" union ">
                    select pk_id from cu_member
                    where del_flag=0 and PAY_STATUS=1
                    start with pk_id = #{item}
                    connect by pk_parent = prior pk_id
                </foreach>
            </if>
            ) a
            where a.pk_id = cb.pk_member
            )
        </if>
        GROUP BY bc.out_exchange_rate
    </select>

    <select id="queryBonusTotalVoByConditionForServer" resultMap="CuMemberBonus">
        select cb.period,cp.settle_date,
        nvl(min(bc.out_exchange_rate),1) out_exchange_rate,sum(cb.direct_income) direct_income,
        sum(cb.expand_income) expand_income, sum(cb.expand_no_capping) expand_no_capping,
        sum(cb.expand_capping) expand_capping,sum(cb.coach_income) coach_income,
        sum(cb.share_income) share_income, sum(cb.service_spend) service_spend,
        sum(cb.service_income) service_income, sum(cb.back_points) back_points,
        sum(cb.pur_real_subtotal) pur_real_subtotal,
        sum(cb.repur_push_income) repur_push_income,
        sum(cb.repur_expand_income) repur_expand_income, sum(cb.repur_real_subtotal) repur_real_subtotal,
        sum(cb.repur_coupon) repur_coupon, sum(repur_coupon_share) repur_coupon_share,
        sum(cb.repur_coupon_subtotal) repur_coupon_subtotal,
        sum(cb.global_points) global_points, sum(cb.car_award_points) car_award_points,
        sum(cb.repur_range_income) repur_range_income,
        sum(cb.retail_range_income) retail_range_income,
        sum(cb.retail_same_level_income) retail_same_level_income,
        sum(cb.retail_area_income) retail_area_income,
        sum(cb.retail_benefit_range_income) retail_benefit_range_income,
        sum(cb.retail_month_repurchase_income) retail_month_repurchase_income,
        sum(cb.retail_benefit_avg_income) retail_benefit_avg_income,
        sum(cb.retail_benefit_income) retail_benefit_income,
        sum(cb.retail_real_subtotal) retail_real_subtotal,
        sum(cb.real_income_total) real_income_total
        from cu_member_bonus cb
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        left join bd_currency bc
        on bc.pk_id = cb.pk_rate
        inner join cu_member cm on cb.PK_MEMBER=cm.pk_id
        where cb.del_flag=0 and cp.del_flag=0
        <if test="bonusParam.pkCountry != null">
            and cb.pk_country = #{bonusParam.pkCountry}
        </if>
        <if test="bonusParam.systemType != null">
            and cm.system_type = #{bonusParam.systemType}
        </if>
        <if test="bonusParam.startDate != null">
            and cp.settle_date >=#{bonusParam.startDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.endDate != null">
            and cp.settle_date &lt;= #{bonusParam.endDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.period != null">
            and cb.period =#{bonusParam.period}
        </if>
        <if test="bonusParam.pkVertexList!=null and bonusParam.pkVertexList.size> 0 ">
            and cm.PK_VERTEX in
            <foreach collection="bonusParam.pkVertexList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="bonusParam.vertexList != null and bonusParam.vertexList.size >0">
            and exists(
            select 1 from (
            <foreach collection="bonusParam.vertexList" item="item" separator=" union ">
                select pk_id from cu_member
                where del_flag=0 and PAY_STATUS=1
                start with pk_id = #{item}
                connect by pk_parent = prior pk_id
            </foreach>
            <if test="bonusParam.teamList != null and bonusParam.teamList.size >0">
                union
                <foreach collection="bonusParam.teamList" item="item" separator=" union ">
                    select pk_id from cu_member
                    where del_flag=0 and PAY_STATUS=1
                    start with pk_id = #{item}
                    connect by pk_parent = prior pk_id
                </foreach>
            </if>
            ) a
            where a.pk_id = cb.pk_member
            )
        </if>
        group by cb.period,cp.settle_date
        order by cb.period desc
    </select>

    <select id="batchQueryCuMemberBonusSeq" resultType="java.lang.Long">
        select cu_member_bonus_seq.nextval
        from dual connect by rownum &lt;= #{rowNum}
    </select>

    <update id="recalculateTeamNewBoxNum">
        MERGE INTO ${retailsTableName} s
            USING (
                SELECT SUM(boxNum) AS boxNum, SUM(pv) AS pv, pk_id
                FROM (
                         SELECT b.PK_ID,
                                NVL(s.NEW_BOX_NUM, 0) AS boxNum,
                                NVL(s.NEW_CONSUME_PV, 0) AS pv
                         FROM ${retailsTableName} s
                                  RIGHT JOIN (
                                     SELECT cm.PK_ID
                                     FROM cu_member cm
                                     WHERE cm.PK_ID != #{pkId}
                                     START WITH cm.PK_ID = #{pkId}
                                     CONNECT BY PRIOR cm.pk_id = cm.pk_parent
                                     ORDER BY LEVEL
                                 ) b ON s.PK_MEMBER = b.PK_ID
                     ) c
                GROUP BY pk_id
            ) result
            ON (s.PK_MEMBER = result.PK_ID)
            WHEN MATCHED THEN
                UPDATE SET
                    s.ENABLE_STATUS = 0,
                    s.TEAM_NEW_BOX_NUM = result.boxNum,
                    s.TEAM_NEW_PV = result.pv
            WHERE s.PK_MEMBER = #{pkId}
    </update>
</mapper>
