<?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.report.report.mapper.ReportCuMemberTreeMapper"
>

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettleExt" type="com.hzs.common.domain.member.ext.CuMemberSettleExt">
        <id column="PK_ID" property="pkId" />
        <result column="PERIOD" property="period" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_PARENT" property="pkParent" />
        <result column="PK_PLACE_PARENT" property="pkPlaceParent" />
        <result column="PLACE_DEPT" property="placeDept" />
        <result column="MEMBER_CODE" property="memberCode" />
        <result column="MEMBER_NAME" property="memberName" />
        <result column="CUSTOMER_TYPE" property="customerType" />
        <result column="PHONE" property="phone" />
        <result column="PK_GRADE" property="pkGrade" />
        <result column="PK_ORI_AWARDS" property="pkOriAwards" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus" />
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_TYPE" property="purchaseType"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus" />
        <result column="SHARE_STATUS" property="shareStatus"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry" />
        <result column="IS_REAL_NAME" property="isRealName"/>
        <result column="IS_BUS_LICENSE" property="isBusLicense"/>
        <result column="IS_MAKER_SPACE" property="isMakerSpace"/>
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_RATE" property="pkRate" />
        <result column="A_BALANCE" property="aBalance" />
        <result column="B_BALANCE" property="bBalance" />
        <result column="R_A_BALANCE" property="rABalance" />
        <result column="R_B_BALANCE" property="rBBalance" />
        <result column="A_SUM_AMOUNT" property="aSumAmount" />
        <result column="B_SUM_AMOUNT" property="bSumAmount" />
        <result column="A_SUM_PV" property="aSumPv" />
        <result column="B_SUM_PV" property="bSumPv" />
        <result column="A_NEW_AMOUNT" property="aNewAmount" />
        <result column="B_NEW_AMOUNT" property="bNewAmount" />
        <result column="A_NEW_PV" property="aNewPv" />
        <result column="B_NEW_PV" property="bNewPv" />
        <result column="A_SUM_REAL_AMOUNT" property="aSumRealAmount" />
        <result column="B_SUM_REAL_AMOUNT" property="bSumRealAmount" />
        <result column="A_SUM_REAL_PV" property="aSumRealPv" />
        <result column="B_SUM_REAL_PV" property="bSumRealPv" />
        <result column="REP_A_BALANCE" property="repABalance" />
        <result column="REP_B_BALANCE" property="repBBalance" />
        <result column="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="MIN_ACHIEVE" property="minAchieve" />
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv" />
        <result column="REPURCHASE_Blo_NEW_PV" property="repurchaseBloNewPv" />
        <result column="ROUND" property="round" />
        <result column="SECOND" property="second" />
        <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" />
    </resultMap>

    <select id="queryCuMemberTreeByPkMemberTable" resultMap="CuMemberSettleExt">
        select
        <if test="tableName != null and tableName != ''">
            t.PK_RATE,t.A_BALANCE,t.B_BALANCE,t.A_SUM_AMOUNT,t.B_SUM_AMOUNT,t.A_SUM_PV,t.B_SUM_PV,t.A_NEW_AMOUNT,
            t.B_NEW_AMOUNT,t.A_NEW_PV,t.B_NEW_PV,t.A_SUM_REAL_AMOUNT,t.B_SUM_REAL_AMOUNT,t.A_SUM_REAL_PV,t.B_SUM_REAL_PV,
            t.REP_A_BALANCE,t.REP_B_BALANCE,t.REP_A_NEW_AMOUNT,t.REP_B_NEW_AMOUNT,t.REP_A_NEW_PV,t.REP_B_NEW_PV,t.REP_A_SUM_AMOUNT,
            t.REP_B_SUM_AMOUNT,t.REP_A_SUM_PV,t.REP_B_SUM_PV,t.REGISTER_AMOUNT,t.REGISTER_PV,t.REGISTER_NEW_PV,
            t.REPURCHASE_AMOUNT,t.REPURCHASE_PV,t.REPURCHASE_NEW_PV,t.MIN_ACHIEVE,t.ROUND,t.SECOND,
        </if>
        cm.PK_AWARDS,cm.PK_PLACE_PARENT,cm.PK_PARENT ,cm.PLACE_DEPT ,cm.MEMBER_NAME, cm.MEMBER_CODE,cm.PK_ID PK_MEMBER,cm.PK_COUNTRY,cm.PK_SETTLE_COUNTRY,cme.MEMBER_CODE parentMemberCode,
        (select count(cr.pk_id) from CU_MEMBER cr where cr.pk_parent = cm.pk_id and cr.del_flag = 0)   directPushNumber
        from
        CU_MEMBER cm
        left join  CU_MEMBER cme on cme.PK_ID = cm.PK_PLACE_PARENT
        <if test="tableName != null and tableName != ''">
            left join  ${tableName} t  on cm.PK_ID = t.PK_MEMBER
        </if>

        where cm.PK_ID = #{pkMember}
        and cm.DEL_FLAG  = 0
    </select>

    <select id="recommendAchievement" resultType="com.hzs.report.report.vo.ReportRecommendAchievementVo">
        select nvl(sum(t.A_SUM_REAL_PV + t.B_SUM_REAL_PV),0) achieve,cm.PK_ID pkMember  from CU_MEMBER cm
        left join ${tableName} t on t.PK_MEMBER = cm.pk_id
        where cm.del_flag = 0
        and cm.CATEGORY = 0
        start
        with cm.PK_PARENT in
        <foreach collection="pkMembers" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        connect by cm.PK_PARENT = prior cm.PK_ID
        group by cm.PK_ID
        order by achieve desc
    </select>

    <select id="listParentAchieve" resultType="com.hzs.report.report.vo.ReportRecommendAchievementVo">
        select nvl(sum(t.A_SUM_REAL_PV + t.B_SUM_REAL_PV),0) achieve,cm.PK_ID pkMember from CU_MEMBER cm
        left join ${tableName} t on t.PK_MEMBER = cm.pk_id
        where cm.del_flag = 0
        and cm.CATEGORY = 0
        and cm.PK_PARENT in
        <foreach collection="pkMembers" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by cm.PK_ID
        order by achieve desc
    </select>

</mapper>
