package com.cyksj.mapper.shop;

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

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zwhui
 * @date 2023/9/14 17:43
 */
public interface YhShopGoodsSkuMapper extends BaseMapper<YhShopGoodsSku> {

	@Select("select sku.*,ygs.price as yhShopSkuPrice from (select user_id from yh_shop where id = #{yhsId} limit 1) ys inner join yh_shop_goods_sku ygs on ygs.user_id = ys.user_id and ygs.sku_id = #{skuId} and ygs.deleted is true and ygs.status is true" +
			" inner join goods_don_sku sku on sku.id = ygs.sku_id limit 1")
	GoodsDonSku selectGoodsDonSkuBySkuIdAndYhsId(@Param("yhsId") Long yhsId, @Param("skuId") Long skuId);

	@Update("update yh_shop_goods_sku set status = 0 where sku_id = #{skuId} and price not between #{minPrice} and #{maxPrice}")
	Integer setBelowPriceShopSkuDown(@Param("skuId") Long skuId, @Param("minPrice") BigDecimal minPrice, @Param("maxPrice") BigDecimal maxPrice);

	@Select("select sku_id,price from yh_shop_goods_sku ys where yhs_id = #{yhsId} and goods_id = #{goodsId} and deleted is true and status is true and exists(select 1 from goods_don_sku sku where sku.id = ys.sku_id and sku.status is true) order by price limit 1")
	YhShopGoodsSku selectMinPriceSkuByYhsId(@Param("yhsId") Long yhsId, @Param("goodsId") Long goodsId);

	@Select("select * from yh_shop_goods_sku where sku_id = #{skuId} and price not between #{minPrice} and #{maxPrice} and status = 1")
	List<YhShopGoodsSku> getBelowPriceShopSku(@Param("skuId") Long skuId, @Param("minPrice") BigDecimal minPrice, @Param("maxPrice") BigDecimal maxPrice);
}
