zhuo
2025-04-21 af1ef97f554b67140773103a3e90bc95b80c5b54
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
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.ruoyi.basic.dto.*;
import com.ruoyi.basic.pojo.IfsInventoryQuantity;
import com.ruoyi.common.constant.InsOrderTypeConstants;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.config.WechatProperty;
import com.ruoyi.common.utils.LimsDateUtil;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WxCpUtils;
import com.ruoyi.inspect.dto.CopperInsOrderDto;
import com.ruoyi.inspect.dto.RawMaterialStandardTreeDto;
import com.ruoyi.basic.mapper.IfsInventoryQuantityMapper;
import com.ruoyi.basic.mapper.StandardTreeMapper;
import com.ruoyi.inspect.dto.SampleProductDto;
import com.ruoyi.inspect.mapper.InsOrderMapper;
import com.ruoyi.inspect.mapper.InsProductMapper;
import com.ruoyi.inspect.mapper.InsSampleMapper;
import com.ruoyi.inspect.pojo.InsOrder;
import com.ruoyi.inspect.pojo.InsReport;
import com.ruoyi.inspect.service.InsOrderService;
import com.ruoyi.inspect.service.InsReportService;
import com.ruoyi.inspect.service.RawMaterialOrderService;
import com.ruoyi.common.numgen.NumberGenerator;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.performance.mapper.AuxiliaryOutputWorkingHoursMapper;
import com.ruoyi.performance.pojo.AuxiliaryOutputWorkingHours;
import com.ruoyi.system.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @Author zhuo
 * @Date 2024/7/31
 */
@Service
@AllArgsConstructor
public class RawMaterialOrderServiceImpl implements RawMaterialOrderService {
 
    private StandardTreeMapper standardTreeMapper;
    private IfsInventoryQuantityMapper ifsInventoryQuantityMapper;
    private UserMapper userMapper;
    private InsOrderService insOrderService;
    private InsOrderMapper insOrderMapper;
    private InsSampleMapper insSampleMapper;
    private final NumberGenerator<InsOrder> numberGenerator;
    private InsReportService insReportService;
    private WechatProperty wechatProperty;
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    private InsProductMapper insProductMapper;
    private AuxiliaryOutputWorkingHoursMapper auxiliaryOutputWorkingHoursMapper;
 
 
    @Override
    public Result selectStandardTreeListByPartNo(String partNo) {
        List<FactoryDto> factoryDtos = standardTreeMapper.selectStandardTreeListByPartNo(partNo);
        if (CollectionUtil.isEmpty(factoryDtos)) {
            return Result.success(null, "零件号为" + partNo + "的原材料没有对应的标准库配置");
        }
        RawMaterialStandardTreeDto rawMaterialStandardTreeDto = new RawMaterialStandardTreeDto();
        for (FactoryDto factoryDto : factoryDtos) {
            for (LaboratoryDto laboratoryDto : factoryDto.getChildren()) {
                for (SampleTypeDto sampleTypeDto : laboratoryDto.getChildren()) {
                    if (sampleTypeDto.getChildren().size() == 0) {
                        sampleTypeDto.setChildren(standardTreeMapper.getStandardTree3(sampleTypeDto.getValue()));
                    }
                    // 判断绑定的是否是当前零件号
                    if (sampleTypeDto.getPartNo() != null && sampleTypeDto.getPartNo().equals(partNo)) {
                        // 添加对象
                        rawMaterialStandardTreeDto.setTreeName(factoryDto.getValue() + "-"
                                + laboratoryDto.getValue() + "-"
                                + sampleTypeDto.getValue());
                        rawMaterialStandardTreeDto.setCode(sampleTypeDto.getCode());
                        rawMaterialStandardTreeDto.setLabel(sampleTypeDto.getLabel());
                        rawMaterialStandardTreeDto.setValue(sampleTypeDto.getValue());
                        rawMaterialStandardTreeDto.setChildren1(sampleTypeDto.getChildren());
                    } else {
                        for (SampleDto sampleDto : sampleTypeDto.getChildren()) {
                            if (sampleDto.getPartNo() != null && sampleDto.getPartNo().equals(partNo)) {
                                // 添加对象
                                rawMaterialStandardTreeDto.setTreeName(factoryDto.getValue() + "-"
                                        + laboratoryDto.getValue() + "-"
                                        + sampleTypeDto.getValue() + "-"
                                        + sampleDto.getValue());
                                rawMaterialStandardTreeDto.setCode(sampleDto.getCode());
                                rawMaterialStandardTreeDto.setLabel(sampleDto.getLabel());
                                rawMaterialStandardTreeDto.setValue(sampleDto.getValue());
                                rawMaterialStandardTreeDto.setChildren2(sampleDto.getChildren());
                            }
                        }
                    }
                }
            }
        }
        return Result.success(rawMaterialStandardTreeDto);
    }
 
