zhaowc
2025-05-20 d09a1552e45e6cfc8f83c0249cd296b8706c0dc2
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
using Model.InterFaceModel;
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.BllQualityEntity;
using WMS.Entity.BllSoEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.LogEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllTransServer;
using static Model.InterFaceModel.RCSModel;
 
namespace WMS.BLL.BllTransServer
{
    public class NoticeServer : DbHelper<BllArrivalNotice>, INoticeServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public NoticeServer() : base(Db)
        {
        }
        /// <summary>
        /// 下发入库单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ErpModel CreateAsnWork(AsnInfo model)
        {
            try
            {
                var resultModel = new ErpModel() { Success = -1, Message = "" };
                if (string.IsNullOrEmpty(model.OrderCode))
                {
                    resultModel.Message = "上游系统单号不可为空!";
                    return resultModel;
                }
                if (model.AsnDetails.Count <= 0)
                {
                    resultModel.Message = "入库单明细不可为空!";
                    return resultModel;
                }
 
                // 入库总表信息
                string asnNo = new Common().GetMaxNo("ASN");
                var asnModel = new BllArrivalNotice()
                {
                    ASNNo = asnNo,
                    Status = "0",//等待执行
                    Type = "0",//原料入库
                    Origin = string.IsNullOrEmpty(model.Origin) ? "MES" : model.Origin,
                    CustomerNo = model.Customer,
                    CustomerName = "",
                    OrderCode = model.OrderCode
                };
 
                // 入库明细表信息
                List<BllArrivalNoticeDetail> detailModels = new List<BllArrivalNoticeDetail>();
                foreach (AsnDetail asnDetailModel in model.AsnDetails)
                {
                    if (string.IsNullOrEmpty(asnDetailModel.SkuNo))
                    {
                        resultModel.Message = "物料编码不可为空!";
                        return resultModel;
                    }
                    if (string.IsNullOrEmpty(asnDetailModel.SkuName))
                    {
                        resultModel.Message = "物料名称不可为空!";
                        return resultModel;
                    }
                    if (asnDetailModel.Qty <= 0)
                    {
                        resultModel.Message = "数量应大于0!";
                        return resultModel;
                    }
                    if (string.IsNullOrEmpty(asnDetailModel.LotNo))
                    {
                        resultModel.Message = "批次号不可为空!";
                        return resultModel;
                    }
                    var detailModel = new BllArrivalNoticeDetail()
                    {
                        ASNNo = asnNo,
                        OrderDetailCode = asnDetailModel.OrderDetailCode,
                        SkuNo = asnDetailModel.SkuNo,
                        SkuName = asnDetailModel.SkuName,
                        LotNo = asnDetailModel.LotNo,
                        LotText = "",
                        Qty = (decimal)asnDetailModel.Qty,//这里是桶数量
                        FactQty = 0,
                        CompleteQty = 0,
                        SupplierLot = asnDetailModel.SupplierLot,
                        Status = "0",
                        IsSampling = "0",
                        InspectStatus = "0",
                        CreateUser = 0
                    };
                    detailModels.Add(detailModel);
                }
 
                Db.BeginTran();
                // 插入入库单总表
                Db.Insertable(asnModel).ExecuteCommand();
 
                // 插入入库单明细表
                Db.Insertable(detailModels).ExecuteCommand();
                Db.CommitTran();
 
                resultModel.Success = 0;
                resultModel.Message = "成功";
                return resultModel;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw ex;
            }
        }
 
