SampleService.cs 73.3 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 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
using Infrastructure;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using WebApp;
using WebMvc.Areas.WebService;
using WebRepository;

namespace WebMvc.Areas.WebService
{
    public class SampleService : ISampleService
    {
        private IUnitWork _unitWork;
        public IRepository<WebRepository.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 IAuth _auth;
        private object stationTaskApp;

        public SampleService(IUnitWork unitWork, IAuth auth, IRepository<Inventory> inventory, IRepository<InventoryTransaction> inventoryTransaction, IRepository<Container> Icontainer, IRepository<ReceiptHeader> receiptHeader, IRepository<ReceiptDetail> receiptDetail, IRepository<WebRepository.Task> repository, IRepository<Location> repositoryl, IRepository<ShipmentHeader> repositoryh, IRepository<TaskDetail> repositorytd, IRepository<ShipmentDetail> repositorysd, BaseDBContext context)
        {
            _unitWork = unitWork;
            _app = repository;
            _appl = repositoryl;
            _apph = repositoryh;
            _appsd = repositorysd;
            _apptd = repositorytd;
            _apprd = receiptDetail;
            _apprh = receiptHeader;
            _appc = Icontainer;
            _appi = inventory;
            _appit = inventoryTransaction;
            _context = context;
            _auth = auth;
            LoginInfo loginInfo = new LoginInfo
            {
                Account = "NouYaWeb"
            };
            SetLoginInfo(loginInfo);
        }

