liding
2025-04-25 6c8973d78b04b1aa132dccbd478ba8abbcf2b6c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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;
 
/**
 * <p>
 * 可靠性计划产品子项 服务实现类
 * </p>
 *
 * @author ld
 * @since 2025-04-01 05:12:38
 */
@Service
@AllArgsConstructor
public class ReliabilityPlanProductItemServiceImpl extends ServiceImpl<ReliabilityPlanProductItemMapper, ReliabilityPlanProductItem> implements ReliabilityPlanProductItemService {
 
    private ReliabilityPlanProductItemMapper reliabilityPlanProductItemMapper;
 
    @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>()
                        .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
    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<Map<String, Object>> itemList(ReliabilityPlanProductItemDto reliabilityPlanProductItemDto) {
        return reliabilityPlanProductItemMapper.itemList(reliabilityPlanProductItemDto.getRePlanId());
    }
 
    @Override
    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());
    }
}