zhuo
2025-03-29 f682213b9ff8a7d41ea16edfb1b68d996c46e080
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
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.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.IfsInventoryQuantityMapper;
import com.ruoyi.basic.mapper.StandardProductListMapper;
import com.ruoyi.basic.pojo.IfsInventoryQuantity;
import com.ruoyi.basic.pojo.StandardProductList;
import com.ruoyi.basic.pojo.StructureTestObject;
import com.ruoyi.common.constant.InsOrderTypeConstants;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.numgen.NumberGenerator;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.api.IfsApiUtils;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.dto.*;
import com.ruoyi.inspect.mapper.*;
import com.ruoyi.inspect.pojo.*;
import com.ruoyi.inspect.service.InsOrderService;
import com.ruoyi.inspect.service.InsOrderStateService;
import com.ruoyi.inspect.service.InsProductService;
import com.ruoyi.inspect.service.InsSampleService;
import com.ruoyi.inspect.vo.InsOrderPrintingVo;
import com.ruoyi.performance.pojo.AuxiliaryOutputWorkingHoursTemporary;
import com.ruoyi.performance.service.AuxiliaryOutputWorkingHoursTemporaryService;
import com.ruoyi.system.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
/**
 * @author gaoaoy
 * @description 针对表【ins_order(检验下单)】的数据库操作Service实现
 * @createDate 2024-03-12 16:17:55
 */
