<?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.activity.other.mapper.AcOtherPriceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.other.AcOtherPrice">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_BASE_CONFIG" property="pkBaseConfig"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PARENT_PV" property="parentPv"/>
        <result column="STATUS" property="status"/>
    </resultMap>

    <!-- 查询会员活动数据 -->
    <select id="queryMemberOrderPrice" resultMap="BaseResultMap">
        select aop.*
        from ac_other_price aop
        where aop.del_flag = 0
          and aop.pk_base_config = #{pkBaseConfig}
          and aop.pk_member = #{pkMember}
    </select>

    <!-- 处理活动直推业绩以及达标状态 -->
    <update id="handleOtherPrice">
        merge into AC_OTHER_PRICE aor
            using (select cm.pk_member,
                          nvl(tmp.parent_pv, 0) parent_pv,
                          case
                              when count(aopd.pk_id) >= #{placeDeptNumber} and parent_pv >= #{parentPv} then
                                  2
                              when count(aopd.pk_id) >= 1 then
                                  1
                              else
                                  0
                              end status
                   from (select distinct cms.pk_member
                         from CU_MEMBER_SETTLE_D${endPeriod} cms
                                  inner join cu_member cm
                                             on cm.pk_id = cms.pk_member
                                                 and cm.del_flag = 0
                                                 and cm.category = 0
                                                 and cm.pay_status = 1
                                                 and cm.pk_settle_country = #{pkCountry}) cm
                            left join (select so.pk_reference       pk_member,
                                              sum(so.order_achieve) parent_pv
                                       from sa_order so
                                       where so.del_flag = 0
                                         and so.order_type in (1, 2)
                                         and so.order_status = 1
                                         and so.pk_country = #{pkCountry}
                                         and so.pay_time >= #{startDate}
                                         and #{endDate} >= so.pay_time
                                       group by so.pk_reference) tmp
                                      on tmp.pk_member = cm.pk_member
                            left join ac_other_price_data aopd
                                      on aopd.pk_member = cm.pk_member
                                          and aopd.del_flag = 0
                                          and aopd.pk_base_config = #{pkBaseConfig}
                                          and aopd.pk_country = #{pkCountry}
                                          and aopd.sum_pv >= #{placeDeptPv}
                   group by cm.pk_member, tmp.parent_pv) tmp
            on (aor.PK_BASE_CONFIG = #{pkBaseConfig} and aor.pk_member = tmp.pk_member)
            WHEN NOT MATCHED THEN
                insert
                    (pk_id,
                     pk_base_config,
                     pk_member,
                     parent_pv,
                     status,
                     pk_country,
                     pk_creator)
                    values (AC_OTHER_PRICE_SEQ.Nextval,
                            #{pkBaseConfig},
                            tmp.pk_member,
                            tmp.parent_pv,
                            tmp.status,
                            #{pkCountry},
                            100000000)
            WHEN MATCHED THEN
                update
                    set aor.parent_pv = tmp.parent_pv,
                        aor.status = tmp.status,
                        aor.pk_modified = 100000000,
                        aor.modified_time = sysdate
    </update>

</mapper>
