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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettleExt" type="com.hzs.common.domain.member.ext.CuMemberSettleExt">
        <id column="PK_ID" property="pkId" />
        <result column="PERIOD" property="period" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_PARENT" property="pkParent" />
        <result column="PK_PLACE_PARENT" property="pkPlaceParent" />
        <result column="PLACE_DEPT" property="placeDept" />
        <result column="MEMBER_CODE" property="memberCode" />
        <result column="MEMBER_NAME" property="memberName" />
        <result column="CUSTOMER_TYPE" property="customerType" />
        <result column="PHONE" property="phone" />
        <result column="PK_GRADE" property="pkGrade" />
        <result column="PK_ORI_AWARDS" property="pkOriAwards" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus" />
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_TYPE" property="purchaseType"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus" />
        <result column="SHARE_STATUS" property="shareStatus"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry" />
        <result column="IS_REAL_NAME" property="isRealName"/>
        <result column="PK_COUNTRY" property="pkCountry" />
        <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="SUM_PV" property="sumPv" />
        <result column="NEW_AMOUNT" property="newAmount" />
        <result column="A_NEW_AMOUNT" property="aNewAmount" />
        <result column="A_NEW_PV" property="aNewPv" />

        <result column="REGISTER_AMOUNT" property="registerAmount" />
        <result column="REGISTER_PV" property="registerPv" />
        <result column="REGISTER_NEW_PV" property="registerNewPv" />
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv" />
        <result column="ASSESS_TYPE" property="assessType"/>
        <result column="PK_ORDER" property="pkOrder" />
        <result column="LEVEL" property="level" />
        <result column="GRADE_VALUE" property="gradeValue" />
        <result column="parentMemberCode" property="parentMemberCode" />
        <result column="AP_PK_ID" property="apPkId" />
        <result column="USABLE_QUANTITY" property="usableQuantity"/>
        <result column="RETAIL_PRICE" property="retailPrice" />
        <result column="PK_DATA_ID" property="pkDataId" />
        <result column="directPushNumber" property="directPushNumber" />
        <result column="historyPerformancePv" property="historyPerformancePv" />
        <result column="expire_date" property="expireDate" />
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade" />
    </resultMap>


    <resultMap id="CuMemberSettleDExt" type="com.hzs.common.domain.member.ext.CuMemberSettleDExt">
        <id column="PK_ID" property="pkId" />
        <result column="PERIOD" property="period" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_PARENT" property="pkParent" />
        <result column="PK_PLACE_PARENT" property="pkPlaceParent" />
        <result column="PLACE_DEPT" property="placeDept" />
        <result column="MEMBER_CODE" property="memberCode" />
        <result column="MEMBER_NAME" property="memberName" />
        <result column="CUSTOMER_TYPE" property="customerType" />
        <result column="PHONE" property="phone" />
        <result column="PK_GRADE" property="pkGrade" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus" />
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_TYPE" property="purchaseType"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus" />
        <result column="SHARE_STATUS" property="shareStatus"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="A_BALANCE" property="aBalance" />
        <result column="B_BALANCE" property="bBalance" />
        <result column="SUM_AMOUNT" property="sumAmount" />
        <result column="SUM_PV" property="sumPv" />
        <result column="NEW_AMOUNT" property="newAmount" />
        <result column="NEW_PV" property="newPv" />
        <result column="MON_NEW_PV" property="monNewPv" />
    </resultMap>

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

    <!-- 结算数据会员网体+业绩数据，用于结算 网体跟业绩冗余在一起-->
    <sql id="CuMemberSettleColumn">
        <include refid="CuMemberTreeColumn"></include>,
        <include refid="CuMemberSettleDetailColumn"></include>,
        <include refid="CuMemberBackColumn"></include>
    </sql>
    <sql id="CuMemberSettleDetailColumn">
        pk_rate           NUMBER(6) ,
            a_balance         NUMBER(17,6) default 0 not null,
            b_balance         NUMBER(17,6) default 0 not null,
            a_sum_amount      NUMBER(17,6) default 0 not null,
            b_sum_amount      NUMBER(17,6) default 0 not null,
            a_sum_pv          NUMBER(17,6) default 0 not null,
            b_sum_pv          NUMBER(17,6) default 0 not null,
            a_new_amount      NUMBER(17,6) default 0 not null,
            b_new_amount      NUMBER(17,6) default 0 not null,
            a_new_pv          NUMBER(17,6) default 0 not null,
            b_new_pv          NUMBER(17,6) default 0 not null,
            a_sum_real_amount NUMBER(17,6) default 0 not null,
            b_sum_real_amount NUMBER(17,6) default 0 not null,
            a_sum_real_pv     NUMBER(17,6) default 0 not null,
            b_sum_real_pv     NUMBER(17,6) default 0 not null,
            rep_a_balance     NUMBER(17,6) default 0 not null,
            rep_b_balance     NUMBER(17,6) default 0 not null,
            rep_a_new_amount  NUMBER(17,6) default 0 not null,
            rep_b_new_amount  NUMBER(17,6) default 0 not null,
            rep_a_new_pv      NUMBER(17,6) default 0 not null,
            rep_b_new_pv      NUMBER(17,6) default 0 not null,
            rep_a_sum_amount  NUMBER(17,6) default 0 not null,
            rep_b_sum_amount  NUMBER(17,6) default 0 not null,
            rep_a_sum_pv      NUMBER(17,6) default 0 not null,
            rep_b_sum_pv      NUMBER(17,6) default 0 not null,
            register_amount  NUMBER(17,6) default 0 not null,
            register_pv     NUMBER(17,6) default 0 not null,
            register_new_amount     NUMBER(17,6) default 0 not null,
            register_new_pv NUMBER(17,6) default 0 not null,
            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,
            min_achieve       number(17,6) default 0 not null,
            round             NUMBER(6) default 0 not null,
            second            NUMBER(4) default 1 not null
    </sql>
    <sql id="CuMemberBackColumn">
        r_a_balance         NUMBER(17,6) default 0 not null,
            r_b_balance         NUMBER(17,6) default 0 not null,
            r_a_sum_amount      NUMBER(17,6) default 0 not null,
            r_b_sum_amount      NUMBER(17,6) default 0 not null,
            r_a_sum_pv          NUMBER(17,6) default 0 not null,
            r_b_sum_pv          NUMBER(17,6) default 0 not null,
            r_a_sum_real_amount NUMBER(17,6) default 0 not null,
            r_b_sum_real_amount NUMBER(17,6) default 0 not null,
            r_a_sum_real_pv     NUMBER(17,6) default 0 not null,
            r_b_sum_real_pv     NUMBER(17,6) default 0 not null
    </sql>
    <sql id="CuMemberTableTrig">
        CREATE OR REPLACE TRIGGER ${tableName}_TRIG
                      BEFORE INSERT OR UPDATE ON ${tableName}
                                                          FOR EACH ROW
        DECLARE
        v_newVal NUMBER(12) := 0;
        v_incval NUMBER(12) := 0;
        BEGIN
            IF INSERTING AND :new.pk_id IS NULL THEN
        SELECT
            ${tableNameSeq}.NEXTVAL
        INTO v_newVal
        FROM DUAL;
        IF v_newVal = 1 THEN
        SELECT NVL(max(pk_id), 0) INTO v_newVal FROM ${tableName};
        v_newVal := v_newVal + 1;
        LOOP
        EXIT WHEN v_incval >= v_newVal;
        SELECT ${tableNameSeq}.nextval INTO v_incval
        FROM dual;
        END LOOP;
        END IF;
        :new.pk_id := v_newVal;
        END IF;
        END;
    </sql>
    <sql id="QueryMemberParent">
        <foreach collection="pkMemberList" item="item" open=" " close=" " separator=" union ">
            select pk_member,pk_parent,member_code,member_name,pk_awards,account_status,
            register_authority,customer_type,income_status,pk_settle_country,purchase_status,share_status,is_maker_space,
            a_balance,b_balance,r_a_balance,r_b_balance,rep_a_balance,rep_b_balance,
            register_amount,register_pv,repurchase_amount,pk_center_code,
            repurchase_pv,category,pk_grade,
            min_achieve,round,second,expire_date
            from ${settleTableName}
            where pay_status = 1
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
        </foreach>
    </sql>

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

    <select id="queryCuMemberTreeByPkMemberTable" resultMap="CuMemberSettleExt">
        select
        cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode,
        (select count(cr.pk_id) from CU_MEMBER cr where cr.pk_parent = cm.pk_id and cr.del_flag = 0)   directPushNumber,
        cm.PK_SETTLE_GRADE
        from CU_MEMBER cm
        left join  CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG  = 0
    </select>

    <select id="queryCuMemberTreeParentTable" resultMap="CuMemberSettleExt" >
        select
        <if test="tableName != null and tableName != ''">
            t.A_BALANCE,t.B_BALANCE,t.SUM_AMOUNT A_SUM_AMOUNT,t.SUM_PV A_SUM_PV,t.NEW_AMOUNT  A_NEW_AMOUNT,
            t.NEW_PV A_NEW_PV,
        </if>
        cm.*,      cme.MEMBER_CODE      parentMemberCode from (  select
        level,cm.PK_ID,
        cm.PK_COUNTRY,
        cm.PK_PLACE_PARENT,
        cm.PK_PARENT,
        cm.PLACE_DEPT,
        cm.MEMBER_NAME,
        cm.MEMBER_CODE,
        cm.PK_ID    PK_MEMBER,
        cm.PK_SETTLE_COUNTRY
        from CU_MEMBER cm
        where  1 = 1
        <if test="level != null">
            and level &lt;  #{level}
        </if>
        and cm.del_flag = 0
        start with cm.PK_PARENT = #{pkMember}
        connect by cm.PK_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC )  cm
        left join CU_MEMBER cme
        on cme.PK_ID = cm.PK_PARENT
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t
            on cm.PK_ID = t.PK_MEMBER
        </if>
    </select>

    <select id="queryCuMemberTreeParentIdTable" resultMap="CuMemberSettleExt" >
        select
        cm.*,      cme.MEMBER_CODE      parentMemberCode from (  select
        level,cm.PK_ID,
        cm.PK_COUNTRY,
        cm.PK_PLACE_PARENT,
        cm.PK_PARENT,
        cm.PLACE_DEPT,
        cm.MEMBER_NAME,
        cm.MEMBER_CODE,
        cm.PK_ID    PK_MEMBER,
        cm.PK_SETTLE_COUNTRY,
        cm.PK_SETTLE_GRADE
        from CU_MEMBER cm
        where  1 = 1
        <if test="level != null">
            and level &lt;  #{level}
        </if>
        and cm.del_flag = 0
        start with cm.PK_PARENT = #{pkMember}
        connect by cm.PK_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC )  cm
        left join CU_MEMBER cme
        on cme.PK_ID = cm.PK_PARENT
        <if test="tableName != null and tableName != ''">
            left join ${tableName} t
            on cm.PK_ID = t.PK_MEMBER
        </if>
    </select>

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

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

    <select id="queryCuMemberTreeByPlacePkMemberTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            a.A_BALANCE,a.B_BALANCE,a.SUM_AMOUNT A_SUM_AMOUNT,a.SUM_PV,a.NEW_AMOUNT  A_NEW_AMOUNT,
            a.NEW_PV A_NEW_PV,
        </if>
        cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode

        from
        CU_MEMBER cm
        left join  CU_MEMBER cme on cme.PK_ID = cm.PK_PARENT
        <if test="tableName != null and tableName != ''">
            left join (
                select
            nvl(sum(t.A_BALANCE),0) A_BALANCE,nvl(sum(t.B_BALANCE),0)  B_BALANCE,nvl(sum(t.SUM_AMOUNT),0) SUM_AMOUNT,nvl(sum(t.SUM_PV),0) SUM_PV,nvl(sum(t.NEW_AMOUNT),0) NEW_AMOUNT,
            nvl(sum(t.NEW_PV),0) NEW_PV ,t.pk_member pk_member
                from ${tableName} t
                group by pk_member
            )  a  on  a.pk_member = cm.pk_id
        </if>
        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG  = 0
    </select>

    <select id="queryCuMemberTreeByParentPkMemberTable" resultMap="CuMemberSettleExt">
        select
            cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode,
            cm.PK_SETTLE_GRADE
        from
        CU_MEMBER cm
        left join  CU_MEMBER cme on cme.PK_ID = cm.PK_PARENT
        left join ${tableName} t on  t.PK_MEMBER = cm.pk_id
        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG  = 0
    </select>
    <select id="queryByMemberParentCount"  resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select count(cr.pk_id) parentMemberCount,cr.PK_PARENT pkMember from CU_MEMBER cr where   cr.PK_PARENT in
        <trim suffixOverrides=" OR PK_ID IN()">	<!-- 表示删除最后一个条件 -->
            <foreach collection="memberIds" item="Id" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR cr.PK_PARENT IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{Id}
            </foreach>
        </trim>
        and cr.del_flag = 0
        and cr.PAY_STATUS = 1
        and cr.category = 0
        group by cr.PK_PARENT
    </select>

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

    <select id="queryCuMemberTreePlaceParentHistoryFramework" resultMap="CuMemberSettleExt">
        select
        level,cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID
        PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode,
        cm.PK_SETTLE_GRADE
        from CU_MEMBER cm
        left join CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        where level &lt; #{level} and cm.del_flag = 0
        <if test="memberIdList.size() > 0 and memberIdList != ''">
            and cm.PK_ID in
            <foreach collection="memberIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        start with cm.PK_PLACE_PARENT = #{pkMember}
        connect by cm.PK_PLACE_PARENT = prior cm.pk_id
        order by cm.Place_Dept ASC
    </select>

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

    <select id="queryByMemberParentSettleSumPv" resultType="com.hzs.member.achieve.param.MemberParentCountParam">
        select sum(cr.SUM_PV) parentMemberSumPv,cm.PK_PARENT pkMember from ${tableName} cr
        left join cu_member cm on cm.pk_id = cr.PK_MEMBER

        where   cm.PK_PARENT in
        <foreach collection="memberIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by cm.PK_PARENT
    </select>

    <select id="queryMemberSettle" resultMap="CuMemberSettleDExt">
        select cr.A_BALANCE ,cr.B_BALANCE ,cr.SUM_AMOUNT ,cr.SUM_PV,cr.NEW_AMOUNT,cr.NEW_PV,cr.MON_NEW_PV,cm.PK_SETTLE_COUNTRY,cm.PK_ID
        PK_MEMBER,cr.PLACE_DEPT
        from CU_MEMBER cm
        left join ${tableName} cr on cm.PK_ID = cr.PK_MEMBER
        where cm.pk_id in
        <trim suffixOverrides=" OR PK_ID IN()">    <!-- 表示删除最后一个条件 -->
            <foreach collection="pkMembers" item="Id" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 1000 == 999">) OR cm.pk_id in (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{Id}
            </foreach>
        </trim>
        and cm.del_flag = 0
    </select>

</mapper>
