|
|
@@ -11,12 +11,15 @@ 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;
|
|
|
@@ -50,6 +53,8 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
|
|
|
private final EquipmentInfoService equipmentInfoService;
|
|
|
|
|
|
+ private final ConnectorInfoService connectorInfoService;
|
|
|
+
|
|
|
private final ChargeOrderService chargeOrderService;
|
|
|
|
|
|
private final OrderRechargeRightsService orderRechargeRightsService;
|
|
|
@@ -64,9 +69,15 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
private final DelayService<DelayChargeOrder> startDelayService;
|
|
|
private final DelayService<DelayChargeOrder> stopDelayService;
|
|
|
|
|
|
- public ChargeServiceImpl(EquipmentRelationService equipmentRelationService, EquipmentInfoService equipmentInfoService, ChargeOrderService chargeOrderService, OrderRechargeRightsService orderRechargeRightsService, UserRechargeRightsService userRechargeRightsService, AccountService accountService, EnPlusService enPlusService, EnPlusConfig enPlusConfig, @Qualifier("StartChargeDelayJob") @Lazy DelayService<DelayChargeOrder> startDelayService, @Qualifier("StopChargeDelayJob") @Lazy 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;
|
|
|
@@ -149,6 +160,11 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
cancelBookByChargeOrder(chargeOrder);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 取消预约订单
|
|
|
+ *
|
|
|
+ * @param chargeOrder 预约订单
|
|
|
+ */
|
|
|
public void cancelBookByChargeOrder(ChargeOrder chargeOrder) {
|
|
|
if (chargeOrder != null) {
|
|
|
// 清除启动/停止队列信息
|
|
|
@@ -179,55 +195,11 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
@Override
|
|
|
@DSTransactional(rollbackFor = Exception.class)
|
|
|
public Map<String, String> queryStartCharge(Long userId, String connectorId, Long userRechargeRightsId, Boolean isBooking, LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
- var map = getConnectorIdAndStationId(connectorId);
|
|
|
- connectorId = map.get("connectorId");
|
|
|
-
|
|
|
- if (CommUtil.isEmptyOrNull(connectorId)) {
|
|
|
- throw new BusinessException("请输入正确的设备编号");
|
|
|
- }
|
|
|
-
|
|
|
- var stationId = map.get("stationId");
|
|
|
-
|
|
|
+ var connectorId2StationId = getConnectorIdAndStationId(connectorId);
|
|
|
+ connectorId = connectorId2StationId.get("connectorId");
|
|
|
+ var stationId = connectorId2StationId.get("stationId");
|
|
|
LOGGER.info("用户:{},站点:{},设备:{}请求充电", userId, stationId, connectorId);
|
|
|
-
|
|
|
- // 设备是否插枪
|
|
|
- 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 qrCode = "";
|
|
|
-
|
|
|
- // 当前用户是否有正在进行中、预约中、启动中的订单
|
|
|
- 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);
|
|
|
- }
|
|
|
- // 传递给EN+的余额要小于实际余额,防止订单超扣的情况,这里少传0.5元
|
|
|
- var amount = account.getBalance() - 50;
|
|
|
+ var account = checkCharge(userId, connectorId, isBooking, startTime);
|
|
|
|
|
|
// 是否有之前预约充电创建的订单记录,有则直接用,没有则创建
|
|
|
ChargeOrder order = chargeOrderService.lambdaQuery().eq(ChargeOrder::getConnectorId, connectorId).in(ChargeOrder::getChargeStatus, ChargeOrder.CHARGE_STATUS_已取消, ChargeOrder.CHARGE_STATUS_预约中).one();
|
|
|
@@ -257,9 +229,6 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 请求设备认证
|
|
|
- var equipAuth = enPlusService.queryEquipAuth(connectorId, order.getStartChargeSeq());
|
|
|
-
|
|
|
// 如果是预约订单,则将订单放入预约充电延迟队列
|
|
|
if (isBooking) {
|
|
|
order = order.setStartTime(startTime).setChargeStatus(ChargeOrder.CHARGE_STATUS_预约中).setIsBooking(ChargeOrder.IS_BOOKING_是);
|
|
|
@@ -280,7 +249,7 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
stopDelayService.addToDelayQueue(delayChargeOrder.setEndTime(endTime));
|
|
|
}
|
|
|
// 切换数据源,修改设备状态为预约中
|
|
|
- updateEquipmentStatus(connectorId);
|
|
|
+ updateEquipmentAndConnectorStatus(connectorId);
|
|
|
LOGGER.info("预约充电成功,用户:{},订单号:{}", userId, order.getStartChargeSeq());
|
|
|
return Map.of("startChargeSeq", order.getStartChargeSeq());
|
|
|
} else {
|
|
|
@@ -289,11 +258,86 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 传递给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 = null;
|
|
|
+ JSONObject startCharge;
|
|
|
|
|
|
try {
|
|
|
+ // 二维码文本
|
|
|
+ var qrCode = "";
|
|
|
startCharge = enPlusService.queryStartCharge(order.getStartChargeSeq(), connectorId, qrCode, amount);
|
|
|
} catch (Exception e) {
|
|
|
// 这里可能超时,忽略
|
|
|
@@ -303,11 +347,7 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
|
|
|
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();
|
|
|
+ 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 {
|
|
|
// 启动充电失败
|
|
|
@@ -350,12 +390,12 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
* @param connectorId
|
|
|
*/
|
|
|
@Override
|
|
|
- public void updateEquipmentStatus(String connectorId) {
|
|
|
+ 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();
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -383,17 +423,8 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
}
|
|
|
|
|
|
// 更新订单信息
|
|
|
- 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();
|
|
|
+ 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"));
|
|
|
@@ -425,7 +456,7 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
} else if (connectorId.length() == 6) {
|
|
|
// 查询EN+设备SN
|
|
|
var equipmentRelation = equipmentRelationService.getByShortId(connectorId);
|
|
|
- connectorId = equipmentRelation.getEquipmentId();
|
|
|
+ connectorId = equipmentRelation.getConnectorId();
|
|
|
stationId = equipmentRelation.getStationId();
|
|
|
if (connectorId.length() == 16) {
|
|
|
// 如果是16位,末尾补1作为单枪枪号
|
|
|
@@ -446,7 +477,7 @@ public class ChargeServiceImpl implements ChargeService {
|
|
|
} else if (connectorId.length() == 6) {
|
|
|
// 查询EN+设备SN
|
|
|
var equipmentRelation = equipmentRelationService.getByShortId(connectorId);
|
|
|
- connectorId = equipmentRelation.getEquipmentId();
|
|
|
+ connectorId = equipmentRelation.getConnectorId();
|
|
|
if (connectorId.length() == 16) {
|
|
|
// 如果是16位,末尾补1作为单枪枪号
|
|
|
connectorId = connectorId.concat("1");
|