yangys
2024-03-28 13ada1093cb8de6e31a718d2222429ded70133c8
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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
package com.qianwen.smartman.modules.cps.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import java.lang.invoke.SerializedLambda;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.smartman.common.cache.DictCache;
import com.qianwen.smartman.common.cache.RegionCache;
import com.qianwen.smartman.common.cache.cps.WorkstationCache;
import com.qianwen.smartman.common.cache.cps.WorkstationOfMachineCache;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.CommonGroupConstant;
import com.qianwen.smartman.common.constant.DateConstant;
import com.qianwen.smartman.common.constant.ExcelConstant;
import com.qianwen.smartman.common.constant.ExtCacheConstant;
import com.qianwen.smartman.common.enums.CommonGroupTypeEnum;
import com.qianwen.smartman.common.enums.DictEnum;
import com.qianwen.smartman.common.enums.StatusType;
import com.qianwen.smartman.common.enums.WcsDataTypeEnums;
import com.qianwen.smartman.common.utils.ListUtils;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.excel.util.ExcelUtil;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.mp.support.Condition;
import com.qianwen.core.mp.support.Query;
import com.qianwen.core.oss.model.BladeFile;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.modules.cps.convert.MachineConvert;
import com.qianwen.smartman.modules.cps.convert.WorkstationWcsConvert;
import com.qianwen.smartman.modules.cps.dto.DeviceMaintainInDTO;
import com.qianwen.smartman.modules.cps.dto.DeviceSimpleDTO;
import com.qianwen.smartman.modules.cps.dto.EmployeeNameDTO;
import com.qianwen.smartman.modules.cps.dto.MachineExtDTO;
import com.qianwen.smartman.modules.cps.dto.WorkstationWcsDTO;
import com.qianwen.smartman.modules.cps.entity.CommonGroupOfItem;
import com.qianwen.smartman.modules.cps.entity.DeviceType;
import com.qianwen.smartman.modules.cps.entity.Machine;
import com.qianwen.smartman.modules.cps.entity.Workstation;
import com.qianwen.smartman.modules.cps.entity.WorkstationOfMachine;
import com.qianwen.smartman.modules.cps.entity.WorkstationWcs;
import com.qianwen.smartman.modules.cps.excel.MachineExcel;
import com.qianwen.smartman.modules.cps.excel.MachineImport;
import com.qianwen.smartman.modules.cps.mapper.MachineMapper;
import com.qianwen.smartman.modules.cps.mapper.WorkstationWcsMapper;
import com.qianwen.smartman.modules.cps.service.ICommonGroupOfItemService;
import com.qianwen.smartman.modules.cps.service.ICommonGroupService;
import com.qianwen.smartman.modules.cps.service.IDeviceTypeService;
import com.qianwen.smartman.modules.cps.service.IEmployeeService;
import com.qianwen.smartman.modules.cps.service.IMachineService;
import com.qianwen.smartman.modules.cps.service.IWorkstationOfMachineService;
import com.qianwen.smartman.modules.cps.service.IWorkstationService;
import com.qianwen.smartman.modules.cps.utils.ThrowFun;
import com.qianwen.smartman.modules.cps.vo.IdsVO;
import com.qianwen.smartman.modules.cps.vo.MachineDetailVO;
import com.qianwen.smartman.modules.cps.vo.MachineListVO;
import com.qianwen.smartman.modules.cps.vo.MachineSelectVO;
import com.qianwen.smartman.modules.cps.vo.MachineSubmitVO;
import com.qianwen.smartman.modules.cps.vo.MachineUpdateVO;
import com.qianwen.smartman.modules.cps.vo.MachineVO;
import com.qianwen.smartman.modules.cps.vo.WorkstationSubmitVO;
import com.qianwen.smartman.modules.dnc.enums.DncEnums;
import com.qianwen.smartman.modules.dnc.vo.HmiDeviceVO;
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
import com.qianwen.smartman.modules.system.service.ICodeGeneratorService;
import com.qianwen.smartman.modules.tpm.entity.MaintainPlan;
import com.qianwen.smartman.modules.tpm.entity.RepairApply;
import com.qianwen.smartman.modules.tpm.entity.RepairRecord;
import com.qianwen.smartman.modules.tpm.enums.DeviceRepairStatusEnum;
import com.qianwen.smartman.modules.tpm.enums.MaintainPlanStatusEnum;
import com.qianwen.smartman.modules.tpm.enums.MaintainStatusEnum;
import com.qianwen.smartman.modules.tpm.enums.MetaTypeEnum;
import com.qianwen.smartman.modules.tpm.enums.RepairRecordEnum;
import com.qianwen.smartman.modules.tpm.service.IMaintainPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/cps/service/impl/MachineServiceImpl.class */
public class MachineServiceImpl extends BaseServiceImpl<MachineMapper, Machine> implements IMachineService {
    private static final Logger log = LoggerFactory.getLogger(MachineServiceImpl.class);
    private static final Pattern NUMBER = Pattern.compile("[1-9]\\d*.\\d*|0\\.\\d*[1-9]\\d*");
    private static final Pattern CHINESE = Pattern.compile("^[A-Za-z0-9]+$");
    @Autowired
    private IWorkstationOfMachineService workstationOfMachineService;
    @Autowired
    @Lazy
    private IMaintainPlanService maintainPlanService;
    @Autowired
    private ICommonGroupService commonGroupService;
    @Autowired
    private OssBuilder ossBuilder;
    @Autowired
    @Lazy
    private IDeviceTypeService typeService;
    @Autowired
    private ICommonGroupOfItemService itemService;
    @Autowired
    @Lazy
    private IWorkstationService workstationService;
    @Autowired
    private WorkstationWcsMapper workstationWcsMapper;
    @Autowired
    private ICodeGeneratorService codeGeneratorService;
    @Autowired
    private IEmployeeService employeeService;
 
 
    @Override // org.springblade.modules.cps.service.IMachineService
    @Transactional(rollbackFor = {Exception.class})
    public MachineVO insert(MachineVO machineVO) {
        checkMachine(machineVO.id, machineVO.getMachineCode());
        checkStopMachineByCode(machineVO.getMachineCode());
        Machine machine = MachineConvert.INSTANCE.convert(machineVO);
        machine.setMaintenanceStatus(Integer.valueOf(MaintainStatusEnum.T1.getType()));
        machine.setRepairStatus(Integer.valueOf(RepairRecordEnum.R1.getType()));
        save(machine);
        saveCommonGroupItem(Lists.newArrayList(new Machine[]{machine}));
        return MachineConvert.INSTANCE.convert(machine);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public Boolean changeCollectSwitch(Long workstationId, Integer collectSwitch) {
        ((MachineMapper) this.baseMapper).changeCollectSwitch(workstationId, collectSwitch, AuthUtil.getTenantId());
        if (0 == collectSwitch.intValue()) {
            WorkstationOfMachine workstationOfMachineServiceOne = this.workstationOfMachineService.getWorkstationOfMachineByWorkstationId(workstationId);
            if (Func.isNotEmpty(workstationOfMachineServiceOne)) {
                WorkstationCache.clearWorkstationParamTypeCache(String.valueOf(workstationOfMachineServiceOne.getMachineId()));
            }
        }
        return true;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<WorkstationWcsDTO> getWorkstationWcsByMachineId(Long machineId, String paramName) {
        List<WorkstationOfMachine> workstationOfMachineList = this.workstationOfMachineService.list(Wrappers.<WorkstationOfMachine>lambdaQuery().eq(WorkstationOfMachine::getMachineId, machineId));
        
        List<Long> workstationIdList =workstationOfMachineList.stream().map((v0) -> {
            return v0.getWorkstationId();
        }).collect(Collectors.toList());
        List<WorkstationWcs> workstationWcsList = this.workstationWcsMapper.getWorkstationWcsByName(workstationIdList, paramName);
        return WorkstationWcsConvert.INSTANCE.convertToDTOList(workstationWcsList);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public Boolean updateBrand(Long id, String brand) {
        Machine machine = (Machine) getById(id);
        machine.setBrand(brand);
        boolean updateById = updateById(machine);
        if (updateById) {
            CacheUtil.clear(ExtCacheConstant.POSTING_MACHINE, Boolean.FALSE);
            CacheUtil.clear(ExtCacheConstant.WORKSTATION_MACHINE, Boolean.FALSE);
        }
        return Boolean.valueOf(updateById);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public MachineDetailVO detailMachine(Long id) {
        MachineDetailVO machineDetailVO = ((MachineMapper) this.baseMapper).selectMachineDetail(id);
        if (Func.isNotEmpty(machineDetailVO)) {
            machineDetailVO.setMachineLifeStatusName(DictCache.getValue(DictEnum.MACHINE_LIFE_STATUS.getName(), machineDetailVO.getMachineLifeStatus()));
            machineDetailVO.setManagementClassName(DictCache.getValue(DictEnum.MACHINE_MANAGEMENT_CLASS, machineDetailVO.getManagementClass()));
            machineDetailVO.setMachineUseStatusName(DictCache.getValue(DictEnum.MACHINE_USE_STATUS.getName(), machineDetailVO.getMachineUseStatus()));
            List<WorkstationOfMachine> workstationOfMachineList = this.workstationOfMachineService.list(Wrappers.<WorkstationOfMachine>lambdaQuery().eq(WorkstationOfMachine::getMachineId, machineDetailVO.getId()));
            if (Func.isNotEmpty(workstationOfMachineList)) {
                List<Workstation> workstationList = this.workstationService.listByIds(workstationOfMachineList.stream().map((v0) -> {
                    return v0.getWorkstationId();
                }).collect(Collectors.toList()));
                Optional.ofNullable(workstationList).ifPresent(workstations -> {
                    List<String> workstationCodeList = workstations.stream().map((v0) -> {
                        return v0.getCode();
                    }).collect(Collectors.toList());
                    List<String> workstationNameList = workstations.stream().map((v0) -> {
                        return v0.getName();
                    }).collect(Collectors.toList());
                    String code = workstationCodeList.stream().reduce((a, b) -> {
                        return a.concat(";").concat(b);
                    }).orElse("");
                    String name = workstationNameList.stream().reduce((a2, b2) -> {
                        return a2.concat(";").concat(b2);
                    }).orElse("");
                    machineDetailVO.setWorkstationCode(code);
                    machineDetailVO.setWorkstationName(name);
                });
            }
        }
        return machineDetailVO;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    @Transactional(rollbackFor = {Exception.class})
    public Machine createMachine(MachineSubmitVO machineSubmitVO) {
        String pinCode = machineSubmitVO.getPinCode();
        if (Func.isNotBlank(pinCode)) {
            checkPinCode(pinCode, null);
            boolean matches = CHINESE.matcher(pinCode).matches();
            if (!matches) {
                throw new ServiceException(MessageUtils.message("machine.ping.code.chinese.characters.exist", new Object[0]));
            }
        }
        String shortCode = machineSubmitVO.getShortCode();
        if (Func.isNotBlank(shortCode)) {
            validShortCode(shortCode, null);
            boolean matches2 = CHINESE.matcher(shortCode).matches();
            if (!matches2) {
                throw new ServiceException(MessageUtils.message("machine.short.code.chinese.characters.exist", new Object[0]));
            }
        }
        if (Func.isBlank(machineSubmitVO.getMachineCode())) {
            String code = this.codeGeneratorService.getGeneratorCode(machineSubmitVO, MetaTypeEnum.MACHINE.getCode());
            log.info("机器编码规则生成的code:{}", code + " ," + code.length());
            machineSubmitVO.setMachineCode(code);
        }
        checkMachine(null, machineSubmitVO.getMachineCode());
        checkStopMachine(machineSubmitVO.getMachineCode());
        Machine machine = MachineConvert.INSTANCE.convert(machineSubmitVO);
        machine.setMaintenanceStatus(Integer.valueOf(MaintainStatusEnum.T1.getType()));
        machine.setRepairStatus(Integer.valueOf(DeviceRepairStatusEnum.NORMAL.getType()));
        machine.setExtendId(UUID.randomUUID().toString());
        machine.setExternalKey(machineSubmitVO.getMachineCode());
        save(machine);
        saveWorkstation(machineSubmitVO, machine);
        CommonGroupOfItem commonGroupOfItem = CommonGroupOfItem.builder().groupId(machine.getGroupId()).itemId(machine.getId()).extendId(machine.getExtendId()).groupType(CommonGroupTypeEnum.MACHINE.getName()).groupCategory(CommonGroupConstant.DEFAULT_CATEGORY).build();
        this.itemService.save(commonGroupOfItem);
        CacheUtil.clear(ExtCacheConstant.POSTING_MACHINE, Boolean.FALSE);
        CacheUtil.clear(ExtCacheConstant.WORKSTATION_MACHINE, Boolean.FALSE);
        return machine;
    }
 
    @Transactional(rollbackFor = {Exception.class})
    public void saveWorkstation(MachineSubmitVO machineSubmitVO, Machine machine) {
        if (1 == machineSubmitVO.getLinkWay().intValue()) {
            WorkstationSubmitVO workstationSaveVO = new WorkstationSubmitVO();
            workstationSaveVO.setCode(machineSubmitVO.getMachineCode());
            workstationSaveVO.setName(machineSubmitVO.getMachineName());
            workstationSaveVO.setGroupId(CommonGroupConstant.DEFAULT_WORKSTATION_ID);
            workstationSaveVO.setLinkWay(0);
            workstationSaveVO.setMachineId(machine.getId());
            workstationSaveVO.setType(0);
            this.workstationService.submit(workstationSaveVO);
        }
    }
 
    private void validShortCode(String shortCode, Long id) {
        int len = shortCode.length();
        if (len < 4 || len > 8) {
            throw new ServiceException(String.format(MessageUtils.message("cps.tpm.machine.short.code.length", new Object[0]), 4, 8));
        }
 
        Long count = Long.valueOf(count(Wrappers.<Machine>lambdaQuery().eq(Machine::getShortCode, shortCode).ne(Func.isNotEmpty(id), Machine::getId, id)));
        if (count.longValue() > 0) {
            throw new ServiceException(MessageUtils.message("cps.tpm.machine.short.code.exists", new Object[0]));
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public IPage<MachineListVO> pageMachine(MachineSelectVO machineSelectVO, Query query) {
        String machineName = machineSelectVO.getMachineName();
        String machineCode = machineSelectVO.getMachineCode();
        Long machineTypeId = machineSelectVO.getMachineTypeId();
        Long groupId = machineSelectVO.getGroupId();
        Integer status = machineSelectVO.getStatus();
        IPage<MachineListVO> page = ((MachineMapper) this.baseMapper).pageMachine(Condition.getPage(query), machineName, machineCode, machineTypeId, groupId, CommonGroupConstant.ALL_MACHINE_ID, status == null ? CommonConstant.ENABLE : status);
        List<MachineListVO> records = page.getRecords();
        List<MachineListVO> collect = records.stream().peek(c -> {
            c.setMachineUseStatusName(DictCache.getValue(DictEnum.MACHINE_USE_STATUS.getName(), c.getMachineUseStatus()));
        }).collect(Collectors.toList());
        page.setRecords(collect);
        return page;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    @Transactional(rollbackFor = {Exception.class})
    public Boolean updateMachine(MachineUpdateVO vo) {
        Long id = vo.getId();
        int nums = ((MachineMapper) this.baseMapper).typeAndonRecord(Lists.newArrayList(new Long[]{id})).intValue();
        if (nums > 0) {
            throw new ServiceException(MessageUtils.message("machine.andon.not.update", new Object[0]));
        }
        String machineCode = vo.getMachineCode();
        checkMachine(id, machineCode);
        String pinCode = vo.getPinCode();
        if (Func.isNotBlank(pinCode)) {
            checkPinCode(pinCode, id);
        }
        String shortCode = vo.getShortCode();
        if (Func.isNotBlank(shortCode)) {
            validShortCode(shortCode, id);
        }
        Machine machine = MachineConvert.INSTANCE.convert(vo);
        boolean updateById = updateById(machine);
        Long groupId = machine.getGroupId();
        if (!Func.isNull(groupId)) {
            this.itemService.update(Wrappers.<CommonGroupOfItem>lambdaUpdate()
                      .set(CommonGroupOfItem::getGroupId, groupId)
                      .set(CommonGroupOfItem::getExtendId, machine.getExtendId())
                      .eq(CommonGroupOfItem::getItemId, machine.getId())); 
            /*
            this.itemService.update((Wrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) Wrappers.lambdaUpdate().set((v0) -> {
                return v0.getGroupId();
            }, groupId)).set((v0) -> {
                return v0.getExtendId();
            }, machine.getExtendId())).eq((v0) -> {
                return v0.getItemId();
            }, machine.getId()));*/
        }
        if (updateById) {
            CacheUtil.clear(ExtCacheConstant.POSTING_MACHINE, Boolean.FALSE);
            CacheUtil.clear(ExtCacheConstant.WORKSTATION_MACHINE, Boolean.FALSE);
        }
        return Boolean.valueOf(updateById);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void renewMachineRepairStatus(Long machineId, Integer maintenanceStatus) {
        Machine machine = (Machine)getOne(new QueryWrapper<Machine>().lambda().eq(Machine::getId, machineId));
     
        if (machine != null) {
            machine.setRepairStatus(maintenanceStatus);
            updateById(machine);
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void updateMachineRepairPriority(Long deviceId, List<RepairRecord> toBeConfirmedRecordList, List<RepairApply> inMaintenanceApplyList, List<RepairApply> toBeRepairedApplyList) {
        if (Func.isNotEmpty(toBeConfirmedRecordList)) {
            renewMachineRepairStatus(deviceId, Integer.valueOf(DeviceRepairStatusEnum.TO_BE_CONFIRM.getType()));
        } else if (Func.isNotEmpty(inMaintenanceApplyList)) {
            renewMachineRepairStatus(deviceId, Integer.valueOf(DeviceRepairStatusEnum.IN_MAINTENANCE.getType()));
        } else if (Func.isNotEmpty(toBeRepairedApplyList)) {
            renewMachineRepairStatus(deviceId, Integer.valueOf(DeviceRepairStatusEnum.TO_BE_REPAIRED.getType()));
        } else {
            renewMachineRepairStatus(deviceId, Integer.valueOf(DeviceRepairStatusEnum.NORMAL.getType()));
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void renewMachineRepairTime(Long deviceId) {
        Machine machine = (Machine) getById(deviceId);
        if (machine != null) {
            machine.setLastRepairTime(LocalDateTime.now());
            updateById(machine);
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void createRepairApplyUpdateMachine(Long deviceId, Integer maintenanceStatus) {
        Machine machine = (Machine) getById(deviceId);
        if (machine != null) {
            if (Func.isNull(machine.getRepairStatus())) {
                throw new ServiceException(MessageUtils.message("cps.tpm.machine.repair.status.is.not.empty", new Object[0]));
            }
            if (machine.getRepairStatus().equals(Integer.valueOf(DeviceRepairStatusEnum.NORMAL.getType()))) {
                renewMachineRepairStatus(deviceId, maintenanceStatus);
            }
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void implementRepairApply(Long deviceId, Integer maintenanceStatus) {
        Machine machine = (Machine) getById(deviceId);
        if (Func.isNull(machine.getRepairStatus())) {
            throw new ServiceException(MessageUtils.message("cps.tpm.machine.repair.status.is.not.empty", new Object[0]));
        }
        if (!machine.getRepairStatus().equals(Integer.valueOf(DeviceRepairStatusEnum.IN_MAINTENANCE.getType())) || !machine.getRepairStatus().equals(Integer.valueOf(DeviceRepairStatusEnum.IN_MAINTENANCE.getType()))) {
            renewMachineRepairStatus(deviceId, maintenanceStatus);
        }
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void updateDeviceMaintainStatus(Long deviceId, Integer status, Long planId, LocalDateTime nextMaintainTime) {
        List<MaintainPlan> checkList = checkExistPlanStatus(deviceId, null);
        List<MaintainPlan> checkTimeList = (List) checkList.stream().sorted(Comparator.comparing((v0) -> {
            return v0.getMaintainDate();
        })).collect(Collectors.toList());
        DeviceMaintainInDTO deviceMaintainInDTO = new DeviceMaintainInDTO();
        deviceMaintainInDTO.setDeviceId(deviceId);
        if (checkList.isEmpty()) {
            deviceMaintainInDTO.setMaintainStatus(Integer.valueOf(MaintainPlanStatusEnum.T1.getType()));
        } else {
            deviceMaintainInDTO.setMaintainStatus(Integer.valueOf(MaintainPlanStatusEnum.of(checkList.get(0).getPlanStatus()).getType()));
        }
        if (checkTimeList.isEmpty()) {
            deviceMaintainInDTO.setNextMaintainTime(null);
        } else {
            deviceMaintainInDTO.setNextMaintainTime(checkTimeList.get(0).getMaintainDate());
        }
        updateDeviceMaintain(deviceMaintainInDTO);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void updateDeviceMaintain(DeviceMaintainInDTO deviceMaintainInDTO) {
        Machine one = (Machine) getById(deviceMaintainInDTO.getDeviceId());
        if (one == null) {
            throw new ServiceException(MessageUtils.message("machine.not.exists", new Object[0]));
        }
        one.setLastMaintainTime(deviceMaintainInDTO.getLastMaintainTime());
        one.setMaintenanceStatus(deviceMaintainInDTO.getMaintainStatus());
        one.setNextMaintainTime(deviceMaintainInDTO.getNextMaintainTime());
        updateById(one);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public void updateDeviceMaintainStatus(Long deviceId, Long planId, LocalDateTime lastMaintainTime) {
        List<MaintainPlan> checkList = checkExistPlanStatus(deviceId, null);
        List<MaintainPlan> checkTimeList = (List) checkList.stream().sorted(Comparator.comparing((v0) -> {
            return v0.getMaintainDate();
        })).collect(Collectors.toList());
        DeviceMaintainInDTO deviceMaintainInDTO = new DeviceMaintainInDTO();
        deviceMaintainInDTO.setDeviceId(deviceId);
        if (checkList.isEmpty()) {
            deviceMaintainInDTO.setMaintainStatus(Integer.valueOf(MaintainStatusEnum.T1.getType()));
        } else {
            deviceMaintainInDTO.setMaintainStatus(checkList.get(0).getPlanStatus());
        }
        if (checkTimeList.isEmpty()) {
            deviceMaintainInDTO.setNextMaintainTime(null);
        } else {
            deviceMaintainInDTO.setNextMaintainTime(checkTimeList.get(0).getMaintainDate());
        }
        if (lastMaintainTime != null) {
            deviceMaintainInDTO.setLastMaintainTime(lastMaintainTime);
        }
        updateDeviceMaintain(deviceMaintainInDTO);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<DeviceSimpleDTO> deviceListByDeviceTypeId(Long deviceTypeId) {
        return ((MachineMapper) this.baseMapper).deviceListByDeviceTypeId(deviceTypeId);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<DeviceSimpleDTO> deviceListByDeviceIdList(List<Long> deviceIdList) {
        return ((MachineMapper) this.baseMapper).deviceListByDeviceIdList(deviceIdList);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public BladeFile exportMachine(MachineSelectVO vo) {
        List<MachineDetailVO> vos = ((MachineMapper) this.baseMapper).listMachineDetail(vo.getMachineName(), vo.getMachineCode(), vo.getMachineTypeId(), vo.getGroupId(), CommonGroupConstant.ALL_MACHINE_ID, vo.getStatus());
        List<MachineDetailVO> list = (List) vos.stream().peek(c -> {
            c.setMachineLifeStatusName(DictCache.getValue(DictEnum.MACHINE_LIFE_STATUS.getName(), c.getMachineLifeStatus()));
            c.setManagementClassName(DictCache.getValue(DictEnum.MACHINE_MANAGEMENT_CLASS, c.getManagementClass()));
            c.setMachineUseStatusName(DictCache.getValue(DictEnum.MACHINE_USE_STATUS.getName(), c.getMachineUseStatus()));
        }).collect(Collectors.toList());
        List<MachineExcel> excels = MachineConvert.INSTANCE.convertVO(list);
        String fileName = String.format("%s-%s.xlsx", "机器管理", DateUtil.time());
        MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, "机器管理数据表", excels, MachineExcel.class);
        return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    @Transactional(rollbackFor = {Exception.class})
    public BladeFile importMachine(MultipartFile file) {
        List<MachineImport> rawData = ExcelUtil.read(file, 0, 2, MachineImport.class);
        ThrowFun.isTrue(Func.isEmpty(rawData)).throwMessage(MessageUtils.message("excel.import.data.can.not.be.null", new Object[0]));
        ThrowFun.isTrue(rawData.size() > 200).throwMessage(MessageUtils.message("excel.import.size.failed", new Object[0]));
        Map<String, Integer> machineCodeMap = new HashMap<>(rawData.size());
        Set<String> machineCodes = new HashSet<>(rawData.size());
        Map<String, Integer> pinMap = new HashMap<>(rawData.size());
        Set<String> pinCodes = new HashSet<>(rawData.size());
        Map<String, Integer> shortMap = new HashMap<>(rawData.size());
        Set<String> shortCodes = new HashSet<>(rawData.size());
        buildMap(rawData, machineCodeMap, machineCodes, pinMap, pinCodes, shortMap, shortCodes);
        List<MachineImport> failExcel = Lists.newArrayList();
        List<DeviceType> deviceTypeList = Lists.newArrayList();
        List<Workstation> workstationList = this.workstationService.list(Wrappers.<Workstation>lambdaQuery().eq(Workstation::getStatus, CommonConstant.ENABLE));
        Map<String, Workstation> workstationMap = workstationList.stream().collect(Collectors.toMap((v0) -> {
            return v0.getCode();
        }, v -> {
            return v;
        }));
        HashMap<String, Long> commonGroupNameMap = this.commonGroupService.getCommonGroupNameMap(CommonGroupTypeEnum.MACHINE.getName(), CommonGroupConstant.DEFAULT_CATEGORY);
        HashMap<String, Long> organizationNameMap = this.commonGroupService.getCommonGroupNameMap(CommonGroupTypeEnum.ORGANIZATION.getName(), CommonGroupConstant.DEFAULT_CATEGORY);
        HashMap<String, Long> commonGroupNameStatusMap = this.commonGroupService.getCommonGroupNameMap(CommonGroupTypeEnum.ORGANIZATION.getName(), CommonGroupConstant.DEFAULT_CATEGORY, CommonConstant.ENABLE);
        Map<String, Long> commonGroupNameDisableMap = this.commonGroupService.getCommonGroupNameMap(CommonGroupTypeEnum.ORGANIZATION.getName(), CommonGroupConstant.DEFAULT_CATEGORY, CommonConstant.DEACTIVATE);
        EmployeeNameDTO employeeNameMap = this.employeeService.getEmployeeNameMap();
        for (MachineImport data : rawData) {
            boolean validImport = validImport(data, machineCodeMap, machineCodes, pinCodes, pinMap, shortCodes, shortMap, failExcel, deviceTypeList, commonGroupNameMap, organizationNameMap, workstationMap, commonGroupNameStatusMap, commonGroupNameDisableMap, employeeNameMap);
            if (validImport) {
                break;
            }
        }
        if (Func.isNotEmpty(failExcel)) {
            return exportFailFile(failExcel);
        }
        Map<String, Long> typeMap = deviceTypeList.stream().collect(Collectors.toMap((v0) -> {
            return v0.getCode();
        }, (v0) -> {
            return v0.getId();
        },(v1, v2) -> {
            return v1;
        }));
        saveExportMachine(rawData, typeMap, commonGroupNameMap, organizationNameMap, employeeNameMap);
        return null;
    }
 
    public void saveExportMachine(List<MachineImport> rawData, Map<String, Long> typeMap, HashMap<String, Long> commonGroupNameMap, HashMap<String, Long> organizationNameMap, EmployeeNameDTO employeeNameMap) {
        List<Machine> collect = rawData.stream().map(c -> {
            Machine machine = MachineConvert.INSTANCE.convert(c);
            String machineTypeName = c.getMachineTypeName();
            String machineTypeCode = c.getMachineTypeCode();
            if (Func.isNotBlank(machineTypeName) && Func.isNotBlank(machineTypeCode)) {
                Long machineTypeId = (Long) typeMap.get(machineTypeCode);
                machine.setMachineTypeId(machineTypeId);
            }
            String groupName = c.getGroupName();
            if (Func.isNotBlank(groupName) && !CommonGroupConstant.ALL_NAME.equals(groupName)) {
                Long groupId = (Long) commonGroupNameMap.get(groupName);
                machine.setGroupId(groupId);
            } else if (CommonGroupConstant.ALL_NAME.equals(groupName)) {
                machine.setGroupId(CommonGroupConstant.DEFAULT_MACHINE_ID);
            } else {
                machine.setGroupId(CommonGroupConstant.DEFAULT_MACHINE_ID);
            }
            String organizationName = c.getOrganizationName();
            if (Func.isNotBlank(organizationName)) {
                Long organizationId = (Long) organizationNameMap.get(organizationName);
                machine.setOrganizationId(organizationId);
            }
            String lifeStatusName = c.getMachineLifeStatusName();
            if (Func.isNotBlank(lifeStatusName)) {
                String lifeStatus = DictCache.getKey(DictEnum.MACHINE_LIFE_STATUS, lifeStatusName);
                machine.setMachineLifeStatus(Integer.valueOf(Integer.parseInt(lifeStatus)));
            }
            String managementClassName = c.getManagementClassName();
            if (Func.isNotBlank(managementClassName)) {
                String managementClass = DictCache.getKey(DictEnum.MACHINE_MANAGEMENT_CLASS, managementClassName);
                machine.setManagementClass(Integer.valueOf(Integer.parseInt(managementClass)));
            }
            String useStatusName = c.getMachineUseStatusName();
            if (Func.isNotBlank(useStatusName)) {
                String useStatus = DictCache.getKey(DictEnum.MACHINE_USE_STATUS, useStatusName);
                machine.setMachineUseStatus(Integer.valueOf(Integer.parseInt(useStatus)));
            }
            String employeeName = c.getEmployeeName();
            if (Func.isNotEmpty(employeeName)) {
                machine.setEmployeeId(employeeNameMap.getEmployeeNameMap().get(employeeName));
            }
            machine.setMaintenanceStatus(Integer.valueOf(MaintainStatusEnum.T1.getType()));
            machine.setRepairStatus(Integer.valueOf(RepairRecordEnum.R1.getType()));
            machine.setExtendId(UUID.randomUUID().toString());
            return machine;
        }).collect(Collectors.toList());
        saveBatch(collect);
        saveCommonGroupItem(collect);
        saveWorkstationOfMachine(collect);
        CacheUtil.clear(ExtCacheConstant.POSTING_MACHINE, Boolean.FALSE);
    }
 
    private BladeFile exportFailFile(List<MachineImport> failExcel) {
        MultipartFile multipartFile = ExcelUtil.exportFillTemplateToMultipartFile(ExcelConstant.DIRECTORY + "machineFailTemplate" + ExcelConstant.SUFFIX, MessageUtils.message("excel.import.failed.report.name", new Object[0]) + ExcelConstant.SUFFIX, "机器表", failExcel, (Object) null, (AbstractMergeStrategy) null);
        return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
    }
 
    private void buildMap(List<MachineImport> rawData, Map<String, Integer> machineCodeMap, Set<String> machineCodes, Map<String, Integer> pinMap, Set<String> pinCodes, Map<String, Integer> shortMap, Set<String> shortCodes) {
        QueryWrapper<Machine> machineCodeQuery = Wrappers.<Machine>query().select(new String[]{"machine_code"});
        QueryWrapper<Machine> pinCodeQuery = Wrappers.<Machine>query().select(new String[]{"pin_code"});
        QueryWrapper<Machine> shortCodeQuery = Wrappers.<Machine>query().select(new String[]{"short_code"});
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        for (MachineImport data : rawData) {
            String machineCode = data.getMachineCode();
            if (Func.isNotBlank(machineCode)) {
                if (machineCodeMap.containsKey(machineCode)) {
                    machineCodeMap.put(machineCode, Integer.valueOf(machineCodeMap.get(machineCode).intValue() + 1));
                } else {
                    machineCodeMap.put(machineCode, 1);
                }
                if (flag1) {
                    machineCodeQuery.or();
                }
                flag1 = true;
                machineCodeQuery.nested(i -> {
                     i.eq("machine_code", machineCode);
                    //QueryWrapper queryWrapper = (QueryWrapper) i.eq("machine_code", machineCode);
                });
                machineCodeQuery.apply("status =1", new Object[0]);
            }
            String pinCode = data.getPinCode();
            if (Func.isNotBlank(pinCode)) {
                if (pinMap.containsKey(pinCode)) {
                    pinMap.put(pinCode, Integer.valueOf(pinMap.get(pinCode).intValue() + 1));
                } else {
                    pinMap.put(pinCode, 1);
                }
                if (flag2) {
                    pinCodeQuery.or();
                }
                flag2 = true;
                pinCodeQuery.nested(i2 -> {
                    i2.eq("pin_code", pinCode);
                });
                pinCodeQuery.apply("status =1", new Object[0]);
            }
            String shortCode = data.getShortCode();
            if (Func.isNotBlank(shortCode)) {
                if (shortMap.containsKey(shortCode)) {
                    shortMap.put(shortCode, Integer.valueOf(shortMap.get(shortCode).intValue() + 1));
                } else {
                    shortMap.put(shortCode, 1);
                }
                if (flag3) {
                    shortCodeQuery.or();
                }
                flag3 = true;
                shortCodeQuery.nested(i3 -> {
                    i3.eq("short_code", shortCode);
                });
                shortCodeQuery.apply("status =1", new Object[0]);
            }
        }
        String sqlSegment1 = machineCodeQuery.getSqlSegment();
        if (Func.isNotBlank(sqlSegment1)) {
            List<String> machineCodeList = listObjs(machineCodeQuery, String::valueOf);
            if (Func.isNotEmpty(machineCodeList)) {
                machineCodes.addAll(machineCodeList);
            }
        }
        String sqlSegment2 = pinCodeQuery.getSqlSegment();
        if (Func.isNotBlank(sqlSegment2)) {
            List<String> pinCodeList = listObjs(pinCodeQuery, String::valueOf);
            if (Func.isNotEmpty(pinCodeList)) {
                pinCodes.addAll(pinCodeList);
            }
        }
        String sqlSegment3 = shortCodeQuery.getSqlSegment();
        if (Func.isNotBlank(sqlSegment3)) {
            List<String> shortCodeList = listObjs(shortCodeQuery, String::valueOf);
            if (Func.isNotEmpty(shortCodeList)) {
                shortCodes.addAll(shortCodeList);
            }
        }
    }
 
    public void saveWorkstationOfMachine(List<Machine> collect) {
        collect.forEach(machine -> {
            if ("是".equals(machine.getLinkWay())) {
                WorkstationSubmitVO workstationSaveVO = new WorkstationSubmitVO();
                workstationSaveVO.setCode(machine.getMachineCode());
                workstationSaveVO.setName(machine.getMachineName());
                workstationSaveVO.setMachineCode(machine.getMachineCode());
                workstationSaveVO.setMachineName(machine.getMachineName());
                workstationSaveVO.setGroupId(CommonGroupConstant.DEFAULT_WORKSTATION_ID);
                workstationSaveVO.setLinkWay(0);
                workstationSaveVO.setType(0);
                workstationSaveVO.setMachineId(machine.getId());
                this.workstationService.submit(workstationSaveVO);
            }
        });
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    @Transactional(rollbackFor = {Exception.class})
    public Boolean delete(IdsVO vo, Integer type) {
        if (StatusType.REMOVE.getType().equals(type)) {
            WorkstationOfMachineCache.clearWorkStationMachineCache(vo.getIds());
            Integer nums = ((MachineMapper) this.baseMapper).typeAndonRecord(Func.toLongList(vo.getIds()));
            if (nums.intValue() > 0) {
                throw new ServiceException(MessageUtils.message("machine.andon.not.remove", new Object[0]));
            }
            removeByIds(Func.toLongList(vo.getIds()));
            this.workstationOfMachineService.remove(Wrappers.<WorkstationOfMachine>lambdaQuery().in(WorkstationOfMachine::getMachineId, vo.getIds()));
            this.itemService.remove( Wrappers.<CommonGroupOfItem>lambdaQuery().in(CommonGroupOfItem::getItemId, vo.getIds()));
            CacheUtil.clear(ExtCacheConstant.POSTING_MACHINE, Boolean.FALSE);
            CacheUtil.clear(ExtCacheConstant.WORKSTATION_MACHINE, Boolean.FALSE);
        }
        return Boolean.valueOf(changeStatus(Func.toLongList(vo.getIds()), CommonConstant.DEACTIVATE));
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<HmiDeviceVO> getHmiWorkstation(String machineId) {
        List<HmiDeviceVO> hmiDeviceVOList = new ArrayList<>();
        Machine machine = (Machine) getById(machineId);
        if (machine == null) {
            throw new ServiceException("机器不存在");
        }
        List<WorkstationOfMachine> list = this.workstationOfMachineService.list(Wrappers.<WorkstationOfMachine>lambdaQuery().eq(WorkstationOfMachine::getMachineId, machineId));
        
        if (Func.isNotEmpty(list)) {
            List<Long> workstationIds =  list.stream().map((v0) -> {
                return v0.getWorkstationId();
            }).collect(Collectors.toList());
            if (Func.isNotEmpty(workstationIds)) {
                List<Workstation> workstations = this.workstationService.listByIds(workstationIds);
                for (Workstation workstation : workstations) {
                    HmiDeviceVO hmiDeviceVO = new HmiDeviceVO();
                    hmiDeviceVO.setWorkstationId(String.valueOf(workstation.getId()));
                    hmiDeviceVO.setWorkstationName(workstation.getName());
                    Integer supportCncRw = workstation.getSupportCncRw();
                    if (DncEnums.SupportCncRw.Support.getCode().equals(supportCncRw)) {
                        hmiDeviceVO.setFileLocations(Arrays.asList(1, 2));
                    } else {
                        hmiDeviceVO.setFileLocations(Collections.singletonList(2));
                    }
                    hmiDeviceVOList.add(hmiDeviceVO);
                }
            }
        }
        return hmiDeviceVOList;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<MachineExtDTO> getMachineByWorkStation(List<Long> workstationIds) {
        return ((MachineMapper) this.baseMapper).getMachineByWorkStation(workstationIds);
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public String queryDateTypeState(Long workstationId) {
        return ((MachineMapper) this.baseMapper).queryDateTypeState(WcsDataTypeEnums.WcsDataType.STATE.getCode(), workstationId);
    }
 
    private boolean validImport(MachineImport data, Map<String, Integer> machineCodeMap, Set<String> machineCodes, Set<String> pinCodes, Map<String, Integer> pinMap, Set<String> shortCodes, Map<String, Integer> shortMap, List<MachineImport> failExcel, List<DeviceType> deviceTypeList, HashMap<String, Long> commonGroupNameMap, HashMap<String, Long> organizationNameMap, Map<String, Workstation> workstationMap, HashMap<String, Long> commonGroupNameStatusMap, Map<String, Long> commonGroupNameDisableMap, EmployeeNameDTO employeeNameMap) {
        boolean validMachineCode = validMachineCode(data, machineCodeMap, machineCodes, failExcel);
        if (validMachineCode) {
            return true;
        }
        boolean validMachineName = validMachineName(data, failExcel);
        if (validMachineName) {
            return true;
        }
        boolean validLinkWay = validLinkWay(data, workstationMap, failExcel);
        if (validLinkWay) {
            return true;
        }
        boolean validWorkstationBind = validWorkstationBind(data, workstationMap, failExcel);
        if (validWorkstationBind) {
            return true;
        }
        String pinCode = data.getPinCode();
        if (Func.isNotBlank(pinCode)) {
            boolean valid = validPinCodeImport(pinCode, data, failExcel, pinCodes, pinMap);
            if (valid) {
                return true;
            }
        }
        String shortCode = data.getShortCode();
        if (Func.isNotBlank(shortCode)) {
            boolean valid2 = validShortCodeImport(shortCode, data, failExcel, shortCodes, shortMap);
            if (valid2) {
                return true;
            }
        }
        String typeCode = data.getMachineTypeCode();
        String typeName = data.getMachineTypeName();
        if (Func.isNotBlank(typeName) && Func.isBlank(typeCode)) {
            data.setFailReason(MessageUtils.message("excel.import.machine.type.code.error", new Object[0]));
            failExcel.add(data);
            return true;
        }
        if (Func.isNotBlank(typeName) && Func.isNotBlank(typeCode)) {
            DeviceType type = this.typeService.getOne(Wrappers.<DeviceType>lambdaQuery().eq(DeviceType::getName, typeName)
                    .eq(DeviceType::getCode, typeCode));
            if (Func.isEmpty(type)) {
                data.setFailReason(MessageUtils.message("cps.tpm.machine.type.not.exists", new Object[0]));
                failExcel.add(data);
                return true;
            }
            deviceTypeList.add(type);
        }
        String groupName = data.getGroupName();
        if (Func.isNotBlank(groupName) && !commonGroupNameMap.containsKey(groupName)) {
            data.setFailReason(MessageUtils.message("cps.tpm.machine.group.not.exists", new Object[0]));
            failExcel.add(data);
            return true;
        }
        String voltage = data.getVoltage();
        if (Func.isNotBlank(voltage)) {
            boolean valid3 = checkNumberFormat(voltage, data, failExcel);
            if (valid3) {
                return true;
            }
        }
        String dimensions = data.getDimensions();
        if (Func.isNotBlank(dimensions)) {
            boolean valid4 = checkNumberFormat(dimensions, data, failExcel);
            if (valid4) {
                return true;
            }
        }
        String netWeight = data.getNetWeight();
        if (Func.isNotBlank(netWeight)) {
            boolean valid5 = checkNumberFormat(netWeight, data, failExcel);
            if (valid5) {
                return true;
            }
        }
        String power = data.getPower();
        if (Func.isNotBlank(power)) {
            boolean valid6 = checkNumberFormat(power, data, failExcel);
            if (valid6) {
                return true;
            }
        }
        String invoiceValue = data.getInvoiceValue();
        if (Func.isNotBlank(invoiceValue)) {
            boolean valid7 = checkNumberFormat(invoiceValue, data, failExcel);
            if (valid7) {
                return true;
            }
        }
        String ovfa = data.getOvfa();
        if (Func.isNotBlank(ovfa)) {
            boolean valid8 = checkNumberFormat(ovfa, data, failExcel);
            if (valid8) {
                return true;
            }
        }
        String netAssetValue = data.getNetAssetValue();
        if (Func.isNotBlank(netAssetValue)) {
            boolean valid9 = checkNumberFormat(netAssetValue, data, failExcel);
            if (valid9) {
                return true;
            }
        }
        String productionTime = data.getProductionTime();
        if (Func.isNotBlank(productionTime)) {
            boolean valid10 = checkTimeFormat(productionTime, data, failExcel);
            if (valid10) {
                return true;
            }
        }
        String deliveryTime = data.getDeliveryTime();
        if (Func.isNotBlank(deliveryTime)) {
            boolean valid11 = checkTimeFormat(deliveryTime, data, failExcel);
            if (valid11) {
                return true;
            }
        }
        String turnToFixedAssetsTime = data.getTurnToFixedAssetsTime();
        if (Func.isNotBlank(turnToFixedAssetsTime)) {
            boolean valid12 = checkTimeFormat(turnToFixedAssetsTime, data, failExcel);
            if (valid12) {
                return true;
            }
        }
        String lifeStatusName = data.getMachineLifeStatusName();
        if (Func.isNotBlank(lifeStatusName)) {
            String lifeStatus = DictCache.getKey(DictEnum.MACHINE_LIFE_STATUS, lifeStatusName);
            if (Func.isBlank(lifeStatus)) {
                data.setFailReason(MessageUtils.message("cps.tpm.machine.life.status.exists", new Object[0]));
                failExcel.add(data);
                return true;
            }
        }
        String managementClassName = data.getManagementClassName();
        if (Func.isNotBlank(managementClassName)) {
            String managementClass = DictCache.getKey(DictEnum.MACHINE_MANAGEMENT_CLASS, managementClassName);
            if (Func.isBlank(managementClass)) {
                data.setFailReason(MessageUtils.message("cps.tpm.machine.management.class.exists", new Object[0]));
                failExcel.add(data);
                return true;
            }
        }
        String organizationName = data.getOrganizationName();
        if (Func.isNotBlank(organizationName)) {
            if (!commonGroupNameStatusMap.containsKey(organizationName)) {
                data.setFailReason(MessageUtils.message("cps.tpm.machine.organization.not.exists", new Object[0]));
                failExcel.add(data);
                return true;
            } else if (commonGroupNameDisableMap.containsKey(data.getOrganizationName())) {
                data.setFailReason(MessageUtils.message("cps.organization.already.exist.stop.status", new Object[0]));
                failExcel.add(data);
                return true;
            }
        }
        String useStatusName = data.getMachineUseStatusName();
        if (Func.isNotBlank(useStatusName)) {
            String useStatus = DictCache.getKey(DictEnum.MACHINE_USE_STATUS, useStatusName);
            if (Func.isBlank(useStatus)) {
                data.setFailReason(MessageUtils.message("cps.tpm.use.status.exists", new Object[0]));
                failExcel.add(data);
                return true;
            }
        }
        String employeeName = data.getEmployeeName();
        if (Func.isNotEmpty(employeeName)) {
            if (!employeeNameMap.getEmployeeNameMap().containsKey(employeeName)) {
                data.setFailReason(MessageUtils.message("cps.employee.name.is.not.exist", new Object[0]));
                failExcel.add(data);
                return true;
            } else if (employeeNameMap.getRepeatEmployeeNameMap().containsKey(employeeName)) {
                data.setFailReason(StrUtil.format(MessageUtils.message("cps.employee.name.already.exists", new Object[0]), new Object[]{employeeName}));
                failExcel.add(data);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public MachineDetailVO detailByCode(String machineCode) {
        MachineDetailVO machineDetailVO = ((MachineMapper) this.baseMapper).selectMachineDetailByCode(machineCode);
        if (Func.isNotEmpty(machineDetailVO)) {
            machineDetailVO.setMachineLifeStatusName(DictCache.getValue(DictEnum.MACHINE_LIFE_STATUS.getName(), machineDetailVO.getMachineLifeStatus()));
            machineDetailVO.setManagementClassName(DictCache.getValue(DictEnum.MACHINE_MANAGEMENT_CLASS, machineDetailVO.getManagementClass()));
            machineDetailVO.setMachineUseStatusName(DictCache.getValue(DictEnum.MACHINE_USE_STATUS.getName(), machineDetailVO.getMachineUseStatus()));
            List<WorkstationOfMachine> workstationOfMachineList = this.workstationOfMachineService.list(Wrappers.<WorkstationOfMachine>lambdaQuery()
                    .eq(WorkstationOfMachine::getMachineId, machineDetailVO.getId()));
            if (Func.isNotEmpty(workstationOfMachineList)) {
                List<Workstation> workstationList = this.workstationService.listByIds(workstationOfMachineList.stream().map(WorkstationOfMachine::getWorkstationId).collect(Collectors.toList()));
                Optional.ofNullable(workstationList).ifPresent(workstations -> {
                    String code = ((Workstation) ListUtils.getFirst(workstations)).getCode();
                    String name = ((Workstation) ListUtils.getFirst(workstations)).getName();
                    Long workstationId = ((Workstation) ListUtils.getFirst(workstations)).getId();
                    machineDetailVO.setWorkstationCode(code);
                    machineDetailVO.setWorkstationName(name);
                    machineDetailVO.setWorkstationId(workstationId);
                });
            }
        }
        return machineDetailVO;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public IPage<MachineListVO> pageMachineByParam(MachineSelectVO machineSelectVO, Query query) {
        IPage<MachineListVO> page = ((MachineMapper) this.baseMapper).pageMachineByParam(Condition.getPage(query), machineSelectVO);
        List<MachineListVO> records = page.getRecords();
        List<MachineListVO> collect = (List) records.stream().peek(c -> {
            c.setMachineUseStatusName(DictCache.getValue(DictEnum.MACHINE_USE_STATUS.getName(), c.getMachineUseStatus()));
        }).collect(Collectors.toList());
        page.setRecords(collect);
        return page;
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<MachineVO> listNoBand() {
        return ((MachineMapper) this.baseMapper).listNoBand();
    }
 
    @Override // org.springblade.modules.cps.service.IMachineService
    public List<MachineVO> listHaveBand() {
        return ((MachineMapper) this.baseMapper).listHaveBand();
    }
 
    private boolean validMachineCode(MachineImport data, Map<String, Integer> machineCodeMap, Set<String> machineCodes, List<MachineImport> failExcel) {
        String machineCode = data.getMachineCode();
        if (Func.isBlank(machineCode)) {
            data.setFailReason(MessageUtils.message("excel.import.machine.code.exists", new Object[0]));
            failExcel.add(data);
            return true;
        } else if (machineCodes.contains(machineCode)) {
            data.setFailReason(MessageUtils.message("excel.import.machine.code.code.same", new Object[0]));
            failExcel.add(data);
            return true;
        } else {
            Integer nums = machineCodeMap.getOrDefault(machineCode, 1);
            if (nums.intValue() > 1) {
                data.setFailReason(MessageUtils.message("excel.import.machine.code.code.same", new Object[0]));
                failExcel.add(data);
                return true;
            } else if (checkStopImportMachine(machineCode)) {
                data.setFailReason(MessageUtils.message("machine.code.already.exist.stop.status", new Object[0]));
                failExcel.add(data);
                return true;
            } else {
                return false;
            }
        }
    }
 
    private boolean validMachineName(MachineImport data, List<MachineImport> failExcel) {
        String machineName = data.getMachineName();
        if (Func.isBlank(machineName)) {
            data.setFailReason(MessageUtils.message("excel.import.machine.name.exists", new Object[0]));
            failExcel.add(data);
            return true;
        }
        return false;
    }
 
    private boolean validLinkWay(MachineImport data, Map<String, Workstation> workstationMap, List<MachineImport> failExcel) {
        String linkWay = data.getLinkWay();
        if (Func.isNotEmpty(linkWay)) {
            if (!"是".equals(linkWay) && !"否".equals(linkWay)) {
                data.setFailReason(MessageUtils.message("excel.import.machine.linkWay.not.correct", new Object[0]));
                failExcel.add(data);
                return true;
            } else if ("是".equals(linkWay)) {
                String machineCode = data.getMachineCode();
                if (workstationMap.containsKey(machineCode)) {
                    data.setFailReason(MessageUtils.message("cps.workstation.code.already.exists", new Object[0]));
                    failExcel.add(data);
                    return true;
                }
                return false;
            } else {
                return false;
            }
        }
        return false;
    }
 
    private boolean validWorkstationBind(MachineImport data, Map<String, Workstation> workstationMap, List<MachineImport> failExcel) {
        Workstation workstation = workstationMap.get(data.getMachineCode());
        if (Func.isNotEmpty(workstation)) {
            long count = this.workstationOfMachineService.count(Wrappers.<WorkstationOfMachine>lambdaQuery()
                      .eq(WorkstationOfMachine::getWorkstationId, workstation.getId()));
            /*
            long count = this.workstationOfMachineService.count((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getWorkstationId();
            }, workstation.getId()));*/
            if (count > 0) {
                data.setFailReason(MessageUtils.message("excel.import.machine.workstation.already.bound", new Object[0]));
                failExcel.add(data);
                return true;
            }
            return false;
        }
        return false;
    }
 
    private boolean validPinCodeImport(String pinCode, MachineImport data, List<MachineImport> failExcel, Set<String> pinCodes, Map<String, Integer> pinMap) {
        int len = pinCode.length();
        if (len < 4 || len > 6) {
            data.setFailReason(String.format(MessageUtils.message("cps.tpm.machine.pin.code.length", new Object[0]), 4, 6));
            failExcel.add(data);
            return true;
        }
        boolean matches = CHINESE.matcher(pinCode).matches();
        if (!matches) {
            data.setFailReason(MessageUtils.message("excel.import.machine.ping.code.have.chinese", new Object[0]));
            failExcel.add(data);
            return true;
        }
        return false;
    }
 
    private boolean validShortCodeImport(String shortCode, MachineImport data, List<MachineImport> failExcel, Set<String> shortCodes, Map<String, Integer> shortMap) {
        int len = shortCode.length();
        if (len < 4 || len > 8) {
            data.setFailReason(String.format(MessageUtils.message("cps.tpm.machine.short.code.length", new Object[0]), 4, 8));
            failExcel.add(data);
            return true;
        } else if (Func.isNotEmpty(shortCodes) && shortCodes.contains(shortCode)) {
            data.setFailReason(MessageUtils.message("cps.tpm.machine.short.code.exists", new Object[0]));
            failExcel.add(data);
            return true;
        } else if (Func.isNotEmpty(shortMap) && shortMap.getOrDefault(shortCode, 1).intValue() > 1) {
            data.setFailReason(MessageUtils.message("cps.tpm.machine.short.code.excel.exists", new Object[0]));
            failExcel.add(data);
            return true;
        } else {
            boolean matches = CHINESE.matcher(shortCode).matches();
            if (!matches) {
                data.setFailReason(MessageUtils.message("excel.import.machine.short.code.have.chinese", new Object[0]));
                failExcel.add(data);
                return true;
            }
            return false;
        }
    }
 
    private boolean checkTimeFormat(String time, MachineImport data, List<MachineImport> failExcel) {
        if (Func.isNotBlank(time)) {
            try {
                DateUtil.parse(time, DateConstant.PATTERN_DATE);
                return false;
            } catch (Exception e) {
                data.setFailReason(MessageUtils.message("excel.import.machine.time.error", new Object[0]));
                failExcel.add(data);
                return true;
            }
        }
        return false;
    }
 
    private boolean checkNumberFormat(String number, MachineImport data, List<MachineImport> failExcel) {
        if (Func.isNotBlank(number)) {
            boolean matches = NUMBER.matcher(number).matches();
            if (!matches) {
                data.setFailReason(MessageUtils.message("excel.import.machine.number.error", new Object[0]));
                failExcel.add(data);
                return true;
            }
            return false;
        }
        return false;
    }
 
    private boolean checkStopImportMachine(String machineCode) {
        Machine machine = getOne(Wrappers.<Machine>lambdaQuery().eq(Machine::getMachineCode, machineCode).eq(Machine::getStatus, CommonConstant.DEACTIVATE));
       
        if (machine != null) {
            return Boolean.TRUE.booleanValue();
        }
        return Boolean.FALSE.booleanValue();
    }
 
    private void checkStopMachine(String machineCode) {
        Machine machine = getOne(Wrappers.<Machine>lambdaQuery().eq(Machine::getMachineCode, machineCode).eq(Machine::getStatus, CommonConstant.DEACTIVATE));
        /*
        Machine machine = (Machine) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getMachineCode();
        }, machineCode)).eq((v0) -> {
            return v0.getStatus();
        }, CommonConstant.DEACTIVATE));*/
        if (machine != null) {
            throw new ServiceException(MessageUtils.message("machine.code.already.exist.stop.status", new Object[0]));
        }
    }
 
    private void checkStopMachineByCode(String workstationCode) {
        Machine machine = getOne(Wrappers.<Machine>lambdaQuery().eq(Machine::getMachineCode, workstationCode).eq(Machine::getStatus, CommonConstant.DEACTIVATE));
        if (machine != null) {
            throw new ServiceException(MessageUtils.message("machine.machine.workstation.already.exist.stop.status", new Object[0]));
        }
    }
 
    private void checkMachine(Long id, String machineCode) {
        Long count = Long.valueOf(count(Wrappers.<Machine>lambdaQuery().ne(Func.isNotEmpty(id), Machine::getId, id)
                .eq(Machine::getMachineCode, machineCode)
                .eq(Machine::getStatus, CommonConstant.ENABLE)));
        /*
        Long count = Long.valueOf(count((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().ne(Func.isNotEmpty(id), (v0) -> {
            return v0.getId();
        }, id).eq((v0) -> {
            return v0.getMachineCode();
        }, machineCode)).eq((v0) -> {
            return v0.getStatus();
        }, CommonConstant.ENABLE)));*/
        if (count.longValue() > 0) {
            throw new ServiceException(MessageUtils.message("machine.code.already.exists", new Object[0]));
        }
    }
 
    private void checkPinCode(String pinCode, Long id) {
        int len = pinCode.length();
        if (len < 4 || len > 6) {
            throw new ServiceException(String.format(MessageUtils.message("cps.tpm.machine.pin.code.length", new Object[0]), 4, 6));
        }
    }
 
    private void saveCommonGroupItem(List<Machine> machines) {
        if (Func.isNotEmpty(machines)) {
            List<CommonGroupOfItem> collect = (List) machines.stream().map(c -> {
                return CommonGroupOfItem.builder().groupId(c.getGroupId()).itemId(c.getId()).extendId(c.getExtendId()).groupType(CommonGroupTypeEnum.MACHINE.getName()).groupCategory(CommonGroupConstant.DEFAULT_CATEGORY).build();
            }).collect(Collectors.toList());
            this.itemService.saveBatch(collect);
        }
    }
 
    private List<MaintainPlan> checkExistPlanStatus(Long deviceId, Long id) {
        List<MaintainPlan> maintainPlanList = this.maintainPlanService.list(Wrappers.<MaintainPlan>query().lambda()
                .eq(Func.isNotEmpty(AuthUtil.getTenantId()), MaintainPlan::getTenantId, AuthUtil.getTenantId())
                .eq(MaintainPlan::getDeviceId, deviceId)
                .notIn(
                  Func.isNotEmpty(id), MaintainPlan::getId, new Object[] { id })
                .notIn(MaintainPlan::getPlanStatus, new Object[] { Integer.valueOf(MaintainPlanStatusEnum.T5.getType()) })
                .orderByDesc(MaintainPlan::getPlanStatus));
        /*
        List<MaintainPlan> maintainPlanList = this.maintainPlanService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq(Func.isNotEmpty(AuthUtil.getTenantId()), (v0) -> {
            return v0.getTenantId();
        }, AuthUtil.getTenantId()).eq((v0) -> {
            return v0.getDeviceId();
        }, deviceId)).notIn(Func.isNotEmpty(id), (v0) -> {
            return v0.getId();
        }, new Object[]{id}).notIn((v0) -> {
            return v0.getPlanStatus();
        }, new Object[]{Integer.valueOf(MaintainPlanStatusEnum.T5.getType())})).orderByDesc((v0) -> {
            return v0.getPlanStatus();
        }));*/
        return maintainPlanList;
    }
}