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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuBonusExpandExt" type="com.hzs.common.domain.member.ext.CuBonusExpandExt">
        <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="SETTLE_DATE" property="settleDate"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="DAY_ACHIEVE" property="dayAchieve"/>
        <result column="DAY_BONUS" property="dayBonus"/>
        <result column="YES_DAY_RATIO" property="yesDayRatio"/>
        <result column="CUR_DAY_RATIO" property="curDayRatio"/>
        <result column="MONTH_ACHIEVE" property="monthAchieve"/>
        <result column="MONTH_BONUS" property="monthBonus"/>
        <result column="MONTH_RATIO" property="monthRatio"/>
        <result column="ADD_TOUCH" property="addTouch"/>
        <result column="SORT" property="sort"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="EXPAND_RATIO" property="expandRatio"/>
    </resultMap>

    <update id="mergeBonusExpandByList">
        merge into cu_bonus_expand ce
        using (
        select * from(
        <foreach collection="cuBonusExpandList" item="item" index="index" separator=" union ">
            select
            #{item.pkId} pk_id, #{item.dayAchieve,jdbcType=NUMERIC} day_achieve, #{item.dayBonus,jdbcType=NUMERIC} day_bonus,
            #{item.yesDayRatio,jdbcType=NUMERIC} yes_day_ratio, #{item.curDayRatio,jdbcType=NUMERIC} cur_day_ratio,
            #{item.monthAchieve,jdbcType=NUMERIC} month_achieve, #{item.monthBonus,jdbcType=NUMERIC} month_bonus,
            #{item.monthRatio,jdbcType=NUMERIC} month_ratio, #{item.dayRepAchieve,jdbcType=NUMERIC} day_rep_achieve,
            #{item.dayRepBonus,jdbcType=NUMERIC} day_rep_bonus, #{item.curDayRepRatio,jdbcType=NUMERIC} cur_day_rep_ratio
            from dual
        </foreach>)
        ) b
        on (ce.pk_id = b.pk_id)
        when matched then
        update set ce.day_achieve = b.day_achieve, ce.day_bonus = b.day_bonus, ce.yes_day_ratio = b.yes_day_ratio,
        ce.cur_day_ratio = b.cur_day_ratio, ce.month_achieve = b.month_achieve,
        ce.month_bonus = b.month_bonus, ce.month_ratio = b.month_ratio,
        ce.day_rep_achieve = b.day_rep_achieve, ce.day_rep_bonus = b.day_rep_bonus,
        ce.cur_day_rep_ratio = b.cur_day_rep_ratio
    </update>

    <update id="truncateTable">
        truncate table cu_member_repeat_bonus
    </update>

    <!-- 时间、期间计算波比 首购 -->
    <select id="calculateDayRatioByBonusExpandParam" resultMap="CuBonusExpandExt">
        <foreach collection="bonusExpandParam.memberIdList" item="item" index="index" separator=" union ">
            select a.pk_member,
            COALESCE(a.order_amount, 0) day_achieve,
            COALESCE(b.pur_real_subtotal, 0) day_bonus,
            COALESCE(ROUND(b.pur_real_subtotal / a.order_amount * 100, 2), 0) yes_day_ratio
            from (
            select #{item} pk_member, sum(order_amount) order_amount
            from (
            select so.pk_member, order_achieve, order_amount
            from sa_order so
            inner join (
            select pk_id, del_flag from cu_member
            start with pk_id= #{item}
            connect by pk_place_parent = prior pk_id
            ) cu
            on cu.pk_id = so.pk_member
            where so.del_flag = 0 and so.order_status = 1
            and so.order_type in (1, 2, 24, 25) and so.pk_country = 1
            and so.pay_time >= #{bonusExpandParam.startDate, jdbcType = DATE}
            and so.pay_time &lt; #{bonusExpandParam.endDate, jdbcType = DATE} )
            ) a
            inner join (
            select #{item} pk_member, nvl(sum(pur_real_subtotal), 0) pur_real_subtotal
            from ${tableName} cb
            inner join (
            select * from cu_member
            start with pk_id = #{item}
            connect by pk_place_parent =prior pk_id
            ) cu
            on cu.pk_id = cb.pk_member
            where
            period between #{bonusExpandParam.startPeriod} and #{bonusExpandParam.endPeriod}
            and cb.del_flag = 0
            and cb.pk_country = 1
            ) b
            on a.pk_member = b.pk_member
            join cu_member cm
            on cm.pk_id = a.pk_member
        </foreach>
    </select>

    <!-- 时间、期间计算波比 复购 -->
    <select id="calculateDayRepRatioByBonusExpandParam" resultMap="CuBonusExpandExt">
        <foreach collection="bonusExpandParam.memberIdList" item="item" index="index" separator=" union ">
            select a.pk_member,
            COALESCE(a.order_amount, 0) day_rep_achieve,
            COALESCE(b.pur_real_subtotal, 0) day_rep_bonus,
            COALESCE(ROUND(b.pur_real_subtotal / a.order_amount * 100, 2), 0) cur_day_rep_ratio
            from(
            select #{item} pk_member, sum(order_amount) order_amount
            from (
            select so.pk_member, order_achieve, order_amount from sa_order so
            inner join (
            select * from cu_member
            start with pk_id = #{item}
            connect by pk_place_parent = prior pk_id
            ) cu
            on cu.pk_id = so.pk_member
            where so.del_flag = 0 and so.order_status = 1
            and so.order_achieve > 0
            and so.order_type in (3, 26) and so.pk_country = 1
            and so.pay_time >= #{bonusExpandParam.startDate,jdbcType=DATE}
            and so.pay_time &lt; #{bonusExpandParam.endDate,jdbcType=DATE} )
            ) a
            inner join(
            select #{item} pk_member, nvl(sum(repur_real_subtotal), 0) pur_real_subtotal
            from ${tableName} cb
            inner join (
            select * from cu_member
            start with pk_id = #{item}
            connect by pk_place_parent =prior pk_id
            ) cu
            on cu.pk_id = cb.pk_member
            where period between #{bonusExpandParam.startPeriod} and #{bonusExpandParam.endPeriod}
            and cb.del_flag = 0
            and cb.pk_country = 1
            ) b
            on a.pk_member = b.pk_member
            join cu_member cm
            on cm.pk_id = a.pk_member
        </foreach>
    </select>

    <select id="queryZeroMemberByVertex" resultMap="CuBonusExpandExt">
        select cm.pk_id pk_member, bt.pk_id pk_vertex, nvl(bt.sort, 99) sort, cm.pk_settle_country pk_country
        from cu_member cm
                 inner join cu_member par
                            on par.pk_id = cm.pk_place_parent
                 left join bd_vertex bt
                           on cm.pk_id = bt.pk_member and bt.del_flag = 0
        where par.member_code = 'GJ30720200'
          and cm.del_flag = 0
        order by sort, pk_member
    </select>

    <select id="queryBonusExpand" resultMap="CuBonusExpandExt">
        select ce.*, cm.member_code, cm.member_name, bv.vertex_name
        from cu_bonus_expand ce
        inner join cu_member cm
        on ce.pk_member = cm.pk_id
        left join bd_vertex bv
        on ce.pk_vertex = bv.pk_id and bv.del_flag = 0
        where ce.del_flag = 0 and cm.del_flag = 0
        <if test="pkCountry != null">
            and ce.pk_country = #{pkCountry}
        </if>
        <if test="settleDate != null">
            and ce.settle_date = #{settleDate,jdbcType=DATE}
        </if>
        <choose>
            <when test="pkParent != null">and ce.pk_parent = #{pkParent}</when>
            <otherwise>and ce.pk_parent is null</otherwise>
        </choose>
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code like '%' || #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name like '%' || #{memberName} || '%'
        </if>
        <if test="vertexName != null and vertexName != ''">
            and bv.vertex_name like '%' || #{vertexName} || '%'
        </if>
        order by ce.sort,ce.pk_member
    </select>

    <select id="queryBonusExpandDis" resultMap="CuBonusExpandExt">
        select settle_date
        from cu_bonus_expand
        where del_flag = 0
          and (yes_day_ratio > 65 or CUR_DAY_RATIO > 65 or MONTH_RATIO > 65)
          and settle_date > sysdate - 7
        group by settle_date
    </select>

</mapper>
