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

import org.springblade.common.base.BaseEntity;
import org.springblade.common.enums.OrderTypeEnum;
import org.springblade.common.enums.TransactionStatusEnum;
import org.springblade.common.utils.CommonUtil;
import org.springblade.modules.api.request.CartSubmitOrderRequest;
import org.springblade.modules.api.request.SubmitOrderRequest;
import org.springblade.modules.api.service.SubmitOrderService;
import org.springblade.modules.custom.entity.BaseWine;
import org.springblade.modules.custom.service.IBaseWineService;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderDetail;
import org.springblade.modules.finance.entity.OrderSampleWine;
import org.springblade.modules.finance.entity.TransactionFlow;
import org.springblade.modules.finance.service.IOrderDetailService;
import org.springblade.modules.finance.service.IOrderSampleWineService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.service.ITransactionFlowService;
import org.springblade.modules.finance.service.impl.OrderTimeoutService;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.platform.entity.UserAddress;
import org.springblade.modules.platform.service.IUserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Author pangqijun
 * Date 2023/7/20
 * Description
 */
@Service
public class CartSampleWineOrderImpl implements SubmitOrderService {

    @Resource
    private IOrderService orderService;
    @Resource
    private IUserAddressService userAddressService;
    @Resource
    private IOrderDetailService orderDetailService;
    @Resource
    private OrderTimeoutService orderTimeoutService;
    @Resource
    private IBaseWineService baseWineService;
    @Resource
    private IOrderSampleWineService orderSampleWineService;
    @Resource
    private ITransactionFlowService transactionFlowService;

    @Override
    public String submitOrder(SubmitOrderRequest request) {
        return null;
    }

    @Override
    public String cartSubmitOrder(CartSubmitOrderRequest cartSubmitOrderRequest) {
        // 交易流水号
        String transactionNo = CommonUtil.genTimeID();
        TransactionFlow flow = new TransactionFlow();
        flow.setStatus(TransactionStatusEnum.NOT_PAY.getValue());// 未支付
        flow.setTransactionNo(transactionNo);
        // 用户地址
        UserAddress userAddress = userAddressService.getById(cartSubmitOrderRequest.getUserAddressId());
        Assert.notNull(userAddress, "用户地址不存在");

        List<Long> goodsIds = cartSubmitOrderRequest.getGoodsList().stream().map(SubmitOrderRequest::getGoodsInfoId).collect(Collectors.toList());
        List<BaseWine> list = baseWineService.lambdaQuery().in(BaseEntity::getId, goodsIds).list();
        Map<Long, BaseWine> goodsInfoMap = list.stream().collect(Collectors.toMap(BaseWine::getId, Function.identity()));

        // 商品总金额
        BigDecimal goodsAmount = new BigDecimal(0);

        for (SubmitOrderRequest request : cartSubmitOrderRequest.getGoodsList()) {
            BaseWine baseWine = goodsInfoMap.get(request.getGoodsInfoId());
            // 初始化订单数据
            Order order = orderService.init();
            order.setTransactionNo(transactionNo);
            order.setOrderType(OrderTypeEnum.GOODS.getValue());
            order.setQuantity(request.getGoodsNum());
            order.setDistributeMode(request.getDistributeMode());
            order.setConsignee(userAddress.getRealName());
            order.setConsigneePhoneNumber(userAddress.getPhone());
            order.setImage(baseWine.getImage());
            order.setTotalAmount(baseWine.getForeignPrice().multiply(BigDecimal.valueOf(request.getGoodsNum())));
            order.setDiscountAmount(BigDecimal.valueOf(0));// 优惠金额
            order.setPaymentAmount(order.getTotalAmount().subtract(order.getDiscountAmount()));
            // 邮费
            order.setPostage(BigDecimal.valueOf(0)); //包邮
            goodsAmount = goodsAmount.add(order.getTotalAmount());


            orderService.save(order);

            //订单详情实体
            OrderDetail orderDetail = this.buildOrderDetail(request.getRemark(), userAddress);
            orderDetail.setOrderId(order.getId());
            orderDetailService.save(orderDetail);

            // 保存样酒订单关联信息
            OrderSampleWine orderSampleWine = new OrderSampleWine();
            orderSampleWine.setBaseWineId(baseWine.getId());
            orderSampleWine.setForeignCapacity(baseWine.getForeignCapacity());
            orderSampleWine.setQuantity(request.getGoodsNum());
            orderSampleWine.setOrderId(order.getId());
            orderSampleWineService.save(orderSampleWine);
        }

        // 交易总金额
        flow.setAmount(goodsAmount);
        transactionFlowService.save(flow);

        // 添加订单到延时队列，处理订单支付超时问题
        orderTimeoutService.addTask(null, flow.getId());

        return transactionNo;
    }
}
