Blame view

WebApp/Apps/task/TaskApp.cs 67 KB
霍尔 authored
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
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 立库任务表
    /// </summary>

    public partial class TaskApp
    {
        private IUnitWork _unitWork;
        public IRepository<Task> _app;
        public IRepository<Container> _appc;
        public IRepository<Inventory> _appi;
        public IRepository<Location> _appl;
        public IRepository<ReceiptDetail> _apprd;
        public IRepository<ReceiptHeader> _apprh;
        public IRepository<ShipmentHeader> _apph;
        public IRepository<ShipmentDetail> _appsd;
        public IRepository<TaskDetail> _apptd;
        public IRepository<InventoryTransaction> _appit;
        private BaseDBContext _context;
        private static IHostingEnvironment _hostingEnvironment;
gy.huang authored
31
        private IAuth _auth;
霍尔 authored
32
33
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);
gy.huang authored
34
        public TaskApp(IUnitWork unitWork, IAuth auth, IRepository<Inventory> inventory, IRepository<InventoryTransaction> inventoryTransaction, IRepository<Container> Icontainer, IRepository<ReceiptHeader> receiptHeader, IRepository<ReceiptDetail> receiptDetail, IRepository<Task> repository, IRepository<Location> repositoryl, IRepository<ShipmentHeader> repositoryh, IRepository<TaskDetail> repositorytd, IRepository<ShipmentDetail> repositorysd, IHostingEnvironment hostingEnvironment, BaseDBContext context)
