chenrui
2025-03-10 44daca093696d96e822df6584866910f7316863b
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.dto.InsProductDto;
import com.ruoyi.inspect.mapper.InsOrderMapper;
import com.ruoyi.inspect.mapper.InsOrderStateMapper;
import com.ruoyi.inspect.mapper.InsProductMapper;
import com.ruoyi.inspect.mapper.InsSampleMapper;
import com.ruoyi.inspect.pojo.InsOrder;
import com.ruoyi.inspect.pojo.InsOrderState;
import com.ruoyi.inspect.pojo.InsProduct;
import com.ruoyi.inspect.pojo.InsSample;
import com.ruoyi.inspect.service.InsProductService;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@AllArgsConstructor
public class InsProductServiceImpl extends ServiceImpl<InsProductMapper, InsProduct>
        implements InsProductService {
 
    private InsProductMapper insProductMapper;
 
    private InsOrderMapper insOrderMapper;
 
    private InsOrderStateMapper insOrderStateMapper;
 
    private InsSampleMapper insSampleMapper;
 
 
    @Override
    public int selectOrderManDay(Integer orderId) {
        return insProductMapper.selectOrderManDay(orderId);
    }
 
    @Override
    public int updateInspected(Integer id, String ids) {
        InsOrder insOrder = new InsOrder();
        insOrder.setId(id);
        insOrder.setIsRevocation(1);//需要审核
        insOrder.setRevocationInsProductIds(ids);
        return insOrderMapper.updateById(insOrder);
    }
 
    @Override
    public boolean write(InsProductDto insProductDto) {
//        List<InsProduct> insProducts = baseMapper.selectBatchIds(insProductDto.getIds());
//        for (InsProduct insProduct : insProducts) {
//            insProduct.setTemperature(insProductDto.getTemperature());
//            insProduct.setHumidity(insProductDto.getHumidity());
//        }
        List<Integer> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                .eq(InsSample::getInsOrderId, insProductDto.getInsOrderId())
                .select(InsSample::getId))
                .stream()
                .map(InsSample::getId)
                .collect(Collectors.toList());
//        Integer insSampleId = baseMapper.selectById(insProductDto.getIds().get(0)).getInsSampleId();
        return baseMapper.update(null, new LambdaUpdateWrapper<InsProduct>().in(InsProduct::getInsSampleId, insSamples)
                .set(Strings.isNotBlank(insProductDto.getTemperature()),InsProduct::getTemperature, insProductDto.getTemperature())
                .set(Strings.isNotBlank(insProductDto.getHumidity()),InsProduct::getHumidity, insProductDto.getHumidity()))>0;
    }
 
    //查询待检项目
    @Override
    public Map<String, Object> selectNoProducts(Page page, InsProduct insProduct, Integer orderId, String ids) {
        Map<String, Object> map = new HashMap<>();
//        map.put("head", PrintChina.printChina(InsProduct.class));
        IPage<InsProduct> insProductIPage = baseMapper.selectNoProducts(page, orderId);
        if (ObjectUtils.isNotEmpty(ids)) {
            List<Integer> collect = Arrays.asList(ids.split(",")).stream()
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            List<InsProduct> products = insProductIPage.getRecords().stream().filter(record -> collect.contains(record.getId())).collect(Collectors.toList());
            insProductIPage.setRecords(products);
        }
        map.put("body", insProductIPage);
        return map;
    }
 
 
    // 查询撤销项目
    public List<List<Integer>> selectRevocation(Integer orderId) {
        // 查询撤销状态的样品 以及 检验项
        String revocationInsProductIds = insOrderMapper.selectById(orderId).getRevocationInsProductIds();
        List<Integer> productIds = new ArrayList<>();
        List<Integer> sampleIds = new ArrayList<>();
        // 如果是新的数据 会存在sample的id 以及product的id 以@分隔
        if(StringUtils.isNotEmpty(revocationInsProductIds)) {
            if(revocationInsProductIds.contains("@")) {
                List<List<Integer>> collect = Arrays.stream(revocationInsProductIds.split("@")).map(item -> {
                    List<Integer> list = JSONArray.parseArray(item, Integer.class);
                    return list;
                }).collect(Collectors.toList());
                sampleIds =  collect.get(0);
                productIds = collect.get(1);
            }else {
                // 以前的数据则是productId,以 ,分隔
                productIds = Arrays.stream(revocationInsProductIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());
            }
        }
        return Arrays.asList(sampleIds,productIds);
    }
 
    @Override
    public List<Map<String,Object>> selectSampleByOrderId(Integer orderId) {
        List<List<Integer>> collect = selectRevocation(orderId);
        List<Integer> sampleIds = collect.get(0);
        List<Integer> productIds = collect.get(1);
 
 
        List<Map<String,Object>> list = new ArrayList<>();
        List<InsSample> insSamples = insSampleMapper.selectList(new LambdaQueryWrapper<InsSample>()
                .eq(InsSample::getInsOrderId, orderId)
                .notIn(CollectionUtils.isNotEmpty(sampleIds), InsSample::getId,sampleIds));
        for(InsSample insSample : insSamples) {
            List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getInsSampleId, insSample.getId())
                    .eq(InsProduct::getState, 1)
                    .notIn(CollectionUtils.isNotEmpty(productIds), InsProduct::getId,productIds));
            if(CollectionUtils.isNotEmpty(productList)) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("sampleCode", insSample.getSampleCode()); // 样品编号
                map.put("sampleId", insSample.getId()); // 前端需要
                map.put("id", insSample.getId());
                map.put("hasChildren", true); // 是否有子项
                list.add(map);
            }
        }
        return list;
    }
 
    @Override
    public List<Map<String,Object>> selectProductsBySampleId(Integer sampleId,Integer orderId) {
        List<List<Integer>> collect1 = selectRevocation(orderId);
        List<Integer> sampleIds = collect1.get(0);
        List<Integer> productIds = collect1.get(1);
 
        List<Map<String, Object>> list = new ArrayList<>();
        if(!sampleIds.contains(sampleId)) {
            InsSample insSample = insSampleMapper.selectById(sampleId);
            List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getInsSampleId, insSample.getId())
                    .eq(InsProduct::getState,1)
                    .notIn(CollectionUtils.isNotEmpty(productIds), InsProduct::getId,productIds));
            for (InsProduct insProduct : productList) {
                // 区分温度循环 和 普通项目
                if(Objects.isNull(insProduct.getSpecialItemParentId()) || !NumberUtil.isInteger(insProduct.getInspectionItem())) {
                    Map<String, Object> map = new HashMap<>();
                    // 如果是温度循环就还得在多嵌套几层
                    if(insProduct.getInspectionItem().equals("温度循环")) {
                        map.put("sampleCode", insSample.getSampleCode()); // 样品编号
                        map.put("inspectionItem",insProduct.getInspectionItem()); // 检验项目
                        map.put("sampleId", insSample.getId()); // 样品id
                        map.put("hasChildren",true);
                        // 温度循环特殊标识
                        map.put("temperature",true);
                        map.put("id",insProduct.getId());
                    }else {
                        map.put("sampleId", insSample.getId()); // 样品id
                        map.put("sampleCode", insSample.getSampleCode()); // 样品编号
                        map.put("inspectionItem",insProduct.getInspectionItem()); // 检验项目
 
                        map.put("inspectionItemSubclass",insProduct.getInspectionItemSubclass()); // 检验子项
                        map.put("sampleType",insProduct.getSampleType()); // 样品分类
                        map.put("sample",insProduct.getSample()); // 样品
                        map.put("model",insProduct.getModel()); // 型号
                        map.put("sonLaboratory",insProduct.getSonLaboratory()); // 子实验室
                        map.put("tell",insProduct.getTell()); // 要求值
                        map.put("id",insProduct.getId());
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }
 
    @Override
    public List<Map<String, Object>> selectTemperatureByProductId(Integer productId) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<InsProduct> productList = null;
         // 已经到撤销页面的就不在查出来
        InsProduct insProduct = insProductMapper.selectById(productId);
        InsSample insSample = insSampleMapper.selectById(insProduct.getInsSampleId());
        InsOrder insOrder = insOrderMapper.selectById(insSample.getInsOrderId());
        List<List<Integer>> lists = selectRevocation(insOrder.getId());
        List<Integer> productIds = lists.get(1);
 
        productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                .eq(InsProduct::getState, 1)
                .eq(InsProduct::getSpecialItemParentId, productId)
                .notIn(CollectionUtils.isNotEmpty(productIds),InsProduct::getId,productIds));
 
 
        if(CollectionUtils.isNotEmpty(productList)) {
            Map<String, List<InsProduct>> collect = productList.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItem));
            // 拿到循环次数
            Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, List<InsProduct>> next = iterator.next();
                Map<String, Object> map = new HashMap<>();
                map.put("hasChildren",true);
                // 温度循环特殊标识 循环次数
                map.put("temperatureNumber",true);
                map.put("sampleId", next.getValue().get(0).getInsSampleId()); // 样品id
                map.put("inspectionItem",next.getKey() + " (循环次数)"); // 检验项目
                map.put("inspectionItemTwo",next.getKey()); // 方便查看具体次数 (传值需要)
                map.put("parentId",productId); // 父id
                map.put("ids",next.getValue().stream().map(InsProduct::getId).collect(Collectors.toList()));
                map.put("sampleCode", next.getValue().get(0).getSampleCode()); // 样品编号
                map.put("id",productId + Integer.parseInt(next.getKey()));
                list.add(map);
            }
        }
        return list;
    }
 
    @Override
    public List<Map<String, Object>> selectTemperatureNumberByProductId(Integer parentId, String inspectionItem) {
        List<Map<String, Object>> list = new ArrayList<>();
        // 已经到撤销页面的就不在查出来
        InsProduct ip = insProductMapper.selectById(parentId);
        InsSample insSample = insSampleMapper.selectById(ip.getInsSampleId());
        InsOrder insOrder = insOrderMapper.selectById(insSample.getInsOrderId());
        List<List<Integer>> lists = selectRevocation(insOrder.getId());
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
 
 
        List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                .eq(InsProduct::getState, 1)
                .eq(InsProduct::getSpecialItemParentId, parentId)
                .eq(InsProduct::getInspectionItem, inspectionItem)
                .notIn(CollectionUtils.isNotEmpty(productIds),InsProduct::getId,productIds));
        // 根据检验子项得到温度点
        Map<String, List<InsProduct>> collect = productList.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItemSubclass));
        Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<InsProduct>> next = iterator.next();
            InsProduct insProduct = next.getValue().get(0);
            Map<String, Object> map = new HashMap<>();
            map.put("sampleId", next.getValue().get(0).getInsSampleId()); // 样品id
            map.put("inspectionItem",""); // 检验项目
            map.put("inspectionItemSubclass",next.getKey()); // 检验子项
            map.put("sampleType",insProduct.getSampleType()); // 样品分类
            map.put("sample",insProduct.getSample()); // 样品
            map.put("model",insProduct.getModel()); // 型号
            map.put("sonLaboratory",insProduct.getSonLaboratory()); // 子实验室
            map.put("sampleCode", next.getValue().get(0).getSampleCode()); // 样品编号
            map.put("tell",insProduct.getTell()); // 要求值
            map.put("number",inspectionItem); // 循环次数
            map.put("parentId",parentId); // 父id
            map.put("ids",next.getValue().stream().map(InsProduct::getId).collect(Collectors.toList())); // 该温度点所有的id
            map.put("id",Math.random());
            list.add(map);
        }
        return list;
    }
 
 
    @Override
    public void revoke(Map<String, Object> data) {
        List<List<Integer>> lists = selectRevocation(Integer.parseInt(data.get("orderId").toString()));
        // 更新order的状态 以及添加需要撤销的项目
        Integer orderId = Integer.parseInt(data.get("orderId").toString());
        InsOrder insOrder = insOrderMapper.selectById(orderId);
        insOrder.setIsRevocation(1);
        List<Integer> sampleIds = new ArrayList<>();
        List<Integer> productIds = new ArrayList<>();
        if(data.containsKey("sampleIds")) {
            sampleIds = JSONArray.parseArray(data.get("sampleIds").toString(), Integer.class);
        }
        if(data.containsKey("productIds")) {
            productIds = JSONArray.parseArray(data.get("productIds").toString(), Integer.class);
 
        }
        sampleIds.addAll(lists.get(0));
        productIds.addAll(lists.get(1));
        String SampleJsonString = JSON.toJSONString(sampleIds);
        String ProductJsonString = JSON.toJSONString(productIds);
        insOrder.setRevocationInsProductIds(SampleJsonString + "@" + ProductJsonString);
        insOrderMapper.updateById(insOrder);
 
    }
 
    @Override
    public List<Map<String, Object>> revokeReviewSample(Integer orderId) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<List<Integer>> lists = selectRevocation(orderId);
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
        List<Integer> newSampleIds = new ArrayList<>();
        // 根据项目来获取样品
        if(CollectionUtils.isNotEmpty(productIds)) {
            newSampleIds = insProductMapper.selectBatchIds(productIds).stream().map(InsProduct::getInsSampleId).distinct().collect(Collectors.toList());
        }
        // 查询需要撤销的样品
        if(CollectionUtils.isNotEmpty(newSampleIds)) {
            List<InsSample> insSamples = insSampleMapper.selectList(new LambdaQueryWrapper<InsSample>()
                    .in(InsSample::getId, newSampleIds));
 
            for (InsSample insSample : insSamples) {
                List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                        .eq(InsProduct::getInsSampleId, insSample.getId())
                        .eq(InsProduct::getState, 1)
                        .in(CollectionUtils.isNotEmpty(productIds), InsProduct::getId, productIds));
                if(CollectionUtils.isNotEmpty(productList)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("sampleCode", insSample.getSampleCode()); // 样品编号
                    map.put("sampleId", insSample.getId()); // 前端需要
                    map.put("id", insSample.getId());
                    map.put("hasChildren", true); // 是否有子项
                    list.add(map);
                }
            }
        }
        if(CollectionUtils.isNotEmpty(sampleIds)) {
            List<InsSample> insSamples2 = insSampleMapper.selectList(new LambdaQueryWrapper<InsSample>()
                    .in(InsSample::getId, sampleIds));
            for (InsSample insSample : insSamples2) {
                List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                        .eq(InsProduct::getInsSampleId, insSample.getId())
                        .eq(InsProduct::getState, 1));
                if(CollectionUtils.isNotEmpty(productList)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("sampleCode", insSample.getSampleCode()); // 样品编号
                    map.put("sampleId", insSample.getId()); // 前端需要
                    map.put("id", insSample.getId());
                    map.put("hasChildren", true); // 是否有子项
                    list.add(map);
                }
            }
        }
 
        list = list.stream().sorted(Comparator.comparing(map -> Integer.parseInt(map.get("id")+""))).collect(Collectors.toList());
        return list;
    }
 
 
 
    @Override
    public List<Map<String, Object>> revokeReviewProduct(Integer orderId, Integer sampleId) {
        String sampleCode = insSampleMapper.selectById(sampleId).getSampleCode();
        List<Map<String,Object>> list = new ArrayList<>();
        List<List<Integer>> lists = selectRevocation(orderId);
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
        List<InsProduct> productList = new ArrayList<>();
        // 如果是一整个样品撤销
        if(sampleIds.contains(sampleId)) {
            // 过滤出温度循环次数的数据
             productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getInsSampleId, sampleId).eq(InsProduct::getState,1));
             productList = productList.stream().filter(item -> !NumberUtil.isNumber(item.getInspectionItem()) && Objects.isNull(item.getSpecialItemParentId())).collect(Collectors.toList());
        }else {
            // 如果是单个项目撤销
            productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getInsSampleId, sampleId)
                    .eq(InsProduct::getState,1)
                    .in(InsProduct::getId, productIds));
            // 判断是否是温度循环下面的温度点
            // 1.过滤出不是温度点的数据
            List<InsProduct> collect = productList.stream().filter(item -> Objects.isNull(item.getSpecialItemParentId())
                    || !NumberUtil.isInteger(item.getInspectionItem())).collect(Collectors.toList());
            if(productList.size() != collect.size()) {
                // 2.返回温度循环数据即可
                List<InsProduct> collect1 = productList.stream().filter(item -> Objects.nonNull(item.getSpecialItemParentId())
                        && NumberUtil.isInteger(item.getInspectionItem())).collect(Collectors.toList());
                InsProduct insProduct = insProductMapper.selectById(collect1.get(0).getSpecialItemParentId());
                collect.add(insProduct);
                productList =collect.stream().distinct().collect(Collectors.toList());
            }
 
        }
        for (InsProduct insProduct : productList) {
            Map<String, Object> map = new HashMap<>();
            if(insProduct.getInspectionItem().equals("温度循环")) {
                map.put("sampleCode", sampleCode); // 样品编号
                map.put("inspectionItem",insProduct.getInspectionItem()); // 检验项目
                map.put("sampleId", sampleId); // 样品id
                map.put("hasChildren",true);
                // 温度循环特殊标识
                map.put("temperature",true);
                map.put("id",insProduct.getId());
            }else {
                map.put("sampleId", sampleId); // 样品id
                map.put("sampleCode", sampleCode); // 样品编号
                map.put("inspectionItem",insProduct.getInspectionItem()); // 检验项目
                map.put("inspectionItemSubclass",insProduct.getInspectionItemSubclass()); // 检验子项
                map.put("sampleType",insProduct.getSampleType()); // 样品分类
                map.put("sample",insProduct.getSample()); // 样品
                map.put("model",insProduct.getModel()); // 型号
                map.put("sonLaboratory",insProduct.getSonLaboratory()); // 子实验室
                map.put("tell",insProduct.getTell()); // 要求值
                map.put("id",insProduct.getId());
            }
            list.add(map);
        }
        return list;
    }
 
 
    @Override
    public List<Map<String, Object>> revokeReviewTemperatureByProductId(Integer productId) {
        // 根据温度循环id 查找order 以及 sample
        List<Map<String, Object>> list = new ArrayList<>();
        InsProduct insProduct = insProductMapper.selectById(productId);
        InsSample insSample = insSampleMapper.selectById(insProduct.getInsSampleId());
        InsOrder insOrder = insOrderMapper.selectById(insSample.getInsOrderId());
        List<List<Integer>> lists = selectRevocation(insOrder.getId());
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
        // 如果当前样品是整个撤销
        if(sampleIds.contains(insSample.getId())) {
            list = selectTemperatureByProductId(productId);
        }else {
            // 撤销的是检验项
            List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getState, 1)
                    .eq(InsProduct::getSpecialItemParentId,productId)
                    .in(InsProduct::getId, productIds));
            Map<String, List<InsProduct>> collect = productList.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItem));
            // 拿到循环次数
            Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, List<InsProduct>> next = iterator.next();
                Map<String, Object> map = new HashMap<>();
                map.put("hasChildren",true);
                // 温度循环特殊标识 循环次数
                map.put("temperatureNumber",true);
                map.put("sampleId", next.getValue().get(0).getInsSampleId()); // 样品id
                map.put("inspectionItem",next.getKey() + " (循环次数)"); // 检验项目
                map.put("inspectionItemTwo",next.getKey()); // 方便查看具体次数 (传值需要)
                map.put("parentId",productId); // 父id
                map.put("ids",next.getValue().stream().map(InsProduct::getId).collect(Collectors.toList()));
                map.put("sampleCode", next.getValue().get(0).getSampleCode()); // 样品编号
                map.put("id",productId + Integer.parseInt(next.getKey()));
                list.add(map);
            }
 
        }
 
 
        return list;
    }
 
    @Override
    public List<Map<String, Object>> revokeReviewConsistentByProductId(Integer parentId, String inspectionItem) {
        List<Map<String, Object>> list = new ArrayList<>();
        InsProduct insProduct = insProductMapper.selectById(parentId);
        InsSample insSample = insSampleMapper.selectById(insProduct.getInsSampleId());
        InsOrder insOrder = insOrderMapper.selectById(insSample.getInsOrderId());
        List<List<Integer>> lists = selectRevocation(insOrder.getId());
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
        // 如果是当前样品撤销 查询所有
        if(sampleIds.contains(insSample.getId())) {
             list = selectTemperatureNumberByProductId(parentId, inspectionItem);
        }else {
            // 查询撤销的温度点
            List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .eq(InsProduct::getState, 1)
                    .eq(InsProduct::getSpecialItemParentId, parentId)
                    .eq(InsProduct::getInspectionItem, inspectionItem)
                    .in(InsProduct::getId, productIds));
            // 根据检验子项得到温度点
            Map<String, List<InsProduct>> collect = productList.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItemSubclass));
            Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, List<InsProduct>> next = iterator.next();
                InsProduct ip = next.getValue().get(0);
                Map<String, Object> map = new HashMap<>();
                map.put("sampleId", next.getValue().get(0).getInsSampleId()); // 样品id
                map.put("inspectionItem",""); // 检验项目
                map.put("inspectionItemParent",inspectionItem + " (循环次数)"); // 检验父项目
                map.put("inspectionItemSubclass",next.getKey()); // 检验子项
                map.put("sampleType",ip.getSampleType()); // 样品分类
                map.put("sample",ip.getSample()); // 样品
                map.put("model",ip.getModel()); // 型号
                map.put("sonLaboratory",ip.getSonLaboratory()); // 子实验室
                map.put("sampleCode", next.getValue().get(0).getSampleCode()); // 样品编号
                map.put("tell",ip.getTell()); // 要求值
                map.put("parentId",parentId); // 父id
                map.put("ids",next.getValue().stream().map(InsProduct::getId).collect(Collectors.toList())); // 该温度点所有的id
                map.put("id",Math.random());
                list.add(map);
            }
        }
        return list;
    }
 
    @Override
    public void revocationPassed(Integer id) {
        List<List<Integer>> lists = selectRevocation(id);
        List<Integer> sampleIds = lists.get(0);
        List<Integer> productIds = lists.get(1);
        if(CollectionUtils.isNotEmpty(sampleIds)) {
            insProductMapper.update(null,new LambdaUpdateWrapper<InsProduct>()
                    .set(InsProduct::getState,0)
                    .in(InsProduct::getInsSampleId,sampleIds));
        }
        if(CollectionUtils.isNotEmpty(productIds)) {
            insProductMapper.update(null,new LambdaUpdateWrapper<InsProduct>()
                    .set(InsProduct::getState,0)
                    .in(InsProduct::getId,productIds));
            // 拿到的productId
            List<InsProduct> productList = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                    .in(InsProduct::getId, productIds));
            if(CollectionUtils.isNotEmpty(productList)) {
                // 得判断一下 是否含有温度循环项目
                List<InsProduct> collect2 = productList.stream().filter(item -> Objects.nonNull(item.getSpecialItemParentId())
                        && NumberUtil.isInteger(item.getInspectionItem())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(collect2)) {
                    // 就是有温度循环的
                    Map<Integer, List<InsProduct>> collect = productList.stream().collect(Collectors.groupingBy(InsProduct::getSpecialItemParentId));
                    // 获取温度循环的id
                    for(Map.Entry<Integer,List<InsProduct>> entry1 : collect.entrySet()) {
                        String number = "";
                        Integer parentId = entry1.getKey();
                        // 查询该温度循环 得到循环次数
                        List<InsProduct> productList2 = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                                .eq(InsProduct::getSpecialItemParentId, parentId)
                                .eq(InsProduct::getState, 1));
                        if(CollectionUtils.isNotEmpty(productList2)) {
                            Map<String, List<InsProduct>> group = productList2.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItem));
                            number = group.size() + "";
                        }
                        // 撤销的温度循环各个温度点
                        List<InsProduct> productList1 = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                                .eq(InsProduct::getState, 0)
                                .eq(InsProduct::getSpecialItemParentId, parentId)
                                .in(InsProduct::getId, productIds));
                        // 根据温度点进行分组
                        Map<String, List<InsProduct>> collect1 = productList1.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItemSubclass));
                        Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect1.entrySet().iterator();
                        while (iterator.hasNext()) {
                            Map.Entry<String, List<InsProduct>> entry = iterator.next();
                            String key = entry.getKey();
                            // 拿到温度循环的ask
                            InsProduct insProduct = insProductMapper.selectById(parentId);
                            String ask = insProduct.getAsk();
                            String newAsk = "";
                            String[] split = ask.split(";");
                            // 只有循环的次数 都不包含该温度点才需要更改
                            List<InsProduct> productList3 = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>()
                                    .eq(InsProduct::getSpecialItemParentId, parentId)
                                    .eq(InsProduct::getInspectionItemSubclass, key)
                                    .eq(InsProduct::getState, 1));
                            if(CollectionUtils.isEmpty(productList3)) {
                                if(key.equals("20℃")) {
                                    newAsk = Arrays.stream(split).filter(item -> (!item.contains(key) || item.contains("20℃(常温)")) && !NumberUtil.isInteger(item.trim())).collect(Collectors.joining(";"))  + ";" + number;
                                }else {
                                    newAsk = Arrays.stream(split).filter(item -> !item.contains(key) && !NumberUtil.isInteger(item)).collect(Collectors.joining(";"))  + ";" + number;
 
                                }
                            }else {
                                newAsk = Arrays.stream(split).filter(item -> !NumberUtil.isInteger(item)).collect(Collectors.joining(";")) + ";" + number;
                            }
                            insProductMapper.update(null, new LambdaUpdateWrapper<InsProduct>()
                                    .set(InsProduct::getAsk, newAsk)
                                    .eq(InsProduct::getId, parentId));
 
                        }
                    }
                }
            }
 
        }
        insOrderMapper.update(null,new LambdaUpdateWrapper<InsOrder>().set(InsOrder::getIsRevocation,0).eq(InsOrder::getId,id));
        // 如果检验项目全部撤销则需要撤销检验单
        InsOrder insOrder = insOrderMapper.selectById(id);
        insOrder.setRevocationTime(LocalDateTime.now());
        //查询该委托单下发的试验室
        List<InsOrderState> insOrderStates = insOrderStateMapper.selectList(Wrappers.<InsOrderState>lambdaQuery().eq(InsOrderState::getInsOrderId, id));
        List<InsSample> insSamples = insSampleMapper.selectList(new LambdaQueryWrapper<InsSample>().eq(InsSample::getInsOrderId, id));
        List<Integer> collect = insSamples.stream().map(InsSample::getId).collect(Collectors.toList());
        List<InsProduct> insProducts = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>().eq(InsProduct::getState, 1).in(InsProduct::getInsSampleId, collect));
        if(CollectionUtils.isEmpty(insProducts)) {
            insOrder.setState(3);
            insOrderMapper.updateById(insOrder);
        }else{
            //遍历各试验室,如果试验室下的项目为空,则删除该试验室下的订单
            insOrderStates.forEach(insOrderState -> {
                //过滤试验室下的项目
                long productCount = insProducts.stream().filter(insProduct -> insProduct.getSonLaboratory().equals(insOrderState.getLaboratory())).count();
                if(productCount == 0){
                    insOrderStateMapper.delete(new LambdaUpdateWrapper<InsOrderState>().eq(InsOrderState::getId, insOrderState.getId()));
                }
            });
        }
    }
 
    @Override
    public void revocationCancel(Integer orderId) {
        InsOrder insOrder = insOrderMapper.selectById(orderId);
        insOrder.setIsRevocation(0);
        insOrder.setRevocationInsProductIds("");
        insOrderMapper.updateById(insOrder);
    }
 
 
