package com.cyksj.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.order.ShoppingCartDonMapper;
import com.cyksj.model.entity.ShoppingCartDon;
import com.cyksj.model.request.OrderPayRequest;
import com.cyksj.model.request.UnifiedOrderPayReq;
import com.cyksj.model.views.GoodsDonSkuDetailView;
import com.cyksj.model.views.ShoppingCartDonPayDetailView;
import com.cyksj.service.order.OrderDonService;
import com.cyksj.service.order.ShoppingCartDonService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目名: yhlxj
 * 文件名: ShoppingCartOrderServiceImpl
 * 创建者: JavaZou
 * 创建时间:2023/10/12 16:01
 */
@Service
@RequiredArgsConstructor
public class ShoppingCartOrderServiceImpl extends ServiceImpl<ShoppingCartDonMapper, ShoppingCartDon> implements ShoppingCartDonService {
	private final BeanSearcher beanSearcher;

	private final OrderDonService orderDonService;
	@Override
	public void addGoodsShoppingCart(ShoppingCartDon goodsCartDon) {
		//最多添加购物车数量30
		int count = this.count(Wrappers.lambdaQuery(ShoppingCartDon.class)
				.eq(ShoppingCartDon::getUserId, goodsCartDon.getUserId()));
		if (count >= 30) {
			throw BusinessRuntimeException.getInstance("最多添加30件商品至购物车");
		}
		Long skuId = goodsCartDon.getSkuId();
		if (skuId == null) {
			throw BusinessRuntimeException.getInstance("请指定商品添加至购物车");
		}
		GoodsDonSkuDetailView goodsDonSkuDetailView = beanSearcher.searchFirst(GoodsDonSkuDetailView.class, MapUtils.builder()
				.field(GoodsDonSkuDetailView::getSkuId, skuId).build());
		if (goodsDonSkuDetailView == null) {
			throw BusinessRuntimeException.getInstance("该商品已不存在..");
		}
		if (!goodsDonSkuDetailView.getGoodsStatus()) {
			throw BusinessRuntimeException.getInstance("该商品已下架..");
		}
		if (!goodsDonSkuDetailView.getGoodsSkuStatus()) {
			throw BusinessRuntimeException.getInstance("该商品规格已下架..");
		}
		if (goodsCartDon.getNum() == null) {
			goodsCartDon.setNum(1);
		}
		goodsCartDon.setGoodsId(goodsDonSkuDetailView.getGoodsId());
		goodsCartDon.setSkuId(skuId);
		this.save(goodsCartDon);
	}

	@Override
	public ShoppingCartDonPayDetailView unifiedPayShoppingCartGoods(UnifiedOrderPayReq unifiedOrderPayReq) {
		Long userId = unifiedOrderPayReq.getUserId();
		//处理订单
		List<OrderPayRequest> payGoods = unifiedOrderPayReq.getPayGoods();
		if (CollUtil.isEmpty(payGoods)) {
			throw BusinessRuntimeException.getInstance("请选择商品后再进行下单..");
		}
		ShoppingCartDonPayDetailView payDetailView = new ShoppingCartDonPayDetailView();
//		String orderNo = ;
//		String payTitle = ;
//		orderDonService.submit();
		return null;
	}
}
