liding
2025-04-25 6c8973d78b04b1aa132dccbd478ba8abbcf2b6c1
inspect-server/src/main/java/com/ruoyi/inspect/service/impl/ReliabilityPlanProductItemServiceImpl.java
@@ -9,8 +9,11 @@
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;
/**
 * <p>
@@ -27,8 +30,76 @@
    private ReliabilityPlanProductItemMapper reliabilityPlanProductItemMapper;
    @Override
    public List<ReliabilityPlanProductItem> selectProductItem() {
        return reliabilityPlanProductItemMapper.selectList(new LambdaQueryWrapper<>());
    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>()
                        .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());
    }
    @Override
@@ -50,4 +121,14 @@
    public List<Map<String, Object>> codeList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) {
        return reliabilityPlanProductItemMapper.codeList(reliabilityPlanProductItemDto.getRePlanId());
    }
    @Override
    public List<Map<String, Object>> materialItem(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) {
        return reliabilityPlanProductItemMapper.materialItem(reliabilityPlanProductItemDto.getRePlanId());
    }
    @Override
    public List<Map<String, Object>> materialCodeList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) {
        return reliabilityPlanProductItemMapper.materialCodeList(reliabilityPlanProductItemDto.getRePlanId());
    }
}