<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hzs.member.achieve.mapper.CuMemberTreeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettleExt" type="com.hzs.common.domain.member.ext.CuMemberSettleExt">
        <id column="PK_ID" property="pkId"/>
        <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="IS_BUS_LICENSE" property="isBusLicense"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="A_BALANCE" property="aBalance"/>
        <result column="B_BALANCE" property="bBalance"/>
        <result column="R_A_BALANCE" property="rABalance"/>
        <result column="R_B_BALANCE" property="rBBalance"/>
        <result column="A_SUM_AMOUNT" property="aSumAmount"/>
        <result column="B_SUM_AMOUNT" property="bSumAmount"/>
        <result column="A_SUM_PV" property="aSumPv"/>
        <result column="B_SUM_PV" property="bSumPv"/>
        <result column="A_NEW_AMOUNT" property="aNewAmount"/>
        <result column="B_NEW_AMOUNT" property="bNewAmount"/>
        <result column="A_NEW_PV" property="aNewPv"/>
        <result column="B_NEW_PV" property="bNewPv"/>
        <result column="A_SUM_REAL_AMOUNT" property="aSumRealAmount"/>
        <result column="B_SUM_REAL_AMOUNT" property="bSumRealAmount"/>
        <result column="A_SUM_REAL_PV" property="aSumRealPv"/>
        <result column="B_SUM_REAL_PV" property="bSumRealPv"/>
        <result column="REP_A_BALANCE" property="repABalance"/>
        <result column="REP_B_BALANCE" property="repBBalance"/>
        <result column="R_REP_A_BALANCE" property="rRepABalance"/>
        <result column="R_REP_B_BALANCE" property="rRepBBalance"/>
        <result column="REP_A_NEW_AMOUNT" property="repANewAmount"/>
        <result column="REP_B_NEW_AMOUNT" property="repBNewAmount"/>
        <result column="REP_A_NEW_PV" property="repANewPv"/>
        <result column="REP_B_NEW_PV" property="repBNewPv"/>
        <result column="REP_A_SUM_AMOUNT" property="repASumAmount"/>
        <result column="REP_B_SUM_AMOUNT" property="repBSumAmount"/>
        <result column="REP_A_SUM_PV" property="repASumPv"/>
        <result column="REP_B_SUM_PV" property="repBSumPv"/>
        <result column="REGISTER_AMOUNT" property="registerAmount"/>
        <result column="REGISTER_PV" property="registerPv"/>
        <result column="REGISTER_NEW_PV" property="registerNewPv"/>
        <result column="UPGRADE_AMOUNT" property="upgradeAmount"/>
        <result column="UPGRADE_PV" property="upgradePv"/>
        <result column="UPGRADE_NEW_PV" property="upgradeNewPv"/>
        <result column="REPURCHASE_AMOUNT" property="repurchaseAmount"/>
        <result column="REPURCHASE_PV" property="repurchasePv"/>
        <result column="REPURCHASE_NEW_PV" property="repurchaseNewPv"/>
        <result column="HI_FUN_AMOUNT" property="hiFunAmount"/>
        <result column="HI_FUN_PV" property="hiFunPv"/>
        <result column="HI_FUN_NEW_PV" property="hiFunNewPv"/>
        <result column="MALL_AMOUNT" property="mallAmount"/>
        <result column="MALL_PV" property="mallPv"/>
        <result column="MALL_NEW_PV" property="mallNewPv"/>
        <result column="MIN_ACHIEVE" property="minAchieve"/>
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv"/>
        <result column="UPGRADE_Blo_NEW_PV" property="upgradeBloNewPv"/>
        <result column="REPURCHASE_Blo_NEW_PV" property="repurchaseBloNewPv"/>
        <result column="HI_FUN_Blo_NEW_PV" property="hiFunBloNewPv"/>
        <result column="MALL_Blo_NEW_PV" property="mallBloNewPv"/>
        <result column="ROUND" property="round"/>
        <result column="SECOND" property="second"/>
        <result column="a_new_box" property="aNewBox"/>
        <result column="b_new_box" property="bNewBox"/>
        <result column="a_sum_box" property="aSumBox"/>
        <result column="b_sum_box" property="bSumBox"/>
        <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="NICK_NAME" property="nickName"/>
    </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(2),
        member_code       VARCHAR2(30) not null,
        member_name       VARCHAR2(100) not null,
        customer_type     NUMBER(2) not null,
        phone             VARCHAR2(40) not null,
        pk_grade          NUMBER(4) not null,
        pk_awards          NUMBER(4) not null,
        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,
        is_bus_license     NUMBER(1) default 1 not null,
        is_maker_space     NUMBER(1) default 1 not null,
        pk_country        number(4) default 1 not null
    </sql>

    <!-- 结算数据会员网体+业绩数据，用于结算 网体跟业绩冗余在一起-->
    <sql id="CuMemberSettleColumn">
        <include refid="CuMemberTreeColumn"></include>,
        <include refid="CuMemberSettleDetailColumn"></include>,
        <include refid="CuMemberBackColumn"></include>
    </sql>

    <sql id="CuMemberSettleDetailColumn">
        pk_rate NUMBER(6) ,
        a_balance         NUMBER(17,6) default 0 not null,
        b_balance         NUMBER(17,6) default 0 not null,
        a_sum_amount      NUMBER(17,6) default 0 not null,
        b_sum_amount      NUMBER(17,6) default 0 not null,
        a_sum_pv          NUMBER(17,6) default 0 not null,
        b_sum_pv          NUMBER(17,6) default 0 not null,
        a_new_amount      NUMBER(17,6) default 0 not null,
        b_new_amount      NUMBER(17,6) default 0 not null,
        a_new_pv          NUMBER(17,6) default 0 not null,
        b_new_pv          NUMBER(17,6) default 0 not null,
        a_sum_real_amount NUMBER(17,6) default 0 not null,
        b_sum_real_amount NUMBER(17,6) default 0 not null,
        a_sum_real_pv     NUMBER(17,6) default 0 not null,
        b_sum_real_pv     NUMBER(17,6) default 0 not null,
        rep_a_balance     NUMBER(17,6) default 0 not null,
        rep_b_balance     NUMBER(17,6) default 0 not null,
        rep_a_new_amount  NUMBER(17,6) default 0 not null,
        rep_b_new_amount  NUMBER(17,6) default 0 not null,
        rep_a_new_pv      NUMBER(17,6) default 0 not null,
        rep_b_new_pv      NUMBER(17,6) default 0 not null,
        rep_a_sum_amount  NUMBER(17,6) default 0 not null,
        rep_b_sum_amount  NUMBER(17,6) default 0 not null,
        rep_a_sum_pv      NUMBER(17,6) default 0 not null,
        rep_b_sum_pv      NUMBER(17,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,
        upgrade_amount  NUMBER(17,6) default 0 not null,
        upgrade_pv      NUMBER(17,6) default 0 not null,
        upgrade_new_amount       NUMBER(17,6) default 0 not null,
        upgrade_new_pv NUMBER(17,6) default 0 not null,
        repurchase_amount NUMBER(17,6) default 0 not null,
        repurchase_pv   NUMBER(17,6) default 0 not null,
        repurchase_new_amount    NUMBER(17,6) default 0 not null,
        repurchase_new_pv NUMBER(17,6) default 0 not null,
        hi_fun_amount NUMBER(17,6) default 0 not null,
        hi_fun_pv   NUMBER(17,6) default 0 not null,
        hi_fun_new_amount        NUMBER(17,6) default 0 not null,
        hi_fun_new_pv NUMBER(17,6) default 0 not null,
        mall_amount NUMBER(17,6) default 0 not null,
        mall_pv     NUMBER(17,6) default 0 not null,
        mall_new_amount          NUMBER(17,6) default 0 not null,
        mall_new_pv NUMBER(17,6) default 0 not null,
        REGISTER_Blo_NEW_PV  NUMBER(17,6) default 0 not null,
        UPGRADE_Blo_NEW_PV    NUMBER(17,6) default 0 not null,
        REPURCHASE_Blo_NEW_PV    NUMBER(17,6) default 0 not null,
        HI_FUN_Blo_NEW_PV    NUMBER(17,6) default 0 not null,
        MALL_Blo_NEW_PV    NUMBER(17,6) default 0 not null,
        min_achieve       number(17,6) default 0 not null,
        round             NUMBER(6) default 0 not null,
        second            NUMBER(4) default 1 not null
    </sql>

    <sql id="CuMemberBackColumn">
        r_a_balance NUMBER(17,6) default 0 not null,
        r_b_balance         NUMBER(17,6) default 0 not null,
        r_a_sum_amount      NUMBER(17,6) default 0 not null,
        r_b_sum_amount      NUMBER(17,6) default 0 not null,
        r_a_sum_pv          NUMBER(17,6) default 0 not null,
        r_b_sum_pv          NUMBER(17,6) default 0 not null,
        r_a_sum_real_amount NUMBER(17,6) default 0 not null,
        r_b_sum_real_amount NUMBER(17,6) default 0 not null,
        r_a_sum_real_pv     NUMBER(17,6) default 0 not null,
        r_b_sum_real_pv     NUMBER(17,6) default 0 not null,
        r_rep_a_balance     NUMBER(17,6) default 0 not null,
        r_rep_b_balance     NUMBER(17,6) default 0 not null,
        r_rep_a_sum_amount  NUMBER(17,6) default 0 not null,
        r_rep_b_sum_amount  NUMBER(17,6) default 0 not null,
        r_rep_a_sum_pv      NUMBER(17,6) default 0 not null,
        r_rep_b_sum_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,is_maker_space,
            a_balance,b_balance,r_a_balance,r_b_balance,rep_a_balance,rep_b_balance,r_rep_a_balance,r_rep_b_balance,
            register_amount,register_pv,upgrade_amount,upgrade_pv,repurchase_amount,pk_center_code,
            repurchase_pv,hi_fun_amount,hi_fun_pv,mall_amount,mall_pv,category,pk_grade,
            min_achieve,round,second
            from ${settleTableName}
            where pay_status = 1
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
        </foreach>
    </sql>

    <select id="queryExistOracleTable" resultType="java.lang.Integer">
        select 1
        from user_tables
        where table_name = upper(#{tableName})
    </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="queryCuMemberTreeByPkMemberTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
            t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
            t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
            t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,t.UPGRADE_AMOUNT,t.UPGRADE_PV,
            t.UPGRADE_NEW_PV,t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.HI_FUN_AMOUNT,t.HI_FUN_PV,t.HI_FUN_NEW_PV,t.MALL_AMOUNT,
            t.MALL_PV ,t.MALL_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND, t.a_new_box, t.b_new_box, t.a_sum_box, t.b_sum_box ,
        </if>
        cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode, grade.GRADE_NAME, awards.AWARDS_NAME,
        (select count(cr.pk_id) from CU_MEMBER cr where cr.pk_parent = cm.pk_id and cr.del_flag = 0)
        directPushNumber,cm.PK_VERTEX ,cm.NICK_NAME
        from
        CU_MEMBER cm
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        left join BD_GRADE grade on grade.PK_ID = cm.PK_SETTLE_GRADE
        left join BD_AWARDS awards on awards.PK_ID = cm.PK_AWARDS
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t on cm.PK_ID = t.PK_MEMBER
        </if>
        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG = 0
    </select>

    <select id="queryCuMemberTreeParentTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            t.PK_RATE,
            t.A_BALANCE,
            t.B_BALANCE,
            t.A_SUM_AMOUNT,
            t.B_SUM_AMOUNT,
            t.A_SUM_PV,
            t.B_SUM_PV,
            t.A_NEW_AMOUNT,
            t.B_NEW_AMOUNT,
            t.A_NEW_PV,
            t.B_NEW_PV,
            t.A_SUM_REAL_AMOUNT,
            t.B_SUM_REAL_AMOUNT,
            t.A_SUM_REAL_PV,
            t.B_SUM_REAL_PV,
            t.REP_A_BALANCE,
            t.REP_B_BALANCE,
            t.REP_A_NEW_AMOUNT,
            t.REP_B_NEW_AMOUNT,
            t.REP_A_NEW_PV,
            t.REP_B_NEW_PV,
            t.REP_A_SUM_AMOUNT,
            t.REP_B_SUM_AMOUNT,
            t.REP_A_SUM_PV,
            t.REP_B_SUM_PV,
            t.REGISTER_AMOUNT,
            t.REGISTER_PV,
            t.REGISTER_NEW_PV,
            t.UPGRADE_AMOUNT,
            t.UPGRADE_PV,
            t.UPGRADE_NEW_PV,
            t.REPURCHASE_AMOUNT,
            t.REPURCHASE_PV,
            t.REPURCHASE_NEW_PV,
            t.HI_FUN_AMOUNT,
            t.HI_FUN_PV,
            t.HI_FUN_NEW_PV,
            t.MALL_AMOUNT,
            t.MALL_PV,
            t.MALL_NEW_PV,
            t.MIN_ACHIEVE,
            t.ROUND,
            t.SECOND,
        </if>
        cm.*, cme.MEMBER_CODE parentMemberCode from ( select
        level,cm.PK_ID,
        cm.PK_COUNTRY,
        cm.PK_PLACE_PARENT,
        cm.PK_PARENT,
        cm.PLACE_DEPT,
        cm.MEMBER_NAME,
        cm.MEMBER_CODE,
        cm.PK_ID PK_MEMBER,
        cm.PK_SETTLE_COUNTRY,
        cm.PK_VERTEX ,cm.NICK_NAME
        from CU_MEMBER cm
        where 1 = 1
        <if test="level != null">
            and level &lt; #{level}
        </if>
        <!-- 体系权限处理 -->
        <if test="vertexIdList != null and vertexIdList.size > 0">
            and cm.PK_VERTEX in
            <foreach collection="vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and cm.del_flag = 0
        start with cm.PK_PARENT = #{pkMember}
        connect by cm.PK_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC ) cm
        left join CU_MEMBER cme
        on cme.PK_ID = cm.PK_PARENT
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t
            on cm.PK_ID = t.PK_MEMBER
        </if>
    </select>

    <select id="queryCuMemberTreeParentTableCount" resultType="long">
        select count(PK_MEMBER)
        from ${tableName} t
                 left join CU_MEMBER cm on cm.pk_id = t.PK_MEMBER
        where cm.PK_PARENT = #{pkMember}
    </select>

    <select id="queryCuMemberTreePlaceParentHistoryTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
            t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
            t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
            t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,t.UPGRADE_AMOUNT,t.UPGRADE_PV,
            t.UPGRADE_NEW_PV,t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.HI_FUN_AMOUNT,t.HI_FUN_PV,t.HI_FUN_NEW_PV,t.MALL_AMOUNT,
            t.MALL_PV ,t.MALL_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND,
        </if>
        level,cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode
        from CU_MEMBER cm
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t on cm.PK_ID = t.PK_MEMBER
        </if>
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        where level &lt; #{level}
        <if test="memberIdList.size() > 0 and memberIdList != ''">
            and cm.PK_ID in
            <foreach collection="memberIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        start with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC
    </select>

    <select id="selectByTreeMemberList" resultMap="CuMemberSettleExt">
        select t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
        t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
        t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
        t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,t.UPGRADE_AMOUNT,t.UPGRADE_PV,
        t.UPGRADE_NEW_PV,t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.HI_FUN_AMOUNT,t.HI_FUN_PV,t.HI_FUN_NEW_PV,t.MALL_AMOUNT,
        t.MALL_PV ,t.MALL_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND, t.a_new_box, t.b_new_box, t.a_sum_box, t.b_sum_box,
        level,cm.PK_AWARDS,cm.PK_SETTLE_COUNTRY,
        cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cme.MEMBER_CODE parentMemberCode,
        cm.PK_VERTEX ,cm.NICK_NAME
        from CU_MEMBER cm
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        left join ${tableName} t on cm.PK_ID = t.PK_MEMBER
        where level &lt; #{level} and cm.del_flag = 0
        <!-- 体系权限处理 -->
        <if test="vertexIdList != null and vertexIdList.size > 0">
            and cm.PK_VERTEX in
            <foreach collection="vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        start
        with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior cm.PK_ID
        order by cm.PLACE_DEPT
    </select>

    <select id="selectByTreeMemberListFive" resultMap="CuMemberSettleExt">
        select level,cm.PK_AWARDS,cm.PK_SETTLE_COUNTRY,
        cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cme.MEMBER_CODE parentMemberCode, grade.GRADE_NAME gradeName, awards.AWARDS_NAME
        from CU_MEMBER cm
        left join BD_GRADE grade on cm.PK_SETTLE_GRADE = grade.PK_ID
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PARENT
        left join BD_AWARDS awards on cm.PK_AWARDS = awards.PK_ID
        where level &lt; #{level} and cm.del_flag = 0
        <!-- 体系权限处理 -->
        <if test="vertexIdList != null and vertexIdList.size > 0">
            and cm.PK_VERTEX in
            <foreach collection="vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        start
        with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior cm.PK_ID
        order by cm.PLACE_DEPT
    </select>

    <select id="selectByselectByTreeMemberCount" resultType="long">
        select count(PK_MEMBER)
        from ${tableName}
        where pk_member = #{pkMember}
    </select>

    <select id="brollyBelowResettle" resultMap="CuMemberSettleExt">
        select t.*, cm.PK_PLACE_PARENT, cm.PK_PARENT, level, cm.PLACE_DEPT
        from ${tableName} t
                 left join CU_MEMBER cm on cm.PK_ID = t.PK_MEMBER
        where cm.DEL_FLAG = 0
            start
        with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior t.pk_member
        order by cm.PLACE_DEPT ASC
    </select>

    <select id="queryCuMemberTreeByPlacePkMemberTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
            t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
            t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
            t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,t.UPGRADE_AMOUNT,t.UPGRADE_PV,
            t.UPGRADE_NEW_PV,t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.HI_FUN_AMOUNT,t.HI_FUN_PV,t.HI_FUN_NEW_PV,t.MALL_AMOUNT,
            t.MALL_PV ,t.MALL_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND,
        </if>
        cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode,
        cm.PK_VERTEX ,cm.NICK_NAME
        from
        CU_MEMBER cm
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PARENT
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t on cm.PK_ID = t.PK_MEMBER
        </if>
        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG = 0
    </select>

    <select id="queryByMemberParentCount" resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select count(cr.pk_id) parentMemberCount,cr.PK_PARENT pkMember from CU_MEMBER cr where cr.PK_PARENT in

        <trim suffixOverrides=" OR PK_ID IN()">    <!-- 表示删除最后一个条件 -->
            <foreach collection="memberIds" item="Id" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR cr.PK_PARENT IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{Id}
            </foreach>
        </trim>
        and cr.del_flag = 0
        group by cr.PK_PARENT
    </select>

    <select id="queryByMemberParentSumPv" resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select sum(cr.REGISTER_PV + cr.UPGRADE_PV + cr.REPURCHASE_PV + cr.HI_FUN_PV + cr.MALL_PV)
        parentMemberSumPv,cm.PK_PARENT pkMember from ${tableName} cr
        left join CU_MEMBER cm on cm.PK_ID = cr.PK_MEMBER
        where cm.del_flag = 0 and
        cm.PK_PARENT in
        <trim suffixOverrides=" OR cm.PK_PARENT IN()">    <!-- 表示删除最后一个条件 -->
            <foreach collection="memberIds" item="Id" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR cm.PK_PARENT IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{Id}
            </foreach>
        </trim>
        group by cm.PK_PARENT
    </select>

    <select id="queryCuMemberTreePlaceParentHistoryFramework" resultMap="CuMemberSettleExt">
        select
        t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
        t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
        t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
        t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,t.UPGRADE_AMOUNT,t.UPGRADE_PV,
        t.UPGRADE_NEW_PV,t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.HI_FUN_AMOUNT,t.HI_FUN_PV,t.HI_FUN_NEW_PV,t.MALL_AMOUNT,
        t.MALL_PV ,t.MALL_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND, t.a_new_box, t.b_new_box, t.a_sum_box, t.b_sum_box,
        level,cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode
        from ${tableName} t
        left join CU_MEMBER cm on cm.PK_ID = t.PK_MEMBER
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        where level &lt; #{level}
        <if test="memberIdList.size() > 0 and memberIdList != ''">
            and cm.PK_ID in
            <foreach collection="memberIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        start with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC
    </select>

    <select id="queryByMemberParentAndMemberSettleCount"
            resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select count(cr.PK_MEMBER) parentMemberCount,cr.PK_PARENT pkMember from ${settleTableName} cr where cr.PK_PARENT
        in
        <trim suffixOverrides=" OR PK_ID IN()">    <!-- 表示删除最后一个条件 -->
            <foreach collection="memberIds" item="Id" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR cr.PK_PARENT IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{Id}
            </foreach>
        </trim>
        group by cr.PK_PARENT
    </select>

    <select id="queryByMemberParentSettleSumPv" resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select sum(cr.REGISTER_PV + cr.UPGRADE_PV + cr.REPURCHASE_PV + cr.HI_FUN_PV + cr.MALL_PV)
        parentMemberSumPv,cr.PK_PARENT pkMember from ${tableName} cr
        where cr.PK_PARENT in
        <foreach collection="memberIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by cr.PK_PARENT
    </select>

    <!-- 查询会员显示业绩 -->
    <select id="queryMemberPv" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        select a.*
        <if test="monthTable != null and monthTable != ''">
            , b.a_sum_pv aMonthPv, b.b_sum_pv bMonthPv
        </if>
        from (select
        cms.pk_member,
        cms.a_sum_pv,
        cms.b_sum_pv,
        cms.a_sum_real_pv,
        cms.b_sum_real_pv,
        cms.a_new_pv + cms.rep_a_new_pv a_new_pv,
        cms.b_new_pv + cms.rep_b_new_pv b_new_pv
        from ${settleTable} cms
        where cms.pk_member = #{pkMember}) a
        <if test="monthTable != null and monthTable != ''">
            left join (select cmm.pk_member, cmm.a_sum_pv, cmm.b_sum_pv
            from ${monthTable} cmm
            where cmm.pk_member = #{pkMember}) b
            on a.pk_member = b.pk_member
        </if>
    </select>

    <!-- 查询会员当天业绩 -->
    <select id="queryMemberTodayPv" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        select a.order_achieve a_new_pv, b.order_achieve b_new_pv
        from (select nvl(sum(so.order_achieve), 0) order_achieve
              from sa_order so
              where so.del_flag = 0
                and so.order_status = 1
                and to_char(so.pay_time, 'yyyy-mm-dd') =
                    to_char(sysdate, 'yyyy-mm-dd')
                and so.order_type in (1, 2, 3, 24, 25, 26, 10)
                and so.pk_member in
                    (select cm.pk_id
                     from cu_member cm
              start with cm.pk_place_parent = #{pkMember}
                     and cm.place_dept = 1
                     and cm.del_flag = 0
              connect by prior cm.pk_id = cm.pk_place_parent) ) a
  left join (select nvl(sum(so.order_achieve), 0) order_achieve
               from sa_order so
              where so.del_flag = 0
                and so.order_status = 1
                and to_char(so.pay_time, 'yyyy-mm-dd') =
                    to_char(sysdate, 'yyyy-mm-dd')
                and so.order_type in (1, 2, 3, 24, 25, 26, 10)
                and so.pk_member in
                    (select cm.pk_id
                       from cu_member cm
                      start with cm.pk_place_parent = #{pkMember}
            and cm.place_dept = 2
            and cm.del_flag = 0
            connect by prior cm.pk_id = cm.pk_place_parent)) b
        on 1 = 1
    </select>

</mapper>
