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);