package com.ruoyi.bst.order.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.device.domain.enums.DeviceUnLockType;
import com.ruoyi.bst.device.domain.vo.DeviceIotVO;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.bst.order.domain.Order;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.OrderVO;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.order.mapper.OrderMapper;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.bst.orderDevice.domain.OrderDeviceVO;
import com.ruoyi.bst.orderDevice.service.OrderDeviceService;
import com.ruoyi.bst.pay.domain.PayVO;
import com.ruoyi.bst.pay.domain.enums.PayBstType;
import com.ruoyi.bst.pay.interfaces.PayHandler;
import com.ruoyi.bst.refund.domain.RefundVO;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.system.operLog.service.OperLogService;

@Service
public class OrderPayHandlerImpl implements PayHandler {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private OrderDeviceService orderDeviceService;

    @Autowired
    private DeviceIotService deviceIotService;

    @Autowired
    private OperLogService operLogService;

    @Autowired
    private DeviceService deviceService;

    // 支付成功后操作
    @Override
    public boolean onPaySuccess(PayVO pay) {
        ServiceUtil.assertion(pay == null, "支付信息不能为空");
        ServiceUtil.assertion(pay.getBstId() == null, "订单ID不能为空");
        ServiceUtil.assertion(!PayBstType.ORDER.getType().equals(pay.getBstType()), "订单支付类型错误");

        // 查询订单
        OrderVO order = orderService.selectOrderById(pay.getBstId());
        ServiceUtil.assertion(order == null, "ID为%s的订单不存在", pay.getBstId());

        // 查询订单设备
        OrderDeviceVO orderDevice = orderDeviceService.selectOriginByOrderId(order.getId());
        ServiceUtil.assertion(orderDevice == null, "ID为%s的订单设备不存在", order.getId());

        LocalDateTime startTime = LocalDateTime.now();

        Integer result = transactionTemplate.execute(status -> {
            // 修改订单信息
            Order data = new Order();
            data.setStatus(OrderStatus.PROCESSING.getCode());
            data.setStartTime(startTime);
            data.setMaxTime(startTime.plusSeconds(order.getSuitSeconds()));  // 计算最大时间
            data.setPayId(pay.getId());
            OrderQuery query = new OrderQuery();
            query.setId(order.getId());
            query.setStatusList(OrderStatus.canPaySuccess());
            int rows = orderMapper.updateByQuery(data, query);
            ServiceUtil.assertion(rows != 1, "更新ID为%s的订单失败", order.getId());

            // 开始使用订单设备
            int start = orderDeviceService.start(orderDevice);
            ServiceUtil.assertion(start != 1, "开始使用ID为%s的订单设备失败", orderDevice.getId());

            return rows;
        });

        if (result != null && result == 1) {

            // 查询设备
            DeviceVO device = deviceService.selectDeviceById(orderDevice.getDeviceId());
            
            // 解锁设备
            DeviceIotVO unlock = deviceIotService.unlock(device, DeviceUnLockType.USER, "订单支付成功开锁：" + order.getNo(), false, 3);
            operLogService.operSysLog("订单支付成功开锁", unlock.isIotSuccess(), LogBizType.ORDER, order.getId(), orderDevice.getDeviceId(), orderDevice.getDeviceLongitude(), orderDevice.getDeviceLatitude());
            ServiceUtil.assertion(unlock.getDb() <= 0, "ID为%s的设备解锁失败", orderDevice.getDeviceId());
        }

        return result != null && result == 1;
    }

    // 支付单退款后操作，啥也不用做
    @Override
    public boolean onRefundSuccess(RefundVO refund) {
        return true;
    }
}