        /// <summary>
        /// 任务完成
        /// </summary>
        /// <param name="TaskNo"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public ErpModel RCSFinishTask(string TaskNo,string Status)
        {
            try
            {
                #region 条件判断
                var resultModel = new ErpModel() { Success = -1, Message = "" };
                if (string.IsNullOrEmpty(TaskNo))
                {
                    resultModel.Message = "任务号不可为空!";
                    return resultModel;
                }
                //任务信息
                var taskInfo = Db.Queryable<LogTask>().First(w => w.TaskNo == TaskNo);
                if (taskInfo == null)
                {
                    resultModel.Message = $"任务号为:{TaskNo}的任务不存在!";
                    return resultModel;
                }
                if (taskInfo.Status != "1")
                {
                    resultModel.Message = $"任务号为:{TaskNo}的任务状态异常!";
                    return resultModel;
                }
                #endregion
                //开启事务
                Db.BeginTran();
                var comTime = DateTime.Now;
 
                if (Status == "0")
                {
                    taskInfo.Status = "3";//异常结束
                    //修改任务状态
                    Db.Updateable(taskInfo).ExecuteCommand();
 
                    resultModel.Success = 0;
                    resultModel.Message = "成功";
 
                    //提交事务
                    Db.CommitTran();
 
                    return resultModel;
                }
                switch (taskInfo.OrderType)
                {
                    #region 入库完成
                    case "0":
                        
                        //目标储位信息
                        var locatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0");
                        if (locatInfo == null)
                        {
                            resultModel.Message = "目标储位信息不存在!";
                            return resultModel;
                        }
 
                        //库存明细
                        var sd1 = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == taskInfo.PalletNo);
                        if (sd1 == null)
                        {
                            resultModel.Message = $"该桶对应的库存信息不存在,桶号:{taskInfo.PalletNo}!";
                            return resultModel;
                        }
                        else
                        {
                            sd1.Qty = 1;
                            sd1.LockQty = 0;
                            sd1.FrozenQty = 0;
                            sd1.InspectQty = 0;
                            sd1.WareHouseNo = locatInfo.WareHouseNo;//所属仓库
                            sd1.RoadwayNo = locatInfo.RoadwayNo;//所属巷道
                            sd1.AreaNo = locatInfo.AreaNo;//所属区域
                            sd1.LocatNo = locatInfo.LocatNo;//储位地址
                            sd1.CompleteTime = comTime;
                            sd1.InspectStatus = "0";//待检验
                            sd1.PalletStatus = "2";//半成品
 
                            //修改库存明细
                            Db.Updateable(sd1).ExecuteCommand();
                        }
                        locatInfo.Status = "1";//有物品
                        //更新储位状态
                        Db.Updateable(locatInfo).ExecuteCommand();
 
                        //入库单明细信息
                        var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(w => w.Id == taskInfo.NoticeDetailNo);
                        if (noticeDetail != null)
                        {
                            //入库单信息
                            var notice = Db.Queryable<BllArrivalNotice>().First(w => w.ASNNo == noticeDetail.ASNNo);
                            if (notice == null)
                            {
                                resultModel.Message = "该任务对应的入库单不存在!";
                                return resultModel;
                            }
                            if (notice.Status == "3" || notice.Status == "4")
                            {
                                resultModel.Message = "该任务对应的入库单已执行完成!";
                                return resultModel;
                            }
                            noticeDetail.CompleteQty += 1;//完成数量
                            noticeDetail.Status = "1";//0:等待执行 1:正在执行 2:执行完成
                            if (noticeDetail.CompleteQty >= noticeDetail.Qty)
                            {
                                noticeDetail.Status = "2";
                                noticeDetail.CompleteTime = comTime;
                            }
                            noticeDetail.UpdateUser = 0;
                            noticeDetail.UpdateTime = comTime;
                            //更新入库单明细
                            Db.Updateable(noticeDetail).ExecuteCommand();
 
                            notice.UpdateUser = 0;
                            notice.UpdateTime = comTime;
                            if (notice.Status == "0")
                            {
                                notice.Status = "1";
                            }
                            var asnDetailNum = Db.Queryable<BllArrivalNoticeDetail>().Count(m => m.IsDel == "0" && m.ASNNo == noticeDetail.ASNNo && m.Status != "2");
                            if (asnDetailNum == 0)
                            {
                                notice.Status = "2";
                                notice.CompleteTime = comTime;//完成时间
                            }
                            //更新入库单
                            Db.Updateable(notice).ExecuteCommand();
                        }
 
                        #region 质检请验,去掉MES
                        //var qualityRequest = Db.Queryable<BllQualityInspectionRequest>().First(m => m.IsDel == "0" && m.SkuNo == noticeDetail.SkuNo && m.LotNo == noticeDetail.LotNo && m.SupplierLot == noticeDetail.SupplierLot);
                        //if (qualityRequest == null)
                        //{
                        //    //质检请验单信息
                        //    string qcNo = new Common().GetMaxNo("QC");
                        //    qualityRequest = new BllQualityInspectionRequest();
                        //    qualityRequest.QcNo = qcNo;
                        //    qualityRequest.Status = "0";
                        //    qualityRequest.SkuNo = noticeDetail.SkuNo;
                        //    qualityRequest.SkuName = noticeDetail.SkuName;
                        //    qualityRequest.LotNo = noticeDetail.LotNo;
                        //    qualityRequest.SupplierLot = noticeDetail.SupplierLot;
                        //    qualityRequest.Qty = noticeDetail.Qty;
                        //    qualityRequest.SamplingQty = 0;
                        //    qualityRequest.ASNNo = noticeDetail.ASNNo;
                        //    qualityRequest.CreateUser = 0;
                        //    qualityRequest.CreateTime = comTime;
 
                        //    // 记录任务日志
                        //    var taskNo = new Common().GetMaxNo("TK");
                        //    var exTask = new LogTask()
                        //    {
                        //        TaskNo = taskNo,
                        //        Sender = "WMS",
                        //        Receiver = "Limes",
                        //        IsSuccess = 0,//是否下发成功 0失败 1成功
                        //        StartLocat = "",//起始位置
                        //        PalletNo = "",//托盘码
                        //        IsSend = 1,//是否可再次下发
                        //        IsCancel = 1,//是否可取消
                        //        IsFinish = 1,//是否可完成    
                        //        Status = "0",//任务状态0:等待执行1正在执行2执行完成
                        //        OrderType = "5",//0 入库单 1 出库单  2 盘点单  3 移库单 4 取样出库单 5 其他
                        //        EndLocat = "",//目标位置
                        //        Type = "3",//任务类型 0 入库任务 1 出库任务  2 移库任务
                        //        Msg = "请验任务"
                        //    };
                        //    // 插入任务日志
                        //    Db.Insertable(exTask).ExecuteCommand();
 
                        //    // 调用Limes接口发起请验
                        //    var sendModel = new SendLimesModel()
                        //    {
                        //        QcNo = qualityRequest.QcNo,
                        //        SkuNo = qualityRequest.SkuNo,
                        //        Qty = qualityRequest.Qty.ToString(),
                        //        LotNo = qualityRequest.LotNo,
                        //        SupplierLot = qualityRequest.SupplierLot,
                        //        RequestUser = "",                       // 请验人
                        //    };
                        //    var jsonData = JsonConvert.SerializeObject(sendModel);
                        //    string response = "";
                        //    try
                        //    {
                        //        var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss")
                        //        //response = HttpHelper.DoPost(url, jsonData, "上传Limes系统发起请验", "Limes");
                        //        var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss")
 
                        //        ////解析返回数据 
                        //        //var limesModel = JsonConvert.DeserializeObject<LimesModel>(response);
                        //        List<string> list1 = new List<string>();
                        //        list1.Add(taskNo);
                        //        //if (limesModel.Success == 0)
                        //        //{
                        //        //更改任务的发送返回时间//
                        //        new TaskServer().EditTaskIssueOk(list1, time1, time2);
 
                        //        // 更新请验单信息
                        //        qualityRequest.Status = "1";
                        //        qualityRequest.SamplingQty = 10;//decimal.Parse(limesModel.SamplingQty);
                        //        qualityRequest.RequestUser = 0;
                        //        qualityRequest.RequestTime = DateTime.Now;
                        //        qualityRequest.UpdateTime = DateTime.Now;
                        //        qualityRequest.UpdateUser = 0;
                        //        //添加质检请验单
                        //        Db.Insertable(qualityRequest).ExecuteCommand();
                        //        //}
                        //        //if (limesModel.Success == -1)
                        //        //{
                        //        //    new TaskServer().EditTaskIssueNo(list1, time1, time2,limesModel.Message);
                        //        //    throw new Exception(limesModel.Message);
                        //        //}
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        throw new Exception(ex.Message);
                        //    }
                        //}
                        #endregion
 
                        break;
                    #endregion
 
                    #region 出库完成
                    case "1":
 
                        //起始储位信息
                        var startLocatInfo = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0");
                        if (startLocatInfo == null)
                        {
                            resultModel.Message = "起始储位信息不存在!";
                            return resultModel;
                        }
                        //库存明细
                        var sd2 = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == taskInfo.PalletNo);
                        if (sd2 == null)
                        {
                            resultModel.Message = "库存信息不存在!";
                            return resultModel;
                        }
                        sd2.Qty = 0;
                        sd2.LockQty = 0;
                        sd2.FrozenQty = 0;
                        sd2.InspectQty = 0;
                        sd2.ASNNo = "";
                        sd2.ASNDetailNo = null;
                        sd2.WareHouseNo = "";//所属仓库
                        sd2.RoadwayNo = "";//所属巷道
                        sd2.AreaNo = "";//所属区域
                        sd2.LocatNo = "";//储位地址
                        sd2.CompleteTime = comTime;
                        //修改库存明细
                        Db.Updateable(sd2).ExecuteCommand();
 
                        startLocatInfo.Status = "0";//空储位
                        //修改起始储位状态
                        Db.Updateable(startLocatInfo).ExecuteCommand();
                        //出库单明细信息
                        var exNoticeDetail = Db.Queryable<BllExportNoticeDetail>().First(w => w.Id == taskInfo.NoticeDetailNo);
                        if (exNoticeDetail != null)
                        {
                            //出库单信息
                            var exNotice = Db.Queryable<BllExportNotice>().First(w => w.SONo == exNoticeDetail.SONo);
                            if (exNotice == null)
                            {
                                resultModel.Message = "该任务对应的出库单不存在!";
                                return resultModel;
                            }
                            if (exNotice.Status == "4" || exNotice.Status == "5")
                            {
                                resultModel.Message = "该任务对应的出库单已执行完成!";
                                return resultModel;
                            }
                            exNotice.UpdateUser = 0;
                            exNotice.UpdateTime = comTime;
                            if (exNotice.Status == "0" || exNotice.Status == "1" || exNotice.Status == "2")
                            {
                                exNotice.Status = "3";//更改为正在执行
                            }
                            var num = Db.Queryable<BllExportNoticeDetail>().Count(m => m.IsDel == "0" && m.SONo == exNoticeDetail.SONo && m.CompleteQty < m.Qty);
                            if (num <= 0)
                            {
                                exNotice.Status = "4"; //更改为执行完成                            
                            }
                            //修改出库单信息
                            Db.Updateable(exNotice).ExecuteCommand();
 
                            //修改出库单明细拣货数量
                            exNoticeDetail.CompleteQty += sd2.Qty;
                            Db.Updateable(exNoticeDetail).ExecuteCommand();
                        }                        
                        break;
                    #endregion
 
                    #region 移库完成
                    case "3":
                        //库存明细
                        var sd3 = Db.Queryable<DataStockDetail>().First(w => w.PalletNo == taskInfo.PalletNo);
                        if (sd3 == null)
                        {
                            resultModel.Message = "库存信息不存在!";
                            return resultModel;
                        }
                        if (taskInfo.Type == "0")//入库任务
                        {
                            //目标储位信息
                            var endLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0");
                            if (endLocatInfo2 == null)
                            {
                                resultModel.Message = "目标储位信息不存在!";
                                return resultModel;
                            }
                            endLocatInfo2.Status = "1";//有物品
                            //修改目标储位状态
                            Db.Updateable(endLocatInfo2).ExecuteCommand();
 
                            sd3.WareHouseNo = endLocatInfo2.WareHouseNo;//所属仓库
                            sd3.RoadwayNo = endLocatInfo2.RoadwayNo;//所属巷道
                            sd3.AreaNo = endLocatInfo2.AreaNo;//所属区域
                            sd3.LocatNo = endLocatInfo2.LocatNo;//储位地址
                            if (endLocatInfo2.AreaNo.Contains("01"))//净桶区
                            {
                                sd3.PalletStatus = "0";                               
                            }
                            else if (endLocatInfo2.AreaNo.Contains("02"))//预混区
                            {
                                sd3.PalletStatus = "1";
                            }
                            else if (endLocatInfo2.AreaNo.Contains("03"))//半成品区
                            {
                                sd3.PalletStatus = "2";
                            }
                            else if (endLocatInfo2.AreaNo.Contains("04"))//脏桶区
                            {
                                sd3.PalletStatus = "3";
                                sd3.LotNo = "";//批次
                                sd3.SkuNo = "";
                                sd3.SkuName = "";
                                sd3.InspectStatus = "0";//待检验
                            }                            
                        }
                        else if (taskInfo.Type == "1")//出库任务
                        {
                            //起始储位信息
                            var startLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0");
                            if (startLocatInfo2 == null)
                            {
                                resultModel.Message = "起始储位信息不存在!";
                                return resultModel;
                            }
                            startLocatInfo2.Status = "0";//空储位
                            //修改起始储位状态
                            Db.Updateable(startLocatInfo2).ExecuteCommand();
 
                            sd3.WareHouseNo = "";//所属仓库
                            sd3.RoadwayNo = "";//所属巷道
                            sd3.AreaNo = "";//所属区域
                            sd3.LocatNo = "";//储位地址
                        }
                        else if (taskInfo.Type == "2")//移库任务
                        {
                            //起始储位信息
                            var startLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0");
                            if (startLocatInfo2 == null)
                            {
                                resultModel.Message = "起始储位信息不存在!";
                                return resultModel;
                            }
                            startLocatInfo2.Status = "0";//空储位
                            //修改起始储位状态
                            Db.Updateable(startLocatInfo2).ExecuteCommand();
 
                            //目标储位信息
                            var endLocatInfo2 = Db.Queryable<SysStorageLocat>().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0");
                            if (endLocatInfo2 == null)
                            {
                                resultModel.Message = "目标储位信息不存在!";
                                return resultModel;
                            }
                            endLocatInfo2.Status = "1";//有物品
                            //修改目标储位状态
                            Db.Updateable(endLocatInfo2).ExecuteCommand();
 
                            sd3.WareHouseNo = endLocatInfo2.WareHouseNo;//所属仓库
                            sd3.RoadwayNo = endLocatInfo2.RoadwayNo;//所属巷道
                            sd3.AreaNo = endLocatInfo2.AreaNo;//所属区域
                            sd3.LocatNo = endLocatInfo2.LocatNo;//储位地址
                            sd3.Status = "0";
                            if (endLocatInfo2.AreaNo.Contains("01"))//洁净区
                            {
                                sd3.PalletStatus = "0";
                                sd3.Status = "0";
                            }
                            else if (endLocatInfo2.AreaNo.Contains("02"))//预混区
                            {
                                sd3.PalletStatus = "1";
                            }
                            else if (endLocatInfo2.AreaNo.Contains("03"))//半成品区
                            {
                                sd3.PalletStatus = "2";
                            }
                            else if (endLocatInfo2.AreaNo.Contains("04"))//脏桶区
                            {
                                sd3.PalletStatus = "3";
                                sd3.LotNo = "";//批次
                                sd3.SkuNo = "";
                                sd3.SkuName = "";
                                sd3.InspectStatus = "0";//待检验
                            }
                        }
                        //修改库存明细
                        Db.Updateable(sd3).ExecuteCommand();
 
                        #region 去掉MES
                        //回传桶状态给MES
                        //BackPalletStatus(taskInfo.PalletNo, sd3.PalletStatus);
                        #endregion
 
                        break;    
                    #endregion
                }
                taskInfo.Status = "2";//执行完成
                //修改任务状态
                Db.Updateable(taskInfo).ExecuteCommand();
 
