<?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.atguigu.ssyx.activity.mapper.ActivityInfoMapper">

    <resultMap id="ActivityRuleMap" type="com.atguigu.ssyx.model.activity.ActivityRule" autoMapping="true"></resultMap>
    <resultMap id="CouponInfoMap" type="com.atguigu.ssyx.model.activity.CouponInfo" autoMapping="true"></resultMap>
    <resultMap id="ActivitySkuMap" type="com.atguigu.ssyx.model.activity.ActivitySku" autoMapping="true"></resultMap>

    <!-- //根据所有skuId列表获取参与活动-->
    <select id="selectCartActivity" resultMap="ActivitySkuMap">
        select
        info.id as activityId,
        sku.sku_id as skuId
        from activity_info info
        inner join activity_sku sku on info.id = sku.activity_id
        <where>
            and sku.sku_id in
            <foreach collection="skuIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
        and now() between info.start_time and info.end_time
    </select>

    <!--//2 根据skuId+分类id+userId查询优惠卷信息-->
    <select id="selectCouponInfoList" resultMap="CouponInfoMap">
        select info.id,
               info.coupon_type,
               info.coupon_name,
               info.amount,
               info.condition_amount,
               info.start_time,
               info.end_time,
               info.range_type,
               info.range_desc,
               info.publish_count,
               info.per_limit,
               info.use_count,
               info.receive_count,
               info.expire_time,
               info.publish_status,
               info.create_time,
               info.update_time,
               info.is_deleted,
               cuse.coupon_status
        from coupon_info info
                 left join coupon_range crange on info.id = crange.coupon_id
                 left join coupon_use cuse on info.id = cuse.coupon_id
            and cuse.user_id = #{userId}
        where (
                    info.range_type = 1
                or (info.range_type = 2 and crange.range_id = #{skuId})
                or (info.range_type = 3 and crange.range_id = #{categoryId})
            )
          and now() between info.start_time and info.end_time
        order by info.amount desc
    </select>

    <!--//根据skuId进行查询，查询sku对应活动里面规则列表-->
    <select id="findActivityRule" resultMap="ActivityRuleMap">
        select DISTINCT rule.id,
                        info.activity_type as activityType,
                        rule.activity_id,
                        rule.condition_amount,
                        rule.condition_num,
                        rule.benefit_amount,
                        rule.benefit_discount
        from activity_info info
                 inner join activity_sku sku on info.id = sku.activity_id
                 inner join activity_rule rule on info.id = rule.activity_id
        where sku.sku_id = #{skuId}
          and now() between info.start_time and info.end_time
        order by rule.condition_amount desc
    </select>

    <!--// 如果之前参加过，活动正在进行中，排除商品-->
    <select id="selectSkuIdListExist" resultType="Long">
        select sku.sku_id
        from activity_info info
        inner join activity_sku sku on info.id = sku.activity_id
        <where>
            <if test="skuIdList != null">
                and sku.sku_id in
                <foreach collection="skuIdList" item="item"
                         index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        and now() between info.start_time and info.end_time
    </select>
</mapper>