@Service
@AllArgsConstructor
public class InsOrderServiceImpl extends ServiceImpl<InsOrderMapper, InsOrder>
        implements InsOrderService {
 
    private InsOrderMapper insOrderMapper;
    private InsSampleService insSampleService;
    private InsSampleMapper insSampleMapper;
    private InsProductService insProductService;
    private InsProductMapper insProductMapper;
    private InsSampleUserMapper insSampleUserMapper;
    private InsOrderStateService insOrderStateService;
    private UserMapper userMapper;
    private IfsInventoryQuantityMapper ifsInventoryQuantityMapper;
    private final NumberGenerator<InsOrder> numberGenerator;
    private InsReportMapper insReportMapper;
    private InsUnqualifiedRetestProductMapper insUnqualifiedRetestProductMapper;
    private IfsApiUtils ifsApiUtils;
    private SpotCheckQuarterItemMapper spotCheckQuarterItemMapper;
    private StandardProductListMapper standardProductListMapper;
    private AuxiliaryOutputWorkingHoursTemporaryService auxiliaryOutputWorkingHoursTemporaryService;
 
 
 
    //获取检验下单数据
    @Override
    public IPage<SampleOrderDto> selectInsOrderParameter(IPage<InsOrder> page, SampleOrderDto sampleOrderDto) {
        //todo: 只看我判断全部,个人,组织的权限
        String laboratory = null;
        // 判断是否是全部
        String isOrderAll = null;
        if (sampleOrderDto.getState() != null && sampleOrderDto.getState() == -2) {
            isOrderAll = "1";
            sampleOrderDto.setState(null);
        }
        return insOrderMapper.selectInsOrderPage(page, QueryWrappers.queryWrappers(sampleOrderDto), laboratory, isOrderAll);
    }
 
 
    /**
     * 分配检验人
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upInsOrder(Integer orderId, Integer sampleId, String appointed, Integer userId, String sonLaboratory) {
        InsOrder insOrder = new InsOrder();
        insOrder.setId(orderId);
        insOrder.setAppointed(StringUtils.isNotEmpty(appointed) ? LocalDate.parse(appointed) : null);
        insOrder.setSendTime(LocalDateTime.now());
        insOrderMapper.updateById(insOrder);
        List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, orderId));
        List<Integer> ids = insSamples.stream().map(InsSample::getId).collect(Collectors.toList());
        List<InsProduct> insProducts = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery()
                .in(InsProduct::getInsSampleId, ids)
                .eq(InsProduct::getState, 1)
                .select(InsProduct::getSonLaboratory).groupBy(InsProduct::getSonLaboratory));
 
        // 批量添加检验任务状态表
        List<InsOrderState> insOrderStateList = insProducts.stream().map(insProduct -> {
            InsOrderState insOrderState = new InsOrderState();
            insOrderState.setInsOrderId(orderId);
            try {
                insOrderState.setLaboratory(insProduct.getSonLaboratory());
            } catch (NullPointerException e) {
                throw new ErrorException("该检验单有未维护实验室的检验项目");
            }
            insOrderState.setInsState(0);
            return insOrderState;
        }).collect(Collectors.toList());
        insOrderStateService.saveBatch(insOrderStateList);
 
        if (userId != null) {
            InsSampleUser insSampleUser = new InsSampleUser();
            insSampleUser.setState(0);
            insSampleUser.setUserId(userId);
            insSampleUser.setInsSampleId(orderId);
            insSampleUser.setSonLaboratory(sonLaboratory);
            insSampleUserMapper.insert(insSampleUser);
        }
 
        // 判断订单有没有绑定抽样计划
        InsOrder order = insOrderMapper.selectById(orderId);
        if (order.getQuarterItemId() != null) {
           // 需要添加下发时间到抽样时间, 取样人员就是检测人
            SpotCheckQuarterItem spotCheckQuarterItem = spotCheckQuarterItemMapper.selectById(order.getQuarterItemId());
            spotCheckQuarterItem.setSpotCheckTime(order.getSendTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            User user = userMapper.selectById(userId);
            spotCheckQuarterItem.setSamplingUser(user.getName());
            spotCheckQuarterItemMapper.updateById(spotCheckQuarterItem);
        }
 
        // 创建工时暂存
        // 缓存样品id, 编号map
//        addWorkingHoursTemporary(userId, insSamples, ids, order);
 
        return 1;
    }
 
    /**
     * 创建工时暂存
     * @param userId
     * @param insSamples
     * @param ids
     * @param order
     */
    private void addWorkingHoursTemporary(Integer userId, List<InsSample> insSamples, List<Integer> ids, InsOrder order) {
        Map<Integer, String> sampleMap = insSamples.stream().collect(Collectors.toMap(InsSample::getId, InsSample::getSampleCode));
        List<InsProduct> insProductList = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery()
                .in(InsProduct::getInsSampleId, ids)
                .eq(InsProduct::getState, 1));
        List<AuxiliaryOutputWorkingHoursTemporary> outputWorkingHours = insProductList.stream().map(insProduct -> {
            AuxiliaryOutputWorkingHoursTemporary auxiliaryOutputWorkingHours = new AuxiliaryOutputWorkingHoursTemporary();
            auxiliaryOutputWorkingHours.setInspectionItemClass(insProduct.getInspectionItemClass());//检测项分类
            auxiliaryOutputWorkingHours.setInspectionItem(insProduct.getInspectionItem());//检测父项
            auxiliaryOutputWorkingHours.setInspectionItemSubclass(insProduct.getInspectionItemSubclass());//检测子项
            auxiliaryOutputWorkingHours.setSample(sampleMap.get(insProduct.getInsSampleId()));//样品编号
            auxiliaryOutputWorkingHours.setOrderId(order.getId());//订单id
            auxiliaryOutputWorkingHours.setOrderNo(order.getEntrustCode());//非加班委托单号
            auxiliaryOutputWorkingHours.setWorkTime(insProduct.getManHour());//非加班工时
            auxiliaryOutputWorkingHours.setAmount(1);//非加班数量
            auxiliaryOutputWorkingHours.setOutputWorkTime((ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getOvertimeWorkTime()) ? auxiliaryOutputWorkingHours.getOvertimeWorkTime() : BigDecimal.ZERO).add(ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getWorkTime()) ? auxiliaryOutputWorkingHours.getWorkTime() : BigDecimal.ZERO));//产量工时
            auxiliaryOutputWorkingHours.setManHourGroup(insProduct.getManHourGroup());//工时分组
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            auxiliaryOutputWorkingHours.setDateTime(LocalDateTime.now().toLocalDate().atStartOfDay().format(formatters));//日期
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTime parse = DateUtil.parse(localDateTime.format(formatter));
            auxiliaryOutputWorkingHours.setWeekDay(getWeek(localDateTime.format(formatters)));//星期
            auxiliaryOutputWorkingHours.setWeek(String.valueOf(DateUtil.weekOfYear(DateUtil.offsetDay(parse, 1))));//周次
            auxiliaryOutputWorkingHours.setCheck(userId);//检测人
            auxiliaryOutputWorkingHours.setPrice(insProduct.getPrice());//单价
            auxiliaryOutputWorkingHours.setSampleId(insProduct.getInsSampleId());//样品id
            auxiliaryOutputWorkingHours.setInsProductId(insProduct.getId());//检验项id
 
            return auxiliaryOutputWorkingHours;
        }).collect(Collectors.toList());
        auxiliaryOutputWorkingHoursTemporaryService.saveBatch(outputWorkingHours);
    }
 
    public static String getWeek(String dayStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(dayStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            return getWeekDay(dayOfWeek);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getWeekDay(int dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
            case Calendar.SUNDAY:
                return "周日";
            default:
                return "未知";
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addInsOrder(List<SampleProductDto> list, InsOrder insOrder) {
        // todo: 下单判断抽样计划的唯一性
        if (insOrder.getQuarterItemId() != null) {
            Long quarterItemCount = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery()
                    .eq(InsOrder::getQuarterItemId, insOrder.getQuarterItemId())
                    .notIn(InsOrder::getState, -1 ,2 ,3));
            if (quarterItemCount > 0) {
                throw new ErrorException("该抽样计划已被绑定过");
            }
        }
        insOrder.setState(0);
 
        LocalDate appointed = insOrder.getAppointed();
 
        insOrderMapper.insert(insOrder); // 主表
 
        list.forEach(a -> {
            a.setId(null);
            a.setInsOrderId(insOrder.getId());
            insSampleMapper.insert(a);
            if (ObjectUtil.isNotEmpty(a.getInsProduct())) {
                // 判断是否填写待检项数量
                if (a.getQuantity() != null) {
                    List<InsProduct> ip2 = new ArrayList<>();
                    for (Integer i = 1; i <= a.getQuantity(); i++) {
                        // 重新拷贝 创建新对象
                        AtomicInteger atomicInteger = new AtomicInteger(i);
                        List<InsProduct> insProducts = a.getInsProduct().stream().map(insProduct -> {
                            insProduct.setRawMaterialTag(String.valueOf(atomicInteger.get()));
                            InsProduct product = new InsProduct();
                            BeanUtils.copyProperties(insProduct, product);
                            return product;
                        }).collect(Collectors.toList());
 
                        ip2.addAll(insProducts);
                    }
                    addInsProductMethod(a.getId(), ip2);
                } else {
                    addInsProductMethod(a.getId(), a.getInsProduct());
                }
            }
            // 判断是否有电缆配置
            if (ObjectUtil.isNotEmpty(a.getInsulating())) {
                // 判断是否有辅助线芯
                if (ObjectUtil.isNotEmpty(a.getAuxiliaryWireCore())) {
                    if (a.getAuxiliaryWireCore().getInsProduct().stream().filter(insProduct -> insProduct.getState() == 1).count() !=
                            a.getInsulating().getInsProduct().stream().filter(insProduct -> insProduct.getState() == 1).count()) {
                        throw new ErrorException("电缆配置辅助线芯检验项数量不统一, 请检查");
                    }
                }
                List<InsProduct> ip2 = new ArrayList<>();
                for (String s : a.getInsulating().getNum()) {
                    // 重新拷贝 创建新对象
                    List<InsProduct> insProducts = a.getInsulating().getInsProduct().stream().map(insProduct -> {
                        insProduct.setCableTag(s);
                        InsProduct product = new InsProduct();
                        BeanUtils.copyProperties(insProduct, product);
                        return product;
                    }).collect(Collectors.toList());
                    ip2.addAll(insProducts);
                }
                for (InsProduct product : ip2) {
                    product.setStandardMethodListId(a.getInsulating().getStandardMethodListId());
                }
                addInsProductMethod(a.getId(), ip2);
            }
            // 判断是否有辅助线芯
            if (ObjectUtil.isNotEmpty(a.getAuxiliaryWireCore())) {
                List<InsProduct> ip2 = new ArrayList<>();
                for (String s : a.getAuxiliaryWireCore().getNum()) {
                    // 重新拷贝 创建新对象
                    List<InsProduct> insProducts = a.getAuxiliaryWireCore().getInsProduct().stream().map(insProduct -> {
                        insProduct.setCableTag(s);
                        InsProduct product = new InsProduct();
                        BeanUtils.copyProperties(insProduct, product);
                        return product;
                    }).collect(Collectors.toList());
                    ip2.addAll(insProducts);
                }
                for (InsProduct product : ip2) {
                    product.setStandardMethodListId(a.getAuxiliaryWireCore().getStandardMethodListId());
                }
                addInsProductMethod(a.getId(), ip2);
            }
            // 子样品配置
            if (ObjectUtil.isNotEmpty(a.getChildSampleList())) {
                for (SampleProductDto b : a.getChildSampleList()) {
                    for (int i = 0; i < b.getNum(); i++) {
                        b.setId(null);
                        b.setInsOrderId(insOrder.getId());
                        b.setParentId(a.getId());
                        insSampleMapper.insert(b);
                        if (ObjectUtil.isNotEmpty(b.getInsProduct())) {
                            addInsProductMethod(b.getId(), b.getInsProduct());
                        }
                    }
                }
            }
        });
        //是否为原材料下单
        if (insOrder.getTypeSource() != null && insOrder.getTypeSource().equals(1)) {
            // 原材料下单: 委托人就是报检人, 生产单位就是供应商单位
            IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(insOrder.getIfsInventoryId());
            Integer declareUserId = ifsInventoryQuantity.getDeclareUserId();
            User user = userMapper.selectById(declareUserId);
            if (user == null) {
               throw new ErrorException("缺少报检人信息");
            }
            // 供应商名称
            insOrder.setProduction(ifsInventoryQuantity.getSupplierName());
            insOrder.setProductionEn("");
            // 委托人名称
            insOrder.setPrepareUserId(user.getId());
            insOrder.setPrepareUser(user.getName());
            insOrder.setPrepareUserEn(user.getNameEn());
            insOrder.setPhone(user.getPhone());
            insOrder.setState(1);
            Long count1 = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery()
                    .eq(InsOrder::getIfsInventoryId, insOrder.getIfsInventoryId())
                    .ne(InsOrder::getState, -1)
                    .eq(InsOrder::getOrderType, InsOrderTypeConstants.ENTER_THE_FACTORY)
                    .ne(InsOrder::getId, insOrder.getId()));
            // 判断之前是否有进厂检验
            if (count1 == 0) {
                ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>().set(IfsInventoryQuantity::getState, 1)
                        .eq(IfsInventoryQuantity::getId, insOrder.getIfsInventoryId()));
            }
 
            // 判断结束状态修改合格状态
            if (ifsInventoryQuantity.getIsFinish().equals(0)) {
                ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                        .eq(IfsInventoryQuantity::getId, insOrder.getIfsInventoryId())
                        .set(IfsInventoryQuantity::getInspectStatus, 0));
            }
 
            // 审核检验单
            upInsOrderOfState(insOrder);
 
            // 分配检验人
            upInsOrder(insOrder.getId(), null, appointed != null ? appointed.toString() : null, SecurityUtils.getUserId().intValue(), "原材料");
 
            // 根据零件号判断是否是辅材
            boolean isRaw = false;
            StructureTestObject productObject = insOrderMapper.selectProductByPartNo(ifsInventoryQuantity.getPartNo());
            // 查询产品
            if (productObject != null && StrUtil.isNotBlank(productObject.getObjectType()) && productObject.getObjectType().equals("1")) {
                isRaw = true;
            } else {
            // 查询对象
                StructureTestObject testObject = insOrderMapper.selectByPartNo(ifsInventoryQuantity.getPartNo());
                if (testObject != null && StrUtil.isNotBlank(testObject.getObjectType()) && testObject.getObjectType().equals("1")) {
                    isRaw = true;
                }
            }
 
            if (isRaw) {
                // 获取当前季度的开始时间和结束时间
                LocalDateTime now = LocalDateTime.now();
                // 获取当前月份
                int month = now.getMonthValue();
                // 确定当前季度的开始月份
                int startMonth = (month - 1) / 3 * 3 + 1;
                // 构造季度的开始时间
                LocalDateTime startOfQuarter = LocalDateTime.of(now.getYear(), Month.of(startMonth), 1, 0, 0);
                // 计算下一个季度的开始时间
                LocalDateTime startOfNextQuarter = startOfQuarter.plusMonths(3);
                // 计算当前季度的结束时间
                LocalDateTime endOfQuarter = startOfNextQuarter.minusSeconds(1);
 
                // 根据下单的规格型号判断是否为季度首次出现
                Integer count = ifsInventoryQuantityMapper.selectIsFirst(insOrder.getPartDetail(),
                        ifsInventoryQuantity.getSupplierName(),
                        startOfNextQuarter,
                        endOfQuarter);
 
                if(count == 0) {
                    ifsInventoryQuantity.setIsFirst(1);
                    ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                            .eq(IfsInventoryQuantity::getId, insOrder.getIfsInventoryId())
                            .set(IfsInventoryQuantity::getIsFirst, 1));
                }
            }
        }
        return insOrder.getId();
    }
 
    /**
     * 添加检验项
     * @param sampleId
     * @param productList
     */
    private void addInsProductMethod(Integer sampleId, List<InsProduct> productList) {
        for (InsProduct product : productList) {
            if (product.getState() == 1) {
                product.setId(null);
                product.setCreateTime(null);
                product.setCreateUser(null);
                product.setUpdateTime(null);
                product.setUpdateUser(null);
                product.setInsSampleId(sampleId);
                if (product.getInspectionItemSubclass() == null) {
                    product.setInspectionItemSubclass("");
                }
                if (StringUtils.isBlank(product.getAsk()) || StringUtils.isBlank(product.getTell())) {
                    throw new ErrorException("有检验项的要求值或要求描述为空, 请先去标准库配置要求值或要求描述");
                }
                if (StringUtils.isBlank(product.getSonLaboratory())) {
                    throw new ErrorException("有检验项的的子实验室为绑定, 请先绑定子实验室");
                }
                insProductMapper.insert(product);
            }
        }
    }
 
 
    @Override
    public Map<String, Object> getInsOrder(Integer id) {
        Map<String, Object> map = new HashMap<>();
        InsOrder insOrder = insOrderMapper.selectById(id);
        List<SampleProductDto> list;
        // 判断是否是进厂报告免检
        if (insOrder.getIsExemption().equals(1)) {
            list = insSampleMapper.selectExemptionByOrderId(id);
        } else {
            list = insSampleMapper.selectSampleProductListByOrderId2(id);
        }
        Map<String, Object> map1 = insSampleMapper.selectInsOrder(id);
        map.put("insOrder", insOrder);
        map.put("sampleProduct", list);
        map.put("insOrderTemplate", map1);
        return map;
    }
 
    /**
     * 审核检验单
     * @param insOrder
     * @return
     */
    @Override
    public int upInsOrderOfState(InsOrder insOrder) {
        insOrder.setExamineTime(LocalDateTime.now());
        if (insOrder.getState() == 1) {
            //审核通过才会生成委托编号
            // todo: 检验类型编号
            InsOrder order = this.getById(insOrder.getId());
            String code = "";
            switch (order.getOrderType()) {
                case InsOrderTypeConstants.SPOT_CHECK:
                    code = "C";
                    break;
                case InsOrderTypeConstants.CUSTOMER_ORDERED:
                    code = "W";
                    break;
                case InsOrderTypeConstants.ENTER_THE_FACTORY:
                case InsOrderTypeConstants.QUARTERLY_TEST:
                    code = "Y";
                    break;
            }
            // 生成编号
            String no = numberGenerator.generateNumberWithPrefix(3,
                    "JCZX/ZB-" + code + LimsDateUtil.resetDate(LocalDateTime.now()),
                    InsOrder::getEntrustCode);
            // 判断是否是季度检验, 是季度检验取消原材料季度检验下单
            if (InsOrderTypeConstants.QUARTERLY_TEST.equals(order.getOrderType())) {
                ifsInventoryQuantityMapper.update(null,  Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                        .eq(IfsInventoryQuantity::getId, order.getIfsInventoryId())
                        .set(IfsInventoryQuantity::getIsQuarter, 0));
 
                // 查询是否有过进厂检验, 有获取里面的编号
                InsOrder order1 = insOrderMapper.selectOne(Wrappers.<InsOrder>lambdaQuery()
                        .eq(InsOrder::getIfsInventoryId, order.getIfsInventoryId())
                        .ne(InsOrder::getState, -1)
                        .eq(InsOrder::getOrderType, InsOrderTypeConstants.ENTER_THE_FACTORY));
                if (order1 != null) {
                  no = order1.getEntrustCode();
                }
            }
 
            int count = 1;
            // 查询样品表
            List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                    .eq(InsSample::getInsOrderId, insOrder.getId()));
            for (InsSample insSample : insSamples) {
                if (StringUtils.isBlank(insSample.getSampleCode())) {
                    // 如果只有一个样品就不需要拼接数字
                    if (insSamples.size() != 1) {
                        insSample.setSampleCode(no + "-" + count);
                        count++;
                    } else {
                        insSample.setSampleCode(no);
                    }
                }
            }
            insSampleService.updateBatchById(insSamples);
 
            insOrder.setEntrustCode(no);
        }
        return insOrderMapper.updateById(insOrder);
    }
 
    @Override
    public Map<String, Object> getInsOrderAndSample(Integer id, String laboratory) {
        Map<String, Object> map = new HashMap<>();
        InsOrder insOrder = insOrderMapper.selectById(id);
        List<SampleProductDto> list = insSampleMapper.getInsOrderAndSample(id, laboratory);
        map.put("insOrder", insOrder);
        map.put("sampleProduct", list);
        //查询所有记录模版去重
        List<Map<Integer, Object>> list2 = insOrderMapper.selectReportModelByOrderId(id, laboratory);
        map.put("reportModel", list2);
        return map;
    }
 
    @Override
    public IPage<SampleProductDto2> selectSampleAndProductByOrderId(IPage<SampleProductDto2> page, SampleProductDto2 sampleProductDto) {
        IPage<SampleProductDto2> productDto2IPage = insOrderMapper.selectSampleAndProductByOrderId(page,
                QueryWrappers.queryWrappers(sampleProductDto)
                        .orderByAsc("sample_code")
                        .orderByAsc("cable_tag")
                        .orderByAsc("sort"),
                sampleProductDto.getId());
        return productDto2IPage;
    }
 
 
    @Override
    public int updateStatus(Integer id) {
        return insOrderMapper.updateStatus(id);
    }
 
 
    /**
     * 获取ifs库存信息
     * @param
     * @return
     */
    @Override
    public void getIfsOrder() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("LOCATION_NO","1302");
        map.put("STATE_DB","To be Inspected");
        List<Map<String, Object>> inventory = ifsApiUtils.getInventory(JSONUtil.toJsonStr(map));
        if(inventory.size() == 0) {
            return;
        }
        // 进行保存
        for (Map<String, Object> map1 : inventory) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 时间格式化
            IfsInventoryQuantity ifsInventoryQuantity = new IfsInventoryQuantity();
            ifsInventoryQuantity.setContract(map1.get("CONTRACT") == null ? "" : map1.get("CONTRACT").toString()); // 域
            ifsInventoryQuantity.setPartNo(map1.get("PART_NO") == null ? "" : map1.get("PART_NO").toString() ); // 零件号
            ifsInventoryQuantity.setPartDesc(map1.get("PART_DESC") == null ? "" : map1.get("PART_DESC").toString()); // 零件描述
            ifsInventoryQuantity.setOrderNo(map1.get("ORDER_NO") == null ? "" : map1.get("ORDER_NO").toString()); // 订单号
            ifsInventoryQuantity.setLineNo(map1.get("LINE_NO") == null ? "" : map1.get("LINE_NO").toString()); // 行号
            ifsInventoryQuantity.setReleaseNo(map1.get("RELEASE_NO") == null ? "" : map1.get("RELEASE_NO").toString()); // 下达号
            ifsInventoryQuantity.setReceiptNo(Integer.parseInt(map1.get("RECEIPT_NO") == null ? "" : map1.get("RECEIPT_NO").toString())); // 接收号
            ifsInventoryQuantity.setStatus(map1.get("STATE") == null ? "" : map1.get("STATE").toString()); // 状态描述
            ifsInventoryQuantity.setStatusDb(map1.get("STATE_DB") == null ? "" : map1.get("STATE_DB").toString()); // 状态
            if(map1.get("QTY_ARRIVED") != null) {
                ifsInventoryQuantity.setQtyArrived(new BigDecimal(map1.get("QTY_ARRIVED").toString())); // 抵达的采购数量
            }
            if(map1.get("QTY_INSPECTED") != null) {
                ifsInventoryQuantity.setQtyInspected(new BigDecimal(map1.get("QTY_INSPECTED").toString())); // 已检验的购买数量
            }
            if(map1.get("QTY_TO_INSPECT") != null) {
                ifsInventoryQuantity.setQtyToInspect(new BigDecimal(map1.get("QTY_TO_INSPECT").toString())); // 要检验的采购数量
            }
            if(map1.get("INV_QTY_IN_STORE") != null) {
                ifsInventoryQuantity.setInvQtyInStore(new BigDecimal(map1.get("INV_QTY_IN_STORE").toString())); // 抵达的库存数量
            }
            if(map1.get("PUR_QTY_IN_STORE") != null) {
                ifsInventoryQuantity.setPurQtyInStore(new BigDecimal( map1.get("PUR_QTY_IN_STORE").toString())); // 抵达的采购数量
 
            }
            ifsInventoryQuantity.setSupplierId(map1.get("SUPPLIER_ID") == null ? "" : map1.get("SUPPLIER_ID").toString()); // 供应商ID
            ifsInventoryQuantity.setSupplierName(map1.get("SUPPLIER_NAME") == null ? "" : map1.get("SUPPLIER_NAME").toString()); // 供应商名称
            ifsInventoryQuantity.setConfigurationId(map1.get("CONFIGURATION_ID") == null ? "" : map1.get("CONFIGURATION_ID").toString()); // 配置标识
            ifsInventoryQuantity.setLotBatchNo(map1.get("LOT_BATCH_NO") == null ? "" : map1.get("LOT_BATCH_NO").toString()); // 批次号
            ifsInventoryQuantity.setUpdateBatchNo(map1.get("LOT_BATCH_NO") == null ? "" : map1.get("LOT_BATCH_NO").toString()); // 批次号
            ifsInventoryQuantity.setWaivDevRejNo(map1.get("WAIV_DEV_REJ_NO") == null ? "" : map1.get("WAIV_DEV_REJ_NO").toString()); // WDR号
            ifsInventoryQuantity.setActivitySeq(map1.get("ACTIVITY_SEQ") == null ? null : Integer.parseInt(map1.get("ACTIVITY_SEQ").toString())); // 活动序列
            ifsInventoryQuantity.setSerialNo(map1.get("SERIAL_NO") == null ? "" : map1.get("SERIAL_NO").toString()); // 序列号
            ifsInventoryQuantity.setLocationNo(map1.get("LOCATION_NO") == null ? "" : map1.get("LOCATION_NO").toString()); // 库位号
            ifsInventoryQuantity.setEngChgLevel(map1.get("ENG_CHG_LEVEL") == null ? "" : map1.get("ENG_CHG_LEVEL").toString()); // 版本号
            ifsInventoryQuantity.setReceiver(map1.get("RECEIVER") == null ? "" : map1.get("RECEIVER").toString()); // 接收人
            ifsInventoryQuantity.setReceiverName(map1.get("RECEIVER_NAME") == null ? "" : map1.get("RECEIVER_NAME").toString()); // 接收人名称
            ifsInventoryQuantity.setBuyerCode(map1.get("BUYER_CODE") == null ? "" : map1.get("BUYER_CODE").toString()); // 采购员
            ifsInventoryQuantity.setBuyerName(map1.get("BUYER_NAME") == null ? "" : map1.get("BUYER_NAME").toString()); // 采购员名称
 
            if(map1.get("ARRIVE_DATE") != null) {
                ifsInventoryQuantity.setArriveDate(LocalDateTime.parse(map1.get("ARRIVE_DATE").toString(),dateTimeFormatter)); // 实际到货日期
            }
            if(map1.get("DELIVERY_DATE") != null) {
                ifsInventoryQuantity.setDeliveryDate(LocalDateTime.parse(map1.get("DELIVERY_DATE").toString(),dateTimeFormatter)); // 实际交货日期
            }
            if(map1.get("PRODUCT_DATE") != null) {
                ifsInventoryQuantity.setProductDate(LocalDateTime.parse(map1.get("PRODUCT_DATE").toString(),dateTimeFormatter)); // 生产日期
 
            }
            if(map1.get("INVALID_DATE") != null) {
                ifsInventoryQuantity.setInvalidDate(LocalDateTime.parse(map1.get("INVALID_DATE").toString(),dateTimeFormatter)); // 失效日期
            }
            if(map1.get("APPROVED_DATE") != null) {
                ifsInventoryQuantity.setApprovedDate(LocalDateTime.parse(map1.get("APPROVED_DATE").toString(),dateTimeFormatter)); // 审批日期
            }
            ifsInventoryQuantity.setReqCeater(map1.get("REQ_CEATER") == null ? "" : map1.get("REQ_CEATER").toString()); // 采购申请创建人
            ifsInventoryQuantity.setReqCeaterName(map1.get("REQ_CEATER_NAME") == null ? "" : map1.get("REQ_CEATER_NAME").toString()); // 采购申请创建人名称
            ifsInventoryQuantity.setLineRemarks(map1.get("LINE_REMARKS") == null ? "" : map1.get("LINE_REMARKS").toString()); // 采购订单行备注
            ifsInventoryQuantity.setBuyUnitMeas(map1.get("BUY_UNIT_MEAS") == null ? "" : map1.get("BUY_UNIT_MEAS").toString()); // 采购单位
            ifsInventoryQuantity.setReceiverDate(LocalDateTime.now()); // 接收日期
            ifsInventoryQuantity.setIsSource(1);
            ifsInventoryQuantity.setState(0);
 
            IfsInventoryQuantity one = ifsInventoryQuantityMapper.selectOne(new LambdaQueryWrapper<IfsInventoryQuantity>()
                    .eq(IfsInventoryQuantity::getOrderNo, ifsInventoryQuantity.getOrderNo())
                    .eq(IfsInventoryQuantity::getLineNo, ifsInventoryQuantity.getLineNo())
                    .eq(IfsInventoryQuantity::getReleaseNo, ifsInventoryQuantity.getReleaseNo())
                    .eq(IfsInventoryQuantity::getReceiptNo, ifsInventoryQuantity.getReceiptNo())
                    .eq(IfsInventoryQuantity::getLocationNo, ifsInventoryQuantity.getLocationNo())
                    .eq(IfsInventoryQuantity::getLotBatchNo, ifsInventoryQuantity.getLotBatchNo())
                    .eq(IfsInventoryQuantity::getSerialNo, ifsInventoryQuantity.getSerialNo())
                    .eq(IfsInventoryQuantity::getEngChgLevel, ifsInventoryQuantity.getEngChgLevel())
                    .eq(IfsInventoryQuantity::getWaivDevRejNo, ifsInventoryQuantity.getWaivDevRejNo())
                    .eq(IfsInventoryQuantity::getActivitySeq, ifsInventoryQuantity.getActivitySeq())
            );
            if(Objects.isNull(one)) {
 
                ifsInventoryQuantity.setIsFirst(0);
                // 查询产业链检测数据
                String industryChainAttrFields = IndustryChainUtils.getIndustryChainAttrFields(ifsInventoryQuantity.getOrderNo(),
                        ifsInventoryQuantity.getLineNo(),
                        ifsInventoryQuantity.getReleaseNo());
                ifsInventoryQuantity.setIndustryChain(industryChainAttrFields);
 
                ifsInventoryQuantityMapper.insert(ifsInventoryQuantity);
            }
        }
    }
 
    /**
     * id是原材料的id
     *
     * 修改订单单号
     * @param insOrder
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEntrustCode(InsOrder insOrder) {
        // 判断当前订单是否生成了报告, 生成了报告不能修改单号
        List<InsOrder> insOrders = insOrderMapper.selectList(Wrappers.<InsOrder>lambdaQuery()
                .eq(InsOrder::getIfsInventoryId, insOrder.getId())
                .ne(InsOrder::getState, -1));
        List<Integer> insOrderIds = insOrders.stream().map(InsOrder::getId).collect(Collectors.toList());
        Long count = insReportMapper.selectCount(Wrappers.<InsReport>lambdaQuery()
                .in(InsReport::getInsOrderId, insOrderIds));
        if (count > 0 ) {
            throw new ErrorException("当前订单已经生成了报告不能修改编号");
        }
 
 
        Long count1 = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery()
                .eq(InsOrder::getEntrustCode, insOrder.getEntrustCode())
                .ne(InsOrder::getIfsInventoryId, insOrder.getId())
                .ne(InsOrder::getInsState, -1));
        if (count1 > 0) {
            throw new ErrorException("该编号重复");
        }
 
        //修改报告的编号
        insOrderMapper.update(null, Wrappers.<InsOrder>lambdaUpdate()
                .eq(InsOrder::getIfsInventoryId, insOrder.getId())
                .set(InsOrder::getEntrustCode, insOrder.getEntrustCode()));
 
        // 修改样品的编号
        // 查询所有的样品
        List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                .in(InsSample::getInsOrderId, insOrderIds));
        for (InsSample insSample : insSamples) {
            String sampleCode = insSample.getSampleCode();
            String updateCode = insOrder.getEntrustCode();
            // 查找最后一个'-'的位置
            int lastDashIndex = sampleCode.lastIndexOf('-');
            if (lastDashIndex != -1) {
                int secondLastDashIndex = sampleCode.lastIndexOf('-', lastDashIndex - 1);
                // 处理最后一个'-'前的部分
                if (secondLastDashIndex != -1) {
                    // 处理最后一个'-'及之后的部分
                    String afterLastDash = sampleCode.substring(lastDashIndex);
 
                    updateCode = updateCode + afterLastDash;
                }
            }
            insSampleMapper.update(null, Wrappers.<InsSample>lambdaUpdate()
                    .eq(InsSample::getId, insSample.getId())
                    .set(InsSample::getSampleCode, updateCode));
 
        }
    }
 
    @Override
    public List<InsUnqualifiedRetestProduct> getRetestResult(Integer insProductId) {
        return insUnqualifiedRetestProductMapper.selectList(Wrappers.<InsUnqualifiedRetestProduct>lambdaQuery()
                .eq(InsUnqualifiedRetestProduct::getInsProductId, insProductId));
    }
 
    /**
     * 修改采购订单接收状态, 避免回滚
     * @param id
     */
    @Transactional
    public void updateIfsInventoryQuantity(Integer id) {
        ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                .set(IfsInventoryQuantity::getIsRegister, 1)
                .eq(IfsInventoryQuantity::getId, id));
    }
 
    /**
     * 新增铜单丝下单
     * @param list
     * @param insOrder
     * @return
     */
    @Override
    public int addRawCopperOrder(List<SampleProductDto> list, CopperInsOrderDto insOrder) {
        insOrder.setState(1);
        insOrder.setTypeSource(1);
 
        LocalDate appointed = insOrder.getAppointed();
 
        insOrderMapper.insert(insOrder); // 主表
 
        list.forEach(a -> {
            a.setId(null);
            a.setInsOrderId(insOrder.getId());
            insSampleMapper.insert(a);
            if (ObjectUtil.isNotEmpty(a.getInsProduct())) {
                // 判断是否填写待检项数量
                if (a.getQuantity() != null) {
                    List<InsProduct> ip2 = new ArrayList<>();
                    for (Integer i = 1; i <= a.getQuantity(); i++) {
                        // 重新拷贝 创建新对象
                        AtomicInteger atomicInteger = new AtomicInteger(i);
                        List<InsProduct> insProducts = a.getInsProduct().stream().map(insProduct -> {
                            insProduct.setRawMaterialTag(String.valueOf(atomicInteger.get()));
                            InsProduct product = new InsProduct();
                            BeanUtils.copyProperties(insProduct, product);
                            return product;
                        }).collect(Collectors.toList());
 
                        ip2.addAll(insProducts);
                    }
                    addInsProductMethod(a.getId(), ip2);
                } else {
                    addInsProductMethod(a.getId(), a.getInsProduct());
                }
            }
            if (ObjectUtil.isNotEmpty(a.getChildSampleList())) {
                for (SampleProductDto b : a.getChildSampleList()) {
                    for (int i = 0; i < b.getNum(); i++) {
                        b.setId(null);
                        b.setInsOrderId(insOrder.getId());
                        b.setParentId(a.getId());
                        insSampleMapper.insert(b);
                        if (ObjectUtil.isNotEmpty(b.getInsProduct())) {
                            addInsProductMethod(b.getId(), b.getInsProduct());
                        }
                    }
                }
            }
        });
 
        // 添加原材料信息
        IfsInventoryQuantity ifsInventoryQuantity = new IfsInventoryQuantity();
        // 基本信息
        ifsInventoryQuantity.setIsSource(0);
        ifsInventoryQuantity.setIsInspect(1);
        ifsInventoryQuantity.setState(1);
        ifsInventoryQuantity.setIsFinish(0);
        ifsInventoryQuantity.setIsCopper(1);
        ifsInventoryQuantity.setIsQuarter(0);
        ifsInventoryQuantity.setInspectStatus(0);
 
        ifsInventoryQuantity.setQtyArrived(insOrder.getQtyArrived());
        ifsInventoryQuantity.setBuyUnitMeas(insOrder.getBuyUnitMeas());
        ifsInventoryQuantity.setSupplierName(insOrder.getSupplierName());
        ifsInventoryQuantity.setUpdateBatchNo(insOrder.getUpdateBatchNo());
        ifsInventoryQuantity.setDeclareDate(insOrder.getDeclareDate());
 
        ifsInventoryQuantityMapper.insert(ifsInventoryQuantity);
 
 
        insOrder.setIfsInventoryId(ifsInventoryQuantity.getId());
        insOrder.setState(1);
 
 
        upInsOrderOfState(insOrder);
        upInsOrder(insOrder.getId(), null, appointed != null ? appointed.toString() : null, SecurityUtils.getUserId().intValue(), "原材料");
 
        return insOrder.getId();
    }
 
    /**
     * 修改委托下单编号
     * @param insOrder
     */
    @Override
    public void updateOrderEntrustCode(InsOrder insOrder) {
        // 判断当前订单是否生成了报告, 生成了报告不能修改单号
        Long count = insReportMapper.selectCount(Wrappers.<InsReport>lambdaQuery()
                .eq(InsReport::getInsOrderId, insOrder.getId()));
        if (count > 0 ) {
            throw new ErrorException("当前订单已经生成了报告不能修改编号");
        }
 
        Long count1 = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery()
                .eq(InsOrder::getEntrustCode, insOrder.getEntrustCode()));
        if (count1 > 0) {
            throw new ErrorException("该编号重复");
        }
 
        //修改报告的编号
        insOrderMapper.update(null, Wrappers.<InsOrder>lambdaUpdate()
                .eq(InsOrder::getId, insOrder.getId())
                .set(InsOrder::getEntrustCode, insOrder.getEntrustCode()));
 
        // 修改样品的编号
        // 查询所有的样品
        List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                .eq(InsSample::getInsOrderId, insOrder.getId()));
        for (InsSample insSample : insSamples) {
            String sampleCode = insSample.getSampleCode();
            String updateCode = insOrder.getEntrustCode();
            // 查找最后一个'-'的位置
            int lastDashIndex = sampleCode.lastIndexOf('-');
            if (lastDashIndex != -1) {
                int secondLastDashIndex = sampleCode.lastIndexOf('-', lastDashIndex - 1);
                // 处理最后一个'-'前的部分
                if (secondLastDashIndex != -1) {
                    // 处理最后一个'-'及之后的部分
                    String afterLastDash = sampleCode.substring(lastDashIndex);
 
                    updateCode = updateCode + afterLastDash;
                }
            }
            insSampleMapper.update(null, Wrappers.<InsSample>lambdaUpdate()
                    .eq(InsSample::getId, insSample.getId())
                    .set(InsSample::getSampleCode, updateCode));
 
        }
    }
 
    /**
     * 修改检验下单内容
     * @param insOrderUpdateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateInsOrder(InsOrderUpdateDto insOrderUpdateDto) {
        // 修改订单
        insOrderUpdateDto.getInsOrder().setState(0);
        insOrderUpdateDto.getInsOrder().setTell("");
        insOrderMapper.updateById(insOrderUpdateDto.getInsOrder());
 
        // 修改检验项
        for (SampleProductDto sampleProductDto : insOrderUpdateDto.getSampleProduct()) {
            insProductService.updateBatchById(sampleProductDto.getInsProduct());
        }
 
        return true;
    }
 
    /**
     * 成品标签打印
     * @param ids
     * @return
     */
    @Override
    public List<InsOrderPrintingVo> labelOrderPrinting(List<Integer> ids) {
        return insOrderMapper.labelOrderPrinting(ids);
    }
 
    /**
     * 根据样品id查询检验项树
     * @param insSampleId
     * @return
     */
    @Override
    public List<StandardProductList> getProductTreeBySampleId(Integer insSampleId) {
        // 查询第一个检验项获取检验项树
        InsProduct insProduct = insProductMapper.selectOne(Wrappers.<InsProduct>lambdaQuery()
                .eq(InsProduct::getInsSampleId, insSampleId)
                .last("limit 1"));
        String tree = insProduct.getFactory() + " - " +
                insProduct.getLaboratory() + " - " +
                insProduct.getSampleType() + " - " +
                insProduct.getSample() + " - " +
                insProduct.getModel();
        // 查询标准树
        List<StandardProductList> standardProductLists = standardProductListMapper.selectList(Wrappers.<StandardProductList>lambdaQuery()
                .eq(StandardProductList::getStandardMethodListId, insProduct.getStandardMethodListId())
                .eq(StandardProductList::getTree, tree)
                .orderByAsc(StandardProductList::getSort));
        for (StandardProductList standardProductList : standardProductLists) {
            standardProductList.setId(null);
        }
 
        return standardProductLists;
    }
 
    /**
     * 添加遗漏的检验项
     * @param omitOrderProductDto
     * @return
     */
    @Override
    public boolean addOmitOrderProduct(OmitOrderProductDto omitOrderProductDto) {
        if (omitOrderProductDto.getInsSampleId() == null) {
            throw new ErrorException("缺少样品Id");
        }
        for (InsProduct product : omitOrderProductDto.getInsProductBindingList()) {
            if (product.getState() == 1) {
                product.setId(null);
                product.setCreateTime(null);
                product.setCreateUser(null);
                product.setUpdateTime(null);
                product.setUpdateUser(null);
                product.setInsSampleId(omitOrderProductDto.getInsSampleId());
                if (StringUtils.isBlank(product.getCableTag())) {
                    product.setCableTag(null);
                }
                if (product.getInspectionItemSubclass() == null) {
                    product.setInspectionItemSubclass("");
                }
                if (StringUtils.isBlank(product.getAsk()) || StringUtils.isBlank(product.getTell())) {
                    throw new ErrorException("有检验项的要求值或要求描述为空, 请填写要求值或要求描述");
                }
                insProductMapper.insert(product);
            }
        }
 
        return true;
    }
 
    /**
     * 成品检验单导出
     * @param sampleOrderDto
     * @param response
     */
    @Override
    public void rawAllInsOrderExport(SampleOrderDto sampleOrderDto, HttpServletResponse response) {
        //判断全部,个人,组织的权限
        //todo:仅看我获取当前人所属实验室id
        String laboratory = null;
        // 判断是否是全部
        String isOrderAll = null;
        if (sampleOrderDto.getState() != null && sampleOrderDto.getState() == -2) {
            isOrderAll = "1";
            sampleOrderDto.setState(null);
        }
        List<SampleOrderDto> sampleOrderDtoList = insOrderMapper.rawAllInsOrderExport(QueryWrappers.queryWrappers(sampleOrderDto), laboratory, isOrderAll);
 
        // 判断是否是不合格, 不合格查询不合格项
        for (SampleOrderDto orderDto : sampleOrderDtoList) {
            if (orderDto.getInsResult() != null && orderDto.getInsResult() == 0){
                // 查询不合格项
                List<String> unqualifiedList = insProductMapper.selectUnqualifiedList(orderDto.getId());
                orderDto.setUnqualifiedItem(CollUtil.join(unqualifiedList, ","));
            }
            orderDto.setCreateTimeString(orderDto.getCreateTime() == null ? "" : orderDto.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
 
        }
 
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        try {
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("委托检测信息导出", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "委托检测信息导出").head(SampleOrderDto.class).build();
 
            //向sheet0写入数据 传入空list这样只导出表头
            excelWriter.write(sampleOrderDtoList, mainSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 修改样品型号
     * @param insSample
     */
    @Override
    public void updateSampleModel(InsSample insSample) {
        // 判断当前订单是否生成了报告, 生成了报告不能修改单号
        Long count = insReportMapper.selectCount(Wrappers.<InsReport>lambdaQuery()
                .eq(InsReport::getInsOrderId, insSample.getInsOrderId()));
        if (count > 0 ) {
            throw new ErrorException("当前订单已经生成了报告不能修改编号");
        }
 
        insSampleService.update(Wrappers.<InsSample>lambdaUpdate()
                .eq(InsSample::getId, insSample.getId())
                .set(InsSample::getModel, insSample.getModel()));
    }
 
}