霍尔 authored
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        {
            _unitWork = unitWork;
            _app = repository;
            _appl = repositoryl;
            _apph = repositoryh;
            _appsd = repositorysd;
            _apptd = repositorytd;
            _apprd = receiptDetail;
            _apprh = receiptHeader;
            _appc = Icontainer;
            _appi = inventory;
            _appit = inventoryTransaction;
            _hostingEnvironment = hostingEnvironment;
            _context = context;
gy.huang authored
49
            _auth = auth;
霍尔 authored
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
        }

        public TaskApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appl._loginInfo = loginInfo;
            _apph._loginInfo = loginInfo;
            _appsd._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            _appc._loginInfo = loginInfo;
            _apprd._loginInfo = loginInfo;
            _apprh._loginInfo = loginInfo;
            _appi._loginInfo = loginInfo;
            _appit._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, Task entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<Task>.GetExpressions(entity));
            data = data.Where(u =>u.LastStatus != TaskStatus.已经完成);

            GetData(data, result, pageRequest);
            result.count = data.Count();

            return result;
        }

        public void Ins(Task entity)
        {
            _app.Add(entity);
        }

        public void Upd(Task entity)
        {
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            foreach (var item in ids)
            {
                Task entity = _app.FindSingle(u => u.Id == item);
                if (entity.FirstStatus > TaskStatus.待下发任务)
                {
                    throw new Exception("任务号:" + entity.TaskNo + "已下发,不允许删除");
                }
                else
                {
                    _app.Delete(entity);
                    _unitWork.Delete<TaskDetail>(u => u.TaskNo.Equals(entity.TaskNo));
                }
            }
        }

        public Task FindSingle(Expression<Func<Task, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<Task> Find(Expression<Func<Task, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<Task> exp = imp.ConvertToModel<Task>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    Task e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(Task entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(Task entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<Task>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<Task> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }

        public TableData FindCode(string Code)
        {
            TableData tab = new TableData();
            try
            {
                TaskDetail taskDetail = _unitWork.Find<TaskDetail>(n => n.ContainerCode == Code && (n.Status == TaskStatus.已经到站台 || n.Status == TaskStatus.放货中)).OrderByDescending(x => x.TaskType).FirstOrDefault();
                if (taskDetail == null)
                {
                    tab.code = 0;
                }
                else
                {
                    tab.code = 200;
                    tab.data = taskDetail;
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg = ex.Message;
            }
            return tab;
        }

        //添加回库任务
203
        public string AddTaskBackInfo(TaskDetail tDetail, int? roadWay, string containercode)
霍尔 authored
204
205
206
207
208
209
210
211
212
213
        {
            string strError = "";
            var taskNo ="";
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    //建立回库任务
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();
gy.huang authored
214
                    if (tDetail.TaskType ==TaskType.出库查看 || tDetail.TaskType == TaskType.盘点)
霍尔 authored
215
216
217
218
219
220
221
                    {
                        taskNo = _app.GetTaskNo(TaskNo.查看容器回库);
                    }
                    else
                    {
                        taskNo = _app.GetTaskNo(TaskNo.容器回库);
                    }
gy.huang authored
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
                    //玻钎布缓存站台逻辑 一期改
                    if (roadWay == 1)
                    {
                        task.TaskNo = taskNo;
                        task.OrderCode = taskNo;
                        task.BusinessType = BusinessType.入库_产成品入库单;
                        task.FirstStatus = TaskStatus.待下发任务;
                        task.LastStatus = TaskStatus.待下发任务;
                        _app.Add(task);

                        Station station = (from st in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "" && n.IsStop == 0)
                                                  join sr in _unitWork.Find<StationRoadway>(n => n.RoadWay == 1)
                                                  on  st.Code equals sr.StationCode
                                                  select st).FirstOrDefault();
                        if (station == null)
                        {
                            return "无可用缓存站台";
                        }
                        station.Containercode = tDetail.ContainerCode;
                        _unitWork.Update(station);
                        taskDetail.TaskNo = taskNo;
                        taskDetail.OrderCode = taskNo;
                        taskDetail.TaskType = TaskType.站台到站台;
                        taskDetail.ContainerCode = containercode;
                        taskDetail.SourceLocation = tDetail.Station;
                        taskDetail.DestinationLocation = station.Code;
                        taskDetail.OderQty = 0;
                        taskDetail.ContainerQty = 0;
                        taskDetail.HadQty = 0;
                        taskDetail.Roadway = roadWay;
                        taskDetail.Station = station.Code;
                        taskDetail.Status = TaskStatus.待下发任务;
                        taskDetail.Priority = 99;
                        _apptd.Add(taskDetail);
霍尔 authored
256
gy.huang authored
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
                        Container container = _unitWork.Find<Container>(n => n.Code == containercode).FirstOrDefault();
                        container.LocationCode = station.Code;
                        _appc.UpdateByTracking(container);
                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == containercode).ToList();
                        foreach (Inventory inv in inventories)
                        {
                            inv.LocationCode = station.Code;
                            _unitWork.Update(inv);
                        }
                    }
                    else
                    {
                        task.TaskNo = taskNo;
                        task.OrderCode = taskNo;
                        task.BusinessType = BusinessType.入库_其他入库单;
                        task.FirstStatus = TaskStatus.待下发任务;
                        task.LastStatus = TaskStatus.待下发任务;
                        _app.Add(task);

                        taskDetail.TaskNo = taskNo;
                        taskDetail.OrderCode = taskNo;
                        taskDetail.TaskType = TaskType.容器回库;
                        taskDetail.ContainerCode = containercode;
                        taskDetail.SourceLocation = tDetail.Station;
281
                        taskDetail.DestinationLocation = _unitWork.Find<Location>(n => n.Roadway == roadWay).Select(u => u.Type).FirstOrDefault();
gy.huang authored
282
283
284
285
286
287
288
289
290
291
                        taskDetail.OderQty = 0;
                        taskDetail.ContainerQty = 0;
                        taskDetail.HadQty = 0;
                        taskDetail.Roadway = roadWay;
                        taskDetail.Station = tDetail.Station;
                        taskDetail.Status = TaskStatus.待下发任务;
                        taskDetail.Priority = 99;
                        _apptd.Add(taskDetail);
                    }
                    tran.Commit();
霍尔 authored
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    strError = ex.Message;
                    return strError;
                }
                return strError;
            }
        }

        public TableData ContainerBack(string containerCode, string IsFull)
        {
            TableData tab = new TableData();
            tab.code = 200;
            ReceiptDetail receiptDel = new ReceiptDetail();
            ShipmentDetail shipmentDetl = new ShipmentDetail();
309
            string str = "";
霍尔 authored
310
311
312
313
314
            try
            {
                List<TaskDetail> taskDels = _unitWork.Find<TaskDetail>(n => (n.Status == TaskStatus.已经到站台 || n.Status == TaskStatus.放货中 || n.Status == TaskStatus.放取货完成 || n.Status==TaskStatus.出库查看完成) && n.ContainerCode == containerCode).ToList();
                if (taskDels.Count != 0)
                {
315
                    //建立回库任务
霍尔 authored
316
                    //**************************
317
                    if (!string.IsNullOrEmpty(taskDels.Select(t => t.Roadway).FirstOrDefault().ToString()))
霍尔 authored
318
                    {
319
320
                        str = AddTaskBackInfo(taskDels.FirstOrDefault(), taskDels.Select(t => t.Roadway).FirstOrDefault(), containerCode);
                        if (str == "")
霍尔 authored
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
                        {
                            //更新容器管理
                            Container container = _unitWork.Find<Container>(n => n.Code == containerCode).FirstOrDefault();
                            if (container == null)
                            {
                                tab.code = 300;
                                tab.msg = "无该容器!";
                                return tab;
                            }
                            container.Status = IsFull;
                            container.UpdateTime = DateTime.Now;
                            _unitWork.Update(container);
                            int tcount = 1;
                            foreach (TaskDetail t in taskDels)
                            {
                                if (t.HadQty == 0)
                                {
                                    t.Status = TaskStatus.待下发任务;
                                    _apptd.Update(t);
                                    continue;
                                }
                                //入库回库
                                if (t.TaskType == TaskType.整盘入库 || t.TaskType == TaskType.补充入库)
                                {
345
                                    tab = ReceiptContainerBack(tab, t, receiptDel, IsFull);
霍尔 authored
346
347
348
349
                                }
                                //出库回库
                                else if (t.TaskType == TaskType.整盘出库 || t.TaskType == TaskType.分拣出库)
                                {
350
                                    tab = ShipmentContainerBack(tab, t, shipmentDetl, tcount);
霍尔 authored
351
                                    tcount++;
gy.huang authored
352
                                }else if (t.TaskType == TaskType.出库查看 || t.TaskType == TaskType.盘点)
霍尔 authored
353
                                {
354
                                    //List<Inventory> inventoryothers = _unitWork.Find<Inventory>(n => n.ContainerCode == t.ContainerCode).ToList();
霍尔 authored
355
                                    //更新同个容器中所有物料对应新的回库仓位
356
357
358
359
360
361
362
363
364
365
366
                                    //if (inventoryothers.Count > 0)
                                    //{
                                    //    foreach (Inventory inv in inventoryothers)
                                    //    {
                                    //        if (inv.LocationCode != location)
                                    //        {
                                    //            inv.LocationCode = location;
                                    //            _appi.Update(inv);
                                    //        }
                                    //    }
                                    //}
霍尔 authored
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
                                }
                                else
                                {
                                    t.Status = TaskStatus.错误;
                                    _apptd.Update(t);
                                    tab.code = 300;
                                    tab.msg += "Id:" + t.Id + " 任务类型不正确/n";
                                }
                            }
                            var taskNos = taskDels.GroupBy(n => n.TaskNo).ToList();
                            foreach (var t in taskNos)
                            {
                                Task task = _unitWork.Find<Task>(n => n.TaskNo == t.Key).FirstOrDefault();
                                List<TaskDetail> tasks = _unitWork.Find<TaskDetail>(n => n.TaskNo == t.Key).OrderByDescending(x => x.Status).ToList();
                                task.FirstStatus = tasks.First().Status;
                                task.LastStatus = tasks.Last().Status;
                                _app.Update(task);
                            }
                            if (tab.code == 200)
                            {
                                tab.msg = "回库任务创建成功";
                            }
                        }
                        else
                        {
                            tab.code = 300;
393
                            tab.msg = str + "建立回库任务失败!";
霍尔 authored
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
                        }
                    }
                    else
                    {
                        tab.code = 300;
                        tab.msg = "仓位分配失败!";
                    }
                }
                else
                {
                    tab.code = 300;
                    tab.msg = "该托盘无物料明细!";
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }

        //容器添加
417
        public bool SetInventory(TaskDetail taskDel, ReceiptDetail receiptDel, string IsFull)
霍尔 authored
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
        {
            try
            {
                //更新库存
                Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDel.ContainerCode && n.MaterialCode == taskDel.MaterialCode).FirstOrDefault();
                InventoryTransaction inventoryTransaction = new InventoryTransaction();
                ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(n => n.Code == receiptDel.ReceiptCode).FirstOrDefault();
                //更新库存和新增库存异动记录,后期修改为接收到WCS完成信号后做此动作

                if (inventory != null)
                {
                    inventory.Lot = receiptDel.Lot;
                    inventory.WarehouseType = receiptHeader.WarehouseType;
                    inventory.Batch = receiptDel.Batch;
                    inventory.ReceiptId = receiptHeader.Id;
                    inventory.ReceiptCode = receiptHeader.Code;
                    inventory.SourceLine = receiptDel.SourceLine;
                    inventory.ReceiptDetailId = receiptDel.Id;
                    inventory.Project = receiptDel.Project;
                    inventory.ContainerCode = taskDel.ContainerCode;
                    inventory.SourceCode = receiptDel.SourceCode;
                    inventory.MaterialCode = receiptDel.MaterialCode;
                    inventory.MaterialId = receiptDel.MaterialId;
                    inventory.SourceCode = receiptDel.SourceCode;
                    inventory.Status = receiptDel.InventoryStatus;
                    inventory.ContainerStatus = IsFull;
                    inventory.Qty += taskDel.HadQty;
gy.huang authored
445
                    inventory.TaskStatus = InventoryTaskType.无盘点任务;
霍尔 authored
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
                    _appi.UpdateByTracking(inventory);

                    inventoryTransaction.WarehouseType = inventory.WarehouseType;
                    inventoryTransaction.SourceCode = taskDel.SourceCode;
                    inventoryTransaction.BillCode = taskDel.OrderCode;
                    inventoryTransaction.Type = TransactionType.入库;
                    inventoryTransaction.ContainerCode = inventory.ContainerCode;
                    inventoryTransaction.MaterialCode = inventory.MaterialCode;
                    inventoryTransaction.MaterialName = taskDel.MaterialName;
                    inventoryTransaction.Batch = inventory.Batch;
                    inventoryTransaction.Lot = inventory.Lot;
                    inventoryTransaction.LocationCode = inventory.LocationCode;
                    inventoryTransaction.Qty = taskDel.OderQty;
                    inventoryTransaction.TaskQty = taskDel.HadQty;
                    inventoryTransaction.Status = inventory.Status;
                    _unitWork.Add(inventoryTransaction);
                }
                else
                {
                    inventory = new Inventory();
                    inventory.Lot = receiptDel.Lot;
                    inventory.WarehouseType = receiptHeader.WarehouseType;
                    inventory.Batch = receiptDel.Batch;
                    inventory.ReceiptId = receiptHeader.Id;
                    inventory.ReceiptCode = receiptHeader.Code;
                    inventory.SourceLine = receiptDel.SourceLine;
                    inventory.ReceiptDetailId = receiptDel.Id;
                    inventory.Project = receiptDel.Project;
                    inventory.ContainerCode = taskDel.ContainerCode;
                    inventory.SourceCode = receiptDel.SourceCode;
                    inventory.MaterialCode = receiptDel.MaterialCode;
                    inventory.MaterialId = receiptDel.MaterialId;
                    inventory.SourceCode = receiptDel.SourceCode;
                    inventory.Status = receiptDel.InventoryStatus;
                    inventory.ContainerStatus = IsFull;
                    inventory.Qty = taskDel.HadQty;
gy.huang authored
482
                    inventory.TaskStatus = InventoryTaskType.无盘点任务;
霍尔 authored
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
                    _appi.Add(inventory);

                    inventoryTransaction.WarehouseType = inventory.WarehouseType;
                    inventoryTransaction.SourceCode = taskDel.SourceCode;
                    inventoryTransaction.BillCode = taskDel.OrderCode;
                    inventoryTransaction.Type = TransactionType.入库;
                    inventoryTransaction.ContainerCode = inventory.ContainerCode;
                    inventoryTransaction.MaterialCode = inventory.MaterialCode;
                    inventoryTransaction.MaterialName = taskDel.MaterialName;
                    inventoryTransaction.Batch = inventory.Batch;
                    inventoryTransaction.Lot = inventory.Lot;
                    inventoryTransaction.LocationCode = inventory.LocationCode;
                    inventoryTransaction.Qty = taskDel.OderQty;
                    inventoryTransaction.TaskQty = taskDel.HadQty;
                    inventoryTransaction.Status = inventory.Status;
                    _appit.Add(inventoryTransaction);
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
508
        public TableData ReceiptContainerBack(TableData tab, TaskDetail taskDel, ReceiptDetail receiptDel, string IsFull)
霍尔 authored
509
510
511
512
513
        {
            receiptDel = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == taskDel.SourceCode && n.MaterialCode == taskDel.MaterialCode).FirstOrDefault();
            if (receiptDel != null)
            {
                //容器添加
514
                if (SetInventory(taskDel, receiptDel, IsFull))
霍尔 authored
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
                {
                    //更新任务明细状态
                    taskDel.Status = TaskStatus.模拟电气拣放货完成按钮;
                    _apptd.Update(taskDel);
                    //更新入库明细状态
                    receiptDel.Status = ReceiptHeaderStatus.上架;
                    if (taskDel.TaskNo.Contains(TaskNo.入库手动分配))
                    {
                        receiptDel.QtyCompleted += taskDel.HadQty;
                    }
                    else
                    {
                        receiptDel.QtyCompleted = taskDel.HadQty;
                    }
                    _apprd.UpdateByTracking(receiptDel);
                    if (taskDel.TaskNo.Contains(TaskNo.入库手动分配))
                    {
                        return tab;
                    }
                    //未满状态
                    if (taskDel.OderQty > taskDel.HadQty)
                    {
                        string str = "";
                        str = AddTaskInfo(receiptDel);
                        //寻找新托盘建立新任务
                        if (str == "")
                        {
                            //更新入库明细
                            receiptDel.Status = ReceiptHeaderStatus.分配完成;
                            _apprd.UpdateByTracking(receiptDel);
                        }
                        else
                        {
                            tab.code = 300;
                            tab.msg += str + "所以无法找到下个入库托盘/n";
                        }
                    }
                }
                else
                {
                    taskDel.Status = TaskStatus.错误;
                    _apptd.Update(taskDel);
                    tab.code = 300;
                    tab.msg += "Id:" + taskDel.Id + " 容器插入失败/n";
                }
            }
            else
            {
                taskDel.Status = TaskStatus.错误;
                _apptd.Update(taskDel);
                tab.code = 300;
                tab.msg += "Id:" + taskDel.Id + " 找不到入库明细/n";
            }
            return tab;
        }

        private string AddTaskInfo(ReceiptDetail receiptDetail)
        {
            string str = "";
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(n => n.Code == receiptDetail.ReceiptCode).FirstOrDefault();
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();
                    var tno = "";
                    int tcount = _unitWork.Find<Task>(n => n.OrderCode == receiptDetail.ReceiptCode).Count();
                    if (tcount > 0)
                    {
                        var tasknum = _unitWork.Find<Task>(n => n.OrderCode == receiptDetail.ReceiptCode).Select(a => a.TaskNo).First();
                        tno = tasknum;
                    }
                    else
                    {
                        tno = _app.GetTaskNo(TaskNo.入库自动分配);
                        task.TaskNo = tno;
                        task.SourceCode = receiptDetail.SourceCode;
                        task.OrderCode = receiptDetail.ReceiptCode;
                        task.BusinessType = receiptHeader.Type;
                        task.TotalQty = receiptHeader.TotalQty;
                        task.TotalLines = receiptHeader.TotalLines;
                        task.FirstStatus = TaskStatus.待下发任务;
                        task.LastStatus = TaskStatus.待下发任务;
                        task.Project = receiptDetail.Project;
                        _app.Add(task);
                    }

                    Inventory inv = (from inventory in _unitWork.Find<Inventory>(n => n.MaterialCode == receiptDetail.MaterialCode && n.ContainerStatus != ContainerStatus. && n.WarehouseType == receiptHeader.WarehouseType)
                                     join container in _unitWork.Find<Container>(x => x.IsLock == 0)
                                     on inventory.ContainerCode equals container.Code
                                     select inventory).FirstOrDefault();
                    if (inv != null)
                    {
                        taskDetail.TaskType = TaskType.补充入库;
                        taskDetail.ContainerQty = receiptDetail.Qty - receiptDetail.QtyCompleted;
                        taskDetail.TaskNo = tno;
                        taskDetail.SourceCode = receiptDetail.SourceCode;
                        taskDetail.OrderCode = receiptDetail.ReceiptCode;
                        taskDetail.ContainerCode = inv.ContainerCode;

                        taskDetail.MaterialCode = receiptDetail.MaterialCode;
                        taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == receiptDetail.MaterialCode).Select(a => a.Name).First();
                        taskDetail.Batch = receiptDetail.Batch;
                        taskDetail.Lot = receiptDetail.Lot;
                        taskDetail.OderQty = receiptDetail.Qty;
                        taskDetail.HadQty = receiptDetail.QtyCompleted;

                        Location loc = _unitWork.Find<Location>(n => n.Code == inv.LocationCode && n.IsStop == false).First();
                        if (loc != null)
                        {
                            taskDetail.SourceLocation = loc.Code;
                            taskDetail.DestinationLocation = loc.Code;
                            taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == inv.LocationCode && n.IsStop == false).Select(a => a.Roadway).First();
                        }
                        else
                        {
                            taskDetail.Roadway = 0;
                        }
                        taskDetail.Priority = 0;
                        taskDetail.Status = TaskStatus.待下发任务;
                        _apptd.Add(taskDetail);
                    }
                    else
                    {
                        Location location = _unitWork.Find<Location>(n => n.Type == receiptHeader.WarehouseType && n.Status == LocationStatus.空容器 && n.IsStop == false).FirstOrDefault();
                        if (location != null)
                        {
                            taskDetail.TaskType = TaskType.整盘入库;
                            taskDetail.ContainerQty = receiptDetail.Qty - receiptDetail.QtyCompleted;
                            taskDetail.TaskNo = tno;
                            taskDetail.SourceCode = receiptDetail.SourceCode;
                            taskDetail.OrderCode = receiptDetail.ReceiptCode;
                            taskDetail.ContainerCode = location.ContainerCode;
                            taskDetail.MaterialCode = receiptDetail.MaterialCode;
                            taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == receiptDetail.MaterialCode).Select(a => a.Name).First();
                            taskDetail.Batch = receiptDetail.Batch;
                            taskDetail.Lot = receiptDetail.Lot;
                            taskDetail.OderQty = receiptDetail.Qty;
                            taskDetail.HadQty = receiptDetail.QtyCompleted;
                            taskDetail.SourceLocation = location.Code;
                            taskDetail.DestinationLocation = location.Code;
                            taskDetail.Roadway = location.Roadway;
                            taskDetail.Priority = 0;
                            taskDetail.Status = TaskStatus.待下发任务;
                            _apptd.Add(taskDetail);
                            location.Status = LocationStatus.入库占用;
                            _appl.Update(location);
                        }
                        else
                        {
                            Inventory invOther = (from inventory in _unitWork.Find<Inventory>(n => n.ContainerStatus != ContainerStatus. && n.WarehouseType == receiptHeader.WarehouseType)
                                                  join container in _unitWork.Find<Container>(x => x.IsLock == 0)
                                                  on inventory.ContainerCode equals container.Code
                                                  select inventory).FirstOrDefault();
                            if (invOther != null)
                            {
                                taskDetail.TaskType = TaskType.补充入库;
                                taskDetail.ContainerQty = receiptDetail.Qty - receiptDetail.QtyCompleted;
                                taskDetail.TaskNo = tno;
                                taskDetail.SourceCode = receiptDetail.SourceCode;
                                taskDetail.OrderCode = receiptDetail.ReceiptCode;
                                taskDetail.ContainerCode = invOther.ContainerCode;

                                taskDetail.MaterialCode = receiptDetail.MaterialCode;
                                taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == receiptDetail.MaterialCode).Select(a => a.Name).First();
                                taskDetail.Batch = receiptDetail.Batch;
                                taskDetail.Lot = receiptDetail.Lot;
                                taskDetail.OderQty = receiptDetail.Qty;
                                taskDetail.HadQty = receiptDetail.QtyCompleted;

                                Location loc = _unitWork.Find<Location>(n => n.Code == invOther.LocationCode && n.IsStop == false).First();
                                if (loc != null)
                                {
                                    taskDetail.SourceLocation = loc.Code;
                                    taskDetail.DestinationLocation = loc.Code;
                                    taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == invOther.LocationCode && n.IsStop == false).Select(a => a.Roadway).First();
                                }
                                else
                                {
                                    taskDetail.Roadway = 0;
                                }
                                taskDetail.Priority = 0;
                                taskDetail.Status = TaskStatus.待下发任务;
                                _apptd.Add(taskDetail);
                            }
                            else
                            {
                                str = "未能找到仓位!";
                                return str;
                            }
                        }
                    }
                    //建立容器出库任务
                    if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == receiptDetail.MaterialCode && n.OrderCode == receiptDetail.ReceiptCode))
                    {
                        int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && n.TaskType == TaskType.容器出库).Count();
                        if (ptdcount < 1)
                        {

                            Task ptask = new Task();
                            TaskDetail ptaskDetail = new TaskDetail();
                            var taskNo = _app.GetTaskNo(TaskNo.容器出库);
                            ptask.TaskNo = taskNo;
                            ptask.OrderCode = taskNo;
                            ptask.BusinessType = BusinessType.出库_其他出库单;
                            ptask.FirstStatus = TaskStatus.待下发任务;
                            ptask.LastStatus = TaskStatus.待下发任务;
                            _app.Add(ptask);

                            ptaskDetail.TaskNo = taskNo;
                            ptaskDetail.OrderCode = taskNo;
                            ptaskDetail.TaskType = TaskType.容器出库;
                            ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                            ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                            ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                            ptaskDetail.OderQty = 0;
                            ptaskDetail.ContainerQty = 0;
                            ptaskDetail.HadQty = 0;
                            ptaskDetail.Roadway = taskDetail.Roadway;
                            ptaskDetail.Station = taskDetail.Station;
                            ptaskDetail.Status = TaskStatus.待下发任务;
                            if (taskDetail.Priority != null)
                            {
                                ptaskDetail.Priority = taskDetail.Priority;
                            }
                            else
                            {
                                ptaskDetail.Priority = 0;
                            }
                            _apptd.Add(ptaskDetail);
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    str = ex.Message;
                    return str;
                }
            }

            return str;
        }
