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

import lombok.extern.slf4j.Slf4j;
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.Trademark;
import org.springblade.modules.custom.service.ITrademarkAuthService;
import org.springblade.modules.custom.service.ITrademarkService;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderTrademark;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.service.IOrderTrademarkService;
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/6/3
 * Description
 */
@Slf4j
@Service("trademarkAuthOrderServiceImpl")
public class TrademarkAuthOrderServiceImpl implements SubmitOrderService {

    @Resource
    private IOrderService orderService;
    @Resource
    private ITrademarkAuthService trademarkAuthService;
    @Resource
    private ITrademarkService trademarkService;
    @Resource
    private IOrderTrademarkService orderTrademarkService;
    @Resource
    private IUserAddressService userAddressService;
    @Resource
    private OrderTimeoutService orderTimeoutService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String submitOrder(SubmitOrderRequest request) {
        Assert.notNull(request.getTrademarkId(), "trademarkId 不能为空");
        Trademark trademark = trademarkService.getById(request.getTrademarkId());
        Assert.notNull(trademark, "商标信息不存在");
        // 查询用户地址信息
        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(trademark.getLogo());
        // 总价 样酒价格乘以数量
        order.setTotalAmount(trademark.getAuthFee().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);

        // 保存商标订单关联信息
        OrderTrademark orderTrademark = new OrderTrademark();
        orderTrademark.setOrderId(order.getId());
        orderTrademark.setTrademarkId(trademark.getId());
        orderTrademark.setUserId(request.getUserId());
        orderTrademark.setAuthDays(trademark.getAuthDays());
        orderTrademarkService.save(orderTrademark);

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

    @Override
    public void paySuccessAfter(Order order) {
        OrderTrademark trademark = orderTrademarkService.lambdaQuery().eq(OrderTrademark::getOrderId, order.getId()).one();
        if (trademark != null) {
            trademarkAuthService.auth(trademark);
        } else {
            log.error("授权失败，授权订单数据不存在");
        }
    }
}
