package com.kym.service.miniapp.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.kym.common.cache.ConnectorStatusCache;
import com.kym.common.config.EnPlusConfig;
import com.kym.common.constant.ResponseEnum;
import com.kym.common.exception.BusinessException;
import com.kym.common.utils.CommUtil;
import com.kym.common.utils.OrderUtils;
import com.kym.entity.admin.ConnectorInfo;
import com.kym.entity.admin.EquipmentInfo;
import com.kym.entity.enplus.response.EnBusinessPolicy;
import com.kym.entity.miniapp.Account;
import com.kym.entity.miniapp.ChargeOrder;
import com.kym.entity.miniapp.OrderRechargeRights;
import com.kym.entity.miniapp.UserRechargeRights;
import com.kym.entity.miniapp.delay.DelayChargeOrder;
import com.kym.service.admin.ConnectorInfoService;
import com.kym.service.admin.EquipmentInfoService;
import com.kym.service.admin.EquipmentRelationService;
import com.kym.service.enplus.EnPlusService;
import com.kym.service.jobs.DelayService;
import com.kym.service.miniapp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author skyline
 * @description 充电
 * @date 2023-07-29 14:24
 */
@Service
@DS("db-miniapp")
public class ChargeServiceImpl implements ChargeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChargeServiceImpl.class);

    private final EquipmentRelationService equipmentRelationService;

    private final EquipmentInfoService equipmentInfoService;

    private final ConnectorInfoService connectorInfoService;

    private final ChargeOrderService chargeOrderService;

    private final OrderRechargeRightsService orderRechargeRightsService;
    private final UserRechargeRightsService userRechargeRightsService;

    private final AccountService accountService;

    private final EnPlusService enPlusService;

    private final EnPlusConfig enPlusConfig;

    private final DelayService<DelayChargeOrder> startDelayService;
    private final DelayService<DelayChargeOrder> stopDelayService;

    public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, EquipmentInfoService equipmentInfoService,
                             ConnectorInfoService connectorInfoService, ChargeOrderService chargeOrderService,
                             OrderRechargeRightsService orderRechargeRightsService, UserRechargeRightsService userRechargeRightsService,
                             AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig,
                             @Qualifier("StartChargeDelayJob") @Lazy DelayService<DelayChargeOrder> startDelayService,
                             @Qualifier("StopChargeDelayJob") @Lazy DelayService<DelayChargeOrder> stopDelayService) {
        this.equipmentRelationService = equipmentRelationService;
        this.equipmentInfoService = equipmentInfoService;
        this.connectorInfoService = connectorInfoService;
        this.chargeOrderService = chargeOrderService;
        this.orderRechargeRightsService = orderRechargeRightsService;
        this.userRechargeRightsService = userRechargeRightsService;
        this.accountService = accountService;
        this.enPlusService = enPlusService;
        this.enPlusConfig = enPlusConfig;
        this.startDelayService = startDelayService;
        this.stopDelayService = stopDelayService;
    }


    /**
     * 预约充电改立即充电
     *
     * @param connectorId
     * @return
     */
    @Override
    public Map<String, String> immediatelyCharge(String connectorId) {
        var userId = StpUtil.getLoginIdAsLong();
        cancelBooking();
        return queryStartCharge(userId, connectorId, null, false, null, null);
    }

    /**
     * 修改预约充电时间
     *
     * @param startChargeSeq
     * @param startTime
     * @return
     */
    @Override
    public void modifyBookingTime(String startChargeSeq, LocalDateTime startTime) {
        // 预约充电队列更新
        var chargeOrder = chargeOrderService.lambdaQuery().eq(ChargeOrder::getStartChargeSeq, startChargeSeq).eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中).one();
        if (chargeOrder == null) {
            throw new BusinessException("预约订单不存在");
        }
        chargeOrder.setStartTime(startTime);
        chargeOrder.setEndTime(null);
        chargeOrderService.lambdaUpdate().eq(ChargeOrder::getStartChargeSeq, startChargeSeq).set(ChargeOrder::getStartTime, startTime).set(ChargeOrder::getEndTime, null).update();
        // 删除队列中原来的数据
        startDelayService.removeFromDelayQueue(startChargeSeq);
        stopDelayService.removeFromDelayQueue(startChargeSeq);
        // 向队列中添加新的数据
        var delayChargeOrder = new DelayChargeOrder();
        BeanUtils.copyProperties(chargeOrder, delayChargeOrder);
        var success = startDelayService.addToDelayQueue(delayChargeOrder);
        if (!success) {
            throw new BusinessException("修改预约时间失败");
        }
    }


    /**
     * 取消预约
     */
    @Override
    public void cancelBooking() {
        var userId = StpUtil.getLoginIdAsLong();
        var chargeOrder = chargeOrderService.lambdaQuery()
                .eq(ChargeOrder::getUserId, userId)
                .eq(ChargeOrder::getIsBooking, ChargeOrder.IS_BOOKING_是)
                .eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中)
                .one();
        cancelBookByChargeOrder(chargeOrder);
    }

    /**
     * 取消预约
     */
    @Override
    @Async
    public void cancelBookingByConnector(String connectorId) {
        var chargeOrder = chargeOrderService.lambdaQuery()
                .eq(ChargeOrder::getConnectorId, connectorId)
                .eq(ChargeOrder::getIsBooking, ChargeOrder.IS_BOOKING_是)
                .eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中)
                .one();
        cancelBookByChargeOrder(chargeOrder);
    }

    /**
     * 取消预约订单
     *
     * @param chargeOrder 预约订单
     */
    public void cancelBookByChargeOrder(ChargeOrder chargeOrder) {
        if (chargeOrder != null) {
            // 清除启动/停止队列信息
            startDelayService.removeFromDelayQueue(chargeOrder.getStartChargeSeq());
            stopDelayService.removeFromDelayQueue(chargeOrder.getStartChargeSeq());
            // 修改订单状态为取消
            chargeOrderService.lambdaUpdate()
                    .set(ChargeOrder::getOrderStatus, ChargeOrder.ORDER_STATUS_取消)
                    .set(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_已取消)
                    .eq(ChargeOrder::getStartChargeSeq, chargeOrder.getStartChargeSeq())
                    .update();
            LOGGER.info("取消/清除预约订单：{}", chargeOrder.getStartChargeSeq());
        } else {
            LOGGER.info("用户没有预约中的订单");
        }
    }


    /**
     * 启动充电
     *
     * @param connectorId
     * @param userRechargeRightsId
     * @param isBooking
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public Map<String, String> queryStartCharge(Long userId, String connectorId, Long userRechargeRightsId, Boolean isBooking, LocalDateTime startTime, LocalDateTime endTime) {
        var connectorId2StationId = getConnectorIdAndStationId(connectorId);
        connectorId = connectorId2StationId.get("connectorId");
        var stationId = connectorId2StationId.get("stationId");
        LOGGER.info("用户：{},站点：{}，设备：{}请求充电", userId, stationId, connectorId);
        var account = checkCharge(userId, connectorId, isBooking, startTime);

        // 是否有之前预约充电创建的订单记录，有则直接用，没有则创建
        ChargeOrder order = chargeOrderService.lambdaQuery()
                .eq(ChargeOrder::getUserId, userId)
                .eq(ChargeOrder::getConnectorId, connectorId)
                .in(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_已取消, ChargeOrder.CHARGE_STATUS_预约中)
                .one();

        if (order == null) {
            // 充电订单号/设备认证号
            String startChargeSeq = OrderUtils.getOrderNo(enPlusConfig.getOperatorId());

            // 组装订单数据
            order = new ChargeOrder();
            order.setIsBooking(isBooking ? ChargeOrder.IS_BOOKING_是 : ChargeOrder.IS_BOOKING_否);
            order.setUserId(userId);
            order.setStationId(stationId);
            order.setStartChargeSeq(startChargeSeq);
            order.setConnectorId(connectorId);
            order.setOrderStatus(ChargeOrder.ORDER_STATUS_未知);
            order.setChargeStatus(ChargeOrder.CHARGE_STATUS_启动中);
            chargeOrderService.save(order);

            // 订单充值权益
            if (!CommUtil.isEmptyOrNull(userRechargeRightsId)) {
                var userRechargeRights = userRechargeRightsService.lambdaQuery().eq(UserRechargeRights::getUserId, userId).eq(UserRechargeRights::getId, userRechargeRightsId).one();
                if (userRechargeRights != null) {
                    var orderRechargeRights = new OrderRechargeRights()
                            .setUserId(userId).setStartChargeSeq(startChargeSeq)
                            .setActivityId(userRechargeRights.getActivityId())
                            .setRightsId(userRechargeRights.getRightsId())
                            .setUserRightsId(userRechargeRights.getId())
                            .setDiscount(userRechargeRights.getDiscount());
                    orderRechargeRightsService.save(orderRechargeRights);
                }
            }
        }

        // 如果是预约订单，则将订单放入预约充电延迟队列
        if (isBooking) {
            order = order.setStartTime(startTime).setChargeStatus(ChargeOrder.CHARGE_STATUS_预约中).setIsBooking(ChargeOrder.IS_BOOKING_是);

            // 如果有设置结束时间,取消时记得删
            if (!CommUtil.isEmptyOrNull(endTime)) {
                order.setEndTime(endTime);
            }
            chargeOrderService.lambdaUpdate()
                    .set(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中)
                    .set(ChargeOrder::getIsBooking, order.getIsBooking())
                    .set(ChargeOrder::getStartTime, startTime)
                    .set(!CommUtil.isEmptyOrNull(endTime), ChargeOrder::getEndTime, endTime)
                    .eq(ChargeOrder::getUserId, userId)
                    .eq(ChargeOrder::getStartChargeSeq, order.getStartChargeSeq())
                    .update();

            var delayChargeOrder = new DelayChargeOrder();
            BeanUtils.copyProperties(order, delayChargeOrder);

            var flag = startDelayService.addToDelayQueue(delayChargeOrder.setStartTime(startTime));
            if (flag) {
                // 如果有结束时间，将订单放入结束充电延迟队列中
                if (endTime != null) {
                    stopDelayService.addToDelayQueue(delayChargeOrder.setEndTime(endTime));
                }
                // 切换数据源,修改设备状态为预约中
                updateEquipmentAndConnectorStatus(connectorId);
                LOGGER.info("预约充电成功，用户：{}，订单号：{}", userId, order.getStartChargeSeq());
                return Map.of("startChargeSeq", order.getStartChargeSeq());
            } else {
                LOGGER.error("预约充电失败，用户：{}，订单号：{}", userId, order.getStartChargeSeq());
                throw new BusinessException("充电预约失败");
            }
        }

        // 传递给EN+的余额要小于实际余额，防止订单超扣的情况，这里少传0.5元
        // TODO: 2023-11-30 快充这里考虑过充的金额要提高
        var amount = account.getBalance() - 50;
        return startCharge(order, connectorId, amount);

    }


    /**
     * 启动充电校验流程
     *
     * @param userId
     * @param connectorId
     * @param isBooking
     * @param startTime
     * @return
     */
    private Account checkCharge(Long userId, String connectorId, Boolean isBooking, LocalDateTime startTime) {
        if (CommUtil.isEmptyOrNull(connectorId)) {
            throw new BusinessException("请输入正确的设备编号");
        }

        // 设备是否插枪
        if (ConnectorStatusCache.INSTANCE.get(connectorId) == EquipmentInfo.SERVICE_STATUS_空闲) {
            throw new BusinessException("请插入充电枪");
        }

        if (isBooking) {
            // 预约时间不能超过未来24H
            if (TimeUnit.SECONDS.convert(Duration.between(LocalDateTime.now(), startTime)) > 3600 * 24) {
                throw new BusinessException("预约充电启动时间不能超过未来24小时");
            }
            // 预约充电通过connectorId查询预约中的订单
            var bookingOrder = chargeOrderService.lambdaQuery().eq(ChargeOrder::getUserId, userId).eq(ChargeOrder::getConnectorId, connectorId).eq(ChargeOrder::getIsBooking, ChargeOrder.IS_BOOKING_是).eq(ChargeOrder::getOrderStatus, ChargeOrder.ORDER_STATUS_未知).eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中).one();
            if (bookingOrder != null) {
                LOGGER.error("用户：{}存在进行中的订单：{}", userId, bookingOrder.getStartChargeSeq());
                throw new BusinessException(ResponseEnum.ORDER_IN_BOOKING);
            }
        }

        // 当前用户是否有正在进行中、预约中、启动中的订单
        var chargeOrder = chargeOrderService.getChargingOrderByUserId(userId);
        if (chargeOrder != null) {
            // 预约中的订单到了启动时间则直接启动，忽略校验
            if (!(chargeOrder.getChargeStatus().equals(ChargeOrder.CHARGE_STATUS_预约中) && chargeOrder.getStartTime().isBefore(LocalDateTime.now()))) {
                LOGGER.error("用户：{}存在进行中的订单：{}", userId, chargeOrder.getStartChargeSeq());
                throw new BusinessException(ResponseEnum.ORDER_IN_PROGRESS);
            }
        }

        // 查询用户余额
        var account = accountService.getAccountByUserId(userId);
        if (account.getBalance() <= 200) {
            LOGGER.error("用户：{}余额不足2元，余额：{}", userId, account.getBalance());
            throw new BusinessException(ResponseEnum.INSUFFICIENT_USER_BALANCE);
        }

        return account;
    }

    /**
     * 启动充电
     *
     * @param order
     * @param connectorId
     * @param amount
     * @return
     */
    private Map<String, String> startCharge(ChargeOrder order, String connectorId, int amount) {

        // 请求设备认证
        var equipAuth = enPlusService.queryEquipAuth(connectorId, order.getStartChargeSeq());

        if (equipAuth.containsKey("SuccStat") && equipAuth.getIntValue("SuccStat") == 0) {
            // 启动充电
            JSONObject startCharge;

            try {
                // 二维码文本
                var qrCode = "";
                startCharge = enPlusService.queryStartCharge(order.getStartChargeSeq(), connectorId, qrCode, amount);
            } catch (Exception e) {
                // 这里可能超时，忽略
                LOGGER.error("EN+启动充电超时，订单：{}", order.getStartChargeSeq(), e);
                return Map.of("startChargeSeq", order.getStartChargeSeq());
            }

            if (startCharge != null && startCharge.containsKey("SuccStat") && startCharge.getIntValue("SuccStat") == 0) {
                // 启动成功，更新充电订单状态
                chargeOrderService.lambdaUpdate().set(ChargeOrder::getIsBooking, ChargeOrder.IS_BOOKING_否).set(ChargeOrder::getChargeStatus, startCharge.getIntValue("StartChargeSeqStat")).eq(ChargeOrder::getStartChargeSeq, order.getStartChargeSeq()).update();
                return Map.of("startChargeSeq", order.getStartChargeSeq());
            } else {
                // 启动充电失败
                LOGGER.error("设备启动充电失败：{}", startCharge);
                switch (equipAuth.getIntValue("FailReason")) {
                    case 1 -> // 设备不存在
                            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_NOT_EXIST);
                    case 2 -> // 设备离线
                            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_OFFLINE);
                    case 3 -> // 存在未完成的订单
                            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_EXIST_ORDER_UNFINISHED);
                    case 4 -> // 此订单为停车中
                            throw new BusinessException(ResponseEnum.EN_PLUS_ORDER_IS_PARKING);
                    case 5 -> // 此订单已关闭
                            throw new BusinessException(ResponseEnum.EN_PLUS_ORDER_CLOSED);
                    case 6 -> // 设备未插枪
                            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_NOT_CONNECTED);
                    case 7 -> // 预充金额存在且小于1元
                            throw new BusinessException(ResponseEnum.INSUFFICIENT_USER_BALANCE);
                }
                throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_START_FAIL);
            }
        } else {
            // 设备认证失败
            LOGGER.error("设备认证失败：{}", equipAuth);
            switch (equipAuth.getIntValue("FailReason")) {
                case 1 -> // 设备未插枪
                        throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_NOT_CONNECTED);
                case 2 -> // 设备检测失败
                        throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_CHECK_FAIL);
            }
            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_AUTH_FAIL);
        }
    }


    /**
     * 修改充电桩状态（这里@DS使用AOP实现，切换数据源需要单独抽出方法，这里@DS不生效，改为手动切换）
     *
     * @param connectorId
     */
    @Override
    public void updateEquipmentAndConnectorStatus(String connectorId) {
        // 手动切换数据源
        DynamicDataSourceContextHolder.push("db-admin");
        equipmentInfoService.lambdaUpdate().set(EquipmentInfo::getServiceStatus, EquipmentInfo.SERVICE_STATUS_预约中).eq(EquipmentInfo::getEquipmentId, connectorId.substring(0, 16)).update();
        connectorInfoService.lambdaUpdate().set(ConnectorInfo::getStatus, EquipmentInfo.SERVICE_STATUS_预约中).eq(ConnectorInfo::getConnectorId, connectorId).update();
        DynamicDataSourceContextHolder.poll();
    }


    /**
     * 请求EN+设备充电状态
     *
     * @return
     */
    @Override
    public ChargeOrder queryEquipChargeStatus() {
        var userId = StpUtil.getLoginIdAsLong();
        var chargeOrder = chargeOrderService.getChargingOrderByUserId(userId);
        if (chargeOrder != null) {
            if (chargeOrder.getChargeStatus().equals(ChargeOrder.CHARGE_STATUS_预约中)) {
                return chargeOrder;
            }
            // 查询充电
            JSONObject data;
            var startChargeSeq = chargeOrder.getStartChargeSeq();
            try {
                data = enPlusService.queryEquipChargeStatus(startChargeSeq);
            } catch (Exception e) {
                LOGGER.error("EN+请求设备充电状态异常", e);
                throw new BusinessException("充电状态查询异常，请稍后重试");
            }

            // TODO 存在并发问题，订单刚启动充电，这里可能将订单状态重新更新为启动中，解决方案：屏蔽启动中状态更新

            // 更新订单信息
            chargeOrderService.lambdaUpdate()
                    .set(ChargeOrder::getSoc, data.getDoubleValue("Soc"))
                    .set(ChargeOrder::getTotalPower, data.getDoubleValue("TotalPower"))
                    .set(ChargeOrder::getTotalMoney, (int) ((data.getDouble("TotalMoney") * 100)))
                    .set(ChargeOrder::getElecMoney, (int) ((data.getDouble("ElecMoney") * 100)))
                    .set(ChargeOrder::getServiceMoney, (int) ((data.getDouble("SeviceMoney") * 100))) // 这里文档service单词错误，按文档填写
                    .set(ChargeOrder::getSumPeriod, (int) (data.getIntValue("SumPeriod")))
                    .set(ChargeOrder::getChargeDetail, data.getString("ChargeDetails"))
                    .set(ChargeOrder::getChargeStatus, data.getIntValue("StartChargeSeqStat"))
                    .eq(ChargeOrder::getStartChargeSeq, startChargeSeq).update();

            chargeOrder.setSoc(data.getDoubleValue("Soc"));
            chargeOrder.setTotalPower(data.getDoubleValue("TotalPower"));
            chargeOrder.setTotalMoney((int) ((data.getDouble("TotalMoney") * 100)));
            chargeOrder.setElecMoney((int) ((data.getDouble("ElecMoney") * 100)));
            chargeOrder.setServiceMoney((int) ((data.getDouble("SeviceMoney") * 100))); // 这里文档service单词错误，按文档填写
            chargeOrder.setSumPeriod(data.getIntValue("SumPeriod"));
            chargeOrder.setChargeDetail(data.getString("ChargeDetails"));
            chargeOrder.setChargeStatus(data.getIntValue("StartChargeSeqStat"));
            return chargeOrder;
        } else {
            LOGGER.debug("用户：{}无进行中的订单", userId);
            return null;
        }
    }

    /**
     * 获取设备接口编号和站点编号
     *
     * @param connectorId 充电桩id，短编号等
     * @return
     */
    @DS("db-admin")
    public Map<String, String> getConnectorIdAndStationId(String connectorId) {
        var stationId = "";
        if (connectorId.length() == 17) {
            var equipmentRelation = equipmentRelationService.getByEquipmentId(connectorId.substring(0, 16));
            stationId = equipmentRelation.getStationId();
        } else if (connectorId.length() == 6) {
            // 查询EN+设备SN
            var equipmentRelation = equipmentRelationService.getByShortId(connectorId);
            connectorId = equipmentRelation.getConnectorId();
            stationId = equipmentRelation.getStationId();
            if (connectorId.length() == 16) {
                // 如果是16位，末尾补1作为单枪枪号
                connectorId = connectorId.concat("1");
            }
        } else {
            // 充电桩编号错误
            LOGGER.error("设备编码错误：{}", connectorId);
            throw new BusinessException(ResponseEnum.EQUIP_CONNECTOR_ID_ERROR);
        }
        return Map.of("connectorId", connectorId, "stationId", stationId);
    }

    @DS("db-admin")
    public String getConnectorId(String connectorId) {
        if (connectorId.length() == 17) {
            return connectorId;
        } else if (connectorId.length() == 6) {
            // 查询EN+设备SN
            var equipmentRelation = equipmentRelationService.getByShortId(connectorId);
            connectorId = equipmentRelation.getConnectorId();
            if (connectorId.length() == 16) {
                // 如果是16位，末尾补1作为单枪枪号
                connectorId = connectorId.concat("1");
            }
        } else {
            // 充电桩编号错误
            LOGGER.error("设备编码错误：{}", connectorId);
            throw new BusinessException(ResponseEnum.EQUIP_CONNECTOR_ID_ERROR);
        }
        return connectorId;
    }

    /**
     * 请求停止充电
     *
     * @param connectorId
     */
    @Override
    public void queryStopCharge(long userId, String connectorId) {
        connectorId = getConnectorId(connectorId);
        LOGGER.info("用户：{},设备：{}请求停止充电", userId, connectorId);
        // 查询充电订单
        var chargeOrder = chargeOrderService.lambdaQuery().eq(ChargeOrder::getUserId, userId).eq(ChargeOrder::getConnectorId, connectorId).eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_充电中).one();
        if (chargeOrder == null) {
            // 记录失败原因
            LOGGER.error("用户：{}请求停止充电异常，设备：{}无进行中的订单", userId, connectorId);
            throw new BusinessException(ResponseEnum.NO_ORDER_IN_PROGRESS);
        }
        var data = enPlusService.queryStopCharge(chargeOrder.getStartChargeSeq(), connectorId);
        if (data.containsKey("SuccStat") && data.getIntValue("SuccStat") != 0) {
            // 记录失败原因
            LOGGER.error("订单：{}停止充电失败，返回结果:{}", chargeOrder.getStartChargeSeq(), data);
            throw new BusinessException(ResponseEnum.EN_PLUS_EQUIP_STOP_FAIL);
        }
        // 这里不更新订单状态，等待EN+推送停止充电结果信息再处理

    }

    /**
     * 请求设备计费策略
     *
     * @param connectorId
     * @return
     */
    @Override
    public EnBusinessPolicy queryEquipBusinessPolicy(String connectorId) {
        var equipBizSeq = OrderUtils.getOrderNo(enPlusConfig.getOperatorId());
        var data = enPlusService.queryEquipBusinessPolicy(equipBizSeq, connectorId);
        return data.toJavaObject(EnBusinessPolicy.class);
    }

}
