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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.other.AcOtherPriceData">
        <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="PLACE_DEPT" property="placeDept"/>
        <result column="SUM_PV" property="sumPv"/>
    </resultMap>

    <!-- 处理其它奖项数据（只有结束期间） -->
    <update id="handleOtherPriceDataOnlyEnd">
        merge into AC_OTHER_PRICE_DATA aord
        using
        (select t.pk_member, t.place_dept, nvl(t.sum_pv, 0) sum_pv
        from CU_MEMBER_SETTLE_D${endPeriod} t
        inner join cu_member cm
        on cm.pk_id = t.pk_member
        and cm.pk_country = #{pkCountry}
        and cm.del_flag = 0
        and cm.category = 0
        and cm.pay_status = 1
        ) tmp
        on (1 = 0)
        <!--
        on (aord.PK_BASE_CONFIG = #{pkBaseConfig} and tmp.pk_member = aord.pk_member and tmp.place_dept = aord.place_dept)
        WHEN MATCHED THEN
            update set aord.sum_pv = tmp.sum_pv, aord.modified_time = sysdate
            -->
        WHEN NOT MATCHED THEN
        insert
        (PK_ID,
        PK_BASE_CONFIG,
        PK_MEMBER,
        PLACE_DEPT,
        SUM_PV,
        PK_COUNTRY,
        PK_CREATOR)
        values (AC_OTHER_PRICE_DATA_SEQ.Nextval,
        #{pkBaseConfig},
        tmp.pk_member,
        tmp.place_dept,
        tmp.sum_pv,
        #{pkCountry},
        100000000)
    </update>

    <!-- 处理其它奖项数据（开始 + 结束期间） -->
    <update id="handleOtherPriceDataAll">
        merge into AC_OTHER_PRICE_DATA aord
        using (
        select t.pk_member, t.place_dept, nvl(t.sum_pv - nvl(tt.sum_pv, 0), 0) sum_pv
        from CU_MEMBER_SETTLE_D${endPeriod} t
        left join CU_MEMBER_SETTLE_D${startPeriod} tt
        on tt.pk_member = t.pk_member
        and tt.place_dept = t.place_dept
        inner join cu_member cm
        on cm.pk_id = t.pk_member
        and cm.pk_settle_country = #{pkCountry}
        and cm.del_flag = 0
        and cm.category = 0
        and cm.pay_status = 1
        ) tmp
        on (1 = 0)
        <!--
        on (aord.PK_BASE_CONFIG = #{pkBaseConfig} and tmp.pk_member = aord.pk_member and tmp.place_dept = aord.place_dept)
        WHEN MATCHED THEN
            update set aord.sum_pv = tmp.sum_pv, aord.modified_time = sysdate
            -->
        WHEN NOT MATCHED THEN
        insert
        (PK_ID,
        PK_BASE_CONFIG,
        PK_MEMBER,
        PLACE_DEPT,
        SUM_PV,
        PK_COUNTRY,
        PK_CREATOR)
        values (AC_OTHER_PRICE_DATA_SEQ.Nextval,
        #{pkBaseConfig},
        tmp.pk_member,
        tmp.place_dept,
        tmp.sum_pv,
        #{pkCountry},
        100000000)
    </update>

    <!-- 删除7天前的历史数据 -->
    <delete id="clearOldData">
        delete
        from AC_OTHER_PRICE_DATA aopd
        where aopd.del_flag = 1
          and sysdate - 7 >= aopd.creation_time
    </delete>

    <!--    旅游奖数据查询-->
    <select id="tourismList" parameterType="com.hzs.activity.base.vo.TourismActivitVO"
            resultType="com.hzs.activity.base.vo.TourismActivitVO">
        SELECT
        ac.ACT_NAME actName,
        acd.PK_MEMBER pkMember,
        acd.PK_BASE_CONFIG pkBaseConfig,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        aw.PK_TRANSACTION AS pkAwards,
        bg.PK_TRANSACTION AS pkSettleGrade,
        ac.ACT_START_DATE actStartDate,
        ac.ACT_END_DATE actEndDate,
        ac.CREATION_TIME creationTime,
        aop.parent_pv order_achieve,
        aop.STATUS status,
        bc.SHORT_NAME shortName
        FROM
        AC_OTHER_PRICE_DATA acd
        LEFT JOIN AC_BASE_CONFIG ac ON ac.PK_ID = acd.PK_BASE_CONFIG
        LEFT JOIN CU_MEMBER cm ON cm.pk_id = acd.PK_MEMBER and cm.PK_SETTLE_COUNTRY=acd.PK_COUNTRY
        INNER JOIN BD_AWARDS aw ON aw.pk_ID = cm.PK_AWARDS
        INNER JOIN BD_GRADE bg ON bg.pk_id = cm.PK_SETTLE_GRADE
        left join AC_OTHER_PRICE aop on aop.PK_MEMBER=cm.pk_id and aop.PK_BASE_CONFIG=acd.PK_BASE_CONFIG and aop.DEL_FLAG=0
        left join BD_COUNTRY bc on bc.PK_ID=cm.PK_SETTLE_COUNTRY
        where acd.DEL_FLAG=0 and ac.DEL_FLAG=0 and cm.DEL_FLAG=0
        and cm.pay_status=1 and cm.category =0
        and aw.DEL_FLAG=0 and bg.DEL_FLAG=0
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>

        <if test="actName != null and actName !='' ">
            AND ac.ACT_NAME like #{actName} || '%'
        </if>
        <if test="pkCountry != null ">
            and acd.PK_COUNTRY=#{pkCountry}
            and ac.PK_COUNTRY=#{pkCountry}
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="actType != null ">
            AND ac.ACT_TYPE = #{actType}
        </if>
        <if test="status != null ">
            AND aop.STATUS = #{status}
        </if>

        <if test="actsStartDate != null and actsStartDate!='' ">
            and ac.ACT_START_DATE &gt;= to_date(#{actsStartDate}, 'yyyy-mm-dd')
        </if>
        <if test="actsEndDate != null and actsEndDate!='' ">
            and ac.ACT_START_DATE &lt;= to_date(#{actsEndDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="acteStartDate != null and acteStartDate!='' ">
            and ac.ACT_END_DATE &gt;= to_date(#{acteStartDate}, 'yyyy-mm-dd')
        </if>
        <if test="acteEndDate != null and acteEndDate!='' ">
            and ac.ACT_END_DATE &lt;= to_date(#{acteEndDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>

        <if test="startDate != null and startDate!='' ">
            and ac.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate!='' ">
            and ac.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        GROUP BY
        ac.ACT_NAME ,
        acd.PK_MEMBER ,
        cm.MEMBER_CODE ,
        cm.MEMBER_NAME ,
        acd.PK_BASE_CONFIG,
        aw.PK_TRANSACTION ,
        bg.PK_TRANSACTION,
        ac.ACT_START_DATE,
        ac.ACT_END_DATE,
        ac.CREATION_TIME,
        aop.parent_pv,
        aop.STATUS,
        bc.SHORT_NAME
        ORDER BY ac.CREATION_TIME DESC
    </select>

    <!--查询部门业绩-->
    <select id="tourismDeptList" parameterType="com.hzs.activity.base.vo.DeptMemberVO"
            resultType="com.hzs.activity.base.vo.DeptMemberVO">
        select acd.PK_MEMBER pkMember,
        acd.place_dept placeDept,
        acd.PK_BASE_CONFIG pkBaseConfig,
        sum(SUM_PV) sumPv
        from AC_OTHER_PRICE_DATA acd
        INNER JOIN AC_BASE_CONFIG ac ON ac.PK_ID = acd.PK_BASE_CONFIG
        where acd.del_flag = 0
        <if test="pkCountry != null ">
            and acd.PK_COUNTRY = #{pkCountry}
        </if>
        AND ac.ACT_TYPE = #{actType}
        GROUP BY acd.PK_MEMBER, acd.PLACE_DEPT, acd.PK_BASE_CONFIG
    </select>

    <!-- 查询会员活动数据 -->
    <select id="queryMemberOtherData" resultMap="BaseResultMap">
        select aopd.*
        from AC_OTHER_PRICE_DATA aopd
        where aopd.del_flag = 0
          and aopd.pk_base_config = #{pkBaseConfig}
          and aopd.pk_member = #{pkMember}
        order by aopd.place_dept
    </select>

    <!-- 查询会员直推业绩 -->
    <select id="queryOrderAchieve" resultType="decimal">
        select sum(so.order_achieve)
        from sa_order so
        where so.del_flag = 0
        and so.pk_reference = #{pkMember}
        and so.order_type in (1, 2)
        and so.order_status = 1
        <if test="startDate != null">
            and so.pay_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and #{endDate} >= so.pay_time
        </if>
    </select>

</mapper>
