bklLiudl
2025-04-02 1bbbbc8bb49411b544626996a1370788142300e0
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
using Model.InterFaceModel;
using Model.ModelDto.BllTaskDto;
using Model.ModelVm.BllTaskVm;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility.Tools;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.BllSoEntity;
using WMS.Entity.BllTaskEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.LogEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllTaskServer;
 
namespace WMS.BLL.BllTaskServer
{
    public class BllTaskSyncServer : DbHelper<BllTaskSync>, IBllTaskSyncServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public BllTaskSyncServer() : base(Db)
        {
        }
        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<BllTaskSyncDto> GetTaskSyncList(TaskSyncVm model, out int count)
        {
            try
            {
                var item = Expressionable.Create<BllTaskSync>()
                    .AndIF(!string.IsNullOrWhiteSpace(model.taskNo), it => it.TaskNo.Contains(model.taskNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(model.type), it => it.Type == model.type)
                    .AndIF(!string.IsNullOrWhiteSpace(model.status), it => it.Status == model.status)
                    .AndIF(!string.IsNullOrWhiteSpace(model.palletNo), it => it.PalletNo.Contains(model.palletNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(model.startTime), it => it.CreateTime >= Convert.ToDateTime(model.startTime))
                    .AndIF(!string.IsNullOrWhiteSpace(model.endTime), it => it.CreateTime <= Convert.ToDateTime(model.endTime).AddDays(1))
                    .ToExpression();//注意 这一句 不能少
                var total = 0;
                var list = GetAllWhereAsync(item)
                    .LeftJoin<SysUserInfor>((it, users) => it.UpdateUser == users.Id)
                    .Select((it, users) => new BllTaskSyncDto()
                    {
                        Id = it.Id,
                        TaskNo = it.TaskNo,
                        Type = it.Type,
                        Status = it.Status,
                        StartLocat = it.StartLocat,
                        EndLocat = it.EndLocat,
                        PalletNo = it.PalletNo,
                        PalletType=it.PalletType,
                        FinishDate = it.FinishDate == null ? "" : Convert.ToDateTime(it.FinishDate).ToString("yyyy-MM-dd HH:mm:ss"),
                        WCSName = it.WCSName,
                        CreateTime = it.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UpdateTime = it.UpdateTime == null ? "" : Convert.ToDateTime(it.UpdateTime).ToString("yyyy-MM-dd HH:mm:ss"),
                        UpdateUserName = users.RealName
                    })
                    .OrderByDescending(it => it.CreateTime)
                    .ToOffsetPage(model.Page, model.Limit, ref total);
 
                count = total;
                return list.OrderByDescending(m => m.CreateTime).ToList();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 接收同步任务信息
        /// </summary>
        /// <param name="model"></param>
        public void ReceiveWCSTaskSync(TaskSyncInfoVm model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.TaskNo))
                {
                    throw new Exception("请输入任务号");
                }
                if (string.IsNullOrEmpty(model.StartLocat))
                {
                    throw new Exception("请输入起始位置");
                }
                if (string.IsNullOrEmpty(model.EndLocat))
                {
                    throw new Exception("请输入目标位置");
                }
                if (string.IsNullOrEmpty(model.PalletNo))
                {
                    throw new Exception("请输入托盘条码");
                }
                //开启事务
                Db.BeginTran();
                var _dateNow = DateTime.Now;
 
                var entry = Db.Queryable<BllTaskSync>().First(w => w.IsDel == "0" && w.TaskNo == model.TaskNo);
                if (entry != null)
                {
                    throw new Exception($"任务号为:{model.TaskNo}的任务已接收,请勿重复推送!");
                }
                entry = new BllTaskSync();
                entry.TaskNo = model.TaskNo;
                entry.Type = model.Type;
                entry.Status = "0";
                entry.StartLocat = model.StartLocat;
                entry.EndLocat = model.EndLocat;
                entry.PalletNo = model.PalletNo;
                entry.PalletType = model.PalletType;
                entry.FinishDate = model.FinishDate;
                entry.WCSName = model.WCSName;
                //添加任务信息
                Db.Insertable(entry).ExecuteCommand();
                
                if (model.Type == "0" || model.Type == "2")//入库任务、移库任务
                {
                    //目标储位
                    var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.EndLocat);
                    if (endLocatInfo == null)
                    {
                        throw new Exception($"目标储位信息不存在!");
                    }
                    endLocatInfo.Flag = "1";//屏蔽
                    //修改目标储位的储位标志为屏蔽
                    Db.Updateable(endLocatInfo).ExecuteCommand();
                }
                if (model.Type == "1" || model.Type == "2")//出库任务、移库任务
                {
                    //起始储位
                    var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == model.StartLocat);
                    if (startLocatInfo == null)
                    {
                        throw new Exception($"起始储位信息不存在!");
                    }
                    startLocatInfo.Flag = "1";//屏蔽
                    //修改目标储位的储位标志为屏蔽
                    Db.Updateable(startLocatInfo).ExecuteCommand();
                }
                if (model.Type == "0" && model.BoxInfoList.Count > 0)//入库任务
                {
                    //添加托盘绑定表
                    var bind = new BllPalletBind
                    {
                        ASNNo = "",
                        ASNDetailNo = 0,
                        TaskNo = model.TaskNo, //任务号
                        PalletNo = model.PalletNo,
                        PalletNo2 = "",
                        PalletNo3 = "",
                        Qty = 0,
                        FullQty = 0,
                        Status = "2", //入库完成
                        Type = model.PalletType, //0 物料托 1 空托
                        LotNo = model.LotNo,
                        LotText = model.LotText,
                        SupplierLot = model.SupplierLot,
                        InspectMark = "0", //0 否 1 是
                        BitPalletMark = "0",
                        IsBale = "0",
                        IsBelt = "0",
                        CompleteTime=model.FinishDate,//完成时间
                        CreateTime = _dateNow
                    };
                    var bindId = Db.Insertable(bind).ExecuteReturnIdentity();
                    //箱支信息
                    var bllBox = Db.Queryable<BllBoxInfo>().Where(w => w.IsDel == "0");
                    var boxList = new List<BllBoxInfo>();
                    foreach (var item in model.BoxInfoList)
                    {
                        var boxEntry = bllBox.First(w => w.BoxNo == item.BoxNo && w.BoxNo3 == item.BoxNo3);
                        if (boxEntry != null)
                        {
                            throw new Exception($"起始储位信息不存在!");
                        }
                        var boxModel = new BllBoxInfo()
                        {
                            ASNNo = "",
                            ASNDetailNo = null,
                            BindNo = bindId,
                            BoxNo = item.BoxNo,
                            BoxNo2 = item.BoxNo,
                            BoxNo3 = item.BoxNo3,
                            PalletNo = model.PalletNo,
                            Qty = item.Qty,
                            FullQty = item.FullQty,
                            Status = "1",//已组托
                            SkuNo = model.SkuNo,
                            SkuName = model.SkuName,
                            LotNo = model.LotNo,
                            Standard = model.Standard,
                            PackageStandard = model.PackageStandard,
                            SupplierLot = model.SupplierLot,
                            InspectMark = item.InspectMark,
                            BitBoxMark = item.BitBoxMark,
                            InspectStatus = "0",//质检状态
 
                            ProductionTime = item.ProductionTime,
                            StoreTime = item.StoreTime,
                            ExpirationTime = item.ExpirationTime,
 
                            QtyCount = item.QtyCount,
                            QtyOrd = item.QtyOrd,
 
                            Origin = "赋码",
                            CreateTime = _dateNow,
                        };
                        boxList.Add(boxModel);
                    }
                    //添加箱支信息
                    Db.Insertable(boxList).ExecuteCommand();
                }
                //提交事务
                Db.CommitTran();
            }
            catch (Exception e)
            {
                //回滚事务
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 处理WCS同步过来的任务
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="orderNo"></param>
        public void HandleTaskSync(string taskNo, string orderNo,string url, int userId)
        {
            try
            {
                #region 条件判断
                if (string.IsNullOrEmpty(taskNo))
                {
                    throw new Exception("请选择要处理的任务");
                }
                //任务信息
                var task = Db.Queryable<BllTaskSync>().First(w => w.TaskNo == taskNo);
                if (task == null)
                {
                    throw new Exception("选择的任务不存在");
                }
                //任务状态,0:未同步 1:已同步 2: 无需同步
                if (task.Status != "0")
                {
                    throw new Exception("该任务已处理,无需再操作");
                }
                if ((task.Type == "0" || task.Type == "1") && task.PalletType == "0")//入库任务、出库任务
                {
                    if (string.IsNullOrEmpty(orderNo))
                    {
                        throw new Exception("请选择单据号");
                    }
                }
                var endLocatInfo = new SysStorageLocat();//目标储位
                if (task.Type == "0" || task.Type == "2")//入库任务,移库任务
                {
                    endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == task.EndLocat);
                    if (endLocatInfo == null)
                    {
                        throw new Exception($"目标储位信息不存在!");
                    }
                    if (endLocatInfo.Status != "0")
                    {
                        throw new Exception($"目标储位不是空储位!");
                    }
                }
                var startLocatInfo = new SysStorageLocat();//起始储位
                if (task.Type == "1" || task.Type == "2")//出库任务,移库任务
                {
                    startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == task.StartLocat);
                    if (startLocatInfo == null)
                    {
                        throw new Exception($"起始储位信息不存在!");
                    }
                    if (startLocatInfo.Status != "1")
                    {
                        throw new Exception($"起始储位上没有物品!");
                    }
                }
                #endregion
 
                //开启事务
                Db.BeginTran();
                var _dateNow = DateTime.Now;
                //日志记录
                var exTask = new LogTask
                {
                    TaskNo = taskNo,
                    Sender = "WCS",
                    Receiver = "WMS",
                    IsSuccess = 1, //是否下发成功 0失败 1成功
                    SendDate = task.FinishDate,  //发送时间
                    BackDate = task.FinishDate,  //返回时间
                    StartRoadway = "",            // 起始巷道
                    StartLocat = "",//起始位置
                    EndLocat = "",//目标位置
                    EndRoadway = "",  // 目标巷道
                    PalletNo = task.PalletNo,//托盘码
                    IsSend = 0,//是否可再次下发
                    IsCancel = 0,//是否可取消
                    IsFinish = 0,//是否可完成
                    Type = "",//任务类型 0 入库任务 1 出库任务  2 移库任务
                    Status = "2",//任务状态0:等待执行1正在执行2执行完成
                    OrderType = "",//0 入库单 1 出库单  2 盘点单  3 移库单
                    FinishDate = task.FinishDate,//完成时间
                    Msg = "", //关键信息
                };
 
                //任务类型,0:未同步 1:已同步 2: 无需同步
                switch (task.Type)
                {
                    case "0"://入库任务
                        #region 入库任务处理逻辑
                        //托盘绑定信息
                        var bindInfo = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.TaskNo == taskNo);
                        if (bindInfo == null)//非产品入库(含空托),将托盘出库,重新组盘入库
                        {
                            string outMode = task.StartLocat;//出库口
                            var logTaskNo = new Common().GetMaxNo("TK");
                            var logTaskEntry = new LogTask    //入库任务
                            {
                                TaskNo = logTaskNo,
                                Sender = "WMS",
                                Receiver = "WCS",
                                IsSuccess = 1, //是否下发成功 0失败 1成功
                                SendDate = DateTime.Now,  //发送时间
                                BackDate = DateTime.Now,  //返回时间
                                StartRoadway = endLocatInfo.RoadwayNo,            // 起始巷道
                                StartLocat = endLocatInfo.LocatNo,//起始位置
                                EndLocat = startLocatInfo.LocatNo,//目标位置
                                EndRoadway = startLocatInfo.RoadwayNo,  // 目标巷道
                                PalletNo = task.PalletNo,//托盘码
                                IsSend = 1,//是否可再次下发
                                IsCancel = 1,//是否可取消
                                IsFinish = 1,//是否可完成
                                Type = "1",//任务类型 0 入库任务 1 出库任务  2 移库任务
                                Status = "1",//任务状态0:等待执行1正在执行2执行完成
                                OrderType = "1",//0 入库单 1 出库单  2 盘点单  3 移库单
                                Msg = $"{task.EndLocat}的出库任务", //关键信息
                            };
                            Db.Insertable(logTaskEntry).ExecuteCommand();
                            //出库数据的集合 
                            var outDtoList = new List<OutCommandDto>();
                            outDtoList.Add(new OutCommandDto()
                            {
                                PalletNo = task.PalletNo,//托盘号
                                StartLocate = task.StartLocat, // 起始位置
                                StartRoadway = startLocatInfo.RoadwayNo,
                                EndLocate = outMode, // 目标位置 
                                TaskNo = exTask.TaskNo, // 任务号
                                TaskType = "1",// 任务类型 (出库)
                                OutMode = outMode,  //目标地址
                                Order = 1
                            });
                            //修改储位状态
                            endLocatInfo.Status = "3"; //要出库的储位改变状态 正在出库
                            endLocatInfo.Flag = "0";
                            Db.Updateable(endLocatInfo).ExecuteCommand();
                            if (outDtoList.Count > 0)
                            {
                                // 正式运行程序放开
                                /*var list2 = outDtoList.Select(m => m.TaskNo).ToList();
                                var jsonData = JsonConvert.SerializeObject(outDtoList);
                                string response = "";
 
                                try
                                {
                                    var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                                    response = HttpHelper.DoPost(url, jsonData, "下发给WCS出库命令", "WCS");
                                    var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
 
                                    ////解析返回数据 
                                    var wcsModel = JsonConvert.DeserializeObject<WcsModel>(response);
                                    if (wcsModel.StatusCode == 0)
                                    {
                                        //更改任务的发送返回时间//
                                        new TaskServer().EditTaskIssueOk(list2, time1, time2);
                                    }
                                    if (wcsModel.StatusCode == -1)
                                    {
                                        new TaskServer().EditTaskIssueNo(list2, time1, time2, wcsModel.Msg);
                                        throw new Exception(wcsModel.Msg);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw new Exception(ex.Message);
                                }*/
                            }
                            //维护日志信息
                            exTask.StartRoadway = "";//起始巷道
                            exTask.StartLocat = "";//起始位置
                            exTask.EndRoadway = endLocatInfo.RoadwayNo;//目标巷道
                            exTask.EndLocat = task.EndLocat;//目标位置
                            exTask.Type = "0";//入库任务
                            exTask.OrderType = "0";//出库单
                            exTask.Msg = $"入库口到=>>{task.EndLocat}的入库任务";//关键信息
 
                            break;
                        }
                        var boxList = Db.Queryable<BllBoxInfo>().Where(w => w.IsDel == "0" && w.BindNo == bindInfo.Id).ToList();
                        if (boxList == null || boxList.Count <= 0)
                        {
                            throw new Exception($"{taskNo}该任务没有对应的箱支信息");
                        }
                        string skuNo = boxList[0].SkuNo;//物料编码
                        string lotNo = boxList[0].LotNo;//批次号
                        string inspectStatus = boxList[0].InspectStatus;//质量状态
                        //入库单信息
                        var arrivalNotice = Db.Queryable<BllArrivalNotice>().First(m => m.IsDel == "0" && m.ASNNo == orderNo && (m.Status == "0" || m.Status == "1"));
                        if (arrivalNotice == null)
                        {
                            throw new Exception("未查询到托盘绑定的入库单信息");
                        }
                        //入库单明细信息
                        var arrivalNoticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.ASNNo == orderNo && m.SkuNo == skuNo && m.LotNo == lotNo);
                        if (arrivalNoticeDetail == null)
                        {
                            throw new Exception("未查询到托盘绑定的入库单明细信息");
                        }
 
