liding
2 天以前 bd46f203b0d30a3d33b7c51ae9a528e4807477e1
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package com.ruoyi.basic.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.AddStandardTreeNodeDto;
import com.ruoyi.basic.dto.SampleTypeDto;
import com.ruoyi.basic.dto.StandardLibraryTreeNodeDto;
import com.ruoyi.basic.dto.StandardLibraryTreeRowDto;
import com.ruoyi.basic.dto.UpdateStandardTreeNodeDto;
import com.ruoyi.basic.dto.StandardTreeTemplateBindingDto;
import com.ruoyi.basic.dto.StandardTreeTemplateBindingVo;
import com.ruoyi.basic.dto.TemplateBindingItemDto;
import com.ruoyi.basic.dto.TemplateInspectionItemDto;
import com.ruoyi.basic.mapper.LaboratoryMapper;
import com.ruoyi.basic.mapper.StandardProductListMapper;
import com.ruoyi.basic.mapper.StandardTreeMapper;
import com.ruoyi.basic.mapper.StandardTreeTemplateBindingMapper;
import com.ruoyi.basic.mapper.StructureItemParameterMapper;
import com.ruoyi.basic.mapper.StructureTestObjectMapper;
import com.ruoyi.basic.pojo.Laboratory;
import com.ruoyi.basic.pojo.StandardProductList;
import com.ruoyi.basic.pojo.StandardTree;
import com.ruoyi.basic.pojo.StandardTemplate;
import com.ruoyi.basic.pojo.StandardTreeTemplateBinding;
import com.ruoyi.basic.pojo.StructureItemParameter;
import com.ruoyi.basic.pojo.StructureTestObject;
import com.ruoyi.basic.service.StandardTreeService;
import com.ruoyi.basic.service.StandardTemplateService;
import com.ruoyi.basic.utils.StandardTreePath;
import com.ruoyi.basic.utils.StandardTreePathParser;
import com.ruoyi.basic.utils.StandardTemplateInspectionItemParser;
import com.ruoyi.common.exception.base.BaseException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.HashSet;
import java.util.Comparator;
import java.util.Set;
 
/**
 * @author Administrator
 * @description 针对表【standard_tree(标准树)】的数据库操作Service实现
 * @createDate 2024-03-01 15:06:44
 */
