From aecf17db2e6095eae5b5dd103d6685b5657501d7 Mon Sep 17 00:00:00 2001 From: "han\\hanst" Date: Wed, 19 Aug 2026 15:47:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F1=E5=B0=8F=E6=97=B6=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=99=BA=E8=83=BD=E7=AB=8B=E4=BD=93=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E5=BA=93=E5=AD=98=E8=B6=8B=E5=8A=BF=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/task/DashboardPushTask.java | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java b/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java index 3c240acb..b8bb99f6 100644 --- a/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java +++ b/src/main/java/com/gaotao/modules/dashboard/task/DashboardPushTask.java @@ -57,6 +57,15 @@ public class DashboardPushTask { */ private Map lastDataHash = new HashMap<>(); + /** + * 库存趋势缓存(小时级刷新,秒级推送复用) + * + *

避免10秒推送任务高频查库,将趋势查询拆分为独立的小时任务。

+ */ + private volatile List> rawMaterialTrendCache = Collections.emptyList(); + private volatile List> specifiedMaterialTrendCache = Collections.emptyList(); + private volatile List> finishedGoodsTrendCache = Collections.emptyList(); + @Autowired private com.gaotao.modules.dashboard.dao.DashboardDao dashboardDao; @@ -541,7 +550,38 @@ public class DashboardPushTask { } /** - * 每5秒检查智能立体仓库看板数据并推送 + * 每1小时刷新一次智能立体仓库库存趋势缓存 + * + *

趋势数据变化频率低于秒级看板推送频率,拆分为小时级任务可降低数据库压力。

+ */ + @Scheduled(initialDelay = 0, fixedRate = 3600000) + public void refreshWarehouseInventoryTrendCache() { + // 检查总开关 + if (!dashboardPushEnabled) { + log.trace("看板推送已禁用,跳过库存趋势缓存刷新"); + return; + } + + try { + List> rawMaterialTrend = dashboardDao.queryRawMaterialInventoryTrend(); + List> specifiedMaterialTrend = dashboardDao.querySpecifiedMaterialInventoryTrend(); + List> finishedGoodsTrend = dashboardDao.queryFinishedGoodsInventoryTrend(); + + // 查询结果写入新对象再整体替换,避免10秒推送读取到中间态 + rawMaterialTrendCache = rawMaterialTrend != null ? new ArrayList<>(rawMaterialTrend) : Collections.emptyList(); + specifiedMaterialTrendCache = specifiedMaterialTrend != null ? new ArrayList<>(specifiedMaterialTrend) : Collections.emptyList(); + finishedGoodsTrendCache = finishedGoodsTrend != null ? new ArrayList<>(finishedGoodsTrend) : Collections.emptyList(); + + log.info("刷新库存趋势缓存完成: 原材料{}条, 规格料{}条, 产成品{}条", + rawMaterialTrendCache.size(), specifiedMaterialTrendCache.size(), finishedGoodsTrendCache.size()); + } catch (Exception e) { + // 刷新失败时保留上一次成功数据,避免看板趋势瞬间被清空 + log.error("刷新库存趋势缓存失败,沿用上次缓存数据: {}", e.getMessage(), e); + } + } + + /** + * 每10秒检查智能立体仓库看板数据并推送 * *

数据来源:

*
    @@ -623,15 +663,12 @@ public class DashboardPushTask { Map shipmentStats = new HashMap<>(); log.debug("发货统计: {}", shipmentStats); - // 查询库存趋势数据 - List> rawMaterialTrend = dashboardDao.queryRawMaterialInventoryTrend(); - log.debug("查询到原材料库存趋势数据: {}条", rawMaterialTrend != null ? rawMaterialTrend.size() : 0); - - List> specifiedMaterialTrend = dashboardDao.querySpecifiedMaterialInventoryTrend(); - log.debug("查询到规格料库存趋势数据: {}条", specifiedMaterialTrend != null ? specifiedMaterialTrend.size() : 0); - - List> finishedGoodsTrend = dashboardDao.queryFinishedGoodsInventoryTrend(); - log.debug("查询到产成品库存趋势数据: {}条", finishedGoodsTrend != null ? finishedGoodsTrend.size() : 0); + // 使用小时级任务刷新后的库存趋势缓存,避免10秒推送高频查库 + List> rawMaterialTrend = new ArrayList<>(rawMaterialTrendCache); + List> specifiedMaterialTrend = new ArrayList<>(specifiedMaterialTrendCache); + List> finishedGoodsTrend = new ArrayList<>(finishedGoodsTrendCache); + log.debug("使用库存趋势缓存数据: 原材料{}条, 规格料{}条, 产成品{}条", + rawMaterialTrend.size(), specifiedMaterialTrend.size(), finishedGoodsTrend.size()); // 构造返回数据 Map resultData = new HashMap<>();