package cn.reghao.tnb.file.app.zorder.service;

import cn.reghao.file.api.iface.JobService;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.web.result.ResultStatus;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.api.dto.ItemSnapshot;
import cn.reghao.tnb.content.api.iface.MallService;
import cn.reghao.tnb.file.app.zorder.db.OrderMapper;
import cn.reghao.tnb.file.app.zorder.model.constant.OrderStatus;
import cn.reghao.tnb.file.app.zorder.model.po.Order;
import cn.reghao.tnb.file.app.zorder.model.vo.OrderDetail;
import cn.reghao.tnb.user.api.iface.UserWalletService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-04-16 11:00:24
 */
@Slf4j
@Service
public class OrderService {
    @DubboReference(check = false)
    private MallService mallService;
    @DubboReference(check = false)
    private UserWalletService userWalletService;

    private JobService jobService;
    private final OrderMapper orderMapper;

    public OrderService(JobService jobService, OrderMapper orderMapper) {
        this.jobService = jobService;
        this.orderMapper = orderMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result payOrder(long orderId) {
        Order order = orderMapper.findByOrderId(orderId);
        if (order != null && order.getStatus() == OrderStatus.toPay.getCode()) {
            long sellerId = mallService.getSellerId(order.getProductId());
            int amount = order.getAmount();
            long loginUser = UserContext.getUserId();
            double quantity = amount*order.getPrice();

            // TODO 支付订单和更新订单状态是一个分布式事务
            // 支付订单
            Result result = userWalletService.pay(loginUser, quantity, sellerId);
            if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
                // 更新订单状态
                int status = OrderStatus.toConfirm.getCode();
                orderMapper.updateOrderStatus(orderId, status);
            }

            return  result;
        }

        return Result.fail("订单支付失败");
    }

    public void deliveryOrder(long orderId) {
        int status = OrderStatus.toConfirm.getCode();
        orderMapper.updateOrderStatus(orderId, status);
    }

    public void receiveOrder(long orderId) {
        int status = OrderStatus.completed.getCode();
        orderMapper.updateOrderStatus(orderId, status);
    }

    public List<OrderDetail> getOrders(int pageNumber) {
        long loginUser = UserContext.getUserId();
        int pageSize = 100;
        List<Order> list = orderMapper.findByOwner(pageSize, loginUser);
        return list.stream().map(order -> {
            long orderId = order.getOrderId();
            return getOrderDetail(orderId);
        }).collect(Collectors.toList());
    }

    public Order getOrder(long orderId) {
        return orderMapper.findByOrderId(orderId);
    }

    public OrderDetail getOrderDetail(long orderId) {
        ItemSnapshot itemSnapshot = mallService.getItemSnapshot(orderId);
        Order order = orderMapper.findByOrderId(orderId);
        return new OrderDetail(order, itemSnapshot);
    }
}