761
        public string SaveInventory(TaskDetail taskDel, ShipmentDetail shipmentDel, int tc)
霍尔 authored
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
        {
            string strError = "";
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDel.ContainerCode && n.MaterialCode == taskDel.MaterialCode).FirstOrDefault();
                    List<Inventory> inventoryothers = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDel.ContainerCode && n.Id != inventory.Id).ToList();
                    // Location lct = _unitWork.Find<Location>(n => n.Code == location && n.IsStop == false).FirstOrDefault();
                    InventoryTransaction inventoryTransaction = new InventoryTransaction();
                    //更新库存和新增库存异动记录,后期修改为接收到WCS完成信号后做此动作
                    if (inventory != null)
                    {
                        inventoryTransaction.WarehouseType = inventory.WarehouseType;
                        inventoryTransaction.SourceCode = taskDel.SourceCode;
                        inventoryTransaction.BillCode = taskDel.OrderCode;
                        inventoryTransaction.Type = TransactionType.出库;
                        inventoryTransaction.ContainerCode = inventory.ContainerCode;
                        inventoryTransaction.MaterialCode = inventory.MaterialCode;
                        inventoryTransaction.MaterialName = taskDel.MaterialName;
                        inventoryTransaction.Batch = inventory.Batch;
                        inventoryTransaction.Lot = inventory.Lot;
                        inventoryTransaction.LocationCode = inventory.LocationCode;
                        inventoryTransaction.Qty = taskDel.OderQty;
                        inventoryTransaction.TaskQty = -taskDel.HadQty;
                        inventoryTransaction.Status = inventory.Status;
                        _appit.Add(inventoryTransaction);

                        int samecount = _unitWork.Find<TaskDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && (n.TaskType == TaskType.整盘出库 || n.TaskType == TaskType.分拣出库) && n.ContainerCode == taskDel.ContainerCode && n.MaterialCode == taskDel.MaterialCode).Count();
                        if (samecount > 1)
                        {
                            if (tc == samecount)
                            {
                                var sumqty = _unitWork.Find<TaskDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && (n.TaskType == TaskType.整盘出库 || n.TaskType == TaskType.分拣出库) && n.ContainerCode == taskDel.ContainerCode && n.MaterialCode == taskDel.MaterialCode).Sum(a => a.HadQty);
                                inventory.Qty -= sumqty;
                                if (inventory.Qty == 0)
                                {
                                    _appi.DeleteByTracking(inventory);
                                }
                                else
                                {
                                    _appi.UpdateByTracking(inventory);
                                }
                            }
                        }
                        else
                        {
                            inventory.Qty -= taskDel.HadQty;
gy.huang authored
810
811
812
813
814
815
816
817
                            if (inventory.Qty == 0)
                            {
                                _appi.DeleteByTracking(inventory);
                            }
                            else
                            {
                                _appi.UpdateByTracking(inventory);
                            }
霍尔 authored
818
819
820
821
822
823
824
825
826
                        }
                    }
                    else
                    {
                        strError = "未找到相应的库存信息,无法做库存变更!";
                        return strError;
                    }
