|
|
@@ -2,7 +2,11 @@ package com.zsElectric.boot.charging.quartz;
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.zsElectric.boot.charging.entity.ThirdPartyConnectorInfo;
|
|
|
+import com.zsElectric.boot.charging.entity.ThirdPartyEquipmentPricePolicy;
|
|
|
+import com.zsElectric.boot.charging.entity.ThirdPartyPolicyInfo;
|
|
|
import com.zsElectric.boot.charging.mapper.ThirdPartyConnectorInfoMapper;
|
|
|
+import com.zsElectric.boot.charging.mapper.ThirdPartyEquipmentPricePolicyMapper;
|
|
|
+import com.zsElectric.boot.charging.mapper.ThirdPartyPolicyInfoMapper;
|
|
|
import com.zsElectric.boot.charging.service.ChargingBusinessService;
|
|
|
import com.zsElectric.boot.charging.vo.ChargingPricePolicyVO;
|
|
|
import com.zsElectric.boot.charging.vo.QueryStationsInfoVO;
|
|
|
@@ -29,6 +33,8 @@ public class ChargingJob {
|
|
|
|
|
|
private final ChargingBusinessService chargingBusinessService;
|
|
|
private final ThirdPartyConnectorInfoMapper connectorInfoMapper;
|
|
|
+ private final ThirdPartyEquipmentPricePolicyMapper pricePolicyMapper;
|
|
|
+ private final ThirdPartyPolicyInfoMapper policyInfoMapper;
|
|
|
|
|
|
// 任务执行标记,防止并发执行
|
|
|
private volatile boolean isPricePolicySyncRunning = false;
|
|
|
@@ -77,7 +83,7 @@ public class ChargingJob {
|
|
|
* 每10分钟执行一次,查询所有充电桩的价格策略并存储到数据库
|
|
|
* cron表达式: 0 10 * * * ? 表示每10分钟执行
|
|
|
*/
|
|
|
- @Scheduled(cron = "0 */10 * * * ?")
|
|
|
+ @Scheduled(cron = "0 */30 * * * ?")
|
|
|
public void syncEquipmentPricePolicy() {
|
|
|
// 检查任务是否正在执行,防止并发
|
|
|
if (isPricePolicySyncRunning) {
|
|
|
@@ -108,6 +114,13 @@ public class ChargingJob {
|
|
|
String connectorId = connector.getConnectorId();
|
|
|
String equipmentId = connector.getEquipmentId();
|
|
|
|
|
|
+ // 检查该充电桩的价格策略上次更新时间,如果不足1小时则跳过
|
|
|
+ if (shouldSkipUpdate(connectorId)) {
|
|
|
+ log.info("充电桩价格策略上次更新时间不足1小时,跳过本次同步 - connectorId: {}, equipmentId: {}",
|
|
|
+ connectorId, equipmentId);
|
|
|
+ continue; // 跳过本次充电桩,不调用第三方接口
|
|
|
+ }
|
|
|
+
|
|
|
log.info("查询价格策略 - connectorId: {}, equipmentId: {}", connectorId, equipmentId);
|
|
|
|
|
|
// 调用业务服务查询价格策略(会自动保存到数据库)
|
|
|
@@ -147,4 +160,64 @@ public class ChargingJob {
|
|
|
log.info("设备价格策略同步定时任务执行结束");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查是否应该跳过更新
|
|
|
+ * 如果该充电桩的价格策略上次更新时间不足1小时,则跳过
|
|
|
+ *
|
|
|
+ * @param connectorId 充电接口ID
|
|
|
+ * @return true-跳过更新, false-需要更新
|
|
|
+ */
|
|
|
+ private boolean shouldSkipUpdate(String connectorId) {
|
|
|
+ try {
|
|
|
+ // 1. 查询该充电桩的最新价格策略记录
|
|
|
+ ThirdPartyEquipmentPricePolicy latestPolicy = pricePolicyMapper.selectList(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ThirdPartyEquipmentPricePolicy>()
|
|
|
+ .eq(ThirdPartyEquipmentPricePolicy::getConnectorId, connectorId)
|
|
|
+ .orderByDesc(ThirdPartyEquipmentPricePolicy::getCreateTime)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ ).stream().findFirst().orElse(null);
|
|
|
+
|
|
|
+ if (latestPolicy == null) {
|
|
|
+ // 没有记录,需要更新
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查询该价格策略的明细记录,获取最近的更新时间
|
|
|
+ List<ThirdPartyPolicyInfo> policyInfoList = policyInfoMapper.selectList(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ThirdPartyPolicyInfo>()
|
|
|
+ .eq(ThirdPartyPolicyInfo::getPricePolicyId, latestPolicy.getId())
|
|
|
+ .orderByDesc(ThirdPartyPolicyInfo::getUpdateTime)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (policyInfoList.isEmpty()) {
|
|
|
+ // 没有明细记录,需要更新
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDateTime lastUpdateTime = policyInfoList.get(0).getUpdateTime();
|
|
|
+ if (lastUpdateTime == null) {
|
|
|
+ // 没有更新时间,需要更新
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 判断是否距离上次更新不足1小时
|
|
|
+ LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1);
|
|
|
+ if (lastUpdateTime.isAfter(oneHourAgo)) {
|
|
|
+ // 上次更新时间在1小时内,跳过更新
|
|
|
+ log.debug("充电桩价格策略距离上次更新不足1小时 - connectorId: {}, lastUpdateTime: {}",
|
|
|
+ connectorId, lastUpdateTime);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 距离上次更新超过1小时,需要更新
|
|
|
+ return false;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("检查更新时间失败,默认需要更新 - connectorId: {}", connectorId, e);
|
|
|
+ // 发生异常时,默认需要更新
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|