ReceiptHeaderApp.cs 49.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
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 ReceiptHeaderApp
    {
        private IUnitWork _unitWork;
        public IRepository<ReceiptHeader> _app;
        public IRepository<Inventory> _appi;
        public IRepository<Location> _appl;
        public IRepository<TaskDetail> _apptd;
        public IRepository<Task> _appt;
        public IRepository<ReceiptDetail> _apprd;
        private BaseDBContext _context;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public ReceiptHeaderApp(IUnitWork unitWork, BaseDBContext context, IRepository<Task> task, IRepository<Inventory> inventory, IRepository<Location> location, IRepository<TaskDetail> taskDetail, IRepository<ReceiptDetail> receiptDetail, IRepository<ReceiptHeader> repository, IHostingEnvironment hostingEnvironment)
        {
            _unitWork = unitWork;
            _app = repository;
            _appi = inventory;
            _appl = location;
            _apptd = taskDetail;
            _apprd = receiptDetail;
            _appt = task;
            _context = context;
            _hostingEnvironment = hostingEnvironment;
        }

        public ReceiptHeaderApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appi._loginInfo = loginInfo;
            _appl._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            _apprd._loginInfo = loginInfo;
            _appt._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, ReceiptHeader entity)
        {
            return _app.Load(pageRequest, entity);
        }

        public void Ins(ReceiptHeader entity)
        {
            entity.TotalQty = 0;
            entity.TotalLines = 0;
            entity.UploadStatus = 0;
            entity.FirstStatus = ReceiptHeaderStatus.新建;
            entity.LastStatus = ReceiptHeaderStatus.新建;
            entity.Station = entity.Station ?? "";

            entity.Code = _app.GetTaskNo(entity.Type);
            entity.SupplierName = _unitWork.FindSingle<Supplier>(u => u.Code.Equals(entity.SupplierCode)).Name;

            _app.Add(entity);
        }

        public void Upd(ReceiptHeader entity)
        {
            if (entity.FirstStatus != ReceiptHeaderStatus.新建 || entity.LastStatus != ReceiptHeaderStatus.新建)
            {
                throw new Exception("当前状态不可编辑!Id: " + entity.Id);
            }
            entity.Station = entity.Station ?? "";
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            foreach (var item in ids)
            {
                ReceiptHeader entity = _app.FindSingle(u => u.Id == item);
                if (entity.FirstStatus != ReceiptHeaderStatus.新建 || entity.LastStatus != ReceiptHeaderStatus.新建)
                {
                    throw new Exception("订单号:" + entity.Code + "进入订单池后,不允许删除");
                }
                _unitWork.Delete<ReceiptDetail>(u => u.ReceiptId.Equals(entity.Id));
                _app.Delete(entity);
            }
        }

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

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

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

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    ReceiptHeader 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(ReceiptHeader entity)
        {
            return _app.ExportData(entity);
        }

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

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

            return result;
        }

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

        public TableData FinishMaterialApp(TaskDetail taskDel)
        {
            TableData tab = new TableData();
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    TaskDetail task = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDel.ContainerCode && n.TaskNo == n.TaskNo && (n.Status == TaskStatus.已经到站台 || n.Status == TaskStatus.放货中) && n.Id == taskDel.Id).FirstOrDefault();
                    if (task != null)
                    {
                        task.HadQty += taskDel.HadQty;
                        task.UpdateBy = _app._loginInfo.Name;
                        if (task.HadQty < task.OderQty)
                        {
                            task.Status = TaskStatus.放货中;
                            _apptd.Update(task);
                            tab.code = 200;
                        }
                        else if (task.HadQty == task.OderQty)
                        {
                            task.Status = TaskStatus.放取货完成;
                            _apptd.Update(task);
                            tab.code = 200;
                        }
                        else
                        {
                            tab.code = 300;
                            tab.msg = "物料数量大于托盘分配数量";
                        }
                        Task Task = _unitWork.Find<Task>(n => n.TaskNo == taskDel.TaskNo).FirstOrDefault();
                        List<TaskDetail> tasks = _unitWork.Find<TaskDetail>(n => n.TaskNo == task.TaskNo).OrderByDescending(x => x.Status).ToList();
                        Task.FirstStatus = tasks.First().Status;
                        Task.LastStatus = tasks.Last().Status;
                        _appt.Update(Task);
                    }
                    else
                    {
                        tab.code = 300;
                        tab.msg = "未找到该任务明细";
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    tab.code = 300;
                    tab.msg = ex.Message;
                }
            }
            return tab;

        }

        public TableData GetPrintDetailData(string rpno)
        {
            var result = new TableData();
            var data = from rd in _unitWork.Find<ReceiptDetail>(u => u.ReceiptCode == rpno)
                       join mt in _unitWork.Find<Material>(null)
                       on rd.MaterialCode equals mt.Code
                       select new
                       {
                           rd.MaterialCode,
                           mt.Name,
                           rd.Batch,
                           rd.Lot,
                           rd.Qty,
                           PBarCode = rd.MaterialCode,
                       };
            result.data = data.ToList();
            result.count = data.Count();

            if (result.count == 0)
            {
                result.msg = "还未建立明细数据!";
            }
            return result;
        }

        public TableData AutoReceiptApp(List<ReceiptHeader> receiptHeaders)
        {
            TableData tab = new TableData();
            tab.code = 200;
            int dcount = 0;
            int hcount = 0;
            try
            {
                List<ReceiptDetail> receiptDetails = new List<ReceiptDetail>();
                foreach (ReceiptHeader receiptHeader in receiptHeaders)
                {
                    receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code && n.Status == ReceiptHeaderStatus.新建).ToList();
                    if (receiptDetails.Count != 0)
                    {
                        string str = "";
                        foreach (ReceiptDetail r in receiptDetails)
                        {
                            str = AddTaskInfo(r);
                            //添加任务和明细
                            if (str == "")
                            {
                                r.Status = ReceiptHeaderStatus.分配完成;
                                r.QtyDivided = r.Qty;
                                _apprd.Update(r);
                                dcount = dcount + 1;
                            }
                            else
                            {
                                tab.code = 300;
                                tab.msg += "单号:"+ r.ReceiptCode + "物料号:" + r.MaterialCode + ": " + str + "<br>";
                            }
                        }
                        hcount += receiptDetails.Count;
                        List<ReceiptDetail> listreceiptDetail = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).OrderByDescending(x => x.Status).ToList();
                        receiptHeader.FirstStatus = (short)listreceiptDetail.First().Status;
                        receiptHeader.LastStatus = (short)listreceiptDetail.Last().Status;
                        _app.Update(receiptHeader);
                    }
                }

                if (hcount == dcount)
                {
                    tab.code = 200;
                    tab.msg = dcount + "条订单分配完成!";
                }
                else
                {
                    tab.code = 300;
                    tab.msg += (receiptDetails.Count - dcount) + "条订单分配失败!";
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }

        //添加任务
        public 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;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    Inventory inv = (from inventory in _unitWork.Find<Inventory>(n => n.MaterialCode == receiptDetail.MaterialCode && n.ContainerStatus != ContainerStatus. && n.WarehouseType == receiptHeader.WarehouseType && n.TaskStatus == "idle")
                                     join container in _unitWork.Find<Container>(x => x.IsLock == 0)
                                     on inventory.ContainerCode equals container.Code
                                     select inventory).FirstOrDefault();
                    var sqty = receiptDetail.Qty;
                    if (inv != null)
                    {
                        taskDetail.TaskType = TaskType.补充入库;
                        taskDetail.ContainerQty = sqty;
                        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;
                        if (receiptDetail.Station != null && receiptDetail.Station != "")
                        {
                            taskDetail.Station = receiptDetail.Station;
                        }else if (receiptHeader.Station != null && receiptHeader.Station != "")
                        {
                            taskDetail.Station = receiptHeader.Station;
                        }

                        taskDetail.HadQty = 0;
                        Location loc = _unitWork.Find<Location>(n => n.Code == inv.LocationCode && n.IsStop == false).First();
                        if (loc != null)
                        {
                            taskDetail.SourceLocation = loc.Code;
                            taskDetail.DestinationLocation = receiptDetail.Station;
                            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.待下发任务;
                        taskDetail.CreateTime = DateTime.Now;
                        _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 = sqty;
                            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 = 0;
                            taskDetail.SourceLocation = location.Code;
                            taskDetail.DestinationLocation = receiptDetail.Station;
                            taskDetail.Roadway = location.Roadway;
                            taskDetail.Priority = 0;
                            if (receiptDetail.Station != null && receiptDetail.Station != "")
                            {
                                taskDetail.Station = receiptDetail.Station;
                            }
                            else if (receiptHeader.Station != null && receiptHeader.Station != "")
                            {
                                taskDetail.Station = receiptHeader.Station;
                            }
                            taskDetail.Status = TaskStatus.待下发任务;
                            taskDetail.CreateTime = DateTime.Now;
                            _apptd.Add(taskDetail);
                            //location.Status = LocationStatus.入库占用;
                            //_appl.UpdateByTracking(location);

                            //location.Status = LocationStatus.任务锁定中;
                            //_appl.UpdateByTracking(location);
                        }
                        else
                        {
                            Inventory invOther = (from inventory in _unitWork.Find<Inventory>(n => n.ContainerStatus != ContainerStatus. && n.WarehouseType == receiptHeader.WarehouseType && n.TaskStatus == "idle")
                                                  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 = sqty;
                                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;
                                if (receiptDetail.Station != null && receiptDetail.Station != "")
                                {
                                    taskDetail.Station = receiptDetail.Station;
                                }
                                else if (receiptHeader.Station != null && receiptHeader.Station != "")
                                {
                                    taskDetail.Station = receiptHeader.Station;
                                }
                                taskDetail.HadQty = 0;
                                Location loc = _unitWork.Find<Location>(n => n.Code == invOther.LocationCode && n.IsStop == false).First();
                                if (loc != null)
                                {
                                    taskDetail.SourceLocation = loc.Code;
                                    taskDetail.DestinationLocation = receiptDetail.Station;
                                    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.待下发任务;
                                taskDetail.CreateTime = DateTime.Now;
                                _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.待下发任务;
                            ptask.CreateTime = DateTime.Now;
                            _appt.Add(ptask);

                            ptaskDetail.TaskNo = taskNo;
                            ptaskDetail.OrderCode = taskNo;
                            ptaskDetail.TaskType = TaskType.容器出库;
                            ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                            ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                            ptaskDetail.DestinationLocation = receiptDetail.Station;
                            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;
                            }
                            ptaskDetail.CreateTime = DateTime.Now;
                            _apptd.Add(ptaskDetail);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    str = ex.Message;
                    return str;
                }
            }

            return str;
        }

        //手动添加入库栈板(组板)
        public TableData HandDivide_InventoryApp(ReceiptDetail r, Inventory inventory,int num)
        {
            TableData tab = new TableData();
            tab.code = 200;
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(n => n.Id == r.Id).FirstOrDefault();
                    ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(n => n.Code == receiptDetail.ReceiptCode).FirstOrDefault();
                    Inventory inv = _unitWork.Find<Inventory>(n => n.Id == inventory.Id && n.TaskStatus == "idle").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;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    if (inv != null)
                    {
                        //任务明细状态更新
                        taskDetail.TaskType = TaskType.补充入库;
                        taskDetail.ContainerQty = num;
                        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).FirstOrDefault();
                        taskDetail.Batch = receiptDetail.Batch;
                        taskDetail.Lot = receiptDetail.Lot;
                        taskDetail.OderQty = num;
                        if (receiptDetail.Station != null && receiptDetail.Station != "")
                        {
                            taskDetail.Station = receiptDetail.Station;
                        }
                        else if (receiptHeader.Station != null && receiptHeader.Station != "")
                        {
                            taskDetail.Station = receiptHeader.Station;
                        }
                        taskDetail.HadQty = 0;
                        Location loc = _unitWork.Find<Location>(n => n.Code == inv.LocationCode && n.IsStop == false).FirstOrDefault();
                        taskDetail.SourceLocation = loc.Code;
                        taskDetail.DestinationLocation = receiptDetail.Station;
                        taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == inv.LocationCode && n.IsStop == false).Select(a => a.Roadway).First();;
                        taskDetail.Priority = 0;
                        taskDetail.Status = TaskStatus.待下发任务;
                        taskDetail.CreateTime = DateTime.Now;
                        _apptd.Add(taskDetail);
                        //入库明细状态更新
                        receiptDetail.Status = ReceiptHeaderStatus.分配完成;
                        receiptDetail.QtyDivided += num;
                        _apprd.Update(receiptDetail);
                        List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).OrderByDescending(x => x.Status).ToList();
                        receiptHeader.FirstStatus = (short)receiptDetails.First().Status;
                        receiptHeader.LastStatus = (short)receiptDetails.Last().Status;
                        tab.msg = "分配成功";
                        _app.Update(receiptHeader);

                        //建立容器出库任务
                        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.待下发任务;
                                ptask.CreateTime = DateTime.Now;
                                _appt.Add(ptask);

                                ptaskDetail.TaskNo = taskNo;
                                ptaskDetail.OrderCode = taskNo;
                                ptaskDetail.TaskType = TaskType.容器出库;
                                ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                                ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                                ptaskDetail.DestinationLocation = receiptDetail.Station;
                                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;
                                }
                                ptaskDetail.CreateTime = DateTime.Now;
                                _apptd.Add(ptaskDetail);
                            }
                        }
                    }

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

        //手动添加入库栈板(空板)
        public TableData HandDivide_LocationApp(ReceiptDetail r, Location loc, int num)
        {
            TableData tab = new TableData();
            tab.code = 200;
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    //任务明细状态更新
                    ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(n => n.Id == r.Id).FirstOrDefault();
                    ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(n => n.Code == receiptDetail.ReceiptCode).FirstOrDefault();
                    Location location = _unitWork.Find<Location>(n => n.Id == loc.Id && n.IsStop == false).FirstOrDefault();
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();
                    var tno = "";
                    int tcount = _unitWork.Find<Task>(n => n.OrderCode == receiptDetail.ReceiptCode).Count();
                    var  ma = _unitWork.Find<Material>(n => n.Code == receiptDetail.MaterialCode).Count();
                    if (ma < 1) {
                        tab.code = 300;
                        tab.msg = "没有"+ receiptDetail.MaterialCode + "此物料,请先添加物料";
                        return tab;
                    }
                    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;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    if (location != null)
                    {
                        taskDetail.TaskType = TaskType.整盘入库;
                        taskDetail.ContainerQty = num;
                        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 = num;
                        taskDetail.HadQty = 0;
                        taskDetail.SourceLocation = location.Code;
                        taskDetail.DestinationLocation = receiptDetail.Station;
                        taskDetail.Roadway = location.Roadway;
                        taskDetail.Priority = 0;
                        if (receiptDetail.Station != null && receiptDetail.Station != "")
                        {
                            taskDetail.Station = receiptDetail.Station;
                        }
                        else if (receiptHeader.Station != null && receiptHeader.Station != "")
                        {
                            taskDetail.Station = receiptHeader.Station;
                        }
                        taskDetail.Status = TaskStatus.待下发任务;
                        taskDetail.CreateTime = DateTime.Now;
                        _apptd.Add(taskDetail);
                        receiptDetail.Status = ReceiptHeaderStatus.分配完成;
                        receiptDetail.QtyDivided += num;
                        _apprd.Update(receiptDetail);
                        List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).OrderByDescending(x => x.Status).ToList();
                        receiptHeader.FirstStatus = (short)receiptDetails.First().Status;
                        receiptHeader.LastStatus = (short)receiptDetails.Last().Status;
                        _app.Update(receiptHeader);
                        location.Status = LocationStatus.入库占用;
                        _appl.Update(location);
                        tab.msg = "分配成功";

                        //建立容器出库任务
                        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.待下发任务;
                                ptask.CreateTime = DateTime.Now;
                                _appt.Add(ptask);

                                ptaskDetail.TaskNo = taskNo;
                                ptaskDetail.OrderCode = taskNo;
                                ptaskDetail.TaskType = TaskType.容器出库;
                                ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                                ptaskDetail.SourceLocation = receiptDetail.Station; ;
                                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;
                                }
                                ptaskDetail.CreateTime = DateTime.Now;
                                _apptd.Add(ptaskDetail);
                            }
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    tab.code = 300;
                    tab.msg += ex.Message;
                }
            }
            return tab;
        }

        //查询类型为1和6,只允许出的站台
        public TableData LoadStation(List<Inventory> list)
        {
            TableData result = new TableData();
            result.code = 200;
            //var s = list.GroupBy(n => n.LocationCode.Substring(0,2)).FirstOrDefault();
            int count = list.GroupBy(n => n.LocationCode.Substring(0, 1)).Count();
            if (count > 1)
            {
                result.code = 300;
                return result;
            }
            int? roadway = _unitWork.Find<Location>(n => n.Code == list.First().LocationCode).Select(a => a.Roadway).FirstOrDefault();
            List<Station> StationLoad = null;
            if (roadway == null)
            {
                StationLoad = (from stat in _unitWork.Find<Station>(n => (n.Type == 1 || n.Type == 6 || n.Type == 11 || n.Type == 12) && n.IsStop != 1 && n.IsOut == 1)
                                             join statto in _unitWork.Find<StationToStation>(a => a.StartStation == list.First().LocationCode)
                                             on stat.Code equals statto.EndStation
                                             select stat).ToList();
                result.count = StationLoad.Count();
            }
            else 
            {
                StationLoad = (from stat in _unitWork.Find<Station>(n => (n.Type == 1 || n.Type == 6 || n.Type == 11 || n.Type == 12) && n.IsStop != 1 && n.IsOut == 1)
                               join statrow in _unitWork.Find<StationRoadway>(a => a.RoadWay == roadway)
                               on stat.Code equals statrow.StationCode
                               select stat).ToList();
                result.count = StationLoad.Count();
            }
            if (roadway == 2)
            {
                Station station = _unitWork.Find<Station>(n => n.Code == "EntranceStationD02(P1004)").FirstOrDefault();
                StationLoad.Add(station);
            }

            result.data = StationLoad;
            return result;
        }

        //查询类型为1,只允许出的站台
        public TableData LoadStationOne()
        {
            TableData tab = new TableData();
            tab.code = 200;
            List<Station> StationLoad = _unitWork.Find<Station>(n => n.Type == 1 && n.IsStop != 1 && n.IsOut == 1 ).ToList();
            tab.data = StationLoad;
            tab.count = StationLoad.Count();
            return tab;
        }

        //通过起始站台请求终点站台
        public TableData StartStationEnd(string StartStation)
        {
            TableData tab = new TableData();
            tab.code = 200;
            try
            {
                List<Station> EndStation = (from station in _unitWork.Find<Station>(null)
                                                     join sts in _unitWork.Find<StationToStation>(n => n.StartStation == StartStation)
                                                     on station.Code equals sts.EndStation
                                                     select station).ToList();
                tab.data = EndStation;
            }
            catch (Exception ex)
            {
                tab.code = 500;
                tab.msg = ex.Message;
            }
            return tab;
        }

        //添加手动入库任务并选站台
        public TableData AddReceiptTaskApp(string station, List<ReceiptDetail> receiptDels)
        {
            TableData tab = new TableData();
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    string str = "";
                    foreach (ReceiptDetail r in receiptDels)
                    {
                       str = CreateTask(r, station);
                        if (!string.IsNullOrEmpty(str))
                        {
                            throw new Exception(str);
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    tab.code = 500;
                    tab.msg = ex.Message;
                }
                return tab;
            }
        }

        //手动入库
        public string CreateTask(ReceiptDetail r, string station)
        {
            string str = "";
                try
                {
                    ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(n => n.Id == r.Id).FirstOrDefault();
                    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;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    Station sta = _unitWork.Find<Station>(n => n.Code == station && n.Containercode != "").FirstOrDefault();
                    if (sta != null)
                    {
                        //任务明细状态更新
                        taskDetail.TaskType = TaskType.补充入库;
                        taskDetail.ContainerQty = receiptDetail.Qty;
                        taskDetail.TaskNo = tno;
                        taskDetail.SourceCode = receiptDetail.SourceCode;
                        taskDetail.OrderCode = receiptDetail.ReceiptCode;
                        taskDetail.ContainerCode = sta.Containercode;

                        taskDetail.MaterialCode = receiptDetail.MaterialCode;
                        taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == receiptDetail.MaterialCode).Select(a => a.Name).FirstOrDefault();
                        taskDetail.Batch = receiptDetail.Batch;
                        taskDetail.Lot = receiptDetail.Lot;
                        taskDetail.OderQty = receiptDetail.Qty;
                        taskDetail.Station = sta.Code;
                        taskDetail.HadQty = 0;
                        taskDetail.SourceLocation = sta.Code;
                        taskDetail.Roadway = _unitWork.Find<StationRoadway>(n => n.StationCode == sta.Code).Select(a => a.RoadWay).FirstOrDefault(); ;
                        taskDetail.Priority = 0;
                        taskDetail.Status = TaskStatus.已经到站台;
                        taskDetail.CreateTime = DateTime.Now;
                        _apptd.Add(taskDetail);
                        //入库明细状态更新
                        receiptDetail.Status = ReceiptHeaderStatus.分配完成;
                        receiptDetail.QtyDivided += receiptDetail.Qty;
                        _apprd.Update(receiptDetail);
                        List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.ReceiptCode == receiptHeader.Code).OrderByDescending(x => x.Status).ToList();
                        receiptHeader.FirstStatus = (short)receiptDetails.First().Status;
                        receiptHeader.LastStatus = (short)receiptDetails.Last().Status;
                        _app.UpdateByTracking(receiptHeader);
                    }
                    else
                    {
                        str = station + "未找到该站台或者该站台无栈板占用!";
                    }
                }
                catch (Exception ex)
                {
                    str += ex.Message;
                }
            return str;
        }

        //通过起始站台请求终点站台
        public TableData SendBack(string SourceCode)
        {
            TableData tab = new TableData();
            tab.code = 200;
            try
            {
                InterfaceLog invter = _unitWork.Find<InterfaceLog>(n => n.TaskNo == SourceCode).FirstOrDefault();
                if (invter != null)
                {
                    invter.Status = 2;
                    _unitWork.Update(invter);
                }
                else
                {
                    tab.code = 300;
                    tab.msg = "未找到任务号为:"+ SourceCode + "的入库接口记录";
                    return tab;
                }
            }
            catch (Exception ex)
            {
                tab.code = 500;
                tab.msg = ex.Message;
            }
            return tab;
        }
    }
}