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.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderDetail;
import org.springblade.modules.finance.entity.OrderGoods;
import org.springblade.modules.finance.service.IOrderDetailService;
import org.springblade.modules.finance.service.IOrderGoodsService;
import org.springblade.modules.finance.service.IOrderService;
import org.springblade.modules.finance.service.impl.OrderTimeoutService;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.entity.GoodsSpec;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springblade.modules.mall.service.IGoodsSpecService;
import org.springblade.modules.platform.entity.UserAddress;
import org.springblade.modules.platform.service.IUserAddressService;
import org.springblade.modules.platform.service.IUserAppService;
import org.springblade.modules.system.entity.UserApp;
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("freeWineSubmitOrderImpl")
public class FreeWineSubmitOrderServiceImpl implements SubmitOrderService {

    @Resource
    private IOrderService orderService;
    @Resource
    private IGoodsInfoService goodsInfoService;
    @Resource
    private IGoodsSpecService goodsSpecService;
    @Resource
    private IUserAddressService userAddressService;
    @Resource
    private IOrderDetailService orderDetailService;
    @Resource
    private IOrderGoodsService orderGoodsService;
    @Resource
    private OrderTimeoutService orderTimeoutService;
    @Resource
    private IUserAppService userAppService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String submitOrder(SubmitOrderRequest request) {
        Assert.notNull(request.getGoodsInfoId(), "goodsInfoId 不能为空");
        // 查询商品信息，并校验
        GoodsInfo goodsInfo = goodsInfoService.getById(request.getGoodsInfoId());
        this.checkGoodsState(goodsInfo);
        // 查询商品规格信息，并校验
        GoodsSpec goodsSpec = goodsSpecService.getById(request.getGoodsSpecId());
        this.checkGoodsSpecState(goodsSpec);
        // 查询用户地址信息
        UserAddress userAddress = userAddressService.getById(request.getUserAddressId());
        Assert.notNull(userAddress, "用户地址信息不存在");

        // 初始化订单数据
        Order order = orderService.init();
        order.setOrderType(OrderTypeEnum.FREE_WINE.getValue());
        order.setQuantity(1);// 一人领一份
        order.setDistributeMode(request.getDistributeMode());
        order.setConsignee(userAddress.getRealName());
        order.setConsigneePhoneNumber(userAddress.getPhone());
        order.setImage(goodsSpec.getCover());
        order.setTotalAmount(BigDecimal.valueOf(0));// 免费
        order.setDiscountAmount(BigDecimal.valueOf(0));// 优惠金额
        order.setPaymentAmount(BigDecimal.valueOf(0));// 免费
        // 邮费
        order.setPostage(new BigDecimal(ParamCache.getValueDefault(CommonConstant.FREE_WINE_GOODS_WL_PRICE, "28")));
        orderService.save(order);

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

        // 保存订单商品关联信息表
        goodsSpec.setSalePrice(BigDecimal.valueOf(0));
        OrderGoods orderGoods = this.buildOrderGoods(goodsInfo, goodsSpec, request.getGoodsNum(), order.getOrderNo());
        orderGoodsService.save(orderGoods);

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

        return order.getOrderNo();
    }

    @Override
    public void paySuccessAfter(Order order) {
        // 更新用户领取酒的状态
        UserApp userApp = userAppService.getByUserId(order.getCreateUserId());
        if (userApp != null) {
            userApp.setFreeWine(true);
            userAppService.updateById(userApp);
        }
    }
}
