huanghongfeng
4 天以前 1d8897348d578648421b024d0dc5ff3d626e05f9
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core;
using WIDESEA_DTO.Stock;
using WIDESEA_Model.Models;
using WIDESEA_Core.Helper;
using Microsoft.AspNetCore.Http;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using System.Diagnostics;
using Newtonsoft.Json;
using System.Security.Policy;
using static WIDESEA_ITaskInfoService.ITaskService;
using MailKit.Search;
using WIDESEA_Common.Log;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
 
        /// <summary>
        /// 库存数据转出库任务
        /// </summary>
        /// <param name="stockInfos"></param>
        /// <returns></returns>
        public List<Dt_Task> GetTasks(List<Dt_StockInfo> stockInfos)
        {
            List<Dt_Task> tasks = new List<Dt_Task>();
            for (int i = 0; i < stockInfos.Count; i++)
            {
                Dt_StockInfo stockInfo = stockInfos[i];
 
                if (stockInfo != null)
                {
                    Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == stockInfo.LocationCode);
                    Dt_RoadwayInfo roadwayInfo = _basicService.RoadwayInfoService.Repository.QueryFirst(x => x.RoadwayNo == locationInfo.RoadwayNo);
                    if (roadwayInfo != null)
                    {
                        Dt_Task task = new()
                        {
                            CurrentAddress = stockInfo.LocationCode,
                            Grade = 0,
                            PalletCode = stockInfo.PalletCode,
                            NextAddress = roadwayInfo.OutSCStationCode,
                            Roadway = locationInfo.RoadwayNo,
                            SourceAddress = stockInfo.LocationCode,
                            TargetAddress = roadwayInfo.OutStationCode,
                            TaskStatus = OutTaskStatusEnum.OutNew.ObjToInt(),
                            TaskType = TaskTypeEnum.Outbound.ObjToInt(),
                            Depth = locationInfo.Depth,
                            TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum))
                        };
                        tasks.Add(task);
                    }
                }
            }
            return tasks;
        }
 
        /// <summary>
        /// 出库任务数据处理
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <param name="stockSelectViews"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_OutboundOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) OutboundTaskDataHandle(int orderDetailId, List<StockSelectViewDTO> stockSelectViews)
        {
            List<Dt_Task> tasks = new List<Dt_Task>();
            Dt_OutboundOrderDetail outboundOrderDetail = _outboundService.OutboundOrderDetailService.Repository.QueryFirst(x => x.Id == orderDetailId);
 
            if (outboundOrderDetail == null)
            {
                throw new Exception("未找到出库单明细信息");
            }
 
            if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity)
            {
                throw new Exception("选择数量超出单据数量");
            }
            List<Dt_StockInfo>? stockInfos = null;
            Dt_OutboundOrderDetail? orderDetail = null;
            List<Dt_OutStockLockInfo>? outStockLockInfos = null;
            List<Dt_LocationInfo>? locationInfos = null;
            if (outboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
            {
                (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = _outboundService.OutboundOrderDetailService.AssignStockOutbound(outboundOrderDetail, stockSelectViews);
                if (result.Item1 != null && result.Item1.Count > 0)
                {
                    tasks = GetTasks(result.Item1);
                    result.Item2.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                    result.Item3.ForEach(x =>
                    {
                        x.Status = OutStockStatus.出库中.ObjToInt();
                    });
 
                    stockInfos = result.Item1;
                    orderDetail = result.Item2;
                    outStockLockInfos = result.Item3;
                    locationInfos = result.Item4;
                }
                else
                {
                    throw new Exception("无库存");
                }
            }
            else
            {
                List<Dt_OutStockLockInfo> stockLockInfos = _outboundService.OutboundStockLockInfoService.GetByOrderDetailId(outboundOrderDetail.OrderId);
                if (stockLockInfos != null && stockLockInfos.Count > 0)
                {
                    List<Dt_StockInfo> stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockLockInfos.Select(x => x.PalletCode).Distinct().ToList());
                    tasks = GetTasks(stocks);
                }
            }
 
            return (tasks, stockInfos, orderDetail == null ? null : new List<Dt_OutboundOrderDetail> { orderDetail }, outStockLockInfos, locationInfos);
        }
 
        /// <summary>
        /// 生成出库任务
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <param name="stockSelectViews"></param>
        /// <returns></returns>
        public WebResponseContent GenerateOutboundTask(int orderDetailId, List<StockSelectViewDTO> stockSelectViews)
        {
            try
            {
                (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_OutboundOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) result = OutboundTaskDataHandle(orderDetailId, stockSelectViews);
 
                WebResponseContent content = GenerateOutboundTaskDataUpdate(result.Item1, result.Item2, result.Item3, result.Item4, result.Item5);
 
                return content;
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 生成出库任务后数据更新到数据库
        /// </summary>
        /// <param name="tasks"></param>
        /// <param name="stockInfos"></param>
        /// <param name="outboundOrderDetails"></param>
        /// <param name="outStockLockInfos"></param>
        /// <param name="locationInfos"></param>
        /// <returns></returns>
        public WebResponseContent GenerateOutboundTaskDataUpdate(List<Dt_Task> tasks, List<Dt_StockInfo>? stockInfos = null, List<Dt_OutboundOrderDetail>? outboundOrderDetails = null, List<Dt_OutStockLockInfo>? outStockLockInfos = null, List<Dt_LocationInfo>? locationInfos = null)
        {
            try
            {
                WebResponseContent content = new WebResponseContent();
                _unitOfWorkManage.BeginTran();
                //判断移库
                content = RelocationTasks(tasks.OrderBy(x => x.Depth).ToList());
                if (content.Status)
                {
                    _unitOfWorkManage.CommitTran();
                }
                else
                {
                    _unitOfWorkManage.RollbackTran();
                    return content;
                }
                //BaseDal.AddData(tasks);
                if (stockInfos != null && outboundOrderDetails != null && outStockLockInfos != null && locationInfos != null)
                {
                    content = _outboundService.OutboundOrderDetailService.LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos, tasks: tasks);
 
                    if (content.Status)
                    {
                        _unitOfWorkManage.CommitTran();
                    }
                    else
                    {
                        _unitOfWorkManage.RollbackTran();
                    }
                    return content;
                }
                else if (outboundOrderDetails != null && outboundOrderDetails.Count > 0)
                {
                    outboundOrderDetails.ForEach(x =>
                    {
                        x.OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt();
                    });
 
                    _outboundService.OutboundOrderDetailService.Repository.UpdateData(outboundOrderDetails);
                }
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
 
        }
        public WebResponseContent RelocationTasks(List<Dt_Task> task)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                _unitOfWorkManage.BeginTran();
                for (int i = 0; i < task.Count; i++)
                {
                    Dt_LocationInfo location = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task[i].SourceAddress && x.RoadwayNo == task[i].Roadway);
                    if (location != null)
                    {
                        //(Dt_LocationInfo?, int?) result = _basicService.LocationInfoService.isDepth(location);
                        (Dt_LocationInfo?, int?) result = isDepth(location);
                        if (result.Item1 != null && result.Item2 != LocationStatusEnum.Lock.ObjToInt() && result.Item2 != LocationStatusEnum.PalletLock.ObjToInt() && result.Item2 != LocationStatusEnum.Free.ObjToInt())
                        {
                            int sum = 0;
                            for (int j = 0; j < task.Count; j++)
                            {
                                if (result.Item1.LocationCode == task[j].SourceAddress)
                                {
                                    sum++;
                                }
                            }
                            if (sum == 0)
                            {
                                return content = RelocationTask(task[i]);
                            }
                            else
                            {
                                BaseDal.AddData(task[i]);
                                _basicService.LocationInfoService.UpdateLocationLock(location, task[i].TaskNum, StockChangeType.Outbound.ObjToInt(), true);
                            }
                        }
                        else if (result.Item1 == null && result.Item2 == LocationStatusEnum.Free.ObjToInt())
                        {
                            BaseDal.AddData(task[i]);
                            location.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                            _basicService.LocationInfoService.UpdateData(location);
                            content = WebResponseContent.Instance.OK();
                        }
                        else if (result.Item1 != null && result.Item2 == LocationStatusEnum.Free.ObjToInt())
                        {
                            BaseDal.AddData(task[i]);
                            location.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                            _basicService.LocationInfoService.UpdateData(location);
                            _basicService.LocationInfoService.UpdateLocationLock(location, task[i].TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                            content = WebResponseContent.Instance.OK();
                        }
                        else if (result.Item1 != null && (result.Item2 == LocationStatusEnum.Lock.ObjToInt() || result.Item2 == LocationStatusEnum.PalletLock.ObjToInt()))
                        {
                            Dt_Task TaskInfo = BaseDal.QueryFirst(x => x.SourceAddress == result.Item1.LocationCode);
                            if (TaskInfo == null)
                            {
                                return content = WebResponseContent.Instance.Error("货位被锁定不可出库");
                            }
                            else
                            {
                                BaseDal.AddData(task[i]);
                                location.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                _basicService.LocationInfoService.UpdateData(location);
                                content = WebResponseContent.Instance.OK();
                            }
                        }
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.OK("任务异常");
                    }
                }
                _unitOfWorkManage.CommitTran();
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 移库任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public WebResponseContent RelocationTask(Dt_Task task)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress && x.RoadwayNo == task.Roadway);
                if (locationInfo != null)
                {
                    int beforeStatus = locationInfo.LocationStatus;
                    //(Dt_LocationInfo?,int?) Result = _basicService.LocationInfoService.isDepth(locationInfo);
                    (Dt_LocationInfo?, int?) Result = isDepth(locationInfo);
                    if (Result.Item1 != null && Result.Item2 == LocationStatusEnum.InStock.ObjToInt())
                    {
                        Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == Result.Item1.LocationCode);
                        Dt_StockInfoDetail stockInfoDetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                        if (stockInfo != null && stockInfoDetail != null)
                        {
                            (Dt_Task?, Dt_LocationInfo?) result = AddRelocationTask(Result.Item1, stockInfo, task);
                            if (result.Item1 != null && result.Item2 != null)
                            {
                                _basicService.LocationInfoService.RelocationLock(Result.Item1, result.Item2, result.Item1.TaskNum);
                                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                _basicService.LocationInfoService.UpdateData(locationInfo);
                                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Relocation.ObjToInt(), "", task.TaskNum);
                                return content = WebResponseContent.Instance.OK();
                            }
                            else
                            {
                                return content = WebResponseContent.Instance.Error("移库任务生成失败");
                            }
                        }
                        else
                        {
                            return content = WebResponseContent.Instance.Error("未找到库存信息");
                        }
                    }
                    else if (Result.Item1 != null && Result.Item2 == LocationStatusEnum.Pallet.ObjToInt())
                    {
                        Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == Result.Item1.LocationCode);
                        if (stockInfo != null)
                        {
                            (Dt_Task?, Dt_LocationInfo?) result = AddRelocationTask(Result.Item1, stockInfo, task);
                            if (result.Item1 != null && result.Item2 != null)
                            {
                                _basicService.LocationInfoService.RelocationLock(Result.Item1, result.Item2, result.Item1.TaskNum);
                                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                _basicService.LocationInfoService.UpdateData(locationInfo);
                                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Relocation.ObjToInt(), "", task.TaskNum);
                                return content = WebResponseContent.Instance.OK();
                            }
                            else
                            {
                                return content = WebResponseContent.Instance.Error("移库任务生成失败");
                            }
                        }
                        else
                        {
                            return content = WebResponseContent.Instance.Error("未找到库存信息");
                        }
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error("异常");
                    }
                }
                else
                {
                    return content = WebResponseContent.Instance.Error("任务信息异常");
                }
            }
            catch (Exception ex)
            {
                return content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
        }
 
        /// <summary>
        /// 判断巷道内移库
        /// </summary>
        /// <param name="TaskNum"></param>
        /// <param name="SourceAddress"></param>
        /// <returns></returns>
        public WebResponseContent IsRelocations(int TaskNum, string SourceAddress)
        {
            try
            {
                WebResponseContent content = new WebResponseContent();
                List<Dt_LocationInfo> loca = new List<Dt_LocationInfo>();
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == TaskNum);
                if (task == null)
                {
                    return content = WebResponseContent.Instance.Error($"未找到该任务信息,任务号:{TaskNum}");
                }
                else
                {
                    //判断是否需要移库
                    string[] targetCodes = SourceAddress.Split("-");
                    if (targetCodes[1] == "001")
                    {
                        targetCodes[1] = "002";
 
                    }
                    else if (targetCodes[1] == "004")
                    {
                        targetCodes[1] = "003";
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"货位解析失败,货位编号:{SourceAddress}");
                    }
                    targetCodes[4] = "01";
                    string LocationCode = string.Join("-", targetCodes); //组装浅库位地址
                    Dt_LocationInfo locationInfos = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode && (x.LocationStatus == (int)LocationStatusEnum.Free || x.LocationStatus == (int)LocationStatusEnum.InStock));
                    if (locationInfos == null)
                    {
                        return content = WebResponseContent.Instance.Error($"未找到该货位信息,货位编号:{locationInfos}");
                    }
                    else
                    {
                        if (locationInfos.LocationStatus == (int)LocationStatusEnum.Free) //判断浅货位是否有货
                        {
                            return content = WebResponseContent.Instance.OK();
                        }
                        else
                        {
                            Dt_StockInfo dt_StockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode && x.StockStatus== (int)StockStatusEmun.已入库);
                            if (dt_StockInfo == null)
                            {
                                return content = WebResponseContent.Instance.Error($"未找到该货位的库存信息,货位编号:{LocationCode}");
                            }
                            else
                            {
                                Dt_LocationInfo newLocation;
                                //查走货位,进行生成移库任务 
                                int Locationtype = 9;   //默认为9
                                if (dt_StockInfo.MaterialType == (int)InventoryMaterialType.成品)
                                {
                                    Locationtype = 11;
                                }
                                else if (dt_StockInfo.MaterialType == (int)InventoryMaterialType.原材料)
                                {
                                    Locationtype = 10;
                                }
                                //newLocation = _basicService.LocationInfoService.GetLocation(locationInfos.RoadwayNo,Locationtype); //拿到了移库后的货位
                                if (dt_StockInfo.MaterialType == (int)InventoryMaterialType.成品)
                                {
                                    string[] targetCodesst = dt_StockInfo.PalletCode.Split("*");
                                    Dt_InboundOrder dt_Inbound = _inboundService.InbounOrderService.Repository.QueryFirst(x => x.OrderName == targetCodesst[0]);
                                    if (dt_Inbound.Startingcolumn != 0 || dt_Inbound.Terminationcolumn != 0)
                                    {
                                        newLocation = _basicService.LocationInfoService.GetLocation2(locationInfos.RoadwayNo, Locationtype, dt_Inbound.Startingcolumn, dt_Inbound.Terminationcolumn);
                                    }
                                    else
                                    {
                                        newLocation = _basicService.LocationInfoService.GetLocation(locationInfos.RoadwayNo, Locationtype);
                                    }
 
                                }
                                else
                                {
                                    newLocation = _basicService.LocationInfoService.GetLocation(locationInfos.RoadwayNo, Locationtype);
                                }
 
 
                                //目标货位查找库位是否有货
                                Dt_StockInfo dt_StockCurren = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == newLocation.LocationCode);
                                if (dt_StockCurren != null) return content = WebResponseContent.Instance.Error($"入库失败,托盘条码:{dt_StockInfo.PalletCode},查找出的货位信息对应已有库存");
 
                                Dt_Task taskcurren = BaseDal.QueryFirst(x => x.TargetAddress == newLocation.LocationCode);
                                if (taskcurren != null) return content = WebResponseContent.Instance.Error($"入库失败,托盘条码:{dt_StockInfo.PalletCode},查找出的货位信息已有入库任务");
 
                                bool crutaskthy = _taskHtyService.CrueeTaskHty(newLocation.LocationCode);
                                if (crutaskthy) return content = WebResponseContent.Instance.Error($"入库失败,托盘条码:{dt_StockInfo.PalletCode},查找出的货位在任务历史信息中,有入库或移库信息");
 
 
                                if (newLocation != null)
                                {
                                    Dt_Task dt_Task = new()
                                    {
                                        PalletCode = dt_StockInfo.PalletCode,
                                        Roadway = locationInfos.RoadwayNo,
                                        TaskType = TaskTypeEnum.RelocationIn.ObjToInt(),
                                        TaskStatus = OutTaskStatusEnum.OutNew.ObjToInt(),
                                        SourceAddress = locationInfos.LocationCode,
                                        TargetAddress = newLocation.LocationCode,
                                        CurrentAddress = locationInfos.LocationCode,
                                        NextAddress = newLocation.LocationCode,
                                        Grade = 2,
                                        Creater = "WMS",
                                        CreateDate = DateTime.Now,
                                        TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                                        MaterialType = dt_StockInfo.MaterialType
                                    };
                                    _unitOfWorkManage.BeginTran();
                                    if (locationInfos.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                                    {
                                        locationInfos.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                        newLocation.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
 
                                    }
                                    else if (locationInfos.LocationStatus == LocationStatusEnum.Pallet.ObjToInt())
                                    {
                                        locationInfos.LocationStatus = LocationStatusEnum.PalletLock.ObjToInt();
                                        newLocation.LocationStatus = LocationStatusEnum.PalletLock.ObjToInt();
                                    }
                                    loca.Add(newLocation);
                                    loca.Add(locationInfos);
                                    _basicService.LocationInfoService.UpdateData(loca);
                                    BaseDal.AddData(dt_Task);
                                    _unitOfWorkManage.CommitTran();
                                    return content = WebResponseContent.Instance.OK(data: dt_Task);
                                }
                                else
                                {
                                    return content = WebResponseContent.Instance.Error($"未找到巷道内可移库的货位");
                                }
 
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                throw;
            }
        }
 
 
        public string ReceiveWMSTask = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTask"];
 
 
        /// <summary>
        /// 接收起点需要的空托盘进行出库
        /// </summary>
        /// <param name="SourceAddress"></param>
        /// <returns></returns>
        public WebResponseContent Empty_outbound(GenerateInv generate)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TargetAddress == generate.SourceAddress);
                if (task == null)
                {
                    string RoadwayNo = "1";
                    if (generate.SourceAddress != "R01-002-041-001-01")
                    {
                        RoadwayNo = "2";
                    }
                    Dt_LocationInfo locationInfos = _basicService.LocationInfoService.Repository.QueryFirst(x => x.RoadwayNo == RoadwayNo && x.EnableStatus != (int)EnableStatusEnum.Disable && x.Depth==1 && x.LocationStatus == LocationStatusEnum.Pallet.ObjToInt());
                    if(locationInfos == null)
                    {
                        locationInfos = _basicService.LocationInfoService.Repository.QueryFirst(x => x.RoadwayNo == RoadwayNo && x.Depth == 2 && x.EnableStatus != (int)EnableStatusEnum.Disable && x.LocationStatus == LocationStatusEnum.Pallet.ObjToInt());
                    }
 
                    if (locationInfos != null)
                    {
                        Dt_StockInfo dt_StockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == locationInfos.LocationCode);
                        if (dt_StockInfo != null && dt_StockInfo.MaterialType == (int)InventoryMaterialType.空托)
                        {
 
 
                            Dt_LocationInfo newSourceAddress;
                            newSourceAddress = _basicService.LocationInfoService.GetLocationplatform(generate.SourceAddress);
                            if (newSourceAddress != null)
                            {
                                Dt_Task dt_Task = new()
                                {
                                    PalletCode = dt_StockInfo.PalletCode,
                                    TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                                    Roadway = locationInfos.RoadwayNo,
                                    TaskType = TaskTypeEnum.PalletOutbound.ObjToInt(),
                                    TaskStatus = InTaskStatusEnum.InNew.ObjToInt(),
                                    SourceAddress = locationInfos.LocationCode,
                                    TargetAddress = newSourceAddress.LocationCode,
                                    CurrentAddress = locationInfos.LocationCode,
                                    NextAddress = newSourceAddress.LocationCode,
                                    Grade = 1,
                                    Creater = "WMS",
                                    Depth = locationInfos.Depth,
                                    CreateDate = DateTime.Now,
                                    PalletCodequantity = (int)dt_StockInfo.Materialweight,
                                    MaterialType = dt_StockInfo.MaterialType
                                };
 
                                _unitOfWorkManage.BeginTran();
                                dt_StockInfo.StockStatus = (int)StockStatusEmun.出库锁定;
                                dt_StockInfo.Remark = "等待堆垛机完成出库任务";
                                if (locationInfos.LocationStatus == LocationStatusEnum.Pallet.ObjToInt())
                                {
                                    locationInfos.LocationStatus = LocationStatusEnum.PalletLock.ObjToInt();
                                }
                                BaseDal.AddData(dt_Task);
                                _basicService.LocationInfoService.UpdateData(locationInfos);
                                _stockService.StockInfoService.Repository.UpdateData(dt_StockInfo);
                                _unitOfWorkManage.CommitTran();
 
                                return content = WebResponseContent.Instance.OK(data: dt_Task);
                            }
                            else
                            {
                                return content = WebResponseContent.Instance.Error($"未找到站台编号,编号:{generate.SourceAddress}");
                            }
                        }
                        else
                        {
                            return content = WebResponseContent.Instance.Error($"无该库位空托为库存信息,库位编号:{locationInfos.LocationCode}");
                        }
 
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"该巷道已无空托盘,巷道号:{RoadwayNo}巷道");
                    }
                }
                else
                {
                    return content = WebResponseContent.Instance.Error($"已有该站台的空托出库任务,站台编号{generate.SourceAddress}");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"出库失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
 
        /// <summary>
        /// 接收起点需要的空托盘进行出库
        /// </summary>
        /// <param name="SourceAddress"></param>
        /// <returns></returns>
        public WebResponseContent Rawmaterialout(GenerateInv3 generate)
        {
            WebResponseContent content = new WebResponseContent();
            List<Dt_StockInfo> dt_StockInfo = _stockService.StockInfoService.Repository.QueryData(x => x.PalletCode.Contains(generate.PalletCode) && x.MaterialType== (int)InventoryMaterialType.原材料).OrderBy(x=>x.CreateDate).Take(generate.outCount).ToList();
 
            if(dt_StockInfo.Count > 0)
            {
                for (int i = 0; i < dt_StockInfo.Count; i++)
                {
                    Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == dt_StockInfo[i].LocationCode);   //确认货位信息是否对上
                    if (locationinfo != null)
                    {
                        Dt_Task dt_Task = new()
                        {
                            PalletCode = dt_StockInfo[i].PalletCode,
                            TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                            Roadway = locationinfo.RoadwayNo,
                            TaskType = TaskTypeEnum.Outbound.ObjToInt(),
                            TaskStatus = InTaskStatusEnum.InNew.ObjToInt(),
                            SourceAddress = locationinfo.LocationCode,
                            TargetAddress = locationinfo.RoadwayNo == "1" ? "R01-002-041-011-01" : "R02-002-027-011-01",
                            CurrentAddress = locationinfo.LocationCode,
                            NextAddress = locationinfo.RoadwayNo == "1" ? "R01-002-041-011-01" : "R02-002-027-011-01",
                            Grade = 1,
                            Creater = "WMS",
                            Depth = locationinfo.Depth,
                            CreateDate = DateTime.Now,
                            PalletCodequantity = (int)dt_StockInfo[i].Materialweight,
                            PLCTo = generate.TargetAddress == "R01-002-041-011-01" ? 1 : 2,
                            MaterialType = dt_StockInfo[i].MaterialType
                        };
 
                        _unitOfWorkManage.BeginTran();
                        dt_StockInfo[i].StockStatus = (int)StockStatusEmun.出库锁定;
                        dt_StockInfo[i].Remark = "等待堆垛机完成出库任务";
                        if (locationinfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                        {
                            locationinfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                        }
                        BaseDal.AddData(dt_Task);
                        _basicService.LocationInfoService.UpdateData(locationinfo);
                        _stockService.StockInfoService.Repository.UpdateData(dt_StockInfo);
                        _unitOfWorkManage.CommitTran();
 
                         content = WebResponseContent.Instance.OK(data: dt_Task);
                    }
                    else
                    {
                         content = WebResponseContent.Instance.Error($"物料信息与货位信息不对,托盘号:{dt_StockInfo[i].PalletCode}");
                    }
                }
                return content;
            }
            else
            {
                return content = WebResponseContent.Instance.Error($"未找到出库的库存信息");
            }
        }
 
        /// <summary>
        /// 手动生成出库任务
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        public WebResponseContent ManualOutbound(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_StockInfo> dtstockt = new List<Dt_StockInfo>();
                List<Dt_LocationInfo> locations = new List<Dt_LocationInfo>();
                List<Dt_Task> taskdt = new List<Dt_Task>();
                List<Dt_StockInfoDetail> dtstocktdetail = new List<Dt_StockInfoDetail>();
                for (int i = 0; i < saveModel.DelKeys.Count; i++)
                {
                    Dt_StockInfo stockt = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == saveModel.DelKeys[i].ToString());
                    if (stockt.StockStatus == (int)StockStatusEmun.已入库 && (stockt.Wlstatus == (int)InventoryMaterialStatus.合格 || stockt.Wlstatus == (int)InventoryMaterialStatus.返工 || stockt.Wlstatus == (int)InventoryMaterialStatus.特采) )
                    {
                        if(stockt.MaterialType != (int)InventoryMaterialType.原材料)
                        {
                            Dt_StockInfoDetail stocktdetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockt.Id);
                            stockt.StockStatus = (int)StockStatusEmun.出库锁定;
                            if (stockt.MaterialType != (int)InventoryMaterialType.空托)
                            {
                                stocktdetail.Status = (int)StockStatusEmun.出库锁定;
                            }
                            Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == stockt.LocationCode);
                            if (locationinfo.RoadwayNo == "1")
                            {
                                if (locationinfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                                {
                                    locationinfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                }
                                else if (locationinfo.LocationStatus == LocationStatusEnum.Pallet.ObjToInt())
                                {
                                    locationinfo.LocationStatus = LocationStatusEnum.PalletLock.ObjToInt();
                                }
                                string LocationName = "R01-002-044-001-01";
                                if (stockt.MaterialType == (int)InventoryMaterialType.空托)
                                {
                                    LocationName = "R01-002-043-001-01";
                                }
                                Dt_LocationInfo newTargetAddress;
                                newTargetAddress = _basicService.LocationInfoService.GetLocationplatform(LocationName);
                                Dt_Task dt_Task = new()
                                {
                                    PalletCode = stockt.PalletCode,
                                    TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                                    Roadway = newTargetAddress.RoadwayNo,
                                    TaskType = TaskTypeEnum.Outbound.ObjToInt(),
                                    TaskStatus = InTaskStatusEnum.InNew.ObjToInt(),
                                    SourceAddress = locationinfo.LocationCode,
                                    TargetAddress = newTargetAddress.LocationCode,
                                    CurrentAddress = locationinfo.LocationCode,
                                    NextAddress = newTargetAddress.LocationCode,
                                    Grade = 1,
                                    Creater = "WMS",
                                    Depth = locationinfo.Depth,
                                    CreateDate = DateTime.Now,
                                    MaterialType= stockt.MaterialType
                                };
                                dtstockt.Add(stockt);
                                locations.Add(locationinfo);
                                taskdt.Add(dt_Task);
                                if (stockt.MaterialType != (int)InventoryMaterialType.空托)
                                {
                                    dtstocktdetail.Add(stocktdetail);
                                }
                            }
                            else
                            {
                                return content = WebResponseContent.Instance.Error($"出库失败,只可出库1巷道的托盘和成品,出库条码:{saveModel.DelKeys[i].ToString()}");
                            }
                        }
                        else
                        {
                            return content = WebResponseContent.Instance.Error($"出库失败,请不要选择原材料出库,出库条码:{saveModel.DelKeys[i].ToString()}");
                        }
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"出库失败,请选择已入库且(合格,返工,特采)的物料出库!!!,出库条码:{saveModel.DelKeys[i].ToString()}");
                    }
 
                }
                var responses = HttpHelper.Post<WebResponseContent>(ReceiveWMSTask, taskdt, "下发任务入库");
                _unitOfWorkManage.BeginTran();
                if (dtstockt.Count > 0)
                {
                    _stockService.StockInfoService.Repository.UpdateData(dtstockt);
                    _stockService.StockInfoDetailService.Repository.UpdateData(dtstocktdetail);
                    _basicService.LocationInfoService.Repository.UpdateData(locations);
                    BaseDal.AddData(taskdt);
                }
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK();
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"出库失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
 
        public WebResponseContent ManualOutbound2(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_StockInfo> dtstockt = new List<Dt_StockInfo>();
                List<Dt_LocationInfo> locations = new List<Dt_LocationInfo>();
                List<Dt_Task> taskdt = new List<Dt_Task>();
                List<Dt_StockInfoDetail> dtstocktdetail = new List<Dt_StockInfoDetail>();
 
 
                List<Dt_StockInfo> stocktData= _stockService.StockInfoService.Repository.QueryData(x=>x.StockStatus== (int)StockStatusEmun.已入库 && x.MaterialType == (int)InventoryMaterialType.原材料);
                List<Dt_LocationInfo> locationinfoData = _basicService.LocationInfoService.Repository.QueryData(x =>x.LocationStatus == LocationStatusEnum.InStock.ObjToInt());
                List<Dt_StockInfoDetail> StockInfoDetailData = _stockService.StockInfoDetailService.Repository.QueryData(x => x.Status == (int)StockStatusEmun.已入库);
 
                
                string json = saveModel.DelKeys[0].ToString();
                List<string> palletCodes = JsonConvert.DeserializeObject<List<string>>(json);
 
                foreach (var palletCode in palletCodes)
                {
                    Dt_StockInfo stockt = stocktData.FirstOrDefault(x => x.PalletCode == palletCode);
                    if (stockt !=null)
                    {
                        if (stockt.StockStatus == (int)StockStatusEmun.已入库 && (stockt.Wlstatus == (int)InventoryMaterialStatus.合格 || stockt.Wlstatus == (int)InventoryMaterialStatus.退货 || stockt.Wlstatus == (int)InventoryMaterialStatus.特采))
                        {
                            Dt_StockInfoDetail stocktdetail = StockInfoDetailData.FirstOrDefault(x => x.StockId == stockt.Id);
                            if(stocktdetail != null)
                            {
                                Dt_LocationInfo locationinfo = locationinfoData.FirstOrDefault(x => x.LocationCode == stockt.LocationCode);
                                if(locationinfo != null)
                                {
                                    stockt.StockStatus = (int)StockStatusEmun.出库锁定;
                                    if (locationinfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                                    {
                                        locationinfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                                    }
 
                                    string TargetAdd = "";
                                    if (saveModel.DelKeys[1].ToString()=="20")
                                    {
                                        TargetAdd = "R02-001-021-001-02";
                                    }else if(saveModel.DelKeys[1].ToString() == "30")
                                    {
                                        TargetAdd = "R02-001-022-001-02";
                                    }
                                    else if (saveModel.DelKeys[1].ToString() == "40")
                                    {
                                        TargetAdd = "R01-002-044-001-01";
                                    }
                                    else
                                    {
                                        TargetAdd = locationinfo.RoadwayNo == "2" ? "R02-002-027-011-01" : "R01-002-041-011-01";
                                    }
 
 
                                    Dt_Task dt_Task = new()
                                    {
                                        PalletCode = stockt.PalletCode,
                                        TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                                        Roadway = locationinfo.RoadwayNo,
                                        TaskType = TaskTypeEnum.Outbound.ObjToInt(),
                                        TaskStatus = InTaskStatusEnum.InNew.ObjToInt(),
                                        SourceAddress = locationinfo.LocationCode,
                                        TargetAddress = TargetAdd,
                                        CurrentAddress = locationinfo.LocationCode,
                                        NextAddress = TargetAdd,
                                        Grade = 1,
                                        Creater = "WMS",
                                        Depth = locationinfo.Depth,
                                        CreateDate = DateTime.Now,
                                        PLCTo = int.Parse(saveModel.DelKeys[1].ToString()),
                                        MaterialType=stockt.MaterialType
                                    };
                                    dtstockt.Add(stockt);
                                    locations.Add(locationinfo);
                                    taskdt.Add(dt_Task);
                                    dtstocktdetail.Add(stocktdetail);
                                }
                                else
                                {
                                    return content = WebResponseContent.Instance.Error($"出库失败,未找到对应的库位信息,请核对!!!,出库条码:{palletCode}");
                                }
                            }
                            else
                            {
                                return content = WebResponseContent.Instance.Error($"出库失败,未找到对应的库存详情信息,请核对!!!,出库条码:{palletCode}");
                            }
                        }
                        else
                        {
                            return content = WebResponseContent.Instance.Error($"出库失败,请选择已入库且(合格,特采,退货)的物料出库!!!,出库条码:{palletCode}");
                        }
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"出库失败,未找到对应条码的库存信息,出库条码:{palletCode}");
 
                    }
                }
                var responses = HttpHelper.Post<WebResponseContent>(ReceiveWMSTask, taskdt, "下发任务入库");
                _unitOfWorkManage.BeginTran();
                if(dtstockt.Count > 0)
                {
                    _stockService.StockInfoService.Repository.UpdateData(dtstockt);
                    _stockService.StockInfoDetailService.Repository.UpdateData(dtstocktdetail);
                    _basicService.LocationInfoService.Repository.UpdateData(locations);
                    BaseDal.AddData(taskdt);
                }
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK($"出库成功,出库总数:{palletCodes.Count},成功数量:{dtstockt.Count}");
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"出库失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
        public WebResponseContent ManualOutbound3(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_StockInfo> dtstockt = new List<Dt_StockInfo>();
 
 
                List<Dt_StockInfo> stocktData = _stockService.StockInfoService.Repository.QueryData(x => x.StockStatus == (int)StockStatusEmun.已入库);
                string json = saveModel.DelKeys[0].ToString();
                List<int> palletCodes = JsonConvert.DeserializeObject<List<int>>(json);
 
                foreach (int pallid in palletCodes)
                {
                    Dt_StockInfo stockt = stocktData.FirstOrDefault(x => x.Id == pallid);
                    if (stockt != null)
                    {
 
                        stockt.Wlstatus = int.Parse(saveModel.DelKeys[1].ToString());
                        dtstockt.Add(stockt);
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"修改失败,未找到对应的库存信息,库存编号:{pallid}");
 
                    }
                }
                _unitOfWorkManage.BeginTran();
                if (dtstockt.Count > 0)
                {
                    _stockService.StockInfoService.Repository.UpdateData(dtstockt);
                }
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK($"修改成功");
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"修改失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
        public WebResponseContent ManualOutbound4(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_StockInfo> dtstockt = new List<Dt_StockInfo>();
 
 
                List<Dt_StockInfo> stocktData = _stockService.StockInfoService.Repository.QueryData(x => x.StockStatus == (int)StockStatusEmun.已入库);
                string json = saveModel.DelKeys[0].ToString();
                List<int> palletCodes = JsonConvert.DeserializeObject<List<int>>(json);
 
                foreach (int pallid in palletCodes)
                {
                    Dt_StockInfo stockt = stocktData.FirstOrDefault(x => x.Id == pallid);
                    if (stockt != null)
                    {
 
                        stockt.Mgeneratetime = DateTime.Parse(saveModel.DelKeys[1].ToString());
                        dtstockt.Add(stockt);
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"修改失败,未找到对应的库存信息,库存编号:{pallid}");
 
                    }
                }
                _unitOfWorkManage.BeginTran();
                if (dtstockt.Count > 0)
                {
                    _stockService.StockInfoService.Repository.UpdateData(dtstockt);
                }
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK($"修改成功");
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"修改失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
        public class PalletCodeList
        {
            public string PalletCode { get; set; }
        }
 
 
        public (Dt_Task?, Dt_LocationInfo?) AddRelocationTask(Dt_LocationInfo location, Dt_StockInfo stockInfo, Dt_Task task)
        {
            Dt_LocationInfo? locationInfos = _basicService.LocationInfoService.AssignLocation(location.RoadwayNo);
            if (locationInfos != null)
            {
                Dt_Task tasks = new()
                {
                    CurrentAddress = location.LocationCode,
                    Grade = 0,
                    PalletCode = stockInfo.PalletCode,
                    NextAddress = locationInfos.LocationCode,
                    Roadway = location.RoadwayNo,
                    SourceAddress = location.LocationCode,
                    TargetAddress = locationInfos.LocationCode,
                    TaskStatus = InTaskStatusEnum.RelocationNew.ObjToInt(),
                    TaskType = TaskTypeEnum.Relocation.ObjToInt(),
                    TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum))
                };
                BaseDal.AddData(tasks);
                BaseDal.AddData(task);
                stockInfo.StockStatus = StockStatusEmun.移库锁定.ObjToInt();
                _stockService.StockInfoService.UpdateData(stockInfo);
            }
            return (task, locationInfos);
        }
        /// <summary>
        /// 生成出库任务
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent GenerateOutboundTask(int[] keys)
        {
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                List<StockSelectViewDTO> stockSelectViews = new List<StockSelectViewDTO>();
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                List<Dt_OutboundOrderDetail> outboundOrderDetails = new List<Dt_OutboundOrderDetail>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                foreach (int key in keys)
                {
 
                    (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_OutboundOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) result = OutboundTaskDataHandle(key, stockSelectViews);
                    if (result.Item2 != null && result.Item2.Count > 0)
                    {
                        stockInfos.AddRange(result.Item2);
                    }
                    if (result.Item3 != null && result.Item3.Count > 0)
                    {
                        outboundOrderDetails.AddRange(result.Item3);
                    }
                    if (result.Item4 != null && result.Item4.Count > 0)
                    {
                        outStockLockInfos.AddRange(result.Item4);
                    }
                    if (result.Item5 != null && result.Item5.Count > 0)
                    {
                        locationInfos.AddRange(result.Item5);
                    }
                    if (result.Item1 != null && result.Item1.Count > 0)
                    {
                        tasks.AddRange(result.Item1);
                    }
 
 
                }
 
                WebResponseContent content = GenerateOutboundTaskDataUpdate(tasks, stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos);
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 空托盘出库任务
        /// </summary>
        /// <param name="inTask"></param>
        /// <returns></returns>
        public WebResponseContent PalletOutboundTask(string roadwayNo, string endStation)
        {
            try
            {
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetPalletStockInfo(roadwayNo);
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error("未找到空托盘库存");
                }
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == stockInfo.LocationCode && x.RoadwayNo == roadwayNo);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error("未找到空托盘库存对应的货位信息");
                }
                Dt_RoadwayInfo roadwayInfo = _basicService.RoadwayInfoService.Repository.QueryFirst(x => x.InStationCode == endStation && x.RoadwayNo == roadwayNo);
                if (roadwayInfo == null)
                {
                    return WebResponseContent.Instance.Error("未找到终点巷道信息");
                }
                Dt_Task task = new Dt_Task()
                {
                    CurrentAddress = stockInfo.LocationCode,
                    Grade = 0,
                    NextAddress = endStation,
                    PalletCode = stockInfo.PalletCode,
                    Roadway = roadwayNo,
                    SourceAddress = stockInfo.LocationCode,
                    TargetAddress = endStation,
                    TaskStatus = OutTaskStatusEnum.OutNew.ObjToInt(),
                    TaskType = TaskTypeEnum.PalletOutbound.ObjToInt(),
                    Depth = locationInfo.Depth,
                    TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum))
 
                };
                int beforeStatus = locationInfo.LocationStatus;
                _unitOfWorkManage.BeginTran();
                stockInfo.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                BaseDal.AddData(task);
                _stockService.StockInfoService.UpdateData(stockInfo);
 
                _basicService.LocationInfoService.UpdateData(locationInfo);
 
                _basicService.LocationInfoService.UpdateLocationLock(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), "", task.TaskNum);
 
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public (Dt_LocationInfo?, int?) isDepth(Dt_LocationInfo locationInfo)
        {
            if (locationInfo.Depth == 2)
            {
                if (locationInfo.Row == 1 || locationInfo.Row == 5)
                {
                    Dt_LocationInfo dt_LocationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.Row == locationInfo.Row + 1 && x.Layer == locationInfo.Layer && x.Column == locationInfo.Column && x.RoadwayNo == locationInfo.RoadwayNo);
 
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.InStock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Free.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Lock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Lock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.PalletLock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Pallet.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Pallet.ObjToInt());
                    }
                }
                else if (locationInfo.Row == 4 || locationInfo.Row == 8)
                {
                    Dt_LocationInfo dt_LocationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.Row == locationInfo.Row + 1 && x.Layer == locationInfo.Layer && x.Column == locationInfo.Column && x.RoadwayNo == locationInfo.RoadwayNo);
 
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.InStock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Free.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Lock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Lock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.PalletLock.ObjToInt());
                    }
                    if (dt_LocationInfo != null && dt_LocationInfo.LocationStatus == LocationStatusEnum.Pallet.ObjToInt())
                    {
                        return (dt_LocationInfo, LocationStatusEnum.Pallet.ObjToInt());
                    }
                }
            }
            return (null, LocationStatusEnum.Free.ObjToInt());
        }
 
        /// <summary>
        /// 人工手动出库(删除库存)
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent ManualOutboundDeleteinventory(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_StockInfo> dtstockt = new List<Dt_StockInfo>();
                List<Dt_LocationInfo> locations = new List<Dt_LocationInfo>();
                List<Dt_StockInfoDetail> dtstocktdetail = new List<Dt_StockInfoDetail>();
 
                for (int i = 0; i < saveModel.DelKeys.Count; i++)
                {
                    Dt_StockInfo stockt = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == saveModel.DelKeys[i].ToString());
                    if (stockt.StockStatus == (int)StockStatusEmun.已入库)
                    {
                        Dt_StockInfoDetail stocktdetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockt.Id);
                        Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == stockt.LocationCode);
                        Dt_Task_Hty task_Hty = new Dt_Task_Hty()
                        {
                            TaskNum = 001,
                            PalletCode = stockt.PalletCode,
                            Roadway = locationinfo.RoadwayNo,
                            TaskType = (int)TaskTypeEnum.Outbound,
                            TaskStatus = (int)OutTaskStatusEnum.OutFinish,
                            SourceAddress = locationinfo.LocationCode,
                            TargetAddress = locationinfo.LocationCode,
                            CurrentAddress = locationinfo.LocationCode,
                            NextAddress = locationinfo.LocationCode,
                            Grade = 1,
                            Dispatchertime = DateTime.Now,
                            Creater = App.User.UserName,
                        CreateDate = DateTime.Now,
                            ModifyDate = DateTime.Now,
                            Modifier = App.User.UserName,
                            Remark = "人工出库",
                            PLCTo = 1,
                            PalletCodequantity = 1,
                            MaterialType = 1
                        };
                        _taskHtyService.AddData(task_Hty);
                        locationinfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                        dtstockt.Add(stockt);
                        locations.Add(locationinfo);
                        if (stockt.MaterialType != (int)InventoryMaterialType.空托)
                        {
                            dtstocktdetail.Add(stocktdetail);
                        }
                        WriteLog.GetLog("人工手动删除库存信息").Write($"托盘条码:{stockt.PalletCode},库位编号:{stockt.LocationCode}", $"人工出库库存");
                    }
                    else
                    {
                        return content = WebResponseContent.Instance.Error($"出库失败,该库存信息不可进行出库");
                    }
 
                }
                _unitOfWorkManage.BeginTran();
                _stockService.StockInfoService.Repository.DeleteData(dtstockt);
                _stockService.StockInfoDetailService.Repository.DeleteData(dtstocktdetail);
                _basicService.LocationInfoService.Repository.UpdateData(locations);
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK();
                return content;
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error($"手动出库信息失败,报错信息:{ex.Message}");
                throw;
            }
        }
 
        /// <summary>
        /// 任务取消
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public WebResponseContent Cancelinventory(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
            if(task != null)
            {
                if(task.TaskType== (int)TaskTypeEnum.Outbound || task.TaskType == (int)TaskTypeEnum.PalletOutbound)
                {
                    //处理出库的逻辑
                    Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                    if (locationinfo.LocationStatus == LocationStatusEnum.Lock.ObjToInt()) 
                    {
                        locationinfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                    }
                    if (locationinfo.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        locationinfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
                    }
                    _basicService.LocationInfoService.Repository.UpdateData(locationinfo);
                    Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == task.PalletCode);
                    stockInfo.StockStatus = (int)StockStatusEmun.已入库;
                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                    Dt_StockInfoDetail stocktdetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                    if(stocktdetail != null)
                    {
                        stocktdetail.Status = (int)StockStatusEmun.已入库;
                        _stockService.StockInfoDetailService.Repository.UpdateData(stocktdetail);
                    }
                    BaseDal.DeleteData(task);
                    BaseDal.DeleteAndMoveIntoHty(task, OperateType.人工删除);
                    WriteLog.GetLog("任务日志").Write($"出库任务取消成功,托盘条码:{task.PalletCode}", $"任务取消");
                    WebResponseContent webResponseContent = HttpHelper.Post<WebResponseContent>(ReceiveWCSTask, task.TaskNum, "任务删除");
                    return content = WebResponseContent.Instance.Error($"出库任务取消成功");
 
                }
                else if(task.TaskType == (int)TaskTypeEnum.Inbound || task.TaskType == (int)TaskTypeEnum.PalletInbound)
                {
                    //处理出库的逻辑
                    Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationinfo.LocationStatus == LocationStatusEnum.Lock.ObjToInt() || locationinfo.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        locationinfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                    }
                    _basicService.LocationInfoService.Repository.UpdateData(locationinfo);
                    Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == task.PalletCode);
                    _stockService.StockInfoService.Repository.DeleteData(stockInfo);
                    Dt_StockInfoDetail stocktdetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                    if (stocktdetail != null)
                    {
                        _stockService.StockInfoDetailService.Repository.DeleteData(stocktdetail);
                    }
                    BaseDal.DeleteData(task);
                    BaseDal.DeleteAndMoveIntoHty(task, OperateType.人工删除);
                    WriteLog.GetLog("任务日志").Write($"入库任务取消成功,托盘条码:{task.PalletCode}", $"任务取消");
                    WebResponseContent webResponseContent = HttpHelper.Post<WebResponseContent>(ReceiveWCSTask, task.TaskNum, "任务删除");
                    return content = WebResponseContent.Instance.Error($"入库任务取消成功");
                }
                else if (task.TaskType == (int)TaskTypeEnum.RelocationIn)    //库内移库
                {
                    //处理出库的逻辑
                    Dt_LocationInfo locationinfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationinfo.LocationStatus == LocationStatusEnum.Lock.ObjToInt() || locationinfo.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        locationinfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                    }
                    _basicService.LocationInfoService.Repository.UpdateData(locationinfo);
 
                    Dt_LocationInfo locationinfo2 = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                    if (locationinfo2.LocationStatus == LocationStatusEnum.Lock.ObjToInt())
                    {
                        locationinfo2.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                    }
                    if (locationinfo2.LocationStatus == LocationStatusEnum.PalletLock.ObjToInt())
                    {
                        locationinfo2.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
                    }
                    _basicService.LocationInfoService.Repository.UpdateData(locationinfo2);
                    Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == task.PalletCode);
                    stockInfo.StockStatus = (int)StockStatusEmun.已入库;
                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                    Dt_StockInfoDetail stocktdetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                    if (stocktdetail != null)
                    {
                        stocktdetail.Status = (int)StockStatusEmun.已入库;
                        _stockService.StockInfoDetailService.Repository.UpdateData(stocktdetail);
                    }
                    BaseDal.DeleteData(task);
                    BaseDal.DeleteAndMoveIntoHty(task, OperateType.人工删除);
                    WriteLog.GetLog("任务日志").Write($"入库任务取消成功,托盘条码:{task.PalletCode}", $"任务取消");
                    WebResponseContent webResponseContent = HttpHelper.Post<WebResponseContent>(ReceiveWCSTask, task.TaskNum, "任务删除");
                    return content = WebResponseContent.Instance.Error($"入库任务取消成功");
                }
                else
                {
                    return content = WebResponseContent.Instance.Error($"该任务的任务类型异常,取消失败");
                }
            }
            else
            {
                return content = WebResponseContent.Instance.Error($"未找到任务号");
            }
        }
    }
}