//    public List<Map<String,Object>> selectProductsByOrderId(Integer orderId) {
//        List<Map<String,Object>> list = new ArrayList<>();
//        List<InsSample> insSamples = insSampleMapper.selectList(new LambdaQueryWrapper<InsSample>().eq(InsSample::getInsOrderId, orderId));
//        if(CollectionUtils.isNotEmpty(insSamples)) {
//            for (InsSample insSample : insSamples) {
//                List<Map<String, Object>> lists = new ArrayList<>();
//                List<InsProduct> insProducts = insProductMapper.selectList(new LambdaQueryWrapper<InsProduct>().eq(InsProduct::getInsSampleId, insSample.getId()));
//                Map<String, Object> map = new HashMap<>();
//                map.put("label", insSample.getSampleCode());
//                map.put("value", insSample.getId());
//                if(CollectionUtils.isNotEmpty(insProducts)) {
//                    Map<String, List<InsProduct>> collect = insProducts.stream().collect(Collectors.groupingBy(InsProduct::getInspectionItem));
//                    Iterator<Map.Entry<String, List<InsProduct>>> iterator = collect.entrySet().iterator();
//                    while (iterator.hasNext()) {
//                        Map.Entry<String, List<InsProduct>> entry = iterator.next();
//                        lists.add(getInspectionItem(entry.getKey(), entry.getValue()));
//                    }
//                    map.put("children", lists);
//                }
//                list.add(map);
//            }
//        }
//        return list;
//    }
 
    public Map<String, Object> getInspectionItem(String inspectionItem, List<InsProduct> insProducts) {
        List<Integer> ids = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("label", inspectionItem);
        for (InsProduct insProduct : insProducts) {
            ids.add(insProduct.getId());
            list.add(getProductsSubclass(insProduct));
        }
        map.put("value", ids);
        map.put("children", list);
        return map;
    }
    public Map<String, Object> getProductsSubclass(InsProduct insProduct) {
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isNotEmpty(insProduct.getInspectionItemSubclass())) {
            map.put("label",insProduct.getInspectionItemSubclass());
        }else {
            map.put("label",insProduct.getInspectionItem());
        }
        map.put("value",insProduct.getId());
        return map;
    }
 
 
    //审核待检撤销
    @Override
    public void checkUpdate(Integer orderId, Integer state) {
        InsOrder insOrder = insOrderMapper.selectById(orderId);
        if (state == 1) {
            List<String> list = new ArrayList<>();
            try {
                list = Arrays.asList(insOrder.getRevocationInsProductIds().split(","));
            } catch (Exception e) {
                throw new ErrorException("还没有选择应该要撤销的检验项目");
            }
            List<Integer> ids = list.stream()
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, orderId));
            List<Integer> collect = insSamples.stream().map(InsSample::getId).collect(Collectors.toList());
            List<InsProduct> insProducts = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery().eq(InsProduct::getState, 1).in(InsProduct::getInsSampleId, collect));
            if (insProducts.stream().map(InsProduct::getId).collect(Collectors.toList()).equals(ids)) {
                //如果该检验单下的所有项目全部都撤销,那么这个单子也需要撤销
                insOrder.setState(3);
            } else {
                List<InsProduct> products = insProductMapper.selectBatchIds(ids);
                //首先根据选择的项目id进行撤销项目
                updateBatchById(products.stream().map(insProduct -> {
                    insProduct.setState(0);
                    return insProduct;
                }).collect(Collectors.toList()));
            }
            //insProductMapper.updateInspected(id);
        }
        //不通过
        insOrder.setIsRevocation(0);
        insOrderMapper.updateById(insOrder);
 
    }
 
    @Override
    public int write2(Integer id, String elasticityModulus) {
        InsProduct insProduct = new InsProduct();
        insProduct.setId(id);
        insProduct.setElasticityModulus(elasticityModulus);
        insProductMapper.updateById(insProduct);
        return 0;
    }
 
}