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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberAchieve" type="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="A_BALANCE" property="aBalance"/>
        <result column="B_BALANCE" property="bBalance"/>
        <result column="REGISTER_AMOUNT" property="registerAmount"/>
        <result column="REGISTER_PV" property="registerPv"/>
        <result column="REGISTER_NEW_PV" property="registerNewPv"/>
        <result column="REGISTER_Blo_NEW_PV" property="registerBloNewPv" />
        <result column="REGISTER_Blo_PV" property="registerBloPv" />
        <result column="ALGEBRA" property="algebra"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="TEAM_NAME" property="teamName"/>
        <result column="ASSESS_STATUS" property="assessStatus"/>
        <result column="mon_new_pv" property="monNewPv" />
        <result column="register_mon_new_pv" property="registerMonNewPv" />
    </resultMap>

    <sql id="CuMemberAchieveCol">
            pk_id         NUMBER(20) primary key,
            period            number(6) not null,
            pk_member         NUMBER(20) not null,
            pk_rate           NUMBER(6) not null,
            a_balance         NUMBER(17,6) default 0 not null,
            b_balance         NUMBER(17,6) default 0 not null,
            sum_amount      NUMBER(17,6) default 0 not null,
            sum_pv          NUMBER(17,6) default 0 not null,
            new_amount      NUMBER(17,6) default 0 not null,
            new_pv          NUMBER(17,6) default 0 not null,
            mon_new_pv      number(17,6) default 0 not null,
            register_amount  NUMBER(17,6) default 0 not null,
            register_pv     NUMBER(17,6) default 0 not null,
            register_new_amount     NUMBER(17,6) default 0 not null,
            register_new_pv NUMBER(17,6) default 0 not null,
            register_mon_new_pv      number(17,6) default 0 not null,
            REGISTER_BLO_PV  NUMBER(17,6) default 0 not null,
            REGISTER_Blo_NEW_PV  NUMBER(17,6) default 0 not null,
            REPURCHASE_Blo_amount    NUMBER(17,6) default 0 not null,
            REPURCHASE_Blo_NEW_amount    NUMBER(17,6) default 0 not null,
            pk_country        NUMBER(6) not null
    </sql>
    <insert id="insertCuMemberAchieveBySettleDate">
        insert into ${targetTableName}(period,pk_member,pk_rate,a_balance,b_balance,sum_amount,
                                       sum_pv,new_amount,new_pv,
                                       register_amount,register_pv,register_new_amount,register_new_pv,
                                       pk_country)
        select #{period} period,pk_member,nvl(pk_rate,0) pk_rate,a_balance,b_balance,sum_amount,
               sum_pv,new_amount,new_pv,pk_country from ${sourceTableName}
    </insert>
    <update id="createCuMemberAchieveTable">
        create table ${tableName}
        (
            <include refid="CuMemberAchieveCol"></include>
        )
    </update>

    <!--每日更新累计金额-->
    <update id="updateCuMemberBackOldAchieveEveryDay">
        merge into ${settleTableNameDetail} a
        using (
        select pk_member,place_dept,max(a.pk_rate) pk_rate,sum(new_amount/bc.out_exchange_rate) new_amount,sum(new_pv) new_pv
        from (
        <foreach collection="memberAchieveParamList" item="achieve" open=" " separator=" union all " close=" ">
            select pk_place_parent pk_member,place_dept,#{achieve.pkRate} pk_rate,
            #{achieve.newAmount} new_amount,#{achieve.newPv} new_pv
            from(
            select pk_place_parent,place_dept from(
            select * from ${tableName}
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_place_parent
            )
            where nvl(pk_place_parent,0) !=0
            )
        </foreach>
        ) a
        inner join bd_currency bc
        on a.pk_rate = bc.pk_id
        group by pk_member,place_dept
        ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept)
        when matched then
        update
        set a.sum_pv = a.sum_pv - b.new_pv
    </update>

    <!--每日更新累计金额-->
    <update id="updateCuMemberBackAchieveEveryDay">
        merge into ${settleTableNameDetail} a
        using (
        select pk_place_parent pk_member,place_dept,max(bc.pk_id) pk_rate,sum(new_amount/bc.out_exchange_rate) new_amount,sum(new_pv) new_pv
        from (
        <foreach collection="memberAchieveParamList" item="achieve" open=" " separator=" union all " close=" ">
            select pk_place_parent,place_dept,#{achieve.pkRate} pk_rate,
            #{achieve.newAmount} new_amount,
            #{achieve.newPv} new_pv
            from(
            select pk_place_parent,place_dept from(
            select * from ${tableName}
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_place_parent
            )
            where nvl(pk_place_parent,0) !=0
            )
        </foreach>
        ) a
        inner join bd_currency bc
        on a.pk_rate = bc.pk_id
        group by pk_place_parent,place_dept
        ) b
        on (a.PK_MEMBER = b.PK_MEMBER and a.place_dept=b.place_dept)
        when matched then
        update set
        a.sum_pv = a.sum_pv + b.new_pv
    </update>

    <!--将昨天的各种金额作为今天的期初-->
    <update id="updateCuMemberSettleAchieveByBeforeDay">
        merge into ${targetTable} a
        using(select pk_member,register_amount,register_pv,pk_country,
        register_blo_pv,register_mon_new_pv,
        supply_point,supply_income_bonus from ${sourceTable} ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
            a.register_amount=b.register_amount,a.register_pv=b.register_pv,
            a.register_blo_pv=b.register_blo_pv,
            <if test="flag >= 0">
                a.register_mon_new_pv=b.register_mon_new_pv,
            </if>
            a.supply_point=b.supply_point,a.supply_income_bonus=b.supply_income_bonus
    </update>

    <update id="updateCuMemberSettleAchieveDetailByBeforeDay">
        merge into ${targetTable} a
        using(select pk_member,place_dept,a_balance,b_balance,
        sum_amount,mon_new_pv,sum_pv from ${sourceTable} ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept)
        when matched then
        update set
        a.a_balance=b.a_balance,a.b_balance=b.b_balance,
        <if test="flag >= 0">
            a.mon_new_pv=b.mon_new_pv,
        </if>
        a.sum_amount=b.sum_amount,a.sum_pv=b.sum_pv
    </update>

    <update id="updateCuMemberSettleCountryDetailByBeforeDay">
        merge into ${targetTable} a
        using(select pk_member,place_dept,pk_country,
        sum_amount,sum_pv from ${sourceTable} ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept and a.pk_country=b.pk_country)
        when matched then
        update set
        a.sum_amount=b.sum_amount,a.sum_pv=b.sum_pv
    </update>

    <update id="updateCuMemberSettleAchieveBySpecialBus">
        merge into ${targetTable} a
        using(select cl.pk_member,
        (case infusing_water_type when  1 then ca.a_sum_pv + a_first_modify_pv  when 2 then ca.a_sum_pv- a_first_modify_pv else a_first_modify_pv end) a_sum_pv,
        (case infusing_water_type when  1 then ca.b_sum_pv + b_first_modify_pv  when 2 then ca.b_sum_pv- b_first_modify_pv else b_first_modify_pv end) b_sum_pv,
        (case infusing_water_type when  1 then ca.a_balance + rep_a_first_modify_balance when 2 then ca.a_balance- rep_a_first_modify_balance else rep_a_first_modify_balance end) a_balance,
        (case infusing_water_type when  1 then ca.b_balance + rep_b_first_modify_balance when 2 then ca.b_balance- rep_b_first_modify_balance else rep_b_first_modify_balance end) b_balance
        from cu_member_achieve_log cl
        left join ${targetTable} ca
        on cl.pk_member = ca.pk_member
        where cl.period= #{period} and approve_status=5 and cl.pk_id = #{pkId}
        order by cl.pk_member,cl.creation_time) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set a.a_sum_pv=b.a_sum_pv,a.b_sum_pv=b.b_sum_pv,
            a.a_balance=b.a_balance,a.b_balance=b.b_balance
    </update>

    <update id="mergeCuMemberAchieveBySettleDate">
        merge into ${targetTableName} a
            using(
                select #{period} period,pk_member,nvl(pk_rate,0) pk_rate,a_balance,b_balance,sum_amount,
                       sum_pv,new_amount,new_pv,
                       register_amount,register_pv,register_new_amount,register_new_pv,
                       register_blo_new_pv,repurchase_blo_new_pv,mon_new_pv,register_mon_new_pv,
                       pk_country from ${sourceTableName}
            ) b
            on (a.pk_member=b.pk_member and a.period=b.period)
            WHEN MATCHED THEN
                update set a.pk_rate = b.pk_rate,a.a_balance = b.a_balance,a.b_balance = b.b_balance,
                    a.sum_amount = b.sum_amount,a.sum_pv = b.sum_pv,a.new_amount = b.new_amount,
                    a.new_pv = b.new_pv,
                    a.register_amount = b.register_amount,a.register_pv = b.register_pv,a.register_new_amount = b.register_new_amount,
                    a.register_new_pv = b.register_new_pv,
                    a.register_blo_new_pv=b.register_blo_new_pv,
                    a.mon_new_pv=b.mon_new_pv,a.register_mon_new_pv=b.register_mon_new_pv,
                    a.pk_country = b.pk_country
            WHEN NOT MATCHED THEN
            insert (period,pk_member,pk_rate,a_balance,b_balance,sum_amount,sum_pv,new_amount,
                    new_pv,register_amount,register_pv,register_new_amount,register_new_pv,
                register_blo_new_pv,repurchase_blo_new_pv,pk_country)
            values(b.period,b.pk_member,b.pk_rate,b.a_balance,b.b_balance,b.sum_amount,
                   ,b.sum_pv,b.new_amount,b.new_pv,
                   b.register_amount,b.register_pv,b.register_new_amount,b.register_new_pv,
                   b.register_blo_new_pv,b.repurchase_blo_new_pv,b.pk_country)
    </update>
    <update id="updateCuMemberSettleInitAchieve">
        update ${targetTableName}  set
            register_amount=0,register_pv=0,register_new_amount=0,register_new_pv=0,
            register_blo_new_pv=0,register_blo_pv=0,supply_point=0,supply_income_bonus=0,
            register_mon_new_pv=0
    </update>

    <update id="updateCuMemberSettleDetailInitAchieve">
        update ${targetTableName}  set
        a_balance=0,b_balance=0,sum_amount=0,
        sum_pv=0,new_amount=0,new_pv=0,mon_new_pv=0
    </update>

    <update id="updateCuMemberCountryDetailInitAchieve">
        update ${targetTableName}  set sum_amount=0,
                                       sum_pv=0,new_amount=0,new_pv=0
    </update>

    <update id="createCuMemberMonthTable">
        create table ${tableName}
        (
            pk_member         NUMBER(20) not null,
            pk_parent         NUMBER(20) not null,
            pk_place_parent   NUMBER(20),
            place_dept        NUMBER(4),
            member_code       VARCHAR2(20) not null,
            member_name       VARCHAR2(100) not null,
            customer_type     NUMBER(2) not null,
            phone             VARCHAR2(40) not null,
            pk_grade          NUMBER(4) not null,
            pk_awards         NUMBER(4) not null,
            pk_team_code      NUMBER(20),
            pk_center_code    NUMBER(20),
            pk_vertex         NUMBER(20) default 1,
            pk_settle_country NUMBER(4) not null,
            sum_pv          NUMBER(17,6) default 0 not null,
            register_pv       NUMBER(17,6) default 0 not null
        )
    </update>
    <update id="mergeCuMemberMonth">
        merge into cu_member_month${date} a
            using(
                select a.*,b.SUM_PV,b.REGISTER_PV from(
                     select ct.pk_member,ct.pk_parent,ct.pk_place_parent,ct.place_dept,ct.member_code,ct.member_name,
                            ct.customer_type,ct.phone,ct.pk_grade,ct.pk_awards,ct.pk_team_code,ct.pk_center_code,
                            ct.pk_vertex,ct.pk_settle_country from cu_member_tree${date} ct where period=#{period}) a
                     inner join(
                    select pk_member,sum(NEW_PV) SUM_PV,sum(register_new_pv) REGISTER_PV
                    from cu_member_achieve${date}
                    group by pk_member) b
                   on a.pk_member=b.pk_member
                ) b
            on (a.pk_member=b.pk_member)
            when matched then
                update set
                    a.pk_parent=b.pk_parent,a.pk_place_parent=b.pk_place_parent,a.place_dept=b.place_dept,
                    a.member_code=b.member_code,a.member_name=b.member_name,a.customer_type=b.customer_type,
                    a.phone=b.phone,a.pk_grade=b.pk_grade,a.pk_awards=b.pk_awards,a.pk_team_code=b.pk_team_code,
                    a.pk_center_code=b.pk_center_code,a.pk_vertex=b.pk_vertex,a.pk_settle_country=b.pk_settle_country,
                    a.SUM_PV=b.SUM_PV,a.REGISTER_PV=b.REGISTER_PV
            WHEN NOT MATCHED THEN
                insert(pk_member,pk_parent,pk_place_parent,place_dept,member_code,
                       member_name,customer_type,phone,pk_grade,pk_awards,pk_team_code,
                       pk_center_code,pk_vertex,pk_settle_country,sum_pv,register_pv)
                    values (b.pk_member,b.pk_parent,b.pk_place_parent,b.place_dept,b.member_code,
                            b.member_name,b.customer_type,b.phone,b.pk_grade,b.pk_awards,b.pk_team_code,
                            b.pk_center_code,b.pk_vertex,b.pk_settle_country,b.sum_pv,b.register_pv)
    </update>
    <update id="chargeBackSaOrder">
        merge into ${targetTable} a
        using (
        select pk_member,place_dept,max(a.pk_rate) pk_rate,sum(new_amount/bc.out_exchange_rate) new_amount,sum(new_pv) new_pv
        from (
            select pk_place_parent pk_member,place_dept,#{achieve.pkRate} pk_rate,
                   #{achieve.newAmount}  new_amount,#{achieve.newPv} new_pv
            from(
            select pk_place_parent,place_dept from(
            select * from(select * from ${sourceTable})
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_place_parent
            )
            where nvl(pk_place_parent,0) !=0
            )
        ) a
        inner join bd_currency bc
        on a.pk_rate = bc.pk_id
        group by pk_member,place_dept
        ) b
        on (a.pk_member = b.pk_member and a.place_dept=b.place_dept)
        when matched then
        update set
        a.sum_amount = a.sum_amount - b.new_amount,
        a.sum_pv = a.sum_pv - b.new_pv
    </update>
    <update id="updateCuMemberBloodAchieveEveryDate">
        merge into ${tableName} a
        using (
        select pk_member,sum(register_blo_new_pv) register_blo_new_pv,sum(repurchase_blo_new_amount) repurchase_blo_new_amount from (
        <foreach collection="memberAchieveParamList" item="achieve" open=" " separator=" union all " close=" ">
            select pk_parent pk_member,#{achieve.registerBloNewPv} register_blo_new_pv,
                   #{achieve.repurchaseBloNewAmount} repurchase_blo_new_amount from(
            select * from ${tableName}
            start with pk_member = #{achieve.pkMember}
            connect by pk_member = prior pk_parent
            )
            where category = 0 and income_status = 0 and nvl(pk_parent,0) !=0
        </foreach>
        ) a
        group by pk_member
        ) b
        on (a.pk_member = b.pk_member)
        when matched then
        update set
        a.register_blo_pv = a.register_blo_pv + b.register_blo_new_pv,
        a.register_blo_new_pv = b.register_blo_new_pv,
        a.repurchase_blo_amount = a.repurchase_blo_amount + b.repurchase_blo_new_amount,
        a.repurchase_blo_new_amount=b.repurchase_blo_new_amount
    </update>
    <update id="mergeBackCuMemberReSaleDate">
        merge into ${targetTable} a
            using(
                select pk_member,sum(use_point) use_point,sum(pretax_income) pretax_income from (
                select cb.pk_member,cr.use_point,cr.pretax_income from cu_member_bonus cb
               inner join cu_member_bonus_range cr
                          on cb.pk_id = cr.pk_bonus
                where cb.del_flag=0 and cr.del_flag=0
                  and cb.period=#{period}
                union
                select cb.pk_member,cp.use_point,cp.pretax_income from cu_member_bonus cb
                                                                           inner join cu_member_bonus_point cp
                                                                                      on cb.pk_id = cp.pk_bonus
                where cb.del_flag=0 and cp.del_flag=0
                  and cb.period=#{period})
                where pretax_income>0
            group by pk_member
            ) b
            on (a.pk_member=b.pk_member)
            when matched then
                update set a.resale_income_number=a.resale_income_number+b.use_point,
                    a.resale_income_bonus= a.resale_income_bonus + b.pretax_income - (b.use_point * 150)
    </update>
    <select id="queryCuMemberSettleCountry"
            resultType="com.hzs.common.domain.member.ext.CuMemberSettleCountryExt">
        select a.*,bc.name countryName from ${settleCountryTable} a
        inner join bd_country bc
        on a.pk_country=bc.pk_id
        where pk_member= #{bonusParam.pkMember}
    </select>

</mapper>
