package com.cyksj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.model.views.GoodsDonSkuView;
import org.apache.ibatis.annotations.Select;

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

/**
 * @author chan
 * @date 2022/9/27 5:31 PM
 */
public interface GoodsDonMapper extends BaseMapper<GoodsDon> {

	@Select("select gdk.price from goods_don gd left join goods_don_sku gdk on gdk.goods_id = gd.id" +
			" where gd.id = #{goodsId} and gd.deleted is true" +
			" and gd.status is true and gd.type = 2" +
			" order by gdk.price limit 1")
	BigDecimal selectMinPriceSkuByGoodsId(Long goodsId);

	@Select("select distinct g.id,g.title from order_don o inner join (select user_id from user_distribute_shared where shared_id = #{cpsUserId}) us on us.user_id = o.user_id and o.is_fixed_distribute is true and o.relation_id != 0 and o.status not in ('close','noPayment')" +
			" inner join goods_don g on o.goods_id = g.id order by g.id")
	List<GoodsDon> getDistributeGoodsDetail(Long cpsUserId);

	@Select("select sku.id as skuId,sku.goods_id,sku.spec_val,g.title as goodsTitle from goods_don_sku sku inner join goods_don g on g.id = sku.goods_id" +
			" where g.deleted is true and type = #{type}")
	List<GoodsDonSkuView> selectGoodsDonSkuByGoodsType(Integer type);

	@Select("select g.id, g.title from (select distinct goods_id from order_don where status not in ('close','noPayment') and yhs_id = #{yhsId}) o inner join goods_don g on g.id = o.goods_id and g.type = 1")
	List<GoodsDon> getYhShopDistributeGoodsDetail(Long yhsId);

	@Select("select g.id, g.title from (select distinct o.goods_id from order_don o inner join user_mirror_shop_order_relate uor on uor.order_id = o.id where o.status not in ('close','noPayment') and uor.mp_id = #{yhsId}) o inner join goods_don g on g.id = o.goods_id")
	List<GoodsDon> getUserMirrorShopShopDistributeGoodsDetail(Long id);
}
