<?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="UPGRADE_AMOUNT" property="upgradeAmount"/>
        <result column="UPGRADE_PV" property="upgradePv"/>
        <result column="UPGRADE_NEW_PV" property="upgradeNewPv"/>
        <result column="REPURCHASE_AMOUNT" property="repurchaseAmount"/>
        <result column="REPURCHASE_PV" property="repurchasePv"/>
        <result column="REPURCHASE_NEW_PV" property="repurchaseNewPv"/>
        <result column="HI_FUN_AMOUNT" property="hiFunAmount"/>
        <result column="HI_FUN_PV" property="hiFunPv"/>
        <result column="HI_FUN_NEW_PV" property="hiFunNewPv"/>
        <result column="MALL_AMOUNT" property="mallAmount"/>
        <result column="MALL_PV" property="mallPv"/>
        <result column="MALL_NEW_PV" property="mallNewPv"/>
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv"/>
        <result column="UPGRADE_Blo_NEW_PV" property="upgradeBloNewPv"/>
        <result column="REPURCHASE_Blo_NEW_PV" property="repurchaseBloNewPv"/>
        <result column="HI_FUN_Blo_NEW_PV" property="hiFunBloNewPv"/>
        <result column="MALL_Blo_NEW_PV" property="mallBloNewPv"/>
        <result column="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="R_REP_A_BALANCE" property="rRepABalance"/>
        <result column="R_REP_B_BALANCE" property="rRepBBalance"/>
        <result column="R_REP_A_SUM_AMOUNT" property="rRepASumAmount"/>
        <result column="R_REP_B_SUM_AMOUNT" property="rRepBSumAmount"/>
        <result column="R_REP_A_SUM_PV" property="rRepASumPv"/>
        <result column="ALGEBRA" property="algebra"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="TEAM_NAME" property="teamName"/>
        <result column="ASSESS_STATUS" property="assessStatus"/>
    </resultMap>

    <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="selectNewAddAchieve" resultType="com.hzs.member.achieve.vo.CuMemberNewAddAchieveVO">
        select t.*, s.settle_date
        from (select a.period,
        a.a_new_pv aNewPv,
        a.b_new_pv bNewPv,
        a.rep_a_new_pv repANewPv,
        a.rep_b_new_pv repBNewPv
        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.b_new_pv bNewPv,
            up.rep_a_new_pv repANewPv,
            up.rep_b_new_pv repBNewPv
            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 s.pk_member,
               s.a_sum_real_pv aSumRealPv,
               s.b_sum_real_pv bSumRealPv,
               s.member_name   memberName,
               s.pk_awards     pkAwards
        from ${settleTable} s
        where s.pay_status = 1
          and s.category = 0
          and s.pk_country = #{pkCountry}
          and s.pk_member = #{pkMember}
    </select>

    <select id="selectUnderMemberAwards" resultType="com.hzs.member.achieve.vo.CuMemberAwardsUnderVO">
        SELECT pk_member,
               a_sum_real_pv aSumRealPv,
               b_sum_real_pv bSumRealPv,
               head_path     memberPath,
               member_name   memberName,
               pk_awards     pkAwards,
               service_code  serviceCode,
               pk_vertex     pkVertex,
               nick_name     nickName
        FROM (SELECT a.pk_member,
                     b.pk_awards,
                     a.a_sum_real_pv,
                     a.b_sum_real_pv,
                     b.head_path,
                     a.member_name,
                     b.pk_vertex,
                     b.service_code,
                     b.nick_name
              FROM ${settleTable} a
                       INNER JOIN (SELECT mt.pk_id,
                                          mt.pk_place_parent,
                                          mt.pk_awards,
                                          mt.head_path,
                                          mt.service_code,
                                          mt.pk_vertex,
                                          mt.nick_name
                                   FROM (SELECT m.pk_id,
                                                m.pk_place_parent,
                                                m.place_dept,
                                                m.pk_awards,
                                                m.head_path,
                                                m.nick_name,
                                                m.service_code,
                                                m.pk_vertex
                                         FROM cu_member m
                                         WHERE m.del_flag = 0
                                           AND m.pk_settle_country = #{pkCountry}) mt
                                       START
                                   WITH mt.pk_id =
                                       (SELECT t.pk_id
                                       FROM cu_member t
                                       WHERE t.del_flag = 0
                                       AND t.pk_settle_country = #{pkCountry}
                                       AND t.pk_place_parent = #{pkMember}
                                       AND t.place_dept = #{placeDept})
                                   CONNECT BY PRIOR mt.pk_id = mt.pk_place_parent) b
                                  ON a.pk_member = b.pk_id
              WHERE a.pk_country = #{pkCountry}
                AND a.pay_status = 1
                AND a.category = 0
              ORDER BY b.pk_awards DESC, a.a_sum_real_pv DESC) tt
        WHERE rownum &lt;= 5
    </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,
        a.a_new_pv as leftAchieve,
        a.b_new_pv as rightAchieve
        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 as leftAchieve,
            up.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,
                        (REGISTER_PV + UPGRADE_PV + REPURCHASE_PV) AS pv
                 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 all
        select 2 placeDept, sum(pv) directAchieve
        from (select pk_member,
                     pk_parent,
                     (REGISTER_PV + UPGRADE_PV + REPURCHASE_PV) AS pv
              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 cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.SERVICE_CODE serviceCode,
        cm.nick_name nickName,
        cm.pk_vertex pkVertex,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.ORDER_TYPE orderType,
        bg.PK_TRANSACTION pkRegisterGrade,
        cm.CREATION_TIME creationTime
        from (
        SELECT
        PK_ID,
        MEMBER_CODE,
        MEMBER_NAME,
        PK_REGISTER_GRADE,
        CREATION_TIME,
        DEL_FLAG,
        CUSTOMER_TYPE,
        SERVICE_CODE,
        pk_vertex,
        nick_name
        FROM
        (
        SELECT pk_id,MEMBER_CODE,MEMBER_NAME,PK_REGISTER_GRADE,CREATION_TIME,DEL_FLAG,CUSTOMER_TYPE,SERVICE_CODE,nick_name,pk_vertex
        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 = #{placeDept}
        and cm.del_flag = 0)
        CONNECT BY pk_place_parent = PRIOR pk_id
        )
        ) cm
        LEFT JOIN SA_ORDER so on so.PK_MEMBER=cm.PK_ID
        LEFT JOIN BD_GRADE bg on cm.PK_REGISTER_GRADE=bg.pk_id
        where
        so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and cm.DEL_FLAG=0
        and bg.DEL_FLAG=0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            and cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and (
            cm.member_code like #{memberCode} || '%'
            or cm.SERVICE_CODE like #{memberCode} || '%'
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and (
            cm.member_name like #{memberName} || '%'
            or cm.nick_name like #{memberName} || '%'
            )
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0 ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY cm.CREATION_TIME DESC

    </select>

    <!--    月度业绩-左右区人数详情(历史) -->
    <select id="placeNewAddAchieveHistoryDetails"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.ORDER_TYPE orderType,
        bg.PK_TRANSACTION pkRegisterGrade,
        cm.CREATION_TIME creationTime,
        cm.SERVICE_CODE serviceCode,
        cm.pk_vertex pkVertex,
        cm.nick_name nickName
        from (
        SELECT
        cm.PK_ID,
        cm.MEMBER_CODE,
        cm.MEMBER_NAME,
        cm.PK_REGISTER_GRADE,
        cm.CREATION_TIME,
        cm.SERVICE_CODE,
        cm.pk_vertex,
        cm.nick_name
        FROM
        (
        SELECT pk_member FROM ${achieveTableName}
        START WITH pk_member = (select pk_member from ${achieveTableName} cm
        where cm.pk_place_parent = #{pkMember} and cm.place_dept = #{placeDept}
        )CONNECT BY pk_place_parent = PRIOR pk_member
        )cms
        INNER JOIN cu_member cm on cms.pk_member=cm.pk_id
        ) cm
        LEFT JOIN SA_ORDER so on so.PK_MEMBER=cm.PK_ID
        LEFT JOIN BD_GRADE bg on cm.PK_REGISTER_GRADE=bg.pk_id
        where
        so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and bg.DEL_FLAG=0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            and cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and (
            cm.member_code like #{memberCode} || '%'
            or cm.SERVICE_CODE like #{memberCode} || '%'
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and (
            cm.member_name like #{memberName} || '%'
            or cm.nick_name like #{memberName} || '%'
            )
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0 ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY cm.CREATION_TIME DESC

    </select>
    <!--    查询左右区业绩详情-->
    <select id="leftRightAchieveDetails"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.SERVICE_CODE serviceCode,
        cm.NICK_NAME nickName,
        cm.pk_vertex pkVertex,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.ORDER_TYPE orderType,
        bg.PK_TRANSACTION pkRegisterGrade,
        so.PAY_TIME creationTime
        from (
        select cm.PK_ID,
        cm.MEMBER_CODE,
        cm.MEMBER_NAME,
        cm.PK_REGISTER_GRADE,
        cm.SERVICE_CODE,
        cm.pk_vertex,
        cm.NICK_NAME
        from(
        select PK_MEMBER,place_dept,PK_PLACE_PARENT
        from (
        select PK_MEMBER,place_dept,PK_PLACE_PARENT from ${achieveTableName}
        where PERIOd= #{period}
        ) cmt START WITH cmt.pk_place_parent =#{pkMember} and cmt.place_dept =#{placeDept}
        CONNECT BY cmt.pk_place_parent = PRIOR cmt.pk_member
        ) cmt INNER JOIN CU_MEMBER cm on cm.PK_ID=cmt.PK_MEMBER and cm.DEL_FLAG=0 and cm.CUSTOMER_TYPE=0
        ) cm
        LEFT JOIN SA_ORDER so on so.PK_MEMBER=cm.PK_ID
        LEFT JOIN BD_GRADE bg on cm.PK_REGISTER_GRADE=bg.pk_id
        where
        so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and bg.DEL_FLAG=0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            and cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and (
            cm.member_code like #{memberCode} || '%'
            or cm.SERVICE_CODE like #{memberCode} || '%'
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and (
            cm.member_name like #{memberName} || '%'
            or cm.nick_name like #{memberName} || '%'
            )
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0 ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY so.ORDER_TYPE DESC

    </select>

    <!--    查询左右区血缘业绩详情-->
    <select id="leftRightDirectAchieveDetails"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.SERVICE_CODE serviceCode,
        cm.NICK_NAME nickName,
        cm.pk_vertex pkVertex,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.ORDER_TYPE orderType,
        bg.PK_TRANSACTION pkRegisterGrade,
        so.PAY_TIME creationTime
        from (
        SELECT
        pk_member
        FROM
        (
        SELECT
        pk_member,
        pk_parent
        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
        ) cmm
        INNER JOIN CU_MEMBER cm on cm.PK_ID=cmm.PK_MEMBER
        LEFT JOIN SA_ORDER so on so.PK_MEMBER=cm.PK_ID
        LEFT JOIN BD_GRADE bg on cm.PK_REGISTER_GRADE=bg.pk_id
        where
        so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and cm.DEL_FLAG=0
        and cm.CUSTOMER_TYPE=0
        and bg.DEL_FLAG=0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            and cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and (
            cm.member_code like #{memberCode} || '%'
            or cm.SERVICE_CODE like #{memberCode} || '%'
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and (
            cm.member_name like #{memberName} || '%'
            or cm.nick_name like #{memberName} || '%'
            )
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0 ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY so.ORDER_TYPE DESC

    </select>

    <!--月度业绩-查询血缘业绩详情-->
    <select id="directAchieveDetails"
            parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.SERVICE_CODE serviceCode,
        cm.NICK_NAME nickName,
        cm.pk_vertex pkVertex,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.ORDER_TYPE orderType,
        bg.PK_TRANSACTION pkRegisterGrade,
        so.PAY_TIME creationTime
        from (
        SELECT
        pk_member
        FROM
        (
        SELECT
        pk_member,
        pk_parent
        FROM
        ${achieveTableName} START WITH pk_place_parent =#{pkMember}
        CONNECT BY pk_place_parent = PRIOR pk_member
        ) START WITH pk_parent =#{pkMember} CONNECT BY pk_parent = PRIOR pk_member
        ) cmm
        INNER JOIN CU_MEMBER cm on cm.PK_ID=cmm.PK_MEMBER
        LEFT JOIN SA_ORDER so on so.PK_MEMBER=cm.PK_ID
        LEFT JOIN BD_GRADE bg on cm.PK_REGISTER_GRADE=bg.pk_id
        where
        so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and cm.DEL_FLAG=0
        and cm.CUSTOMER_TYPE=0
        and bg.DEL_FLAG=0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            and cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and (
            cm.member_code like #{memberCode} || '%'
            or cm.SERVICE_CODE like #{memberCode} || '%'
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and (
            cm.member_name like #{memberName} || '%'
            or cm.nick_name like #{memberName} || '%'
            )
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !='' ">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0 ">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY so.PAY_TIME DESC

    </select>


    <!--    查询历史左右区人数-->
    <select id="selectABCoun" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        SELECT 1 placeDept, COUNT(1) memberCount
        FROM ${achieveTableName} START WITH pk_member = (select pk_member from ${achieveTableName} cm
            where cm.pk_place_parent = #{pkMember} and cm.place_dept = 1
            )
        CONNECT BY pk_place_parent = PRIOR pk_member
        UNION
        SELECT 2 placeDept, COUNT(1) memberCount
        FROM ${achieveTableName} START WITH pk_member = (select pk_member from ${achieveTableName} cm
            where cm.pk_place_parent = #{pkMember} and cm.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="selectParentAchieve" parameterType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO"
            resultType="com.hzs.member.achieve.controller.vo.PlaceNewAddAchieveVO">
        select (sum(REGISTER_PV) + sum(UPGRADE_PV) + sum(REPURCHASE_PV)) AS directAchieve
        from ${achieveTableName} start with pk_parent=#{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>

    <!--    查询前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,
                        cm.NICK_NAME   nickName,
                        cm.PK_VERTEX   pkVertex,
                        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="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="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,
        cmt.pk_vertex,
        cmt.nick_name,
        cmt.service_code
        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 cu_member cmt
        on cmt.pk_id = 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
        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} or cmt.SERVICE_CODE = #{memberCode})
        </if>
        <if test="memberName != null and memberName != ''">
            and (cm.member_name = #{memberName} or cmt.NICK_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,
        cmt.pk_vertex,
        cmt.nick_name,
        cmt.service_code
        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 cu_member cmt
        on cmt.pk_id = 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
        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} or cmt.SERVICE_CODE = #{memberCode})
        </if>
        <if test="memberName != null and memberName != ''">
            and (cm.member_name = #{memberName} or cmt.NICK_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>
</mapper>
