package org.springblade.modules.finance.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.modules.api.request.SubmitOrderRequest;
import org.springblade.modules.api.service.SubmitOrderService;
import org.springblade.modules.business.entity.FacilityCargoWay;
import org.springblade.modules.business.entity.Shop;
import org.springblade.modules.business.service.IFacilityCargoWayService;
import org.springblade.modules.business.service.IShopService;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderGoods;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.vo.MerchantPageOrderVO;
import org.springblade.modules.finance.vo.OrderGoodsVO;
import org.springblade.modules.finance.mapper.OrderGoodsMapper;
import org.springblade.modules.finance.service.IOrderGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 商品订单表 服务实现类
 *
 * @author BladeX
 * @since 2024-07-17
 */
@AllArgsConstructor
@Service("orderGoodsServiceImpl")
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService, SubmitOrderService {

	private final IOrderService iOrderService;
	private final IGoodsInfoService iGoodsInfoService;
	private final IFacilityCargoWayService iFacilityCargoWayService;
	private final IShopService iShopService;
	private final IUserService iUserService;

	@Override
	public IPage<OrderGoodsVO> selectOrderGoodsPage(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
		return page.setRecords(baseMapper.selectOrderGoodsPage(page, orderGoods));
	}

	@Override
	public IPage<OrderGoodsVO> selectUserOrder(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
		return page.setRecords(baseMapper.selectUserOrder(page, orderGoods));
	}

	@Override
	public IPage<MerchantPageOrderVO> getMerchantOrder(IPage page, MerchantPageOrderVO merchantPageOrderVO){
		return page.setRecords(baseMapper.getMerchantOrder(page, merchantPageOrderVO));
	}

	@Transactional
	@Override
	public String submitOrder(SubmitOrderRequest request) {
		GoodsInfo goods = iGoodsInfoService.getById(request.getGoodsInfoId());
		Assert.notNull(goods, "商品信息不存在");
		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(request.getCargoWayId());
		Assert.notNull(cargoWay, "货道信息不存在");
		Shop shop = iShopService.getById(request.getShopId());
		Assert.notNull(shop, "门店信息不存在");
		User user = iUserService.getById(request.getUserId());

		// 订单初始化
		Order order = iOrderService.init();
		order.setOrderType(request.getOrderType());
		order.setOrderType(request.getOrderType());
		order.setSource(request.getOrderSource());
		order.setUserId(request.getUserId());
		// 订单总金额
		order.setTotalAmount(cargoWay.getSalePrice().multiply(BigDecimal.valueOf(request.getGoodsNum())));
		// 实付金额
		BigDecimal payAmount = cargoWay.getSalePrice().multiply(BigDecimal.valueOf(cargoWay.getDiscount())).multiply(BigDecimal.valueOf(request.getGoodsNum()));
		order.setPaymentAmount(payAmount);
		order.setDiscountAmount(order.getTotalAmount().subtract(order.getPaymentAmount()));
		order.setConsignee(user.getName());
		order.setConsigneePhoneNumber(user.getPhone());
		iOrderService.save(order);

		OrderGoods orderGoods = new OrderGoods();
		orderGoods.setOrderId(order.getId());
		orderGoods.setGoodsNum(request.getGoodsNum());
		orderGoods.setDeviceId(request.getDeviceId());
		orderGoods.setGoodsName(goods.getGoodsName());
		orderGoods.setCargoWayId(request.getCargoWayId());
		orderGoods.setCargoWayNo(request.getCargoWayNo());
		orderGoods.setGoodsNo(goods.getGoodsNo());
		orderGoods.setGoodsImage(goods.getGoodsCover());
		orderGoods.setGoodsId(request.getGoodsInfoId());
		orderGoods.setGoodsPrice(cargoWay.getSalePrice());
		orderGoods.setMerchantId(shop.getMerchantId());
		orderGoods.setShopId(request.getShopId());
		orderGoods.setUserId(request.getUserId());
		orderGoods.setStoreName(shop.getStoreName());
		baseMapper.insert(orderGoods);

		// 扣减货道库存
		cargoWay.setGoodsStore(cargoWay.getGoodsStore() - request.getGoodsNum());
		iFacilityCargoWayService.updateById(cargoWay);
		return order.getOrderNo();
	}


	@Override
	public void releaseGoodsStock(Order order) {
		OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
		// 更新货道库存
		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
		cargoWay.setGoodsStore(cargoWay.getGoodsStore() + orderGoods.getGoodsNum());
		iFacilityCargoWayService.updateById(cargoWay);
	}

	@Override
	public void paySuccessAfter(Order order) {
		OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
		// 更新货道销售信息
		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
		cargoWay.setTotalTrade(cargoWay.getTotalTrade() + orderGoods.getGoodsNum());
		cargoWay.setTotalAmount(cargoWay.getTotalAmount().add(order.getPaymentAmount()));
		iFacilityCargoWayService.updateById(cargoWay);
	}
}
