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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberMonthAchieve" type="com.hzs.common.domain.member.ext.CuMemberMonthAchieveExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PERIOD" property="period"/>
        <result column="SETTLE_MONTH" property="settleMonth"/>
        <result column="RECOMMEND_MONEY" property="recommendMoney"/>
        <result column="RECOMMEND_ACHIEVE" property="recommendAchieve"/>
        <result column="REPURCHASE_MONEY" property="repurchaseMoney"/>
        <result column="REPURCHASE_ACHIEVE" property="repurchaseAchieve"/>
        <result column="WALL_TYPE" property="wallType"/>
        <result column="MONEY" property="money"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="HEAD_PATH" property="headPath"/>
    </resultMap>

    <insert id="insertCuMemberMonthAchieve">
        merge into CU_MEMBER_MONTH_ACHIEVE a
            using (
                select pk_member,
                       #{period} period,
                       RECOMMEND_MONEY,
                       RECOMMEND_ACHIEVE,
                       REPURCHASE_MONEY,
                       REPURCHASE_ACHIEVE,
                       pk_country
                from (
                         select pk_member,
                                pk_country,
                                sum(REGISTER_NEW_AMOUNT + UPGRADE_NEW_AMOUNT)                    RECOMMEND_MONEY,
                                sum(REGISTER_NEW_PV + UPGRADE_NEW_PV)                            RECOMMEND_ACHIEVE,
                                sum(REPURCHASE_NEW_AMOUNT + HI_FUN_NEW_AMOUNT + MALL_NEW_AMOUNT) REPURCHASE_MONEY,
                                sum(REPURCHASE_PV + HI_FUN_NEW_PV + MALL_NEW_PV)                 REPURCHASE_ACHIEVE
                         from ${tableName}
                         where REGISTER_NEW_AMOUNT + UPGRADE_NEW_AMOUNT > 0
                            or REPURCHASE_NEW_AMOUNT + HI_FUN_NEW_AMOUNT + MALL_NEW_AMOUNT > 0
                         group by pk_member, pk_country)
            ) b
            on (a.pk_member = b.pk_member and a.period = b.period)
            when matched then
                update set a.RECOMMEND_MONEY = b.RECOMMEND_MONEY, a.RECOMMEND_ACHIEVE = b.RECOMMEND_ACHIEVE,
                    a.REPURCHASE_MONEY = b.REPURCHASE_MONEY,a.REPURCHASE_ACHIEVE = b.REPURCHASE_ACHIEVE
            WHEN NOT MATCHED THEN
                insert (PK_ID, PK_MEMBER, PERIOD, SETTLE_MONTH, RECOMMEND_MONEY, RECOMMEND_ACHIEVE, REPURCHASE_MONEY,
                        REPURCHASE_ACHIEVE, PK_COUNTRY, PK_CREATOR)
                    values (CU_MEMBER_MONTH_ACHIEVE_SEQ.NEXTVAL,
                            b.pk_member, b.period, #{month}, b.RECOMMEND_MONEY, b.RECOMMEND_ACHIEVE, b.REPURCHASE_MONEY,
                            b.REPURCHASE_ACHIEVE, b.PK_COUNTRY, 1)
    </insert>

    <select id="queryCuMemberMonthAchieveByDate" resultMap="CuMemberMonthAchieve">
        select *
        from (
                 select *
                 from (
                          select pk_member, pk_country, ${recommend} wall_type, sum(recommend_money) money
                          from CU_MEMBER_MONTH_ACHIEVE
                          where del_flag = 0
                            and period >= #{startPeriod}
                            and period &lt; #{endPeriod}
                          group by pk_member, pk_country
                          order by sum(recommend_money) desc
                      )
                 where rownum &lt;= 3
                 union
                 select *
                 from (
                          select pk_member, pk_country, ${repurchase} wall_type, sum(repurchase_money) money
                          from CU_MEMBER_MONTH_ACHIEVE
                          where del_flag = 0
                            and period >= #{startPeriod}
                            and period &lt; #{endPeriod}
                          group by pk_member, pk_country
                          order by sum(repurchase_money) desc
                      )
                 where rownum &lt;= 3) a
        order by wall_type, a.money desc
    </select>

</mapper>
