| | |
| | | |
| | | @Override |
| | | public List<ReliabilityPlanProductItem> selectProductItem(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { |
| | | // 从数据库中查询新的数据 |
| | | List<Map<String, Object>> maps = reliabilityPlanProductItemMapper.itemList(reliabilityPlanProductItemDto.getRePlanId()); |
| | | List<ReliabilityPlanProductItem> newItemList = new ArrayList<>(); |
| | | for (Map<String, Object> map : maps) { |
| | | ReliabilityPlanProductItem item = new ReliabilityPlanProductItem(); |
| | | item.setInspectionItem((String) map.get("inspectionItem")); |
| | | item.setStandard((String) map.get("standard")); |
| | | item.setFrequency((String) map.get("frequency")); |
| | | item.setPlanId(reliabilityPlanProductItemDto.getRePlanId()); |
| | | item.setType(reliabilityPlanProductItemDto.getType()); |
| | | newItemList.add(item); |
| | | } |
| | | |
| | | // 查询数据库中已存在的数据 |
| | | List<ReliabilityPlanProductItem> existingItemList = reliabilityPlanProductItemMapper.selectList( |
| | | new LambdaQueryWrapper<ReliabilityPlanProductItem>() |
| | | return reliabilityPlanProductItemMapper.selectList(new LambdaQueryWrapper<ReliabilityPlanProductItem>() |
| | | .eq(ReliabilityPlanProductItem::getPlanId, reliabilityPlanProductItemDto.getRePlanId()) |
| | | .eq(ReliabilityPlanProductItem::getType, reliabilityPlanProductItemDto.getType()) |
| | | ); |
| | | |
| | | // 将已存在的数据转换为以 inspectionItem_planId_type 为键的 Map |
| | | Map<String, ReliabilityPlanProductItem> existingItemMap = new HashMap<>(); |
| | | for (ReliabilityPlanProductItem item : existingItemList) { |
| | | StringBuilder keyBuilder = new StringBuilder(); |
| | | keyBuilder.append(item.getInspectionItem()) |
| | | .append("_") |
| | | .append(item.getPlanId()) |
| | | .append("_") |
| | | .append(item.getType()); |
| | | existingItemMap.put(keyBuilder.toString(), item); |
| | | } |
| | | |
| | | List<ReliabilityPlanProductItem> itemsToUpdate = new ArrayList<>(); |
| | | List<ReliabilityPlanProductItem> itemsToInsert = new ArrayList<>(); |
| | | |
| | | // 对比新数据和已存在的数据,区分出需要更新和需要新增的记录 |
| | | for (ReliabilityPlanProductItem newItem : newItemList) { |
| | | StringBuilder keyBuilder = new StringBuilder(); |
| | | keyBuilder.append(newItem.getInspectionItem()) |
| | | .append("_") |
| | | .append(newItem.getPlanId()) |
| | | .append("_") |
| | | .append(newItem.getType()); |
| | | String key = keyBuilder.toString(); |
| | | ReliabilityPlanProductItem existingItem = existingItemMap.get(key); |
| | | if (existingItem != null) { |
| | | // 存在则更新 |
| | | existingItem.setStandard(newItem.getStandard()); |
| | | existingItem.setFrequency(newItem.getFrequency()); |
| | | itemsToUpdate.add(existingItem); |
| | | } else { |
| | | // 不存在则新增 |
| | | itemsToInsert.add(newItem); |
| | | } |
| | | } |
| | | |
| | | // 执行更新和新增操作 |
| | | if (!itemsToUpdate.isEmpty()) { |
| | | saveOrUpdateBatch(itemsToUpdate); |
| | | } |
| | | if (!itemsToInsert.isEmpty()) { |
| | | saveBatch(itemsToInsert); |
| | | } |
| | | |
| | | // 可以考虑在前面的查询中保留需要的结果,避免再次查询 |
| | | return existingItemList.stream() |
| | | .filter(item -> item.getPlanId().equals(reliabilityPlanProductItemDto.getRePlanId()) |
| | | && item.getType().equals(reliabilityPlanProductItemDto.getType())) |
| | | .collect(Collectors.toList()); |
| | | .eq(ReliabilityPlanProductItem::getType, reliabilityPlanProductItemDto.getType())); |
| | | } |
| | | |
| | | @Override |