package com.cyksj.mapper.manage.coupon;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyksj.model.entity.Coupon;
import com.cyksj.model.entity.CouponDistributePopularize;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/*
 *项目名: netflix
 *文件名: CouponDistributePopularizeMapper
 *创建者: JavaZou
 *创建时间:2022/11/17 18:07
 */
public interface CouponDistributePopularizeMapper extends BaseMapper<CouponDistributePopularize> {
	@Update("update coupon_distribute_popularize set deleted = false where coupon_id = #{id}")
	void deleteByCouponId(Long id);

	@Select("select distinct c.* from (select * from coupon_distribute_popularize where distribute_id = #{distributeId} and deleted is true) cp inner join coupon c on c.id = cp.coupon_id and type = 'discount' inner join coupon_sku cs on cs.coupon_id = c.id and cs.sku_id = #{skuId} order by c.discount desc limit 1")
	Coupon selectDiscountCouponByUserId(@Param("distributeId") Long distributeId, @Param("skuId") Long skuId);

	void batchInsert(@Param("list") List<CouponDistributePopularize> couponRelates);

	String getCouponCodeByUid(@Param("userId") Long userId, @Param("couponId") Long couponId);
}