827
                        tran.Commit();
霍尔 authored
828
829
830
831
832
833
834
835
836
837
838
839

                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    strError = ex.Message;
                    return strError;
                }
                return strError;
            }
        }
840
        public TableData ShipmentContainerBack(TableData tab, TaskDetail taskDel, ShipmentDetail shipDel, int tc)
霍尔 authored
841
842
        {
            shipDel = _unitWork.Find<ShipmentDetail>(n => n.SourceCode == taskDel.SourceCode && n.MaterialCode == taskDel.MaterialCode).FirstOrDefault();
843
            string str = "";
霍尔 authored
844
845
846
            if (shipDel != null)
            {
                //库存变更
847
848
                str = SaveInventory(taskDel, shipDel, tc);
                if (str == "")
霍尔 authored
849
850
851
852
853
854
855
856
857
858
859
860
861
862
                {
                    //更新任务明细状态
                    taskDel.Status = TaskStatus.模拟电气拣放货完成按钮;
                    _apptd.Update(taskDel);
                    //更新出库明细状态
                    shipDel.Status = ShipmentHeaderStatus.拣货完成;
                    shipDel.QtyCompleted += taskDel.HadQty;
                    _appsd.Update(shipDel);
                }
                else
                {
                    taskDel.Status = TaskStatus.错误;
                    _apptd.Update(taskDel);
                    tab.code = 300;
863
                    tab.msg += "Id:" + str + " 库存变更失败/n";
霍尔 authored
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
                }
            }
            else
            {
                taskDel.Status = TaskStatus.错误;
                _apptd.Update(taskDel);
                tab.code = 300;
                tab.msg += "Id:" + taskDel.Id + " 找不到出库明细/n";
            }
            return tab;
        }

        /// <summary>
        /// 创建空托回库任务
        /// </summary>
        /// <param name="station"></param>
        /// <param name="location"></param>
        /// <param name="containercode"></param>
        /// <returns></returns>