@Service
@AllArgsConstructor
public class StandardTreeServiceImpl extends ServiceImpl<StandardTreeMapper, StandardTree>
        implements StandardTreeService {
 
 
    private StandardTreeMapper standardTreeMapper;
 
    private StandardProductListMapper standardProductListMapper;
 
    private StructureItemParameterMapper structureItemParameterMapper;
 
    private StructureTestObjectMapper structureTestObjectMapper;
 
    private LaboratoryMapper laboratoryMapper;
 
    private StandardTreeTemplateBindingMapper standardTreeTemplateBindingMapper;
 
    private StandardTemplateService standardTemplateService;
 
 
    @Override
    public List<StandardLibraryTreeNodeDto> selectStandardTreeList() {
        List<StandardLibraryTreeRowDto> rows = standardTreeMapper.selectStandardLibraryTree();
        List<StandardLibraryTreeNodeDto> industries = new ArrayList<>();
        Map<Integer, StandardLibraryTreeNodeDto> industryMap = new LinkedHashMap<>();
        for (StandardLibraryTreeRowDto row : rows) {
            StandardLibraryTreeNodeDto industry = industryMap.get(row.getIndustryId());
            if (industry == null) {
                industry = new StandardLibraryTreeNodeDto();
                industry.setId(row.getIndustryId());
                industry.setLabel(row.getIndustryName());
                industry.setValue(row.getIndustryName());
                industry.setCode("[1]");
                industry.setChildren(new ArrayList<>());
                industryMap.put(row.getIndustryId(), industry);
                industries.add(industry);
            }
            if (row.getStandardId() != null) {
                StandardLibraryTreeNodeDto standard = new StandardLibraryTreeNodeDto();
                standard.setId(row.getStandardId());
                standard.setLabel(row.getStandardCode());
                standard.setValue(row.getStandardLabel());
                standard.setCode("[2]");
                standard.setSampleEn(row.getStandardNameEn());
                standard.setStandardMethodId(row.getStandardMethodId());
                standard.setStandardCode(row.getStandardCode());
                standard.setStandardName(row.getStandardName());
                standard.setChildren(null);
                industry.getChildren().add(standard);
            }
        }
        return industries;
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addStandardTree(AddStandardTreeNodeDto dto) {
        if (dto == null) {
            throw new BaseException("标准树路径格式错误");
        }
        String name = StandardTreePathParser.normalizeNodeName(dto.getName());
        if (StringUtils.isBlank(dto.getParentTree())) {
            // 新增行业节点
            Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
                    .eq(Laboratory::getLaboratoryName, name));
            if (count > 0) {
                throw new BaseException("行业名称已存在");
            }
            Laboratory laboratory = new Laboratory();
            laboratory.setLaboratoryName(name);
            laboratory.setSort(0);
            return laboratoryMapper.insert(laboratory);
        }
        throw new BaseException("二级标准请在“标准维护”中通过行业绑定进行维护");
        /*
        // 在行业下新增标准节点
        StandardTreePath path = StandardTreePathParser.parse(dto.getParentTree());
        if (path.getStandard() != null) {
            throw new BaseException("标准节点下不能继续新增");
        }
        String industry = path.getIndustry();
        Long labCount = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
                .eq(Laboratory::getLaboratoryName, industry));
        if (labCount == 0) {
            throw new BaseException("行业不存在");
        }
        Long stdCount = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, industry)
                .eq(StandardTree::getSample, name)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel));
        if (stdCount > 0) {
            throw new BaseException("该行业下标准名称已存在");
        }
        StandardTree tree = new StandardTree();
        tree.setLaboratory(industry);
        tree.setSample(name);
        tree.setFactory(null);
        tree.setSampleType(null);
        tree.setModel(null);
        tree.setSort(0);
        return standardTreeMapper.insert(tree);
        */
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delStandardTree(String tree) {
        StandardTreePath path = StandardTreePathParser.parse(tree);
        String industry = path.getIndustry();
        String standard = path.getStandard();
        if (standard != null) {
            throw new BaseException("二级标准请在“标准维护”中通过行业绑定进行维护");
        }
        if (standard == null) {
            // 删除行业:行业下仍有标准时拒绝删除
            Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                    .eq(StandardTree::getLaboratory, industry)
                    .isNull(StandardTree::getSampleType)
                    .isNull(StandardTree::getModel));
            if (count > 0) {
                throw new BaseException("该行业下仍有标准,请先删除标准");
            }
            Laboratory laboratory = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
                    .eq(Laboratory::getLaboratoryName, industry));
            if (laboratory == null) {
                throw new BaseException("行业不存在");
            }
            Long objectCount = structureTestObjectMapper.selectCount(Wrappers.<StructureTestObject>lambdaQuery()
                    .eq(StructureTestObject::getLaboratoryId, laboratory.getId()));
            if (objectCount > 0) {
                throw new BaseException("该行业已绑定检测对象,禁止删除");
            }
            int deleted = laboratoryMapper.delete(Wrappers.<Laboratory>lambdaQuery()
                    .eq(Laboratory::getLaboratoryName, industry));
            if (deleted == 0) {
                throw new BaseException("行业不存在");
            }
            return deleted;
        }
        // 删除标准及其标准库配置(不可达,保留历史实现说明)
        StandardTree standardTree = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, industry)
                .eq(StandardTree::getSample, standard)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel));
        if (standardTree == null) {
            throw new BaseException("标准不存在");
        }
        standardTreeTemplateBindingMapper.delete(Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
                .eq(StandardTreeTemplateBinding::getStandardTreeId, standardTree.getId()));
        standardTreeMapper.deleteById(standardTree.getId());
        standardProductListMapper.delete(Wrappers.<StandardProductList>lambdaQuery()
                .eq(StandardProductList::getLaboratory, industry)
                .eq(StandardProductList::getSample, standard)
                .isNull(StandardProductList::getSampleType)
                .isNull(StandardProductList::getModel));
        return 1;
    }
 
    @Override
    public List<SampleTypeDto> getStandardTree2() {
        return standardTreeMapper.getStandardTree2();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upStandardProducts(Map<String, Object> product) {
        List<Integer> ids = JSON.parseArray(product.get("ids") + "");
        StandardProductList productList = JSON.parseObject(JSON.toJSONString(product.get("standardProductList")), StandardProductList.class);
        if (productList.getMethodS() != null) {
            standardProductListMapper.update(productList, Wrappers.<StandardProductList>lambdaUpdate().in(StandardProductList::getId, ids).like(StandardProductList::getMethod, productList.getMethodS()));
            return 1;
        }
        if (productList.getRadiusList() != null) {
            standardProductListMapper.update(productList, Wrappers.<StandardProductList>lambdaUpdate().in(StandardProductList::getId, ids).like(StandardProductList::getRadiusList, productList.getRadius()));
            return 1;
        }
        standardProductListMapper.update(productList, Wrappers.<StandardProductList>lambdaUpdate().in(StandardProductList::getId, ids));
        return 1;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTreeSort(List<StandardLibraryTreeNodeDto> list) {
        List<StandardTree> trees = new ArrayList<>();
        int industrySort = 0;
        for (StandardLibraryTreeNodeDto industry : list) {
            Laboratory lab = new Laboratory();
            lab.setId(industry.getId());
            lab.setSort(industrySort);
            laboratoryMapper.updateById(lab);
            industrySort++;
 
            List<StandardLibraryTreeNodeDto> children = industry.getChildren();
            if (children != null) {
                int standardSort = 0;
                for (StandardLibraryTreeNodeDto standard : children) {
                    StandardTree tree = new StandardTree();
                    tree.setId(standard.getId());
                    tree.setSort(standardSort);
                    trees.add(tree);
                    standardSort++;
                }
            }
        }
        if (!trees.isEmpty()) {
            this.updateBatchById(trees);
        }
        return true;
    }
 
    /**
     * 修改标准树节点名
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStandardTree(UpdateStandardTreeNodeDto dto) {
        if (dto == null) {
            throw new BaseException("标准树路径格式错误");
        }
        String name = StandardTreePathParser.normalizeNodeName(dto.getName());
        String oldLabel = StandardTreePathParser.normalizeNodeName(dto.getOldLabel());
        if (StringUtils.isBlank(dto.getParentTree())) {
            return updateIndustry(oldLabel, name);
        }
        throw new BaseException("二级标准请在“标准维护”中通过行业绑定进行维护");
    }
 
    private int updateIndustry(String oldName, String newName) {
        if (Objects.equals(oldName, newName)) {
            return ensureIndustryExists(oldName);
        }
        Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
                .eq(Laboratory::getLaboratoryName, newName));
        if (count > 0) {
            throw new BaseException("行业名称已存在");
        }
        Laboratory old = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
                .eq(Laboratory::getLaboratoryName, oldName));
        if (old == null) {
            throw new BaseException("行业不存在");
        }
        Laboratory update = new Laboratory();
        update.setId(old.getId());
        update.setLaboratoryName(newName);
        if (laboratoryMapper.updateById(update) == 0) {
            throw new BaseException("行业不存在");
        }
        // 级联标准树规范化记录
        standardTreeMapper.update(null, Wrappers.<StandardTree>lambdaUpdate()
                .eq(StandardTree::getLaboratory, oldName)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel)
                .set(StandardTree::getLaboratory, newName));
        // 级联标准库配置
        standardTreeMapper.updateProductListIndustry(oldName, newName);
        // 级联基础检验项
        structureItemParameterMapper.update(null, Wrappers.<StructureItemParameter>lambdaUpdate()
                .eq(StructureItemParameter::getLaboratory, oldName)
                .set(StructureItemParameter::getLaboratory, newName));
        // 级联未完成订单
        standardTreeMapper.updateInsOrderIndustry(oldName, newName);
        standardTreeMapper.updateInsSampleIndustry(oldName, newName);
        standardTreeMapper.updateInsProductIndustry(oldName, newName);
        return 1;
    }
 
    private int updateStandard(String industry, String oldName, String newName) {
        if (Objects.equals(oldName, newName)) {
            return ensureStandardExists(industry, oldName);
        }
        Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, industry)
                .eq(StandardTree::getSample, newName)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel));
        if (count > 0) {
            throw new BaseException("该行业下标准名称已存在");
        }
        StandardTree old = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, industry)
                .eq(StandardTree::getSample, oldName)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel));
        if (old == null) {
            throw new BaseException("标准不存在");
        }
        StandardTree update = new StandardTree();
        update.setId(old.getId());
        update.setSample(newName);
        if (standardTreeMapper.updateById(update) == 0) {
            throw new BaseException("标准不存在");
        }
        // 级联标准库配置
        standardTreeMapper.updateProductListStandard(industry, oldName, newName);
        // 级联未完成订单
        standardTreeMapper.updateInsOrderStandard(industry, oldName, newName);
        standardTreeMapper.updateInsSampleStandard(industry, oldName, newName);
        standardTreeMapper.updateInsProductStandard(industry, oldName, newName);
        return 1;
    }
 
    private int ensureIndustryExists(String industry) {
        Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
                .eq(Laboratory::getLaboratoryName, industry));
        if (count == 0) {
            throw new BaseException("行业不存在");
        }
        return 1;
    }
 
    private int ensureStandardExists(String industry, String standard) {
        Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, industry)
                .eq(StandardTree::getSample, standard)
                .isNull(StandardTree::getSampleType)
                .isNull(StandardTree::getModel));
        if (count == 0) {
            throw new BaseException("标准不存在");
        }
        return 1;
    }
 
    @Override
    public List<StandardTreeTemplateBindingVo> selectTemplateBindings(Integer standardTreeId) {
        ensureTwoLevelStandard(standardTreeId);
        List<StandardTreeTemplateBinding> bindings = standardTreeTemplateBindingMapper.selectList(
                Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
                        .eq(StandardTreeTemplateBinding::getStandardTreeId, standardTreeId)
                        .orderByAsc(StandardTreeTemplateBinding::getSort, StandardTreeTemplateBinding::getId));
        List<StandardTreeTemplateBindingVo> result = new ArrayList<>();
        for (StandardTreeTemplateBinding binding : bindings) {
            StandardTemplate template = standardTemplateService.getById(binding.getTemplateId());
            if (template == null) {
                continue;
            }
            StandardTreeTemplateBindingVo vo = new StandardTreeTemplateBindingVo();
            vo.setTemplateId(template.getId());
            vo.setName(template.getName());
            vo.setNumber(template.getNumber());
            vo.setRemark(template.getRemark());
            vo.setSort(binding.getSort());
            result.add(vo);
        }
        return result;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTemplateBindings(StandardTreeTemplateBindingDto dto) {
        if (dto == null || dto.getStandardTreeId() == null) {
            throw new BaseException("标准不能为空");
        }
        ensureTwoLevelStandard(dto.getStandardTreeId());
        List<TemplateBindingItemDto> templates = dto.getTemplates() == null ? new ArrayList<>() : dto.getTemplates();
        Set<Integer> templateIds = new HashSet<>();
        List<StandardTreeTemplateBinding> bindings = new ArrayList<>();
        int index = 0;
        for (TemplateBindingItemDto item : templates) {
            if (item == null || item.getTemplateId() == null || !templateIds.add(item.getTemplateId())) {
                throw new BaseException("原始记录模板不能重复绑定");
            }
            StandardTemplate template = standardTemplateService.getById(item.getTemplateId());
            if (template == null) {
                throw new BaseException("原始记录模板不存在");
            }
            StandardTreeTemplateBinding binding = new StandardTreeTemplateBinding();
            binding.setStandardTreeId(dto.getStandardTreeId());
            binding.setTemplateId(item.getTemplateId());
            binding.setSort(item.getSort() == null ? index : item.getSort());
            bindings.add(binding);
            index++;
        }
        standardTreeTemplateBindingMapper.delete(Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
                .eq(StandardTreeTemplateBinding::getStandardTreeId, dto.getStandardTreeId()));
        for (StandardTreeTemplateBinding binding : bindings) {
            standardTreeTemplateBindingMapper.insert(binding);
        }
        return true;
    }
 
    @Override
    public List<TemplateInspectionItemDto> selectTemplateInspectionItems(Integer standardTreeId) {
        List<StandardTreeTemplateBindingVo> bindings = selectTemplateBindings(standardTreeId);
        List<TemplateInspectionItemDto> result = new ArrayList<>();
        for (StandardTreeTemplateBindingVo binding : bindings) {
            List<TemplateInspectionItemDto> items = StandardTemplateInspectionItemParser.parse(binding.getTemplateId(),
                    binding.getSort(), standardTemplateService.getStandTempThingById(binding.getTemplateId()));
            result.addAll(items);
        }
        result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateSort)
                .thenComparing(TemplateInspectionItemDto::getTemplateRowIndex));
        return result;
    }
 
    private StandardTree ensureTwoLevelStandard(Integer standardTreeId) {
        StandardTree standardTree = this.getById(standardTreeId);
        if (standardTree == null || StringUtils.isBlank(standardTree.getSample())
                || standardTree.getSampleType() != null || standardTree.getModel() != null) {
            throw new BaseException("请选择标准库二级标准");
        }
        return standardTree;
    }
}