refactor(mes-excoil): 优化外部卷材数据同步性能(只插入不更新)
- 添加空数据检查并返回默认结果 - 实现批量数据转换提升处理效率 - 采用批量查询替代逐条查询减少数据库访问 - 分离新增和更新操作支持批量处理 - 实现分批插入和更新避免大数据量问题 - 每批处理500条记录确保系统稳定性
This commit is contained in:
@@ -155,28 +155,84 @@ public class MesExCoilServiceImpl implements IMesExCoilService {
|
|||||||
apiRows = fetchIncrementalFromApi(maxInsdate);
|
apiRows = fetchIncrementalFromApi(maxInsdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (apiRows == null || apiRows.isEmpty()) {
|
||||||
|
Map<String, Object> 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 insertCount = 0;
|
||||||
int updateCount = 0;
|
int updateCount = 0;
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
|
// 1. 批量转换所有数据
|
||||||
|
List<MesExCoil> allEntities = new ArrayList<>(apiRows.size());
|
||||||
for (Map<String, Object> row : apiRows) {
|
for (Map<String, Object> row : apiRows) {
|
||||||
MesExCoil entity = mapRowToEntity(row);
|
MesExCoil entity = mapRowToEntity(row);
|
||||||
entity.setSyncTime(now);
|
entity.setSyncTime(now);
|
||||||
|
allEntities.add(entity);
|
||||||
LambdaQueryWrapper<MesExCoil> 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++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// // 2. 收集所有 excoilid
|
||||||
|
// List<String> excoilids = allEntities.stream()
|
||||||
|
// .map(MesExCoil::getExcoilid)
|
||||||
|
// .filter(StringUtils::isNotBlank)
|
||||||
|
// .distinct()
|
||||||
|
// .collect(java.util.stream.Collectors.toList());
|
||||||
|
|
||||||
|
// // 3. 批量查询已存在的记录
|
||||||
|
// Map<String, MesExCoil> existingMap = new HashMap<>();
|
||||||
|
// if (!excoilids.isEmpty()) {
|
||||||
|
// LambdaQueryWrapper<MesExCoil> wrapper = Wrappers.lambdaQuery();
|
||||||
|
// wrapper.in(MesExCoil::getExcoilid, excoilids);
|
||||||
|
// List<MesExCoil> existingList = baseMapper.selectList(wrapper);
|
||||||
|
// existingMap = existingList.stream()
|
||||||
|
// .collect(java.util.stream.Collectors.toMap(MesExCoil::getExcoilid, v -> v, (a, b) -> a));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 4. 分离新增和更新的记录
|
||||||
|
List<MesExCoil> toInsert = new ArrayList<>();
|
||||||
|
// List<MesExCoil> 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<MesExCoil> 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<MesExCoil> batch = toUpdate.subList(i, end);
|
||||||
|
// baseMapper.updateBatchById(batch, batch.size());
|
||||||
|
// }
|
||||||
|
// updateCount = toUpdate.size();
|
||||||
|
// }
|
||||||
|
|
||||||
Map<String, Object> result = new HashMap<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
result.put("totalFetched", apiRows.size());
|
result.put("totalFetched", apiRows.size());
|
||||||
result.put("insertCount", insertCount);
|
result.put("insertCount", insertCount);
|
||||||
|
|||||||
Reference in New Issue
Block a user