自动化立体仓库 - WMS系统
pang.jiabao
昨天 89142d4419ba96a94384b9aca370e70318d4194a
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.mapper.ManLocDetlMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.SaasUtils;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.DetlDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.Synchro;
import com.zy.system.entity.User;
import com.zy.system.service.SaasLogService;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
 
/**
 * 移动端服务核心类
 * Created by vincent on 2020/6/28
 */
@Slf4j
@Service
public class MobileServiceImpl implements MobileService {
 
    @Autowired
    private MatService matService;
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private PackService packService;
    @Autowired
    private OpenService openService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ManPakOutService manPakOutService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private ManLocDetlService manLocDetlService;
    @Autowired
    private ManLocDetlMapper manLocDetlMapper;
    @Autowired
    private WaitPakinLogService waitPakinLogService;
 
    @Autowired
    private  LocCheckService locCheckService;
 
    @Autowired
    private OrderCheckService orderCheckService;
 
    @Autowired
    private OriginRuleService originRuleService;
 
    @Autowired
    private LocInPrintMatService locInPrintMatService;
 
    @Autowired
    private LocOutPrintMatService locOutPrintMatService;
 
    @Autowired
    private LocOwnerService locOwnerService;
    @Autowired
    private PlaService plaService;
    @Autowired
    private PlaQtyService plaQtyService;
    @Autowired
    private SaasLogService saasLogService;
 
 
 
 
    @Override
    @Transactional
    public void comb(CombParam param, Long userId) {
        if (Cools.isEmpty(param.getBarcode(), param.getCombMats())) {
            throw new CoolException(BaseRes.PARAM);
        }
//        if (Cools.isEmpty(param.getBarcode())){
//            throw new CoolException("请填写货主信息");
//        }
        // 判断是否有相同条码的数据
        if (waitPakinService.selectCount(new EntityWrapper<WaitPakin>().
                eq("zpallet", param.getBarcode()).eq("io_status", "N")) > 0) {
            throw new CoolException(param.getBarcode() + "数据正在进行入库");
        }
        try{
            param.setOrderNo(param.getCombMats().get(0).getOrderNo());
        }catch (Exception e){
 
        }
 
        int countLoc = locDetlService.selectCount(new EntityWrapper<LocDetl>().eq("zpallet",param.getBarcode()));
        WrkMast wrkMast = wrkMastService.selectByBarcode(param.getBarcode());
        if (wrkMast != null && wrkMast.getIoType() < 100){
            throw new CoolException("工作档/库存条码数据已存在===>>" + param.getBarcode());
 
        }
        if (countLoc > 0 ) {
            throw new CoolException("工作档/库存条码数据已存在===>>" + param.getBarcode());
        }
 
 
        //设置非null批号,
        for (CombParam.CombMat combMat : param.getCombMats()) {
            if (combMat.getBatch() == null){
                combMat.setBatch("");
            }
        }
        Date now = new Date();
        // 无单组托
        if (Cools.isEmpty(param.getOrderNo())) {
 
            // 生成入库通知档
            List<DetlDto> detlDtos = new ArrayList<>();
            param.getCombMats().forEach(elem -> {
                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme(),elem.getWeight());
                if (DetlDto.has(detlDtos, detlDto)) {
                    DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch());
                    assert one != null;
                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
                    one.setOwner(detlDto.getOwner());
                } else {
                    detlDtos.add(detlDto);
                }
            });
 
 
            for (DetlDto detlDto : detlDtos) {
                String uuid = String.valueOf(System.currentTimeMillis());
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                WaitPakin waitPakin = new WaitPakin();
                waitPakin.sync(mat);
                waitPakin.setBatch(detlDto.getBatch());
                waitPakin.setZpallet(param.getBarcode());   // 托盘码
                waitPakin.setIoStatus("N");     // 入出状态
                waitPakin.setAnfme(detlDto.getAnfme());  // 数量
                waitPakin.setStatus("Y");    // 状态
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                waitPakin.setOwner(detlDto.getOwner());
                waitPakin.setUuid(uuid);
                waitPakin.setWeight(detlDto.getWeight());
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
            }
        // 关联组托
        } else {
            for (CombParam.CombMat combMat : param.getCombMats()) {
                OrderDetl orderDetl = orderDetlService.selectItem(combMat.getOrderNo(), combMat.getMatnr(), combMat.getBatch());
                if (orderDetl == null) {
                    throw new CoolException("找不到组托的单据明细");
 
                }
                Double anfme = orderDetl.getAnfme();
                Double workQty = orderDetl.getWorkQty();
                BigDecimal a1 = new BigDecimal(orderDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP);
                BigDecimal b1 = new BigDecimal(orderDetl.getWorkQty()).setScale(2,BigDecimal.ROUND_HALF_UP);
                BigDecimal c1 = new BigDecimal(combMat.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP);
                double e1 = b1.add(c1).doubleValue();
                BigDecimal f1 = new BigDecimal(e1).setScale(2,BigDecimal.ROUND_HALF_UP);
                Double d1 = a1.subtract(f1).doubleValue();
                if ( d1 < 0.0) {
                    throw new CoolException("组托数量已超出订单需求量,请检查是否有其他的组托已完成");
                }
            }
 
            // 生成入库通知档
            List<DetlDto> detlDtos = new ArrayList<>();
            for (CombParam.CombMat elem : param.getCombMats()) {
                Order order = orderService.selectByNo(elem.getOrderNo());
                if (order.getSettle() > 2) {
                    throw new CoolException("单据编号已过期");
                }
                // 订单明细数量校验
                OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), elem.getMatnr(), elem.getBatch());
                if(orderDetl == null){
                    throw new CoolException("该单据中不存在该物料明细:" + elem);
                }
                if (elem.getAnfme() > orderDetl.getEnableQty()) {
                    throw new CoolException(orderDetl.getMatnr() + "入库数量不合法");
                }
                // 修改订单作业数量
                if (!orderDetlService.increaseWorkQty(order.getId(), elem.getMatnr(), elem.getBatch(), elem.getAnfme())) {
                    throw new CoolException("修改单据作业数量失败");
                }
 
                DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme(),elem.getWeight());
                if (DetlDto.has(detlDtos, detlDto)) {
                    DetlDto one = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch());
                    assert one != null;
                    one.setAnfme(one.getAnfme() + detlDto.getAnfme());
                } else {
                    detlDtos.add(detlDto);
                }
            }
            int i=0;
            for (DetlDto detlDto : detlDtos) {
                Order order = orderService.selectByNo(param.getCombMats().get(i).getOrderNo());
                i++;
                String uuid = String.valueOf(System.currentTimeMillis());
                Mat mat = matService.selectByMatnr(detlDto.getMatnr());
                if (Cools.isEmpty(mat)) {
                    throw new CoolException(detlDto.getMatnr() + "商品档案不存在");
                }
                WaitPakin waitPakin = new WaitPakin();
                waitPakin.sync(mat);
                waitPakin.setOrderNo(order.getOrderNo()); // 单据编号
                waitPakin.setBatch(detlDto.getBatch());     // 序列码
                waitPakin.setZpallet(param.getBarcode());   // 托盘码
                waitPakin.setIoStatus("N");     // 入出状态
                waitPakin.setAnfme(detlDto.getAnfme());  // 数量
                waitPakin.setStatus("Y");    // 状态
                waitPakin.setAppeUser(userId);
                waitPakin.setAppeTime(now);
                waitPakin.setModiUser(userId);
                waitPakin.setModiTime(now);
                waitPakin.setOwner(detlDto.getOwner());
                waitPakin.setPayment(detlDto.getPayment());
                waitPakin.setUuid(uuid);
                waitPakin.setWeight(detlDto.getWeight());
                if (!waitPakinService.insert(waitPakin)) {
                    throw new CoolException("保存入库通知档失败");
                }
                orderService.updateSettle(order.getId(), 2L, userId);
            }
 
        }
 
    }
 
    // 商品上架
    @Override
    public void onSale(CombParam param) {
        Date now = new Date();
        // 获取库位号
        String locno = param.getLocno();
        Node node = nodeService.selectByUuid(locno);
        if (Cools.isEmpty(node)) {
            throw new CoolException(param.getLocno() + ":库位不存在");
        }
 
        // 获取商品列表
        for(CombParam.CombMat combMat : param.getCombMats()){
 
            Mat mat = matService.selectByMatnr(combMat.getMatnr());
            if (Cools.isEmpty(mat)){
                throw new CoolException(combMat.getMatnr() + ":商品档案不存在!");
            }
            if (Cools.isEmpty(combMat.getAnfme()) || combMat.getAnfme()==0){
                throw new CoolException(combMat.getMatnr() + ":商品数量有误!");
            }
            if (Cools.isEmpty(combMat.getBatch())){
//                throw new CoolException(combMat.getMatnr() + ":商品批号有误!");
            }
            ManLocDetl manLocDetl = new ManLocDetl();
            manLocDetl.setLocNo(locno);
            manLocDetl.setNodeId(node.getId());
            manLocDetl.setMaktx(mat.getMaktx());
            manLocDetl.setMatnr(mat.getMatnr());
            manLocDetl.setBatch(Cools.isEmpty(combMat.getBatch()) ? "" : combMat.getBatch());
            manLocDetl.setAnfme(combMat.getAnfme());
            manLocDetl.setModiTime(now);
            manLocDetl.setOwner(1);
            if (!manLocDetlService.insert(manLocDetl)) {
                throw new CoolException("商品上架失败!");
            }
        }
    }
 
    // 商品下架
    @Override
    public void offSale(OffSaleParam offSaleParam) {
        ManLocDetl manLocDetl = manLocDetlMapper.selectLocNo0(offSaleParam.getLocNo(), offSaleParam.getMatnr());
        if (Cools.isEmpty(manLocDetl)){
            throw new CoolException("无此商品!");
        }
        double anfme = manLocDetl.getAnfme() - offSaleParam.getAnfme();
        if (anfme < 0) {
            throw new CoolException("商品库存不足!");
        } else if (anfme == 0){
            manLocDetlMapper.deleteLocNo0(offSaleParam.getLocNo(), offSaleParam.getMatnr());
        }
        manLocDetlMapper.updateAnfme0(anfme,manLocDetl.getNodeId());
    }
 
    // 查找商品
 
 
    @Override
    @Transactional
    public void adjust(MobileAdjustParam param, Long userId) {
        BasDevp basDevp = basDevpService.selectById(param.getStaNo());
        if (null == basDevp || basDevp.getWrkNo() == null) {
            throw new CoolException(param.getStaNo() + "盘点站无效");
        }
        if (!param.getWrkNo().equals(basDevp.getWrkNo())) {
            throw new CoolException(param.getStaNo() + "盘点站更新,请重新检索");
        }
        WrkMast wrkMast = wrkMastService.selectById(param.getWrkNo());
        if (wrkMast.getWrkSts() < 10) {
            throw new CoolException("盘点无效,任务已盘点再入库");
        }
        Date now = new Date();
        List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
 
        List<WrkDetl> list = param.getWrkDetls();
 
        // 修改数量
        Iterator<WrkDetl> iterator = wrkDetls.iterator();
        while (iterator.hasNext()) {
            WrkDetl wrkDetl = iterator.next();
            Iterator<WrkDetl> iterator1 = list.iterator();
            while (iterator1.hasNext()) {
                WrkDetl wrkDetl1 = iterator1.next();
                if (wrkDetl1.getAnfme() == 0) {
                    iterator1.remove();
                }
                if (wrkDetl.getMatnr().equals(wrkDetl1.getMatnr()) && Cools.eq(wrkDetl.getBatch(), wrkDetl1.getBatch())) {
                    if (!wrkDetl.getAnfme().equals(wrkDetl1.getAnfme())) {
                        // todo 盘点记录、保存调整记录
                        // 修改明细
                        if (!wrkDetlService.updateAnfme(wrkDetl1.getAnfme(), wrkMast.getWrkNo(), wrkDetl.getMatnr(), wrkDetl.getBatch())) {
                            throw new CoolException(wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "批号修改数量失败");
                        }
                    }
                    iterator.remove();
                    iterator1.remove();
                }
            }
        }
 
        // 删除明细
        for (WrkDetl wrkDetl : wrkDetls) {
            // todo 盘点记录、保存调整记录
            if (!wrkDetlService.updateAnfme(-1.0D, wrkMast.getWrkNo(), wrkDetl.getMatnr(), wrkDetl.getBatch())) {
                throw new CoolException("删除" + wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "批号任务明细失败");
            }
        }
 
        // 添加明细
        for (WrkDetl wrkDetl : list) {
            if (wrkDetl.getAnfme() == 0.0D) { continue; }
            // todo 盘点记录、保存调整记录
            String orderNo = wrkDetl.getOrderNo();
            Mat mat = matService.selectByMatnr(wrkDetl.getMatnr());
            wrkDetl.sync(mat);
            wrkDetl.setOrderNo(orderNo);
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            wrkDetl.setAppeTime(now);
            wrkDetl.setAppeUser(userId);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("添加" + wrkMast.getWrkNo() + "盘点任务," + wrkDetl.getMatnr() + "商品," + wrkDetl.getBatch() + "批号任务明细失败");
            }
        }
 
        // 修改盘点任务主档状态
        wrkMast.setFullPlt(wrkDetlService.selectByWrkNo(wrkMast.getWrkNo()).size() != 0?"Y":"N");
        wrkMast.setModiTime(now);
        wrkMast.setModiUser(userId);
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("修改盘点任务主档失败");
        }
 
    }
 
    @Override
    @Transactional
    public void pakoutByOrder(JSONObject param, Long userId) {
        Integer staNo = param.containsKey("staNo") ? Integer.parseInt(param.get("staNo").toString()) : 0;
        String orderNo = param.containsKey("orderNo") ? param.get("orderNo").toString() : "";
 
        BasDevp sta = basDevpService.checkSiteStatus(staNo);
        //根据订单号生成出库任务工作档
        Order order = orderService.selectOne(new EntityWrapper<Order>().eq("order_no", orderNo));
        if (order.getSettle() != 1 && order.getSettle() != 2){
            throw new CoolException("该订单已处理");
        }
 
        List<OrderDetl> orderDetls = orderDetlService.selectList(new EntityWrapper<OrderDetl>().eq("order_no", orderNo));
        Date now = new Date();
        for(OrderDetl orderDetl : orderDetls){
            //查询所有库位状态为F的库位信息
            List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(),orderDetl.getBatch(),orderDetl.getOwner());
            if (locDetls.size() == 0) {
                throw new CoolException("库存中没有该物料");
            }
            for(LocDetl locDetl : locDetls){
                //如果该库位出库路线所用的堆垛机out_enable不为Y,跳过该循环
                LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                        .eq("loc_no", locDetl.getLocNo()));
                if (Cools.isEmpty(locMast)) {
                    continue;
                }
                BasCrnp crn_no = basCrnpService.selectOne(new EntityWrapper<BasCrnp>()
                        .eq("crn_no", locMast.getCrnNo()));
                if (Cools.isEmpty(crn_no) || !crn_no.getOutEnable().equals("Y")){
                    continue;
                }
 
                //可出库数量 = 订单数量 - 作业中数量
                Double outQty = orderDetl.getAnfme() - orderDetl.getWorkQty();
                if(outQty <= 0){
                    break;
                }
                // 判断入出库类型:101.全板出库 or 103.拣料出库
                Double sumCount = locDetlService.getLocDetlSumQty(locDetl.getLocNo());
                Double curOutQty = outQty >= locDetl.getAnfme() ? locDetl.getAnfme() : outQty;   //本次出库量
                int ioType = sumCount <= curOutQty ? 101 : 103;
 
                stockOut(orderDetl, sta, locDetl, curOutQty, ioType, userId, now);
                order.setSettle(2L);
                order.setUpdateBy(userId);
                order.setUpdateTime(now);
                if(!orderService.update(order, new EntityWrapper<Order>().eq("order_no", orderNo))){
                    throw new CoolException("更新订单状态失败");
                }
                orderDetl.setWorkQty(orderDetl.getWorkQty() + curOutQty);
                orderDetl.setUpdateBy(userId);
                orderDetl.setUpdateTime(now);
                Wrapper wrapper = new EntityWrapper<OrderDetl>().eq("order_no", orderNo)
                        .eq("matnr",orderDetl.getMatnr());
                if(!Cools.isEmpty(orderDetl.getBatch())){
                    wrapper.eq("batch", orderDetl.getBatch());
                }
                if(!orderDetlService.update(orderDetl, wrapper)){
                    throw new CoolException("更新订单明细失败");
                }
            }
 
        }
    }
 
    @Override
    @Transactional
    public void stockOut(OrderDetl orderDetl, BasDevp staNo, LocDetl locDetl,
                         Double curOutQty, Integer ioType, Long userId, Date now) {
        // 获取库位
        LocMast locMast = locMastService.selectById(locDetl.getLocNo());
        // 获取路径
        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                .eq("type_no", ioType)
                .eq("stn_no", staNo.getDevNo())
                .eq("crn_no", locMast.getCrnNo());
 
        StaDesc staDesc = staDescService.selectOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            throw new CoolException("出库路径不存在");
        }
        // 生成工作号
        int workNo = commonService.getWorkNo(2);
        // 生成工作档
        WrkMast wrkMast = new WrkMast();
        wrkMast.setWrkNo(workNo);
        wrkMast.setIoTime(now);
        wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
        wrkMast.setIoType(ioType); // 入出库状态
        wrkMast.setIoPri(13D); // 优先级:13
        wrkMast.setCrnNo(locMast.getCrnNo());
        wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
        wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
        wrkMast.setSourceLocNo(locDetl.getLocNo()); // 源库位
        wrkMast.setFullPlt("Y"); // 满板:Y
        wrkMast.setPicking("N"); // 拣料
        wrkMast.setExitMk("N"); // 退出
        wrkMast.setEmptyMk("N"); // 空板
        wrkMast.setLinkMis("N");
        wrkMast.setAppeUser(userId); // 操作人员数据
        wrkMast.setAppeTime(now);
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        wrkMast.setBarcode(locMast.getBarcode());
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败,出库库位号:" + locDetl.getLocNo());
        }
 
        WrkDetl wrkDetl = new WrkDetl();
        wrkDetl.sync(locDetl);
        wrkDetl.setWrkNo(workNo);
        wrkDetl.setIoTime(now);
        wrkDetl.setAnfme(curOutQty); // 数量
        wrkDetl.setOrderNo(orderDetl.getOrderNo());
        wrkDetl.setAppeTime(now);
        wrkDetl.setAppeUser(userId);
        wrkDetl.setModiTime(now);
        wrkDetl.setModiUser(userId);
        if (!wrkDetlService.insert(wrkDetl)) {
            throw new CoolException("保存工作档明细失败");
        }
 
        // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
        locMast = locMastService.selectById(locDetl.getLocNo());
        if (locMast.getLocSts().equals("F")) {
            locMast.setLocSts(ioType == 101 ? "R" : "P");
            locMast.setModiUser(userId);
            locMast.setModiTime(new Date());
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("预约库位状态失败,库位号:" + locDetl.getLocNo());
            }
        } else {
            throw new CoolException(locDetl.getLocNo() + "库位不是在库状态");
        }
    }
 
    @Transactional
    @Override
    public R manDetlIn(JSONObject json, User user) {
        Date date = new Date();
        String jsonLocNo = (String) json.get("locNo");
 
        LocMast locMast = locMastService.selectLocStatus(jsonLocNo);
        if (locMast.getLocSts().equals("X")){
            return R.error("库位已被冻结");
        }
 
        List<ManLocDetl> locDetls = manLocDetlService.selectList(new EntityWrapper<ManLocDetl>().eq("loc_no", jsonLocNo));
        Node node = nodeService.selectOne(new EntityWrapper<Node>()
                .eq("name", jsonLocNo));
 
//        if (node.getParentName().equals("A") || node.getParentName().equals("B")){
//            if (node.getRow1() == 1 && locDetls.size() >=11){
//                return R.error("该库位排已满");
//            }
//            if (node.getRow1() == 6 && locDetls.size() >=4){
//                return R.error("该库位排已满");
//            }
//            if (node.getRow1() == 12 && locDetls.size() >=4){
//                return R.error("该库位排已满");
//            }
//
//        }
//        if (locDetls.size() >=12){
//            return R.error("该库位排已满");
//        }
 
 
 
        JSONArray combMats = json.getJSONArray("combMats");
        for (int i = 0; i < combMats.size(); i++) {
            OrderDetl jsonOrderDetl = combMats.getObject(i, OrderDetl.class);
 
            //查订单
            Order order = orderService.selectOne(new EntityWrapper<Order>()
                    .eq("order_no", jsonOrderDetl.getOrderNo()));
            if (Cools.isEmpty(node, order)) {
                return R.error("参数为空");
            }
 
            //判断订单类型是否是入库
            DocType docType=docTypeService.selectById(order.getDocType());
            if(docType.getPakin()!=1 || docType.getStatus()!=1){
                return R.error("该订单是出库订单,无法入库");
            }
 
            OrderDetl orderDetl = orderDetlService.selectItem(jsonOrderDetl.getOrderNo(),jsonOrderDetl.getMatnr(),jsonOrderDetl.getBatch());
            if (Cools.isEmpty(orderDetl)) {
                return R.error("单据明细有误,请检查");
            }
            if (orderDetl.getAnfme() - (jsonOrderDetl.getAnfme() + orderDetl.getWorkQty()) < 0) {
                return R.error("入库数量大于可入数量");
            }
            //查询平库中是否有一样的物料号,有的话直接增加数量
            ManLocDetl checkManLocDetl = manLocDetlService.selectInventory(jsonLocNo,orderDetl.getMatnr(),jsonOrderDetl.getBatch());
            if (checkManLocDetl == null) {
 
                ManLocDetl manLocDetl = new ManLocDetl();
                Synchro.Copy(orderDetl, manLocDetl);
                manLocDetl.setLocNo(node.getName());
                manLocDetl.setNodeId(node.getId());
                manLocDetl.setMatnr(orderDetl.getMatnr());
                manLocDetl.setMaktx(jsonOrderDetl.getMaktx());
                manLocDetl.setAnfme(jsonOrderDetl.getAnfme());
                manLocDetl.setModiTime(date);
                manLocDetl.setCreateTime(date);
                manLocDetl.setBatch(jsonOrderDetl.getBatch());
                manLocDetl.setOwner(orderDetl.getOwner());
                manLocDetl.setWeight(jsonOrderDetl.getWeight());
                manLocDetl.setOrderNo(jsonOrderDetl.getOrderNo());
                manLocDetl.setStockFreeze(1);
                if(!manLocDetlService.insert(manLocDetl)){
                    return R.error("插入平库物料失败!");
                }
            }
            else {
                  if (checkManLocDetl.getWeight() == null){
                      checkManLocDetl.setWeight(0.0);
                  }
                  if (jsonOrderDetl.getWeight() == null){
                      jsonOrderDetl.setWeight(0.0);
                  }
                if(manLocDetlService.increase(checkManLocDetl.getAnfme() + jsonOrderDetl.getAnfme(),
                        node.getName(),
                        jsonOrderDetl.getMatnr(),
                        jsonOrderDetl.getBatch(),checkManLocDetl.getWeight()+jsonOrderDetl.getWeight()
                        )<=0
                        ){
                    return R.error("修改平库物料失败!");
                }
 
            }
            orderDetl.setQty(orderDetl.getQty() + jsonOrderDetl.getAnfme());
            orderDetl.setWorkQty(orderDetl.getWorkQty() + jsonOrderDetl.getAnfme());
            orderDetl.setUpdateTime(date);
            orderDetlService.updateById(orderDetl);
            //更新订单状态
            List<OrderDetl> orderDetls=orderDetlService.selectList(new EntityWrapper<OrderDetl>().eq("order_no",order.getOrderNo()));
            order.setSettle(2L);
            boolean log=true;
            for (OrderDetl orderDetl1: orderDetls) {
                //工作中的数量小于总订单数时,订单状态改为2,作业中
                if(orderDetl1.getQty() < orderDetl.getAnfme()){
                    log=false;
                }
            }
            if(log){
                order.setSettle(4L);
            }
            if(!orderService.updateById(order)){
                return R.error("更新订单状态失败");
            }
            orderService.checkComplete(order.getOrderNo());
 
            SaasUtils.insertLog(0,jsonLocNo,jsonOrderDetl.getMatnr(), jsonOrderDetl.getAnfme(),user.getUsername(),
        null,null,null,null,null,null);
        }
 
 
        return R.ok("上架完成");
    }
 
    @Transactional
    @Override
    public R manDetlOut(JSONObject json,User user) {
        Date date = new Date();
        String jsonLocNo = (String) json.get("locNo");
 
        LocMast locMast = locMastService.selectLocStatus(jsonLocNo);
        if (locMast.getLocSts().equals("X")){
            return R.error("库位已被冻结");
        }
 
        Node node = nodeService.selectOne(new EntityWrapper<Node>()
                .eq("name", jsonLocNo));
        JSONArray combMats = json.getJSONArray("combMats");
        for (int i = 0; i < combMats.size(); i++) {
            OrderDetl jsonOrderDetl = combMats.getObject(i, OrderDetl.class);
            Order order = orderService.selectOne(new EntityWrapper<Order>()
                    .eq("order_no", jsonOrderDetl.getOrderNo()));
 
            //判断订单类型是否是出库
            DocType docType=docTypeService.selectById(order.getDocType());
            if(docType.getPakout()!=1 || docType.getStatus()!=1){
                return R.error("该订单是入库订单,无法出库");
            }
            if (Cools.isEmpty(node, order)) {
                return R.error("参数为空");
            }
            OrderDetl orderDetl = orderDetlService.selectItem(jsonOrderDetl.getOrderNo(),jsonOrderDetl.getMatnr(),"");
            if (Cools.isEmpty(orderDetl)) {
                return R.error("单据明细有误,请检查");
            }
            EntityWrapper<ManPakOut> manPakOutEntityWrapper = new EntityWrapper<>();
            manPakOutEntityWrapper.eq("loc_no",jsonLocNo);
            manPakOutEntityWrapper.eq("matnr",jsonOrderDetl.getMatnr());
            manPakOutEntityWrapper.eq("doc_num",order.getOrderNo());
            ManPakOut manPakOut = manPakOutService.selectOne(manPakOutEntityWrapper); //获取拣货单中的对应的订单
            if(Cools.isEmpty(manPakOut)){
                return R.error("没有找到对应的出库单"+jsonLocNo+"-------"+jsonOrderDetl.getMatnr());
            }
            if(manPakOut.getCount() == null){
                manPakOut.setCount(0.0);
            }
//            if (jsonOrderDetl.getAnfme() > manPakOut.getAnfme()) { //判断工作数量是否大于拣货单的数量
//                return R.error("出库数量大于拣货单总数量");
//            }
            BigDecimal a1 = new BigDecimal(manPakOut.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP);
            BigDecimal b1 = new BigDecimal(manPakOut.getCount()).setScale(2,BigDecimal.ROUND_HALF_UP);
            Double c1 = a1.subtract(b1).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            if (jsonOrderDetl.getAnfme() > c1) { //判断工作数量是否大于拣货单剩余可出的数量
                return R.error("出库数量大于拣货单剩余可出数量");
            }
 
            BigDecimal e1 = new BigDecimal(orderDetl.getWorkQty()).setScale(2,BigDecimal.ROUND_HALF_UP);
            BigDecimal f1 = new BigDecimal(orderDetl.getQty()).setScale(2,BigDecimal.ROUND_HALF_UP);
            Double g1 = e1.subtract(f1).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            if ( g1  <jsonOrderDetl.getAnfme() ){
                return R.error("出库数量大于单据剩余可出数量");
            }
            //查询平库中是否有一样的物料号
            ManLocDetl checkManLocDetl = manLocDetlService.selectInventory(jsonLocNo,orderDetl.getMatnr(),orderDetl.getBatch());
            if (checkManLocDetl == null) {
                return R.error("该库位没有出库的物料信息");
            }
            if (jsonOrderDetl.getAnfme() > checkManLocDetl.getAnfme()) {
                return R.error("出库数量大于可出数量");
            }
            if(checkManLocDetl.getWeight() == null){
                checkManLocDetl.setWeight(0.0);
            }
            if(jsonOrderDetl.getWeight() == null){
                jsonOrderDetl.setWeight(0.0);
            }
            Double finalQty =  new BigDecimal(checkManLocDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal(jsonOrderDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() ;
            Double weight =  new BigDecimal(checkManLocDetl.getWeight()).setScale(2,BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal(jsonOrderDetl.getWeight()).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() ;
            checkManLocDetl.setAnfme(finalQty);
            checkManLocDetl.setWeight(weight);
            if (weight < 0){
                return R.error("出库重量有误!");
            }
            if (finalQty < 0){
                //  if(manLocDetlService.deleteDatailed(jsonLocNo, jsonOrderDetl.getMatnr(), jsonOrderDetl.getBatch())<=0) {
                return R.error("数量有误!");
                //   }
            }else {
                if(manLocDetlService.increase(finalQty, jsonLocNo, jsonOrderDetl.getMatnr(), jsonOrderDetl.getBatch(),weight)<=0){
                    return R.error("修改平库物料失败!");
                }
            }
            orderDetl.setQty( new BigDecimal(orderDetl.getQty()).setScale(2,BigDecimal.ROUND_HALF_UP).add(new BigDecimal(jsonOrderDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue() );
            orderDetl.setUpdateTime(date);
            orderDetlService.updateById(orderDetl);
            //更新订单状态
            List<OrderDetl> orderDetls=orderDetlService.selectList(new EntityWrapper<OrderDetl>().eq("order_no",order.getOrderNo()));
            order.setSettle(2L);
            boolean log=true;
            for (OrderDetl orderDetl1: orderDetls) {
                //工作中的数量小于总订单数时,订单状态改为2,作业中
                if(orderDetl1.getQty() < orderDetl1.getAnfme()){
                    log=false;
                }
            }
            if(log){
                order.setSettle(4L);
            }
            if(!orderService.updateById(order)){
                return R.error("更新订单状态失败");
            }
            if(manPakOut.getCount() == null){
                manPakOut.setCount(0.0);
            }
 
            manPakOut.setCount(new BigDecimal(manPakOut.getCount()).setScale(2,BigDecimal.ROUND_HALF_UP).add(new BigDecimal(jsonOrderDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
            if (!manPakOutService.update(manPakOut,manPakOutEntityWrapper)){
                return R.error("更新拣货单完成数目失败");
            }
            if (manPakOut.getAnfme().equals(manPakOut.getCount())){
                manPakOut.setStatus(1);
                if (!manPakOutService.update(manPakOut,manPakOutEntityWrapper)){
                    return R.error("更新拣货单状态失败");
                }
            }
            EntityWrapper<ManLocDetl> manLocDetlEntityWrapper = new EntityWrapper<>();
            manLocDetlEntityWrapper.eq("loc_no",manPakOut.getLocNo());
            manLocDetlEntityWrapper.eq("matnr",manPakOut.getMatnr());
            if (checkManLocDetl.getAnfme() == 0 ){
                if(!manLocDetlService.delete(manLocDetlEntityWrapper)){
                    return R.error("删除平库库存失败");
                }
            }else{
                checkManLocDetl.setStatus(1);
                if(!manLocDetlService.update(checkManLocDetl,manLocDetlEntityWrapper)){
                    return R.error("更新平库库存状态失败");
                }
            }
            SaasUtils.insertLog(1,jsonLocNo,jsonOrderDetl.getMatnr(), jsonOrderDetl.getAnfme(),user.getUsername(),
                    null,null,null,null,null,null);
 
        }
        return R.ok("下架完成");
    }
 
    @Override
    public R manDetlInBarcode(JSONObject json, User user) {
        String jsonLocNo = (String) json.get("locNo"); //获取库位码
        String jsonBarNo = (String) json.get("barcode"); //获取托盘码
 
        LocMast locMast = locMastService.selectLocStatus(jsonLocNo);
        if (locMast.getLocSts().equals("X")){
            return R.error("库位已被冻结");
        }
 
        List<WaitPakin> waitPakins = waitPakinService.selectList(new EntityWrapper<WaitPakin>().eq("zpallet", jsonBarNo)); //获取对应组托信息
        if (Cools.isEmpty(waitPakins)){
            return R.error("未查询到组托");
        } else if (waitPakins.get(0).getIoStatus().equals("Y")) {
            return R.error("组托已经生成工作档");
        }
 
        for (WaitPakin waitPakin: waitPakins) {
            List<ManLocDetl> manLocDetls = manLocDetlService.selectList(new EntityWrapper<ManLocDetl>().eq("zpallet", waitPakin.getZpallet()));
            if (!Cools.isEmpty(manLocDetls) || manLocDetls.size()>0){
                return R.error("所在库位已存在明细");
            }
        }
        //判断订单是否完成
        boolean log =true;
 
        for (WaitPakin waitPakin: waitPakins) {
            Order order = orderService.selectOne(new EntityWrapper<Order>().eq("order_no", waitPakin.getOrderNo()));  //获取对应订单信息
            if (order.equals(null)){
                return R.error("未查询到订单信息");
            }
            OrderDetl orderDetl = orderDetlService.selectOne(new EntityWrapper<OrderDetl>().eq("order_no", waitPakin.
                    getOrderNo()).eq("matnr", waitPakin.getMatnr()));  //获取对应订单明细信息
            if (orderDetl.equals(null)){
                return R.error("未查询到订单明细信息");
            }
            Node node = nodeService.selectOne(new EntityWrapper<Node>().eq("name", jsonLocNo));  //获取对应库位信息
            if (node.equals(null)){
                return R.error("未查询到库位信息");
            }
            Date date = new Date();
            ManLocDetl manLocDetl = new ManLocDetl();  //初始化库存实体类
            manLocDetl.setLocNo(node.getName());
            manLocDetl.setNodeId(node.getId());
            manLocDetl.setZpallet(waitPakin.getZpallet());
            manLocDetl.setAnfme(waitPakin.getAnfme());
            manLocDetl.setMatnr(waitPakin.getMatnr());
            manLocDetl.setMaktx(waitPakin.getMaktx());
            manLocDetl.setStatus(1);
            manLocDetl.setCreateBy(user.getId());
            manLocDetl.setCreateTime(date);
            manLocDetl.setUpdateBy(user.getId());
            manLocDetl.setModiTime(date);
            manLocDetl.setOrderNo(waitPakin.getOrderNo());
            manLocDetl.setStockFreeze(1);
 
            if (!manLocDetlService.insert(manLocDetl)){  //数据库插入实体类信息
                return R.error("物料信息入库失败");
            }
            orderDetl.setQty(orderDetl.getQty()+ waitPakin.getAnfme());  //更新订单明细数量
            if (!orderDetlService.updateById(orderDetl)){
                return R.error("更新订单明细失败");
            }
 
            WaitPakinLog waitPakinLog = new WaitPakinLog(); //初始化入库通知单日志实体类
            waitPakinLog.setZpallet(waitPakin.getZpallet());
            waitPakinLog.setAnfme(waitPakin.getAnfme());
            waitPakinLog.setMatnr(waitPakin.getMatnr());
            waitPakinLog.setLocNo(jsonLocNo);
            waitPakinLog.setMaktx(waitPakin.getMaktx());
            waitPakinLog.setOrderNo(waitPakin.getOrderNo());
            waitPakinLog.setWeight(waitPakin.getWeight());
            waitPakinLog.setStatus("Y");
            waitPakinLog.setIoStatus("Y");
            waitPakinLog.setModiUser(user.getId());
            waitPakinLog.setModiTime(date);
            waitPakinLog.setAppeUser(user.getId());
            waitPakinLog.setAppeTime(date);
            waitPakinLog.setUuid(waitPakin.getUuid());
 
            if (!waitPakinLogService.insert(waitPakinLog)){  //插入入库通知历史档数据库
                return R.error("插入历史入库通知档失败");
            }
            //订单是否完成
            List<OrderDetl> or = orderDetlService.selectList(new EntityWrapper<OrderDetl>().eq("order_no", waitPakin.getOrderNo()));
            for (OrderDetl o: or) {
                if(o.getAnfme()-o.getQty()>0){
                    log=false;
                    break;
                }
            }
            if(log){
                order.setSettle(4L);
                if(!orderService.update(order,new EntityWrapper<Order>().eq("order_no", waitPakin.getOrderNo()))){
                    return R.error("订单转完成状态失败!!!");
                }
            }
        }
 
        if (!waitPakinService.delete(new EntityWrapper<WaitPakin>().eq("zpallet",jsonBarNo))){  //删除原入库通知档
            return R.error("删除入库通知档失败");
        }
        return R.ok("上架成功");
    }
 
    @Override
    public R adjustNew(MobileAdjustNewParam combParam, Long userId) {
 
 
        for (WrkDetl detl: combParam.getWrkDetls()){
            EntityWrapper<LocCheck> wrapper = new EntityWrapper<>();
            wrapper.eq("order_no", combParam.getOrderNo())
                    .eq("matnr", detl.getMatnr())
                    .eq("loc_no", detl.getLocNo())
                    .eq("batch",detl.getBatch());
            LocCheck CheckDetl = locCheckService.selectOne(wrapper);
            if (CheckDetl == null){
                Date now = new Date();
                LocCheck locCheck = new LocCheck();
                locCheck.setLocNo(detl.getLocNo());
                locCheck.setMaktx(detl.getMaktx());
                locCheck.setType(1);
                locCheck.setMatnr(detl.getMatnr());
                locCheck.setAnfme(detl.getAnfme());
                locCheck.setRealAnfme(detl.getAnfme());
                locCheck.setDiffAnfme(detl.getAnfme());
                locCheck.setExamine(0);
                locCheck.setOwner(detl.getOwner());
                locCheck.setPayment(detl.getPayment());
                locCheck.setCreateTime(now);
                locCheck.setUpdateTime(now);
                locCheck.setOrderNo(combParam.getOrderNo());
                locCheck.setBatch(detl.getBatch());
 
                if (!locCheckService.insert(locCheck)){
                    return R.error("盘点明细插入失败");
                }
            }else {
                CheckDetl.setRealAnfme(detl.getAnfme());
                CheckDetl.setDiffAnfme(detl.getAnfme()- CheckDetl.getAnfme());
                if (!locCheckService.update(CheckDetl,wrapper)){
                    return R.error("明细更新失败");
                }
 
 
 
            }
 
 
        }
        OrderCheck orderCheck = orderCheckService.selectOne(new EntityWrapper<OrderCheck>().eq("order_no", combParam.getOrderNo()));
        orderCheck.setSettle(2L);
        if (!orderCheckService.update(orderCheck,new EntityWrapper<OrderCheck>().eq("order_no", combParam.getOrderNo()))){
            return R.error("更新盘点单状态失败!");
        }
 
 
        return R.ok("盘点成功");
    }
 
    @Override
    public R manDetlOrigin(JSONObject json, User user) {
        Date now = new Date();
        String jsonLocNo = (String) json.get("locNo"); //获取库位码
        String ownerName = (String) json.get("owner"); //获取拥有者
        LocOwner locOwner = locOwnerService.selectOne(new EntityWrapper<LocOwner>().eq("owner", ownerName));
        Long ownerId = locOwner.getId();
        String jsonCode = (String) json.get("code"); //获取条码
        String jsonOrigin = (String) json.get("origin"); //获取来源地
        if (Cools.isEmpty(jsonLocNo)){
            return R.error("库位号参数为空!");
        }else if (Cools.isEmpty(ownerId)){
            return R.error("拥有者参数为空!");
        }else if (Cools.isEmpty(jsonCode)){
            return R.error("条码参数为空!");
        }else if (Cools.isEmpty(jsonOrigin)){
            return R.error("来源地参数为空!");
        }
        Integer jsonOwner = ownerId.intValue(); //获取拥有者
        OriginRule originRule = originRuleService.selectOne(new EntityWrapper<OriginRule>()
                .eq("origin_address", jsonOrigin));
 
        String code = "0";
        String weight = "0";
        String date = "0";
        try{
            if (originRule.getStartCode() != 0 && originRule.getEndCode() !=0){
                code = jsonCode.substring(originRule.getStartCode() - 1, originRule.getEndCode());
            }
            if (originRule.getStartWeight() != 0 && originRule.getEndWeight() !=0){
                weight = jsonCode.substring(originRule.getStartWeight() - 1, originRule.getEndWeight());
            }
            if (originRule.getStartDate() != 0 && originRule.getEndDate() !=0){
                date = jsonCode.substring(originRule.getStartDate() - 1, originRule.getEndDate());
            }
        }catch (Exception e){
            return R.error("条码位置有误");
        }
        Node node = nodeService.selectOne(new EntityWrapper<Node>().eq("name", jsonLocNo));  //获取对应库位信息
        if (Cools.isEmpty(node)){
            return R.error("未查询到库位信息");
        }
 
        JSONArray combMats = json.getJSONArray("combMats");
        for (int i = 0; i < combMats.size(); i++) {
            OrderDetl jsonOriginDetl = combMats.getObject(i, OrderDetl.class);
 
            Mat mat = matService.selectByMatnr(jsonOriginDetl.getMatnr());
            if (Cools.isEmpty(mat)){
                return R.error("未查询到商品信息");
            }
            //增加打印档案
            LocInPrintMat locInPrintMat = new LocInPrintMat(now,user.getId(), mat.getMatnr(),jsonOriginDetl.getBatch(),jsonOriginDetl.getAnfme(),jsonLocNo,mat.getMaktx());
            locInPrintMat.setOwnerId(ownerId);
            locInPrintMatService.insert(locInPrintMat);
 
            Wrapper<ManLocDetl> manLocDetlWrapper = new EntityWrapper<ManLocDetl>().eq("loc_no", jsonLocNo)
                    .eq("matnr", jsonOriginDetl.getMatnr()).eq("batch",date).eq("owner",jsonOwner);
            ManLocDetl manLocDetl1 = manLocDetlService.selectOne(manLocDetlWrapper);
            if(!Cools.isEmpty(manLocDetl1)){
                BigDecimal inAnfme = BigDecimal.valueOf(jsonOriginDetl.getAnfme());
                BigDecimal anfme = BigDecimal.valueOf(manLocDetl1.getAnfme());
 
                BigDecimal sum = anfme.add(inAnfme);
                manLocDetl1.setAnfme(sum.doubleValue());
                if (!manLocDetlService.update(manLocDetl1,manLocDetlWrapper)) {
                    return R.error("物料信息上架失败");
                }
 
            }else {
                ManLocDetl manLocDetl = new ManLocDetl();  //初始化库存实体类
                manLocDetl.sync(mat);
                manLocDetl.setLocNo(node.getUuid());
                manLocDetl.setNodeId(node.getId());
//            manLocDetl.setZpallet(waitPakin.getZpallet());
                manLocDetl.setAnfme(jsonOriginDetl.getAnfme());
                manLocDetl.setName(jsonCode);
                manLocDetl.setModel(code);
                manLocDetl.setWeight(Double.valueOf(weight));
                manLocDetl.setBatch(date);
                manLocDetl.setCustName(jsonOriginDetl.getOrigin());
                manLocDetl.setStatus(1);
                manLocDetl.setCreateBy(user.getId());
                manLocDetl.setCreateTime(now);
                manLocDetl.setUpdateBy(user.getId());
                manLocDetl.setModiTime(now);
                // manLocDetl.setOrderNo(waitPakin.getOrderNo());
                manLocDetl.setStockFreeze(1);
                manLocDetl.setOwner(jsonOwner);
                try {
                    Date produceTime = DateUtils.convert(manLocDetl.getBatch(),DateUtils.yyyyMMdd);
                    manLocDetl.setProduceTime(produceTime);
                } catch (Exception e) {
                    manLocDetl.setProduceTime(now);
                }
 
                if (!manLocDetlService.insert(manLocDetl)){  //数据库插入实体类信息
                    return R.error("物料信息入库失败");
                }
            }
 
        }
        return R.ok();
    }
 
    @Override
    public R manDetlOrigInNo(JSONObject json, User user) {
        Date now = new Date();
        String jsonLocNo = (String) json.get("locNo"); //获取库位码
        String ownerName = (String) json.get("owner"); //获取拥有者
        LocOwner locOwner = locOwnerService.selectOne(new EntityWrapper<LocOwner>().eq("owner", ownerName));
        Long ownerId = locOwner.getId();
        if (Cools.isEmpty(jsonLocNo)){
            return R.error("库位号参数为空!");
        }else if (Cools.isEmpty(ownerId)){
            return R.error("拥有者参数为空!");
        }
        Integer jsonOwner = ownerId.intValue(); //获取拥有者
        Node node = nodeService.selectOne(new EntityWrapper<Node>().eq("name", jsonLocNo));  //获取对应库位信息
        if (Cools.isEmpty(node)){
            return R.error("未查询到库位信息");
        }
 
        JSONArray combMats = json.getJSONArray("combMats");
        for (int i = 0; i < combMats.size(); i++) {
            OrderDetl jsonOriginDetl = combMats.getObject(i, OrderDetl.class);
 
            Mat mat = matService.selectByMatnr(jsonOriginDetl.getMatnr());
            if (Cools.isEmpty(mat)){
                return R.error("未查询到商品信息");
            }
            //增加打印档案
            LocInPrintMat locInPrintMat = new LocInPrintMat(now,user.getId(), mat.getMatnr(),jsonOriginDetl.getBatch(),jsonOriginDetl.getAnfme(),jsonLocNo,mat.getMaktx());
            locInPrintMat.setOwnerId(ownerId);
            locInPrintMatService.insert(locInPrintMat);
 
            Wrapper<ManLocDetl> manLocDetlWrapper = new EntityWrapper<ManLocDetl>().eq("loc_no", jsonLocNo)
                    .eq("matnr", jsonOriginDetl.getMatnr()).eq("batch",jsonOriginDetl.getBatch()).eq("owner",jsonOwner);
            ManLocDetl manLocDetl1 = manLocDetlService.selectOne(manLocDetlWrapper);
            if(!Cools.isEmpty(manLocDetl1)){
                BigDecimal inAnfme = BigDecimal.valueOf(jsonOriginDetl.getAnfme());
                BigDecimal anfme = BigDecimal.valueOf(manLocDetl1.getAnfme());
 
                BigDecimal sum = anfme.add(inAnfme);
                manLocDetl1.setAnfme(sum.doubleValue());
                if (!manLocDetlService.update(manLocDetl1,manLocDetlWrapper)) {
                    return R.error("物料信息上架失败");
                }
 
            }else {
                ManLocDetl manLocDetl = new ManLocDetl();  //初始化库存实体类
                manLocDetl.sync(mat);
                manLocDetl.setLocNo(node.getUuid());
                manLocDetl.setNodeId(node.getId());
//            manLocDetl.setZpallet(waitPakin.getZpallet());
                manLocDetl.setAnfme(jsonOriginDetl.getAnfme());
                manLocDetl.setName("");
                manLocDetl.setModel("");
                manLocDetl.setWeight(jsonOriginDetl.getAnfme());
                manLocDetl.setBatch(jsonOriginDetl.getBatch());
                manLocDetl.setCustName(jsonOriginDetl.getOrigin());
                manLocDetl.setStatus(1);
                manLocDetl.setCreateBy(user.getId());
                manLocDetl.setCreateTime(now);
                manLocDetl.setUpdateBy(user.getId());
                manLocDetl.setModiTime(now);
                manLocDetl.setOwner(jsonOwner);
                // manLocDetl.setOrderNo(waitPakin.getOrderNo());
                manLocDetl.setStockFreeze(1);
                try {
                    Date produceTime = DateUtils.convert(jsonOriginDetl.getBatch(),DateUtils.yyyyMMdd);
                    manLocDetl.setProduceTime(produceTime);
                } catch (Exception e) {
                    manLocDetl.setProduceTime(now);
                }
                if (!manLocDetlService.insert(manLocDetl)){  //数据库插入实体类信息
                    return R.error("物料信息入库失败");
                }
            }
 
        }
        return R.ok();
    }
 
    @Override
    public R manDetlOriginOut(JSONObject json, User user) {
        Date now = new Date();
        JSONArray combMats = json.getJSONArray("combMats");
        List<MatPrint> matPrintList=new ArrayList<>();
        List<Long> ids=new ArrayList<>();
        for (int i = 0; i < combMats.size(); i++) {
            MatPrint matPrintNow = combMats.getObject(i, MatPrint.class);
            if (!ids.contains(matPrintNow.getIndex())){
                ids.add(matPrintNow.getIndex());
                matPrintList.add(matPrintNow);
            }
        }
        for (MatPrint jsonOriginDetl:matPrintList) {
            Double parseLong = jsonOriginDetl.getAnfme();
            Long ownerId = Long.parseLong(jsonOriginDetl.getOwner());
            jsonOriginDetl.setOwnerId(ownerId);
            Wrapper<ManLocDetl> manLocDetlWrapper = new EntityWrapper<ManLocDetl>().eq("loc_no", jsonOriginDetl.getLocNo())
                    .eq("matnr", jsonOriginDetl.getMatnr()).eq("batch", jsonOriginDetl.getBatch()).eq("owner",jsonOriginDetl.getOwnerId());
            ManLocDetl manLocDetl = manLocDetlService.selectOne(manLocDetlWrapper);
            if (Cools.isEmpty(manLocDetl)){
                return R.error("未查询到商品明细信息!库位号:"+jsonOriginDetl.getLocNo()+";商品编号:"+jsonOriginDetl.getMatnr()+"批次:"+jsonOriginDetl.getBatch()+"货主"+jsonOriginDetl.getOwnerId());
            }
            BigDecimal outAnfme = BigDecimal.valueOf(parseLong);
            BigDecimal anfme = BigDecimal.valueOf(manLocDetl.getAnfme());
 
            if (manLocDetl.getAnfme() > parseLong){
                BigDecimal num = anfme.subtract(outAnfme);
                manLocDetl.setAnfme(num.doubleValue());
                manLocDetl.setUpdateBy(user.getId());
                manLocDetl.setModiTime(now);
                if (!manLocDetlService.update(manLocDetl,manLocDetlWrapper)) {
                    return R.error("物料信息下架失败");
                }
 
            }else if (manLocDetl.getAnfme().equals(jsonOriginDetl.getAnfme())) {
                if (!manLocDetlService.delete(manLocDetlWrapper)) {
                    return R.error("物料信息删除失败");
                }
            }
        }
        for (MatPrint jsonOriginDetl:matPrintList) {
            //增加打印档案
            LocOutPrintMat locOutPrintMat = new LocOutPrintMat(now, user.getId(), jsonOriginDetl.getMatnr(), jsonOriginDetl.getBatch(), jsonOriginDetl.getAnfme(), jsonOriginDetl.getLocNo(), jsonOriginDetl.getMaktx());
            locOutPrintMat.setOwnerId(jsonOriginDetl.getOwnerId());
            locOutPrintMatService.insert(locOutPrintMat);
        }
        return R.ok();
    }
 
    @Override
    public void plaPakin(String brand, String locNo, String batch, Integer packageNo, User user) {
        Pla pla = plaService.selectByBatchAndPackageNo(batch, packageNo,brand);
        if(Cools.isEmpty(pla)){
            throw new CoolException("系统未检测到该包物料信息,请重新录入");
        }
 
        if(!pla.getStatus().equals(GlobleParameter.PLA_STATUS_0) && !pla.getStatus().equals(GlobleParameter.PLA_STATUS_00)){
            throw new CoolException("该物料状态不为待入库,无法入库");
        }
 
        pla.setStatus(GlobleParameter.PLA_STATUS_1);
        pla.setModifyTime(new Date());
        pla.setPakinTime(new Date());
 
        Node node = nodeService.selectByUuid(locNo);
        if(Cools.isEmpty(node)){
            throw new CoolException("库位信息不正确");
        }
 
        pla.setStash(node.getParentName());
        pla.setLocNo(node.getUuid());
 
        plaService.updateById(pla);
        SaasUtils.insertLog(0,locNo,pla.getBrand(),pla.getWeightAnfme(),user.getUsername(),
                null,pla.getBatch(),pla.getPackageNo(),pla.getOwner(),pla.getWorkshop(),null);
    }
 
    @Override
    @Transactional
    @Synchronized
    public void plaPakout(String brand, String locNo, String batch, Integer packageNo, String orderNo, String plaQtyId, String wrkNo, Double anfme, boolean isReplace) {
        Pla pla = plaService.selectByBatchAndPackageNo(batch, packageNo,brand);
 
        ManPakOut manPakOut = manPakOutService.selectById(wrkNo);
 
        PlaQty plaQty = plaQtyService.selectById(plaQtyId);
 
        //物料替换
        if(isReplace){
            replacePla(pla,manPakOut,plaQty);
        }else {
            if((!Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_2) && !Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_3) && !Cools.eq(pla.getStatus(),GlobleParameter.PLA_STATUS_1)) ||
                    !Cools.eq(pla.getBatch(),manPakOut.getBatch()) ||
                    !Cools.eq(pla.getPackageNo()+"",manPakOut.getBarcode()) ||
                    !Cools.eq(pla.getBrand(),manPakOut.getMaktx()) ){
                throw new CoolException("物料不一致,无法出库" + "批号:" + pla.getBatch() + ",包号:" + pla.getPackageNo() + ",牌号:" + pla.getBrand());
            }
        }
 
        //更新拣货单信息
        if(manPakOut.getCount() + anfme > manPakOut.getAnfme()){
            throw new CoolException("拣货重量大于拣货单所需拣货重量,请重新分配拣货重量");
        }
        if(manPakOut.getStatus() == 1){
            throw new CoolException("该拣料单已出库");
        }
        manPakOut.setCount(manPakOut.getCount() + anfme);
        if (manPakOut.getAnfme().equals(manPakOut.getCount())){
            manPakOut.setStatus(1);
            manPakOut.setUpdateTime(new Date());
            manPakOutService.updateById(manPakOut);
        }
 
        //更新plaQty信息
        plaQty.setPakoutTime(Utils.getDateStr(new Date()));
        plaQtyService.updateById(plaQty);
 
        //更新pla明细
        pla.setWeightAnfme(pla.getWeightAnfme() - anfme);
        pla.setQtyAnfme(pla.getQtyAnfme() - anfme);
        pla.setModifyTime(new Date());
        if(pla.getWeightAnfme() <= 0){
            pla.setStatus(GlobleParameter.PLA_STATUS_4);
        }else {
            pla.setStatus(GlobleParameter.PLA_STATUS_3);
        }
        log.warn("pla出库v1,更新为待出库或则全部出库,pla:{},anfme:{}",pla,anfme);
        plaService.updateById(pla);
        SaasUtils.insertLog(1,locNo,pla.getBrand(),anfme,null,null,
                pla.getBatch(),pla.getPackageNo(),pla.getOwner(),pla.getWorkshop(),null);
 
        //更新订单明细
        Order order = orderService.selectByNo(orderNo);
        OrderDetl orderDetl = orderDetlService.selectById(plaQty.getOrderDetlId());
        if (orderDetl == null){
            throw new CoolException("订单明细不存在或者不匹配");
        }
        orderDetl.setQty(orderDetl.getQty() + anfme);
        orderDetlService.updateById(orderDetl);
 
        orderService.checkComplete(orderNo);
 
    }
 
    @Override
    public void plaPackOutWithoutOrder(String brand, String batch, Integer packageNo, Double anfme) {
        Pla pla = plaService.selectByBatchAndPackageNo(batch, packageNo,brand);
        if(Cools.isEmpty(pla)){
            throw new CoolException("当前物料明细不存在");
        }
        if (!pla.getStatus().equals(GlobleParameter.PLA_STATUS_1) && !pla.getStatus().equals(GlobleParameter.PLA_STATUS_3)){
            throw new CoolException("当前物料状态无法出库");
        }
        if(anfme > pla.getWeightAnfme()){
            throw new CoolException("当前物料剩余库存不足");
        }
 
        log.warn("无订单拣货出库,更新为待出库或则全部出库,pla:{},anfme:{}",pla,anfme);
        pla.setWeightAnfme(pla.getWeightAnfme() - anfme);
        if(pla.getWeightAnfme() > 0){
            pla.setStatus(GlobleParameter.PLA_STATUS_3);
        }else {
            pla.setStatus(GlobleParameter.PLA_STATUS_4);
        }
 
        plaService.updateById(pla);
 
        SaasUtils.insertLog(1,pla.getLocNo(),pla.getBrand(),anfme,null,null,pla.getBatch(),pla.getPackageNo(),pla.getOwner(),pla.getWorkshop(),null);
 
    }
 
    @Override
    public List<ManPakOut> getManPakoutByOrderNo(String orderNo) {
        return manPakOutService.selectList(new EntityWrapper<ManPakOut>().eq("doc_num",orderNo).eq("status",0));
    }
 
    public void replacePla(Pla plaNew, ManPakOut manPakOut, PlaQty plaQty){
        //还原旧物料状态
        Pla plaOld = plaService.selectByBatchAndPackageNo(manPakOut.getBatch(), Integer.parseInt(manPakOut.getBarcode()), manPakOut.getMaktx());
 
        //更新新物料信息
        plaNew.setQtyAnfme(plaOld.getQtyAnfme());
        plaNew.setStatus(plaOld.getStatus());
        plaService.updateById(plaNew);
 
        plaOld.setStatus(GlobleParameter.PLA_STATUS_1);
        plaOld.setQtyAnfme(0.0);
 
        plaService.updateById(plaOld);
 
        //更新plaQty信息
        plaQty.setBatch(plaNew.getBatch());
        plaQty.setBrand(plaNew.getBrand());
        plaQty.setPackageNo(plaNew.getPackageNo());
 
        plaQtyService.updateById(plaQty);
 
        //更新拣货单信息
        manPakOut.setMaktx(plaNew.getBrand());
        manPakOut.setBatch(plaNew.getBatch());
        manPakOut.setBarcode(plaNew.getPackageNo() + "");
        manPakOut.setUpdateTime(new Date());
 
        manPakOutService.updateById(manPakOut);
 
    }
 
    @Override
    public List<Pla> pdaSelectInfmt(String barcode) {
        EntityWrapper<Pla> plaEntityWrapper = new EntityWrapper<>();
        String[] split = barcode.split(";");
        if (split.length == 4){
            plaEntityWrapper.eq("brand",split[0]);
            plaEntityWrapper.eq("batch",split[1]);
            plaEntityWrapper.eq("package_no",split[2]);
            plaEntityWrapper.eq("line",split[3]);
        }else if (split.length == 1){
            Node node = nodeService.selectOne(new EntityWrapper<Node>().eq("name", barcode));
            if (Cools.isEmpty(node)){
                throw new CoolException("请检查输入的库位码");
            }
            plaEntityWrapper.eq("loc_no",barcode);
            plaEntityWrapper.notLike("status","全部出库");
        }else {
            throw new CoolException("条码信息有误!");
        }
        List<Pla> plaList = plaService.selectList(plaEntityWrapper);
        if (Cools.isEmpty(plaList)){
            throw new CoolException("未查询到库存物料信息");
        }
        return plaList;
    }
 
    public boolean checkMaximum(String locNo, int packageNum){
 
        Node node = nodeService.selectByUuid(locNo);
        Node nodeParent = nodeService.selectById(node.getParentId());
 
        if(Cools.isEmpty(nodeParent.getMajor())){
            return true;
        }
 
        //当前库存包数
        int count = plaService.selectCount(new EntityWrapper<Pla>().eq("loc_no", locNo)
                .notIn("status", "全部出库"));
 
        if(nodeParent.getMajor() < count + packageNum){
            return false;
        }
 
        return true;
    }
}