<?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.member.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>

    <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>

    <select id="queryCuMemberMonthAchieveRecommend" resultMap="CuMemberMonthAchieve">
        select rownum rank, nvl(cu.nick_name, cu.member_name) member_name, cu.HEAD_PATH, a.money
        from (
                 select pk_member, sum(recommend_money) money
                 from CU_MEMBER_MONTH_ACHIEVE
                 where del_flag = 0
                   and pk_country = #{pkCountry}
                   and period between #{startPeriod} and #{endPeriod}
                 group by pk_member
                 order by sum(recommend_money) desc) a
                 inner join cu_member cu
                            on a.pk_member = cu.pk_id
        where rownum &lt;= 3
    </select>

    <select id="queryCuMemberMonthAchieveInfo" resultType="com.hzs.member.achieve.controller.vo.RealPerformanceVo">
        select nvl(sum(A_NEW_PV), 0)     firstLeftPurchaseTotal,
               nvl(sum(B_NEW_PV), 0)     firstRightPurchaseTotal,
               nvl(sum(REP_A_NEW_PV), 0) firstLeftRepurchaseTotal,
               nvl(sum(REP_B_NEW_PV), 0) firstRightRepurchaseTotal
        from ${achieveTable}
        WHERE pk_member = #{pkMember}
    </select>
</mapper>
