<?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.benefits.mapper.UpgradeGiveConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.activity.benefits.AcUpgradeGiveRuleConfig">
        <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="MEMBER_POINT" property="memberPoint"/>
        <result column="GRADE" property="grade"/>
    </resultMap>

    <!--   查询赠送权益规则-->
    <select id="queryBenefitsRuleList" parameterType="com.hzs.activity.benefits.vo.UpgradeGiveConfigVO"
            resultType="com.hzs.activity.benefits.vo.UpgradeGiveConfigVO">
        select  a.*,b.PK_TRANSACTION as pkGradeKey
        from AC_UPGRADE_GIVE_RULE_CONFIG a
        INNER JOIN BD_GRADE b on a.GRADE=b.PK_ID
        WHERE
        a.DEL_FLAG = 0
        AND b.DEL_FLAG =0 AND a.PK_BASE_ID = #{pkBaseId}
        AND a.PK_COUNTRY = #{pkCountry}
        order by a.CREATION_TIME asc
    </select>

    <!-- 查询会员符合等级数量 -->
    <select id="queryMemberCount" resultType="int">
        select count(1)
        from (
        <!-- 查询符合条件会员（数量+等级），空单不算（会员对应订单至少有一单） -->
        select cm.pk_id
        from cu_member cm
        left join sa_order so
        on so.pk_member = cm.pk_id
        and so.order_type in (1, 2, 7, 24, 25)
        and so.del_flag = 0
        and so.pk_country = #{pkCountry}
        and so.order_status = 1
        and so.pay_time >= #{startDate}
        and #{endDate} >= so.pay_time
        where cm.del_flag = 0
        and cm.pay_status = 1
        and cm.pk_settle_country = #{pkCountry}
        and cm.category = 0
        and cm.pk_settle_grade in
        (select bg.pk_id
        from bd_grade bg
        where bg.del_flag = 0
        and bg.pk_country = #{pkCountry}
        and bg.grade_value >=
        (select bg.grade_value
        from bd_grade bg
        where bg.del_flag = 0
        and bg.pk_country = #{pkCountry}
        and bg.pk_id = #{grade}))
        and cm.phone = #{phone}
        and cm.member_name = #{memberName}
        group by cm.pk_id
        having count(so.pk_id) > 0)
    </select>

    <!--    查询权益赠送会员权益升级记录-->
    <select id="queryBenefitsRuleLogList" parameterType="com.hzs.activity.benefits.vo.AcGiftBenefitsRuleLogVO"
            resultType="com.hzs.activity.benefits.vo.AcGiftBenefitsRuleLogVO">
        select
        gbr.MEMBER_POINT memberPoint,
        gbr.LEGAL_RIGHT_NEW legalRightNew,
        gbr.LEGAL_RIGHT_OLD legalRightOld,
        bc.CREATION_TIME creationTime ,
        bc.ACT_NAME actName,
        bc.ACT_START_DATE actStartDate,
        bc.ACT_END_DATE actEndDate ,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        ba.PK_TRANSACTION pkAwards,
        bg.PK_TRANSACTION pkGrade
        from AC_GIFT_BENEFITS_RULE_LOG gbr
        left join AC_BASE_CONFIG bc on bc.PK_ID = gbr.PK_BASE_ID
        left join CU_MEMBER cm on cm.PK_ID = gbr.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
        where 1= 1 and cm.del_flag = 0 and cm.CATEGORY = 0 and gbr.del_flag = 0
        <if test="actName != null and actName != ''">
            and bc.act_name like #{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
    </select>

</mapper>
