From ff7b11e27179964e870633c1cee105ff02f0c561 Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 25 Feb 2025 16:25:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=BE=99=E5=85=B4=E5=8F=91?= =?UTF-8?q?=E8=BF=90=E7=9C=8B=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../busi/MesShippingKanbanCfgServiceImpl.java | 39 +++++++++++----------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/modules/i3plus-ext-mes-pcn-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/pcn/apiservice/serviceimpl/busi/MesShippingKanbanCfgServiceImpl.java b/modules/i3plus-ext-mes-pcn-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/pcn/apiservice/serviceimpl/busi/MesShippingKanbanCfgServiceImpl.java index ab97e52..c4780ac 100644 --- a/modules/i3plus-ext-mes-pcn-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/pcn/apiservice/serviceimpl/busi/MesShippingKanbanCfgServiceImpl.java +++ b/modules/i3plus-ext-mes-pcn-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/pcn/apiservice/serviceimpl/busi/MesShippingKanbanCfgServiceImpl.java @@ -1043,8 +1043,7 @@ public class MesShippingKanbanCfgServiceImpl implements IMesShippingKanbanCfgSer List values = new ArrayList<>(); List strShippingGroupList = Arrays.asList(cfg.getShippingGroupCode().split(",")); if (CollectionUtils.isEmpty(strShippingGroupList)) { - values.add("0"); - return values; + return Collections.singletonList("0"); } Date nowTime = DateUtil.now(); @@ -1083,19 +1082,19 @@ public class MesShippingKanbanCfgServiceImpl implements IMesShippingKanbanCfgSer List values = new ArrayList<>(); List partShippingGroups = getShippingGroups(cfg); if (CollectionUtils.isEmpty(partShippingGroups)) { - values.add("0"); - return values; + return Collections.singletonList("0"); } Date nowTime = DateUtil.now(); Date prevHourTime = DateUtil.addMinutes(nowTime, -60); - StringBuilder hql = new StringBuilder(); - hql.append("select new map(workCenterCode, count(1) as totalCount) from " + MesProductionRecord.class.getName()); - hql.append(" where organizeCode = :organizeCode and isDeleted = :isDeleted and isValid = :isValid "); - hql.append(" and createDatetime BETWEEN :startDateTime AND :endDateTime "); - hql.append(" group by workCenterCode"); - List ddd = entityManager.createQuery(hql.toString()) + // Use a single query to fetch counts grouped by work center code + List productionCounts = entityManager.createQuery( + "SELECT new map(pr.workCenterCode as workCenterCode, COUNT(pr) as totalCount) " + + "FROM " + MesProductionRecord.class.getName() + " pr " + + "WHERE pr.organizeCode = :organizeCode AND pr.isDeleted = :isDeleted AND pr.isValid = :isValid " + + "AND pr.createDatetime BETWEEN :startDateTime AND :endDateTime " + + "GROUP BY pr.workCenterCode", Map.class) .setParameter("organizeCode", cfg.getOrganizeCode()) .setParameter("isValid", CommonEnumUtil.VALID) .setParameter("isDeleted", CommonEnumUtil.FALSE) @@ -1103,21 +1102,23 @@ public class MesShippingKanbanCfgServiceImpl implements IMesShippingKanbanCfgSer .setParameter("endDateTime", DateUtil.formatDate(prevHourTime)) .getResultList(); - Map shippingGroupCount = new HashMap<>(); - if (!CollectionUtils.isEmpty(ddd)) { - for (Map dd : ddd) { - Object totalCount = dd.get("totalCount"); - if (totalCount != null) { - shippingGroupCount.put(dd.get("workCenterCode").toString(), totalCount.toString()); - } - } - } + // Use a map to store counts for quick access + Map shippingGroupCount = productionCounts.stream() + .collect(Collectors.toMap( + map -> map.get("workCenterCode").toString(), + map -> map.get("totalCount").toString(), + (existing, replacement) -> existing // Handle duplicates if any + )); + + // Collect counts for each shipping group for (MesPartShippingGroup shippingGroup : partShippingGroups) { if (StringUtils.isEmpty(shippingGroup.getCarrierCode())) { continue; } values.add(shippingGroupCount.getOrDefault(shippingGroup.getCarrierCode(), "0")); } + + // Ensure at least one value is returned if (values.isEmpty()) { values.add("0"); }