883
        public string AddEmptyTaskBack(Station station, string location, string containercode,string type)
霍尔 authored
884
885
886
887
888
889
890
        {
            string strError = "";
            //创建任务和调用WCS下发任务接口下发任务,任何回库任务都是立即下发给WCS
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
                    containercode = Guid.NewGuid().ToString("N");
                    while (true)
                    {
                        if (_unitWork.IsExist<Container>(n => n.Code == containercode))
                        {
                            containercode = Guid.NewGuid().ToString("N");
                        }
                        else
                        {
                            Container con = new Container
                            {
                                Code = containercode,
                                IsLock = ContainerLock.任务锁,
                                Status = ContainerStatus.,
                                Type = type,
                                PrintCount = 0
                            };
                            _unitWork.Add(con);
                            station.Containercode = containercode;
                            _unitWork.Update(station);
                            break;
                        }
                    }
霍尔 authored
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();

                    var tno = _app.GetTaskNo(TaskNo.空托盘入库);
                    task.TaskNo = tno;
                    task.OrderCode = tno;
                    task.BusinessType = BusinessType.入库_其他入库单;
                    task.FirstStatus = TaskStatus.待下发任务;
                    task.LastStatus = TaskStatus.待下发任务;
                    _app.Add(task);

                    taskDetail.TaskNo = tno;
                    taskDetail.OrderCode = tno;
                    taskDetail.TaskType = TaskType.空容器入库;
                    taskDetail.ContainerCode = containercode;
929
                    taskDetail.SourceLocation = station.Code;
霍尔 authored
930
931
932
933
934
935
936
937
938
939
940
941
942
                    taskDetail.DestinationLocation = location;
                    taskDetail.OderQty = 0;
                    taskDetail.ContainerQty = 0;
                    taskDetail.HadQty = 0;
                    Location lcount = _unitWork.Find<Location>(n => n.Code == location && n.IsStop == false).FirstOrDefault();
                    if (lcount != null)
                    {
                        taskDetail.Roadway = lcount.Roadway;
                    }
                    else
                    {
                        taskDetail.Roadway = 0;
                    }
943
                    taskDetail.Station = station.Code;
霍尔 authored
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
                    taskDetail.Status = TaskStatus.待下发任务;
                    taskDetail.Priority = 99;
                    _apptd.Add(taskDetail);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    strError = ex.Message;
                    return strError;
                }
                return strError;
            }
        }

        /// <summary>
        /// 空托回库
        /// </summary>
        /// <param name="station"></param>
        /// <param name="containerCode"></param>
        /// <returns></returns>
