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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettleExt" type="com.hzs.common.domain.member.ext.CuMemberSettleExt">
        <result column="PERIOD" property="period" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_PARENT" property="pkParent" />
        <result column="PK_PLACE_PARENT" property="pkPlaceParent" />
        <result column="PLACE_DEPT" property="placeDept" />
        <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_ORI_AWARDS" property="pkOriAwards" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus" />
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_TYPE" property="purchaseType"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus" />
        <result column="SHARE_STATUS" property="shareStatus"/>
        <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_COUNTRY" property="pkCountry" />
        <result column="PK_RATE" property="pkRate" />
        <result column="REGISTER_AMOUNT" property="registerAmount" />
        <result column="REGISTER_PV" property="registerPv" />
        <result column="REGISTER_NEW_PV" property="registerNewPv" />
        <result column="REGISTER_Blo_PV" property="registerBloPv" />
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv" />
        <result column="ASSESS_TYPE" property="assessType"/>
        <result column="PK_ORDER" property="pkOrder" />
        <result column="LEVEL" property="level" />
        <result column="GRADE_VALUE" property="gradeValue" />
        <result column="parentMemberCode" property="parentMemberCode" />
        <result column="AP_PK_ID" property="apPkId" />
        <result column="USABLE_QUANTITY" property="usableQuantity"/>
        <result column="RETAIL_PRICE" property="retailPrice" />
        <result column="PK_DATA_ID" property="pkDataId" />
        <result column="directPushNumber" property="directPushNumber" />
        <result column="historyPerformancePv" property="historyPerformancePv" />
        <result column="expire_date" property="expireDate" />
        <result column="abonus_ratio" property="abonusRatio" />
        <result column="register_mon_new_pv" property="registerMonNewPv" />
        <result column="SUPPLY_POINT" property="supplyPoint" />
        <result column="SUPPLY_INCOME_BONUS" property="supplyIncomeBonus" />
        <result column="RESALE_INCOME_NUMBER" property="resaleIncomeNumber" />
        <result column="RESALE_INCOME_BONUS" property="resaleIncomeBonus" />
        <collection property="cuMemberSettleDetailExtList" ofType="com.hzs.common.domain.member.ext.CuMemberSettleDetailExt">
            <result column="b_pk_member" property="pkMember" />
            <result column="B_PLACE_DEPT" property="placeDept" />
            <result column="A_BALANCE" property="aBalance" />
            <result column="B_BALANCE" property="bBalance" />
            <result column="SUM_AMOUNT" property="sumAmount" />
            <result column="SUM_PV" property="sumPv" />
            <result column="NEW_AMOUNT" property="newAmount" />
            <result column="NEW_PV" property="newPv" />
            <result column="mon_new_pv" property="monNewPv" />
        </collection>
    </resultMap>

    <!-- 会员网体月表数据，用于查询 网体跟业绩分开 横向分表-->
    <sql id="CuMemberTreeColumn">
        period          NUMBER(6) default 0 not null,
        pk_member         NUMBER(20) not null,
        pk_parent         NUMBER(20) not null,
        pk_place_parent   NUMBER(20),
        place_dept        NUMBER(4),
        member_code       VARCHAR2(20) not null,
        member_name       VARCHAR2(100) not null,
        customer_type     NUMBER(2) not null,
        phone             VARCHAR2(40) not null,
        pk_grade          NUMBER(6) not null,
        pk_awards          NUMBER(6) not null,
        pk_rate           NUMBER(6),
        account_status   number(1) default 0 not null,
        pay_status         NUMBER(2) default 0 not null,
        register_authority number(2) default 0 not null,
        category        number(2)  default 0 not null,
        purchase_type    number(2)  default 0 not null,
        purchase_status number(2)  default 0 not null,
        share_status  number(2)  default 0 not null,
        income_status number(2) default 0 not null,
        pk_team_code number(20),
        pk_center_code number(20),
        pk_vertex    number(20),
        pk_settle_country number(4) not null,
        is_real_name    NUMBER(1) default 1 not null,
        pk_country        number(4) default 1 not null,
        expire_date         DATE,
        supply_point     NUMBER(6) default 0 not null,
        supply_income_bonus  NUMBER(15,6) default 0 not null,
        resale_income_number NUMBER(6) default 0 not null,
        resale_income_bonus  NUMBER(15,6) default 0 not null,
        register_amount  NUMBER(17,6) default 0 not null,
        register_pv     NUMBER(17,6) default 0 not null,
        register_new_amount     NUMBER(17,6) default 0 not null,
        register_new_pv NUMBER(17,6) default 0 not null,
        register_mon_new_pv      number(17,6) default 0 not null,
        register_blo_pv  NUMBER(17,6) default 0 not null,
        register_blo_new_pv  NUMBER(17,6) default 0 not null,
        repurchase_blo_amount    NUMBER(17,6) default 0 not null,
        repurchase_blo_new_amount    NUMBER(17,6) default 0 not null
    </sql>

    <sql id="CuMemberSettleDetailColumn">
            pk_member             NUMBER(20) not null,
            place_dept            NUMBER(4),
            a_balance         NUMBER(17,6) default 0 not null,
            b_balance         NUMBER(17,6) default 0 not null,
            sum_amount      NUMBER(17,6) default 0 not null,
            sum_pv          NUMBER(17,6) default 0 not null,
            new_amount      NUMBER(17,6) default 0 not null,
            new_pv          NUMBER(17,6) default 0 not null,
            mon_new_pv      number(17,6) default 0 not null
    </sql>

    <sql id="CuMemberSettleCountryColumn">
            pk_member             NUMBER(20) not null,
            place_dept            NUMBER(4),
            pk_country        number(4) default 1 not null,
            sum_amount      NUMBER(17,6) default 0 not null,
            sum_pv          NUMBER(17,6) default 0 not null,
            new_amount      NUMBER(17,6) default 0 not null,
            new_pv          NUMBER(17,6) default 0 not null
    </sql>
    <sql id="CuMemberTableTrig">
        CREATE OR REPLACE TRIGGER ${tableName}_TRIG
                      BEFORE INSERT OR UPDATE ON ${tableName}
            FOR EACH ROW
            DECLARE
            v_newVal NUMBER(12) := 0;
        v_incval NUMBER(12) := 0;
        BEGIN
            IF INSERTING AND :new.pk_id IS NULL THEN
        SELECT
            ${tableNameSeq}.NEXTVAL
        INTO v_newVal
        FROM DUAL;
        IF v_newVal = 1 THEN
        SELECT NVL(max(pk_id), 0) INTO v_newVal FROM ${tableName};
        v_newVal := v_newVal + 1;
        LOOP
                EXIT WHEN v_incval >= v_newVal;
        SELECT ${tableNameSeq}.nextval INTO v_incval
        FROM dual;
        END LOOP;
        END IF;
        :new.pk_id := v_newVal;
        END IF;
        END;
    </sql>
    <sql id="QueryMemberParent">
        <foreach collection="pkMemberList" item="item" open=" " close=" " separator=" union ">
            select pk_member,pk_parent,member_code,member_name,pk_awards,account_status,
            register_authority,customer_type,income_status,pk_settle_country,purchase_status,share_status,
            register_amount,register_pv,pk_center_code,category,pk_grade,
            supply_point,supply_income_bonus,pk_place_parent,place_dept,
            resale_income_number,resale_income_bonus,expire_date
            from ${settleTableName}
            where pay_status = 1
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
        </foreach>
    </sql>

    <insert id="insertCuMemberSettleByMember">
        insert into ${settleTableName}(pk_member, pk_parent, pk_place_parent, place_dept, member_code, member_name,
                                       customer_type, phone, pk_grade, pk_awards, account_status, pay_status, register_authority, category,
                                       purchase_type, purchase_status, share_status, income_status, pk_team_code,
                                       pk_center_code, pk_vertex, pk_settle_country, is_real_name, pk_country, pk_rate, expire_date,
                                       resale_income_number, resale_income_bonus)
        select cm.pk_id,
               pk_parent,
               pk_place_parent,
               place_dept,
               member_code,
               member_name,
               customer_type,
               phone,
               pk_settle_grade,
               pk_awards,
               account_status,
               pay_status,
               register_authority,
               category,
               (case when purchase_status = 0 then 1 else 0 end)        purchase_type,
               purchase_status,
               share_status,
               income_status,
               pk_team_code,
               pk_center_code,
               pk_vertex,
               pk_settle_country,
               is_real_name,
               cm.pk_country,
               nvl(bc.pk_id, -1)                                        pk_rate,
               cm.expire_date,
               cm.resale_income_number,
               (cm.resale_income_bonus - cm.resale_income_number * 150) cm
        from cu_member cm
                 left join (
            select pk_country, max(pk_id) pk_id
            from bd_currency
            where del_flag = 0
              and pk_country = 1
              and enable_state = 0
              and effective_date &lt;= sysdate
            group by pk_country) bc
                           on cm.pk_settle_country = bc.pk_country
        where cm.del_flag = 0
    </insert>

    <insert id="insertCuMemberSettleDetailByMember">
        insert into ${settleTableName}(pk_member, place_dept)
        select pk_place_parent pk_member, place_dept
        from cu_member
        where del_flag = 0
          and pk_place_parent is not null
        union
        select pk_member, place_dept
        from ${yesSettleTableName}
    </insert>

    <update id="createCuMemberSettleTable">
        create table ${tableName}
        (
        <include refid="CuMemberTreeColumn"></include>
        )
    </update>

    <update id="createCuMemberSettleDetailTable">
        create table ${tableName}
        (
        <include refid="CuMemberSettleDetailColumn"></include>
        )
    </update>

    <update id="createCuMemberSettleCountryTable">
        create table ${tableName}
        (
        <include refid="CuMemberSettleCountryColumn"></include>
        )
    </update>

    <update id="createCuMemberTreeParentIndex">
        create index ${tableName}_parent on ${tableName}(pk_parent)
    </update>

    <update id="createCuMemberTreePlaceIndex">
        create index ${tableName}_place on ${tableName}(pk_place_parent,place_dept)
    </update>

    <update id="createCuMemberTableUniqueIndex">
        create unique index ${tableName}_PK_M on ${tableName}(PK_MEMBER,period)
    </update>

    <update id="createCuMemberTreeDayOnlyIndex">
        create unique index ${tableName}_PK_M on ${tableName}(PK_MEMBER)
    </update>

    <update id="createCuMemberTreeDeptOnlyIndex">
        create unique index ${tableName}_PK_M on ${tableName}(PK_MEMBER,PLACE_DEPT)
    </update>

    <update id="createCuMemberTreeCountryOnlyIndex">
        create unique index ${tableName}_PK_M on ${tableName}(PK_MEMBER,PLACE_DEPT,PK_COUNTRY)
    </update>

    <update id="updateCuMemberTreeByCancelOrder">
        update ${settleTableName} set customer_type =2,category =1
        where pk_member in
        <foreach collection="saOrderExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
    </update>
    <update id="updateCuMemberSettleByPeriod">
        merge into ${updateTableName} a
        using(select pk_member,max(pk_awards) pk_awards from tmp_cu_member_awards group by pk_member) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
            a.pk_awards = b.pk_awards
    </update>

    <update id="createCuMemberTableSequence" statementType="STATEMENT">
        <include refid="CuMemberTableTrig"></include>
    </update>

    <update id="updateCuMemberSettle">
        merge into ${settleTableName} a
        using(
        <foreach collection="cuMemberTreeList" item="item" open=" " separator=" union " close=" ">
            select #{item.pkMember} pk_member,
            #{item.resaleIncomeNumber} resale_income_number,#{item.resaleIncomeBonus} resale_income_bonus,
            #{item.registerAmount} register_amount,#{item.registerPv} register_pv,#{item.registerNewAmount} register_new_amount,
            #{item.registerNewPv} register_new_pv from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.register_amount = b.register_amount,a.register_pv=b.register_pv,
        a.register_new_amount = b.register_new_amount,a.register_new_pv=b.register_new_pv,
        a.register_mon_new_pv=a.register_mon_new_pv+b.register_new_pv,
        a.resale_income_number=b.resale_income_number,a.resale_income_bonus=b.resale_income_bonus
    </update>

    <update id="updateCuMemberSettleDetail">
        merge into ${settleTableNameDetail} a
        using(
            <foreach collection="cuMemberSettleDetailExtList" item="detail" open=" " separator=" union " close=" ">
                select #{detail.pkMember} pk_member,#{detail.placeDept} place_dept,#{detail.aBalance} a_balance, #{detail.bBalance} b_balance,
                #{detail.newAmount} new_amount,#{detail.newPv} new_pv from dual
            </foreach>
        ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept)
        when matched then
        update set a.a_balance = b.a_balance, a.b_balance=b.b_balance,a.new_amount=b.new_amount,
        a.new_pv=b.new_pv,a.sum_amount = a.sum_amount + b.new_amount,
        a.sum_pv = a.sum_pv + b.new_pv,a.mon_new_pv=a.mon_new_pv + b.new_pv
    </update>

    <update id="updateCuMemberSettleCountry">
        merge into ${settleTableNameCountry} a
        using(
        <foreach collection="cuMemberSettleCountryExtList" item="detail" open=" " separator=" union " close=" ">
            select #{detail.pkMember} pk_member,#{detail.placeDept} place_dept,#{detail.pkCountry} pk_country,
            #{detail.newAmount} new_amount,#{detail.newPv} new_pv from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept and a.pk_country=b.pk_country)
        when not matched then
        insert (pk_member,place_dept,pk_country,sum_amount, sum_pv,new_amount,new_pv)
        values(b.pk_member,b.place_dept,b.pk_country,b.new_amount, b.new_pv,b.new_amount,b.new_pv)
        when matched then
        update set a.new_amount=b.new_amount,a.new_pv=b.new_pv,
            a.sum_amount = a.sum_amount + b.new_amount,
        a.sum_pv = a.sum_pv + b.new_pv
    </update>

    <update id="updateCuMemberTreeGradeByCancelOrder">
        merge into ${settleTableName} a
        using (select cl.pk_id,cl.pk_member,cl.new_level from cu_member_level cl
        inner join (
        select pk_member,max(pk_id) pk_id from cu_member_level
        where del_flag=0 and upgrade_time &lt; #{currentDate, jdbcType=DATE}
        and pk_member in
        <foreach collection="saOrderExtList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
        group by pk_member) ce
        on cl.pk_id = ce.pk_id
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.pk_grade = b.new_level
    </update>

    <update id="updateBackCuMemberSettleAward">
        merge into ${settleTableName} a
            using (select pk_member,max(old_level) old_level from cu_member_awards
                   where period = #{period} and up_type = 1
                   group by pk_member ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set
                    a.pk_awards = b.old_level
    </update>
    <delete id="truncateCuMemberTreeTable">
        truncate table ${tableName}
    </delete>

    <select id="queryExistOracleTable" resultType="java.lang.Integer">
        select 1
        from user_tables
        where table_name = upper(#{tableName})
    </select>

    <select id="queryExistOrderTableCount" resultType="java.lang.Integer">
        select count(1) from ${tableName}
    </select>

    <select id="queryCuMemberSettleEnoughAwardAchieve" resultMap="CuMemberSettleExt">
        select a.pk_member,a.pk_parent,a.pk_place_parent,a.pk_country,a.pk_settle_country,
        bw.pk_id pk_awards,a.pk_awards pk_ori_awards  from(
        select ct.pk_member,(ba.awards_value+5) awards_value,ba.pk_id pk_awards,
        b.sum_pv,ct.pk_settle_country,ct.pk_parent,
        ct.pk_place_parent,ct.pk_country from ${settleTableName} ct
        inner join(
        select pk_member,min(sum_pv) sum_pv,count(pk_member) numb from (
        SELECT *
        FROM (
        SELECT pk_member, sum_pv, ROW_NUMBER() OVER(PARTITION BY pk_member ORDER BY sum_pv desc) AS rn
        FROM ${settleTableNameDetail}
        ) subquery
        WHERE rn &lt;= 3
         )
        group by pk_member
        having count(pk_member)>=3 ) b
        on ct.pk_member=b.pk_member
        inner join bd_awards ba
        on ct.pk_awards = ba.pk_id) a
        inner join bd_awards bw
        on a.awards_value = bw.awards_value and a.pk_settle_country= bw.pk_country
        where bw.del_flag=0 and bw.community_check &lt;= a.sum_pv
    </select>
    <select id="queryCuMemberSettleBloodEnoughAwardAchieve" resultMap="CuMemberSettleExt">
        select a.pk_member,a.pk_parent,a.pk_place_parent,a.pk_country,a.pk_settle_country,
               b.pk_awards,a.pk_awards pk_ori_awards from ${settleTableName} a
                                                              inner join(
            select a.pk_member,bw.pk_id pk_awards,count(a.pk_member)
            from (select ct.pk_member,(ba.awards_value+5) awards_value,
                         b.register_blo_pv,ct.pk_settle_country from ${settleTableName} ct
               inner join(
                select pk_parent,(register_blo_pv+register_pv) register_blo_pv from ${settleTableName}
                where category =0 and income_status =0 and pk_parent in
                <foreach collection="cuMemberSettleExtList" item="item" open="(" close=")" separator=",">
                    #{item.pkMember}
                </foreach>
                ) b
             on ct.pk_member=b.pk_parent
            inner join bd_awards ba
              on ct.pk_awards = ba.pk_id
                  where ct.category =0 and ct.income_status =0 and ba.del_flag=0) a
                 inner join bd_awards bw
                 on a.awards_value = bw.awards_value and a.pk_settle_country= bw.pk_country
            where bw.del_flag=0 and bw.blo_community_check &lt;= a.register_blo_pv
            group by a.pk_member,bw.pk_id
            having count(a.pk_member)>=3) b
            on a.pk_member=b.pk_member
    </select>
    <select id="queryCuMemberSettlePlaceUnderUmbrella" resultType="java.lang.Integer">
        select count(a.pk_member) from (
          select pk_member,pk_awards,pk_parent,pk_place_parent,place_dept
          from ${settleTableName}
                   start with pk_place_parent = #{pkMember} and place_dept=#{placeDept}
          connect by pk_place_parent = prior pk_member
        ) a
        inner join bd_awards ba
        on a.pk_awards = ba.pk_id
        where ba.awards_value>=(select awards_value from bd_awards where pk_id=#{awardsValue})
    </select>

    <select id="queryCuMemberSettleBloodUnderUmbrella" resultType="java.lang.Integer">
        select count(pk_member) from(
        select pk_member from ${settleTableName}
        start with pk_parent = #{pkMember}
        connect by pk_parent = prior pk_member) a
        where a.pk_member in(
        select a.pk_member from (
                                    select pk_member,pk_awards,pk_parent,pk_place_parent,place_dept
                                    from ${settleTableName}
                                             start with pk_place_parent = #{pkMember} and place_dept=#{placeDept}
                                    connect by pk_place_parent = prior pk_member
                                ) a
                                    inner join bd_awards ba
                                               on a.pk_awards = ba.pk_id
        where ba.awards_value>=(select awards_value from bd_awards where pk_id=#{awardsValue}))
    </select>

    <select id="queryMemberSettleBySaOrder" resultMap="CuMemberSettleExt">
        select c.pk_member,member_code,member_name,pk_parent,c.pk_place_parent,c.place_dept,
               c.pk_awards,c.pk_grade,account_status,category,income_status,pk_settle_country,
               bg.grade_value,b.a_balance,b.b_balance,b.place_dept b_place_dept,b.sum_pv,register_amount,register_pv,pk_center_code,
        c.register_authority,c.expire_date,supply_point,supply_income_bonus,
        resale_income_number,resale_income_bonus from ${settleTableName} c
        left join ${settleTableNameDetail} b
        on c.pk_member=b.pk_member
        left join bd_grade bg
        on c.pk_grade = bg.pk_id
        where c.pk_member in
        <foreach collection="memberPkList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="batchQueryCuMemberSettlePlaceParent" resultMap="CuMemberSettleExt">
        select pk_order,a.pk_member,member_code,member_name,pk_parent,a.pk_place_parent,a.place_dept,pk_grade,a.pk_awards,bg.grade_value,account_status,
        income_status,pk_settle_country,purchase_status,share_status,a.register_authority,
        register_amount,register_pv,pk_center_code,category,expire_date,supply_point,supply_income_bonus,
        resale_income_number,resale_income_bonus,bc.pk_id pk_rate,b.pk_member b_pk_member,
        b.a_balance,b.b_balance,b.place_dept b_place_dept,b.sum_pv from (
            <foreach collection="firPurchaseOrderList" item="item" open=" " close=" " separator=" union all ">
            select #{item.pkId} pk_order,pk_member,member_code,member_name,pk_parent,pk_place_parent,place_dept,pk_grade,pk_awards,account_status,
                   income_status,pk_settle_country,purchase_status,share_status,register_authority,
                   register_amount,register_pv,pk_center_code,category,expire_date,supply_point,supply_income_bonus,
                resale_income_number,resale_income_bonus,level sort
                from ${settleTableName}
                start with pk_member = #{item.pkMember}
                connect by pk_member = prior pk_place_parent
            </foreach>
        ) a
        left join ${settleTableNameDetail} b
        on a.pk_member=b.pk_member
        left join bd_grade bg
        on a.pk_grade = bg.pk_id
        left join bd_currency bc
        on bc.pk_country=a.pk_settle_country and bc.del_flag=0
        order by a.pk_order,a.sort,b.place_dept
    </select>

    <select id="batchQueryCuMemberSettleParent" resultMap="CuMemberSettleExt">
        select a.pk_member,pk_parent,member_code,member_name,a.pk_place_parent,a.place_dept,
               a.pk_awards,account_status,category,a.register_authority,customer_type,pk_grade,
        income_status,pk_settle_country,bg.grade_value,register_amount,register_pv,pk_center_code,category,
        b.a_balance,b.b_balance,b.place_dept b_place_dept,
        supply_point,supply_income_bonus,
        resale_income_number,resale_income_bonus,expire_date from (
        <include refid="QueryMemberParent"></include>
        ) a
        left join ${settleTableNameDetail} b
        on a.pk_member=b.pk_member
        left join bd_grade bg
        on bg.pk_id = a.pk_grade
    </select>

    <select id="batchCuMemberSettlePlaceParent" resultMap="CuMemberSettleExt">
        select a.pk_member,pk_parent,member_code,member_name,a.pk_place_parent,a.place_dept,
        a.pk_awards,account_status,category,a.register_authority,pk_grade,
        income_status,pk_settle_country,bg.grade_value,register_amount,register_pv,pk_center_code,category,
        b.a_balance,b.b_balance,b.place_dept b_place_dept,b.sum_pv,
        supply_point,supply_income_bonus,
        resale_income_number,resale_income_bonus,expire_date from (
        <foreach collection="pkMemberList" item="item" open=" " close=" " separator=" union all ">
            select pk_member,member_code,member_name,pk_parent,pk_place_parent,place_dept,pk_grade,pk_awards,account_status,
            income_status,pk_settle_country,purchase_status,share_status,register_authority,
            register_amount,register_pv,pk_center_code,category,expire_date,supply_point,supply_income_bonus,
            resale_income_number,resale_income_bonus,level sort
            from ${settleTableName}
            start with pk_member = #{item}
            connect by pk_member = prior pk_place_parent
        </foreach>
        ) a
        left join ${settleTableNameDetail} b
        on a.pk_member=b.pk_member
        left join bd_grade bg
        on bg.pk_id = a.pk_grade
    </select>

    <select id="batchQueryMemberSettleParentBySaOrder"  resultMap="CuMemberSettleExt">
        select a.*,bc.pk_id pk_rate from(
        <foreach collection="saOrderList" item="item" separator=" union ">
        select * from(
        select pk_member,pk_parent,pk_place_parent,place_dept,member_code,member_name,pk_awards,pk_grade,account_status,
               category,income_status,pk_settle_country,
               register_amount,register_pv,pk_center_code,register_authority,supply_point,supply_income_bonus,
            resale_income_number,resale_income_bonus from ${settleTableName}
        start with pk_member= #{item.pkMember}
        connect by pk_member =prior pk_parent
        order by level)
        where rownum &lt;=11
        </foreach>)a
        left join bd_currency bc
        on bc.pk_country=a.pk_settle_country and bc.del_flag=0
    </select>

    <select id="queryCuMemberSettlePlaceDeptUnderUmbrella" resultType="java.lang.Integer">
        select place_dept from settleTableName where pk_place_parent=#{pkMember}
    </select>
    <select id="queryEnoughLeaderCuMemberSettle" resultMap="CuMemberSettleExt">
        select ct.pk_member,member_code,member_name,pk_place_parent,ct.place_dept,pk_grade,ct.pk_awards,bg.grade_value,account_status,
               income_status,pk_settle_country,register_pv,mon_new_pv,register_mon_new_pv,
               category,expire_date,ba.abonus_ratio,cd.place_dept b_place_dept from ${settleTableName} ct
            inner join ${settleTableNameDetail} cd
            on ct.pk_member=cd.pk_member
            inner join BD_AWARDS ba
        on ct.pk_awards=ba.pk_id
            inner join bd_grade bg
            on ct.pk_grade=bg.pk_id
        where ba.abonus_ratio >0
          and ct.category=0 and ct.income_status=0
    </select>

</mapper>
