<?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.bonus.achieve.mapper.CuMemberRetailRangeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberRetailRangeExt" type="com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt">
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_PARENT" property="pkParent"/>
        <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="PK_SHARE_AWARDS" property="pkShareAwards"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus"/>
        <result column="PAY_TIME" property="payTime"/>
        <result column="CATEGORY" property="category"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus"/>
        <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_RATE" property="pkRate"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="NEW_BOX_NUM" property="newBoxNum"/>
        <result column="CONSUME_BOX_NUM" property="consumeBoxNum"/>
        <result column="MONTH_BOX_NUM" property="monthBoxNum"/>
        <result column="NEW_CONSUME_PV" property="newConsumePv"/>
        <result column="CONSUME_PV" property="consumePv"/>
        <result column="MONTH_CONSUME_PV" property="monthConsumePv"/>
        <result column="TEAM_NEW_BOX_NUM" property="teamNewBoxNum"/>
        <result column="TEAM_BOX_NUM" property="teamBoxNum"/>
        <result column="TEAM_MONTH_BOX_NUM" property="teamMonthBoxNum"/>
        <result column="TEAM_NEW_PV" property="teamNewPv"/>
        <result column="TEAM_CONSUME_PV" property="teamConsumePv"/>
        <result column="TEAM_MONTH_PV" property="teamMonthPv"/>
        <result column="TEAM_NEW_AMOUNT" property="teamNewAmount"/>
        <result column="TEAM_CONSUME_AMOUNT" property="teamConsumeAmount"/>
        <result column="TEAM_MONTH_AMOUNT" property="teamMonthAmount"/>
        <result column="RECOMMEND_NUM" property="recommendNum"/>
        <result column="TEAM_NUM" property="teamNum"/>
        <result column="REGION_ADDRESS" property="regionAddress"/>
        <result column="GRADE_VALUE" property="gradeValue"/>
        <result column="GRADE_NAME" property="gradeName"/>
        <result column="AWARDS_NAME" property="awardsName"/>
        <result column="AWARDS_VALUE" property="awardsValue"/>
        <result column="SHARE_AWARDS_VALUE" property="shareAwardsValue"/>
        <result column="RANGE_RATIO" property="rangeRatio"/>
        <result column="BENEFIT_AVG_RATIO" property="benefitAvgRatio"/>
        <result column="BENEFIT_SHARE_RATIO" property="benefitShareRatio"/>
        <result column="COUNTRY_NAME" property="countryName"/>
        <result column="big_box_num" property="bigBoxNum"/>
        <result column="small_box_num" property="smallBoxNum"/>
        <result column="big_team_pv" property="bigTeamPv"/>
        <result column="small_team_pv" property="smallTeamPv"/>
    </resultMap>

    <update id="mergeMemberRetailRangeInit">
        update ${rangeTableName}
        set new_box_num         = 0,
            consume_box_num     = 0,
            month_box_num       = 0,
            new_consume_pv      = 0,
            consume_pv          = 0,
            month_consume_pv    = 0,
            team_new_box_num    = 0,
            team_box_num        = 0,
            team_month_box_num  = 0,
            team_new_pv         = 0,
            team_consume_pv     = 0,
            team_month_pv       = 0,
            team_new_amount     = 0,
            team_consume_amount = 0,
            team_month_amount   = 0,
            recommend_num       = 0,
            team_num            = 0,
            enable_status       = 1,
            pk_grade = (select pk_id from bd_grade where del_flag = 0 and grade_value = 20),
            region_address      = 0,
            big_box_num         = 0,
            small_box_num       = 0,
            big_team_pv         = 0,
            small_team_pv       = 0
    </update>

    <update id="mergeMemberRetailRangeByYesterday">
        merge into ${rangeTableName} a
        using(
        select pk_member, pk_grade, pk_awards, pk_share_awards, enable_status,
        new_box_num, consume_box_num, month_box_num,
        new_consume_pv, consume_pv, month_consume_pv,
        team_new_box_num, team_box_num, team_month_box_num,
        team_new_pv, team_consume_pv, team_month_pv,
        team_new_amount, team_consume_amount, team_month_amount,
        recommend_num, team_num,
        big_box_num, small_box_num, big_team_pv, small_team_pv
        from ${yesterdayRangeTableName}
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.consume_box_num = b.consume_box_num,
        a.consume_pv = b.consume_pv,
        a.team_box_num = b.team_box_num,
        a.team_consume_pv = b.team_consume_pv,
        a.team_consume_amount = b.team_consume_amount,
        a.recommend_num = b.recommend_num, a.team_num = b.team_num,
        a.pk_grade = b.pk_grade,
        a.enable_status = b.enable_status,
        a.big_box_num = b.big_box_num, a.small_box_num = b.small_box_num,
        a.big_team_pv = b.big_team_pv, a.small_team_pv = b.small_team_pv
        <if test="isSameMonth == 0">
            ,a.month_box_num = b.month_box_num
            ,a.month_consume_pv = b.month_consume_pv
            ,a.team_month_box_num = b.team_month_box_num
            ,a.team_month_pv = b.team_month_pv
            ,a.team_month_amount = b.team_month_amount
        </if>
    </update>

    <update id="mergeCuMemberRetailRangeExt">
        merge into ${rangeTableName} a
        using (
        <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
            select #{item.pkMember} pk_member, #{item.pkGrade} pk_grade,
            #{item.newBoxNum} new_box_num, #{item.consumeBoxNum} consume_box_num, #{item.monthBoxNum} month_box_num,
            #{item.newConsumePv} new_consume_pv, #{item.consumePv} consume_pv, #{item.monthConsumePv} month_consume_pv,
            #{item.teamNewBoxNum} team_new_box_num, #{item.teamBoxNum} team_box_num, #{item.teamMonthBoxNum} team_month_box_num,
            #{item.teamNewPv} team_new_pv, #{item.teamConsumePv} team_consume_pv, #{item.teamMonthPv} team_month_pv,
            #{item.teamNewAmount} team_new_amount, #{item.teamConsumeAmount} team_consume_amount, #{item.teamMonthAmount} team_month_amount,
            #{item.recommendNum} recommend_num, #{item.teamNum} team_num, #{item.enableStatus} enable_status
            from dual
        </foreach>
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.pk_grade = b.pk_grade,
        a.new_box_num = b.new_box_num, a.consume_box_num = b.consume_box_num, a.month_box_num = b.month_box_num,
        a.new_consume_pv = b.new_consume_pv, a.consume_pv = b.consume_pv, a.month_consume_pv = b.month_consume_pv,
        a.team_new_box_num = b.team_new_box_num, a.team_box_num = b.team_box_num, a.team_month_box_num = b.team_month_box_num,
        a.team_new_pv = b.team_new_pv, a.team_consume_pv = b.team_consume_pv, a.team_month_pv = b.team_month_pv,
        a.team_new_amount = b.team_new_amount, a.team_consume_amount = b.team_consume_amount, a.team_month_amount = b.team_month_amount,
        a.recommend_num = b.recommend_num, a.team_num = b.team_num, a.enable_status = b.enable_status
    </update>

    <select id="queryCuMemberRetailRangeParent" resultMap="CuMemberRetailRangeExt">
        <foreach collection="saOrderExtList" item="item" close=" " open=" " separator="union">
            select
            a.pk_member,
            a.member_code,
            a.member_name,
            a.pk_parent,
            a.pk_settle_country,
            a.category,
            a.account_status,
            a.pk_grade,
            a.income_status,
            a.pay_time,
            a.pk_rate,
            a.system_type,
            a.income_status,
            a.pk_country,
            a.pk_settle_country,
            a.enable_status,
            new_box_num,
            consume_box_num,
            month_box_num,
            new_consume_pv,
            consume_pv,
            month_consume_pv,
            team_new_box_num,
            team_box_num,
            team_month_box_num,
            team_new_pv,
            team_consume_pv,
            team_month_pv,
            team_new_amount,
            team_consume_amount,
            team_month_amount,
            recommend_num,
            team_num,
            ba.grade_value,
            ba.grade_name,
            a.big_box_num,
            a.small_box_num,
            a.big_team_pv,
            a.small_team_pv,
            br.name country_name
            from (
            select * from ${tableName}
            start with pk_member = #{item}
            connect by pk_member = prior pk_parent
            ) a
            inner join bd_grade ba
            on a.pk_grade = ba.pk_id
            left join bd_area br
            on br.pk_id = a.region_address and br.del_flag = 0
            where ba.del_flag = 0
        </foreach>
    </select>
    <select id="queryCuMemberRetailRangeByMemberId"
            resultType="com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt">
        SELECT
            a.pk_member,
            a.member_code,
            a.member_name,
            a.pk_parent,
            a.pk_settle_country,
            a.category,
            a.account_status,
            a.pk_grade,
            a.income_status,
            a.pay_time,
            a.pk_rate,
            a.system_type,
            a.income_status,
            a.pk_country,
            a.pk_settle_country,
            a.enable_status,
            new_box_num,
            consume_box_num,
            month_box_num,
            new_consume_pv,
            consume_pv,
            month_consume_pv,
            team_new_box_num,
            team_box_num,
            team_month_box_num,
            team_new_pv,
            team_consume_pv,
            team_month_pv,
            team_new_amount,
            team_consume_amount,
            team_month_amount,
            recommend_num,
            team_num,
            ba.grade_value,
            ba.grade_name,
            a.big_box_num,
            a.small_box_num,
            a.big_team_pv,
            a.small_team_pv,
            br.name country_name
        FROM
            ${tableName} a
                INNER JOIN bd_grade ba ON a.pk_grade = ba.pk_id
                LEFT JOIN bd_area br ON br.pk_id = a.region_address
                AND br.del_flag = 0
        WHERE
            ba.del_flag = 0
          and a.pk_member = #{memberId}
    </select>

</mapper>