gy.huang authored
965
        public TableData EmptyContainerBack(string station, string containerCode,string type)
霍尔 authored
966
967
968
969
970
        {
            TableData tab = new TableData();
            tab.code = 200;
            try
            {
971
972
973
974
975
976
977
                Station st = new Station();
                st = _unitWork.Find<Station>(n => n.Code == station).FirstOrDefault();
                if (st.Containercode != "") {
                    tab.code = 300;
                    tab.msg = "站台已被占用!";
                    return tab;
                }
霍尔 authored
978
979
980
981
982
983
984
985
986
                if (_unitWork.IsExist<Location>(n => n.ContainerCode == containerCode))
                {
                    tab.code = 300;
                    tab.msg = "库内存在该托盘!";
                }
                else
                {
                    StationRoadway sr = _unitWork.Find<StationRoadway>(n => n.StationCode == station).FirstOrDefault();
                    //建立回库任务,分配仓位
987
988
989
990
991
992
993
994
995
996
                    Location loc = new Location();
                    if (sr.StationPlace == StationPlace.巷道北面)
                    {
                        loc = _unitWork.Find<Location>(n => n.Status == ContainerStatus. && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == sr.RoadWay).OrderBy(a => a.Row).FirstOrDefault();
                    }
                    else if (sr.StationPlace == StationPlace.巷道南面)
                    {
                        loc = _unitWork.Find<Location>(n => n.Status == ContainerStatus. && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == sr.RoadWay).OrderByDescending(a => a.Row).FirstOrDefault();
                    }
                    if (loc != null)
霍尔 authored
997
998
                    {
                        //任务创建和下发成功后,相应锁定库位和容器,回库完成后解锁仓位和容器
999
1000
                        string str = AddEmptyTaskBack(st, loc.Code, containerCode,type);
                        if (str  == "")
霍尔 authored
1001
1002
1003
1004
1005
1006
1007
                        {
                            tab.code = 200;
                            tab.msg = "空托回库任务建立成功!";
                        }
                        else
                        {
                            tab.code = 300;
1008
                            tab.msg = str + "建立空托回库任务失败!";
霍尔 authored
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
                        }
                    }
                    else
                    {
                        tab.code = 300;
                        tab.msg = "仓位分配失败,没有足够的空仓位!";
                    }
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }
        //拣货完成
        public TableData FinishPickApp(TaskDetail taskDel)
        {
            TableData tab = new TableData();
            try
            {
                TaskDetail td = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDel.ContainerCode && n.TaskNo == n.TaskNo && (n.Status == TaskStatus.已经到站台 || n.Status == TaskStatus.放货中) && n.Id == taskDel.Id).FirstOrDefault();
                if (td != null)
                {
                    td.HadQty += taskDel.HadQty;
                    if (td.HadQty < td.ContainerQty)
                    {
                        td.Status = TaskStatus.放货中;
                        _apptd.Update(td);
                        tab.code = 200;
                    }
                    else if (td.HadQty == td.ContainerQty)
                    {
                        td.Status = TaskStatus.放取货完成;
                        _apptd.Update(td);
                        tab.code = 200;
                    }
                    else
                    {
                        tab.code = 300;
                        tab.msg = "物料拣货数量大于容器分配数量";
                    }
                }
                else
                {
                    tab.code = 300;
                    tab.msg = "未找到该任务明细";
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg = ex.Message;
            }
            return tab;

        }

        //查看完成
        public TableData FinishCheckApp(TaskDetail taskDel)
        {
            TableData tab = new TableData();
            try
            {
                TaskDetail td = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDel.ContainerCode && n.TaskNo == n.TaskNo && (n.Status == TaskStatus.已经到站台) && n.Id == taskDel.Id).FirstOrDefault();
                if (td != null)
                {
                        td.Status = TaskStatus.出库查看完成;
                        _apptd.Update(td);
                        tab.code = 200;

                }
                else
                {
                    tab.code = 300;
                    tab.msg = "未找到该任务明细";
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg = ex.Message;
            }
            return tab;

        }

        public TableData TaskCancelApp(List<Task> tlist)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                TableData tab = new TableData();
                tab.code = 200;
                int totaltaskcount = 0;
                string sErrorMsg = "";
                try
                {
                    foreach (Task task in tlist)
                    {
                        if (task.FirstStatus <= TaskStatus.待下发任务)
                        {
                            List<TaskDetail> taskDetail = _unitWork.Find<TaskDetail>(n => n.TaskNo == task.TaskNo).ToList();
                            if (_unitWork.IsExist<ShipmentHeader>(u => u.Code == task.OrderCode))
                            {
                                ShipmentHeader shipHd = _unitWork.Find<ShipmentHeader>(n => n.Code == task.OrderCode).FirstOrDefault();
                                shipHd.FirstStatus = ShipmentHeaderStatus.新建;
                                shipHd.LastStatus = ShipmentHeaderStatus.新建;
                                _apph.Update(shipHd);

                                List<ShipmentDetail> shipDels = _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == task.OrderCode).ToList();
                                foreach (ShipmentDetail t in shipDels)
                                {
                                    t.Status = ShipmentHeaderStatus.新建;
                                    t.QtyDivided = 0;
                                    _appsd.Update(t);
                                }

                                foreach (TaskDetail td in taskDetail)
                                {
                                    _apptd.Delete(td);
                                }

                                _app.Delete(task);
                            }
                            else if (_unitWork.IsExist<ReceiptHeader>(u => u.Code == task.OrderCode))
                            {
                                //入库任务取消;
                                ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(n => n.Code == task.OrderCode).FirstOrDefault();
                                receiptHeader.FirstStatus = ReceiptHeaderStatus.新建;
                                receiptHeader.LastStatus = ReceiptHeaderStatus.新建;
                                _apprh.Update(receiptHeader);

                                List<ReceiptDetail> receiptDetail = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == task.OrderCode).ToList();
                                foreach (ReceiptDetail d in receiptDetail)
                                {
                                    d.Status = ReceiptHeaderStatus.新建;
                                    _apprd.Update(d);
                                }

                                foreach (TaskDetail td in taskDetail)
                                {
                                    Location loc = _unitWork.Find<Location>(n => n.Code == td.DestinationLocation).First();
                                    loc.Status = LocationStatus.空容器;
                                    _appl.Update(loc);
                                    _apptd.Delete(td);
                                }

                                _app.Delete(task);
                            }
                            else
                            {
                                foreach (TaskDetail td in taskDetail)
                                {
                                    _apptd.Delete(td);
                                }

                                _app.Delete(task);
                            }
                            totaltaskcount += 1;
                        }
                        else
                        {
                            sErrorMsg += "单号为:" + task.OrderCode + "任务号为:" + task.TaskNo + "任务已下发不可以取消任务!<br>";
                            tab.msg = sErrorMsg;
                        }
                    }

                    if (totaltaskcount == tlist.Count)
                    {
                        tab.code = 200;
                        tab.msg = "任务全部成功取消!";
                    }
                    else
                    {
                        tab.code = 200;
                        tab.msg = "取消任务已全部执行完成!部分未成功取消的信息如下:<br>" + tab.msg;
                    }

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    tab.code = 300;
                    tab.msg += ex.Message;
                }
                return tab;
            }
        }

        // <summary>
        /// 线轴入库
        /// </summary>
        public void WireIn(string[] barcode)
        {
            var GoodsNo = barcode[0];
            var WirePart = barcode[1];
            var WireLength = decimal.Parse(barcode[2]);
            var LotNo = barcode[3];

            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    //添加线轴信息到容器表
                    Container container = new Container();
                    //判断容器表中是否存在
                    if (_unitWork.IsExist<Container>(u => u.Code == GoodsNo))
                    {
                        container = _unitWork.FindSingle<Container>(u => u.Code == GoodsNo);
                        container.Status = ContainerStatus.;
                        _appc.Update(container);
                    }
                    else
                    {
                        container.Code = GoodsNo;
                        container.Type = "S";
                        container.Status = ContainerStatus.;
                        container.PrintCount = 0;
                        _appc.Add(container);
                    }

                    //添加线轴信息到库存表
                    Inventory inventory = new Inventory();
                    if (_unitWork.IsExist<Inventory>(u => u.ContainerCode == GoodsNo))
                    {
                        throw new Exception("库存已存在此物料: " + GoodsNo);
                    }
                    else
                    {
                        inventory.ContainerCode = GoodsNo;
                        inventory.WarehouseType = "P";
                        inventory.MaterialCode = WirePart;
                        inventory.Status = "Entering";
                        inventory.ContainerStatus = _unitWork.FindSingle<Container>(u => u.Code == GoodsNo).Status.ToString();
                        inventory.Lot = LotNo;
                        inventory.Qty = WireLength;
gy.huang authored
1247
                        inventory.TaskStatus = InventoryTaskType.无盘点任务;
霍尔 authored
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
                        _appi.Add(inventory);
                    }

                    //创建任务表
                    Task task = new Task();
                    var taskNo = _app.GetTaskNo(TaskNo.入库自动分配);
                    task.TaskNo = taskNo;
                    task.OrderCode = taskNo;
                    task.BusinessType = BusinessType.入库_手持入库单;
                    task.TotalQty = WireLength;
                    task.FirstStatus = task.LastStatus = TaskStatus.新建任务;
                    _app.Add(task);

                    //创建任务明细表
                    TaskDetail detail = new TaskDetail();
                    detail.TaskNo = taskNo;
                    detail.OrderCode = taskNo;
                    detail.TaskType = TaskType.整盘入库;
                    detail.ContainerCode = GoodsNo;
                    detail.OderQty = WireLength;
                    detail.ContainerQty = WireLength;
                    detail.MaterialCode = WirePart;
                    detail.Lot = LotNo;
                    detail.Status = TaskStatus.新建任务;
                    _apptd.Add(detail);

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw new Exception(ex.Message);
                }
            }
        }
