<?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.tourism.mapper.AcTourismDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.tourism.AcTourismData">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <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_COUNTRY" property="pkCountry"/>
        <result column="PK_BASE_ID" property="pkBaseId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="POINT" property="point"/>
    </resultMap>

    <!-- 旅游活动更新积分 -->
    <update id="handleTourismData">
        MERGE INTO AC_TOURISM_DATA atd
        USING (
        select t.pk_member, sum(t.point) point
        from (
        select t.pk_member, sum(t.point) point
        from (
        <!-- 升级奖衔，自己获得积分 -->
        select t.pk_member, sum(point) point
        from (select cma.pk_member,
        case
        when ba.awards_value in (10, 15, 20) then
        10
        when ba.awards_value in (25, 30, 35) then
        20
        when ba.awards_value in (40, 45, 50) then
        30
        else
        0
        end point
        from (select cma.pk_member, cma.new_level
        from cu_member_awards cma
        where cma.del_flag = 0
        and cma.pk_country = #{pkCountry}
        and cma.creation_time >= #{startDate}
        and #{endDate} >= cma.creation_time
        and cma.up_type = 1
        and cma.new_level in
        (select ba.pk_id
        from bd_awards ba
        where ba.pk_country = #{pkCountry}
        and ba.del_flag = 0
        and ba.awards_value >= 10)
        group by cma.pk_member, cma.new_level) cma
        left join bd_awards ba
        on ba.pk_id = cma.new_level) t
        group by t.pk_member
        union all
        <!-- 升级奖衔，招商人获得积分 -->
        select t.pk_parent, sum(point) point
        from (select cma.pk_member,
        cm.pk_parent,
        case
        when ba.awards_value in (10, 15, 20) then
        10
        when ba.awards_value in (25, 30, 35) then
        20
        when ba.awards_value in (40, 45, 50) then
        30
        else
        0
        end point
        from (select cma.pk_member, cma.new_level
        from cu_member_awards cma
        where cma.del_flag = 0
        and cma.pk_country = #{pkCountry}
        and cma.creation_time >= #{startDate}
        and #{endDate} >= cma.creation_time
        and cma.up_type = 1
        and cma.new_level in
        (select ba.pk_id
        from bd_awards ba
        where ba.pk_country = #{pkCountry}
        and ba.del_flag = 0
        and ba.awards_value >= 10)
        group by cma.pk_member, cma.new_level) cma
        inner join cu_member cm
        on cma.pk_member = cm.pk_id
        and cm.del_flag = 0
        and cm.pay_status = 1
        and cm.category = 0
        left join bd_awards ba
        on ba.pk_id = cma.new_level
        left join cu_member cmp
        on cmp.pk_id = cm.pk_parent
        left join bd_awards bap
        on bap.pk_id = cmp.pk_awards
        where bap.awards_value >= 10
        ) t
        group by t.pk_parent
        ) t
        group by t.pk_member
        union all
        select t.pk_id, TRUNC(sum(t.order_achieve) / 10000) point
        from (
        <!-- 直推推荐PV -->
        select cm.pk_id,
        sum(nvl(so.order_achieve, 0) * #{rate}) order_achieve
        from cu_member cm
        left join sa_order so
        on so.pk_reference = cm.pk_id
        and so.del_flag = 0
        and so.order_status = 1
        and so.pk_country = #{pkCountry}
        and so.order_type in (1, 2, 7, 25, 26)
        and so.pay_time >= #{startDate}
        and #{endDate} >= so.pay_time
        and so.order_achieve != 0
        where cm.del_flag = 0
        and cm.pk_country = #{pkCountry}
        and cm.category = 0
        and cm.pk_awards in
        (select ba.pk_id
        from bd_awards ba
        where ba.pk_country = #{pkCountry}
        and ba.del_flag = 0
        and ba.awards_value >= 10)
        and so.order_achieve is not null
        group by cm.pk_id
        union all
        <!-- 本人复购PV，排除指定产品不统计 -->
        select cm.pk_id,
        sum (nvl(soi.achievement, 0) * soi.quantity * #{rate}) order_achieve
        from cu_member cm
        left join sa_order so
        on so.pk_member = cm.pk_id
        and so.del_flag = 0
        and so.order_status = 1
        and so.pk_country = #{pkCountry}
        and so.order_type in (3, 12, 14, 22)
        and so.pay_time >= #{startDate}
        and #{endDate} >= so.pay_time
        and so.order_achieve != 0
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        and soi.achievement != 0
        and soi.pk_product not in
        (select bp.pk_id
        from bd_product bp
        where bp.del_flag = 0
        and bp.pk_country = #{pkCountry}
        <if test="existProductCodeList != null and existProductCodeList.size > 0">
            and bp.product_code in
            <foreach collection="existProductCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        )
        where cm.del_flag = 0
        and cm.pk_country = #{pkCountry}
        and cm.category = 0
        and cm.pk_awards in
        (select ba.pk_id
        from bd_awards ba
        where ba.pk_country = #{pkCountry}
        and ba.del_flag = 0
        and ba.awards_value >= 10)
        and soi.achievement is not null
        group by cm.pk_id) t
        group by t.pk_id) t
        group by t.pk_member
        ) t
        on (t.pk_member = atd.pk_member and atd.pk_base_id = #{pkBaseId})
        WHEN NOT MATCHED THEN
        insert
        (PK_ID,
        PK_BASE_ID,
        PK_MEMBER,
        POINT,
        CREATION_TIME,
        PK_CREATOR,
        PK_COUNTRY)
        values
        (AC_TOURISM_DATA_SEQ.Nextval,
        #{pkBaseId},
        t.pk_member,
        t.point,
        sysdate,
        100000000,
        #{pkCountry})
        WHEN MATCHED THEN
        update set atd.point = t.point, atd.modified_time = sysdate
    </update>

    <select id="queryList" resultType="com.hzs.activity.tourism.vo.TourismDataVo">
        select bc.CREATION_TIME creationTime ,td.POINT point,bc.ACT_NAME actName,
        bc.ACT_START_DATE actStartDate, bc.ACT_END_DATE actEndDate ,
        cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,ba.AWARDS_NAME awardsVal,bg.GRADE_NAME gradeVal,
        bv.VERTEX_NAME vertexName
        from AC_TOURISM_DATA td
        left join AC_BASE_CONFIG bc on bc.PK_ID = td.PK_BASE_ID
        left join CU_MEMBER cm on cm.PK_ID = td.PK_MEMBER
        left join bd_awards ba on ba.PK_ID = cm.PK_AWARDS
        left join bd_grade bg on bg.PK_ID = cm.PK_SETTLE_GRADE
        left join BD_VERTEX bv on bv.PK_ID = cm.PK_VERTEX
        where 1= 1 and cm.del_flag = 0 and cm.CATEGORY = 0

        <if test="pkVertex != null">
            and cm.PK_VERTEX = #{pkVertex}
        </if>
        <if test="actName != null and actName != ''">
            and bc.act_name = #{actName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="startCreationTime != null ">
            and bc.CREATION_TIME >= #{startCreationTime}
        </if>
        <if test="endCreationTime != null ">
            and bc.CREATION_TIME &lt;= #{endCreationTime}
        </if>

        <if test="startActStartDate != null ">
            and bc.ACT_START_DATE >= #{startActStartDate}
        </if>
        <if test="endActStartDate != null ">
            and bc.ACT_START_DATE &lt;= #{endActStartDate}
        </if>

        <if test="startActEndDate != null ">
            and bc.ACT_END_DATE >= #{startActEndDate}
        </if>
        <if test="endActEndDate != null ">
            and bc.ACT_END_DATE &lt;= #{endActEndDate}
        </if>
        order by bc.CREATION_TIME desc , td.point desc

    </select>

</mapper>
