package com.ruoyi.ss.transactionBill.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.iot.service.IotService;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.payBill.domain.PayBillVO;
import com.ruoyi.ss.payBill.interfaces.AfterPay;
import com.ruoyi.ss.transactionBill.domain.TransactionBill;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillDeviceRechargeStatus;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;

/**
 * @author wjh
 * 2024/8/14
 */
@Service
@Slf4j
public class RechargeDepositAfterPay implements AfterPay {

    @Autowired
    private TransactionBillService transactionBillService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private IotService iotService;

    @Override
    public int onPaySuccess(PayBillVO payBill) {
        TransactionBillVO bill = transactionBillService.selectSmTransactionBillByBillId(payBill.getBstId());
        ServiceUtil.assertion(bill == null, "订单不存在");
        ServiceUtil.assertion(!TransactionBillStatus.UNPAID_DEPOSIT.getStatus().equals(bill.getStatus()), "订单状态非待支付押金");

        deviceService.pullDeviceInfo(bill.getDeviceId());

        DeviceVO device = deviceService.selectById(bill.getDeviceId());
        ServiceUtil.assertion(device == null, "设备不存在");

        Integer result = transactionTemplate.execute(status -> {
            // 修改订单信息
            TransactionBill data = new TransactionBill();
            data.setSuitStartTime(LocalDateTime.now());
            data.setSuitStartEle(device.getTotalElectriQuantity());
            data.setStatus(TransactionBillStatus.SUCCESS_DEPOSIT.getStatus());
            data.setDepositPayId(payBill.getPayId());
            data.setPayTime(DateUtils.toDate(payBill.getPayTime()));
            data.setDeviceRechargeStatus(TransactionBillDeviceRechargeStatus.SUCCESS.getStatus());
            data.setOpenMsg("成功");
            TransactionBillQuery query = new TransactionBillQuery();
            query.setBillId(bill.getBillId());
            query.setStatus(TransactionBillStatus.UNPAID_DEPOSIT.getStatus());
            int update = transactionBillService.updateByQuery(data, query);
            ServiceUtil.assertion(update != 1, "修改订单信息失败，状态已经发生改变：%s", bill.getBillNo());

            // 处理商户获取手机号扣款
            int received = transactionBillService.receiveMchShowMobileAmount(bill);
            ServiceUtil.assertion(received != 1, "商户获取用户手机号扣款失败");

            // 开启设备
            this.openDevice(bill);

            return update;
        });

        return result == null ? 0 : result;
    }

    /**
     * 订单开启设备
     */
    private int openDevice(TransactionBillVO bill) {
        try {
            iotService.open(bill, "分时段订单" + bill.getBillNo() + "开启");
            return 1;
        } catch (Exception e) {
            log.error(e.getMessage());
            TransactionBill data = new TransactionBill();
            data.setDeviceRechargeStatus(TransactionBillDeviceRechargeStatus.FAIL.getStatus());
            data.setOpenMsg(e.getMessage());
            data.setBillId(bill.getBillId());
            return transactionBillService.updateSmTransactionBill(data);
        }
    }
}
