package com.ruoyi.inspect.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ruoyi.inspect.dto.ReliabilityPlanProductItemDto; import com.ruoyi.inspect.pojo.ReliabilityPlanProductItem; import com.ruoyi.inspect.mapper.ReliabilityPlanProductItemMapper; import com.ruoyi.inspect.service.ReliabilityPlanProductItemService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 可靠性计划产品子项 服务实现类 *

* * @author ld * @since 2025-04-01 05:12:38 */ @Service @AllArgsConstructor public class ReliabilityPlanProductItemServiceImpl extends ServiceImpl implements ReliabilityPlanProductItemService { private ReliabilityPlanProductItemMapper reliabilityPlanProductItemMapper; @Override public List selectProductItem(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { // 从数据库中查询新的数据 List> maps = reliabilityPlanProductItemMapper.itemList(reliabilityPlanProductItemDto.getRePlanId()); List newItemList = new ArrayList<>(); for (Map 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 existingItemList = reliabilityPlanProductItemMapper.selectList( new LambdaQueryWrapper() .eq(ReliabilityPlanProductItem::getPlanId, reliabilityPlanProductItemDto.getRePlanId()) .eq(ReliabilityPlanProductItem::getType, reliabilityPlanProductItemDto.getType()) ); // 将已存在的数据转换为以 inspectionItem_planId_type 为键的 Map Map 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 itemsToUpdate = new ArrayList<>(); List 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()); } @Override public int addOrUpdateItem(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { if (reliabilityPlanProductItemDto.getId() == null) { reliabilityPlanProductItemDto.setPlanId(reliabilityPlanProductItemDto.getRePlanId()); return reliabilityPlanProductItemMapper.insert(reliabilityPlanProductItemDto); } else { return reliabilityPlanProductItemMapper.updateById(reliabilityPlanProductItemDto); } } @Override public List> itemList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { return reliabilityPlanProductItemMapper.itemList(reliabilityPlanProductItemDto.getRePlanId()); } @Override public List> codeList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { return reliabilityPlanProductItemMapper.codeList(reliabilityPlanProductItemDto.getRePlanId()); } @Override public List> materialItem(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { return reliabilityPlanProductItemMapper.materialItem(reliabilityPlanProductItemDto.getRePlanId()); } @Override public List> materialCodeList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) { return reliabilityPlanProductItemMapper.materialCodeList(reliabilityPlanProductItemDto.getRePlanId()); } }