                        //总数量
                        decimal bindQty = boxList.Sum(s => s.Qty);
                        //库存明细
                        var sd = new DataStockDetail()
                        {
                            LotNo = arrivalNoticeDetail.LotNo,
                            LotText = arrivalNoticeDetail.LotText,
                            SupplierLot = arrivalNoticeDetail.SupplierLot,
                            SkuNo = arrivalNoticeDetail.SkuNo,
                            SkuName = arrivalNoticeDetail.SkuName,
                            Standard = arrivalNoticeDetail.Standard,
                            Qty = bindQty,
                            LockQty = 0,
                            FrozenQty = 0,
                            InspectQty = 0,
                            ASNNo = arrivalNoticeDetail.ASNNo,
                            ASNDetailNo = arrivalNoticeDetail.Id,
                            WareHouseNo = endLocatInfo.WareHouseNo,
                            RoadwayNo = endLocatInfo.RoadwayNo,
                            AreaNo = endLocatInfo.AreaNo,
                            LocatNo = endLocatInfo.LocatNo,
                            PalletNo = bindInfo.PalletNo,
                            PalletNo2 = bindInfo.PalletNo2,
                            PalletNo3 = bindInfo.PalletNo3,
                            //PalletType = item.PalletType,
                            CompleteTime = task.FinishDate,
                            ProductionTime = bindInfo.ProductionTime,
                            ExpirationTime = bindInfo.ExpirationTime,
                            Status = "0",
                            InspectMark = bindInfo.InspectMark,
                            InspectStatus = inspectStatus,
                            BitPalletMark = bindInfo.BitPalletMark,
                            PackagNo = arrivalNoticeDetail.PackagNo,
                            IsBale = bindInfo.IsBale,
                            IsBelt = bindInfo.IsBelt,
                            Demo = bindInfo.Demo,
 
                            OwnerNo= arrivalNotice.CustomerNo,//供应商编码
                            OwnerName=arrivalNotice.CustomerName,//供应商名称
 
                            IsDel = "0",
                            CreateUser = 0,
                            CreateTime = (DateTime)task.FinishDate
                        };
                        //新增库存明细信息
                        var sdId = Db.Insertable(sd).ExecuteReturnIdentity();
                        //库存箱码明细
                        var dataBoxList = new List<DataBoxInfo>();
                        foreach (var demo in boxList)
                        {
                            var box = new DataBoxInfo()
                            {
                                StockDetailId = sdId,
                                BindNo = demo.BindNo,
                                BoxNo = demo.BoxNo,
                                BoxNo2 = demo.BoxNo2,
                                BoxNo3 = demo.BoxNo3,
                                PalletNo = demo.PalletNo,
                                PalletNo2 = demo.PalletNo2,
                                PalletNo3 = demo.PalletNo3,
                                Qty = demo.Qty,
                                FullQty = demo.FullQty,
                                Status = "2",
                                LotNo = demo.LotNo,
                                LotText = demo.LotText,
                                SkuNo = demo.SkuNo,
                                SkuName = demo.SkuName,
                                Standard = arrivalNoticeDetail.Standard,
                                ProductionTime = demo.ProductionTime,
                                SupplierLot = demo.SupplierLot,
                                InspectStatus = demo.InspectStatus,
                                InspectMark = demo.InspectMark,
                                BitBoxMark = demo.BitBoxMark,
 
                                CreateUser = 0,
                                CreateTime = (DateTime)task.FinishDate
                            };
                            dataBoxList.Add(box);
                            demo.ASNNo = arrivalNotice.ASNNo;//入库单号
                            demo.ASNDetailNo = arrivalNoticeDetail.Id;//入库单明细号
                            demo.Status = "2";//已组托
                        }
                        //修改箱支信息
                        Db.Updateable(boxList).ExecuteCommand();
                        //添加库存箱支明细
                        Db.Insertable(dataBoxList).ExecuteCommand();
                        #region 库存 
                        var dataStock = Db.Queryable<DataStock>().First(m => m.IsDel == "0" && m.SkuNo == arrivalNoticeDetail.SkuNo && m.LotNo == arrivalNoticeDetail.LotNo);
                        if (dataStock != null)
                        {
                            dataStock.Qty += bindQty;
                            Db.Updateable(dataStock).ExecuteCommand();
                        }
                        else
                        {
                            var dataStockInfo = new DataStock()
                            {
                                SkuNo = arrivalNoticeDetail.SkuNo,
                                SkuName = arrivalNoticeDetail.SkuName,
                                Standard = arrivalNoticeDetail.Standard,
                                LotNo = arrivalNoticeDetail.LotNo,
                                LotText = arrivalNoticeDetail.LotText,
                                Qty = bindQty,
                                LockQty = 0,
                                FrozenQty = 0,
                                IsSampling = bindInfo.InspectMark,
                                IsDel = "0",
                                CreateUser = userId,
                                CreateTime = (DateTime)task.FinishDate,
 
                                OwnerNo=arrivalNotice.CustomerNo,
                                OwnerName=arrivalNotice.CustomerName
                            };
                            Db.Insertable(dataStockInfo).ExecuteCommand();
                        }
                        #endregion
                        //修改托盘绑定信息
                        bindInfo.ASNNo = orderNo;
                        bindInfo.ASNDetailNo = arrivalNoticeDetail.Id;
                        bindInfo.Qty = bindQty;
                        bindInfo.WareHouseNo = endLocatInfo.WareHouseNo;
                        bindInfo.RoadwayNo = endLocatInfo.RoadwayNo;
                        bindInfo.LocatNo = endLocatInfo.LocatNo;
                        Db.Updateable(bindInfo).ExecuteCommand();
                        #region 入库单 及 明细
                        //入库单明细
                        arrivalNoticeDetail.FactQty += bindQty;//已组数量
                        arrivalNoticeDetail.CompleteQty += bindQty;//完成数量
                        if (arrivalNoticeDetail.CompleteQty >= arrivalNoticeDetail.Qty)
                        {
                            arrivalNoticeDetail.Status = "2";
                            arrivalNoticeDetail.CompleteTime = task.FinishDate;
                        }
                        arrivalNoticeDetail.UpdateTime = _dateNow;
                        arrivalNoticeDetail.UpdateUser = userId;
                        Db.Updateable(arrivalNoticeDetail).ExecuteCommand();
                        //入库单
                        var asnDetailNum = Db.Queryable<BllArrivalNoticeDetail>().Count(m => m.IsDel == "0" && m.ASNNo == arrivalNoticeDetail.ASNNo && m.Status != "2");
                        if (asnDetailNum == 0)
                        {
                            arrivalNotice.Status = "2";
                            arrivalNotice.CompleteTime = task.FinishDate;
                            arrivalNotice.UpdateTime = _dateNow;
                            arrivalNotice.UpdateUser = userId;
                            Db.Updateable(arrivalNotice).ExecuteCommand();
                        }
                        #endregion
 
