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

import org.springblade.common.cache.ParamCache;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.OrderTypeEnum;
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.service.IOrderDetailService;
import org.springblade.modules.finance.service.IOrderSampleWineService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.service.impl.OrderTimeoutService;
import org.springblade.modules.platform.entity.UserAddress;
import org.springblade.modules.platform.service.IUserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Author pangqijun
 * Date 2023/5/29
 * Description 样酒下单实现类
 */
@Service("sampleWineSubmitOrderServiceImpl")
public class SampleWineSubmitOrderServiceImpl 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;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String submitOrder(SubmitOrderRequest request) {
        Assert.notNull(request.getGoodsInfoId(), "goodsInfoId 不能为空");
        BaseWine baseWine = baseWineService.getById(request.getGoodsInfoId());
        Assert.notNull(baseWine, "基酒信息不存在");
        // 查询用户地址信息
        UserAddress userAddress = userAddressService.getById(request.getUserAddressId());
        Assert.notNull(userAddress, "用户地址信息不存在");

        // 初始化订单数据
        Order order = orderService.init();
        order.setOrderType(OrderTypeEnum.SAMPLE_WINE.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(order.getQuantity())));
        order.setDiscountAmount(BigDecimal.valueOf(0));// 优惠金额
        order.setPaymentAmount(order.getTotalAmount().subtract(order.getDiscountAmount()));// 免费
        // 邮费
        order.setPostage(BigDecimal.valueOf(0)); //包邮
        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);

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