gy.huang authored
1283
1284
1285
1286
1287
1288
1289

        // <summary>
        /// 整托手动入库
        /// </summary>
        public TableData HandTaskInApp(string containerCode,string station)
        {
            TableData response = new TableData();
1290
            response.code = 200;
gy.huang authored
1291
1292
            try
            {
1293
                Station s = _unitWork.Find<Station>(n => n.Code == station && n.Containercode != "").FirstOrDefault();
gy.huang authored
1294
1295
1296
                if (s == null)
                {
                    response.code = 500;
1297
                    response.msg = "该托盘未添加物料,请添加物料!";
gy.huang authored
1298
1299
                    return response;
                }
1300
1301

                Container container = _unitWork.Find<Container>(n => n.Code == s.Containercode).FirstOrDefault();
gy.huang authored
1302
1303
                if (container == null)
                {
1304
1305
1306
                    response.code = 500;
                    response.msg = "未找到该托盘!";
                    return response;
gy.huang authored
1307
                }
1308
1309
                containerCode = container.Code;
                StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == s.Code).FirstOrDefault();
gy.huang authored
1310
1311
1312
1313
1314
1315
                TaskDetail taskDetail = new TaskDetail
                {
                    Station = s.Code,
                    TaskType = TaskType.出库查看
                };
                string str = AddTaskBackInfo(taskDetail, stationRoadway.RoadWay, containerCode);
gy.huang authored
1316
                if (!string.IsNullOrEmpty(str))
gy.huang authored
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
                {
                    response.code = 500;
                    response.msg = "失败:" + str;
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
gy.huang authored
1329
1330
1331
1332
1333
1334
1335
1336
        }

        // <summary>
        /// 整托手动添加物料库存
        /// </summary>
        public TableData AddInventoryApp(string containerCode, string material,string Batch, string station,int num, string sourceCode)
        {
            TableData response = new TableData();
1337
            response.code = 200;
gy.huang authored
1338
1339
1340
1341
1342
1343
1344
1345
1346
            try
            {
                Station s = _unitWork.Find<Station>(n => n.Code == station && (n.Containercode == "" || n.Containercode == containerCode)).FirstOrDefault();
                if (s == null)
                {
                    response.code = 500;
                    response.msg = "该站台已被别的托盘占用不能入库!";
                    return response;
                }
1347
1348
                Container container = _unitWork.Find<Container>(n => n.Code == containerCode).FirstOrDefault();
                if (container != null)
gy.huang authored
1349
                {
1350
1351
1352
1353
                    containerCode = container.Code;
                    container.Status = ContainerStatus.;
                    container.Type = ContainerType.普通栈板;
                    _unitWork.Update(container);
gy.huang authored
1354
                }
1355
                else
gy.huang authored
1356
                {
1357
1358
                    containerCode = Guid.NewGuid().ToString("N");
                    while (true)
gy.huang authored
1359
                    {
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
                        if (_unitWork.IsExist<Container>(n => n.Code == containerCode))
                        {
                            containerCode = Guid.NewGuid().ToString("N");
                        }
                        else
                        {
                            Container con = new Container
                            {
                                Code = containerCode,
                                IsLock = ContainerLock.任务锁,
                                Status = ContainerStatus.,
                                Type = ContainerType.普通栈板,
                                PrintCount = 0
                            };
                            _unitWork.Add(con);
                            s.Containercode = containerCode;
                            _unitWork.Update(s);
                            break;
                        }
                    }
gy.huang authored
1380
                }
1381
gy.huang authored
1382
1383
1384
1385
1386
                //更新库存
                Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == containerCode && n.MaterialCode == material).FirstOrDefault();
                InventoryTransaction inventoryTransaction = new InventoryTransaction();
                if (sourceCode == "")
                {
1387
                    sourceCode = "HandTaskIn" + station;
gy.huang authored
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
                }
                //更新库存和新增库存异动记录,后期修改为接收到WCS完成信号后做此动作
                if (inventory != null)
                {
                    inventory = new Inventory();
                    inventory.Batch = Batch;
                    inventory.SourceCode = sourceCode;
                    inventory.MaterialCode = material;
                    inventory.Status = InventoryStatus.良品;
                    inventory.ContainerStatus = ContainerStatus.;
                    inventory.Qty = num;
gy.huang authored
1399
                    inventory.TaskStatus = InventoryTaskType.无盘点任务;
gy.huang authored
1400
1401
1402
1403
1404
1405
                    _appi.UpdateByTracking(inventory);

                    inventoryTransaction.Type = TransactionType.入库;
                    inventoryTransaction.SourceCode = inventory.SourceCode;
                    inventoryTransaction.ContainerCode = inventory.ContainerCode;
                    inventoryTransaction.MaterialCode = inventory.MaterialCode;
1406
                    inventoryTransaction.LocationCode = "";
gy.huang authored
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
                    inventoryTransaction.Batch = inventory.Batch;
                    inventoryTransaction.Qty = num;
                    inventoryTransaction.Status = inventory.Status;
                    _unitWork.Add(inventoryTransaction);
                }
                else
                {
                    inventory = new Inventory
                    {
                        Batch = Batch,
                        SourceCode = sourceCode,
                        MaterialCode = material,
                        Status = InventoryStatus.良品,
                        ContainerStatus = ContainerStatus.,
1421
1422
                        Qty = num,
                        ContainerCode = containerCode
gy.huang authored
1423
1424
1425
1426
1427
1428
                    };
                    _appi.Add(inventory);

                    inventoryTransaction.Type = TransactionType.入库;
                    inventoryTransaction.SourceCode = inventory.SourceCode;
                    inventoryTransaction.ContainerCode = inventory.ContainerCode;
1429
                    inventoryTransaction.LocationCode = "";
gy.huang authored
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
                    inventoryTransaction.MaterialCode = inventory.MaterialCode;
                    inventoryTransaction.Batch = inventory.Batch;
                    inventoryTransaction.Qty = num;
                    inventoryTransaction.Status = inventory.Status;
                    _appit.Add(inventoryTransaction);
                }
            }
            catch (Exception ex)
            {
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }
gy.huang authored
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453

        // <summary>
        /// 站台到站台
        /// </summary>
        public string StationToStationApp(string containerCode, string startStation, string endStation)
        {
            IStationTaskApp stationTaskApp = new IStationTaskApp(_unitWork, _auth, _context, _apprh);
            string str = stationTaskApp.StationTask(containerCode, startStation, endStation);
            return str;
        }
1454
1455
1456
1457
1458
1459
1460
1461


        // <summary>
        /// 通过站台返回库存
        /// </summary>
        public TableData StationToInventoryApp(string stationCode)
        {
            TableData tab = new TableData();
gy.huang authored
1462
            try
1463
            {
gy.huang authored
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
                Station station = _unitWork.Find<Station>(n => n.Code == stationCode && n.Containercode != "").FirstOrDefault();
                if (station == null)
                {
                    tab.data = null;
                }
                else
                {
                    List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == station.Containercode).ToList();
                    tab.data = inventories;
                }
1474
            }
gy.huang authored
1475
            catch (Exception ex)
1476
            {
gy.huang authored
1477
1478
                tab.code = 500;
                tab.msg = ex.Message;
1479
1480
1481
            }
            return tab;
        }
霍尔 authored
1482
1483
1484
    }
}