                        //改变托盘状态为:使用中
                        var palletInfo = Db.Queryable<SysPallets>().First(m => m.PalletNo == task.PalletNo && m.IsDel == "0");
                        if (palletInfo == null)
                        {
                            throw new Exception("未在托盘表中查询到托盘信息");
                        }
                        palletInfo.Status = "1";
                        palletInfo.UpdateTime = _dateNow;
                        palletInfo.UpdateUser = userId;
                        Db.Updateable(palletInfo).ExecuteCommand();
 
                        //更改目标储位状态
                        endLocatInfo.Status = "1";//储位状态,有物品
                        endLocatInfo.Flag = "0";//储位标识,正常
                        endLocatInfo.UpdateTime = _dateNow;
                        endLocatInfo.UpdateUser = userId;
                        Db.Updateable(endLocatInfo).ExecuteCommand();
                        //维护日志信息
                        exTask.StartRoadway = "";//起始巷道
                        exTask.StartLocat = "";//起始位置
                        exTask.EndRoadway = endLocatInfo.RoadwayNo;//目标巷道
                        exTask.EndLocat = task.EndLocat;//目标位置
                        exTask.Type = "0";//入库任务
                        exTask.OrderType = "0";//出库单
                        exTask.Msg = $"入库口到=>>{task.EndLocat}的入库任务";//关键信息
                        #endregion
                        break;
                    
