<?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.recommend.mapper.AcRecomUpgradeRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.recommend.AcRecomUpgradeRecord">
        <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_RULE_ID" property="pkRuleId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="PK_RECOMMEND_GRADE" property="pkRecommendGrade"/>
        <result column="PK_ORDER_ID" property="pkOrderId"/>
        <result column="UPGRADE_STATUS" property="upGradeStatus"/>
    </resultMap>

    <select id="selectUpgradeRecordByOrderId" resultMap="BaseResultMap">
        select *
        from ac_recom_upgrade_record r
        where r.del_flag = 0
          and r.pk_order_id = #{pkOrderId}
          and r.creation_time >= #{startDate}
          and r.creation_time &lt; #{endDate} + 1
    </select>

    <select id="selectRecommendNumber" resultType="long">
        select r.pk_id
        from ac_recom_upgrade_record r
        where r.del_flag = 0
          and r.pk_country = #{pkCountry}
          and r.pk_member = #{pkMember}
          and r.pk_settle_grade = #{pkSettleGrade}
          and r.pk_recommend_grade >= #{pkRecommendGrade}
          and r.upgrade_status = #{upgradeStatus}
    </select>

    <!-- 判断会员是否达标并且查询对应订单 -->
    <select id="listRecomOrder" resultType="com.hzs.common.domain.sale.order.SaOrder">
        select t.* from (
        select so.*
        from sa_order so
        left join cu_member_level cml
        on cml.pk_member = so.pk_member
        and cml.pk_order = so.pk_id
        left join bd_grade bg
        on bg.pk_id = cml.new_level
        where so.del_flag = 0
        and so.order_status = 1
        and so.order_type in (1, 2)
        and so.pay_time >= #{startTime}
        and #{endTime} >= so.pay_time
        and so.pk_reference = #{pkMember}
        and bg.grade_value >= (select grade_value from bd_grade where pk_id = #{pkGrade})
        order by so.pay_time
        ) t
        where #{num} >= rownum
    </select>
      <select id="queryList" resultType="com.hzs.activity.recommend.vo.AcDirectPushUpgradeListVO">
        SELECT
        abc.ACT_NAME,
        cm.member_code,
        cm.member_name,
        BG.GRADE_NAME AS pkSettleGrade,
        BGG.GRADE_NAME AS pkRecommendGrade,
        abc.ACT_START_DATE,
        abc.ACT_END_DATE,
        arur.CREATION_TIME,
        arur.UPGRADE_TIME
        FROM
        AC_RECOM_UPGRADE_RECORD arur
        LEFT JOIN AC_BASE_CONFIG abc ON arur.PK_BASE_ID = abc.PK_ID
        LEFT JOIN CU_MEMBER cm ON arur.pk_member = cm.PK_ID
        LEFT JOIN BD_GRADE BG ON arur.PK_SETTLE_GRADE = BG.PK_ID
        LEFT JOIN BD_GRADE BGG ON arur.PK_RECOMMEND_GRADE = BGG.PK_ID
        WHERE arur.DEL_FLAG = 0
        <if test="actName != null and actName != ''">
            AND abc.ACT_NAME = #{actName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            AND cm.member_code = #{memberCode}
        </if>
        <if test="startActStartDate != null and startActStartDate != ''">
            AND abc.ACT_START_DATE >= TO_DATE(SUBSTR(#{startActStartDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="startActEndDate != null and startActEndDate != ''">
            AND abc.ACT_START_DATE &lt;= TO_DATE(SUBSTR(#{startActEndDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="endActStartDate != null and endActStartDate != ''">
            AND abc.ACT_END_DATE >= TO_DATE(SUBSTR(#{endActStartDate},1,10), 'YYYY-MM-DD')
        </if>
        <if test="endActEndDate != null and endActEndDate != ''">
            AND abc.ACT_END_DATE &lt;= TO_DATE(SUBSTR(#{endActEndDate},1,10), 'YYYY-MM-DD')
        </if>
    </select>
</mapper>
