<?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="SYSTEM_TYPE" property="systemType"/>
        <result column="NEW_BOX_NUM" property="newBoxNum"/>
        <result column="CONSUME_BOX_NUM" property="consumeBoxNum"/>
        <result column="MONTH_BOX_NUM" property="monthBoxNum"/>
        <result column="NEW_CONSUME_PV" property="newConsumePv"/>
        <result column="CONSUME_PV" property="consumePv"/>
        <result column="MONTH_CONSUME_PV" property="monthConsumePv"/>
        <result column="TEAM_NEW_BOX_NUM" property="teamNewBoxNum"/>
        <result column="TEAM_BOX_NUM" property="teamBoxNum"/>
        <result column="TEAM_MONTH_BOX_NUM" property="teamMonthBoxNum"/>
        <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="REGION_ADDRESS" property="regionAddress"/>
        <result column="GRADE_VALUE" property="gradeValue"/>
        <result column="GRADE_NAME" property="gradeName"/>
        <result column="AWARDS_NAME" property="awardsName"/>
        <result column="AWARDS_VALUE" property="awardsValue"/>
        <result column="SHARE_AWARDS_VALUE" property="shareAwardsValue"/>
        <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="COUNTRY_NAME" property="countryName"/>
        <result column="big_box_num" property="bigBoxNum"/>
        <result column="small_box_num" property="smallBoxNum"/>
        <result column="big_team_pv" property="bigTeamPv"/>
        <result column="small_team_pv" property="smallTeamPv"/>
        <result column="region_vertex_pk_id" property="regionVertexPkId"/>

        <result column="all_new_box_num" property="allNewBoxNum"/>
        <result column="all_new_consume_pv" property="allNewConsumePv"/>

        <result column="all_team_new_box_num" property="allTeamNewBoxNum"/>
        <result column="all_team_new_pv" property="allTeamNewPv"/>
        <result column="all_team_box_num" property="allTeamBoxNum"/>
        <result column="all_team_month_box_num" property="allTeamMonthBoxNum"/>
        <result column="all_team_consume_pv" property="allTeamConsumePv"/>
        <result column="all_team_month_pv" property="allTeamMonthPv"/>
    </resultMap>

    <update id="mergeMemberRetailRangeInit">
        update ${rangeTableName}
        set new_box_num = 0,
            consume_box_num = 0,
            month_box_num = 0,
            new_consume_pv = 0,
            consume_pv = 0,
            month_consume_pv = 0,
            team_new_box_num = 0,
            team_box_num = 0,
            team_month_box_num = 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,
            enable_status = 1,
            pk_grade = (select pk_id from bd_grade where del_flag = 0 and grade_value = 20),
            region_address = 0,
            big_box_num = 0,
            small_box_num = 0,
            big_team_pv = 0,
            small_team_pv = 0,
            all_team_new_box_num = 0,
            all_team_box_num = 0,
            all_team_month_box_num = 0,
            all_team_new_pv = 0,
            all_team_consume_pv = 0,
            all_team_month_pv = 0
    </update>

    <update id="mergeMemberRetailRangeByYesterday">
        merge into ${rangeTableName} a
        using(
        select pk_member, pk_grade, pk_awards, pk_share_awards, enable_status,
        new_box_num, consume_box_num, month_box_num,
        new_consume_pv, consume_pv, month_consume_pv,
        team_new_box_num, team_box_num, team_month_box_num,
        team_new_pv, team_consume_pv, team_month_pv,
        team_new_amount, team_consume_amount, team_month_amount,
        recommend_num, team_num,
        big_box_num, small_box_num, big_team_pv, small_team_pv,
        all_team_new_box_num, all_team_box_num, all_team_month_box_num,
        all_team_new_pv, all_team_consume_pv, all_team_month_pv
        from ${yesterdayRangeTableName}
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.consume_box_num = b.consume_box_num,
        a.consume_pv = b.consume_pv,
        a.team_box_num = b.team_box_num,
        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.pk_grade = b.pk_grade,
        a.enable_status = b.enable_status,
        a.big_box_num = b.big_box_num, a.small_box_num = b.small_box_num,
        a.big_team_pv = b.big_team_pv, a.small_team_pv = b.small_team_pv,
        a.all_team_new_box_num = b.all_team_new_box_num,
        a.all_team_box_num = b.all_team_box_num,
        a.all_team_new_pv = b.all_team_new_pv,
        a.all_team_consume_pv = b.all_team_consume_pv
        <if test="isSameMonth == 0">
            ,a.month_box_num = b.month_box_num
            ,a.month_consume_pv = b.month_consume_pv
            ,a.team_month_box_num = b.team_month_box_num
            ,a.team_month_pv = b.team_month_pv
            ,a.team_month_amount = b.team_month_amount
            ,a.all_team_month_box_num = b.all_team_month_box_num
            ,a.all_team_month_pv = b.all_team_month_pv
        </if>
    </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.pkGrade} pk_grade, #{item.pkShareAwards} pk_share_awards,
            #{item.newBoxNum} new_box_num, #{item.consumeBoxNum} consume_box_num, #{item.monthBoxNum} month_box_num,
            #{item.newConsumePv} new_consume_pv, #{item.consumePv} consume_pv, #{item.monthConsumePv} month_consume_pv,
            #{item.teamNewBoxNum} team_new_box_num, #{item.teamBoxNum} team_box_num, #{item.teamMonthBoxNum} team_month_box_num,
            #{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.enableStatus} enable_status,
            #{item.allTeamNewBoxNum} all_team_new_box_num, #{item.allTeamNewPv} all_team_new_pv,
            #{item.allTeamBoxNum} all_team_box_num, #{item.allTeamConsumePv} all_team_consume_pv,
            #{item.allTeamMonthBoxNum} all_team_month_box_num, #{item.allTeamMonthPv} all_team_month_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_grade = b.pk_grade, a.pk_share_awards = b.pk_share_awards,
        a.new_box_num = b.new_box_num, a.consume_box_num = b.consume_box_num, a.month_box_num = b.month_box_num,
        a.new_consume_pv = b.new_consume_pv, a.consume_pv = b.consume_pv, a.month_consume_pv = b.month_consume_pv,
        a.team_new_box_num = b.team_new_box_num, a.team_box_num = b.team_box_num, a.team_month_box_num = b.team_month_box_num,
        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.enable_status = b.enable_status,
        a.all_team_new_box_num = b.all_team_new_box_num, a.all_team_new_pv = b.all_team_new_pv,
        a.all_team_box_num = b.all_team_box_num, a.all_team_consume_pv = b.all_team_consume_pv,
        a.all_team_month_box_num = b.all_team_month_box_num, a.all_team_month_pv = b.all_team_month_pv
    </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.is_activate     enable_status,
                       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, enable_status, system_type)
                    values (b.pk_member, b.pk_parent, b.pk_grade, b.pk_awards, b.pk_range_awards, b.enable_status, 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,
                       nvl(b.enable_status, 1) enable_status
                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
                where b.enable_status = 0
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.enable_status = b.enable_status, 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.newBoxNum} new_box_num, #{item.newConsumePv} new_consume_pv,
            #{item.teamNewBoxNum} team_new_box_num, #{item.teamNewPv} team_new_pv,
            #{item.teamNewAmount} team_new_amount, #{item.enableStatus} enable_status,
            #{item.allNewBoxNum} all_new_box_num, #{item.allNewConsumePv} all_new_consume_pv,
            #{item.allTeamNewBoxNum} all_team_new_box_num, #{item.allTeamNewPv} all_team_new_pv
            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_box_num = b.new_box_num, a.new_consume_pv = b.new_consume_pv,
        a.team_new_box_num = b.team_new_box_num, a.team_new_pv = b.team_new_pv,
        a.team_new_amount = b.team_new_amount, a.enable_status = b.enable_status,
        a.all_new_box_num = b.all_new_box_num, a.all_new_consume_pv = b.all_new_consume_pv,
        a.all_team_new_box_num = b.all_team_new_box_num, a.all_team_new_pv = b.all_team_new_pv
    </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="mergeMemberRetailRegion">
        merge into ${rangeTableName} a
            using (
                select cm.pk_member, cr.county region_address
                from ${rangeTableName} cm
                         inner join cu_member_retail_region cr
                                    on cr.pk_member = cm.pk_member
                                        and cr.effective = 0
                                        and cr.del_flag = 0
                where cr.creation_time &lt; #{settleDate, jdbcType=DATE}
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.region_address = b.region_address
    </update>

    <update id="mergeCuMemberRetailRangeBySpecial">
        merge into ${rangeTableName} a
            using (
                select a.*
                from cu_member_spe_retail a
                         inner join(
                    select member_code, max(nvl(modified_time, creation_time)) creation_time
                    from cu_member_spe_retail
                    where del_flag = 0
                      and period = #{period}
                    group by member_code
                ) b on a.member_code = b.member_code and nvl(a.modified_time, a.creation_time) = b.creation_time
            ) b
            on (a.member_code = b.member_code)
            when matched then
                update set
                    a.big_box_num = b.team_box_num - b.small_box_num,
                    a.small_box_num = b.small_box_num
    </update>

    <update id="mergeCuMemberRetailBackBoxBySpecial">
        merge into ${rangeTableName} a
            using (
                select *
                from ${beforeTableName} a
                         inner join (
                    select cr.*
                    from cu_member_spe_retail cr
                             inner join (
                        select member_code, max(pk_id) pk_id
                        from cu_member_spe_retail
                        where period &lt; #{period}
                          and del_flag = 0
                        group by member_code
                    ) b on cr.member_code = b.member_code
                    where cr.period = #{period}
                      and cr.del_flag = 0
                ) b on a.member_code = b.member_code
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set
                    a.big_box_num = 0,
                    a.small_box_num = 0,
                    a.team_box_num = a.team_box_num
    </update>

    <select id="queryCuMemberRetailRangeParent" resultMap="CuMemberRetailRangeExt">
        <foreach collection="saOrderExtList" item="item" close=" " open=" " separator="union">
            select
            a.pk_member,
            a.member_code,
            a.member_name,
            a.pk_parent,
            a.pk_settle_country,
            a.category,
            a.account_status,
            a.pk_grade,
            a.pk_awards,
            a.pk_share_awards,
            a.income_status,
            a.consume_pv,
            a.pay_time,
            a.pk_rate,
            a.system_type,
            a.income_status,
            a.pk_country,
            a.pk_settle_country,
            a.enable_status,
            new_box_num,
            consume_box_num,
            month_box_num,
            new_consume_pv,
            consume_pv,
            month_consume_pv,
            team_new_box_num,
            team_box_num,
            team_month_box_num,
            team_new_pv,
            team_consume_pv,
            team_month_pv,
            team_new_amount,
            team_consume_amount,
            team_month_amount,
            recommend_num,
            team_num,
            ba.grade_value,
            ba.grade_name,
            bw.awards_value,
            bw.awards_name,
            bwa.awards_value share_awards_value,
            a.big_box_num,
            a.small_box_num,
            a.big_team_pv,
            a.small_team_pv,
            a.all_team_new_box_num,
            a.all_team_box_num,
            a.all_team_month_box_num,
            a.all_team_new_pv,
            a.all_team_consume_pv,
            a.all_team_month_pv,
            bw.range_ratio,
            bw.month_repurchase_ratio,
            bw.benefit_avg_ratio,
            bw.benefit_share_ratio,
            br.name country_name,
            a.pk_vertex,
            bv.region_vertex_pk_id
            from (
            select * from ${tableName}
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
            ) a
            inner join bd_grade ba
            on a.pk_grade = ba.pk_id
            inner join bd_awards bw
            on bw.pk_id = a.pk_awards
            left join bd_awards bwa
            on bwa.pk_id = a.pk_share_awards and bwa.del_flag = 0
            left join bd_area br
            on br.pk_id = a.region_address and br.del_flag = 0
            left join bd_vertex bv
            on bv.pk_id = a.pk_vertex
            where ba.del_flag = 0 and bw.del_flag = 0
        </foreach>
    </select>

    <select id="queryCuMemberRetailRangeExtByParent" resultMap="CuMemberRetailRangeExt">
        select a.pk_member,
               a.member_code,
               a.member_name,
               a.pk_parent,
               a.pk_settle_country,
               a.category,
               a.account_status,
               a.pk_grade,
               a.pk_awards,
               a.pk_share_awards,
               a.income_status,
               a.consume_pv,
               a.pay_time,
               a.pk_rate,
               a.income_status,
               a.pk_country,
               a.pk_settle_country,
               a.big_box_num,
               a.small_box_num,
               a.big_team_pv,
               a.small_team_pv,
               a.system_type,
               a.all_team_box_num,
               a.all_team_month_box_num,
               a.all_team_consume_pv,
               a.all_team_month_pv,
               new_box_num,
               consume_box_num,
               month_box_num,
               new_consume_pv,
               consume_pv,
               month_consume_pv,
               team_new_box_num,
               team_box_num,
               team_month_box_num,
               team_new_pv,
               team_consume_pv,
               team_month_pv,
               team_new_amount,
               team_consume_amount,
               team_month_amount,
               recommend_num,
               team_num,
               ba.grade_value,
               ba.grade_name,
               bw.awards_value,
               bw.awards_name,
               bws.awards_value share_awards_value,
               bw.range_ratio,
               bw.month_repurchase_ratio,
               bw.benefit_avg_ratio,
               bw.benefit_share_ratio,
               br.name          country_name
        from ${tableName} a
                 inner join bd_grade ba
                            on a.pk_grade = ba.pk_id
                 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
                 left join bd_area br
                           on br.pk_id = a.region_address and br.del_flag = 0
        where ba.del_flag = 0
          and a.pk_parent = #{pkParent}
    </select>

    <select id="batchQueryMemberRetailRangeByMemberPk" resultMap="CuMemberRetailRangeExt">
        <foreach collection="memberPkList" item="item" close=" " open=" " separator="union">
            select pk_member, member_code, member_name, c.pk_parent,
            c.pk_awards, c.pk_grade, c.pk_rate,
            c.enable_status, c.income_status,
            c.pk_country, c.pk_settle_country,
            c.account_status, c.category, c.system_type,
            bg.grade_value, bg.grade_name,
            bw.awards_value, bw.awards_name,
            c.pk_share_awards, bwa.awards_value share_awards_value,
            bw.range_ratio, bw.month_repurchase_ratio, bw.benefit_avg_ratio, bw.benefit_share_ratio,
            bw.coach_algebra, bw.coach_ratio
            from (
            select *
            from ${rangeTableName}
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
            ) c
            inner join bd_grade bg
            on c.pk_grade = bg.pk_id
            left join bd_awards bw
            on bw.pk_id = c.pk_awards
            left join bd_awards bwa
            on bwa.pk_id = c.pk_share_awards
            and bwa.del_flag = 0
            where bg.del_flag = 0
            and bw.del_flag = 0
        </foreach>
    </select>

    <select id="batchQueryMemberRetailRangeByAwards" resultMap="CuMemberRetailRangeExt">
        select pk_member,
               member_code,
               member_name,
               a.pk_awards,
               a.pk_grade,
               a.pk_rate,
               a.income_status,
               a.pk_country,
               a.pk_settle_country,
               a.account_status,
               a.category,
               bg.grade_value,
               bg.grade_name,
               bw.awards_value share_awards_value,
               bw.awards_name,
               a.month_consume_pv,
               a.team_month_pv,
               bw.range_ratio,
               bw.month_repurchase_ratio,
               bw.benefit_avg_ratio,
               bw.benefit_share_ratio
        from ${rangeTableName} a
                 inner join bd_awards bw
                            on bw.pk_id = a.pk_share_awards
                 inner join bd_grade bg
                            on bg.pk_id = a.pk_grade
        where bw.del_flag = 0
          and bg.del_flag = 0
          and bg.grade_value >= 70
          and bw.awards_value >= 20
          and a.enable_status = 0
    </select>

    <select id="batchQueryMemberRetailRangeByParent" resultMap="CuMemberRetailRangeExt">
        select pk_member, pk_parent, member_code, member_name,
        a.pk_awards, a.pk_grade, a.pk_rate,
        a.income_status, a.pk_country, a.pk_settle_country,
        a.account_status, a.category,
        bg.grade_value, bg.grade_name,
        bw.awards_value, bw.awards_name,
        a.month_consume_pv, a.team_month_pv,
        bw.range_ratio, bw.month_repurchase_ratio,
        bw.benefit_avg_ratio, bw.benefit_share_ratio
        from ${rangeTableName} a
        inner join bd_awards bw
        on bw.pk_id = a.pk_awards
        and bw.del_flag = 0
        inner join bd_grade bg
        on bg.pk_id = a.pk_grade
        and bg.del_flag = 0
        where a.pk_parent in
        <foreach collection="memberRetailRangeExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </select>

    <select id="batchQueryMemberRetailRangeByCounty" resultMap="CuMemberRetailRangeExt">
        select a.pk_member,
               a.pk_parent,
               a.member_code,
               a.member_name,
               a.pk_vertex,
               a.pk_rate,
               a.pk_grade,
               a.pk_awards,
               a.pk_share_awards,
               a.enable_status,
               a.income_status,
               a.pk_country,
               a.pk_settle_country,
               a.region_address,
               ba.name          country_name,
               bg.grade_value,
               bg.grade_name,
               bw.awards_value,
               bw.awards_name,
               bwa.awards_value share_awards_value,
               a.big_box_num,
               a.small_box_num,
               a.big_team_pv,
               a.small_team_pv,
               a.all_team_new_box_num,
               a.all_team_box_num,
               a.all_team_month_box_num,
               a.all_team_new_pv,
               a.all_team_consume_pv,
               a.all_team_month_pv,
               a.account_status,
               a.category,
               bw.range_ratio,
               bw.month_repurchase_ratio,
               bw.benefit_avg_ratio,
               bw.benefit_share_ratio,
               a.system_type,
               bv.region_vertex_pk_id
        from ${rangeTableName} a
                 inner join bd_awards bw
                            on bw.pk_id = a.pk_awards
                 inner join bd_awards bwa
                            on bwa.pk_id = a.pk_share_awards
                 inner join bd_grade bg
                            on bg.pk_id = a.pk_grade
                 inner join bd_area ba
                            on ba.pk_id = a.region_address
                 left join bd_vertex bv
                           on bv.pk_id = a.pk_vertex
        where bw.del_flag = 0
          and bg.del_flag = 0
          and ba.del_flag = 0
          and a.region_address > 0
    </select>

    <select id="batchQueryMemberRetailSecondRangeByCounty" resultMap="CuMemberRetailRangeExt">
        select cm.pk_id                                       pk_member,
               cm.pk_parent,
               cm.member_code,
               cm.member_name,
               cm.pk_awards,
               cm.pk_settle_grade                             pk_grade,
               bt.pk_id                                       pk_rate,
               cm.income_status,
               cm.pk_country,
               cm.pk_settle_country,
               cr.county                                      region_address,
               ba.name                                        country_name,
               bg.grade_value,
               bg.grade_name,
               bw.awards_value,
               bw.awards_name,
               cm.account_status,
               cm.category,
               cm.system_type,
               bw.range_ratio,
               bw.month_repurchase_ratio,
               bw.benefit_avg_ratio,
               bw.benefit_share_ratio,
               nvl(nvl(sc.enable_status, r.enable_status), 1) enable_status,
               bt.pk_id pk_rate,
               bv.region_vertex_pk_id
        from cu_member cm
                 inner join bd_awards bw
                            on bw.pk_id = cm.pk_awards
                 inner join bd_grade bg
                            on bg.pk_id = cm.pk_settle_grade
                 inner join cu_member_retail_region cr
                            on cr.pk_member = cm.pk_id
                 inner join bd_area ba
                            on ba.pk_id = cr.county
                 inner join bd_currency bt
                            on bt.pk_country = cm.pk_settle_country
                 left join ${rangeTableName} r
                           on r.pk_member = cm.pk_id
                 left join ${secondRangeTableName} sc
                           on sc.pk_member = cm.pk_id
                 left join bd_vertex bv
                           on bv.pk_id = cm.pk_vertex
        where bw.del_flag = 0
          and bg.del_flag = 0
          and ba.del_flag = 0
          and cr.effective = 0
          and cr.del_flag = 0
          and cm.del_flag = 0
          and cr.county = #{county}
    </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,
               nvl(cm.is_activate, a.enable_status)                           enable_status,
               a.new_box_num,
               nvl(b.consume_box_num, 0) + nvl(a.new_box_num, 0)              consume_box_num,
               nvl(b.month_box_num, 0) + nvl(a.new_box_num, 0)                month_box_num,
               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_box_num,
               nvl(b.team_box_num, 0) + nvl(a.team_new_box_num, 0)            team_box_num,
               nvl(b.team_month_box_num, 0) + nvl(a.team_new_box_num, 0)      team_month_box_num,
               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_box_num, 0)                                          big_box_num,
               nvl(b.small_box_num, 0)                                        small_box_num,
               nvl(b.big_team_pv, 0)                                          big_team_pv,
               nvl(b.small_team_pv, 0)                                        small_team_pv,
               a.system_type,
               a.all_new_box_num,
               a.all_new_consume_pv,
               a.all_team_new_box_num,
               a.all_team_new_pv,
               nvl(b.all_team_box_num, 0) + nvl(a.all_team_new_box_num, 0)    all_team_box_num,
               nvl(b.all_team_month_box_num, 0) + nvl(a.all_team_new_pv, 0)   all_team_month_box_num,
               nvl(b.all_team_consume_pv, 0) + nvl(a.all_team_new_box_num, 0) all_team_consume_pv,
               nvl(b.all_team_month_pv, 0) + nvl(a.all_team_new_pv, 0)        all_team_month_pv,
               bg.grade_value,
               bg.grade_name,
               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,
               br.name                                                        country_name,
               cr.county                                                      region_address,
               bv.region_vertex_pk_id
        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 cu_member_retail_region cr
                           on cr.pk_member = cm.pk_id and cr.effective = 0 and cr.del_flag = 0
                 left join bd_area br
                           on br.pk_id = cr.county and br.del_flag = 0
                 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,
               nvl(b.consume_box_num, 0) + nvl(a.new_box_num, 0)              consume_box_num,
               nvl(b.team_box_num, 0) + nvl(a.team_new_box_num, 0)            team_box_num,
               nvl(b.big_box_num, 0)                                          big_box_num,
               nvl(b.small_box_num, 0)                                        small_box_num,
               nvl(b.all_team_box_num, 0) + nvl(a.all_team_new_box_num, 0)    all_team_box_num,
               nvl(b.all_team_month_box_num, 0) + nvl(a.all_team_new_pv, 0)   all_team_month_box_num,
               nvl(b.all_team_consume_pv, 0) + nvl(a.all_team_new_box_num, 0) all_team_consume_pv,
               nvl(b.all_team_month_pv, 0) + nvl(a.all_team_new_pv, 0)        all_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_box_num,
               consume_box_num,
               month_box_num,
               new_consume_pv,
               consume_pv,
               month_consume_pv,
               team_new_box_num,
               team_box_num,
               team_month_box_num,
               team_new_pv,
               team_consume_pv,
               team_month_pv,
               team_new_amount,
               team_consume_amount,
               team_month_amount,
               big_box_num,
               small_box_num,
               big_team_pv,
               small_team_pv,
               all_team_box_num,
               all_team_month_box_num,
               all_team_consume_pv,
               all_team_month_pv
        from ${tableName}
        where pk_parent = #{pkParent}
    </select>

</mapper>
