<?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.achieve.mapper.CuMemberRetailRangeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberRetailRangeExt" type="com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt">
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_PARENT" property="pkParent"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="CUSTOMER_TYPE" property="customerType"/>
        <result column="PHONE" property="phone"/>
        <result column="PK_GRADE" property="pkGrade"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="PK_SHARE_AWARDS" property="pkShareAwards"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus"/>
        <result column="PAY_TIME" property="payTime"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="IS_REAL_NAME" property="isRealName"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="NEW_CONSUME_PV" property="newConsumePv"/>
        <result column="CONSUME_PV" property="consumePv"/>
        <result column="MONTH_CONSUME_PV" property="monthConsumePv"/>
        <result column="NEW_CONSUME_AMOUNT" property="newConsumeAmount"/>
        <result column="CONSUME_AMOUNT" property="consumeAmount"/>
        <result column="MONTH_CONSUME_AMOUNT" property="monthConsumeAmount"/>
        <result column="TEAM_NEW_PV" property="teamNewPv"/>
        <result column="TEAM_CONSUME_PV" property="teamConsumePv"/>
        <result column="TEAM_MONTH_PV" property="teamMonthPv"/>
        <result column="TEAM_NEW_AMOUNT" property="teamNewAmount"/>
        <result column="TEAM_CONSUME_AMOUNT" property="teamConsumeAmount"/>
        <result column="TEAM_MONTH_AMOUNT" property="teamMonthAmount"/>
        <result column="RECOMMEND_NUM" property="recommendNum"/>
        <result column="TEAM_NUM" property="teamNum"/>
        <result column="big_team_pv" property="bigTeamPv"/>
        <result column="small_team_pv" property="smallTeamPv"/>
        <result column="reg_consume_new_amount" property="regConsumeNewAmount"/>
        <result column="reg_consume_new_pv" property="regConsumeNewPv"/>
        <result column="reg_team_new_amount" property="regTeamNewAmount"/>
        <result column="reg_team_new_pv" property="regTeamNewPv"/>
        <result column="rep_consume_new_amount" property="repConsumeNewAmount"/>
        <result column="rep_consume_new_pv" property="repConsumeNewPv"/>
        <result column="rep_team_new_amount" property="repTeamNewAmount"/>
        <result column="rep_team_new_pv" property="repTeamNewPv"/>
        <result column="point_count" property="pointCount"/>

        <result column="AWARDS_VALUE" property="awardsValue"/>
        <result column="AWARDS_NAME" property="awardsName"/>
        <result column="RANGE_RATIO" property="rangeRatio"/>
        <result column="MONTH_REPURCHASE_RATIO" property="monthRepurchaseRatio"/>
        <result column="BENEFIT_AVG_RATIO" property="benefitAvgRatio"/>
        <result column="BENEFIT_SHARE_RATIO" property="benefitShareRatio"/>
        <result column="COACH_ALGEBRA" property="coachAlgebra"/>
        <result column="COACH_RATIO" property="coachRatio"/>

        <result column="all_team_new_pv" property="allTeamNewPv"/>
        <result column="all_team_consume_pv" property="allTeamConsumePv"/>
        <result column="all_team_month_pv" property="allTeamMonthPv"/>
    </resultMap>

    <update id="mergeMemberRetailRangeInit">
        update ${rangeTableName}
        set pk_grade               = (select pk_id from bd_grade where del_flag = 0 and grade_value = 30),
            new_consume_pv         = 0,
            consume_pv             = 0,
            month_consume_pv       = 0,
            new_consume_amount     = 0,
            consume_amount         = 0,
            month_consume_amount   = 0,
            team_new_pv            = 0,
            team_consume_pv        = 0,
            team_month_pv          = 0,
            team_new_amount        = 0,
            team_consume_amount    = 0,
            team_month_amount      = 0,
            recommend_num          = 0,
            team_num               = 0,
            big_team_pv            = 0,
            small_team_pv          = 0,
            reg_consume_new_amount = 0,
            reg_consume_new_pv     = 0,
            reg_team_new_amount    = 0,
            reg_team_new_pv        = 0,
            rep_consume_new_amount = 0,
            rep_consume_new_pv     = 0,
            rep_team_new_amount    = 0,
            rep_team_new_pv        = 0
    </update>

    <update id="mergeMemberRetailRangeByYesterday">
        merge into ${rangeTableName} a
        using(
        select pk_member, pk_grade, pk_awards, pk_share_awards,
        consume_pv, month_consume_pv,
        consume_amount, month_consume_amount,
        team_consume_pv, team_month_pv,
        team_consume_amount, team_month_amount,
        recommend_num, team_num, big_team_pv, small_team_pv
        from ${yesterdayRangeTableName}
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.pk_grade = b.pk_grade,
        a.consume_pv = b.consume_pv,
        a.consume_amount = b.consume_amount,
        a.team_consume_pv = b.team_consume_pv,
        a.team_consume_amount = b.team_consume_amount,
        a.recommend_num = b.recommend_num, a.team_num = b.team_num,
        a.big_team_pv = b.big_team_pv, a.small_team_pv = b.small_team_pv
        <if test="isSameMonth == 0">
            ,a.month_consume_pv = b.month_consume_pv
            ,a.month_consume_amount = b.month_consume_amount
            ,a.team_month_pv = b.team_month_pv
            ,a.team_month_amount = b.team_month_amount
        </if>
    </update>

    <!-- 将昨天的结算明细迁移到今天 -->
    <update id="mergeMemberRetailRangeDetailByYesterday">
        merge into ${detailTableName} a
            using (
                select x.pk_member,
                       x.child_node,
                       cm.pk_parent,
                       x.stage,
                       x.stage_status,
                       x.stage_date,
                       x.point_11,
                       x.point_12,
                       x.point_21,
                       x.point_22,
                       x.point_23,
                       x.point_24,
                       x.point_31,
                       x.point_32,
                       x.point_33,
                       x.point_34,
                       x.point_35,
                       x.point_36,
                       x.point_37,
                       x.point_38,
                       x.point_member_11,
                       x.point_member_12,
                       x.point_member_21,
                       x.point_member_22,
                       x.point_member_23,
                       x.point_member_24,
                       x.point_member_31,
                       x.point_member_32,
                       x.point_member_33,
                       x.point_member_34,
                       x.point_member_35,
                       x.point_member_36,
                       x.point_member_37,
                       x.point_member_38,
                       x.point_type,
                       x.point_gift,
                       x.creation_time,
                       x.point_first,
                       x.point_member_first,
                       x.point_second,
                       x.point_member_second,
                       x.point_third,
                       x.point_member_third
                from ${yesterdayDetailTableName} x
                         left join cu_member cm
                                   on cm.pk_id = x.pk_member
            ) b
            on (a.pk_member = b.pk_member and a.child_node = b.child_node)
            when not matched then
                insert (pk_member,
                        child_node,
                        pk_parent,
                        stage,
                        stage_status,
                        stage_date,
                        point_11,
                        point_12,
                        point_21,
                        point_22,
                        point_23,
                        point_24,
                        point_31,
                        point_32,
                        point_33,
                        point_34,
                        point_35,
                        point_36,
                        point_37,
                        point_38,
                        point_member_11,
                        point_member_12,
                        point_member_21,
                        point_member_22,
                        point_member_23,
                        point_member_24,
                        point_member_31,
                        point_member_32,
                        point_member_33,
                        point_member_34,
                        point_member_35,
                        point_member_36,
                        point_member_37,
                        point_member_38,
                        point_type,
                        point_gift,
                        creation_time,
                        point_first,
                        point_member_first,
                        point_second,
                        point_member_second,
                        point_third,
                        point_member_third)
                    values (b.pk_member,
                            b.child_node,
                            b.pk_parent,
                            b.stage,
                            b.stage_status,
                            b.stage_date,
                            b.point_11,
                            b.point_12,
                            b.point_21,
                            b.point_22,
                            b.point_23,
                            b.point_24,
                            b.point_31,
                            b.point_32,
                            b.point_33,
                            b.point_34,
                            b.point_35,
                            b.point_36,
                            b.point_37,
                            b.point_38,
                            b.point_member_11,
                            b.point_member_12,
                            b.point_member_21,
                            b.point_member_22,
                            b.point_member_23,
                            b.point_member_24,
                            b.point_member_31,
                            b.point_member_32,
                            b.point_member_33,
                            b.point_member_34,
                            b.point_member_35,
                            b.point_member_36,
                            b.point_member_37,
                            b.point_member_38,
                            b.point_type,
                            b.point_gift,
                            b.creation_time,
                            b.point_first,
                            b.point_member_first,
                            b.point_second,
                            b.point_member_second,
                            b.point_third,
                            b.point_member_third)
            when matched then
                update set
                    a.pk_parent = b.pk_parent,
                    a.stage = b.stage,
                    a.stage_status = b.stage_status,
                    a.stage_date = b.stage_date,
                    a.point_11 = b.point_11,
                    a.point_12 = b.point_12,
                    a.point_21 = b.point_21,
                    a.point_22 = b.point_22,
                    a.point_23 = b.point_23,
                    a.point_24 = b.point_24,
                    a.point_31 = b.point_31,
                    a.point_32 = b.point_32,
                    a.point_33 = b.point_33,
                    a.point_34 = b.point_34,
                    a.point_35 = b.point_35,
                    a.point_36 = b.point_36,
                    a.point_37 = b.point_37,
                    a.point_38 = b.point_38,
                    a.point_member_11 = b.point_member_11,
                    a.point_member_12 = b.point_member_12,
                    a.point_member_21 = b.point_member_21,
                    a.point_member_22 = b.point_member_22,
                    a.point_member_23 = b.point_member_23,
                    a.point_member_24 = b.point_member_24,
                    a.point_member_31 = b.point_member_31,
                    a.point_member_32 = b.point_member_32,
                    a.point_member_33 = b.point_member_33,
                    a.point_member_34 = b.point_member_34,
                    a.point_member_35 = b.point_member_35,
                    a.point_member_36 = b.point_member_36,
                    a.point_member_37 = b.point_member_37,
                    a.point_member_38 = b.point_member_38,
                    a.point_type = b.point_type,
                    a.point_gift = b.point_gift,
                    a.creation_time = b.creation_time,
                    a.point_first = b.point_first,
                    a.point_member_first = b.point_member_first,
                    a.point_second = b.point_second,
                    a.point_member_second = b.point_member_second,
                    a.point_third = b.point_third,
                    a.point_member_third = b.point_member_third
    </update>

    <!-- 更新会员日结数据 -->
    <update id="mergeCuMemberRetailRangeExt">
        merge into ${rangeTableName} a
        using (
        <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
            select #{item.pkMember} pk_member, #{item.pkAwards} pk_awards, #{item.pkShareAwards} pk_share_awards,
            #{item.newConsumePv} new_consume_pv, #{item.consumePv} consume_pv, #{item.monthConsumePv} month_consume_pv,
            #{item.newConsumeAmount} new_consume_amount, #{item.consumeAmount} consume_amount, #{item.monthConsumeAmount} month_consume_amount,
            #{item.teamNewPv} team_new_pv, #{item.teamConsumePv} team_consume_pv, #{item.teamMonthPv} team_month_pv,
            #{item.teamNewAmount} team_new_amount, #{item.teamConsumeAmount} team_consume_amount, #{item.teamMonthAmount} team_month_amount,
            #{item.recommendNum} recommend_num, #{item.teamNum} team_num,
            #{item.regConsumeNewAmount} reg_consume_new_amount, #{item.regConsumeNewPv} reg_consume_new_pv,
            #{item.regTeamNewAmount} reg_team_new_amount, #{item.regTeamNewPv} reg_team_new_pv,
            #{item.repConsumeNewAmount} rep_consume_new_amount, #{item.repConsumeNewPv} rep_consume_new_pv,
            #{item.repTeamNewAmount} rep_team_new_amount, #{item.repTeamNewPv} rep_team_new_pv
            from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.pk_awards = b.pk_awards, a.pk_share_awards = b.pk_share_awards,
        a.new_consume_pv = b.new_consume_pv, a.consume_pv = b.consume_pv, a.month_consume_pv = b.month_consume_pv,
        a.new_consume_amount = b.new_consume_amount, a.consume_amount = b.consume_amount, a.month_consume_amount = b.month_consume_amount,
        a.team_new_pv = b.team_new_pv, a.team_consume_pv = b.team_consume_pv, a.team_month_pv = b.team_month_pv,
        a.team_new_amount = b.team_new_amount, a.team_consume_amount = b.team_consume_amount, a.team_month_amount = b.team_month_amount,
        a.recommend_num = b.recommend_num, a.team_num = b.team_num,
        a.reg_consume_new_amount = b.reg_consume_new_amount, a.reg_consume_new_pv = b.reg_consume_new_pv,
        a.reg_team_new_amount = b.reg_team_new_amount, a.reg_team_new_pv = b.reg_team_new_pv,
        a.rep_consume_new_amount = b.rep_consume_new_amount, a.rep_consume_new_pv = b.rep_consume_new_pv,
        a.rep_team_new_amount = b.rep_team_new_amount, a.rep_team_new_pv = b.rep_team_new_pv
    </update>

    <!-- 更新会员日结数据 -->
    <update id="mergeCuMemberRetailDetail">
        merge into ${rangeDetailTableName} a
        using (
        <foreach collection="cuMemberDetailList" item="item" open=" " close=" " separator=" union ">
            select
            #{item.pkMember} pk_member, #{item.childNode} child_node, #{item.pkParent} pk_parent,
            #{item.stage} stage, #{item.stageStatus} stage_status, #{item.stageDate, jdbcType=TIMESTAMP} stage_date,
            #{item.point11, jdbcType=NUMERIC} point_11, #{item.point12, jdbcType=NUMERIC} point_12,
            #{item.point21, jdbcType=NUMERIC} point_21, #{item.point22, jdbcType=NUMERIC} point_22,
            #{item.point23, jdbcType=NUMERIC} point_23, #{item.point24, jdbcType=NUMERIC} point_24,
            #{item.point31, jdbcType=NUMERIC} point_31, #{item.point32, jdbcType=NUMERIC} point_32,
            #{item.point33, jdbcType=NUMERIC} point_33, #{item.point34, jdbcType=NUMERIC} point_34,
            #{item.point35, jdbcType=NUMERIC} point_35, #{item.point36, jdbcType=NUMERIC} point_36,
            #{item.point37, jdbcType=NUMERIC} point_37, #{item.point38, jdbcType=NUMERIC} point_38,
            #{item.pointMember11, jdbcType=NUMERIC} point_member_11, #{item.pointMember12, jdbcType=NUMERIC} point_member_12,
            #{item.pointMember21, jdbcType=NUMERIC} point_member_21, #{item.pointMember22, jdbcType=NUMERIC} point_member_22,
            #{item.pointMember23, jdbcType=NUMERIC} point_member_23, #{item.pointMember24, jdbcType=NUMERIC} point_member_24,
            #{item.pointMember31, jdbcType=NUMERIC} point_member_31, #{item.pointMember32, jdbcType=NUMERIC} point_member_32,
            #{item.pointMember33, jdbcType=NUMERIC} point_member_33, #{item.pointMember34, jdbcType=NUMERIC} point_member_34,
            #{item.pointMember35, jdbcType=NUMERIC} point_member_35, #{item.pointMember36, jdbcType=NUMERIC} point_member_36,
            #{item.pointMember37, jdbcType=NUMERIC} point_member_37, #{item.pointMember38, jdbcType=NUMERIC} point_member_38,
            #{item.pointType} point_type, #{item.pointGift} point_gift,
            #{item.creationTime, jdbcType=TIMESTAMP} creation_time,
            #{item.pointFirst, jdbcType=NUMERIC} point_first, #{item.pointMemberFirst, jdbcType=NUMERIC} point_member_first,
            #{item.pointSecond, jdbcType=NUMERIC} point_second, #{item.pointMemberSecond, jdbcType=NUMERIC} point_member_second,
            #{item.pointThird, jdbcType=NUMERIC} point_third, #{item.pointMemberThird, jdbcType=NUMERIC} point_member_third
            from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member and a.child_node = b.child_node)
        when not matched then
        insert (pk_member, child_node, pk_parent,
        stage, stage_status, stage_date,
        point_11, point_12,
        point_21, point_22,
        point_23, point_24,
        point_31, point_32,
        point_33, point_34,
        point_35, point_36,
        point_37, point_38,
        point_member_11, point_member_12,
        point_member_21, point_member_22,
        point_member_23, point_member_24,
        point_member_31, point_member_32,
        point_member_33, point_member_34,
        point_member_35, point_member_36,
        point_member_37, point_member_38,
        point_type, point_gift,
        creation_time,
        point_first, point_member_first,
        point_second, point_member_second,
        point_third, point_member_third)
        values (b.pk_member, b.child_node, b.pk_parent,
        b.stage, b.stage_status, b.stage_date,
        b.point_11, b.point_12,
        b.point_21, b.point_22,
        b.point_23, b.point_24,
        b.point_31, b.point_32,
        b.point_33, b.point_34,
        b.point_35, b.point_36,
        b.point_37, b.point_38,
        b.point_member_11, b.point_member_12,
        b.point_member_21, b.point_member_22,
        b.point_member_23, b.point_member_24,
        b.point_member_31, b.point_member_32,
        b.point_member_33, b.point_member_34,
        b.point_member_35, b.point_member_36,
        b.point_member_37, b.point_member_38,
        b.point_type, b.point_gift,
        b.creation_time,
        b.point_first, b.point_member_first,
        b.point_second, b.point_member_second,
        b.point_third, b.point_member_third)
        when matched then
        update set
        a.stage = b.stage, a.stage_status = b.stage_status, a.stage_date = b.stage_date,
        a.point_11 = b.point_11, a.point_12 = b.point_12,
        a.point_21 = b.point_21, a.point_22 = b.point_22,
        a.point_23 = b.point_23, a.point_24 = b.point_24,
        a.point_31 = b.point_31, a.point_32 = b.point_32,
        a.point_33 = b.point_33, a.point_34 = b.point_34,
        a.point_35 = b.point_35, a.point_36 = b.point_36,
        a.point_37 = b.point_37, a.point_38 = b.point_38,
        a.point_member_11 = b.point_member_11, a.point_member_12 = b.point_member_12,
        a.point_member_21 = b.point_member_21, a.point_member_22 = b.point_member_22,
        a.point_member_23 = b.point_member_23, a.point_member_24 = b.point_member_24,
        a.point_member_31 = b.point_member_31, a.point_member_32 = b.point_member_32,
        a.point_member_33 = b.point_member_33, a.point_member_34 = b.point_member_34,
        a.point_member_35 = b.point_member_35, a.point_member_36 = b.point_member_36,
        a.point_member_37 = b.point_member_37, a.point_member_38 = b.point_member_38,
        a.point_type = b.point_type, a.point_gift = b.point_gift,
        a.creation_time = b.creation_time,
        a.point_first = b.point_first, a.point_member_first = b.point_member_first,
        a.point_second = b.point_second, a.point_member_second = b.point_member_second,
        a.point_third = b.point_third, a.point_member_third = b.point_member_third
    </update>

    <!-- 当前网体更新极差秒接表 -->
    <update id="mergeMemberSecondRetailRangeByRange">
        merge into ${secondRangeTableName} a
            using (
                select a.pk_id           pk_member,
                       a.pk_parent,
                       a.pk_settle_grade pk_grade,
                       a.pk_awards,
                       a.pk_range_awards,
                       a.system_type
                from (select *
                      from cu_member
                      where del_flag = 0
                          start
                      with pk_id = #{pkMember}
                      connect by pk_id = prior pk_parent
                     ) a
            ) b
            on (a.pk_member = b.pk_member)
            when not matched then
                insert (pk_member, pk_parent, pk_grade, pk_awards, pk_share_awards, system_type)
                    values (b.pk_member, b.pk_parent, b.pk_grade, b.pk_awards, b.pk_range_awards, b.system_type)
    </update>

    <!-- 更新极差秒接表，用昨天的结算表 -->
    <update id="mergeMemberSecondRetailRangeByRangeEnable">
        merge into ${secondRangeTableName} a
            using (
                select a.pk_id           pk_member,
                       a.pk_parent,
                       a.pk_settle_grade pk_grade,
                       a.pk_awards,
                       a.pk_range_awards
                from (select *
                      from cu_member
                      where del_flag = 0
                          start
                      with pk_id = #{pkMember}
                      connect by pk_id = prior pk_parent
                     ) a
                         left join ${rangeTableName} b
                                   on a.pk_id = b.pk_member
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.pk_grade = b.pk_grade
    </update>

    <!-- 更新新零售极差秒接表 -->
    <update id="mergeCuMemberSecondRange">
        merge into ${secondTableName} a
        using (
        <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
            select
            #{item.pkMember} pk_member, #{item.pkGrade} pk_grade,
            #{item.pkAwards} pk_awards, #{item.pkShareAwards} pk_share_awards,
            #{item.newConsumePv} new_consume_pv,
            #{item.teamNewPv} team_new_pv,
            #{item.teamNewAmount} team_new_amount
            from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.pk_grade = b.pk_grade, a.pk_awards = b.pk_awards, a.pk_share_awards = b.pk_share_awards,
        a.new_consume_pv = b.new_consume_pv,
        a.team_new_pv = b.team_new_pv,
        a.team_new_amount = b.team_new_amount
    </update>

    <update id="mergeMemberRetailGradeByMember">
        merge into ${rangeTableName} a
        using (
        select pk_id pk_member, pk_parent,
        pk_settle_grade, pk_awards, pk_range_awards
        from cu_member start with pk_id = #{pkMember}
        connect by prior pk_parent = pk_id
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.pk_grade = b.pk_settle_grade, a.pk_awards = b.pk_awards
        <if test="isToday == 0">
            , a.pk_share_awards = b.pk_range_awards
        </if>
    </update>
    <update id="updateMemberRetailSecondRangeSelf">
        UPDATE ${tableName}
        SET
        PK_GRADE = ${cuMember.pkSettleGrade},
        PK_AWARDS = ${cuMember.pkAwards},
        <if test="updateType != null and updateType == 1">
            REG_CONSUME_NEW_AMOUNT = REG_CONSUME_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
            REG_CONSUME_NEW_PV = REG_CONSUME_NEW_PV ${symbol} #{orderExt.orderAchieve},
        </if>
        <if test="updateType != null and updateType == 2">
            REP_CONSUME_NEW_AMOUNT = REP_CONSUME_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
            REP_CONSUME_NEW_PV = REP_CONSUME_NEW_PV ${symbol} #{orderExt.orderAchieve},
        </if>
        ALL_CONSUME_NEW_AMOUNT = ALL_CONSUME_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
        ALL_CONSUME_NEW_PV = ALL_CONSUME_NEW_PV ${symbol} #{orderExt.orderAchieve}
        WHERE
        PK_MEMBER = #{cuMember.pkId}
    </update>
    <update id="updateMemberRetailSecondRangeParent">
        UPDATE ${tableName}
        SET
        <if test="updateType != null and updateType == 1">
            REG_TEAM_NEW_AMOUNT = REG_TEAM_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
            REG_TEAM_NEW_PV = REG_TEAM_NEW_PV ${symbol} #{orderExt.orderAchieve},
        </if>
        <if test="updateType != null and updateType == 2">
            REP_TEAM_NEW_AMOUNT = REP_TEAM_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
            REP_TEAM_NEW_PV = REP_TEAM_NEW_PV ${symbol} #{orderExt.orderAchieve},
        </if>
        ALL_TEAM_NEW_AMOUNT = ALL_TEAM_NEW_AMOUNT ${symbol} #{orderExt.orderAmount},
        ALL_TEAM_NEW_PV = ALL_TEAM_NEW_PV ${symbol} #{orderExt.orderAchieve}
        WHERE
        PK_MEMBER in
        <foreach collection="memberList" item="member" separator="," open="(" close=")">
            #{member.pkId}
        </foreach>
    </update>
    <insert id="initMemberRetailSecondRange">
            INSERT INTO ${tableName} (
            "PK_MEMBER",
            "PK_PARENT",
            "PK_GRADE",
            "PK_AWARDS",
            "ALL_CONSUME_NEW_AMOUNT",
            "ALL_CONSUME_NEW_PV",
            "ALL_TEAM_NEW_AMOUNT",
            "ALL_TEAM_NEW_PV",
            "REG_CONSUME_NEW_AMOUNT",
            "REG_CONSUME_NEW_PV",
            "REG_TEAM_NEW_AMOUNT",
            "REG_TEAM_NEW_PV",
            "REP_CONSUME_NEW_AMOUNT",
            "REP_CONSUME_NEW_PV",
            "REP_TEAM_NEW_AMOUNT",
            "REP_TEAM_NEW_PV"
            )
            SELECT
            #{member.pkId},
            #{member.pkParent},
            #{member.pkSettleGrade},
            #{member.pkAwards},
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
            FROM DUAL
            WHERE NOT EXISTS (
            SELECT 1
            FROM ${tableName}
            WHERE "PK_MEMBER" = #{member.pkId}
            )
    </insert>

    <select id="queryCuMemberRetailRangeParent" resultMap="CuMemberRetailRangeExt">
        <foreach collection="saOrderExtList" item="item" close=" " open=" " separator="union">
            select
            a.pk_member,
            a.pk_parent,
            a.member_code,
            a.member_name,
            a.pk_grade,
            a.pk_awards,
            a.pk_share_awards,
            a.account_status,
            a.pay_time,
            a.category,
            a.income_status,
            a.pk_vertex,
            a.pk_settle_country,
            a.pk_rate,
            new_consume_pv,
            consume_pv,
            month_consume_pv,
            new_consume_amount,
            consume_amount,
            month_consume_amount,
            team_new_pv,
            team_consume_pv,
            team_month_pv,
            team_new_amount,
            team_consume_amount,
            team_month_amount,
            recommend_num,
            team_num,
            big_team_pv,
            small_team_pv,
            reg_consume_new_amount,
            reg_consume_new_pv,
            reg_team_new_amount,
            reg_team_new_pv,
            rep_consume_new_amount,
            rep_consume_new_pv,
            rep_team_new_amount,
            rep_team_new_pv,
            point_count,
            bw.awards_value,
            bw.awards_name
            from (
            select * from ${tableName}
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
            ) a
            inner join bd_awards bw
            on bw.pk_id = a.pk_awards
            left join bd_vertex bv
            on bv.pk_id = a.pk_vertex
        </foreach>
    </select>

    <select id="listCuMemberRetailRange" resultMap="CuMemberRetailRangeExt">
        select
        a.pk_member,
        a.pk_parent,
        a.member_code,
        a.member_name,
        a.pk_grade,
        a.pk_awards,
        a.pk_share_awards,
        a.account_status,
        a.pay_time,
        a.category,
        a.income_status,
        a.pk_vertex,
        a.pk_settle_country,
        a.pk_rate,
        new_consume_pv,
        consume_pv,
        month_consume_pv,
        new_consume_amount,
        consume_amount,
        month_consume_amount,
        team_new_pv,
        team_consume_pv,
        team_month_pv,
        team_new_amount,
        team_consume_amount,
        team_month_amount,
        recommend_num,
        team_num,
        big_team_pv,
        small_team_pv,
        reg_consume_new_amount,
        reg_consume_new_pv,
        reg_team_new_amount,
        reg_team_new_pv,
        rep_consume_new_amount,
        rep_consume_new_pv,
        rep_team_new_amount,
        rep_team_new_pv,
        bw.awards_value,
        bw.awards_name
        from ${tableName} a
        inner join bd_awards bw
        on bw.pk_id = a.pk_awards
        left join bd_vertex bv
        on bv.pk_id = a.pk_vertex
        where a.pk_member in
        <foreach collection="pkMemberList" item="items" open="(" close=")" separator=",">
            #{items}
        </foreach>
    </select>

    <select id="queryCuMemberRetailRangeExtByParent" resultMap="CuMemberRetailRangeExt">
        select a.pk_member,
               a.pk_parent,
               a.member_code,
               a.member_name,
               a.pk_grade,
               a.pk_awards,
               a.pk_share_awards,
               a.account_status,
               a.pay_time,
               a.category,
               a.income_status,
               a.pk_vertex,
               a.pk_settle_country,
               a.pk_rate,
               new_consume_pv,
               consume_pv,
               month_consume_pv,
               new_consume_amount,
               consume_amount,
               month_consume_amount,
               team_new_pv,
               team_consume_pv,
               team_month_pv,
               team_new_amount,
               team_consume_amount,
               team_month_amount,
               recommend_num,
               team_num,
               big_team_pv,
               small_team_pv,
               reg_consume_new_amount,
               reg_consume_new_pv,
               reg_team_new_amount,
               reg_team_new_pv,
               rep_consume_new_amount,
               rep_consume_new_pv,
               rep_team_new_amount,
               rep_team_new_pv,
               bw.awards_value,
               bw.awards_name
        from ${tableName} a
                 inner join bd_awards bw
                            on bw.pk_id = a.pk_awards
                 left join bd_vertex bv
                           on bv.pk_id = a.pk_vertex
        where a.pk_parent = #{pkParent}
    </select>

    <select id="queryCuMemberSecondRangeParent" resultMap="CuMemberRetailRangeExt">
        select a.pk_member,
               cm.member_code,
               cm.member_name,
               a.pk_parent,
               cm.pk_settle_country,
               cm.category,
               cm.account_status,
               a.pk_grade,
               a.pk_awards,
               a.pk_share_awards,
               cm.income_status,
               cm.pay_time,
               cm.pk_vertex,
               nvl(b.pk_rate, bt.pk_id)                                  pk_rate,
               a.new_consume_pv,
               nvl(b.consume_pv, 0) + nvl(a.new_consume_pv, 0)           consume_pv,
               nvl(b.month_consume_pv, 0) + nvl(a.new_consume_pv, 0)     month_consume_pv,
               a.team_new_pv,
               nvl(b.team_consume_pv, 0) + nvl(a.team_new_pv, 0)         team_consume_pv,
               nvl(b.team_month_pv, 0) + nvl(a.team_new_pv, 0)           team_month_pv,
               a.team_new_amount,
               nvl(b.team_consume_amount, 0) + nvl(a.team_new_amount, 0) team_consume_amount,
               nvl(b.team_month_amount, 0) + nvl(a.team_new_amount, 0)   team_month_amount,
               nvl(b.big_team_pv, 0)                                     big_team_pv,
               nvl(b.small_team_pv, 0)                                   small_team_pv,
               a.system_type,
               bw.awards_value,
               bw.awards_name,
               bw.range_ratio,
               bw.month_repurchase_ratio,
               bw.benefit_avg_ratio,
               bw.benefit_share_ratio,
               bws.awards_value                                          share_awards_value
        from (
                 select *
                 from ${secondTableName} start with pk_member = #{pkMember}
                 connect by pk_member = prior pk_parent
             ) a
                 left join ${rangeTableName} b
                           on a.pk_member = b.pk_member
                 inner join cu_member cm
                            on cm.pk_id = a.pk_member
                 inner join bd_awards bw
                            on bw.pk_id = a.pk_awards and bw.del_flag = 0
                 inner join bd_awards bws
                            on bws.pk_id = a.pk_share_awards and bws.del_flag = 0
                 inner join bd_grade bg
                            on bg.pk_id = a.pk_grade
                 left join bd_vertex bv
                           on bv.pk_id = cm.pk_vertex
                 inner join bd_currency bt
                            on bt.pk_country = cm.pk_settle_country
        where bg.del_flag = 0
    </select>

    <select id="queryCuMemberSecondRangeExtByParent" resultMap="CuMemberRetailRangeExt">
        select cm.pk_id                                              pk_member,
               cm.pk_parent,
               nvl(b.month_consume_pv, 0) + nvl(a.new_consume_pv, 0) month_consume_pv,
               nvl(b.team_month_pv, 0) + nvl(a.team_new_pv, 0)       team_month_pv
        from cu_member cm
                 left join ${secondTableName} a
                           on cm.pk_id = a.pk_member
                 left join ${rangeTableName} b
                           on cm.pk_id = b.pk_member
        where cm.pk_parent = #{pkMember}
          and cm.del_flag = 0
    </select>

    <select id="queryCuMemberRetailRangeEnoughAchieve" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt">
        select pk_member,
               pk_parent,
               new_consume_pv,
               consume_pv,
               month_consume_pv,
               team_new_pv,
               team_consume_pv,
               team_month_pv,
               team_new_amount,
               team_consume_amount,
               team_month_amount,
               big_team_pv,
               small_team_pv
        from ${tableName}
        where pk_parent = #{pkParent}
    </select>
    <select id="findParentMemberList" resultType="com.hzs.common.domain.member.ext.CuMemberExt">
        select * from (
            select cm.*
            from cu_member cm
                start
            with cm.pk_id = #{pkMember}
            connect by cm.pk_id = prior cm.pk_parent
            order by level
            ) r where r.pk_id != #{pkMember}
    </select>

</mapper>
