From e531ce019d88b9f544b4235f92929ba0b1065305 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Thu, 21 May 2026 15:35:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(mes-excoil):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E5=8D=B7=E6=9D=90=E6=95=B0=E6=8D=AE=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=80=A7=E8=83=BD(=E5=8F=AA=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=96=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加空数据检查并返回默认结果 - 实现批量数据转换提升处理效率 - 采用批量查询替代逐条查询减少数据库访问 - 分离新增和更新操作支持批量处理 - 实现分批插入和更新避免大数据量问题 - 每批处理500条记录确保系统稳定性 --- .../service/impl/MesExCoilServiceImpl.java | 82 ++++++++++++++++--- 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java b/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java index 78a15f46..b5363752 100644 --- a/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java +++ b/klp-mes/src/main/java/com/klp/mes/excoil/service/impl/MesExCoilServiceImpl.java @@ -155,28 +155,84 @@ public class MesExCoilServiceImpl implements IMesExCoilService { apiRows = fetchIncrementalFromApi(maxInsdate); } + if (apiRows == null || apiRows.isEmpty()) { + Map result = new HashMap<>(); + result.put("totalFetched", 0); + result.put("insertCount", 0); + result.put("updateCount", 0); + result.put("fullSync", isFullSync); + return result; + } + int insertCount = 0; int updateCount = 0; Date now = new Date(); + // 1. 批量转换所有数据 + List allEntities = new ArrayList<>(apiRows.size()); for (Map row : apiRows) { MesExCoil entity = mapRowToEntity(row); entity.setSyncTime(now); - - LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); - wrapper.eq(MesExCoil::getExcoilid, entity.getExcoilid()); - MesExCoil existing = baseMapper.selectOne(wrapper); - - if (existing != null) { - entity.setExId(existing.getExId()); - baseMapper.updateById(entity); - updateCount++; - } else { - baseMapper.insert(entity); - insertCount++; - } + allEntities.add(entity); } +// // 2. 收集所有 excoilid +// List excoilids = allEntities.stream() +// .map(MesExCoil::getExcoilid) +// .filter(StringUtils::isNotBlank) +// .distinct() +// .collect(java.util.stream.Collectors.toList()); + +// // 3. 批量查询已存在的记录 +// Map existingMap = new HashMap<>(); +// if (!excoilids.isEmpty()) { +// LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); +// wrapper.in(MesExCoil::getExcoilid, excoilids); +// List existingList = baseMapper.selectList(wrapper); +// existingMap = existingList.stream() +// .collect(java.util.stream.Collectors.toMap(MesExCoil::getExcoilid, v -> v, (a, b) -> a)); +// } + + // 4. 分离新增和更新的记录 + List toInsert = new ArrayList<>(); +// List toUpdate = new ArrayList<>(); + + for (MesExCoil entity : allEntities) { +// MesExCoil existing = existingMap.get(entity.getExcoilid()); +// if (existing != null) { +// // 更新:设置主键 +// entity.setExId(existing.getExId()); +// toUpdate.add(entity); +// } else { + // 新增 + toInsert.add(entity); +// } + } + + // 5. 批量插入 + if (!toInsert.isEmpty()) { + // 分批插入,每批500条 + int batchSize = 500; + for (int i = 0; i < toInsert.size(); i += batchSize) { + int end = Math.min(i + batchSize, toInsert.size()); + List batch = toInsert.subList(i, end); + baseMapper.insertBatch(batch); + } + insertCount = toInsert.size(); + } + +// // 6. 批量更新 +// if (!toUpdate.isEmpty()) { +// // 分批更新,每批500条 +// int batchSize = 500; +// for (int i = 0; i < toUpdate.size(); i += batchSize) { +// int end = Math.min(i + batchSize, toUpdate.size()); +// List batch = toUpdate.subList(i, end); +// baseMapper.updateBatchById(batch, batch.size()); +// } +// updateCount = toUpdate.size(); +// } + Map result = new HashMap<>(); result.put("totalFetched", apiRows.size()); result.put("insertCount", insertCount);