                //提交事务
                Db.CommitTran();
 
                resultModel.Success = 0;
                resultModel.Message = "成功";
                return resultModel;
            }
            catch (Exception ex)
            {
                //回滚事务
                Db.RollbackTran();
                throw ex;
            }
        }
 
        /// <summary>
        /// 回传桶状态给MES
        /// </summary>
        /// <param name="PalletNo">桶号</param>
        /// <param name="Status">0:净桶 1:预混 2:半成品 3:脏桶</param>
        /// <param name="url"></param>
        public void BackPalletStatus(string PalletNo, string Status,string url="")
        {
            try
            {
                var obj = new
                {
                    PalletNo = PalletNo,
                    Status = Status
                };
                string jsonReq = JsonConvert.SerializeObject(obj);
                var response = HttpHelper.DoPost(url, jsonReq, "回传MES桶状态", "MES").ToString();
                var result = JsonConvert.DeserializeObject<ReMes>(response);//解析返回数据
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 下发出库单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public SoResInfo CreateSoWork(SoInfo model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.OrderCode))
                {
                    throw new Exception("上游系统单号不可为空!");
                }
                if (model.SoDetails.Count <= 0)
                {
                    throw new Exception("出库单明细不可为空!");
                }
                //返回信息
                SoResInfo result = new SoResInfo();
 
                var skuNos = model.SoDetails.Select(a => a.SkuNo).Distinct().ToList();
                //获取库存明细
                var stockDetailList = Db.Queryable<DataStockDetail>().Where(s => skuNos.Contains(s.SkuNo) && (s.Qty - s.FrozenQty - s.LockQty) > 0 && s.InspectStatus == "1").ToList();
                var billNo = "";
                var bl = true;
                do
                {
                    //获取自增单据号
                    billNo = new Common().GetMaxNo("SO");
                    var no = billNo;
                    bl = Db.Queryable<BllExportNotice>().Any(m => m.SONo == no);
                } while (bl);
 
                List<SoDetailInfo> soDetailList = new List<SoDetailInfo>();
                //开启事务
                Db.BeginTran();
 
                var list = new List<BllExportNoticeDetail>();
                //添加出库单
                foreach (var d in model.SoDetails)
                {
                    if (d.Qty < 1)
                    {
                        throw new Exception("出库数量必须大于0");
                    }
                    if (string.IsNullOrWhiteSpace(d.LotNo))
                    {
                        throw new Exception("批次不可为空!");
                    }
                    //库存明细
                    List<DataStockDetail> stockDetails = stockDetailList.Where(s => s.SkuNo == d.SkuNo && s.LotNo == d.LotNo).OrderByDescending(o => o.CompleteTime).ToList();
                    if (stockDetails.Count < 1)
                    {
                        throw new Exception($"库存中未查询到出库物料信息:{d.SkuNo}");
                    }
                    //判断数量
                    var qty = stockDetails.Sum(s => s.Qty - s.FrozenQty - s.LockQty);
                    if (d.Qty > qty)
                    {
                        throw new Exception($"库存中出库物料信息:{d.SkuNo}、{d.LotNo} 库存数量不足");
                    }
                    //添加出库单明细
                    var noticeDetail = new BllExportNoticeDetail()
                    {
                        SONo = billNo,
                        SkuNo = d.SkuNo,
                        SkuName = d.SkuName,
                        Standard = "",
                        LotNo = d.LotNo,
                        LotText = "",
                        Qty = d.Qty,
                        AllotQty = 0,
                        FactQty = 0,
                        CompleteQty = 0,
                        PackagNo = "",
                        Price = 0,
                        Money = 0,
                        IsBale = "",
                        IsBelt = "",
                        SupplierLot = "",
                        IsWave = "0",
                        WaveNo = "",
                        IsIssueLotNo = "1",
 
                        OrderDetailCode = d.OrderDetailCode,
 
                        CreateUser = 0,
                    };
                    list.Add(noticeDetail);
                    //锁定库存数量
                    decimal lockQty = 0;
                    foreach (var item in stockDetails)
                    {
                        if (lockQty >= d.Qty)
                        {
                            break;
                        }
                        if (item.Qty - item.LockQty - item.FrozenQty <= d.Qty - lockQty)
                        {
                            item.LockQty += item.Qty - item.LockQty - item.FrozenQty;
                            lockQty += (decimal)(item.Qty - item.LockQty - item.FrozenQty);
                        }
                        else
                        {
                            item.LockQty += d.Qty - lockQty;
                            lockQty += d.Qty - lockQty;
                        }
                        Db.Updateable(item).UpdateColumns(it => new { it.LockQty }).ExecuteCommand();
                    }
 
                    SoDetailInfo soDetail = new SoDetailInfo();
                    soDetail.OrderDetailCode = d.OrderDetailCode;
                    soDetail.LockQty = d.Qty;
                    soDetail.LotNo = d.LotNo;
 
                    soDetailList.Add(soDetail);
                }
                //出库单信息
                var notice = new BllExportNotice()
                {
                    SONo = billNo,
                    OrderCode = model.OrderCode,
                    Type = model.SoType,
                    Status = "0",
                    Origin = "WMS",
                    CustomerNo = model.Customer,
                    CustomerName = "",
                    LogisticsId = null,
                    IsWave = "0",
                    WaveNo = "",
                    IsDespatch = "0",
 
                    CreateUser = 0,
                };
 
                var n = Db.Insertable<BllExportNotice>(notice).ExecuteCommand();
                var m = Db.Insertable<BllExportNoticeDetail>(list).ExecuteCommand();
 
                if (n <= 0 || m <= 0)
                {
                    Db.RollbackTran();
                    throw new Exception("操作失败");
                }
                //提交事务
                Db.CommitTran();
                //回传上游系统锁定数量信息
                result.Success = "0";
                result.Message = "操作成功";
                result.SoDetails = soDetailList;
                return result;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
    }
}