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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberAchieve" type="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="A_BALANCE" property="aBalance"/>
        <result column="B_BALANCE" property="bBalance"/>
        <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="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="REPURCHASE_AMOUNT" property="repurchaseAmount"/>
        <result column="REPURCHASE_PV" property="repurchasePv"/>
        <result column="REPURCHASE_NEW_PV" property="repurchaseNewPv"/>
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv" />
        <result column="REPURCHASE_Blo_NEW_PV" property="repurchaseBloNewPv" />
        <result column="R_A_BALANCE" property="rABalance"/>
        <result column="R_B_BALANCE" property="rBBalance"/>
        <result column="R_A_SUM_AMOUNT" property="rASumAmount"/>
        <result column="R_B_SUM_AMOUNT" property="rBSumAmount"/>
        <result column="R_A_SUM_PV" property="rASumPv"/>
        <result column="R_B_SUM_PV" property="rBSumPv"/>
        <result column="R_A_SUM_REAL_AMOUNT" property="rASumRealAmount"/>
        <result column="R_B_SUM_REAL_AMOUNT" property="rBSumRealAmount"/>
        <result column="R_A_SUM_REAL_PV" property="rASumRealPv"/>
        <result column="R_B_SUM_REAL_PV" property="rBSumRealPv"/>
        <result column="ALGEBRA" property="algebra"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="TEAM_NAME" property="teamName"/>
        <result column="ASSESS_STATUS" property="assessStatus"/>
    </resultMap>

    <insert id="insertCuMemberAchieveBySettleDate">
        insert into ${targetTableName}(period,pk_member,pk_rate,a_balance,b_balance,a_sum_amount,
                                       b_sum_amount,a_sum_pv,b_sum_pv,a_new_amount,b_new_amount,a_new_pv,
                                       b_new_pv,a_sum_real_amount,b_sum_real_amount,a_sum_real_pv,b_sum_real_pv,
                                       rep_a_balance,rep_b_balance,rep_a_new_amount,rep_b_new_amount,rep_a_new_pv,
                                       rep_b_new_pv,rep_a_sum_amount,rep_b_sum_amount,rep_a_sum_pv,rep_b_sum_pv,
                                       register_amount,register_pv,register_new_amount,register_new_pv,
                                       repurchase_amount,repurchase_pv,repurchase_new_amount,repurchase_new_pv,
                                       r_a_balance,r_b_balance,r_a_sum_amount,r_b_sum_amount,r_a_sum_pv,r_b_sum_pv,
                                       r_a_sum_real_amount,r_b_sum_real_amount,r_a_sum_real_pv,r_b_sum_real_pv,
                                       pk_country)
        select #{period} period,pk_member,nvl(pk_rate,0) pk_rate,a_balance,b_balance,a_sum_amount,
               b_sum_amount,a_sum_pv,b_sum_pv,a_new_amount,b_new_amount,a_new_pv,
               b_new_pv,a_sum_real_amount,b_sum_real_amount,a_sum_real_pv,b_sum_real_pv,
               rep_a_balance,rep_b_balance,rep_a_new_amount,rep_b_new_amount,rep_a_new_pv,
               rep_b_new_pv,rep_a_sum_amount,rep_b_sum_amount,rep_a_sum_pv,rep_b_sum_pv,
               register_amount,register_pv,register_new_amount,register_new_pv,
               repurchase_amount,repurchase_pv,repurchase_new_amount,repurchase_new_pv,
               r_a_balance,r_b_balance,r_a_sum_amount,r_b_sum_amount,r_a_sum_pv,r_b_sum_pv,
               r_a_sum_real_amount,r_b_sum_real_amount,r_a_sum_real_pv,r_b_sum_real_pv,
               pk_country from ${sourceTableName}
    </insert>
    <update id="createCuMemberAchieveTable">
        create table ${tableName}
        (
            pk_id         NUMBER(20) primary key,
            period            number(6) not null,
            pk_member         NUMBER(20) not null,
            pk_rate           NUMBER(6) not null,
            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,
            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,
            REGISTER_Blo_NEW_PV  NUMBER(17,6) default 0 not null,
            REPURCHASE_Blo_NEW_PV    NUMBER(17,6) default 0 not null,
            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,
            pk_country        NUMBER(6) not null
        )
    </update>

    <!--每日更新累计金额-->
    <update id="updateCuMemberBackOldAchieveEveryDay">
        merge into ${tableName} a
        using (
        select pk_member,max(a.pk_rate) pk_rate,sum(a_new_amount/bc.out_exchange_rate) a_new_amount,sum(a_new_pv)
        a_new_pv,
        sum(b_new_amount/bc.out_exchange_rate) b_new_amount,sum(b_new_pv) b_new_pv,
        sum(rep_a_new_amount/bc.out_exchange_rate) rep_a_new_amount,sum(rep_b_new_amount/bc.out_exchange_rate)
        rep_b_new_amount,
        sum(rep_a_new_pv) rep_a_new_pv,sum(rep_b_new_pv) rep_b_new_pv,sum(a_new_real_amount/bc.out_exchange_rate)
        a_new_real_amount,
        sum(a_new_real_pv) a_new_real_pv,sum(b_new_real_amount/bc.out_exchange_rate)
        b_new_real_amount,sum(b_new_real_pv) b_new_real_pv
        from (
        <foreach collection="memberAchieveParamList" item="achieve" open=" " separator=" union all " close=" ">
            select pk_place_parent pk_member,#{achieve.pkRate} pk_rate,
            (case when place_dept =1 then #{achieve.newAmount} else 0 end) a_new_amount,
            (case when place_dept =1 then #{achieve.newPv} else 0 end) a_new_pv,
            (case when place_dept =1 then #{achieve.repNewAmount} else 0 end) rep_a_new_amount,
            (case when place_dept =1 then #{achieve.repNewPv} else 0 end) rep_a_new_pv,
            (case when place_dept =1 then #{achieve.newRealAmount} else 0 end) a_new_real_amount,
            (case when place_dept =1 then #{achieve.newRealPv} else 0 end) a_new_real_pv,
            (case when place_dept =2 then #{achieve.newAmount} else 0 end) b_new_amount,
            (case when place_dept =2 then #{achieve.newPv} else 0 end) b_new_pv,
            (case when place_dept =2 then #{achieve.repNewAmount} else 0 end) rep_b_new_amount,
            (case when place_dept =2 then #{achieve.repNewPv} else 0 end) rep_b_new_pv,
            (case when place_dept =2 then #{achieve.newRealAmount} else 0 end) b_new_real_amount,
            (case when place_dept =2 then #{achieve.newRealPv} else 0 end) b_new_real_pv
            from(
            select pk_place_parent,place_dept from(
            select * from ${tableName}
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_place_parent
            )
            where nvl(pk_place_parent,0) !=0
            )
        </foreach>
        ) a
        inner join bd_currency bc
        on a.pk_rate = bc.pk_id
        group by pk_member
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update
        set a.period = a.period + 1,
        a.a_sum_pv = a.a_sum_pv - b.a_new_pv,
        a.b_sum_pv = a.b_sum_pv - b.b_new_pv,
        a.rep_a_sum_pv = a.rep_a_sum_pv - b.rep_a_new_pv,
        a.rep_b_sum_pv = a.rep_b_sum_pv - b.rep_b_new_pv,
        a.a_sum_real_pv = a.a_sum_real_pv - b.a_new_real_pv,
        a.b_sum_real_pv = a.b_sum_real_pv - b.b_new_real_pv
    </update>

    <!--每日更新累计金额-->
    <update id="updateCuMemberBackAchieveEveryDay">
        merge into ${tableName} a
        using (
        select pk_member,max(a.pk_rate) pk_rate,sum(a_new_amount/bc.out_exchange_rate) a_new_amount,sum(a_new_pv)
        a_new_pv,
        sum(b_new_amount/bc.out_exchange_rate) b_new_amount,sum(b_new_pv) b_new_pv,
        sum(rep_a_new_amount/bc.out_exchange_rate) rep_a_new_amount,sum(rep_b_new_amount/bc.out_exchange_rate)
        rep_b_new_amount,
        sum(rep_a_new_pv) rep_a_new_pv,sum(rep_b_new_pv) rep_b_new_pv,sum(a_new_real_amount/bc.out_exchange_rate)
        a_new_real_amount,
        sum(a_new_real_pv) a_new_real_pv,sum(b_new_real_amount/bc.out_exchange_rate)
        b_new_real_amount,sum(b_new_real_pv) b_new_real_pv
        from (
        <foreach collection="memberAchieveParamList" item="achieve" open=" " separator=" union all " close=" ">
            select pk_place_parent pk_member,#{achieve.pkRate} pk_rate,
            (case when place_dept =1 then #{achieve.newAmount} else 0 end) a_new_amount,
            (case when place_dept =1 then #{achieve.newPv} else 0 end) a_new_pv,
            (case when place_dept =1 then #{achieve.repNewAmount} else 0 end) rep_a_new_amount,
            (case when place_dept =1 then #{achieve.repNewPv} else 0 end) rep_a_new_pv,
            (case when place_dept =1 then #{achieve.newRealAmount} else 0 end) a_new_real_amount,
            (case when place_dept =1 then #{achieve.newRealPv} else 0 end) a_new_real_pv,
            (case when place_dept =2 then #{achieve.newAmount} else 0 end) b_new_amount,
            (case when place_dept =2 then #{achieve.newPv} else 0 end) b_new_pv,
            (case when place_dept =2 then #{achieve.repNewAmount} else 0 end) rep_b_new_amount,
            (case when place_dept =2 then #{achieve.repNewPv} else 0 end) rep_b_new_pv,
            (case when place_dept =2 then #{achieve.newRealAmount} else 0 end) b_new_real_amount,
            (case when place_dept =2 then #{achieve.newRealPv} else 0 end) b_new_real_pv
            from(
            select pk_place_parent,place_dept from(
            select * from ${tableName}
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_place_parent
            )
            where nvl(pk_place_parent,0) !=0
            )
        </foreach>
        ) a
        inner join bd_currency bc
        on a.pk_rate = bc.pk_id
        group by pk_member
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.period = a.period + 1,
        a.pk_rate = b.pk_rate,
        a.a_sum_pv = a.a_sum_pv + b.a_new_pv,
        a.b_sum_pv = a.b_sum_pv + b.b_new_pv,
        a.rep_a_sum_pv = a.rep_a_sum_pv + b.rep_a_new_pv,
        a.rep_b_sum_pv = a.rep_b_sum_pv + b.rep_b_new_pv,
        a.a_sum_real_pv = a.a_sum_real_pv + b.a_new_real_pv,
        a.b_sum_real_pv = a.b_sum_real_pv + b.b_new_real_pv,
        a.r_a_sum_amount = a.r_a_sum_amount + b.a_new_amount,
        a.r_b_sum_amount = a.r_b_sum_amount + b.b_new_amount,
        a.r_a_sum_pv = a.r_a_sum_pv +b.a_new_pv,
        a.r_b_sum_pv = a.r_b_sum_pv + b.b_new_pv,
        a.r_a_sum_real_amount = a.r_a_sum_real_amount + b.a_new_real_amount,
        a.r_b_sum_real_amount = a.r_b_sum_real_amount + b.b_new_real_amount,
        a.r_a_sum_real_pv = a.r_a_sum_real_pv + b.a_new_real_pv,
        a.r_b_sum_real_pv = a.r_b_sum_real_pv + b.b_new_real_pv
    </update>

    <!--将昨天的各种金额作为今天的期初-->
    <update id="updateCuMemberSettleAchieveByBeforeDay">
        merge into ${targetTable} a
            using(select pk_member,a_balance,b_balance,
                         a_sum_amount,b_sum_amount,a_sum_pv,b_sum_pv,
                         a_new_amount,b_new_amount,a_new_pv,b_new_pv,
                         a_sum_real_amount,b_sum_real_amount,a_sum_real_pv,b_sum_real_pv,
                         rep_a_balance,rep_b_balance,
                         rep_a_sum_amount,rep_b_sum_amount,rep_a_sum_pv,rep_b_sum_pv,
                         register_amount,register_pv,repurchase_amount,repurchase_pv,pk_country,min_achieve,round,second,
                      r_a_balance,r_b_balance,r_a_sum_amount,r_b_sum_amount,r_a_sum_pv,r_b_sum_pv,
                      r_a_sum_real_amount,r_b_sum_real_amount,
                      r_a_sum_real_pv,r_b_sum_real_pv from ${sourceTable} ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set
                    a.a_balance=b.a_balance,a.b_balance=b.b_balance,
                    a.a_sum_amount=b.a_sum_amount,a.b_sum_amount=b.b_sum_amount,
                    a.a_sum_pv=b.a_sum_pv,a.b_sum_pv=b.b_sum_pv,
                    a.a_new_amount=b.a_new_amount,a.b_new_amount=b.b_new_amount,
                    a.a_new_pv=b.a_new_pv,a.b_new_pv=b.b_new_pv,
                    a.a_sum_real_amount=b.a_sum_real_amount,a.b_sum_real_amount=b.b_sum_real_amount,
                    a.a_sum_real_pv=b.a_sum_real_pv,a.b_sum_real_pv=b.b_sum_real_pv,
                    a.rep_a_balance=b.rep_a_balance,a.rep_b_balance=b.rep_b_balance,
                    a.rep_a_sum_pv=b.rep_a_sum_pv,a.rep_b_sum_pv=b.rep_b_sum_pv,
                    a.register_amount=b.register_amount,a.register_pv=b.register_pv,
                    a.repurchase_amount=b.repurchase_amount,a.repurchase_pv=b.repurchase_pv,
                    a.min_achieve=b.min_achieve,a.round=b.round,a.second=b.second,a.r_a_balance=b.r_a_balance,a.r_b_balance=b.r_b_balance,
                    a.r_a_sum_amount = b.r_a_sum_amount,a.r_b_sum_amount = b.r_b_sum_amount,a.r_a_sum_pv = b.r_a_sum_pv,a.r_b_sum_pv = b.r_b_sum_pv,
                    a.r_a_sum_real_amount = b.r_a_sum_real_amount,a.r_b_sum_real_amount = b.r_b_sum_real_amount,
                    a.r_a_sum_real_pv = b.r_a_sum_real_pv,a.r_b_sum_real_pv = b.r_b_sum_real_pv
    </update>
    <update id="updateCuMemberSettleAchieveBySpecialBus">
        merge into ${targetTable} a
            using(select cl.pk_member,
                         (case infusing_water_type when  1 then ca.a_sum_real_pv + a_sum_real_modify_pv  when 2 then ca.a_sum_real_pv- a_sum_real_modify_pv else a_sum_real_modify_pv end) a_sum_real_pv,
                         (case infusing_water_type when  1 then ca.b_sum_real_pv +b_sum_real_modify_pv when 2 then ca.b_sum_real_pv- b_sum_real_modify_pv else b_sum_real_modify_pv end) b_sum_real_pv,
                         (case infusing_water_type when  1 then ca.a_sum_pv + a_first_modify_pv  when 2 then ca.a_sum_pv- a_first_modify_pv else a_first_modify_pv end) a_sum_pv,
                         (case infusing_water_type when  1 then ca.b_sum_pv + b_first_modify_pv  when 2 then ca.b_sum_pv- b_first_modify_pv else b_first_modify_pv end) b_sum_pv,
                         (case infusing_water_type when  1 then ca.a_balance + rep_a_first_modify_balance when 2 then ca.a_balance- rep_a_first_modify_balance else rep_a_first_modify_balance end) a_balance,
                         (case infusing_water_type when  1 then ca.b_balance + rep_b_first_modify_balance when 2 then ca.b_balance- rep_b_first_modify_balance else rep_b_first_modify_balance end) b_balance,
                         (case infusing_water_type when  1 then ca.rep_a_sum_pv + rep_a_modify_sum_pv when 2 then ca.rep_a_sum_pv- rep_a_modify_sum_pv else rep_a_modify_sum_pv end) rep_a_sum_pv,
                         (case infusing_water_type when  1 then ca.rep_b_sum_pv + rep_b_modify_sum_pv when 2 then ca.rep_b_sum_pv- rep_b_modify_sum_pv else rep_b_modify_sum_pv end) rep_b_sum_pv,
                         (case infusing_water_type when  1 then ca.rep_a_balance + rep_a_modify_balance when 2 then ca.rep_a_balance- rep_a_modify_balance else rep_a_modify_balance end) rep_a_balance,
                         (case infusing_water_type when  1 then ca.rep_b_balance + rep_b_modify_balance when 2 then ca.rep_b_balance- rep_b_modify_balance else rep_b_modify_balance end) rep_b_balance from cu_member_achieve_log cl
                                                                                                                                                                                                                 left join ${targetTable} ca
                                                                                                                                                                                                                           on cl.pk_member = ca.pk_member
                  where cl.period= #{period} and approve_status=5 and cl.pk_id = #{pkId}
                  order by cl.pk_member,cl.creation_time) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.a_sum_real_pv = b.a_sum_real_pv, a.b_sum_real_pv=b.b_sum_real_pv,a.a_sum_pv=b.a_sum_pv,a.b_sum_pv=b.b_sum_pv,
                    a.a_balance=b.a_balance,a.b_balance=b.b_balance,a.rep_a_sum_pv=b.rep_a_sum_pv,a.rep_b_sum_pv=b.rep_b_sum_pv,
                    a.rep_a_balance=b.rep_a_balance,a.rep_b_balance=b.rep_b_balance
    </update>
    <update id="updateCuMemberSettleAchieveByDueDate">
        merge into ${currentTableName} a
            using (
                select ct.pk_member,r_a_balance,r_b_balance,r_a_sum_pv,r_b_sum_pv,r_a_sum_real_pv,r_b_sum_real_pv from ${yesTableName} ct
                                                                                                                           inner join CU_MEMBER_ACHIEVE_LOG cl
                                                                                                                                      on ct.pk_member = cl.pk_member
                where cl.due_date = #{currentDate, jdbcType=DATE}
            ) b
            on (a.pk_member = b.pk_member)
            when matched then
                update set a.r_a_balance=b.r_a_balance,a.r_b_balance=b.r_b_balance,
                    a.r_a_sum_pv=b.r_a_sum_pv,a.r_b_sum_pv=b.r_b_sum_pv,
                    a.r_a_sum_real_pv=b.r_a_sum_real_pv,a.r_b_sum_real_pv=b.r_b_sum_real_pv
    </update>
    <update id="mergeCuMemberAchieveBySettleDate">
        merge into ${targetTableName} a
            using(
                select #{period} period,pk_member,nvl(pk_rate,0) pk_rate,a_balance,b_balance,a_sum_amount,
                       b_sum_amount,a_sum_pv,b_sum_pv,a_new_amount,b_new_amount,a_new_pv,
                       b_new_pv,a_sum_real_amount,b_sum_real_amount,a_sum_real_pv,b_sum_real_pv,
                       rep_a_balance,rep_b_balance,rep_a_new_amount,rep_b_new_amount,rep_a_new_pv,
                       rep_b_new_pv,rep_a_sum_amount,rep_b_sum_amount,rep_a_sum_pv,rep_b_sum_pv,
                       register_amount,register_pv,register_new_amount,register_new_pv,
                       repurchase_amount,repurchase_pv,repurchase_new_amount,repurchase_new_pv,
                       register_blo_new_pv,upgrade_blo_new_pv,repurchase_blo_new_pv,
                       r_a_balance,r_b_balance,r_a_sum_amount,r_b_sum_amount,r_a_sum_pv,r_b_sum_pv,
                       r_a_sum_real_amount,r_b_sum_real_amount,r_a_sum_real_pv,r_b_sum_real_pv,
                       pk_country from ${sourceTableName}
            ) b
            on (a.pk_member=b.pk_member and a.period=b.period)
            WHEN MATCHED THEN
                update set a.pk_rate = b.pk_rate,a.a_balance = b.a_balance,a.b_balance = b.b_balance,
                    a.a_sum_amount = b.a_sum_amount,a.b_sum_amount = b.b_sum_amount,
                    a.a_sum_pv = b.a_sum_pv,a.b_sum_pv = b.b_sum_pv,a.a_new_amount = b.a_new_amount,
                    a.b_new_amount = b.b_new_amount,a.a_new_pv = b.a_new_pv,a.b_new_pv = b.b_new_pv,
                    a.a_sum_real_amount = b.a_sum_real_amount,a.b_sum_real_amount = b.b_sum_real_amount,
                    a.a_sum_real_pv = b.a_sum_real_pv,a.b_sum_real_pv = b.b_sum_real_pv,
                    a.rep_a_balance = b.rep_a_balance,a.rep_b_balance = b.rep_b_balance,
                    a.rep_a_new_amount = b.rep_a_new_amount,a.rep_b_new_amount = b.rep_b_new_amount,
                    a.rep_a_new_pv = b.rep_a_new_pv,a.rep_b_new_pv = b.rep_b_new_pv,
                    a.rep_a_sum_amount = b.rep_a_sum_amount,a.rep_b_sum_amount = b.rep_b_sum_amount,
                    a.rep_a_sum_pv = b.rep_a_sum_pv,a.rep_b_sum_pv = b.rep_b_sum_pv,
                    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.repurchase_amount = b.repurchase_amount,a.repurchase_pv = b.repurchase_pv,
                    a.repurchase_new_amount = b.repurchase_new_amount,a.repurchase_new_pv = b.repurchase_new_pv,
                    a.register_blo_new_pv=b.register_blo_new_pv,a.upgrade_blo_new_pv=b.upgrade_blo_new_pv,
                    a.repurchase_blo_new_pv=b.repurchase_blo_new_pv,
                    a.r_a_balance = b.r_a_balance,a.r_b_balance = b.r_b_balance,a.r_a_sum_amount = b.r_a_sum_amount,
                    a.r_b_sum_amount = b.r_b_sum_amount,a.r_a_sum_pv = b.r_a_sum_pv,a.r_b_sum_pv = b.r_b_sum_pv,
                    a.r_a_sum_real_amount = b.r_a_sum_real_amount,a.r_b_sum_real_amount = b.r_b_sum_real_amount,
                    a.r_a_sum_real_pv = b.r_a_sum_real_pv,a.r_b_sum_real_pv = b.r_b_sum_real_pv,
                    a.pk_country = b.pk_country
            WHEN NOT MATCHED THEN
                insert (period,pk_member,pk_rate,a_balance,b_balance,a_sum_amount,
                        b_sum_amount,a_sum_pv,b_sum_pv,a_new_amount,b_new_amount,a_new_pv,
                        b_new_pv,a_sum_real_amount,b_sum_real_amount,a_sum_real_pv,b_sum_real_pv,
                        rep_a_balance,rep_b_balance,rep_a_new_amount,rep_b_new_amount,rep_a_new_pv,
                        rep_b_new_pv,rep_a_sum_amount,rep_b_sum_amount,rep_a_sum_pv,rep_b_sum_pv,
                        register_amount,register_pv,register_new_amount,register_new_pv,
                        repurchase_amount,repurchase_pv,repurchase_new_amount,repurchase_new_pv,
                        register_blo_new_pv,upgrade_blo_new_pv,repurchase_blo_new_pv,
                        r_a_balance,r_b_balance,r_a_sum_amount,r_b_sum_amount,r_a_sum_pv,r_b_sum_pv,
                        r_a_sum_real_amount,r_b_sum_real_amount,r_a_sum_real_pv,r_b_sum_real_pv,
                        pk_country)
                    values(b.period,b.pk_member,b.pk_rate,b.a_balance,b.b_balance,b.a_sum_amount,
                           b.b_sum_amount,b.a_sum_pv,b.b_sum_pv,b.a_new_amount,b.b_new_amount,b.a_new_pv,
                           b.b_new_pv,b.a_sum_real_amount,b.b_sum_real_amount,b.a_sum_real_pv,b.b_sum_real_pv,
                           b.rep_a_balance,b.rep_b_balance,b.rep_a_new_amount,b.rep_b_new_amount,b.rep_a_new_pv,
                           b.rep_b_new_pv,b.rep_a_sum_amount,b.rep_b_sum_amount,b.rep_a_sum_pv,b.rep_b_sum_pv,
                           b.register_amount,b.register_pv,b.register_new_amount,b.register_new_pv,
                           b.repurchase_amount,b.repurchase_pv,b.repurchase_new_amount,b.repurchase_new_pv,
                           b.register_blo_new_pv,b.upgrade_blo_new_pv,b.repurchase_blo_new_pv,
                           b.r_a_balance,b.r_b_balance,b.r_a_sum_amount,b.r_b_sum_amount,b.r_a_sum_pv,b.r_b_sum_pv,
                           b.r_a_sum_real_amount,b.r_b_sum_real_amount,b.r_a_sum_real_pv,b.r_b_sum_real_pv,
                           b.pk_country)
    </update>
    <update id="updateCuMemberSettleInitAchieve">
        update ${targetTableName} a set
                                        a.a_balance=0,a.b_balance=0,a.a_sum_amount=0,a.b_sum_amount=0,
                                        a.a_sum_pv=0,a.b_sum_pv=0,a.a_new_amount=0,a.b_new_amount=0,
                                        a.a_new_pv=0,a.b_new_pv=0,a.a_sum_real_amount=0,a.b_sum_real_amount=0,
                                        a.a_sum_real_pv=0,a.b_sum_real_pv=0,a.rep_a_balance=0,a.rep_b_balance=0,
                                        a.rep_a_new_amount=0,a.rep_b_new_amount=0,a.rep_a_new_pv=0,a.rep_b_new_pv=0,
                                        a.rep_a_sum_amount=0,a.rep_b_sum_amount=0,a.rep_a_sum_pv=0,a.rep_b_sum_pv=0,
                                        a.register_amount=0,a.register_pv=0,a.register_new_amount=0,a.register_new_pv=0,
                                        a.repurchase_amount=0,a.repurchase_pv=0,a.repurchase_new_amount=0,a.repurchase_new_pv=0,
                                        a.register_blo_new_pv=0,a.upgrade_blo_new_pv=0,a.repurchase_blo_new_pv=0,
                                        a.min_achieve=0,a.round=0,a.second=1,a.r_a_balance=0,a.r_b_balance=0,
                                        a.r_a_sum_amount = 0,a.r_b_sum_amount = 0,a.r_a_sum_pv = 0,a.r_b_sum_pv = 0,
                                        a.r_a_sum_real_amount = 0,a.r_b_sum_real_amount = 0,
                                        a.r_a_sum_real_pv = 0,a.r_b_sum_real_pv = 0
    </update>

    <update id="createCuMemberMonthTable">
        create table ${tableName}
        (
            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(4) not null,
            pk_awards         NUMBER(4) not null,
            pk_team_code      NUMBER(20),
            pk_center_code    NUMBER(20),
            pk_vertex         NUMBER(20) default 1,
            pk_settle_country NUMBER(4) not null,
            a_sum_pv          NUMBER(17,6) default 0 not null,
            b_sum_pv          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_pv       NUMBER(17,6) default 0 not null,
            repurchase_pv     NUMBER(17,6) default 0 not null
        )
    </update>
    <update id="mergeCuMemberMonth">
        merge into cu_member_month${date} a
            using(
                select a.*,b.A_SUM_PV,b.b_SUM_PV,b.REP_A_SUM_PV,b.REP_b_SUM_PV,b.REGISTER_PV,
                       b.REPURCHASE_PV from(
                                               select ct.pk_member,ct.pk_parent,ct.pk_place_parent,ct.place_dept,ct.member_code,ct.member_name,
                                                      ct.customer_type,ct.phone,ct.pk_grade,ct.pk_awards,ct.pk_team_code,ct.pk_center_code,
                                                      ct.pk_vertex,ct.pk_settle_country from cu_member_tree${date} ct where period=#{period}) a
                                               inner join(
                    select pk_member,sum(A_NEW_PV) A_SUM_PV,sum(b_NEW_PV) b_SUM_PV,sum(REP_A_NEW_PV) REP_A_SUM_PV,sum(REP_b_NEW_PV) REP_b_SUM_PV,
                           sum(register_new_pv) REGISTER_PV,sum(repurchase_new_pv) REPURCHASE_PV
                    from cu_member_achieve${date}
                    group by pk_member) b
                                                         on a.pk_member=b.pk_member
            ) b
            on (a.pk_member=b.pk_member)
            when matched then
                update set
                    a.pk_parent=b.pk_parent,a.pk_place_parent=b.pk_place_parent,a.place_dept=b.place_dept,
                    a.member_code=b.member_code,a.member_name=b.member_name,a.customer_type=b.customer_type,
                    a.phone=b.phone,a.pk_grade=b.pk_grade,a.pk_awards=b.pk_awards,a.pk_team_code=b.pk_team_code,
                    a.pk_center_code=b.pk_center_code,a.pk_vertex=b.pk_vertex,a.pk_settle_country=b.pk_settle_country,
                    a.A_SUM_PV=b.A_SUM_PV,a.b_SUM_PV=b.b_SUM_PV,a.REP_A_SUM_PV=b.REP_A_SUM_PV,
                    a.REP_b_SUM_PV=b.REP_b_SUM_PV,a.REGISTER_PV=b.REGISTER_PV,
                    a.REPURCHASE_PV =b.REPURCHASE_PV
            WHEN NOT MATCHED THEN
                insert(pk_member,pk_parent,pk_place_parent,place_dept,member_code,
                       member_name,customer_type,phone,pk_grade,pk_awards,pk_team_code,
                       pk_center_code,pk_vertex,pk_settle_country,a_sum_pv,b_sum_pv,
                       rep_a_sum_pv,rep_b_sum_pv,register_pv,repurchase_pv)
                    values (b.pk_member,b.pk_parent,b.pk_place_parent,b.place_dept,b.member_code,
                            b.member_name,b.customer_type,b.phone,b.pk_grade,b.pk_awards,b.pk_team_code,
                            b.pk_center_code,b.pk_vertex,b.pk_settle_country,b.a_sum_pv,b.b_sum_pv,
                            b.rep_a_sum_pv,b.rep_b_sum_pv,b.register_pv,b.repurchase_pv)
    </update>

    <select id="queryCuMemberAchieveByPkMember" resultMap="CuMemberAchieve">
        SELECT PK_MEMBER,PK_RATE,A_BALANCE,B_BALANCE,A_SUM_PV,B_SUM_PV,A_SUM_REAL_PV,B_SUM_REAL_PV,
               REP_A_BALANCE,REP_B_BALANCE,REP_A_NEW_PV,REP_B_NEW_PV,REP_A_SUM_PV,REP_B_SUM_PV,PK_COUNTRY
        FROM ${tableName}
        WHERE (pk_member = #{pkMember} AND pk_country = #{pkCountry})
    </select>
    <select id="queryCuMemberUnderBloodAchieveByPkMember" resultType="java.math.BigDecimal">
        select sum(REGISTER_PV+REPURCHASE_PV) pv from ${achieveTable}
        where pk_member in(
            select pk_member
            from ${treeTable}
            where pk_member != #{pkMember}
        start with pk_member = #{pkMember}
        connect by pk_parent = prior pk_member) and period >= #{period}
    </select>


    <select id="selectNewAddAchieve" resultType="com.hzs.member.achieve.vo.CuMemberNewAddAchieveVO">
        select t.*, s.settle_date
        from (select a.period,
        a.a_new_pv aNewPv,
        a.rep_a_new_pv repANewPv
        from ${achieveTableName} a
        where a.pk_member = #{pkMember}
        and a.pk_country = #{pkCountry}
        <if test="upAchieveTableName != null and upAchieveTableName != ''">
            union all
            select up.period,
            up.a_new_pv aNewPv,
            up.rep_a_new_pv repANewPv
            from ${upAchieveTableName} up
            where up.pk_member = #{pkMember}
            and up.pk_country = #{pkCountry}
        </if>
        ) t,
        cu_member_settle_period s
        where t.period = s.pk_id
        and s.del_flag = 0
        and s.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        and s.settle_date &lt;= to_date(#{endDate}, 'yyyy-mm-dd')
        order by s.settle_date
    </select>


    <select id="selectAwardsByPkMember" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        SELECT cm.pk_id       pk_member,
               s.sum_pv       aSumRealPv,
               cm.member_name memberName,
               cm.pk_awards   pkAwards,
               cm.place_dept  placeDept,
               cm.pk_settle_country  pkCountry
        FROM ${settleTable} s,
             (SELECT m.pk_id, m.member_name, m.pk_awards, m.place_dept,m.pk_settle_country
        from cu_member m
        where m.del_flag = 0
        and m.category in (0, 1)
        and m.pk_place_parent = #{pkMember}
        and m.place_dept &lt;= 3
        ) cm
        where s.pk_member = #{pkMember}
        and s.place_dept = cm.place_dept
        order by cm.place_dept
    </select>


    <select id="selectMinRealPvByPkMember" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        select min(m.sum_pv) aSumRealPv
        from ${settleTable} m
        where m.pk_member = #{pkMember}
        and m.place_dept  &lt;= 3
    </select>

    <select id="selectUnderMemberAwards" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        select pk_member,
            sum_pv aSumRealPv,
            head_path memberPath,
            member_name memberName,
            pk_awards pkAwards,
            pk_settle_country pkCountry,
            '${settleTable}' settleTable
        from (select b.pk_id pk_member,
            b.pk_awards,
            a.sum_pv,
            a.place_dept,
            b.head_path,
            b.member_name,
            b.pk_settle_country
            from ${settleTable} a
                inner join (select mt.pk_id,
                mt.pk_place_parent,
                mt.pk_awards,
                mt.head_path,
                mt.member_name,
                mt.pk_settle_country
                from (select m.pk_id,
                m.pk_place_parent,
                m.place_dept,
                m.pk_awards,
                m.member_name,
                m.head_path,
                m.pk_settle_country
                from cu_member m
                where m.del_flag = 0
                and m.pk_settle_country = #{pkCountry}) mt
                start with mt.pk_place_parent = #{pkMember}
                and mt.place_dept = #{placeDept}
                connect by prior mt.pk_id = mt.pk_place_parent) b
            on a.pk_member = b.pk_place_parent
            and a.place_dept =#{placeDept}
            order by b.pk_awards desc, a.sum_pv desc) t
        where rownum &lt;= 5

    </select>


    <select id="selectUnderMemberAwardsFive" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        <foreach collection="memberAwardsList" item="achieve" open=" " separator=" union all " close=" ">
            select s.pk_member,
            s.a_sum_real_pv aSumRealPv,
            cm.member_name  memberName,
            cm.head_path    memberPath,
            cm.pk_awards     pkAwards,
            cm.place_dept  placeDept
            from ${achieve.settleTable} s,
            (select m.pk_id, m.member_name,m.pk_awards,m.head_path,m.place_dept
            from cu_member m
            where m.del_flag = 0
            and m.category = 0
            and m.pay_status = 1
            and m.pk_place_parent = #{achieve.pkMember}
            and m.place_dept &lt;= 3
            and m.pk_settle_country = #{achieve.pkCountry}) cm
            where cm.pk_id = s.pk_member
            and s.pay_status = 1
            and s.category = 0
            and s.pk_country = #{achieve.pkCountry}
        </foreach>
    </select>



    <select id="selectNewAddAchieveContrast" resultType="com.hzs.member.achieve.vo.CuMemberMonthAchieveContrastVO">
        select sum(a.a_new_pv + a.rep_a_new_pv)/10000 aNewPv, sum(a.b_new_pv + a.rep_b_new_pv)/10000 bNewPv
        from ${achieveTable}  a
        where a.pk_member = #{pkMember}
          and a.pk_country = #{pkCountry}
    </select>

    <select id="selectExistTable" resultType="Integer">
        select count(1) from user_tables t where t.table_name=upper('${achieveTable}')
    </select>

    <!--查询左右区新增业绩-->
    <select id="selectNewABAchieve"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select sum(leftAchieve) as leftAchieve,
        sum(rightAchieve) as rightAchieve
        from (select a.period,
        <if test="achievementType==1">
            a.a_new_pv+a.rep_a_new_pv as leftAchieve,
            a.b_new_pv + a.rep_b_new_pv as rightAchieve
        </if>
        <if test="achievementType==2">
            a.a_new_pv as leftAchieve,
            a.b_new_pv as rightAchieve
        </if>
        <if test="achievementType == 3">
            a.rep_a_new_pv as leftAchieve,
            a.rep_b_new_pv as rightAchieve
        </if>
        from ${achieveTableName} a
        where a.pk_member = #{pkMember}
        and a.pk_country = #{pkCountry}
        <if test="upAchieveTableName != null and upAchieveTableName != ''">
            union all
            select up.period,up.a_new_pv+up.rep_a_new_pv as leftAchieve,
            up.b_new_pv + up.rep_b_new_pv as rightAchieve
            from ${upAchieveTableName} up
            where up.pk_member = #{pkMember}
            and up.pk_country = #{pkCountry}
        </if>
        ) t,
        cu_member_settle_period s
        where t.period = s.pk_id
        and s.del_flag = 0
        and s.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        and s.settle_date &lt;= to_date(#{endDate}, 'yyyy-mm-dd')

    </select>
    <!--    查询左右区血缘业绩-->
    <select id="selectABDirectAchieve"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">


        select 1 placeDept,sum(pv) directAchieve from
        (
        select pk_member,pk_parent,

        <if test="achievementType==1">
            (REGISTER_PV+UPGRADE_PV+REPURCHASE_PV) AS pv
        </if>
        <if test="achievementType==2">
            (REGISTER_PV+UPGRADE_PV) AS pv
        </if>
        <if test="achievementType==3">
            (REPURCHASE_PV) AS pv
        </if>
        from ${achieveTableName}
        start with pk_place_parent=#{pkMember} and place_dept=1
        connect by pk_place_parent=prior pk_member
        )start with pk_parent=#{pkMember}
        connect by pk_parent=prior pk_member
        union
        select 2 placeDept,sum(pv) directAchieve from
        (select pk_member,pk_parent,
        <if test="achievementType==1">
            (REGISTER_PV+UPGRADE_PV+REPURCHASE_PV) AS pv
        </if>
        <if test="achievementType==2">
            (REGISTER_PV+UPGRADE_PV) AS pv
        </if>
        <if test="achievementType==3">
            (REPURCHASE_PV) AS pv
        </if>
        from ${achieveTableName}
        start with pk_place_parent=#{pkMember} and place_dept=2
        connect by pk_place_parent=prior pk_member)
        start with pk_parent=#{pkMember}
        connect by pk_parent=prior pk_member
    </select>
    <!--    月度业绩-详情-->
    <select id="placeNewAddAchieveDetails"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">

        select #{placeDept} placeDept,sum(pv) directAchieve from
        (
        select pk_member,pk_parent,
        <if test="achievementType==1">
            (REGISTER_PV+UPGRADE_PV+REPURCHASE_PV) AS pv
        </if>
        <if test="achievementType==2">
            (REGISTER_PV+UPGRADE_PV) AS pv
        </if>
        <if test="achievementType==3">
            (REPURCHASE_PV) AS pv
        </if>
        from ${achieveTableName}
        start with pk_place_parent=#{pkMember} and place_dept=#{placeDept}
        connect by pk_place_parent=prior pk_member
        )start with pk_parent=#{pkMember}
        connect by pk_parent=prior pk_member

    </select>


    <!--    查询历史左右区人数-->
    <select id="selectABCoun" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        SELECT 1 placeDept, count(pk_member) memberCount FROM ${achieveTableName}
                                                                  start with pk_place_parent = #{pkMember} and place_dept = 1
        CONNECT BY pk_place_parent = PRIOR pk_member
        union
        SELECT 2 placeDept, count(pk_member) memberCount FROM ${achieveTableName}
                                                                  start with pk_place_parent = #{pkMember} and place_dept = 2
        CONNECT BY pk_place_parent = PRIOR pk_member
    </select>

    <!--    查询最新月份左右区人数-->
    <select id="selectABNewMonthCoun" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        SELECT 1 placeDept, COUNT(1) memberCount
        FROM cu_member
        WHERE del_flag = 0
            START WITH pk_id = (select cm.pk_id from cu_member cm
            where cm.pk_place_parent = #{pkMember}
            and cm.place_dept = 1
            and cm.del_flag = 0)
        CONNECT BY pk_place_parent = PRIOR pk_id
        UNION
        SELECT 2 placeDept, COUNT(1) memberCount
        FROM cu_member
        WHERE del_flag = 0
            START WITH pk_id = (select cm.pk_id from cu_member cm
            where cm.pk_place_parent = #{pkMember}
            and cm.place_dept = 2
            and cm.del_flag = 0)
        CONNECT BY pk_place_parent = PRIOR pk_id
    </select>



    <!--根据日期查询最新期数-->
    <select id="mostNewPeriod" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="java.lang.Integer">
        select  max(PK_ID) from (
                                    select s.PK_ID  from cu_member_settle_period s
                                                             INNER JOIN ${achieveTableName} t on t.period=s.PK_ID
                                    where  s.del_flag = 0
                                      and s.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
                                      and s.settle_date &lt; to_date(#{endDate}, 'yyyy-mm-dd')
                                )
    </select>

    <!--查询直推业绩-->
    <select id="selectParentAchieve" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select
        <if test="achievementType==1">
            (sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) AS directAchieve
        </if>
        <if test="achievementType==2">
            (sum(REGISTER_PV)+sum(UPGRADE_PV)) AS directAchieve
        </if>
        <if test="achievementType==3">
            (sum(REPURCHASE_PV)) AS directAchieve
        </if>
        from ${achieveTableName}
        start with pk_member=#{pkMember}
        connect by pk_parent = prior pk_member
    </select>

    <!--    查询各个等级直推人数和业绩(推荐伞下)-->
    <select id="selectParentCounAchieve" parameterType="com.hzs.member.achieve.controller.vo.ParentAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.ParentAchieveVO">
        WITH temp AS
                 (SELECT a.period,
                         a.REGISTER_PV + a.UPGRADE_PV + a.REPURCHASE_PV + a.HI_FUN_PV +
                         a.MALL_PV AS achieve,
                         a.pk_member
                  FROM ${achieveTableName} a
                  WHERE a.period = #{period}),
             tree AS
                 (SELECT t.pk_member, t.pk_grade, t.pk_parent
                  FROM ${treeTableNameEnd} t
                           INNER JOIN cu_member_settle_period s
                                      ON t.period = s.pk_id
                  WHERE s.del_flag = 0
                    AND t.period = #{period}),
             result AS
                 (SELECT tt.PK_GRADE AS pkGrade,
                         SUM(tt.achieve) AS achieve,
                         COUNT(0) AS peopleNumber
                  FROM (SELECT aup.achieve, t.PK_GRADE
                        FROM temp aup
                                 INNER JOIN tree t
                                            ON aup.pk_member = t.pk_member
                            START WITH t.pk_member = #{pkMember}
                        CONNECT BY t.pk_parent = PRIOR t.pk_member) tt
                  GROUP BY tt.PK_GRADE)
        SELECT tt.achieve,
               tt.pkGrade,
               tt.peopleNumber,
               bg.PK_TRANSACTION AS pkGradeKey,
               bg.color,
               bg.GRADE_VALUE    AS gradeValue
        FROM result tt
                 LEFT JOIN BD_GRADE bg
                           ON tt.pkGrade = bg.pk_id
    </select>

    <!--    查询本人直推业绩-->
    <select id="selfParentCounAchieve" parameterType="com.hzs.member.achieve.controller.vo.ParentAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.ParentAchieveVO">
        select  tt.achieve,tt.pk_grade pkGrade,bg.PK_TRANSACTION as pkGradeKey ,bg.color,bg.GRADE_VALUE as gradeValue  from (
                                                                                                                                select tt.* from(
                                                                                                                                                    select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
                                                                                                                                                                                                                                           start with pk_member=#{pkMember}
                                                                                                                                                    connect by pk_parent = prior pk_member
                                                                                                                                                    group by pk_grade) tt
                                                                                                                            ) tt left join BD_GRADE bg on tt.pk_grade=bg.pk_id

    </select>
    <!--查询所有直推的人-->
    <select id="allDirectPush" parameterType="com.hzs.member.achieve.controller.vo.ParentAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.ParentAchieveVO">
        SELECT t.pk_member,m.MEMBER_NAME
        FROM ${treeTableNameEnd} t
                 INNER JOIN CU_MEMBER m on m.PK_ID=t.PK_MEMBER
        where t.period = #{period}
          and  t.pk_parent= #{pkMember} ORDER BY PK_MEMBER
    </select>

    <!--    查询前10直推业绩-->
    <select id="parentCounAchieveTop" parameterType="com.hzs.member.achieve.controller.vo.ParentAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.ParentAchieveVO">
        select * from (
                          select cm.MEMBER_NAME memberName,
                                 cma.PK_MEMBER pkMember,
                                 cma.achieve  achieve
                          from CU_MEMBER cm
                                   INNER  JOIN(
                              SELECT
                                  a.pk_member,
                                  b.achieve
                              FROM
                                      ( SELECT pk_member,PK_GRADE FROM ${treeTableNameEnd} WHERE pk_parent = #{pkMember} AND period = #{period} ) a
                                          INNER JOIN ( SELECT pk_member, sum( REGISTER_BLO_NEW_PV ) + sum( UPGRADE_BLO_NEW_PV ) + sum( REPURCHASE_BLO_NEW_PV ) achieve
                                                       FROM ${achieveTableName} GROUP BY pk_member ) b ON a.pk_member = b.pk_member
                              ORDER BY
                                  b.achieve DESC
                          ) cma on cma.PK_MEMBER=cm.PK_ID
                      )
        WHERE ROWNUM &lt;= 10

    </select>

    <!--直推业绩前十人伞下等级业绩-->
    <select id="umbrellaAchieveTop" parameterType="com.hzs.member.achieve.controller.vo.ParentAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.ParentAchieveVO">
        SELECT tt.achieve, tt.PK_GRADE, bg.PK_TRANSACTION AS pkGradeKey, bg.color,pkMember,bg.GRADE_VALUE as gradeValue FROM (
        <if test="pkMember != null">
            select #{pkMember} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember2 != null">
            UNION
            select #{pkMember2} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember2}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember3 != null">
            UNION
            select #{pkMember3} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember3}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember4 != null">
            UNION
            select #{pkMember4} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember4}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember5 != null">
            UNION
            select #{pkMember5} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember5}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember6 != null">
            UNION
            select #{pkMember6} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember6}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember7 != null">
            UNION
            select #{pkMember7} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember7}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember8 != null">
            UNION
            select #{pkMember8} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember8}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember9 != null">
            UNION
            select #{pkMember9} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember9}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        <if test="pkMember10 != null">
            UNION
            select #{pkMember10} pkMember,a.* from(
            select pk_grade,(sum(REGISTER_PV)+sum(UPGRADE_PV)+sum(REPURCHASE_PV)) achieve from ${achieveTableMonth}
            start with pk_member=#{pkMember10}
            connect by pk_parent = prior pk_member
            group by pk_grade) a
        </if>
        ) tt
        inner JOIN BD_GRADE bg
        ON bg.pk_id = tt.PK_GRADE
    </select>


    <select id="selectPlacementDataList" resultType="com.hzs.member.settlecenter.vo.PlacementDataBaseVO">
        select *
        from (select *
        from (select t.member_code       memberCode,
        t.member_name       memberName,
        a.pk_transaction    pkAwards,
        t.pk_settle_country settleCountry,
        v.vertex_name       vertexName,
        t.pk_vertex,
        mt.team_name        teamName,
        t.pk_team_code,
        t.period,
        a.pk_id,
        a.awards_value,
        p.settle_date,
        t.pk_member,
        t.pk_place_parent
        from ${memberTableName} t
        left join bd_awards a
        on t.pk_awards = a.pk_id
        and a.del_flag = 0
        left join bd_vertex v
        on t.pk_vertex = v.pk_id
        and v.del_flag = 0
        left join cu_member_team mt
        on mt.pk_id = t.pk_team_code
        and mt.del_flag = 0
        left join cu_member_settle_period p
        on p.pk_id = t.period
        and p.del_flag = 0
        where  t.pay_status = 1
        and t.category = 0
        <if test="memberName != null and memberName != ''">
            and t.member_name like '%' || #{memberName} || '%'
        </if>
        <if test="pkSettleCountry != null">
            and t.pk_settle_country = #{pkSettleCountry}
        </if>
        <if test="pkVertexList != null">
            and t.pk_vertex in
            <foreach item="pkVertex" collection="pkVertexList" open="(" separator="," close=")">
                #{pkVertex}
            </foreach>
        </if>
        <if test="pkTeamCodeList != null">
            and t.pk_team_code in
            <foreach item="pkTeamCode" collection="pkTeamCodeList" open="(" separator="," close=")">
                #{pkTeamCode}
            </foreach>
        </if>
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) tt
        where 1=1
        <if test="pkAwards != null and pkAwards != ''">
            and tt.pk_id = #{pkAwards}
        </if>
        start with tt.pk_member = #{pkMember}
        connect by prior tt.pk_member = tt.pk_place_parent) cm,

        (select ma.a_sum_pv         aSumPv,
        ma.b_sum_pv         bSumPv,
        ma.a_sum_amount     aSumAmount,
        ma.b_sum_amount     bSumAmount,
        ma.a_balance        aBalance,
        ma.b_balance        bBalance,
        ma.rep_a_sum_pv     repASumPv,
        ma.rep_b_sum_pv     repBSumPv,
        ma.rep_a_sum_amount repASumAmount,
        ma.rep_b_sum_amount repBSumAmount,
        ma.rep_a_balance    repABalance,
        ma.rep_b_balance    repBBalance,
        ma.a_sum_real_pv    aSumRealPv,
        ma.b_sum_real_pv    bSumRealPv,
        ma.pk_rate  pkRate,
        ma.pk_member pkMember
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) a
        where cm.pk_member = a.pkMember

    </select>



    <select id="selectAchieveByPkMember" resultType="com.hzs.member.settlecenter.vo.PlacementDataBaseVO">
        select ma.a_sum_pv         aSumPv,
        ma.b_sum_pv         bSumPv,
        ma.a_sum_amount     aSumAmount,
        ma.b_sum_amount     bSumAmount,
        ma.a_balance        aBalance,
        ma.b_balance        bBalance,
        ma.rep_a_sum_pv     repASumPv,
        ma.rep_b_sum_pv     repBSumPv,
        ma.rep_a_sum_amount repASumAmount,
        ma.rep_b_sum_amount repBSumAmount,
        ma.rep_a_balance    repABalance,
        ma.rep_b_balance    repBBalance,
        ma.a_sum_real_pv    aSumRealPv,
        ma.b_sum_real_pv    bSumRealPv,
        ma.pk_rate          pkRate,
        ma.pk_member        pkMember
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')
        and ma.pk_member in
        <foreach item="pkMember" collection="pkMembers" open="(" separator="," close=")">
            #{pkMember}
        </foreach>
    </select>


    <select id="selectRecommendDataList" resultType="com.hzs.member.settlecenter.vo.RecommendDataBaseVO">
        select  memberCode,
        memberName,
        pkAwards,
        awardsValue,
        settleCountry,
        vertexName,
        pkVertex,
        teamName,
        cm.pkMember,
        pkParent,
        realAchieve,
        realAmount,
        firstAchieve,
        firstAmount,
        repeatAchieve,
        repeatAmount,
        hiFunAchieve,
        hiFunAmount,
        pkRate
        from (select tt.*, level l
        from (select t.member_code       memberCode,
        t.member_name       memberName,
        a.pk_transaction    pkAwards,
        a.pk_id,
        a.awards_value      awardsValue,
        t.pk_settle_country settleCountry,
        v.vertex_name       vertexName,
        t.pk_vertex         pkVertex,
        mt.team_name        teamName,
        t.pk_team_code,
        t.period,
        p.settle_date,
        t.pk_member pkMember,
        t.pk_parent pkParent
        from ${memberTableName} t
        left join bd_awards a
        on t.pk_awards = a.pk_id
        and a.del_flag = 0
        left join bd_vertex v
        on t.pk_vertex = v.pk_id
        and v.del_flag = 0
        left join cu_member_team mt
        on mt.pk_id = t.pk_team_code
        and mt.del_flag = 0
        left join cu_member_settle_period p
        on p.pk_id = t.period
        and p.del_flag = 0
        where t.pay_status = 1
        and t.category = 0
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) tt
        where 1=1
        <if test="pkAwards != null and pkAwards != ''">
            and tt.pk_id = #{pkAwards}
        </if>
        <if test="memberName != null and memberName != ''">
            and tt.member_name like '%' || #{memberName} || '%'
        </if>
        <if test="pkSettleCountry != null">
            and tt.settleCountry = #{pkSettleCountry}
        </if>
        <if test="pkVertexList != null">
            and tt.pkVertex in
            <foreach item="pkVertex" collection="pkVertexList" open="(" separator="," close=")">
                #{pkVertex}
            </foreach>
        </if>
        <if test="pkTeamCodeList != null">
            and tt.pk_team_code in
            <foreach item="pkTeamCode" collection="pkTeamCodeList" open="(" separator="," close=")">
                #{pkTeamCode}
            </foreach>
        </if>
        start with tt.pkMember = #{pkMember}
        connect by prior tt.pkMember = tt.pkParent) cm,
        (select ma.register_pv + ma.upgrade_pv + ma.repurchase_pv as realAchieve,
        ma.register_amount + ma.upgrade_amount + repurchase_amount as realAmount,
        ma.register_pv + ma.upgrade_pv as firstAchieve,
        ma.register_amount + ma.upgrade_amount as firstAmount,
        ma.repurchase_pv repeatAchieve,
        ma.repurchase_amount repeatAmount,
        ma.hi_fun_pv hiFunAchieve,
        ma.hi_fun_amount hiFunAmount,
        ma.pk_rate pkRate,
        ma.pk_member pkMember
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) a
        where cm.pkMember = a.pkMember
        order by cm.l desc
    </select>

    <select id="selectRecommendDataListByPkMember" resultType="com.hzs.member.settlecenter.vo.RecommendDataBaseVO">
        select ma.register_pv + ma.upgrade_pv + ma.repurchase_pv as realAchieve,
        ma.register_amount + ma.upgrade_amount + repurchase_amount as realAmount,
        ma.register_pv + ma.upgrade_pv as firstAchieve,
        ma.register_amount + ma.upgrade_amount as firstAmount,
        ma.repurchase_pv repeatAchieve,
        ma.repurchase_amount repeatAmount,
        ma.hi_fun_pv hiFunAchieve,
        ma.hi_fun_amount hiFunAmount,
        ma.pk_rate pkRate,
        ma.pk_member pkMember
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')
        and ma.pk_member in
        <foreach item="pkMember" collection="pkMembers" open="(" separator="," close=")">
            #{pkMember}
        </foreach>

    </select>
    <select id="selectRecommendVertexValue" resultType="com.hzs.member.settlecenter.vo.RecommendDataDetailVO">
        select sum(b.realAchieve) realAchieve,
        sum(b.realAmount) realAmount,
        sum(b.firstAchieve) firstAchieve,
        sum(b.firstAmount) firstAmount,
        sum(b.repeatAchieve) repeatAchieve,
        sum(b.repeatAmount) repeatAmount,
        sum(b.hiFunAchieve) hiFunAchieve,
        sum(b.hiFunAmount) hiFunAmount,
        a.pk_vertex pkVertex
        from (select t.pk_member, t.pk_vertex
        from ${memberTableName} t
        left join bd_vertex v
        on t.pk_vertex = v.pk_id
        and v.del_flag = 0
        left join cu_member_settle_period p
        on p.pk_id = t.period
        and p.del_flag = 0
        where t.pk_settle_country = #{pkSettleCountry}
        and t.pay_status = 1
        and t.category = 0
        <if test="pkVertexList != null">
            and t.pk_vertex in
            <foreach item="pkVertex" collection="pkVertexList" open="(" separator="," close=")">
                #{pkVertex}
            </foreach>
        </if>
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) a,
        (select ma.register_pv + ma.upgrade_pv + ma.repurchase_pv as realAchieve,
        ma.register_amount + ma.upgrade_amount + repurchase_amount as realAmount,
        ma.register_pv + ma.upgrade_pv as firstAchieve,
        ma.register_amount + ma.upgrade_amount as firstAmount,
        ma.repurchase_pv repeatAchieve,
        ma.repurchase_amount repeatAmount,
        ma.hi_fun_pv hiFunAchieve,
        ma.hi_fun_amount hiFunAmount,
        ma.pk_rate pkRate,
        ma.pk_member pkMember
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) b
        where a.pk_member = b.pkMember
        group by a.pk_vertex
    </select>


    <select id="selectRecommendMarketValue" resultType="com.hzs.member.settlecenter.vo.RecommendDataDetailVO">
        select sum(b.realAchieve) realAchieve,
               sum(b.realAmount) realAmount,
               sum(b.firstAchieve) firstAchieve,
               sum(b.firstAmount) firstAmount,
               sum(b.repeatAchieve) repeatAchieve,
               sum(b.repeatAmount) repeatAmount,
               sum(b.hiFunAchieve) hiFunAchieve,
               sum(b.hiFunAmount) hiFunAmount
        from (select t.pk_member, t.pk_vertex
              from ${memberTableName} t
                       left join cu_member_settle_period p
                                 on p.pk_id = t.period
                                     and p.del_flag = 0
              where t.pk_settle_country = #{pkSettleCountry}
                and t.pay_status = 1
                and t.category = 0
                and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) a,
             (select ma.register_pv + ma.upgrade_pv + ma.repurchase_pv as realAchieve,
                     ma.register_amount + ma.upgrade_amount + repurchase_amount as realAmount,
                     ma.register_pv + ma.upgrade_pv as firstAchieve,
                     ma.register_amount + ma.upgrade_amount as firstAmount,
                     ma.repurchase_pv repeatAchieve,
                     ma.repurchase_amount repeatAmount,
                     ma.hi_fun_pv hiFunAchieve,
                     ma.hi_fun_amount hiFunAmount,
                     ma.pk_rate,
                     ma.pk_member
              from ${achieveTableName} ma
                       left join cu_member_settle_period p
                                 on p.pk_id = ma.period
              where ma.pk_country = #{pkSettleCountry}
                and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')) b
        where a.pk_member = b.pk_member

    </select>

    <select id="getQueryRealPerformance" resultType="com.hzs.member.achieve.controller.vo.RealPerformanceVo">
        select * from (select sum(a.A_SUM_REAL_PV) leftGrandTotal, sum(a.B_SUM_REAL_PV) rightGrandTotal,
                              sum(a.A_SUM_PV) firstLeftPurchaseTotal, sum(a.B_SUM_PV) firstRightPurchaseTotal,
                              sum(a.REP_A_SUM_PV) firstLeftRepurchaseTotal, sum(a.REP_B_SUM_PV) firstRightRepurchaseTotal
                       from ${achieveTable}  a
                       where a.pk_member = #{pkMember}
                         and a.pk_country = #{pkCountry} order by period desc ) where rownum = 1
    </select>

    <select id="getRepurchasePerformancePic" resultType="com.hzs.member.achieve.controller.vo.AchievementVo">
        select sum(a.REP_A_SUM_PV) leftAchieve, sum(a.REP_B_SUM_PV) rightAchieve

        from ${achieveTable}  a
        where a.pk_member = #{pkMember}
          and a.pk_country = #{pkCountry}
    </select>

    <select id="getQueryRealPerformancePic" resultType="com.hzs.member.achieve.controller.vo.AzAchievementPie">
        select sum(a.A_SUM_REAL_PV) leftAchievement, sum(a.B_SUM_REAL_PV) rightAchievement

        from ${achieveTable}  a
        where a.pk_member = #{pkMember}
          and a.pk_country = #{pkCountry}
    </select>

    <select id="getAzAchievement" resultType="com.hzs.member.achieve.controller.vo.AchievementVo">
        select sum(a.A_NEW_PV) leftAchieve, sum(a.B_NEW_PV) rightAchieve
        from CU_MEMBER_ACHIEVE202306  a
                 left join (
            select t.pk_id
            from cu_member t
            where t.del_flag = 0 start
            with t.pk_place_parent = #{pkMember}
            connect by t.pk_place_parent = prior t.pk_id
        )t  on t.pk_id = a.pk_member
            and a.pk_country =#{pkCountry}



    </select>

    <select id="selectByParent" resultType="decimal">

        select sum(a.REGISTER_PV) registerPv
        from ${achieveTable}  a
        where
        a.pk_country = #{pkCountry}
        <if test="memberIds != null">
            and a.PK_MEMBER in
            <foreach item="pkMember" collection="memberIds" open="(" separator="," close=")">
                #{pkMember}
            </foreach>
        </if>
        group by a.PK_MEMBER
        order by registerPv desc
    </select>

    <update id="updateCuMemberAchieve">
        merge into ${tableName} a
            using(select #{period} period, a.*
                  from (select user_id pk_member,
                               sum(CURR_DAY_MAIN_REG_AMOUNT)/6.4 REGISTER_AMOUNT,
                               sum(CURR_DAY_MAIN_REG_PV)/6.4 REGISTER_PV,
                               sum(CURR_DAY_MAIN_UP_AMOUNT)/6.4 UPGRADE_AMOUNT,
                               sum(CURR_DAY_MAIN_UP_PV)/6.4 UPGRADE_PV,
                               sum(CURR_DAY_MAIN_REPUR_AMOUNT)/6.4 REPURCHASE_AMOUNT,
                               sum(CURR_DAY_MAIN_REPURCHASE_PV)/6.4 REPURCHASE_PV,
                               sum(CURR_DAY_SEA2_REG_AMOUNT)/6.4 HI_FUN_AMOUNT,
                               sum(CURR_DAY_SEA2_REG_PV)/6.4 HI_FUN_PV,
                               sum(CURR_DAY_MALL_AMOUNT)/6.4 MALL_AMOUNT,
                               sum(CURR_DAY_MALL_PV)/6.4 MALL_PV
                        from G_MEMBER_ACHIV@link_test
                        where period between - 729 and #{period}
                        group by user_id) a) b on
                (a.pk_member = b.pk_member and a.period = b.period)
            when matched then update set a.REGISTER_AMOUNT = b.REGISTER_AMOUNT,
                a.REGISTER_PV = b.REGISTER_PV, a.UPGRADE_AMOUNT = b.UPGRADE_AMOUNT,
                a.UPGRADE_PV = b.UPGRADE_PV, a.REPURCHASE_AMOUNT = b.REPURCHASE_AMOUNT,
                a.REPURCHASE_PV = b.REPURCHASE_PV, a.HI_FUN_AMOUNT = b.HI_FUN_AMOUNT,
                a.HI_FUN_PV = b.HI_FUN_PV, a.MALL_AMOUNT = b.MALL_AMOUNT, a.MALL_PV = b.MALL_PV
    </update>

    <select id="selectPlaceImage" resultType="com.hzs.member.settlecenter.vo.PlacementImageDataVO">
        select sum(ma.a_sum_pv) leftSumAchieve,
        sum(ma.b_sum_pv) rightSumAchieve,
        sum(ma.rep_a_sum_pv) leftRegSumAchieve,
        sum(ma.rep_b_sum_pv) rightRegSumAchieve,
        sum(ma.a_sum_real_pv) leftRealSumAchieve,
        sum(ma.b_sum_real_pv) rightRealSumAchieve
        from ${achieveTableName} ma
        left join cu_member_settle_period p
        on p.pk_id = ma.period
        where ma.pk_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')
        and ma.pk_member in
        (select tt.pk_member
        from (select t.pk_member, t.pk_place_parent
        from ${memberTableName}  t
        left join cu_member_settle_period p
        on p.pk_id = t.period
        and p.del_flag = 0
        where t.pay_status = 1
        and t.category = 0
        and t.pk_settle_country = #{pkSettleCountry}
        and p.settle_date = to_date(#{date}, 'yyyy-mm-dd')
        <if test="memberName != null and memberName != ''">
            and t.member_name like  #{memberName} || '%'
        </if>
        ) tt
        where 1 = 1
        start with tt.pk_member = #{pkMember}
        connect by prior tt.pk_member = tt.pk_place_parent)
    </select>


    <select id="selectRecommendVertexImage" resultType="com.hzs.member.settlecenter.vo.RecommendImageDataVO">
        select sum(ma.a_sum_pv + ma.b_sum_pv) sumPv,
               sum(ma.a_sum_amount + ma.b_sum_amount) sumAmount,
               sum(ma.rep_a_sum_pv + ma.rep_b_sum_pv) repSumPv,
               sum(ma.rep_a_sum_amount + ma.rep_b_sum_amount) repSumAmount,
               sum(ma.a_sum_real_pv + ma.b_sum_real_pv) sumRealPv,
               sum(ma.a_sum_real_amount + ma.b_sum_real_amount) sumRealAmount,
               sum(ma.hi_fun_pv) hiFunPv,
               sum(ma.hi_fun_amount) hiFunAmount
        from ${achieveTableName} ma
        where ma.pk_country = #{pkSettleCountry}
          and ma.period = #{period}
          and ma.pk_member in
              (select tt.pk_member
               from (select t.pk_member, t.pk_parent
                     from ${memberTableName} t
                     where t.pay_status = 1
                       and t.category = 0
                       and t.pk_settle_country = #{pkSettleCountry}
                       and t.period = #{period} ) tt
               where 1 = 1
        start with tt.pk_member = #{pkMember}
        connect by prior tt.pk_member = tt.pk_parent)
    </select>

    <select id="selectRecommendTeamImage" resultType="com.hzs.member.settlecenter.vo.RecommendImageDataVO">
        select p.pk_team_code pkTeam,
               sum(ma.a_sum_pv + ma.b_sum_pv) sumPv,
               sum(ma.a_sum_amount + ma.b_sum_amount) sumAmount,
               sum(ma.rep_a_sum_pv + ma.rep_b_sum_pv) repSumPv,
               sum(ma.rep_a_sum_amount + ma.rep_b_sum_amount) repSumAmount,
               sum(ma.a_sum_real_pv + ma.b_sum_real_pv) sumRealPv,
               sum(ma.a_sum_real_amount + ma.b_sum_real_amount) sumRealAmount,
               sum(ma.hi_fun_pv) hiFunPv,
               sum(ma.hi_fun_amount) hiFunAmount
        from ${achieveTableName} ma,
             (select tt.pk_member, tt.pk_team_code
              from (select t.pk_member, t.pk_parent, t.pk_team_code
                    from ${memberTableName} t
                    where t.pay_status = 1
                      and t.category = 0
                      and t.pk_settle_country = #{pkSettleCountry}
                      and t.period = #{period}) tt
              where 1 = 1
                  start with tt.pk_member = #{pkMember}
              connect by prior tt.pk_member = tt.pk_parent) p
        where ma.pk_member = p.pk_member
          and ma.pk_country = #{pkSettleCountry}
          and ma.period = #{period}
        group by p.pk_team_code
    </select>
    <select id="queryMemberMonthAchieve" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select * from(
        <foreach collection="monthList" item="item" separator=" union ">
            select ${item} month,bc.out_exchange_rate,cm.pk_settle_country,member_name,bg.pk_transaction pk_grade,ba.pk_transaction pk_awards,
            cm.A_SUM_PV,cm.B_SUM_PV,cm.REP_A_SUM_PV,cm.REP_B_SUM_PV,nvl((a.REGISTER_PV+a.UPGRADE_PV), 0) firstAchieve,
            nvl((a.REPURCHASE_PV+a.HI_FUN_PV+a.MALL_PV), 0) repurchaseAchieve from cu_member_month${item} cm
            inner join(
            select #{pkMember} pk_member,sum(REGISTER_PV) REGISTER_PV,
            sum(UPGRADE_PV) UPGRADE_PV,
            sum(REPURCHASE_PV) REPURCHASE_PV,
            sum(HI_FUN_PV) HI_FUN_PV,
            sum(MALL_PV) MALL_PV from cu_member_month${item}
            start with pk_parent = #{pkMember}
            connect by pk_parent = prior pk_member)a
            on cm.pk_member = a.pk_member
            left join bd_grade bg
            on bg.pk_id=cm.pk_grade
            left join bd_awards ba
            on ba.pk_id=cm.pk_awards
            left join bd_currency bc
            on bc.pk_country = cm.pk_settle_country
        </foreach>)
        order by month desc
    </select>
    <select id="queryMemberMonthAchieveDetail" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select * from(
        <foreach collection="memberList" item="item" separator=" union ">
            select bc.out_exchange_rate,cm.pk_settle_country,cm.member_code,member_name,bg.pk_transaction pk_grade,
            cm.pk_grade grade,ba.pk_transaction pk_awards,cm.pk_awards awards,
            cm.A_SUM_PV,cm.B_SUM_PV,cm.REP_A_SUM_PV,cm.REP_B_SUM_PV,(a.REGISTER_PV+a.UPGRADE_PV) firstAchieve,
            (a.REPURCHASE_PV+a.HI_FUN_PV+a.MALL_PV) repurchaseAchieve from cu_member_month${monthAchieveParam.month} cm
            inner join(
            select #{item} pk_member,sum(REGISTER_PV) REGISTER_PV,
            sum(UPGRADE_PV) UPGRADE_PV,
            sum(REPURCHASE_PV) REPURCHASE_PV,
            sum(HI_FUN_PV) HI_FUN_PV,
            sum(MALL_PV) MALL_PV from cu_member_month${monthAchieveParam.month}
            start with pk_member = #{item}
            connect by pk_parent = prior pk_member)a
            on cm.pk_member = a.pk_member
            left join bd_grade bg
            on bg.pk_id=cm.pk_grade
            left join bd_awards ba
            on ba.pk_id=cm.pk_awards
            left join bd_currency bc
            on bc.pk_country = cm.pk_settle_country
        </foreach>
        ) a
        where 1=1
        <if test="monthAchieveParam.memberCode != null and monthAchieveParam.memberCode != ''">
            and a.member_code = #{monthAchieveParam.memberCode}
        </if>
        <if test="monthAchieveParam.memberName != null and monthAchieveParam.memberName != ''">
            and a.member_name = #{monthAchieveParam.memberName}
        </if>
        <if test="monthAchieveParam.pkGrade != null">
            and a.grade = #{monthAchieveParam.pkGrade}
        </if>
        <if test="monthAchieveParam.pkAwards != null">
            and a.awards = #{monthAchieveParam.pkAwards}
        </if>
    </select>
    <select id="queryPkMemberMonthAchieve" resultType="java.lang.Long">
        select pk_member from cu_member_month${month}
        where pk_parent = #{pkMember}
    </select>

    <select id="queryPlaceParentMemberMonthAchieve" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select so.pk_member,so.order_type,cm.member_code,cm.member_name,
        so.order_achieve,bc.out_exchange_rate,bg.pk_transaction pk_grade,
        cm.pk_grade grade,ba.pk_transaction pk_awards,cm.pk_awards awards,cm.pk_settle_country from sa_order so
        inner join(
        select pk_member,member_code,member_name,PK_GRADE,PK_AWARDS,pk_settle_country from cu_member_month${month}
        start with pk_place_parent = #{pkMember} and place_dept=#{placeDept}
        connect by pk_place_parent = prior pk_member) cm
        on so.pk_member=cm.pk_member
        left join bd_grade bg
        on bg.pk_id=cm.pk_grade
        left join bd_awards ba
        on ba.pk_id=cm.pk_awards
        left join bd_currency bc
        on bc.pk_country = cm.pk_settle_country and bc.del_flag=0
        where so.del_flag=0 and so.order_status=1 and so.pay_time >= #{startDate, jdbcType=DATE}
        and so.pay_time &lt; #{endDate, jdbcType=DATE}
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name = #{memberName}
        </if>
        <if test="pk_grade != null">
            and cm.PK_GRADE = #{pk_grade}
        </if>
        <if test="pk_awards != null">
            and cm.PK_AWARDS = #{pk_awards}
        </if>
        order by so.creation_time desc
    </select>

    <select id="queryParentMemberMonthAchieve" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select so.pk_member,so.order_type,cm.member_code,cm.member_name,
        so.order_achieve,bc.out_exchange_rate,bg.pk_transaction pk_grade,
        cm.pk_grade grade,ba.pk_transaction pk_awards,cm.pk_awards awards,cm.pk_settle_country from sa_order so
        inner join(
        select pk_member,member_code,member_name,PK_GRADE,PK_AWARDS,pk_settle_country from cu_member_month${month}
        start with pk_parent = #{pkMember}
        connect by pk_parent = prior pk_member) cm
        on so.pk_member=cm.pk_member
        left join bd_grade bg
        on bg.pk_id=cm.pk_grade
        left join bd_awards ba
        on ba.pk_id=cm.pk_awards
        left join bd_currency bc
        on bc.pk_country = cm.pk_settle_country and bc.del_flag=0
        where so.del_flag=0 and so.order_status=1 and so.pay_time >= #{startDate, jdbcType=DATE}
        and so.pay_time &lt; #{endDate, jdbcType=DATE}
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name = #{memberName}
        </if>
        <if test="pk_grade != null">
            and cm.PK_GRADE = #{pk_grade}
        </if>
        <if test="pk_awards != null">
            and cm.PK_AWARDS = #{pk_awards}
        </if>
        order by so.creation_time desc
    </select>


    <select id="selectNewAddAchieveMonthByPeriod" resultType="com.hzs.member.achieve.vo.CuMemberNewAddAchieveVO">
        select tt.start_period startPeriod,
            tt.end_period endPeriod,
            p.settle_date settleDate,
            to_char(p.settle_date, 'yyyymm') monthDate
        from cu_member_settle_period p,
            (select t.*
            from (select d.start_period, d.end_period
            from cu_member_settle_period_detail d
            where d.settle_cycle = 1
            and d.del_flag = 0
            and d.pk_country = #{pkCountry}
            and d.end_period &lt;= #{endPeriod}
            order by pk_id desc) t
            where ROWNUM &lt;= #{periodNum}) tt
        where p.pk_id = tt.end_period
    </select>


    <select id="selectTableNameIsExist" resultType="string">
            SELECT table_name FROM user_tables WHERE table_name in
            <foreach item="table" collection="tableNames" open="(" separator="," close=")">
                #{table}
            </foreach>
    </select>


    <select id="selectNewAddAchieveByPeriod" resultType="com.hzs.member.achieve.vo.CuMemberNewAddAchieveVO">
        <foreach collection="achieve.achieveTableList" item="achieveTableName" open=" " separator=" union all " close=" ">
            select  replace('${achieveTableName}','CU_MEMBER_SETTLE_D','') periodNum,  s.new_pv aNewPv
            from ${achieveTableName} s
            where s.pk_member = #{achieve.pkMember}
            and s.place_dept = #{achieve.placeDept}
        </foreach>
        order by periodNum
    </select>
<!--    查询会员安置伞下第一层会员-->
    <select id="placeMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT 1 lev,
    cm.PK_MEMBER pkMember
    FROM
    ${endSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 and cm.PK_PLACE_PARENT = #{pkMember}
    </select>
<!--     查询本期安置网体会员-->
    <select id="placeBefMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT 1 lev,
    cm.PK_MEMBER pkMember
    FROM
    ${curSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 and cm.PK_PLACE_PARENT = #{pkMember}
    </select>
<!--    校验会员是否在某会员安置伞下-->
    <select id="checkPlaceMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
select * from (
 SELECT
    cm.PK_MEMBER pkMember
    FROM
    ${endSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 START WITH cm.PK_PLACE_PARENT = #{pkPlaceParent} CONNECT BY cm.PK_PLACE_PARENT = PRIOR cm.PK_MEMBER
) where pkMember=#{pkMember}

    </select>

    <!--    查询会员推荐伞下第一层会员-->
    <select id="parentMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT 1 lev,
    cm.PK_MEMBER pkMember
    FROM
    ${endSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 and cm.PK_PARENT = #{pkMember}
    </select>

    <!--    查询本期推荐伞下第一层会员-->
    <select id="parentBefMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT 1 lev,
    cm.PK_MEMBER pkMember
    FROM
    ${beforeSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 and cm.PK_PARENT = #{pkMember}
    </select>
<!--     校验会员是否在某会员推荐线-->
    <select id="checkParentMemberQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
select * from (
SELECT
    cm.PK_MEMBER pkMember
    FROM
    ${endSettleTableName} cm
    WHERE
    cm.CATEGORY = 0 START WITH cm.PK_PARENT = #{pkParent} CONNECT BY cm.PK_PARENT = PRIOR cm.PK_MEMBER
) where pkMember=#{pkMember}

    </select>


<!--    安置数据查询-->
    <select id="placeFrameQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT
        cm.pk_id pkMember,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.CONSUME_ACHIEVE consumeAchieve,
        cm.PLACE_DEPT placeDept,
        cm.PAY_TIME payTime,
        bg.PK_TRANSACTION as pkRegisterGrade,
        aw.PK_TRANSACTION as pkAwards,
        0 lev,
        <if test="endAchieveTable!=null and endSettleTabl!=null">
            (SELECT COUNT(1)
            FROM ${endSettleTableName}
            WHERE CATEGORY = 0
            START WITH pk_place_parent = #{pkMember}
            CONNECT BY pk_place_parent = PRIOR PK_MEMBER) AS sumCount,
            (SELECT COUNT(1)
            FROM ${endSettleTableName}
            WHERE CATEGORY = 0
            START WITH pk_place_parent = #{pkMember}
            CONNECT BY pk_place_parent = PRIOR PK_MEMBER) -
            (SELECT COUNT(1)
            FROM ${curSettleTableName}
            WHERE CATEGORY = 0
            START WITH pk_place_parent = #{pkMember}
            CONNECT BY pk_place_parent = PRIOR PK_MEMBER) AS newCount,
            (SELECT SUM(cm.SUM_PV)
            FROM ${endAchieveTableName} cm
            WHERE CATEGORY = 0 and cm.PK_MEMBER=#{pkMember}) AS sumPv,
            (SELECT SUM(cm.NEW_PV)
            FROM ${curAchieveTableName} cm
            WHERE CATEGORY = 0 and cm.PK_MEMBER=#{pkMember} ) AS newPv,
        </if>

        (SELECT SUM(so.ORDER_ACHIEVE)
        FROM SA_ORDER so
        INNER JOIN CU_MEMBER cm ON so.PK_MEMBER = cm.PK_ID
        WHERE so.ORDER_STATUS = 1
        AND so.DEL_FLAG = 0
        and so.ORDER_TYPE in (1,2)
        AND so.PK_MEMBER IN (
        SELECT pk_id
        FROM cu_member
        WHERE del_flag = 0
        AND CATEGORY = 0
        START WITH pk_place_parent = #{pkMember}
        CONNECT BY pk_place_parent = PRIOR pk_id
        )
        AND so.PAY_TIME >= to_date(#{todayDate}, 'yyyy-mm-dd' )
        AND so.PAY_TIME &lt;= to_date(#{todayDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )
        ) AS orderAchieve
        FROM CU_MEMBER cm
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        where cm.DEL_FLAG=0
        <if test="pkMember != null">
            AND cm.PK_ID = #{pkMember}
        </if>
        <if test="memberList!=null and memberList.size>0">
            union all
            <foreach collection="memberList" item="item" index="index" separator=" union all ">
                SELECT
                cm.pk_id pkMember,
                cm.MEMBER_CODE memberCode,
                cm.MEMBER_NAME memberName,
                cm.CONSUME_ACHIEVE consumeAchieve,
                cm.PLACE_DEPT placeDept,
                cm.PAY_TIME payTime,
                bg.PK_TRANSACTION as pkRegisterGrade,
                aw.PK_TRANSACTION as pkAwards,
                #{lev} lev,
                <if test="endAchieveTable!=null and endSettleTabl!=null">
                (SELECT COUNT(1)
                FROM ${endSettleTableName}
                WHERE CATEGORY = 0
                START WITH pk_place_parent = #{item.pkMember}
                CONNECT BY pk_place_parent = PRIOR PK_MEMBER) AS sumCount,
                 null as  newCount,
                    (SELECT SUM(cm.SUM_PV)
                    FROM ${endAchieveTableName} cm
                    WHERE CATEGORY = 0 and cm.PK_MEMBER=#{item.pkMember}) AS sumPv,
                    null as newPv,
                </if>
                (SELECT SUM(so.ORDER_ACHIEVE)
                FROM SA_ORDER so
                INNER JOIN CU_MEMBER cm ON so.PK_MEMBER = cm.PK_ID
                WHERE so.ORDER_STATUS = 1
                AND so.DEL_FLAG = 0
                and so.ORDER_TYPE in (1,2)
                AND so.PK_MEMBER IN (
                SELECT pk_id
                FROM cu_member
                WHERE del_flag = 0
                AND CATEGORY = 0
                START WITH pk_place_parent = #{item.pkMember}
                CONNECT BY pk_place_parent = PRIOR pk_id
                )
                AND so.PAY_TIME >= to_date(#{todayDate}, 'yyyy-mm-dd' )
                AND so.PAY_TIME &lt;= to_date(#{todayDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )
                ) AS orderAchieve
                FROM CU_MEMBER cm
                LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
                LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
                where cm.DEL_FLAG=0
                AND cm.PK_ID =#{item.pkMember}
            </foreach>
        </if>
    </select>
<!--     查询本期安置伞下业绩和新增人数-->
    <select id="placeBefFrameQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        <if test="befMemberList!=null and befMemberList.size>0">
            <foreach collection="befMemberList" item="item" index="index" separator=" union all ">
                SELECT
                #{item.pkMember} pkMember,
                (SELECT COUNT(1)
                FROM ${curSettleTableName}
                WHERE CATEGORY = 0
                START WITH PK_MEMBER = #{item.pkMember}
                CONNECT BY pk_place_parent = PRIOR PK_MEMBER) -
                (SELECT COUNT(1)
                FROM ${beforeSettleTableName}
                WHERE CATEGORY = 0
                START WITH pk_place_parent = #{item.pkMember}
                CONNECT BY pk_place_parent = PRIOR PK_MEMBER) AS newCount,
                (SELECT SUM(cm.NEW_PV)
                FROM ${curAchieveTableName} cm
                WHERE  cm.PK_MEMBER=#{item.pkMember} ) AS newPv
                FROM dual
            </foreach>
        </if>

    </select>

    <!--    推荐数据查询-->
    <select id="parentFrameQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        SELECT
        cm.pk_id pkMember,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.CONSUME_ACHIEVE consumeAchieve,
        cm.PAY_TIME payTime,
        bg.PK_TRANSACTION as pkRegisterGrade,
        aw.PK_TRANSACTION as pkAwards,
        0 lev,
        <if test="staSettleTabl!=null and endSettleTabl!=null">
        (SELECT COUNT(1)
        FROM ${endSettleTableName}
        WHERE CATEGORY = 0
        START WITH PK_PARENT = #{pkMember}
        CONNECT BY PK_PARENT = PRIOR PK_MEMBER) AS sumCount,
        (SELECT COUNT(1)
        FROM ${curSettleTableName}
        WHERE CATEGORY = 0
        START WITH PK_PARENT = #{pkMember}
        CONNECT BY PK_PARENT = PRIOR PK_MEMBER) -
        (SELECT COUNT(1)
        FROM ${beforeSettleTableName}
        WHERE CATEGORY = 0
        START WITH PK_PARENT = #{pkMember}
        CONNECT BY PK_PARENT = PRIOR PK_MEMBER) AS newCount,
            (
            SELECT SUM(REGISTER_PV)
            FROM ${endSettleTableName}
            WHERE CATEGORY = 0
            START WITH PK_PARENT = #{pkMember}
            CONNECT BY PK_PARENT = PRIOR PK_MEMBER
            ) -
            (
            SELECT SUM(REGISTER_PV)
            FROM ${staSettleTableName}
            WHERE CATEGORY = 0
            START WITH PK_PARENT = #{pkMember}
            CONNECT BY PK_PARENT = PRIOR PK_MEMBER
            ) as sumPv,
            (
            SELECT SUM(REGISTER_PV)
            FROM ${curSettleTableName}
            WHERE CATEGORY = 0
            START WITH PK_PARENT = #{pkMember}
            CONNECT BY PK_PARENT = PRIOR PK_MEMBER
            ) -
            nvl((
            SELECT SUM(REGISTER_PV)
            FROM ${beforeSettleTableName}
            WHERE CATEGORY = 0
            START WITH PK_PARENT = #{pkMember}
            CONNECT BY PK_PARENT = PRIOR PK_MEMBER
            ),0) as newPv,
        </if>
        (SELECT SUM(so.ORDER_ACHIEVE)
        FROM SA_ORDER so
        INNER JOIN CU_MEMBER cm ON so.PK_MEMBER = cm.PK_ID
        WHERE so.ORDER_STATUS = 1
        AND so.DEL_FLAG = 0
        and so.ORDER_TYPE in (1,2)
        AND so.PK_MEMBER IN (
        SELECT pk_id
        FROM cu_member
        WHERE del_flag = 0
        AND CATEGORY = 0
        START WITH PK_PARENT = #{pkMember}
        CONNECT BY PK_PARENT = PRIOR pk_id
        )
        AND so.PAY_TIME >= to_date(#{todayDate}, 'yyyy-mm-dd' )
        AND so.PAY_TIME &lt;= to_date(#{todayDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )
        ) AS orderAchieve
        FROM CU_MEMBER cm
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        where cm.DEL_FLAG=0
        <if test="pkMember != null">
            AND cm.PK_ID = #{pkMember}
        </if>
        <if test="memberList!=null and memberList.size>0">
            union all
            <foreach collection="memberList" item="item" index="index" separator=" union all ">
                SELECT
                cm.pk_id pkMember,
                cm.MEMBER_CODE memberCode,
                cm.MEMBER_NAME memberName,
                cm.CONSUME_ACHIEVE consumeAchieve,
                cm.PAY_TIME payTime,
                bg.PK_TRANSACTION as pkRegisterGrade,
                aw.PK_TRANSACTION as pkAwards,
                #{lev} lev,
                <if test="staSettleTabl!=null and endSettleTabl!=null">
                (SELECT COUNT(1)
                FROM ${endSettleTableName}
                WHERE CATEGORY = 0
                START WITH PK_PARENT = #{item.pkMember}
                CONNECT BY PK_PARENT = PRIOR PK_MEMBER) AS sumCount,
                null AS newCount,
                    (
                    SELECT SUM(REGISTER_PV)
                    FROM ${endSettleTableName}
                    WHERE CATEGORY = 0
                    START WITH PK_PARENT = #{item.pkMember}
                    CONNECT BY PK_PARENT = PRIOR PK_MEMBER
                    ) -
                    nvl((SELECT SUM(REGISTER_PV)
                    FROM ${staSettleTableName}
                    WHERE CATEGORY = 0
                    START WITH PK_PARENT = #{item.pkMember}
                    CONNECT BY PK_PARENT = PRIOR PK_MEMBER),0) as sumPv,
                    null as newPv,
                </if>
                (SELECT SUM(so.ORDER_ACHIEVE)
                FROM SA_ORDER so
                INNER JOIN CU_MEMBER cm ON so.PK_MEMBER = cm.PK_ID
                WHERE so.ORDER_STATUS = 1
                AND so.DEL_FLAG = 0
                and so.ORDER_TYPE in (1,2)
                AND so.PK_MEMBER IN (
                SELECT pk_id
                FROM cu_member
                WHERE del_flag = 0
                AND CATEGORY = 0
                START WITH PK_PARENT = #{item.pkMember}
                CONNECT BY PK_PARENT = PRIOR pk_id
                )
                AND so.PAY_TIME >= to_date(#{todayDate}, 'yyyy-mm-dd' )
                AND so.PAY_TIME &lt;= to_date(#{todayDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )
                ) AS orderAchieve
                FROM CU_MEMBER cm
                LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
                LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
                where cm.DEL_FLAG=0
                AND cm.PK_ID =#{item.pkMember}
            </foreach>
        </if>
    </select>
<!--    查询本期推荐伞下业绩和新增人数-->
    <select id="parentBefFrameQuery" resultType="com.hzs.member.achieve.controller.vo.PlaceFrameQueryVO">
        <if test="befMemberList!=null and befMemberList.size>0">
            <foreach collection="befMemberList" item="item" index="index" separator=" union all ">
                SELECT
                    #{item.pkMember} pkMember,
                    (SELECT COUNT(1)
                    FROM ${curSettleTableName}
                    WHERE CATEGORY = 0
                    START WITH PK_PARENT = #{item.pkMember}
                    CONNECT BY PK_PARENT = PRIOR PK_MEMBER) -
                    (SELECT COUNT(1)
                    FROM ${beforeSettleTableName}
                    WHERE CATEGORY = 0
                    START WITH PK_PARENT = #{item.pkMember}
                    CONNECT BY PK_PARENT = PRIOR PK_MEMBER) AS newCount,
                    (
                    SELECT SUM(REGISTER_PV)
                    FROM ${curSettleTableName}
                    WHERE CATEGORY = 0
                    START WITH PK_PARENT = #{item.pkMember}
                    CONNECT BY PK_PARENT = PRIOR PK_MEMBER
                    ) -
                nvl((
                SELECT SUM(REGISTER_PV)
                FROM ${beforeSettleTableName}
                WHERE CATEGORY = 0
                START WITH PK_PARENT = #{item.pkMember}
                CONNECT BY PK_PARENT = PRIOR PK_MEMBER
                ),0) as newPv
                FROM dual
            </foreach>
        </if>

    </select>
</mapper>