    @Override
    public IPage<IfsInventoryQuantity> getWarehouseSubmit(IPage<IfsInventoryQuantity> page, IfsInventoryQuantity ifsInventoryQuantity) {
        return standardTreeMapper.selectIfsPage(page, QueryWrappers.queryWrappers(ifsInventoryQuantity));
    }
 
    @Override
    public IPage<IfsInventoryQuantityDto> getIfsByStateOne(IPage<IfsInventoryQuantityDto> page, IfsInventoryQuantityDto ifsInventoryQuantityDto) {
        return standardTreeMapper.getIfsByStateOne(page, QueryWrappers.queryWrappers(ifsInventoryQuantityDto));
    }
 
 
    /**
     * 报检
     * @param ids
     * @return
     */
    @Override
    public int inspectionReport(List<Integer> ids) {
        Integer userId = SecurityUtils.getUserId().intValue();
        ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                .in(IfsInventoryQuantity::getId, ids)
                .set(IfsInventoryQuantity::getDeclareUser, userMapper.selectById(userId).getName())
                .set(IfsInventoryQuantity::getDeclareUserId, userId)
                .set(IfsInventoryQuantity::getIsInspect, 1)
                .set(IfsInventoryQuantity::getDeclareDate, LocalDateTime.now())
        );
        threadPoolTaskExecutor.execute(() -> {
            List<IfsInventoryQuantity> quantityList = ifsInventoryQuantityMapper.selectList(Wrappers.<IfsInventoryQuantity>lambdaQuery()
                    .in(IfsInventoryQuantity::getId, ids));
            // 企业微信通知
            String message = "";
            message += "新增报检通知";
            for (IfsInventoryQuantity inventoryQuantity : quantityList) {
                message += "\n批次号: " + inventoryQuantity.getUpdateBatchNo();
                message += "\n零件描述: " + inventoryQuantity.getPartDesc();
                message += "\n抵达数量: " + inventoryQuantity.getQtyArrived().stripTrailingZeros().toPlainString() + inventoryQuantity.getBuyUnitMeas();
 
                // 判断有没有到20吨. 或者能否免检
                int result = notificationRawOrder(inventoryQuantity.getId());
                switch (result) {
                    case 1:
                        message += "\n当前样品已检验过, 可以免检";
                        break;
                    case 2:
                        message += "\n当前样品已超过20吨";
                        break;
                }
                message += "\n";
            }
            WxCpUtils.informWebHook(wechatProperty.getExaminingUrl(), message);
        });
        return 1;
    }
 
    /**
     * 撤销报检
     * @param id
     * @return
     */
    @Override
    public int revokeInspectionReport(Integer id) {
        return ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                .eq(IfsInventoryQuantity::getId, id)
                .set(IfsInventoryQuantity::getIsInspect, 0)
        );
    }
 
    /**
     * 打印标签查询
     * @param ids
     * @return
     */
    @Override
    public List<IfsInventoryQuantityDto> printLabel(List<Integer> ids) {
        return ifsInventoryQuantityMapper.printLabel(ids);
    }
 
    /**
     * 报检
     * @param ifsInventoryQuantity
     * @return
     */
    @Override
    public int inspectionReportOne(IfsInventoryQuantity ifsInventoryQuantity) {
        Integer userId = SecurityUtils.getUserId().intValue();
        ifsInventoryQuantityMapper.update(null, Wrappers.<IfsInventoryQuantity>lambdaUpdate()
                .eq(IfsInventoryQuantity::getId, ifsInventoryQuantity.getId())
                .set(IfsInventoryQuantity::getDeclareUser, userMapper.selectById(userId).getName())
                .set(IfsInventoryQuantity::getDeclareUserId, userId)
                .set(IfsInventoryQuantity::getIsInspect, 1)
                .set(IfsInventoryQuantity::getDeclareDate, LocalDateTime.now())
                .set(IfsInventoryQuantity::getUpdateBatchNo, ifsInventoryQuantity.getUpdateBatchNo())
        );
 
        threadPoolTaskExecutor.execute(() -> {
            IfsInventoryQuantity inventoryQuantity = ifsInventoryQuantityMapper.selectById(ifsInventoryQuantity.getId());
            // 企业微信通知
            String message = "";
            message += "新增报检通知";
            message += "\n批次号: " + inventoryQuantity.getUpdateBatchNo();
            message += "\n零件描述: " + inventoryQuantity.getPartDesc();
            message += "\n抵达数量: " + inventoryQuantity.getQtyArrived().stripTrailingZeros().toPlainString() + inventoryQuantity.getBuyUnitMeas();
            WxCpUtils.informWebHook(wechatProperty.getExaminingUrl(), message);
        });
        return 1;
    }
 
    /**
     * 获取铜产业链检测数据
     * @param id
     * @return
     */
    @Override
    public String getIndustryChain(Integer id) {
        return ifsInventoryQuantityMapper.selectById(id).getIndustryChain();
    }
 
    /**
     * 原材料撤销接口
     * @param ifsInventoryId
     * @return
     */
    @Override
    public boolean repealRawOrder(Integer ifsInventoryId) {
        // 查询判断是否是铜单丝
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(ifsInventoryId);
        if (ifsInventoryQuantity.getIsCopper() != null && ifsInventoryQuantity.getIsCopper().equals(1)) {
            // 铜单丝直接删除就行
            ifsInventoryQuantityMapper.deleteById(ifsInventoryId);
        } else {
            ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                    .set(IfsInventoryQuantity::getState, 0)
                    .set(IfsInventoryQuantity::getIsQuarter, 1)
                    .eq(IfsInventoryQuantity::getId, ifsInventoryId));
        }
 
        insOrderService.update(Wrappers.<InsOrder>lambdaUpdate()
                .eq(InsOrder::getIfsInventoryId, ifsInventoryId)
                .set(InsOrder::getState, -1)
                .set(InsOrder::getEntrustCode, ""));// 撤销
        return true;
    }
 
    /**
     * 原材料免检下单
     * @param list
     * @param insOrder
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addExemptionOrder(List<SampleProductDto> list, InsOrder insOrder) {
        if (!insOrder.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY)) {
            throw new ErrorException("只有进厂检验才能免检");
        }
        insOrder.setSendTime(LocalDateTime.now());
        // 修改订单表直接为已检验
        insOrder.setState(4);
        insOrder.setTypeSource(1);
 
        String code = "Y";
        // 生成编号
        String no = numberGenerator.generateNumberWithPrefix(3,
                "JCZX/ZB-" + code + LimsDateUtil.resetDate(LocalDateTime.now()),
                InsOrder::getEntrustCode);
 
        insOrderMapper.insert(insOrder); // 主表
 
        AtomicInteger count = new AtomicInteger();
        list.forEach(a -> {
            count.getAndIncrement();
            a.setId(null);
            a.setInsOrderId(insOrder.getId());
            if (StrUtil.isEmpty(a.getSampleCode())) {
                // 如果只有一个样品就不需要拼接数字
                if (list.size() != 1) {
                    a.setSampleCode(no + "-" + count.get());
                } else {
                    a.setSampleCode(no);
                }
            }
            insSampleMapper.insert(a);
 
            if (!CollectionUtil.isEmpty(a.getInsProduct())) {
                throw new ErrorException("免检不需要填写检验项");
            }
        });
 
        // 原材料下单: 委托人就是报检人, 生产单位就是供应商单位
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(insOrder.getIfsInventoryId());
        Integer declareUserId = ifsInventoryQuantity.getDeclareUserId();
        User user = userMapper.selectById(declareUserId);
        // 供应商名称
        insOrder.setProduction(ifsInventoryQuantity.getSupplierName());
        // 委托人名称
        insOrder.setPrepareUser(user.getName());
        insOrder.setPhone(user.getPhone());
 
        // 修改原材料数据直接为已检验
        ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>().set(IfsInventoryQuantity::getState, 2)
                .eq(IfsInventoryQuantity::getId, insOrder.getIfsInventoryId()));
 
        insOrder.setExamineTime(LocalDateTime.now());
 
        insOrder.setEntrustCode(no);
        insOrder.setIsExemption(1);
 
        insOrderMapper.updateById(insOrder);
 
        // 添加工时
        addAuxiliary(insOrder, ifsInventoryQuantity);
 
        // todo: ifs直接移库
        insReportService.isRawMaterial(insOrder);
 
        return insOrder.getId();
    }
 
 
    /**
     * 查询待下单
     * @param page
     * @param ifsInventoryQuantity
     * @return
     */
    @Override
    public IPage<IfsInventoryQuantityCheckDto> selectIfsInventoryQuantity(Page<IfsInventoryQuantityCheckDto> page, IfsInventoryQuantityCheckDto ifsInventoryQuantity) {
        return standardTreeMapper.selectIfsInventoryQuantity(page, QueryWrappers.queryWrappers(ifsInventoryQuantity));
    }
 
    /**
     * 查询已检验
     * @param page
     * @param ifsInventoryQuantityDto
     * @return
     */
    @Override
    public IPage<IfsInventoryQuantitySupplierDto> getIfsByOver(Page<IfsInventoryQuantitySupplierDto> page, IfsInventoryQuantitySupplierDto ifsInventoryQuantityDto) {
        // todo: 只看我
        String beginDeclareDate = ifsInventoryQuantityDto.getBeginDeclareDate();
        String endDeclareDate = ifsInventoryQuantityDto.getEndDeclareDate();
        ifsInventoryQuantityDto.setBeginDeclareDate(null);
        ifsInventoryQuantityDto.setEndDeclareDate(null);
 
        IPage<IfsInventoryQuantitySupplierDto> ifsByOver = standardTreeMapper.getIfsByOver(page, QueryWrappers.queryWrappers(ifsInventoryQuantityDto), beginDeclareDate, endDeclareDate);
        return ifsByOver;
    }
 
    /**
     * 删除原材料报检信息
     * @param id
     * @return
     */
    @Override
    public int delIfsInventory(Integer id) {
        return ifsInventoryQuantityMapper.deleteById(id);
    }
 
    /**
     * 原材料放行免检
     * @param ifsInventoryId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rawOrderRelease(Integer ifsInventoryId, String partDetail) {
        // 修改原材料数据直接为已检验
        ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                .set(IfsInventoryQuantity::getState, 2)
                .set(IfsInventoryQuantity::getIsQuarter, 0)
                .eq(IfsInventoryQuantity::getId, ifsInventoryId));
 
        // 查询原材料信息
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(ifsInventoryId);
        // 委托人就是报检人, 生产单位就是供应商单位
        Integer declareUserId = ifsInventoryQuantity.getDeclareUserId();
        User user = userMapper.selectById(declareUserId);
 
        InsOrder insOrder = new InsOrder();
        insOrder.setState(4);
        insOrder.setTypeSource(1);
        String code = "Y";
        // 生成编号
        String no = numberGenerator.generateNumberWithPrefix(3,
                "JCZX/ZB-" + code + LimsDateUtil.resetDate(LocalDateTime.now()),
                InsOrder::getEntrustCode);
        insOrder.setExamineTime(LocalDateTime.now());
        insOrder.setEntrustCode(no);
        insOrder.setIsExemption(1);
        // 供应商名称
        insOrder.setProduction(ifsInventoryQuantity.getSupplierName());
        // 委托人名称
        insOrder.setPrepareUser(user.getName());
        insOrder.setPhone(user.getPhone());
        insOrder.setIfsInventoryId(ifsInventoryId);
        insOrder.setPartDetail(partDetail);
        insOrder.setSendTime(LocalDateTime.now());
        insOrder.setSample(ifsInventoryQuantity.getPartDesc());
        // 进厂检验
        insOrder.setOrderType(InsOrderTypeConstants.ENTER_THE_FACTORY);
 
        insOrderMapper.insert(insOrder);
 
        // 添加工时
        addAuxiliary(insOrder, ifsInventoryQuantity);
 
        // todo: ifs直接移库
        insReportService.isRawMaterial(insOrder);
        return true;
    }
 
    /**
     * 0, 无提示, 1提示  当前批次的样品已检验过, 可以免检, 2 提示 当前批次的样品已超20吨, 需要多级多次检验
     *
     * 原材料下单通知免检或者多次检验
     * @param ifsInventoryId
     * @return
     */
    @Override
    public int notificationRawOrder(Integer ifsInventoryId) {
        IfsInventoryQuantity ifsInventory = ifsInventoryQuantityMapper.selectById(ifsInventoryId);
        // 查询当前批次, 供应商, 零件号的原材料是否超过了20吨, 超过了20吨需要进行多次检验提醒
        List<IfsInventoryQuantity> quantityList = ifsInventoryQuantityMapper.selectList(Wrappers.<IfsInventoryQuantity>lambdaQuery()
                .eq(IfsInventoryQuantity::getPartNo, ifsInventory.getPartNo())
                .eq(IfsInventoryQuantity::getUpdateBatchNo, ifsInventory.getUpdateBatchNo())
                .eq(IfsInventoryQuantity::getSupplierId, ifsInventory.getSupplierId())
                .eq(IfsInventoryQuantity::getSupplierName, ifsInventory.getSupplierName()));
 
        // 判断是否大于20吨
        BigDecimal bigDecimal = new BigDecimal("20000");
        BigDecimal count = BigDecimal.ZERO;
        for (IfsInventoryQuantity inventoryQuantity : quantityList) {
            // 判断单位是kg或者t
            if (inventoryQuantity.getBuyUnitMeas().equalsIgnoreCase("t")) {
                count = count.add(inventoryQuantity.getQtyArrived().multiply(new BigDecimal("1000")));
            } else if (inventoryQuantity.getBuyUnitMeas().equalsIgnoreCase("kg")) {
                count = count.add(inventoryQuantity.getQtyArrived());
            }
        }
 
        if (count.compareTo(bigDecimal) >= 0) {
            return 2;
        } else {
            // 判断之前是否出过报告, 出过报告可以免检
            int reportCount = ifsInventoryQuantityMapper.selectReportCountById(ifsInventoryId);
            if (reportCount > 0) {
                return 1;
            }
        }
        return 0;
    }
 
    /**
     * 报检可以新增报检信息
     * @param ifsInventoryQuantity
     */
    @Override
    public void addIfsInventoryQuantity(IfsInventoryQuantity ifsInventoryQuantity) {
        ifsInventoryQuantity.setIsSource(0);
        ifsInventoryQuantity.setState(0);
        ifsInventoryQuantity.setIsFinish(0);
        ifsInventoryQuantityMapper.insert(ifsInventoryQuantity);
    }
 
 
 
 
    /**
     * 铜单丝下单免检
     * @param list
     * @param insOrder
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRawCopperOrderExemptionOrder(List<SampleProductDto> list, CopperInsOrderDto insOrder) {
        if (!insOrder.getOrderType().equals(InsOrderTypeConstants.ENTER_THE_FACTORY)) {
            throw new ErrorException("只有进厂检验才能免检");
        }
        insOrder.setSendTime(LocalDateTime.now());
        // 修改订单表直接为已检验
        insOrder.setState(4);
        insOrder.setTypeSource(1);
 
        String code = "Y";
        // 生成编号
        String no = numberGenerator.generateNumberWithPrefix(3,
                "JCZX/ZB-" + code + LimsDateUtil.resetDate(LocalDateTime.now()),
                InsOrder::getEntrustCode);
 
        insOrderMapper.insert(insOrder); // 主表
 
        AtomicInteger count = new AtomicInteger();
        list.forEach(a -> {
            count.getAndIncrement();
            a.setId(null);
            a.setInsOrderId(insOrder.getId());
            if (StrUtil.isEmpty(a.getSampleCode())) {
                // 如果只有一个样品就不需要拼接数字
                if (list.size() != 1) {
                    a.setSampleCode(no + "-" + count.get());
                } else {
                    a.setSampleCode(no);
                }
            }
            insSampleMapper.insert(a);
 
            if (!CollectionUtil.isEmpty(a.getInsProduct())) {
                throw new ErrorException("免检不需要填写检验项");
            }
        });
        // 添加原材料信息
        IfsInventoryQuantity ifsInventoryQuantity = new IfsInventoryQuantity();
        // 基本信息
        ifsInventoryQuantity.setIsSource(0);
        ifsInventoryQuantity.setState(2);
        ifsInventoryQuantity.setIsInspect(1);
        ifsInventoryQuantity.setIsFinish(1);
        ifsInventoryQuantity.setIsCopper(1);
        ifsInventoryQuantity.setInspectStatus(1);
        ifsInventoryQuantity.setIsQuarter(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.setExamineTime(LocalDateTime.now());
 
        insOrder.setEntrustCode(no);
        insOrder.setIsExemption(1);
 
        insOrderMapper.updateById(insOrder);
 
        // 添加工时
        addAuxiliary(insOrder, ifsInventoryQuantity);
 
        return insOrder.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean concessionRelease(Integer ifsInventoryId) {
        // 查询原材料信息
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(ifsInventoryId);
        if (!ifsInventoryQuantity.getInspectStatus().equals(2)) {
            throw new ErrorException("不合格的原材料才能让步放行");
        }
 
        // todo:需要判断oa流程是否是让步放行
        String toLocation = insReportService.moveRawMaterial(ifsInventoryQuantity);
 
        ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                .set(IfsInventoryQuantity::getInspectStatus, 4)
                .set(IfsInventoryQuantity::getToLocation, toLocation)
                .eq(IfsInventoryQuantity::getId, ifsInventoryId));
 
        return true;
    }
 
    /**
     * 原材料进厂撤销下单
     * @param enterOrderId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean repealEnterRawOrder(Integer enterOrderId) {
        // 查询订单
        InsOrder order = insOrderMapper.selectById(enterOrderId);
 
        // 查询判断是否是铜单丝
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(order.getIfsInventoryId());
        if (ifsInventoryQuantity.getIsCopper() != null && ifsInventoryQuantity.getIsCopper().equals(1)) {
            // 铜单丝直接删除就行
            ifsInventoryQuantityMapper.deleteById(order.getIfsInventoryId());
        } else {
            ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                    .set(IfsInventoryQuantity::getState, 0)
                    .set(IfsInventoryQuantity::getIsQuarter, 1)
                    .eq(IfsInventoryQuantity::getId, order.getIfsInventoryId()));
        }
        insOrderService.update(Wrappers.<InsOrder>lambdaUpdate()
                .eq(InsOrder::getId, enterOrderId)
                .set(InsOrder::getState, -1)
                .set(InsOrder::getEntrustCode, ""));// 撤销
 
        // 清除之前的工时
        auxiliaryOutputWorkingHoursMapper.delete(Wrappers.<AuxiliaryOutputWorkingHours>lambdaQuery()
                .eq(AuxiliaryOutputWorkingHours::getOrderId, enterOrderId));
 
        // 清除之前报告
        insReportService.remove(Wrappers.<InsReport>lambdaQuery()
                .eq(InsReport::getInsOrderId, enterOrderId));
 
        return true;
    }
 
    /**
     * 原材料季度撤销下单
     * @param quarterOrderId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean repealQuarterRawOrder(Integer quarterOrderId) {
        // 查询订单
        InsOrder order = insOrderMapper.selectById(quarterOrderId);
 
        Long count = insOrderMapper.selectCount(Wrappers.<InsOrder>lambdaQuery()
                .eq(InsOrder::getIfsInventoryId, order.getIfsInventoryId())
                .ne(InsOrder::getState, -1)
                .eq(InsOrder::getOrderType, InsOrderTypeConstants.ENTER_THE_FACTORY)
                .ne(InsOrder::getId, order.getId()));
        // 判断之前是否有进厂检验, 没有需要修改原材料信息
        if (count == 0) {
            ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                    .set(IfsInventoryQuantity::getState, 0)
                    .set(IfsInventoryQuantity::getIsQuarter, 1)
                    .eq(IfsInventoryQuantity::getId, order.getIfsInventoryId()));
        } else {
            ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                    .set(IfsInventoryQuantity::getIsQuarter, 1)
                    .eq(IfsInventoryQuantity::getId, order.getIfsInventoryId()));
        }
 
        insOrderService.update(Wrappers.<InsOrder>lambdaUpdate()
                .eq(InsOrder::getId, quarterOrderId)
                .set(InsOrder::getState, -1)
                .set(InsOrder::getEntrustCode, ""));// 撤销
        auxiliaryOutputWorkingHoursMapper.delete(Wrappers.<AuxiliaryOutputWorkingHours>lambdaQuery()
                .eq(AuxiliaryOutputWorkingHours::getOrderId, quarterOrderId));
        // 清除之前报告
        insReportService.remove(Wrappers.<InsReport>lambdaQuery()
                .eq(InsReport::getInsOrderId, quarterOrderId));
        return true;
    }
 
    /**
     * 原材料报检全部信息导出
     * @param ifsInventoryQuantityDto
     * @param response
     */
    @Override
    public void rawAllExport(IfsInventoryQuantitySupplierDto ifsInventoryQuantityDto, HttpServletResponse response) throws UnsupportedEncodingException {
        // 判断是否是根据选择的导出
        List<IfsInventoryQuantitySupplierDto> ifsByOverList = new ArrayList<>();
        if (StringUtils.isNotBlank(ifsInventoryQuantityDto.getIds())) {
            List<String> ifsIds = StrUtil.split(ifsInventoryQuantityDto.getIds(), ",");
            ifsByOverList = standardTreeMapper.getIfsByIds(ifsIds);
        } else {
            String beginDeclareDate = ifsInventoryQuantityDto.getBeginDeclareDate();
            String endDeclareDate = ifsInventoryQuantityDto.getEndDeclareDate();
            ifsInventoryQuantityDto.setBeginDeclareDate(null);
            ifsInventoryQuantityDto.setEndDeclareDate(null);
            ifsInventoryQuantityDto.setIds(null);
            ifsByOverList = standardTreeMapper.getIfsByOverList(QueryWrappers.queryWrappers(ifsInventoryQuantityDto), beginDeclareDate, endDeclareDate);
        }
 
        for (IfsInventoryQuantitySupplierDto dto : ifsByOverList) {
            dto.setSendTimeString(dto.getSendTime() == null ? "" : dto.getSendTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dto.setReceiverDateString(dto.getReceiverDate() == null ? "" : dto.getReceiverDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dto.setDeclareDateString(dto.getDeclareDate() == null ? "" : dto.getDeclareDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            switch (dto.getInspectStatus()) {
                case 1:
                    dto.setInspectStatusString("合格");
                    break;
                case 2:
                    dto.setInspectStatusString("不合格");
                    // 查询不合格项
                    List<String> unqualifiedList = insProductMapper.selectUnqualifiedList(dto.getEnterOrderId() == null ? dto.getQuarterOrderId() : dto.getEnterOrderId());
                    dto.setUnqualifiedItem(CollUtil.join(unqualifiedList, ","));
                    break;
                case 3:
                    dto.setInspectStatusString("未下单");
                    break;
                case 4:
                    dto.setInspectStatusString("让步放行");
                    break;
                case 0:
                    dto.setInspectStatusString("检验中");
                    break;
 
            }
        }
 
 
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("原材料检测信息导出", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        try {
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "原材料检测信息导出").head(IfsInventoryQuantitySupplierDto.class).build();
 
            //向sheet0写入数据 传入空list这样只导出表头
            excelWriter.write(ifsByOverList, mainSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 原材料查询可以季度检验的内容
     * @param page
     * @param ifsInventoryQuantityDto
     * @return
     */
    @Override
    public IPage<IfsInventoryQuantitySupplierDto> getIfsByQuarter(Page page, IfsInventoryQuantitySupplierDto ifsInventoryQuantityDto) {
 
        // todo: 只看我
        String beginDeclareDate = ifsInventoryQuantityDto.getBeginDeclareDate();
        String endDeclareDate = ifsInventoryQuantityDto.getEndDeclareDate();
        ifsInventoryQuantityDto.setBeginDeclareDate(null);
        ifsInventoryQuantityDto.setEndDeclareDate(null);
 
        return standardTreeMapper.getIfsByQuarter(page, QueryWrappers.queryWrappers(ifsInventoryQuantityDto), beginDeclareDate, endDeclareDate);
    }
 
    /**
     * 提前入库
     * @param ifsInventoryId
     * @return
     */
    @Override
    public boolean advancedGodown(Integer ifsInventoryId) {
        // 查询原材料信息
        IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(ifsInventoryId);
        if (!ifsInventoryQuantity.getInspectStatus().equals(0)
                && !ifsInventoryQuantity.getInspectStatus().equals(3)) {
            throw new ErrorException("未检测完成数据才能提前入库");
        }
 
        // todo:需要判断oa流程是否是让步放行
        String toLocation = insReportService.moveRawMaterial(ifsInventoryQuantity);
 
        ifsInventoryQuantityMapper.update(null, new LambdaUpdateWrapper<IfsInventoryQuantity>()
                .set(IfsInventoryQuantity::getInspectStatus, 1)
                .set(IfsInventoryQuantity::getIsFinish, 1)
                .set(IfsInventoryQuantity::getToLocation, toLocation)
                .eq(IfsInventoryQuantity::getId, ifsInventoryId));
 
        return true;
    }
 
 
    /**
     * 添加工时
     * @param insOrder
     * @param ifsInventoryQuantity
     */
    private void addAuxiliary(InsOrder insOrder, IfsInventoryQuantity ifsInventoryQuantity) {
        AuxiliaryOutputWorkingHours auxiliaryOutputWorkingHours = new AuxiliaryOutputWorkingHours();
        auxiliaryOutputWorkingHours.setInspectionItemClass(ifsInventoryQuantity.getUpdateBatchNo() + "免检");//检测项分类
        auxiliaryOutputWorkingHours.setSample(insOrder.getEntrustCode());//样品编号
        auxiliaryOutputWorkingHours.setOrderId(insOrder.getId());//订单id
        auxiliaryOutputWorkingHours.setOrderNo(insOrder.getEntrustCode());//非加班委托单号
 
        // 免检默认2
        auxiliaryOutputWorkingHours.setWorkTime(new BigDecimal("2"));//非加班工时
        auxiliaryOutputWorkingHours.setAmount(1);//非加班数量
        auxiliaryOutputWorkingHours.setOutputWorkTime((ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getOvertimeWorkTime()) ? auxiliaryOutputWorkingHours.getOvertimeWorkTime() : BigDecimal.ZERO).add(ObjectUtils.isNotEmpty(auxiliaryOutputWorkingHours.getWorkTime()) ? auxiliaryOutputWorkingHours.getWorkTime() : BigDecimal.ZERO));//产量工时
        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(SecurityUtils.getUserId().intValue());//检测人
        auxiliaryOutputWorkingHours.setPrice(new BigDecimal("1"));//单价
 
        auxiliaryOutputWorkingHoursMapper.insert(auxiliaryOutputWorkingHours);
    }
 
    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 "未知";
        }
    }
}