浏览代码

feat(charging): 优化充电桩价格策略同步与更新频率控制

- 在ChargingJob中增加价格策略同步判断,若上次更新时间不足1小时则跳过同步
- 实现shouldSkipUpdate方法,通过查询最新价格策略及明细更新时间判断是否跳过更新
- 在ThirdPartyChargingServiceImpl更新价格策略明细时,增加更新时间判定,1小时内跳过更新
- 减少频繁调用第三方接口和数据库写操作,提升系统性能与稳定性
- 增加日志记录详细更新跳过原因,便于后续追踪调试
SheepHy 2 天之前
父节点
当前提交
b052c877c6

+ 11 - 1
src/main/java/com/zsElectric/boot/business/service/impl/ThirdPartyChargingServiceImpl.java

@@ -655,7 +655,17 @@ public class ThirdPartyChargingServiceImpl implements ThirdPartyChargingService
                 savePolicyInfoDetail(policyInfo, policyId);
                 log.debug("价格策略明细新增成功 - policyId: {}, startTime: {}", policyId, policyInfo.getStartTime());
             } else if (!isPolicyInfoSame(existing, policyInfo)) {
-                // 存在且数据变化,执行更新
+                // 检查上次更新时间,如果低于1小时,跳过本次更新
+                if (existing.getUpdateTime() != null) {
+                    LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1);
+                    if (existing.getUpdateTime().isAfter(oneHourAgo)) {
+                        log.info("价格策略明细上次更新时间不足1小时,跳过更新 - policyId: {}, startTime: {}, lastUpdateTime: {}",
+                                policyId, policyInfo.getStartTime(), existing.getUpdateTime());
+                        continue; // 跳过本次更新
+                    }
+                }
+                
+                // 存在且数据变化,且距离上次更新超过1小时,执行更新
                 updatePolicyInfoDetail(existing, policyInfo);
                 log.debug("价格策略明细更新成功 - policyId: {}, startTime: {}", policyId, policyInfo.getStartTime());
                 

+ 74 - 1
src/main/java/com/zsElectric/boot/charging/quartz/ChargingJob.java

@@ -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;
+        }
+    }
 }