                    case "1"://出库任务
                        #region 出库任务处理逻辑
                        if (task.PalletType == "1")//空托出库
                        {
                            //托盘库存明细
                            var palletStockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == task.PalletNo && w.SkuNo == "100099");
                            if (palletStockDetail == null)
                            {
                                throw new Exception($"托盘库存明细信息不存在!");
                            }
                            //库存总表
                            var palletStock = Db.Queryable<DataStock>().First(a => a.IsDel == "0" && a.SkuNo == palletStockDetail.SkuNo);
                            if (palletStock == null)
                            {
                                throw new Exception("未查询到该托盘分配的库存信息!");
                            }
                            //修改库存总量
                            palletStock.Qty -= (decimal)palletStockDetail.Qty;
                            if (palletStock.Qty <= 0)
                            {
                                Db.Deleteable(palletStock).ExecuteCommand();
                            }
                            else
                            {
                                Db.Updateable(palletStock).ExecuteCommand();
                            }
                            //删除库存明细
                            Db.Deleteable(palletStockDetail).ExecuteCommand();
                        }
                        else
                        {
                            //托盘库存明细
                            var stockDetail = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == task.PalletNo && w.LocatNo == task.StartLocat);
                            if (stockDetail == null)
                            {
                                throw new Exception($"托盘库存明细信息不存在!");
                            }
                            //库存总表
                            var stock = Db.Queryable<DataStock>().First(a => a.IsDel == "0" && a.SkuNo == stockDetail.SkuNo && a.LotNo == stockDetail.LotNo);
                            if (stock == null)
                            {
                                throw new Exception("未查询到该托盘分配的库存信息!");
                            }
                            //出库单
                            var notice = Db.Queryable<BllExportNotice>().First(w => w.IsDel == "0" && w.SONo == orderNo && (w.Status == "1" || w.Status == "2" || w.Status == "3"));
                            if (notice == null)
                            {
                                throw new Exception($"对应的出库单不存在!");
                            }
                            //出库单明细
                            var noticeDetail = Db.Queryable<BllExportNoticeDetail>().First(w => w.IsDel == "0" && w.SkuNo == stockDetail.SkuNo && w.LotNo == stockDetail.LotNo);
                            if (noticeDetail == null)
                            {
                                throw new Exception($"对应的出库单明细不存在!");
                            }
                            //添加分配表信息
                            var allot = new BllExportAllot
                            {
                                SONo = notice.SONo,
                                WaveNo = notice.WaveNo,
                                SODetailNo = noticeDetail.Id,
                                StockId = stockDetail.Id,
                                LotNo = stockDetail.LotNo,
                                LotText = stockDetail.LotText,
                                SupplierLot = stockDetail.SupplierLot,
                                SkuNo = stockDetail.SkuNo,
                                SkuName = stockDetail.SkuName,
                                Standard = stockDetail.Standard,
                                PalletNo = stockDetail.PalletNo,
                                IsBale = "0", //是否裹包
                                IsBelt = "0", //是否打带
 
                                //BoxexQty = s.Qty,
                                Qty = (decimal)stockDetail.Qty,
                                CompleteQty = 0,
                                Status = "5",//已完成
                                LogisticsId = 0,
                                IsAdvance = "0",
                                OutMode = task.EndLocat,//出库口
 
                                CreateUser = userId,
                                CreateTime = _dateNow
                            };
 
                            //库存箱支信息
                            var boxInfo = Db.Queryable<DataBoxInfo>().Where(w => w.IsDel == "0" && w.StockDetailId == stockDetail.Id && w.PalletNo == task.PalletNo).ToList();
                            if (boxInfo == null || boxInfo.Count <= 0)
                            {
                                throw new Exception($"该托盘上没有可拣货的箱子!");
                            }
                            decimal pickQty = 0;//拣货的数量
                            var comList = new List<BllCompleteDetail>();
                            foreach (var item in boxInfo)
                            {
                                //添加拣货明细
                                var completeDetail = new BllCompleteDetail()
                                {
                                    SONo = notice.SONo,
                                    SODetailNo = noticeDetail.Id,
                                    ExportAllotId = allot.Id,
                                    StockId = allot.StockId,
                                    BoxNo = item.BoxNo,
                                    BoxNo2 = item.BoxNo2,
                                    BoxNo3 = item.BoxNo3,
 
                                    LotNo = allot.LotNo,
                                    LotText = allot.LotText,
                                    SupplierLot = allot.SupplierLot,
                                    SkuNo = allot.SkuNo,
                                    SkuName = allot.SkuName,
                                    Standard = allot.Standard,
                                    PalletNo = task.PalletNo,
                                    CompleteQty = item.Qty,
 
                                    CreateUser = userId,
                                    CreateTime = _dateNow,
                                };
                                comList.Add(completeDetail);
 
                                //删除库存箱码明细
                                Db.Deleteable(item).ExecuteCommand();
                                pickQty += item.Qty;
                            }
                            //添加拣货明细
                            Db.Insertable(comList).ExecuteCommand();
                            //添加分配表信息
                            allot.CompleteQty += pickQty;//拣货数量
                            Db.Insertable(allot).ExecuteCommand();
                            //删除库存明细
                            Db.Deleteable(stockDetail).ExecuteCommand();
                            //删除或修改库存
                            stock.Qty -= pickQty;
                            if (stock.Qty <= 0)
                            {
                                Db.Deleteable(stock).ExecuteCommand();
                            }
                            else
                            {
                                Db.Updateable(stock).ExecuteCommand();
                            }
                            //修改出库单明细
                            noticeDetail.CompleteQty += pickQty;//拣货数量
                            noticeDetail.FactQty += pickQty;//下架数量
                            Db.Updateable(noticeDetail).ExecuteCommand();
                            var num = Db.Queryable<BllExportNoticeDetail>().Count(m => m.IsDel == "0" && m.SONo == orderNo && m.Id != noticeDetail.Id && m.CompleteQty < m.Qty);
                            if (num <= 0)
                            {
                                notice.Status = "4"; //更改为执行完成
                                notice.UpdateUser = userId;
                                notice.UpdateTime = _dateNow;
                            }
                            //修改出库单信息
                            Db.Updateable(notice).ExecuteCommand();
                        }
 
                        //改变托盘状态为:未使用
                        var pallet = Db.Queryable<SysPallets>().First(m => m.PalletNo == task.PalletNo && m.IsDel == "0");
                        if (pallet == null)
                        {
                            throw new Exception("未在托盘表中查询到托盘信息");
                        }
                        pallet.Status = "0";
                        pallet.UpdateTime = _dateNow;
                        pallet.UpdateUser = userId;
                        Db.Updateable(pallet).ExecuteCommand();
                        //将起始储位状态改为:空储位
                        startLocatInfo.Status = "0";
                        startLocatInfo.Flag = "0";
                        startLocatInfo.UpdateTime = _dateNow;
                        startLocatInfo.UpdateUser = userId;
                        Db.Updateable(startLocatInfo).ExecuteCommand();
 
                        //维护日志信息
                        exTask.StartRoadway = startLocatInfo.RoadwayNo;//起始巷道
                        exTask.StartLocat = startLocatInfo.LocatNo;//起始储位
                        exTask.EndRoadway = "";//目标巷道
                        exTask.EndLocat = task.EndLocat;//目标位置
                        exTask.Type = "1";//出库任务
                        exTask.OrderType = "1";//出库单
                        exTask.Msg = $"{task.StartLocat}的出库任务";//关键信息
                        break;
                        #endregion
                    
                    case "2"://移库任务
                        #region 移库任务处理逻辑
                        //托盘库存明细
                        var stockDetail2 = Db.Queryable<DataStockDetail>().First(w => w.IsDel == "0" && w.PalletNo == task.PalletNo && w.LocatNo == task.StartLocat);
                        if (stockDetail2 == null)
                        {
                            throw new Exception($"托盘库存明细信息不存在!");
                        }
                        //修改托盘库存明细的储位地址
                        stockDetail2.LocatNo = endLocatInfo.LocatNo;//储位地址
                        stockDetail2.WareHouseNo = endLocatInfo.WareHouseNo;//所属仓库
                        stockDetail2.AreaNo = endLocatInfo.AreaNo;//所属区域
                        stockDetail2.RoadwayNo = endLocatInfo.RoadwayNo;//所属巷道
                        Db.Updateable(stockDetail2).ExecuteCommand();
 
                        //将起始储位状态改为:空储位
                        startLocatInfo.Status = "0";
                        startLocatInfo.Flag = "0";
                        startLocatInfo.UpdateTime = _dateNow;
                        startLocatInfo.UpdateUser = userId;
                        Db.Updateable(startLocatInfo).ExecuteCommand();
 
                        //将目标储位状态改为:有物品
                        endLocatInfo.Status = "1";
                        endLocatInfo.Flag = "0";
                        endLocatInfo.UpdateTime = _dateNow;
                        endLocatInfo.UpdateUser = userId;
                        Db.Updateable(endLocatInfo).ExecuteCommand();
 
                        //维护日志信息
                        exTask.StartRoadway = startLocatInfo.RoadwayNo;//起始巷道
                        exTask.StartLocat = startLocatInfo.LocatNo;//起始储位
                        exTask.EndRoadway = endLocatInfo.RoadwayNo;//目标巷道
                        exTask.EndLocat = endLocatInfo.LocatNo;//目标储位
                        exTask.Type = "2";//移库任务
                        exTask.OrderType = "3";//移库单
                        exTask.Msg = string.Format("移库任务:{0}=>>{1}", task.StartLocat, task.EndLocat);//关键信息
                        break;
                        #endregion
                }
                //修改任务同步状态
                task.Status = "1";//已同步
                task.UpdateTime = _dateNow;
                task.UpdateUser = userId;
                Db.Updateable(task).ExecuteCommand();
 
                //添加日志记录
                Db.Insertable(exTask).ExecuteCommand();
 
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "任务同步", taskNo, "处理", $"处理了任务号为{taskNo}的任务信息", userId);
                //提交事务
                Db.CommitTran();
            }
            catch (Exception e)
            {
                //回滚事务
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 处理WCS同步过来的任务-无需处理
        /// </summary>
        /// <param name="taskNo"></param>
        /// <param name="userId"></param>
        public void NoHandleTaskSync(string taskNo, int userId)
        {
            try
            {
                if (string.IsNullOrEmpty(taskNo))
                {
                    throw new Exception("请选择要处理的任务");
                }
                //任务信息
                var task = Db.Queryable<BllTaskSync>().First(w => w.TaskNo == taskNo);
                if (task == null)
                {
                    throw new Exception("选择的任务不存在");
                }
                //任务状态,0:未同步 1:已同步 2: 无需同步
                if (task.Status != "0")
                {
                    throw new Exception("该任务已处理,无需再操作");
                }
                var _dateNow = DateTime.Now;
                //开启事务
                Db.BeginTran();
                //修改任务同步状态
                task.Status = "2";//无需同步
                task.UpdateTime = _dateNow;
                task.UpdateUser = userId;
                Db.Updateable(task).ExecuteCommand();
                if (task.Type == "0" || task.Type == "2")//入库任务、移库任务
                {
                    //目标储位
                    var endLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == task.EndLocat);
                    if (endLocatInfo == null)
                    {
                        throw new Exception($"目标储位信息不存在!");
                    }
                    endLocatInfo.Flag = "0";//正常
                    endLocatInfo.UpdateTime = _dateNow;
                    endLocatInfo.UpdateUser = userId;
                    //修改目标储位的储位标志为正常
                    Db.Updateable(endLocatInfo).ExecuteCommand();
                }
                if (task.Type == "1" || task.Type == "2")//出库任务、移库任务
                {
                    //起始储位
                    var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.IsDel == "0" && w.LocatNo == task.StartLocat);
                    if (startLocatInfo == null)
                    {
                        throw new Exception($"起始储位信息不存在!");
                    }
                    startLocatInfo.Flag = "0";//正常
                    startLocatInfo.UpdateTime = _dateNow;
                    startLocatInfo.UpdateUser = userId;
                    //修改目标储位的储位标志为正常
                    Db.Updateable(startLocatInfo).ExecuteCommand();
                }
                if (task.Type == "0")//入库任务
                {
                    var bindInfo = Db.Queryable<BllPalletBind>().First(m => m.IsDel == "0" && m.TaskNo == taskNo);
                    if (bindInfo != null)//非产品入库,将托盘出库,重新组盘入库
                    {
                        //删除任务对应的托盘绑定信息
                        bindInfo.IsDel = "1";
                        bindInfo.UpdateTime = _dateNow;
                        bindInfo.UpdateUser = userId;
                        Db.Updateable(bindInfo).ExecuteCommand();
                    }
                    var boxList = Db.Queryable<BllBoxInfo>().Where(w => w.IsDel == "0" && w.BindNo == bindInfo.Id).ToList();
                    foreach (var item in boxList)
                    {
                        item.IsDel = "1";
                        item.UpdateTime = _dateNow;
                        item.UpdateUser = userId;
                    }
                    //删除任务对应的箱支信息
                    Db.Updateable(boxList).ExecuteCommand();
                }
 
                //添加操作日志记录
                var k = new OperationCrServer().AddLogOperationCr("库内作业", "任务同步", taskNo, "无需处理", $"处理了任务号为{taskNo}的任务信息", userId);
                //提交事务
                Db.CommitTran();
            }
            catch (Exception e)
            { 
                //回滚事务
                Db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 获取单据列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<string> GetOrderNoList(string type)
        {
            string sqlString = string.Empty;
            try
            {
                if (type == "0")
                {
                    // 未关单的单据
                    sqlString = $"select ASNNo from BllArrivalNotice where Status in ('0','1')  and IsDel='0' order by CreateTime;";
                }
                else
                {
                    sqlString = $"select SONo from BllExportNotice where Status in ('1','2','3') and IsDel='0' order by CreateTime;";
                }
                
                var modelList = Db.Ado.SqlQuery<string>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}