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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberRangeExt" type="com.hzs.common.domain.member.achieve.ext.CuMemberRangeExt">
        <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_RANGE_AWARDS" property="pkRangeAwards" />
        <result column="PK_BENEFIT_AWARDS" property="pkBenefitAwards" />
        <result column="ACCOUNT_STATUS" property="accountStatus" />
        <result column="PAY_STATUS" property="payStatus" />
        <result column="PAY_TIME" property="payTime" />
        <result column="REGISTER_AUTHORITY" property="registerAuthority" />
        <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="CONSUME_PV" property="consumePv" />
        <result column="MONTH_PV" property="monthPv" />
        <result column="EFFECTIVE_NUM" property="effectiveNum" />
        <result column="RECOMMEND_NUM" property="recommendNum" />
        <result column="TEAM_NUM" property="teamNum" />
        <result column="TEAM_CONSUME_AMOUNT" property="teamConsumeAmount"/>
        <result column="TEAM_CONSUME_PV" property="teamConsumePv" />
        <result column="TEAM_MONTH_AMOUNT" property="teamMonthAmount" />
        <result column="TEAM_MONTH_PV" property="teamMonthPv" />
        <result column="SMALL_AREA_PV" property="smallAreaPv" />
        <result column="ASSESS_STATUS" property="assessStatus" />
        <result column="MONTH_MEMBER" property="monthMember" />
        <result column="RANGE_AWARDS_VALUE" property="rangeAwardsValue" />
        <result column="pk_order" property="pkOrder" />
        <result column="range_store_status" property="rangeStoreStatus" />
        <result column="ev_effective_num" property="evEffectiveNum" />
        <result column="provide_state" property="provideState" />
        <result column="buy_effective_num" property="buyEffectiveNum" />
        <result column="first_agent" property="firstAgent" />
        <result column="second_agent" property="secondAgent" />
        <result column="NEW_PV" property="newPv" />
        <result column="NEW_TEAM_PV" property="newTeamPv" />
        <result column="DEPT_NEW_TEAM_PV" property="deptNewTeamPv"/>
        <result column="TEAM_CONSUME_AMOUNT" property="deptTeamConsumeAmount"/>
        <result column="DEPT_TEAM_CONSUME_PV" property="deptTeamConsumePv"/>
        <result column="DEPT_TEAM_MONTH_AMOUNT" property="deptTeamMonthAmount"/>
        <result column="DEPT_TEAM_MONTH_PV" property="deptTeamMonthPv"/>
    </resultMap>
    <update id="mergeCuMemberRangeExt">
        merge into ${rangeTableName} a
        using (
                select pk_member,min(consume_status) consume_status,max(pk_range_awards) pk_range_awards,
                       max(pk_awards) pk_awards,max(consume_pv) consume_pv,
                       max(month_pv) month_pv,max(new_pv) new_pv,max(new_team_pv) new_team_pv,max(effective_num) effective_num,
                       max(buy_effective_num) buy_effective_num,max(first_agent) first_agent,max(second_agent) second_agent,
                       max(team_consume_pv) team_consume_pv,max(team_month_pv) team_month_pv,max(small_area_pv) small_area_pv,
                       max(team_consume_amount) team_consume_amount,max(team_month_amount) team_month_amount,
                       max(assess_status) assess_status,max(dept_new_team_pv) dept_new_team_pv,
                       max(dept_team_consume_amount) dept_team_consume_amount,max(dept_team_consume_pv) dept_team_consume_pv,
                       max(dept_team_month_amount) dept_team_month_amount,max(dept_team_month_pv) dept_team_month_pv from(
                <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
                    select #{item.pkMember} pk_member,#{item.pkRangeAwards} pk_range_awards,#{item.pkAwards} pk_awards,
                    #{item.consumeStatus} consume_status,#{item.consumePv} consume_pv,#{item.monthPv} month_pv,
                    #{item.newPv} new_pv,#{item.newTeamPv} new_team_pv,#{item.effectiveNum} effective_num,
                    #{item.buyEffectiveNum} buy_effective_num,#{item.firstAgent} first_agent,#{item.secondAgent} second_agent,
                    #{item.teamConsumeAmount} team_consume_amount,#{item.teamMonthAmount} team_month_amount,
                    #{item.deptNewTeamPv} dept_new_team_pv,#{item.deptTeamConsumeAmount} dept_team_consume_amount,
                    #{item.deptTeamConsumePv} dept_team_consume_pv,#{item.deptTeamMonthAmount} dept_team_month_amount,
                    #{item.deptTeamMonthPv} dept_team_month_pv,
                    #{item.teamConsumePv} team_consume_pv,#{item.teamMonthPv} team_month_pv,#{item.smallAreaPv} small_area_pv,
                    #{item.assessStatus} assess_status from dual
                </foreach>)
                group by pk_member
            ) b
        on (a.pk_member=b.pk_member)
        when matched then
            update set a.pk_range_awards=b.pk_range_awards,a.pk_awards=b.pk_awards,
                a.consume_status=b.consume_status,a.consume_pv=b.consume_pv,
                a.month_pv=b.month_pv,a.effective_num=b.effective_num,
                a.new_pv=b.new_pv,a.new_team_pv=b.new_team_pv,a.buy_effective_num=b.buy_effective_num,
                a.first_agent=b.first_agent,a.second_agent=b.second_agent,a.team_consume_amount=b.team_consume_amount,
                a.team_month_amount=b.team_month_amount,a.dept_new_team_pv=b.dept_new_team_pv,
                a.dept_team_consume_amount=b.dept_team_consume_amount,
                a.dept_team_consume_pv=b.dept_team_consume_pv,a.dept_team_month_amount=b.dept_team_month_amount,
                a.dept_team_month_pv=b.dept_team_month_pv,
                a.team_consume_pv=b.team_consume_pv,a.team_month_pv=b.team_month_pv,
                a.small_area_pv=b.small_area_pv,a.assess_status=b.assess_status
    </update>
    <update id="mergeMemberRangeByYesterday">
        merge into ${rangeTableName} a
        using(
                select pk_member,consume_status,consume_pv,month_pv,effective_num,dept_team_consume_amount,
                       buy_effective_num,first_agent,second_agent,recommend_num,team_consume_amount,dept_team_consume_pv,
                       dept_team_month_amount,dept_team_month_pv,
                       team_consume_pv,team_month_amount,team_month_pv,small_area_pv,assess_status from ${yesterdayRangeTableName}
            ) b
        on (a.pk_member=b.pk_member)
        when matched then
            update set a.consume_status=b.consume_status,a.consume_pv=b.consume_pv,
                a.effective_num=b.effective_num,a.buy_effective_num=b.buy_effective_num,
                a.first_agent=b.first_agent,a.second_agent=b.second_agent,a.recommend_num=b.recommend_num,
                a.team_consume_amount=b.team_consume_amount,a.team_consume_pv=b.team_consume_pv,a.small_area_pv=b.small_area_pv,
                a.dept_team_consume_amount=b.dept_team_consume_amount,a.dept_team_consume_pv=b.dept_team_consume_pv
                <if test="isSameMonth == 0">
                    ,a.dept_team_month_amount=b.dept_team_month_amount,a.dept_team_month_pv=b.dept_team_month_pv,
                    a.team_month_amount=b.team_month_amount,a.team_month_pv=b.team_month_pv,a.month_pv=b.month_pv,
                    a.assess_status=b.assess_status
                </if>
    </update>
    <update id="mergeMemberRangeInit">
        update ${rangeTableName} set consume_pv=0,month_pv=0,new_pv=0,new_team_pv=0,dept_new_team_pv=0,
        effective_num=0,buy_effective_num=0,first_agent=0,second_agent=0,recommend_num=0,
        team_num=0,team_consume_amount=0,team_month_amount=0,dept_team_consume_amount=0,
        dept_team_consume_pv=0,dept_team_month_amount=0,dept_team_month_pv=0,
        team_consume_pv=0,team_month_pv=0,small_area_pv=0,consume_status=1
        <if test="isSameMonth == 0">
            ,assess_status=2
        </if>
    </update>
    <update id="updateMemberRangeByPurchase">
        update ${rangeTableName} set assess_status=0 where purchase_status=0
    </update>
    <update id="mergeMemberSecondRangeByRange">
        merge into ${secondRangeTableName} a
        using (
            select a.pk_id pk_member,a.pk_parent,a.pk_range_awards,nvl(b.assess_status,3) assess_status,
                   nvl(b.consume_status,1) consume_status,
                   nvl(b.month_member,0) month_member from (select * from cu_member
                           where del_flag = 0
                           start with pk_id = #{pkMember}
                           connect by pk_id = prior pk_parent) a
            left join ${rangeTableName} b
            on a.pk_id=b.pk_member
            ) b
        on (a.pk_member=b.pk_member)
            when not matched then
                insert (pk_member,pk_parent,pk_range_awards,assess_status,month_member,consume_status)
                    values(b.pk_member,b.pk_parent,b.pk_range_awards,b.assess_status,b.month_member,b.consume_status)
    </update>
    <update id="mergeCuMemberSecondRange">
        merge into ${secondTableName} a
        using (
        <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
            select #{item.pkMember} pk_member,#{item.pkRangeAwards} pk_range_awards,#{item.consumeStatus} consume_status,
            #{item.newPv} new_pv,#{item.newEffectiveNum} new_effective_num,#{item.newTeamPv} new_team_pv,
            #{item.assessStatus} assess_status,#{item.buyEffectiveNum} buy_effective_num,
            #{item.firstAgent} first_agent,#{item.secondAgent} second_agent,
            #{item.deptNewTeamPv} dept_new_team_pv from dual
        </foreach>
        ) b
        on (a.pk_member=b.pk_member)
        when matched then
        update set a.pk_range_awards=b.pk_range_awards,a.new_pv=b.new_pv,a.new_effective_num=b.new_effective_num,
        a.new_team_pv=b.new_team_pv,a.assess_status=b.assess_status,a.buy_effective_num=b.buy_effective_num,
        a.first_agent=b.first_agent,a.second_agent=b.second_agent,a.dept_new_team_pv=b.dept_new_team_pv,
            a.consume_status=b.consume_status
    </update>
    <update id="mergeSecondCuMemberRangeExt">
        merge into ${secondTableName} a
        using (
        <foreach collection="cuMemberRangeExtList" item="item" open=" " close=" " separator="union">
            select #{item.pkMember} pk_member,#{item.pkParent} pk_parent,#{item.pkRangeAwards} pk_range_awards,
            #{item.newPv} new_pv,#{item.newTeamPv} new_team_pv,#{item.newEffectiveNum} new_effective_num,
            #{item.assessStatus} assess_status,#{item.monthMember} month_member from dual
        </foreach>
        ) b
        on (a.pk_member=b.pk_member)
        when not matched then
        insert(pk_member,pk_parent,pk_range_awards,new_pv,new_team_pv,new_effective_num,assess_status,month_member)
        values (b.pk_member,b.pk_parent,b.pk_range_awards,b.new_pv,b.new_team_pv,b.new_effective_num,b.assess_status,b.month_member)
        when matched then
        update set a.pk_range_awards=b.pk_range_awards,a.new_pv=b.new_pv,a.new_team_pv=b.new_team_pv
    </update>
    <select id="queryCuMemberRangeParent" resultMap="CuMemberRangeExt">
        <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_range_awards,a.pk_awards,a.pk_benefit_awards,a.income_status,a.consume_pv,a.range_store_status,
                   a.consume_status,a.month_pv,a.effective_num,a.buy_effective_num,a.first_agent,a.second_agent,
                   a.team_consume_amount,a.team_consume_pv,a.team_month_amount,a.team_month_pv,a.small_area_pv,a.new_pv,a.new_team_pv,
                   a.dept_new_team_pv,a.dept_team_consume_amount,a.dept_team_consume_pv,
                   a.dept_team_month_amount,a.dept_team_month_pv,
                   a.assess_status,a.month_member,a.pay_time,a.pk_rate,ba.awards_value range_awards_value from(
            select * from ${tableName}
            start with pk_member = #{item.pkMember}
            connect by pk_member = prior pk_parent) a
            inner join bd_range_awards ba
            on a.pk_range_awards=ba.pk_id
            where ba.del_flag=0
        </foreach>
    </select>
    <select id="queryCuMemberRangeExtByParent" resultMap="CuMemberRangeExt">
        select a.pk_member,a.pk_parent,a.pk_settle_country,a.pk_range_awards,a.pk_awards,a.pk_benefit_awards,a.income_status,a.consume_pv,
               a.month_pv,a.effective_num,a.buy_effective_num,a.first_agent,a.second_agent,a.consume_status,
               a.team_consume_amount,a.team_consume_pv,a.team_month_amount,a.team_month_pv,a.small_area_pv,0 new_pv,0 new_team_pv,
               a.dept_new_team_pv,a.dept_team_consume_amount,a.dept_team_consume_pv,
               a.dept_team_month_amount,a.dept_team_month_pv,
               a.assess_status,a.month_member,a.pay_time,a.pk_rate,ba.awards_value range_awards_value from ${tableName} a
        inner join bd_range_awards ba
         on a.pk_range_awards=ba.pk_id
        where ba.del_flag=0 and a.pk_parent= #{pkParent}
        and a.pay_time &lt;= #{orderDate}
    </select>
    <select id="validateCuMemberPlaceAwards" resultType="java.lang.Integer">
        select count(*) from(
        <foreach collection="cuMemberRangeExtList" item="item" close=" " open=" " separator=" union all ">
            select distinct 1 from(
            select * from ${rangeTableName}
            start with pk_member= #{item.pkMember}
            connect by pk_parent = prior pk_member)
            where pk_range_awards>= #{pkRangeAwards} and pay_time &lt;= #{orderDate}
        </foreach>)
    </select>
    <select id="queryCuMemberSecondRangeParent"
            resultType="com.hzs.common.domain.member.achieve.ext.CuMemberRangeExt">
        select a.pk_member,cm.member_code,cm.member_name,a.pk_parent,cm.pk_settle_country,cm.category,cm.account_status,
               a.pk_range_awards,cm.pk_awards,cm.pk_benefit_awards,cm.income_status,a.consume_status,
               nvl(b.consume_pv,0)+a.new_pv consume_pv,a.new_pv,cm.pay_time,nvl(b.pk_rate,0) pk_rate,
               nvl(b.month_pv,0)+a.new_pv month_pv,nvl(b.effective_num,0)+a.new_effective_num effective_num,
               nvl(b.team_consume_amount,0) team_consume_amount,nvl(b.team_month_amount,0) team_month_amount,
               nvl(b.team_consume_pv,0)+a.new_team_pv team_consume_pv,nvl(b.team_month_pv,0)+a.new_team_pv team_month_pv,
               nvl(b.small_area_pv,0) small_area_pv,a.new_effective_num,a.new_team_pv,
               a.dept_new_team_pv,nvl(dept_team_consume_amount,0) dept_team_consume_amount,
               nvl(b.dept_team_month_amount,0) dept_team_month_amount,nvl(b.dept_team_month_pv,0)+a.dept_new_team_pv dept_team_month_pv,
               nvl(b.dept_team_consume_pv,0)+a.dept_new_team_pv dept_team_consume_pv,
               a.buy_effective_num,nvl(b.range_store_status,0) range_store_status,a.first_agent,a.second_agent,
               a.assess_status,a.month_member,ba.awards_value range_awards_value from(
              select * from ${secondTableName}
                start with pk_member = #{pkMember}
              connect by pk_member = prior pk_parent) a
              inner join cu_member cm
              on cm.pk_id=a.pk_member
              left join ${rangeTableName} b
              on a.pk_member=b.pk_member
              inner join bd_range_awards ba
               on a.pk_range_awards=ba.pk_id
        where ba.del_flag=0
    </select>
    <select id="querySecondCuMemberRangeExtByParent" resultMap="CuMemberRangeExt">
        select a.pk_member,a.pk_parent,nvl(b.pk_settle_country,1) pk_settle_country,a.pk_range_awards,a.consume_status,
               nvl(b.income_status,0) income_status,nvl(b.consume_pv,0)+a.new_pv consume_pv,a.new_pv,a.new_team_pv,
               a.dept_new_team_pv,nvl(dept_team_consume_amount,0) dept_team_consume_amount,
               nvl(b.dept_team_month_amount,0) dept_team_month_amount,nvl(b.dept_team_month_pv,0)+a.new_team_pv dept_team_month_pv,
               nvl(b.dept_team_consume_pv,0)+a.dept_new_team_pv dept_team_consume_pv,
               nvl(b.month_pv,0)+a.new_pv month_pv,nvl(b.effective_num,0)+a.new_effective_num effective_num,a.new_effective_num,
               nvl(b.team_consume_pv,0)+a.new_team_pv team_consume_pv,nvl(b.team_month_pv,0)+a.new_team_pv team_month_pv,nvl(b.small_area_pv,0) small_area_pv,
               a.assess_status,a.month_member,nvl(b.pay_time,null) pay_time,nvl(b.pk_rate,0) pk_rate,ba.awards_value range_awards_value from ${secondTableName} a
        left join ${tableName} b
        on a.pk_member=b.pk_member
       inner join bd_range_awards ba
        on a.pk_range_awards=ba.pk_id
        where ba.del_flag=0 and a.pk_parent= #{pkParent}
    </select>
    <select id="validateSecondCuMemberPlaceAwards" resultType="java.lang.Integer">
        select count(*) from(
        <foreach collection="cuMemberRangeExtList" item="item" close=" " open=" " separator=" union all ">
            select distinct 1 from(
            select * from ${secondTableName}
            start with pk_member= #{item.pkMember}
            connect by pk_parent = prior pk_member)
            where pk_range_awards>= #{pkRangeAwards}
        </foreach>)
    </select>

</mapper>