        public void 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;
        }
        //入库
        public string LK_In(string Request)
        {
            XmlDocument doc = new XmlDocument();
            ConfigXmlDocument reDoc = new ConfigXmlDocument();
            //读取传入的xml
            var config = AppSettingsJson.GetAppSettings();
            reDoc.Load(config.GetSection("NouYaWebInService:Url").Value);

            //取出报文接收状态字段
            XmlElement element = (XmlElement)reDoc.SelectSingleNode("//Status");
            string name = "";
            string value = "";
            string UpperTaskNo = "";
            string SourceCode = "";
            string TaskType = "";
            string CustomerCode = "";
            string MaterialCode = "";
            string SupplierCode = "";
            decimal? Num = 0;
            string WareCell = "";
            string Code = "";
            var NewCode = "";
            //站台
            var Station = "";
            try
            {
                doc.LoadXml(Request);
                XmlNodeList RecordSets = doc.SelectNodes("//Record");
                using (var tran = _context.Database.BeginTransaction())
                {
                    foreach (XmlNode RecordSet in RecordSets)
                    {
                        XmlNodeList nodes = RecordSet.ChildNodes;
                        foreach (XmlElement node in nodes)
                        {
                            //抓取单个的xml节点
                            //获得字节里面的属性值
                            name = node.GetAttribute("name");
                            value = node.GetAttribute("value");
                            //上游任务号
                            if (name == "tc_dcf000" && !string.IsNullOrEmpty(value))
                            {
                                UpperTaskNo = value;
                            }
                            //上游作业类型
                            else if (name == "tc_dcf001" && !string.IsNullOrEmpty(value))
                            {
                                TaskType = value;
                            }
                            //上游系统单号
                            else if (name == "tc_dcf003" && !string.IsNullOrEmpty(value))
                            {
                                SourceCode = value;
                            }
                            //供应商
                            else if (name == "dcf004" && !string.IsNullOrEmpty(value))
                            {
                                SupplierCode = value;
                            }
                            //提取物料号
                            else if (name == "tc_dcf016" && !string.IsNullOrEmpty(value))
                            {
                                MaterialCode = value;
                            }
                            //入库数量
                            else if (name == "tc_dcf021" && !string.IsNullOrEmpty(value))
                            {
                                Num = decimal.Parse(value);
                            }
                            //条码
                            else if (name == "tc_dcf025" && !string.IsNullOrEmpty(value))
                            {
                                Code = value;
                            }
                            //仓库
                            else if (name == "tc_dcf028" && !string.IsNullOrEmpty(value))
                            {
                                WareCell = value;
                            }
                            //站台
                            else if (name == "tc_dcf035" && !string.IsNullOrEmpty(value))
                            {
                                Station = value;
                            }
                        }
                        switch (TaskType)
                        {
                            case "D111":
                                TaskType = "采购入库";
                                break;
                            case "D113":
                                TaskType = "多角采购入库";
                                break;
                            case "D311":
                                TaskType = "杂收";
                                break;
                            case "D411":
                                TaskType = "完工入库";
                                break;
                            case "D521":
                                TaskType = "销退";
                                break;
                            case "D621":
                                TaskType = "仓库件直接调拨";
                                break;
                            case "D701":
                                TaskType = "余料退库";
                                break;
                            default:
                                break;
                        }
                        switch (WareCell)
                        {
                            case "B0030":
                                WareCell = "YCL_WareCell";
                                break;
                            case "B1011":
                                WareCell = "PPJ_WareCell";
                                break;
                            case "B0020":
                                WareCell = "PPP_WareCell";
                                break;
                            case "B1031":
                                WareCell = "TB_WareCell";
                                break;
                            case "B0010":
                                WareCell = "CP_WareCell";
                                break;
                            default:
                                break;
                        }
                        //if (WareCell == "B1032")
                        //{
                        //    WareCell = "YCL_WareCell";
                        //}
                        //else if (WareCell == "B1011")
                        //{
                        //    WareCell = "PPJ_WareCell";
                        //}
                        //else if (WareCell == "B1020")
                        //{
                        //    WareCell = "PPP_WareCell";
                        //}
                        //else if (WareCell == "B1031")
                        //{
                        //    WareCell = "TB_WareCell";
                        //}
                        //else if (WareCell == "B1010")
                        //{
                        //    WareCell = "CP_WareCell";
                        //}
                        if (Code == "")
                        {
                            tran.Rollback();
                            throw new Exception("条码为空,无法创建任务!");
                        }
                        else
                        {
                            #region 保存入库单主表
                            ReceiptHeader receiptHeader = _unitWork.FindSingle<ReceiptHeader>(u => u.SourceCode.Equals(UpperTaskNo + "&" + SourceCode));
                            if (receiptHeader != null)
                            {
                                if (receiptHeader.FirstStatus != ReceiptHeaderStatus.新建)
                                {
                                    throw new Exception("单据进入订单池后,不允许修改!");
                                }
                            }
                            else
                            {
                                NewCode = _unitWork.GetTaskNo(TaskNo.入库手动分配);
                                receiptHeader = new ReceiptHeader();
                                receiptHeader.Type = TaskType;
                                receiptHeader.SourceCode = UpperTaskNo + "&" + SourceCode;
                                receiptHeader.Station = Station;
                                receiptHeader.WarehouseType = WareCell;
                                receiptHeader.SupplierCode = SupplierCode;
                                receiptHeader.CreateTime = DateTime.Now;
                                receiptHeader.CreateBy = "Erp";
                                receiptHeader.UploadStatus = 0;

                                receiptHeader.Code = _unitWork.GetTaskNo(TaskNo.入库手动分配);
                                receiptHeader.FirstStatus = ReceiptHeaderStatus.新建;
                                receiptHeader.LastStatus = ReceiptHeaderStatus.新建;
                                receiptHeader.TotalLines = 0;
                                receiptHeader.TotalQty = 0;
                            }

                            if (receiptHeader.Id == null)
                            {
                                _apprh.Add(receiptHeader);
                            }
                            #endregion

                            #region 保存入库单子表
                            ReceiptDetail receiptDetail = new ReceiptDetail();

                            receiptDetail.SourceCode = UpperTaskNo + "&" + SourceCode;
                            receiptDetail.Station = "";
                            receiptDetail.MaterialCode = MaterialCode;
                            receiptDetail.Qty = Num;
                            receiptDetail.CreateTime = receiptHeader.CreateTime;
                            receiptDetail.CreateBy = receiptHeader.CreateBy;
                            //任务类型
                            receiptDetail.MoCode = TaskType;
                            receiptDetail.ReceiptId = receiptHeader.Id;
                            receiptDetail.ReceiptCode = receiptHeader.Code;
                            receiptDetail.QtyDivided = 0;
                            receiptDetail.QtyCompleted = 0;
                            receiptDetail.Price = 0;
                            receiptDetail.Project = Code;
                            receiptDetail.Status = ReceiptHeaderStatus.新建;

                            _apprd.Add(receiptDetail);

                            #endregion

                            #region 更新入库单主表汇总信息
                            receiptHeader.TotalLines += 1;
                            receiptHeader.TotalQty += receiptDetail.Qty;
                            if (receiptHeader.UpdateBy == null)
                            {
                                receiptHeader.UpdateBy = "wms";
                                receiptHeader.UpdateTime = DateTime.Now;
                            }
                            _apprh.UpdateByTracking(receiptHeader);
                            #endregion

                            #region 暂存台回库逻辑
                            //if (Station != "")
                            //{
                            //    Station sta = _unitWork.Find<Station>(n => n.Code == Station && n.Type == StationType.暂存区站台).FirstOrDefault();
                            //    if (sta != null)
                            //    {
                            //        if (string.IsNullOrEmpty(sta.Containercode))
                            //        {
                            //            tran.Rollback();
                            //            throw new Exception("Wms暂存台未找到托盘");
                            //        }
                            //        else
                            //        {
                            //            Inventory inventory = _unitWork.Find<Inventory>(n => n.QrCode == Code).FirstOrDefault();
                            //            if (inventory == null)
                            //            {
                            //                tran.Rollback();
                            //                throw new Exception("未找到该库存 qrcode:" + Code);
                            //            }
                            //            else
                            //            {
                            //                inventory.SourceCode = UpperTaskNo + "&" + SourceCode;
                            //                inventory.Qty = Num;
                            //                _unitWork.Update(inventory);
                            //                if (_unitWork.IsExist<TaskDetail>(n => n.ContainerCode == sta.Containercode))
                            //                {
                            //                    //建立回库任务
                            //                    WebRepository.Task task = new WebRepository.Task();
                            //                    TaskDetail taskDetail = new TaskDetail();
                            //                    var taskNo = Station + _app.GetTaskNo(TaskNo.容器回库);
                            //                    StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == Station).FirstOrDefault();
                            //                    if (stationRoadway == null)
                            //                    {
                            //                        tran.Rollback();
                            //                        throw new Exception("站台未找到巷道 station:" + Station);
                            //                    }
                            //                    Location loc = null;
                            //                    if (loc == null)
                            //                    {
                            //                        if (stationRoadway.RoadWay == 4)
                            //                        {
                            //                            while (true)
                            //                            {
                            //                                if (stationRoadway.StationPlace == StationPlace.巷道北面)
                            //                                {
                            //                                    loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "").OrderBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                }
                            //                                else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                            //                                {
                            //                                    loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "").OrderBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenByDescending(y => y.Row).FirstOrDefault();
                            //                                }

                            //                                //更新仓位对应的容器,如果回库分配的不是原仓位则更新仓位且锁定
                            //                                if (loc == null)
                            //                                {
                            //                                    tran.Rollback();
                            //                                    throw new Exception("未找到仓位");
                            //                                }
                            //                                else
                            //                                {
                            //                                    int i = _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                            //                                    {
                            //                                        Status = LocationStatus.任务锁定中,
                            //                                        ContainerCode = sta.Containercode,
                            //                                        UpdateTime = DateTime.Now
                            //                                    });
                            //                                    if (i == 1)
                            //                                    {
                            //                                        break;
                            //                                    }
                            //                                }
                            //                            }
                            //                        }
                            //                        else if (stationRoadway.RoadWay == 5)
                            //                        {
                            //                            Container container = _unitWork.Find<Container>(n => n.Code == sta.Containercode).FirstOrDefault();
                            //                            while (true)
                            //                            {
                            //                                Inventory inve = _unitWork.Find<Inventory>(n => n.ContainerCode == container.Code).FirstOrDefault();
                            //                                if (inve != null)
                            //                                {
                            //                                    //按照区域分库位 静态动态
                            //                                    List<LocationDistribution> locationDistributions = _unitWork.Find<LocationDistribution>(n => n.RoadWay == stationRoadway.RoadWay).ToList();
                            //                                    if (locationDistributions.Count != 0)
                            //                                    {
                            //                                        if (inventory != null)
                            //                                        {
                            //                                            foreach (LocationDistribution locationDistribution in locationDistributions)
                            //                                            {
                            //                                                if (inventory.NameDescription.Substring((int)locationDistribution.ContrastStart - 1, (int)locationDistribution.Length) == locationDistribution.ContrastName)
                            //                                                {
                            //                                                    if (stationRoadway.StationPlace == StationPlace.五巷道北面)
                            //                                                    {
                            //                                                        if (container.Type == ContainerType.木栈板母板_2)
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                               && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                        }
                            //                                                        else
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                            && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();

                            //                                                            if (loc == null)
                            //                                                            {
                            //                                                                loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                                && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2).OrderBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                            }
                            //                                                        }
                            //                                                    }
                            //                                                    else if (stationRoadway.StationPlace == StationPlace.六巷道北面)
                            //                                                    {
                            //                                                        if (container.Type == ContainerType.木栈板母板_2)
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                               && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                        }
                            //                                                        else
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                                   && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                            if (loc == null)
                            //                                                            {
                            //                                                                loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                                && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2).OrderBy(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                            }
                            //                                                        }
                            //                                                    }
                            //                                                    else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                            //                                                    {
                            //                                                        if (container.Type == ContainerType.木栈板母板_2)
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                               && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                        }
                            //                                                        else
                            //                                                        {
                            //                                                            loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                                   && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2)
                            //                                                                   join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                                   on loca.ContiguousCode equals Cloca.Code
                            //                                                                   select loca).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                            if (loc == null)
                            //                                                            {
                            //                                                                loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.Line == locationDistribution.PointX && n.Row >= locationDistribution.PointY_1 && n.Row <= locationDistribution.PointY_2
                            //                                                                && n.Layer >= locationDistribution.PointZ_1 && n.Layer <= locationDistribution.PointZ_2).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                            }
                            //                                                        }
                            //                                                    }

                            //                                                    if (loc != null)
                            //                                                    {
                            //                                                        break;
                            //                                                    }
                            //                                                    else if (loc == null && locationDistribution.Type == DistributionType.静态库位)
                            //                                                    {
                            //                                                        tran.Rollback();
                            //                                                        throw new Exception("未找到仓位(分配仓位为静态)");
                            //                                                    }
                            //                                                }
                            //                                            }
                            //                                        }
                            //                                    }

                            //                                    if (loc == null)
                            //                                    {
                            //                                        if (stationRoadway.StationPlace == StationPlace.五巷道北面)
                            //                                        {
                            //                                            if (container.Type == ContainerType.木栈板母板_2)
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                            }
                            //                                            else
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                if (loc == null)
                            //                                                {
                            //                                                    loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                }
                            //                                            }
                            //                                        }
                            //                                        else if (stationRoadway.StationPlace == StationPlace.六巷道北面)
                            //                                        {
                            //                                            if (container.Type == ContainerType.木栈板母板_2)
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                            }
                            //                                            else
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                if (loc == null)
                            //                                                {
                            //                                                    loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => (n.Code.Contains("EC-") || n.Code.Contains("ED-")) ? 0 : 1).ThenBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                }
                            //                                            }
                            //                                        }
                            //                                        else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                            //                                        {
                            //                                            if (container.Type == ContainerType.木栈板母板_2)
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                            }
                            //                                            else
                            //                                            {
                            //                                                loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位)
                            //                                                       join Cloca in _unitWork.Find<Location>(n => n.Status != LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay)
                            //                                                       on loca.ContiguousCode equals Cloca.Code
                            //                                                       select loca).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenBy(n => (n.Code.Contains("EA-") || n.Code.Contains("EB-")) ? 0 : 1).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                if (loc == null)
                            //                                                {
                            //                                                    loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(b => b.MaxHeight).ThenBy(n => n.DistributionType).ThenByDescending(a => a.Row).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //                                                }
                            //                                            }
                            //                                        }
                            //                                    }
                            //                                }


                            //                                //更新仓位对应的容器,如果回库分配的不是原仓位则更新仓位且锁定
                            //                                if (loc == null)
                            //                                {
                            //                                    tran.Rollback();
                            //                                    throw new Exception("未找到仓位");
                            //                                }
                            //                                else
                            //                                {
                            //                                    if (_unitWork.IsExist<TaskDetail>(a => (a.DestinationLocation == loc.ContiguousCode || a.SourceLocation == loc.ContiguousCode) && a.Roadway == 1))
                            //                                    {
                            //                                        continue;
                            //                                    }
                            //                                    int i = _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                            //                                    {
                            //                                        Status = LocationStatus.任务锁定中,
                            //                                        ContainerCode = container.Code,
                            //                                        UpdateTime = DateTime.Now
                            //                                    });

                            //                                    container = _unitWork.Find<Container>(n => n.Code == container.Code).FirstOrDefault();
                            //                                    if (container != null && loc != null)
                            //                                    {
                            //                                        if (container.Type == ContainerType.木栈板母板_2)
                            //                                        {
                            //                                            Location Cloc = _unitWork.Find<Location>(u => u.Code == loc.ContiguousCode).FirstOrDefault();
                            //                                            if (Cloc != null)
                            //                                            {
                            //                                                int j = _unitWork.Update<Location>(n => n.Id == Cloc.Id && n.Version == Cloc.Version, n => new Location
                            //                                                {
                            //                                                    ContainerCode = container.Code,
                            //                                                    Status = LocationStatus.任务锁定中,
                            //                                                    UpdateBy = "wms",
                            //                                                    UpdateTime = DateTime.Now
                            //                                                });
                            //                                                if (j != 1)
                            //                                                {
                            //                                                    tran.Rollback();
                            //                                                    throw new Exception("邻仓位更改失败");
                            //                                                }
                            //                                            }
                            //                                        }
                            //                                    }
                            //                                    if (i == 1)
                            //                                    {
                            //                                        break;
                            //                                    }
                            //                                }
                            //                            }
                            //                        }
                            //                    }

                            //                    task.TaskNo = taskNo;
                            //                    task.OrderCode = taskNo;
                            //                    task.BusinessType = BusinessType.入库_其他入库单;
                            //                    task.FirstStatus = WebRepository.TaskStatus.待下发任务;
                            //                    task.LastStatus = WebRepository.TaskStatus.待下发任务;
                            //                    _app.Add(task);

                            //                    taskDetail.TaskNo = taskNo;
                            //                    taskDetail.OrderCode = taskNo;
                            //                    taskDetail.TaskType = WebRepository.TaskType.容器回库;
                            //                    taskDetail.ContainerCode = sta.Containercode;
                            //                    taskDetail.SourceLocation = Station;
                            //                    if (loc == null)
                            //                    {
                            //                        taskDetail.DestinationLocation = _unitWork.Find<Location>(n => n.Roadway == stationRoadway.RoadWay).Select(u => u.Type).FirstOrDefault();
                            //                    }
                            //                    else
                            //                    {
                            //                        taskDetail.DestinationLocation = loc.Code;
                            //                    }
                            //                    taskDetail.OderQty = 0;
                            //                    taskDetail.ContainerQty = 0;
                            //                    taskDetail.HadQty = 0;
                            //                    taskDetail.Roadway = stationRoadway.RoadWay;
                            //                    taskDetail.Station = Station;
                            //                    taskDetail.Status = WebRepository.TaskStatus.待下发任务;
                            //                    taskDetail.Priority = 99;
                            //                    taskDetail.MaterialCode = MaterialCode;
                            //                    _apptd.Add(taskDetail);
                            //                }
                            //            }
                            //        }
                            //    }
                            //    else
                            //    {
                            //        tran.Rollback();
                            //        throw new Exception("该站台不是暂存区 Station:" + Station);
                            //    }
                            //}
                            #endregion
                        }
                    }
                    InterfaceLog Oldinter = _unitWork.Find<InterfaceLog>(n => n.TaskNo == (UpperTaskNo + "&" + SourceCode) && n.Status != 4).FirstOrDefault();
                    if (Oldinter != null)
                    {
                        tran.Rollback();
                        throw new Exception("重复下发");
                    }
                    else
                    {
                        InterfaceLog inter = new InterfaceLog
                        {
                            Type = "LK_In",
                            AllNum = RecordSets.Count,
                            ComNum = 0,
                            Request = Request,
                            Status = 0,
                            TaskNo = UpperTaskNo + "&" + SourceCode,
                            CreateTime = DateTime.Now,
                            CreateBy = "WMS"
                        };
                        _unitWork.Add(inter);
                    }
                    element.SetAttribute("code", "0");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                element.SetAttribute("code", "1");
                element.SetAttribute("description", ex.Message);
                InterfaceLog ErrorInter = new InterfaceLog
                {
                    Type = "入库创建报错",
                    AllNum = 0,
                    ComNum = 0,
                    Request = Request,
                    Status = 4,
                    Message = ex.Message,
                    TaskNo = UpperTaskNo + "&" + SourceCode,
                    CreateTime = DateTime.Now,
                    CreateBy = "WMS"
                };
                _unitWork.Add(ErrorInter);
            }
            return reDoc.InnerXml;
        }
        //出库
        public string LK_Out(string Request)
        {
            XmlDocument doc = new XmlDocument();
            ConfigXmlDocument reDoc = new ConfigXmlDocument();
            //读取传入的xml
            var config = AppSettingsJson.GetAppSettings();
            reDoc.Load(config.GetSection("NouYaWebInService:Url").Value);
            //取出报文接收状态字段
            XmlElement element = (XmlElement)reDoc.SelectSingleNode("//Status");
            string name = "";
            string value = "";
            string SourceCode = "";
            string UpperTaskNo = "";
            string MaterialCode = "";
            string shipTo = "";
            string Station = "";
            string NewStation = "";
            decimal Num = 0;
            string WareCell = "";
            string Location = "";
            try
            {
                doc.LoadXml(Request);
                XmlNodeList RecordSets = doc.SelectNodes("//Record");
                using (var tran = _context.Database.BeginTransaction())
                {
                    foreach (XmlNode RecordSet in RecordSets)
                    {
                        XmlNodeList nodes = RecordSet.ChildNodes;
                        foreach (XmlElement node in nodes)
                        {
                            //抓取单个的xml节点
                            //获得字节里面的属性值
                            name = node.GetAttribute("name");
                            value = node.GetAttribute("value");
                            //上游系统单号
                            if (name == "tc_dfa000" && !string.IsNullOrEmpty(value))
                            {
                                UpperTaskNo = value;
                            }
                            else if (name == "tc_dfa003" && !string.IsNullOrEmpty(value))
                            {
                                SourceCode = value;
                            }
                            //供应商
                            else if (name == "tc_dfa004" && !string.IsNullOrEmpty(value))
                            {
                                shipTo = value;
                            }
                            //物料号
                            else if (name == "tc_dfa023" && !string.IsNullOrEmpty(value))
                            {
                                MaterialCode = value;
                            }
                            //出库数量
                            else if (name == "tc_dfa025" && !string.IsNullOrEmpty(value))
                            {
                                Num = decimal.Parse(value);
                            }
                            //仓库
                            else if (name == "tc_dfa027" && !string.IsNullOrEmpty(value))
                            {
                                WareCell = value;
                            }
                            //站台
                            else if (name == "tc_dfa030" && !string.IsNullOrEmpty(value))
                            {
                                Station = value;
                            }
                            //库位
                            else if (name == "tc_dfa037" && !string.IsNullOrEmpty(value))
                            {
                                Location = value;
                            }
                        }
                        //不良品出库,自动分配出库站台
                        if (Station == "B0080")
                        {
                            Station sta = _unitWork.Find<Station>(n => (n.Code == "EntranceStationA01" || n.Code == "EntranceStationA02") && n.Containercode == "").FirstOrDefault();
                            if (sta != null)
                            {
                                Station = sta.Code;
                            }
                            else
                            {
                                List<TaskDetail> tads1 = _unitWork.Find<TaskDetail>(n => n.Station == "EntranceStationA01").ToList();
                                List<TaskDetail> tads2 = _unitWork.Find<TaskDetail>(n => n.Station == "EntranceStationA02").ToList();
                                if (tads1.Count > tads2.Count)
                                {
                                    Station = "EntranceStationA02";
                                }
                                else
                                {
                                    Station = "EntranceStationA01";
                                }
                            }
                            WareCell = "YCL_WareCell";
                        }
                        else
                        {
                            StationRoadway Staroadway = _unitWork.Find<StationRoadway>(n => n.StationCode == Station).FirstOrDefault();
                            if (Staroadway != null)
                            {
                                if (Staroadway.RoadWay == 1)
                                {
                                    WareCell = "YCL_WareCell";
                                }
                                else if (Staroadway.RoadWay == 2)
                                {
                                    WareCell = "PPJ_WareCell";
                                }
                                else if (Staroadway.RoadWay == 3)
                                {
                                    WareCell = "PPP_WareCell";
                                }
                                else if (Staroadway.RoadWay == 4)
                                {
                                    WareCell = "TB_WareCell";
                                }
                                else if (Staroadway.RoadWay == 5)
                                {
                                    WareCell = "CP_WareCell";
                                }
                            }
                        }

                        #region 保存出库单主表
                        ShipmentHeader shipmentheader = _unitWork.FindSingle<ShipmentHeader>(u => u.SourceCode.Equals(UpperTaskNo + "&" + SourceCode) && u.Station == Station);
                        if (shipmentheader != null)
                        {
                            if (shipmentheader.FirstStatus != ReceiptHeaderStatus.新建)
                            {
                                tran.Rollback();
                                throw new Exception("单据进入订单池后,不允许修改!");
                            }
                        }
                        else
                        {
                            shipmentheader = new ShipmentHeader
                            {
                                Type = BusinessType.出库_其他出库单,
                                SourceCode = UpperTaskNo + "&" + SourceCode,
                                Station = Station,
                                WarehouseType = WareCell,
                                UploadStatus = 0,
                                ShipTo = shipTo,

                                Code = _unitWork.GetTaskNo(TaskNo.出库手动分配),
                                FirstStatus = ReceiptHeaderStatus.新建,
                                LastStatus = ReceiptHeaderStatus.新建,
                                TotalLines = 0,
                                TotalQty = 0,
                                CreateBy = "WMS",
                                CreateTime = DateTime.Now
                            };
                            _unitWork.Add(shipmentheader);
                        }
                        #endregion

                        #region 保存出库单子表
                        ShipmentDetail shipmentDetail = new ShipmentDetail
                        {
                            SourceCode = UpperTaskNo + "&" + SourceCode,
                            MaterialCode = MaterialCode,
                            InventoryStatus = "",
                            Qty = Num,
                            CreateTime = shipmentheader.CreateTime,
                            CreateBy = shipmentheader.CreateBy,

                            ShipmentId = shipmentheader.Id,
                            ShipmentCode = shipmentheader.Code,
                            QtyDivided = 0,
                            QtyCompleted = 0,
                            Price = 0,
                            Status = ReceiptHeaderStatus.新建
                        };


                        _unitWork.Add(shipmentDetail);

                        #endregion

                        #region 更新出库单主表汇总信息
                        shipmentheader.TotalLines += 1;
                        shipmentheader.TotalQty += shipmentDetail.Qty;
                        if (shipmentheader.UpdateBy == null)
                        {
                            shipmentheader.UpdateBy = "wms";
                            shipmentheader.UpdateTime = DateTime.Now;
                        }
                        _apph.UpdateByTracking(shipmentheader);
                        #endregion
                    }
                    InterfaceLog Oldinter = _unitWork.Find<InterfaceLog>(n => n.TaskNo == (UpperTaskNo + "&" + SourceCode)).FirstOrDefault();
                    if (Oldinter != null)
                    {
                        tran.Rollback();
                        throw new Exception("重复下发");
                    }
                    else
                    {
                        InterfaceLog inter = new InterfaceLog
                        {
                            Type = "LK_Out",
                            AllNum = RecordSets.Count,
                            ComNum = 0,
                            Request = Request,
                            Status = 0,
                            TaskNo = UpperTaskNo + "&" + SourceCode,
                            CreateTime = DateTime.Now,
                            CreateBy = "WMS"
                        };
                        _unitWork.Add(inter);
                    }
                    element.SetAttribute("code", "0");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                element.SetAttribute("code", "1");
                element.SetAttribute("description", ex.Message);
                InterfaceLog ErrorInter = new InterfaceLog
                {
                    Type = "出库创建报错",
                    AllNum = 0,
                    ComNum = 0,
                    Request = Request,
                    Status = 4,
                    Message = ex.Message,
                    TaskNo = UpperTaskNo + "&" + SourceCode,
                    CreateTime = DateTime.Now,
                    CreateBy = "WMS"
                };
                _unitWork.Add(ErrorInter);
            }
            return reDoc.InnerXml;
        }
        //RGV移动
        public string LK_RgvMove(string Request)
        {
            TableData response = new TableData();
            response.code = 200;
            XmlDocument doc = new XmlDocument();
            ConfigXmlDocument reDoc = new ConfigXmlDocument();
            //读取传入的xml
            var config = AppSettingsJson.GetAppSettings();
            reDoc.Load(config.GetSection("NouYaWebInService:Url").Value);
            //填入是否报错
            XmlElement element = (XmlElement)reDoc.SelectSingleNode("//Status");
            string name = "";
            string value = "";
            string FromStation = "";
            string ToStaTion = "";
            string SourceCode = "";
            try
            {
                doc.LoadXml(Request);
                XmlNodeList nodes = doc.SelectNodes("//Field");
                using (var tran = _context.Database.BeginTransaction())
                {
                    foreach (XmlElement node in nodes)
                    {

                        //获得字节里面的属性值
                        name = node.GetAttribute("name");
                        value = node.GetAttribute("value");
                        if (name == "imn01" && !string.IsNullOrEmpty(value))
                        {
                            SourceCode = value;
                        }
                        else if (name == "station_out" && !string.IsNullOrEmpty(value))
                        {
                            FromStation = value;
                        }
                        else if (name == "station_in" && !string.IsNullOrEmpty(value))
                        {
                            ToStaTion = value;
                        }
                    }
                    #region 创建站台到站台任务
                    Station fromstation = _unitWork.Find<Station>(n => n.Code == FromStation).FirstOrDefault();
                    Station tostation = _unitWork.Find<Station>(n => n.Code == ToStaTion).FirstOrDefault();
                    StationRoadway star = _unitWork.Find<StationRoadway>(n => n.StationCode == FromStation).FirstOrDefault();
                    IStationTaskApp stationTaskApp = new IStationTaskApp(_unitWork, _auth, _context, _apprh);

                    if (fromstation == null)
                    {
                        tran.Rollback();
                        throw new Exception(fromstation.Code + ":无此站台!");
                    }
                    else if (tostation == null)
                    {
                        tran.Rollback();
                        throw new Exception(tostation.Code + ":无此站台!");
                    }
                    if (fromstation.Containercode == null || fromstation.Containercode == "")
                    {
                        tran.Rollback();
                        throw new Exception(fromstation.Code + ":无此站台!");
                    }
                    else
                    {
                        WebRepository.Task ptask = new WebRepository.Task();
                        TaskDetail ptaskDetail = new TaskDetail();
                        var taskNo = _app.GetTaskNo(TaskNo.站台到站台);
                        ptask.TaskNo = taskNo;
                        ptask.OrderCode = taskNo;
                        ptask.SourceCode = SourceCode;
                        ptask.BusinessType = BusinessType.出库_其他出库单;
                        ptask.FirstStatus = WebRepository.TaskStatus.待下发任务;
                        ptask.LastStatus = WebRepository.TaskStatus.待下发任务;
                        _unitWork.Add(ptask);

                        ptaskDetail.TaskNo = taskNo;
                        ptaskDetail.OrderCode = taskNo;
                        ptask.SourceCode = SourceCode;
                        ptaskDetail.TaskType = TaskType.站台到站台;
                        ptaskDetail.ContainerCode = fromstation.Containercode;
                        ptaskDetail.SourceLocation = fromstation.Code;
                        ptaskDetail.DestinationLocation = tostation.Code;
                        ptaskDetail.OderQty = 0;
                        ptaskDetail.ContainerQty = 0;
                        ptaskDetail.HadQty = 0;
                        ptaskDetail.Roadway = 0;
                        ptaskDetail.Station = tostation.Code;
                        ptaskDetail.Status = WebRepository.TaskStatus.待下发任务;
                        ptaskDetail.Priority = 0;
                        ptaskDetail.CreateTime = DateTime.Now;
                        _unitWork.Add(ptaskDetail);
                    }
                    #endregion
                    InterfaceLog Oldinter = _unitWork.Find<InterfaceLog>(n => n.TaskNo == Request).FirstOrDefault();
                    if (Oldinter != null)
                    {
                        tran.Rollback();
                        throw new Exception("重复下发!");
                    }
                    else
                    {
                        InterfaceLog inter = new InterfaceLog
                        {
                            Type = "LK_RgvMove",
                            AllNum = 0,
                            ComNum = 0,
                            Request = Request,
                            Status = 0,
                            TaskNo = SourceCode,
                            CreateTime = DateTime.Now,
                            CreateBy = "WMS"
                        };
                        _unitWork.Add(inter);
                    }
                    element.SetAttribute("code", "0");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                element.SetAttribute("code", "1");
                element.SetAttribute("description", ex.Message);
                InterfaceLog ErrorInter = new InterfaceLog
                {
                    Type = "RGVMove创建报错",
                    AllNum = 0,
                    ComNum = 0,
                    Request = Request,
                    Status = 4,
                    Message = ex.Message,
                    TaskNo = SourceCode,
                    CreateTime = DateTime.Now,
                    CreateBy = "WMS"
                };
                _unitWork.Add(ErrorInter);
            }
            return reDoc.InnerXml;
        }

        public ReceiptResponse ReceiptService(MaterialsInformation[] materials)
        {
            ReceiptResponse receiptResponse = new ReceiptResponse();
            try
            {
                receiptResponse.Code = 200;
                receiptResponse.materials = materials;
                receiptResponse.Message = "success";
            }
            catch (Exception ex)
            {
                receiptResponse.Code = 300;
                receiptResponse.Message = ex.Message;
            }
            return receiptResponse;
        }
    }
}