|
|
@@ -6,18 +6,25 @@ import com.kym.common.config.EnPlusConfig;
|
|
|
import com.kym.common.constant.ResponseEnum;
|
|
|
import com.kym.common.exception.BusinessException;
|
|
|
import com.kym.common.utils.OrderUtils;
|
|
|
+import com.kym.entity.admin.EquipmentInfo;
|
|
|
import com.kym.entity.enplus.response.EnBusinessPolicy;
|
|
|
import com.kym.entity.miniapp.ChargeOrder;
|
|
|
+import com.kym.service.admin.EquipmentInfoService;
|
|
|
import com.kym.service.admin.EquipmentRelationService;
|
|
|
import com.kym.service.enplus.EnPlusService;
|
|
|
import com.kym.service.miniapp.AccountService;
|
|
|
import com.kym.service.miniapp.ChargeOrderService;
|
|
|
import com.kym.service.miniapp.ChargeService;
|
|
|
+import com.kym.service.queue.DelayedItem;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.DelayQueue;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
|
/**
|
|
|
* @author skyline
|
|
|
@@ -31,6 +38,8 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
|
|
|
private final EquipmentRelationService equipmentRelationService;
|
|
|
|
|
|
+ private final EquipmentInfoService equipmentInfoService;
|
|
|
+
|
|
|
private final ChargeOrderService chargeOrderService;
|
|
|
|
|
|
private final AccountService accountService;
|
|
|
@@ -39,8 +48,16 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
|
|
|
private final EnPlusConfig enPlusConfig;
|
|
|
|
|
|
- public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, ChargeOrderService chargeOrderService, AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig) {
|
|
|
+ /**
|
|
|
+ * 预约充电队列
|
|
|
+ */
|
|
|
+ DelayQueue<DelayedItem> delayQueue = new DelayQueue<>();
|
|
|
+
|
|
|
+ private ExecutorService executorService = Executors.newFixedThreadPool(2);
|
|
|
+
|
|
|
+ public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, EquipmentInfoService equipmentInfoService, ChargeOrderService chargeOrderService, AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig) {
|
|
|
this.equipmentRelationService = equipmentRelationService;
|
|
|
+ this.equipmentInfoService = equipmentInfoService;
|
|
|
this.chargeOrderService = chargeOrderService;
|
|
|
this.accountService = accountService;
|
|
|
this.enPlusService = enPlusService;
|
|
|
@@ -51,17 +68,26 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
* 启动充电
|
|
|
*
|
|
|
* @param connectorId
|
|
|
+ * @param isBooking
|
|
|
+ * @param startTime
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Map<String, String> queryStartCharge(String connectorId) {
|
|
|
+ public Map<String, String> queryStartCharge(String connectorId, Boolean isBooking, LocalDateTime startTime) {
|
|
|
var userId = StpUtil.getLoginIdAsLong();
|
|
|
+// if (isBooking) {
|
|
|
+// // 预约充电通过connectorId查询预约中的订单
|
|
|
+// var bookingOrder = chargeOrderService.lambdaQuery()
|
|
|
+// .eq(ChargeOrder::getConnectorId, connectorId).eq(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_预约中).one();
|
|
|
+// userId =bookingOrder.getUserId();
|
|
|
+// }
|
|
|
var map = getConnectorIdAndStationId(connectorId);
|
|
|
connectorId = map.get("connectorId");
|
|
|
var stationId = map.get("stationId");
|
|
|
LOGGER.info("用户:{},设备:{}请求启动充电", userId, connectorId);
|
|
|
// 二维码文本
|
|
|
var qrCode = "";
|
|
|
+
|
|
|
// 当前设备是是否有正在进行中的订单
|
|
|
var chargeOrder = chargeOrderService.getChargingOrderByUserId(userId);
|
|
|
if (chargeOrder != null) {
|
|
|
@@ -83,18 +109,38 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
// 请求设备认证
|
|
|
var equipAuth = enPlusService.queryEquipAuth(connectorId, startChargeSeq);
|
|
|
|
|
|
+ // 组装订单数据
|
|
|
+ var order = new ChargeOrder();
|
|
|
+ order.setUserId(userId);
|
|
|
+ order.setStationId(stationId);
|
|
|
+ order.setStartChargeSeq(startChargeSeq);
|
|
|
+ order.setConnectorId(connectorId);
|
|
|
+ order.setOrderStatus(ChargeOrder.ORDER_STATUS_未知);
|
|
|
+
|
|
|
+ // 如果是预约订单,则将订单放入预约充电延迟队列
|
|
|
+ if (isBooking) {
|
|
|
+ order.setChargeStatus(ChargeOrder.CHARGE_STATUS_预约中);
|
|
|
+ chargeOrderService.save(order);
|
|
|
+ var flag = delayQueue.offer(new DelayedItem<ChargeOrder>(order, startTime));
|
|
|
+ if (flag) {
|
|
|
+ // 修改设备状态为预约中
|
|
|
+ equipmentInfoService.lambdaUpdate()
|
|
|
+ .set(EquipmentInfo::getServiceStatus, EquipmentInfo.SERVICE_STATUS_预约中)
|
|
|
+ .eq(EquipmentInfo::getEquipmentId, connectorId.substring(0, 16))
|
|
|
+ .update();
|
|
|
+ return Map.of("startChargeSeq", startChargeSeq);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("充电预约失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ chargeOrderService.save(order);
|
|
|
+
|
|
|
if (equipAuth.containsKey("SuccStat") && equipAuth.getIntValue("SuccStat") == 0) {
|
|
|
- // TODO 查询业务策略信息(计费信息),目前计费在EN+完成,后续自主计费需要开发
|
|
|
// 启动充电
|
|
|
var startCharge = enPlusService.queryStartCharge(startChargeSeq, connectorId, qrCode, amount);
|
|
|
if (startCharge.containsKey("SuccStat") && startCharge.getIntValue("SuccStat") == 0) {
|
|
|
// 启动成功,生成充电订单
|
|
|
- var order = new ChargeOrder();
|
|
|
- order.setUserId(userId);
|
|
|
- order.setStationId(stationId);
|
|
|
- order.setStartChargeSeq(startChargeSeq);
|
|
|
- order.setConnectorId(connectorId);
|
|
|
- order.setOrderStatus(ChargeOrder.ORDER_STATUS_未知);
|
|
|
order.setChargeStatus(startCharge.getIntValue("StartChargeSeqStat"));
|
|
|
chargeOrderService.save(order);
|
|
|
return Map.of("startChargeSeq", startChargeSeq);
|