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

    <!-- 删除会员阶段奖金表 -->
    <delete id="deleteCuMemberBonusStage">
        delete
        from CU_MEMBER_BONUS_STAGE
        where PK_BONUS in (
            select pk_id
            from cu_member_bonus
            where period = #{period}
              and grant_status &lt;= 2)
    </delete>

    <!-- 批量插入会员阶段奖 -->
    <insert id="batchInsertCuMemberBonusStage">
        insert ALL
        <foreach item="item" index="index" collection="cuMemberBonusStageList" separator=" ">
            into cu_member_bonus_stage (
            pk_bonus, pk_order, pk_bonus_items, income_status,
            cal_type, cal_achieve, cal_value,
            pretax_income, income_tax, real_income, remark,
            income_ratio, income_dial_ratio, order_dial_ratio,
            pk_creator, child_node, stage, stage_num, pk_source_member)
            values (
            #{item.pkBonus}, #{item.pkOrder}, #{item.pkBonusItems}, #{item.incomeStatus},
            #{item.calType}, #{item.calAchieve}, #{item.calValue},
            #{item.pretaxIncome}, #{item.incomeTax}, #{item.realIncome}, #{item.remark},
            #{item.incomeRatio}, #{item.incomeDialRatio}, #{item.orderDialRatio},
            #{item.pkCreator}, #{item.childNode}, #{item.stage}, #{item.stageNum}, #{item.pkSourceMember})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <select id="queryMemberBonusStageByBonusItemsFoServer"
            resultType="com.hzs.common.domain.member.ext.CuMemberBonusDetailExt"
            parameterType="com.hzs.bonus.bonus.param.BonusParam">
        select b.period,cp.settle_date,so.order_code,so.order_type,bt.short_name source_country_name,
        cu.member_code source_member_code,cu.member_name source_member_name,nvl(so.pk_rate,b.pk_rate) pk_rate,
        nvl(bc.out_exchange_rate,0) out_exchange_rate,
        so.order_amount order_amount, so.order_achieve order_achieve,
        b.pk_country,b.pk_bonus_items,b.pretax_income,b.income_tax,b.real_income,b.income_status,
        tar.member_code,tar.member_name,
        bg.pk_transaction pk_settle_grade, ba.pk_transaction pk_awards,
        bv.vertex_name,ct.team_name,
        b.remark,b.income_ratio,b.income_dial_ratio,b.order_dial_ratio,bct.short_name settle_country
        from (
        select
        cd.PK_SOURCE_MEMBER,
        cb.pk_member,cd.pk_order,cb.period,cb.pk_country,cd.pk_bonus_items,cd.income_status,
        cd.CAL_ACHIEVE,cd.pretax_income,cd.income_tax,cd.real_income,
        cd.remark,cd.income_ratio,cd.income_dial_ratio,cd.order_dial_ratio,cb.pk_rate
        from cu_member_bonus cb
        inner join cu_member_bonus_stage cd
        on cb.pk_id = cd.pk_bonus
        where 1=1
        <if test="bonusParam.period != null">
            and cb.period = #{bonusParam.period}
        </if>
        <if test="bonusParam.pkBonusItems != null">
            and cd.pk_bonus_items = #{bonusParam.pkBonusItems}
        </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>
        ) b
        left join sa_order so
        on so.pk_id = b.pk_order
        <if test="bonusParam.orderType != null">
            and so.order_type = #{bonusParam.orderType}
        </if>
        LEFT JOIN cu_member tar ON b.pk_member = tar.pk_id
        LEFT JOIN cu_member cu ON b.PK_SOURCE_MEMBER = cu.pk_id
        left join cu_member_settle_period cp
        on b.period = cp.pk_id
        left join bd_vertex bv
        on bv.pk_id = tar.pk_vertex
        left join cu_member_team ct
        on ct.pk_id = tar.pk_team_code
        left join bd_currency bc
        on bc.pk_id=nvl(so.pk_rate,b.pk_rate)
        left join bd_grade bg
        on bg.pk_id=tar.pk_settle_grade
        left join bd_awards ba
        on ba.pk_id=tar.pk_awards
        LEFT JOIN bd_country bt ON bt.pk_id = cu.pk_country
        left join bd_country bct
        on bct.pk_id=tar.pk_settle_country
        <where>
            1=1
            <if test="bonusParam.pkCountry != null">
                and tar.pk_settle_country = #{bonusParam.pkCountry}
            </if>
            <if test="bonusParam.systemType != null">
                and cu.system_type = #{bonusParam.systemType}
            </if>
            <if test="bonusParam.pkVertex != null">
                and bv.pk_id = #{bonusParam.pkVertex}
            </if>
            <if test="bonusParam.pkTeamCode != null">
                and ct.pk_id =#{bonusParam.pkTeamCode}
            </if>
            <if test="bonusParam.sourceCountry != null">
                and bt.pk_id = #{bonusParam.sourceCountry}
            </if>
            <if test="bonusParam.incomeStatus != null">
                and b.income_status = #{bonusParam.incomeStatus}
            </if>
            <if test="bonusParam.orderCode != null and bonusParam.orderCode != ''">
                and so.order_code like #{bonusParam.orderCode}|| '%'
            </if>
            <if test="bonusParam.sourceMemberCode != null and bonusParam.sourceMemberCode != ''">
                and cu.member_code = #{bonusParam.sourceMemberCode}
            </if>
            <if test="bonusParam.sourceMemberName != null and bonusParam.sourceMemberName != ''">
                and cu.member_name = #{bonusParam.sourceMemberName}
            </if>
            <if test="bonusParam.memberCode != null and bonusParam.memberCode != ''">
                and tar.member_code = #{bonusParam.memberCode}
            </if>
            <if test="bonusParam.memberName != null and bonusParam.memberName != ''">
                and tar.member_name = #{bonusParam.memberName}
            </if>
            <if test="bonusParam.settleCountry != null">
                and tar.pk_settle_country = #{bonusParam.settleCountry}
            </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>
        </where>
        <choose>
            <when test="bonusParam.sort == null or bonusParam.sort ==1">order by cp.settle_date desc, b.real_income desc</when>
            <when test="bonusParam.sort ==2">order by tar.pk_vertex, b.real_income desc</when>
            <otherwise>order by tar.pk_teamcode</otherwise>
        </choose>
    </select>
</mapper>
