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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberBonusDetail" type="com.hzs.common.domain.member.ext.CuMemberBonusDetailExt">
        <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_BONUS" property="pkBonus"/>
        <result column="PK_ORDER" property="pkOrder"/>
        <result column="PK_BONUS_ITEMS" property="pkBonusItems"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="CAL_TYPE" property="calType"/>
        <result column="CAL_ACHIEVE" property="calAchieve"/>
        <result column="CAL_VALUE" property="calValue"/>
        <result column="PRETAX_INCOME" property="pretaxIncome"/>
        <result column="INCOME_TAX" property="incomeTax"/>
        <result column="REAL_INCOME" property="realIncome"/>
        <result column="REMARK" property="remark"/>
        <result column="INCOME_RATIO" property="incomeRatio"/>
        <result column="INCOME_DIAL_RATIO" property="incomeDialRatio"/>
        <result column="ORDER_DIAL_RATIO" property="orderDialRatio"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="source_country_name" property="sourceCountryName"/>
        <result column="SOURCE_MEMBER_CODE" property="sourceMemberCode"/>
        <result column="SOURCE_MEMBER_NAME" property="sourceMemberName"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="OUT_EXCHANGE_RATE" property="outExchangeRate"/>
        <result column="ORDER_AMOUNT" property="orderAmount"/>
        <result column="ORDER_ACHIEVE" property="orderAchieve"/>
        <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="TEAM_CODE" property="teamCode"/>
        <result column="SETTLE_DATE" property="settleDate"/>
        <result column="ROUND" property="round"/>
        <result column="SECOND" property="second"/>
        <result column="SETTLE_COUNTRY" property="settleCountry"/>
        <result column="PERIOD" property="period"/>
        <result column="pk_source_country" property="pkSourceCountry"/>
        <result column="service_code" property="serviceCode"/>
        <result column="pk_vertex" property="pkVertex"/>
        <result column="nick_name" property="nickName"/>
    </resultMap>

    <insert id="batchInsertCuMemberBonusDetail">
        insert ALL
        <foreach item="item" index="index" collection="cuMemberBonusDetailList" separator=" ">
            into cu_member_bonus_detail (
            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_country, pk_creator)
            values (
            #{item.pkBonus}, nvl(#{item.pkOrder, jdbcType=BIGINT},0), #{item.pkBonusItems}, #{item.incomeStatus},
            #{item.calType}, #{item.calAchieve, jdbcType=NUMERIC}, #{item.calValue, jdbcType=NUMERIC}, #{item.pretaxIncome, jdbcType=NUMERIC},
            #{item.incomeTax, jdbcType=NUMERIC}, #{item.realIncome, jdbcType=NUMERIC}, #{item.remark},
            #{item.incomeRatio, jdbcType=NUMERIC}, #{item.incomeDialRatio, jdbcType=NUMERIC}, #{item.orderDialRatio, jdbcType=NUMERIC},
            #{item.pkCountry}, #{item.pkCreator})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <delete id="deleteCuMemberBonusDetail">
        delete
        from CU_MEMBER_BONUS_DETAIL
        where PK_BONUS in (
            select pk_id
            from cu_member_bonus
            where period = #{period} and grant_status &lt;= 2)
    </delete>

    <select id="queryMemberBonusDetailByCondition" resultMap="CuMemberBonusDetail">
        select b.period,cp.settle_date,so.order_code,so.order_type,so.pk_country pk_source_country,bc.out_exchange_rate,
        cu.member_code source_member_code,cu.member_name source_member_name,so.pk_rate,so.order_amount,
        so.order_achieve,b.pk_bonus_items,b.real_income,b.round,b.second,
        cu.service_code,
        cu.pk_vertex,
        cu.nick_name
        from sa_order so
        inner join (
        select cb.pk_member,cb.period,cd.pk_order,cd.pk_bonus_items,cd.CAL_ACHIEVE,cd.remark,cd.real_income,0 round,0 second from
        cu_member_bonus cb
        inner join cu_member_bonus_detail cd
        on cb.pk_id = cd.pk_bonus
        where cb.income_status=0 and cd.income_status = 0
        <if test="pkBonus != null">
            and cd.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period = #{period}
        </if>
        union all
        select cb.pk_member,cb.period,cp.pk_order,cp.pk_bonus_items,cp.CAL_ACHIEVE,cp.remark,cp.real_income,cp.round,cp.second from
        cu_member_bonus cb
        inner join cu_member_bonus_push cp
        on cb.pk_id = cp.pk_bonus
        where cb.income_status=0 and cp.income_status = 0
        <if test="pkBonus != null">
            and cp.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period =#{period}
        </if>
        union all
        select cb.pk_member,cb.period,ce.pk_order,ce.pk_bonus_items,ce.CAL_ACHIEVE,ce.remark,ce.real_income,0 round,0 second from
        cu_member_bonus cb
        inner join cu_member_bonus_expand ce
        on cb.pk_id = ce.pk_bonus
        where cb.income_status=0 and ce.income_status = 0
        <if test="pkBonus != null">
            and ce.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period =#{period}
        </if>
        union all
        select cb.pk_member,cb.period,cc.pk_order,cc.pk_bonus_items,cc.CAL_ACHIEVE,cc.remark,cc.real_income,0 round,0 second from
        cu_member_bonus cb
        inner join cu_member_bonus_coach cc
        on cb.pk_id = cc.pk_bonus
        where cb.income_status=0 and cc.income_status = 0
        <if test="pkBonus != null">
            and cc.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period =#{period}
        </if>
        union all
        select cb.pk_member,cb.period,cr.pk_order,cr.pk_bonus_items,cr.CAL_ACHIEVE,cr.remark,cr.real_income,0 round,0 second from
        cu_member_bonus cb
        inner join CU_MEMBER_BONUS_RANGE cr
        on cb.pk_id = cr.pk_bonus
        where cb.income_status=0 and cr.income_status = 0
        <if test="pkBonus != null">
            and cr.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period =#{period}
        </if>
        ) b
        on so.pk_id = b.pk_order
        inner join cu_member cu
        on so.pk_member = cu.pk_id
        inner join cu_member_settle_period cp
        on b.period = cp.pk_id
        left join bd_currency bc
        on bc.pk_id=so.pk_rate
        <where>
            b.pk_member=#{pkMember}
            <if test="orderCode != null and orderCode != ''">
                and so.order_code like #{orderCode}|| '%'
            </if>
            <if test="sourceMemberCode != null and sourceMemberCode != ''">
                and (cu.member_code = #{sourceMemberCode} or cu.service_code = #{sourceMemberCode})
            </if>
            <if test="sourceMemberName != null and sourceMemberName != ''">
                and (cu.member_name = #{sourceMemberName} or cu.nick_name = #{sourceMemberName})
            </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>
        </where>
        order by b.period,b.pk_bonus_items,b.real_income desc
    </select>
    <select id="queryMemberBonusDetailByBonusItems" resultMap="CuMemberBonusDetail">
        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,so.pk_rate,so.pk_country pk_source_country,bc.out_exchange_rate,so.order_amount,
        so.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,
        cu.service_code,
        cu.pk_vertex,
        cu.nick_name
        from sa_order so
        inner join (
        select 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 from cu_member_bonus cb
        inner join cu_member_bonus_detail cd
        on cb.pk_id = cd.pk_bonus
        where cb.income_status=0 and cd.income_status = 0
        and cd.pk_bonus_items = #{pkBonusItems}
        <if test="pkBonus != null">
            and cd.pk_bonus = #{pkBonus}
        </if>
        <if test="period != null">
            and cb.period = #{period}
        </if>
        ) b
        on so.pk_id = b.pk_order
        inner join cu_member tar
        on b.pk_member = tar.pk_id
        inner join cu_member cu
        on so.pk_member = cu.pk_id
        inner 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=so.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=so.pk_country
        <where>
            1=1 and b.pk_member=#{pkMember}
            <if test="orderCode != null and orderCode != ''">
                and so.order_code like #{orderCode}|| '%'
            </if>
            <if test="sourceMemberCode != null and sourceMemberCode != ''">
                and (cu.member_code = #{sourceMemberCode} or cu.service_code = #{sourceMemberCode})
            </if>
            <if test="sourceMemberName != null and sourceMemberName != ''">
                and (cu.member_name = #{sourceMemberName} or cu.nick_name = #{sourceMemberName})
            </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>
        </where>
        order by b.period,b.pk_bonus_items,b.real_income desc
    </select>

    <select id="queryMemberBonusDetailByConditionForServer" resultMap="CuMemberBonusDetail">
        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,
            so.pk_rate,
            bc.out_exchange_rate,
            so.order_amount,
            so.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 * from (
        select
        cb.pk_member 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
        from cu_member_bonus cb
        inner join cu_member_bonus_detail cd
        on cb.pk_id = cd.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and cd.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.period != null">
            and cb.period = #{bonusParam.period}
        </if>
        union all
        select
        cb.pk_member PK_SOURCE_MEMBER,
        cb.pk_member,cp.pk_order,cb.period,cb.pk_country,cp.pk_bonus_items,cp.income_status,cp.CAL_ACHIEVE,cp.pretax_income,cp.income_tax,cp.real_income,
        cp.remark,cp.income_ratio,cp.income_dial_ratio,cp.order_dial_ratio
        from cu_member_bonus cb
        inner join cu_member_bonus_push cp
        on cb.pk_id = cp.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and cp.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.period != null">
            and cb.period =#{bonusParam.period}
        </if>
        union all
        select
        cb.pk_member PK_SOURCE_MEMBER,
        cb.pk_member,ce.pk_order,cb.period,cb.pk_country,ce.pk_bonus_items,ce.income_status,ce.CAL_ACHIEVE,ce.pretax_income,ce.income_tax,ce.real_income,
        ce.remark,ce.income_ratio,ce.income_dial_ratio,ce.order_dial_ratio
        from cu_member_bonus cb
        inner join cu_member_bonus_expand ce
        on cb.pk_id = ce.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and ce.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.period != null">
            and cb.period =#{bonusParam.period}
        </if>
        union all
        select
        cb.pk_member PK_SOURCE_MEMBER,
        cb.pk_member,cc.pk_order,cb.period,cb.pk_country,cc.pk_bonus_items,cc.income_status,cc.CAL_ACHIEVE,cc.pretax_income,cc.income_tax,cc.real_income,
        cc.remark,cc.income_ratio,cc.income_dial_ratio,cc.order_dial_ratio
        from cu_member_bonus cb
        inner join cu_member_bonus_coach cc
        on cb.pk_id = cc.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and cc.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.period != null">
            and cb.period =#{bonusParam.period}
        </if>
        union all
        select
        cb.pk_member PK_SOURCE_MEMBER,
        cb.pk_member,cr.pk_order,cb.period,cb.pk_country,cr.pk_bonus_items,cr.income_status,cr.CAL_ACHIEVE,cr.pretax_income,cr.income_tax,cr.real_income,
        cr.remark,cr.income_ratio,cr.income_dial_ratio,cr.order_dial_ratio
        from cu_member_bonus cb
        inner join cu_member_bonus_range cr
        on cb.pk_id = cr.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and cr.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.pkGrade != null">
            and cr.pk_range = #{bonusParam.pkGrade}
        </if>
        <if test="bonusParam.period != null">
            and cb.period = #{bonusParam.period}
        </if>
        <if test="bonusParam.pkBonusItems != null">
            and cd.pk_bonus_items = #{bonusParam.pkBonusItems}
        </if>
        union all
        select
        cs.PK_SOURCE_MEMBER,
        cb.pk_member,cs.pk_order,cb.period,cb.pk_country,cs.pk_bonus_items,cs.income_status,cs.CAL_ACHIEVE,cs.pretax_income,cs.income_tax,cs.real_income,
        cs.remark,cs.income_ratio,cs.income_dial_ratio,cs.order_dial_ratio
        from cu_member_bonus cb
        inner join CU_MEMBER_BONUS_STAGE cs
        on cb.pk_id = cs.pk_bonus
        where 1=1
        <if test="bonusParam.pkBonus != null">
            and cs.pk_bonus = #{bonusParam.pkBonus}
        </if>
        <if test="bonusParam.pkGrade != null">
            and cs.pk_range = #{bonusParam.pkGrade}
        </if>
        <if test="bonusParam.period != null">
            and cb.period = #{bonusParam.period}
        </if>
        <if test="bonusParam.pkBonusItems != null">
            and cd.pk_bonus_items = #{bonusParam.pkBonusItems}
        </if>
        ) x
        where 1=1
        <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 = x.pk_member
            )
        </if>
        ) b
        left join sa_order so
        on so.pk_id = b.pk_order
        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=so.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.orderType != null">
                and so.order_type = #{bonusParam.orderType}
            </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>

    <select id="queryMemberBonusDetailByBonusItemsForServer" resultMap="CuMemberBonusDetail">
        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,
        nvl(so.order_amount,0) order_amount, nvl(so.order_achieve,0) 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 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_detail 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 so.pk_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=so.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>
