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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberBonus" type="com.hzs.common.domain.member.ext.CuMemberBonusExt">
        <id column="PK_ID" property="pkId" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PERIOD" property="period" />
        <result column="PK_RATE" property="pkRate" />
        <result column="out_exchange_rate" property="outExchangeRate" />
        <result column="DIRECT_INCOME" property="directIncome" />
        <result column="SEE_POINT_INCOME" property="seePointIncome" />
        <result column="EXPAND_INCOME" property="expandIncome" />
        <result column="EXPAND_NO_CAPPING" property="expandNoCapping" />
        <result column="COACH_INCOME" property="coachIncome" />
        <result column="SHARE_INCOME" property="shareIncome" />
        <result column="SERVICE_INCOME" property="serviceIncome" />
        <result column="BACK_POINTS" property="backPoints" />
        <result column="SERVICE_SPEND" property="serviceSpend" />
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal" />
        <result column="REPUR_RANGE_INCOME" property="repurRangeIncome" />
        <result column="SAME_LEVEL_INCOME" property="sameLevelIncome" />
        <result column="POINT_SHARE_INCOME" property="pointShareIncome" />
        <result column="FIRE_SHARE_INCOME" property="fireShareIncome" />
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal" />
        <result column="GLOBAL_POINTS" property="globalPoints" />
        <result column="REAL_INCOME_TOTAL" property="realIncomeTotal" />
        <result column="INCOME_STATUS" property="incomeStatus" />
        <result column="GRANT_STATUS" property="grantStatus" />
        <result column="SETTLE_DATE" property="settleDate" />
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="VERTEX_NAME" property="vertexName" />
        <result column="TEAM_NAME" property="teamName" />
        <result column="STORE_INCOME" property="storeIncome" />
        <result column="STORE_RANGE_INCOME" property="storeRangeIncome" />
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal" />
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal" />

    </resultMap>
    <update id="updateCuMemberBonusStatus">
        update cu_member_bonus set grant_status = #{grantStatus}, pk_modified=100000000, modified_time=sysdate
        where del_flag= 0
          and grant_status = #{oldGrantStatus}
        and period between #{startPeriod} and #{endPeriod}
        <if test="pkCountry != null">
            and pk_country = #{pkCountry}
        </if>
    </update>
    <delete id="deleteCuMemberBonus">
        delete from cu_member_bonus where period= #{period} and grant_status &lt;= 2
    </delete>

    <insert id="batchInsertCuMemberBonus">
        merge into cu_member_bonus cb
        using(
        select pk_id,pk_member,period,pk_rate,direct_income,see_point_income,expand_income,expand_no_capping,
        coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_range_income,same_level_income,
        point_share_income,fire_share_income,repur_real_subtotal,
        global_points,store_income,store_range_income,real_income_total,
        pk_country,pk_creator,grant_status,income_status from(
        <foreach item="item" index="index" collection="cuMemberBonusList" separator=" union ">
            select #{item.pkId} pk_id,#{item.pkMember} pk_member,#{item.period} period,#{item.pkRate,jdbcType=INTEGER} pk_rate,
            #{item.directIncome} direct_income,#{item.seePointIncome} see_point_income,#{item.expandIncome} expand_income,#{item.expandNoCapping} expand_no_capping,
            #{item.coachIncome} coach_income,#{item.shareIncome} share_income,
            #{item.serviceIncome} service_income,#{item.backPoints} back_points,#{item.serviceSpend} service_spend,
            #{item.purRealSubtotal} pur_real_subtotal,#{item.repurRangeIncome} repur_range_income,
            #{item.sameLevelIncome} same_level_income,#{item.pointShareIncome} point_share_income,
            #{item.fireShareIncome} fire_share_income,#{item.repurRealSubtotal} repur_real_subtotal,#{item.globalPoints} global_points,
            #{item.storeIncome} store_income,#{item.storeRangeIncome} store_range_income,#{item.realIncomeTotal} real_income_total,
            #{item.pkCountry} pk_country,#{item.pkCreator} pk_creator,#{item.grantStatus} grant_status,#{item.incomeStatus} income_status from dual
        </foreach>) )b
        on (cb.pk_member=b.pk_member and cb.period=b.period)
        WHEN NOT MATCHED THEN
            insert(pk_id,pk_member,period,pk_rate,direct_income,see_point_income,expand_income,expand_no_capping,
        coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_range_income,same_level_income,
        point_share_income,fire_share_income,repur_real_subtotal,
        global_points,store_income,store_range_income,real_income_total,
        pk_country,pk_creator,grant_status,income_status)
            values(b.pk_id,b.pk_member,b.period,b.pk_rate,b.direct_income,b.see_point_income,b.expand_income,b.expand_no_capping,
        b.coach_income,b.share_income,b.service_income,b.back_points,b.service_spend,
        b.pur_real_subtotal,b.repur_range_income,b.same_level_income,
        b.point_share_income,b.fire_share_income,b.repur_real_subtotal,
        b.global_points,b.store_income,b.store_range_income,b.real_income_total,
        b.pk_country,b.pk_creator,b.grant_status,b.income_status)
    </insert>
    <insert id="insertOrUpdateCuMemberBonus">
        merge into cu_member_bonus a
        using(
        select pk_id,pk_member,period,pk_rate,direct_income,expand_income,expand_no_capping,
        expand_capping,coach_income,share_income,service_income,back_points,service_spend,
        pur_real_subtotal,repur_push_income,repur_range_income,repur_expand_income,repur_real_subtotal,
        global_points,real_income_total,
        pk_country,pk_creator,grant_status,income_status from(
        <foreach item="item" index="index" collection="cuMemberBonusList" separator=" union ">
            select #{item.pkId} pk_id,#{item.pkMember} pk_member,#{item.period} period,#{item.pkRate} pk_rate,
            #{item.directIncome} direct_income,#{item.expandIncome} expand_income,#{item.expandNoCapping} expand_no_capping,
            #{item.expandCapping} expand_capping,#{item.coachIncome} coach_income,#{item.shareIncome} share_income,
            #{item.serviceIncome} service_income,#{item.backPoints} back_points,#{item.serviceSpend} service_spend,
            #{item.purRealSubtotal} pur_real_subtotal,#{item.repurPushIncome} repur_push_income,
            #{item.repurRangeIncome} repur_range_income,#{item.repurExpandIncome} repur_expand_income,
            #{item.repurRealSubtotal} repur_real_subtotal,#{item.globalPoints} global_points,
            #{item.realIncomeTotal} real_income_total,#{item.pkCountry} pk_country,#{item.pkCreator} pk_creator,
            #{item.grantStatus} grant_status,#{item.incomeStatus} income_status from dual
        </foreach>)
        ) b
        on (a.pk_member = b.pk_member and a.period = b.period)
        when matched then
        update set
            a.direct_income = a.direct_income + b.direct_income,a.expand_income=a.expand_income+b.expand_income,
            a.expand_no_capping=a.expand_no_capping+b.expand_no_capping,a.expand_capping=a.expand_capping+b.expand_capping,
            a.coach_income=a.coach_income+b.coach_income,a.share_income=a.share_income+b.share_income,
            a.service_income=a.service_income+b.service_income,a.back_points=a.back_points+b.back_points,
            a.service_spend=a.service_spend+b.service_spend,
            a.pur_real_subtotal=a.pur_real_subtotal+b.pur_real_subtotal,
            a.repur_range_income=a.repur_range_income+b.repur_range_income,
            a.repur_real_subtotal=a.repur_real_subtotal+b.repur_real_subtotal,
            a.global_points=a.global_points+b.global_points,a.real_income_total=a.real_income_total+b.real_income_total
        when not matched then
        insert (pk_id,pk_member,period,pk_rate,direct_income,expand_income,expand_no_capping,
            expand_capping,coach_income,share_income,service_income,back_points,service_spend,
            pur_real_subtotal,repur_push_income,repur_range_income,repur_expand_income,repur_real_subtotal,
            global_points,real_income_total,
            pk_country,pk_creator,grant_status,income_status)
            values(b.pk_id,b.pk_member,b.period,b.pk_rate,b.direct_income,b.expand_income,b.expand_no_capping,
            b.expand_capping,b.coach_income,b.share_income,b.service_income,b.back_points,b.service_spend,
            b.pur_real_subtotal,b.repur_push_income,b.repur_range_income,b.repur_expand_income,b.repur_real_subtotal,
            b.global_points,b.pk_country,b.pk_creator,b.grant_status,b.income_status)
    </insert>

    <select id="queryMemberBonusTotalByCondition" resultMap="CuMemberBonus">
        select cb.period,sum(direct_income) direct_income,sum(expand_income) expand_income,sum(expand_no_capping) expand_no_capping,
               sum(coach_income) coach_income,sum(share_income) share_income,
               sum(service_income) service_income,sum(back_points) back_points,sum(service_spend) service_spend,
               sum(pur_real_subtotal) pur_real_subtotal,
               sum(real_income_total) real_income_total,cp.settle_date from cu_member_bonus cb
        inner join cu_member cm
        on cb.pk_member = cm.pk_id
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        where cb.del_flag=0 and cm.del_flag=0
        and cp.del_flag=0 and cb.income_status=0
        <if test="pkVertex != null and pkVertex != ''">
            and cm.pk_vertex = #{pkVertex}
        </if>
        <if test="pkTeamCode != null and pkTeamCode != ''">
            and cm.pk_team_code = #{pkTeamCode}
        </if>
        <if test="startDate != null">
            and cp.settle_date >=#{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and cp.settle_date &lt; #{endDate, jdbcType=DATE}
        </if>
        group by cb.period,cp.settle_date
    </select>

    <select id="queryMemberBonusByCondition" resultMap="CuMemberBonus">
        select cp.settle_date,cb.direct_income,cb.see_point_income,cb.expand_income,cb.expand_no_capping,
        cb.coach_income,cb.share_income,cb.service_income,cb.back_points,cb.service_spend,
        cb.pur_real_subtotal,cb.repur_range_income,cb.same_level_income,cb.point_share_income,cb.repur_real_subtotal,
        cb.global_points,cb.store_income,cb.store_range_income,cb.real_income_total from cu_member_bonus cb
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        where cb.del_flag=0 and cp.del_flag=0
           and cb.pk_member=#{pkMember}
        <if test="grantStatus != null">
            and cb.grant_status >= #{grantStatus}
        </if>
        <if test="startDate != null">
            and cp.settle_date >=#{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and cp.settle_date &lt; #{endDate, jdbcType=DATE}
        </if>
        order by cp.settle_date desc
    </select>
    <select id="queryMemberBonusByConditionForServer" resultMap="CuMemberBonus">
        select cm.member_code,cm.member_name,cm.pk_settle_grade,cm.pk_awards,bv.vertex_name,ct.team_name,
        cb.period,cp.settle_date,cb.direct_income,cb.see_point_income,cb.expand_income,cb.expand_no_capping,
        cb.coach_income,cb.share_income,cb.service_income,cb.back_points,cb.service_spend,
        cb.pur_real_subtotal,cb.repur_range_income,cb.same_level_income,cb.point_share_income,cb.repur_real_subtotal,
        cb.global_points,cb.pk_rate,bc.out_exchange_rate,cb.store_income,cb.store_range_income,
        cb.real_income_total from cu_member_bonus cb
        inner join cu_member cm
        on cb.pk_member = cm.pk_id
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        left join bd_vertex bv
        on bv.pk_id = cm.pk_vertex
        left join cu_member_team ct
        on ct.pk_id = cm.pk_team_code
        left join bd_currency bc
        on bc.pk_country = cb.pk_country and bc.del_flag=0
        where cb.del_flag=0 and cm.del_flag=0
        and cp.del_flag=0
        <if test="bonusParam.pkCountry != null">
            and cm.pk_settle_country = #{bonusParam.pkCountry}
        </if>
        <if test="bonusParam.memberCode != null and bonusParam.memberCode != ''">
            and cm.member_code = #{bonusParam.memberCode}
        </if>
        <if test="bonusParam.memberName != null and bonusParam.memberName != ''">
            and cm.member_name = #{bonusParam.memberName}
        </if>
        <if test="bonusParam.pkVertex != null and bonusParam.pkVertex != ''">
            and cm.pk_vertex = #{bonusParam.pkVertex}
        </if>
        <if test="bonusParam.pkTeamCode != null and bonusParam.pkTeamCode != ''">
            and cm.pk_team_code = #{bonusParam.pkTeamCode}
        </if>
        <if test="bonusParam.startDate != null">
            and cp.settle_date >=#{bonusParam.startDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.endDate != null">
            and cp.settle_date &lt; #{bonusParam.endDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.period != null">
            and cb.period >=#{bonusParam.period}
        </if>
        <if test="bonusParam.vertexList != null and bonusParam.vertexList.size >0">
            and exists(
            select 1 from (
            <foreach collection="bonusParam.vertexList" item="item" separator=" union ">
            select pk_id from cu_member
            where del_flag=0 and PAY_STATUS=1
            start with pk_id = #{item}
            connect by pk_parent = prior pk_id
            </foreach>
            <if test="bonusParam.teamList != null and bonusParam.teamList.size >0">
                union
                <foreach collection="bonusParam.teamList" item="item" separator=" union ">
                    select pk_id from cu_member
                    where del_flag=0 and PAY_STATUS=1
                    start with pk_id = #{item}
                    connect by pk_parent = prior pk_id
                </foreach>
            </if>
            ) a
            where a.pk_id = cb.pk_member
            )
        </if>
        order by cb.period desc
    </select>
    <select id="queryBonusTotalVoByConditionForServer" resultMap="CuMemberBonus">
        select cb.period,cp.settle_date,nvl(min(bc.out_exchange_rate),1) out_exchange_rate,sum(cb.direct_income) direct_income,sum(cb.expand_income) expand_income,
        sum(cb.expand_no_capping) expand_no_capping,sum(cb.coach_income) coach_income,
        sum(cb.share_income) share_income,sum(cb.service_income) service_income,sum(cb.back_points) back_points,
        sum(cb.service_spend) service_spend,sum(cb.pur_real_subtotal) pur_real_subtotal,
        sum(cb.same_level_income) same_level_income,sum(cb.repur_range_income) repur_range_income,
        sum(cb.point_share_income) point_share_income,sum(cb.repur_real_subtotal) repur_real_subtotal,
        sum(cb.global_points) global_points,sum(store_income) store_income,sum(store_range_income) store_range_income,
        sum(cb.real_income_total) real_income_total from cu_member_bonus cb
        inner join cu_member_settle_period cp
        on cb.period = cp.pk_id
        left join bd_currency bc
        on bc.pk_id = cb.pk_rate  and bc.del_flag=0
        where cb.del_flag=0 and cp.del_flag=0
        <if test="bonusParam.pkCountry != null">
            and cb.pk_country = #{bonusParam.pkCountry}
        </if>
        <if test="bonusParam.startDate != null">
            and cp.settle_date >=#{bonusParam.startDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.endDate != null">
            and cp.settle_date &lt; #{bonusParam.endDate, jdbcType=DATE}
        </if>
        <if test="bonusParam.period != null">
            and cb.period =#{bonusParam.period}
        </if>
        <if test="bonusParam.vertexList != null and bonusParam.vertexList.size >0">
            and exists(
            select 1 from (
            <foreach collection="bonusParam.vertexList" item="item" separator=" union ">
                select pk_id from cu_member
                where del_flag=0 and PAY_STATUS=1
                start with pk_id = #{item}
                connect by pk_parent = prior pk_id
            </foreach>
            <if test="bonusParam.teamList != null and bonusParam.teamList.size >0">
                union
                <foreach collection="bonusParam.teamList" item="item" separator=" union ">
                    select pk_id from cu_member
                    where del_flag=0 and PAY_STATUS=1
                    start with pk_id = #{item}
                    connect by pk_parent = prior pk_id
                </foreach>
            </if>
            ) a
            where a.pk_id = cb.pk_member
            )
        </if>
        group by cb.period,cp.settle_date
        order by cb.period desc
    </select>
    <select id="batchQueryCuMemberBonusSeq" resultType="java.lang.Long">
        select cu_member_bonus_seq.nextval from dual connect by rownum &lt;= #{rowNum}
    </select>

    <select id="queryWeekMemberBonus" resultMap="CuMemberBonus">
        select cb.pk_member,sum(expand_income) expand_income from cu_member_bonus cb
                                                                                                                   inner join BD_SYSTEM_CONFIG sy
                                                                                                                              on cb.pk_country = sy.pk_country
        where sy.key = 'EXPAND_CAP_METHOD1' and sy.value='2'
          and cb.period>= #{startPeriod} and cb.period &lt; #{endPeriod}
        group by cb.pk_member
    </select>
</mapper>
