package com.fzm.mall.server.front.goods.mapper;

import com.fzm.mall.server.front.goods.model.MarketingPromotion;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.mall.server.front.goods.model.MarketingPromotionRule;
import com.fzm.mall.server.front.goods.model.vo.PromotionVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品促销 Mapper 接口
 * </p>
 *
 * @author fzm
 * @since 2021-03-09
 */
public interface MarketingPromotionMapper extends BaseMapper<MarketingPromotion> {
    @Select("select * from marketing_promotion where status = 1 and range_type = 1 and start_time<=#{time} and end_time>=#{time} and scope in(1,#{type}) and merchant_id = #{merchantId} " +
            "union all select * from marketing_promotion where status = 1 and range_type = 2 and start_time<=#{time} and end_time>=#{time} and merchant_id = #{merchantId} and scope in(1,#{type}) and promotion_id in (select coupon_id from marketing_coupon_category where category_id in (select category_id from goods_category_spu where goods_id = #{goodsId})) " +
            "union all select * from marketing_promotion where status = 1 and range_type = 3  and start_time<=#{time} and end_time>=#{time} and scope in(1,#{type}) and promotion_id in (select promotion_id from marketing_promotion_sku where sku_id in (select sku_id from goods_sku where goods_id = #{goodsId} and status = 1));")
    PromotionVo getByGoodsId(@Param("goodsId") String goodsId, @Param("type") Integer type, @Param("time") Long time,@Param("merchantId") String merchantId);

    @Select("select * from marketing_promotion_rule where promotion_id = #{promotionId} and status = 1 order by condition_val desc")
    List<MarketingPromotionRule> getRule(@Param("promotionId") String promotionId);

    @Select("select b.*,a.sku_id from marketing_promotion_sku a left join marketing_promotion b on a.promotion_id = b.promotion_id where a.sku_id = #{skuId}   and b.start_time <= #{time} and b.end_time >= #{time}")
    PromotionVo getBySkuId(@Param("skuId") String skuId, @Param("time") Long time);

    @Select("select a.* from marketing_promotion a left join marketing_promotion_category b on a.promotion_id = b.promotion_id where b.category_id = #{categoryId}   and a.start_time <= #{time} and a.end_time >= #{time}")
    PromotionVo getByCategoryId(@Param("categoryId") String categoryId, @Param("time") Long time);

    @Select("select * from marketing_promotion  where  merchant_id = #{merchantId} and `status`=1 and start_time <= #{time} and end_time >= #{time}")
    PromotionVo getByMerchantId(@Param("merchantId") String merchantId, @Param("time") Long time);

    @Select("select * from marketing_promotion where status = 1 and range_type = 1 and start_time<=#{time} and end_time>=#{time} and scope in(1,#{type}) and merchant_id = #{merchantId} " +
            "union all select * from marketing_promotion where status = 1 and range_type = 2 and start_time<=#{time} and end_time>=#{time} and merchant_id = #{merchantId} and scope in(1,#{type}) and promotion_id in (select coupon_id from marketing_coupon_category where category_id in (select category_id from goods_category_spu where goods_id = #{goodsId})) " +
            "union all select * from marketing_promotion where status = 1 and range_type = 3  and start_time<=#{time} and end_time>=#{time} and scope in(1,#{type}) and promotion_id in (select promotion_id from marketing_promotion_sku where sku_id = #{skuId}); ")
    PromotionVo getBySkuIdAndType(@Param("skuId") String skuId, @Param("time") Long time, @Param("type") Integer type,@Param("merchantId")String merchantId,@Param("goodsId")String goodsId);
}
