IStackerStatusApp.cs 137 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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Xml;
using System.Linq;
using Infrastructure;
using WebRepository;
using ServiceReference1;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;

namespace WebApp
{
    /// <summary>
    /// 堆垛机运行节点接口App
    /// </summary>

    public partial class IStackerStatusApp : ApiApp
    {
        public IRepository<ReceiptHeader> _apprh;
        public IRepository<Container> _appc;
        public IRepository<ReceiptDetail> _apprd;
        public IRepository<Inventory> _appi;
        private object obj = new object();
        public IStackerStatusApp(IUnitWork unitWork, IRepository<Inventory> inventory, IRepository<Container> Container, IAuth auth, BaseDBContext context, IRepository<ReceiptDetail> receiptDetail, IRepository<ReceiptHeader> receiptHeader) : base(unitWork, auth, context)
        {
            _apprd = receiptDetail;
            _apprh = receiptHeader;
            _appi = inventory;
            _appc = Container;

            //系统字段赋值
            _apprd._loginInfo = _apprh._loginInfo = _loginInfo;
            _appi._loginInfo = _apprh._loginInfo = _loginInfo;
            _appc._loginInfo = _apprh._loginInfo = _loginInfo;
        }

        public Response<TaskDetail> SetStackerStatus(STKStatusModel sTKStatusModel)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                Response<TaskDetail> Response = new Response<TaskDetail>();
                try
                {
                    TaskDetail td = null;
                    if (sTKStatusModel.Status == 30)
                    {
                        td = _unitWork.FindSingle<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成);
                    }
                    else if (sTKStatusModel.Status == 40)
                    {
                        td = _unitWork.FindSingle<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成);
                    }
                    //WCS回库 货物到Rgv上给一次反馈站台占用解除
                    else if (sTKStatusModel.Status == 10)
                    {
                        td = _unitWork.FindSingle<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成);
                        Station stationType = _unitWork.Find<Station>(n => n.Code == td.SourceLocation).FirstOrDefault();
                        if (td == null)
                        {
                            td.UpdateTime = DateTime.Now;
                            td.Error = "任务重发,任务号:" + sTKStatusModel.TaskNo;
                            _unitWork.Update(td);
                            Response.Message = "重发";
                            return Response;
                        }
                        if (stationType != null)
                        {
                            if (stationType.Type == StationType.PP片上胶站台)
                            {
                                Station stationEmpty = (from se in _unitWork.Find<Station>(n => n.IsEmpty == 1 && n.IsStop == 0 && n.Containercode != "")
                                                        join sts in _unitWork.Find<StationToStation>(a => a.EndStation == td.SourceLocation)
                                                        on se.Code equals sts.StartStation
                                                        select se).FirstOrDefault();
                                if (stationEmpty != null)
                                {
                                    stationEmpty.Containercode = "";
                                    stationEmpty.UpdateBy = "wms";
                                    stationEmpty.UpdateTime = DateTime.Now;
                                    _unitWork.Update(stationEmpty);
                                }
                            }
                        }


                        //成品库入库
                        if (td.Roadway == 5)
                        {
                            StoreStation store = _unitWork.Find<StoreStation>(n => n.ContainerCode == td.ContainerCode && n.Status == StoreStationStatus.到达站台).FirstOrDefault();
                            if (store != null)
                            {
                                _unitWork.Delete(store);
                            }
                        }

                        if (td.Roadway == 2 && td.TaskType == TaskType.移库)
                        {
                            Location loc = _unitWork.Find<Location>(n => n.Code == td.SourceLocation && n.Status == LocationStatus.任务锁定中).FirstOrDefault();
                            if (loc == null)
                            {
                                Response.Code = 300;
                                Response.Status = false;
                                Response.Message = "移库初始仓位状态不正确!";
                                td.Error = "移库初始仓位状态不正确!";
                                _unitWork.Update(td);
                                return Response;
                            }
                            else
                            {
                                td.UpdateTime = DateTime.Now;
                                td.Error = "堆垛机已接任务,UpdateTime:" + DateTime.Now;
                                _unitWork.Update(td);
                            }
                        }
                        else if (td.Roadway == 1 && td.TaskType == TaskType.移库)
                        {
                            Location loc = _unitWork.Find<Location>(n => n.Code == td.SourceLocation && n.Status == LocationStatus.任务锁定中).FirstOrDefault();
                            if (loc == null)
                            {
                                Response.Code = 300;
                                Response.Status = false;
                                Response.Message = "移库初始仓位状态不正确!";
                                td.Error = "移库初始仓位状态不正确!";
                                _unitWork.Update(td);
                                return Response;
                            }
                            else
                            {
                                _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                                {
                                    Status = LocationStatus.空仓位,
                                    ContainerCode = "",
                                    UpdateTime = DateTime.Now
                                });
                                td.UpdateTime = DateTime.Now;
                                td.Error = "堆垛机已接任务:"+ DateTime.Now;
                                _unitWork.Update(td);
                            }
                        }
                        else if (td.Roadway == 2 && td.SourceLocation == "InStationB04")
                        {
                            Station sta = _unitWork.Find<Station>(n => n.Code == "InStationB03").FirstOrDefault();
                            if (sta.Containercode == td.ContainerCode)
                            {
                                sta.Containercode = "";
                                sta.UpdateBy = "wms";
                                sta.UpdateTime = DateTime.Now;
                                _unitWork.Update(sta);
                            }

                            StoreStation storeStation = _unitWork
                                .Find<StoreStation>(n =>
                                    n.ContainerCode == td.ContainerCode && n.StationCode == "In").FirstOrDefault();
                            if (storeStation != null)
                            {
                                TaskHistory taskHistory = new TaskHistory
                                {
                                    OrderCode = storeStation.ContainerCode,
                                    BusinessType = "In",
                                    FirstStatus = 0,
                                    LastStatus = 0,
                                    CreateTime = DateTime.Now
                                };
                                _unitWork.Add(taskHistory);
                                _unitWork.Delete(storeStation);
                            }
                        }
                        else
                        {
                            Station station = _unitWork.Find<Station>(n => n.Code == td.SourceLocation && n.Containercode == td.ContainerCode && td.SourceLocation != "PP_SpecialPoint").FirstOrDefault();
                            if (station != null)
                            {
                                station.Containercode = "";
                                station.UpdateBy = "wms";
                                station.UpdateTime = DateTime.Now;
                            }
                            else if (_unitWork.IsExist<Station>(n => n.Containercode == td.SourceLocation))
                            {
                                station = _unitWork.Find<Station>(n => n.Code == td.SourceLocation).FirstOrDefault();
                                station.Containercode = "";
                                station.UpdateBy = "wms";
                                station.UpdateTime = DateTime.Now;
                            }
                            else
                            {
                                Response.Message = "重发";
                                return Response;
                            }
                            _unitWork.Update(station);
                            
                        }

                        tran.Commit();
                        return Response;
                    }
                    //WCS回库仓位高度请求
                    else if (sTKStatusModel.Status == 20)
                    {
                        td = _unitWork.Find<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成 && (u.TaskType == TaskType.容器回库 || u.TaskType == TaskType.空容器入库)).FirstOrDefault();
                        StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == td.SourceLocation).FirstOrDefault();
                        Location loc = null;
                        if (stationRoadway == null)
                        {
                            Response.Code = 300;
                            Response.Status = false;
                            Response.Message = "未找到起始站台!";
                            return Response;
                        }
                        else if (td == null)
                        {
                            Response.Code = 300;
                            Response.Status = false;
                            Response.Message = "未找到回库任务!";
                            return Response;
                        }
                        else if (_unitWork.IsExist<Location>(n => n.ContainerCode == td.ContainerCode))
                        {
                            if (_unitWork.IsExist<Inventory>(n => n.ContainerCode == td.ContainerCode && n.TaskStatus == InventoryTaskType.有盘点任务))
                            {
                                loc = _unitWork.Find<Location>(n => n.ContainerCode == td.ContainerCode).FirstOrDefault();
                            }
                            else
                            {
                                Response.Result = td;
                                Response.Message = "已有仓位!";
                                return Response;
                            }
                        }

                        Container container = _unitWork.Find<Container>(n => n.Code == td.ContainerCode).FirstOrDefault();
                        if (loc == null && container != null)
                        {
                            if (sTKStatusModel.Height == -1)
                            {
                                sTKStatusModel.Height = (int)container.Height;
                            }
                            string error = "";
                            while (true)
                            {
                                if (td.Roadway == 3)
                                {
                                    if (td.TaskType == TaskType.容器回库 && td.Roadway == 3 && _unitWork.IsExist<Inventory>(n => n.IsFold == "isflod" && n.ContainerCode == td.ContainerCode))
                                    {
                                        loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => n.DistributionType).ThenBy(b => b.Layer).ThenBy(a => a.MaxHeight).ThenByDescending(y => y.Row).FirstOrDefault();
                                    }
                                    else
                                    {
                                        List<LocationDistribution> locationDistributions = _unitWork.Find<LocationDistribution>(n => n.RoadWay == td.Roadway).ToList();
                                        if (locationDistributions.Count != 0)
                                        {
                                            Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode && n.NameDescription != "").FirstOrDefault();
                                            if (inventory != null)
                                            {
                                                foreach (LocationDistribution locationDistribution in locationDistributions)
                                                {
                                                    if (inventory.NameDescription.Substring((int)locationDistribution.ContrastStart - 1, (int)locationDistribution.Length) == locationDistribution.ContrastName)
                                                    {
                                                        loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && 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.Layer==1||n.Layer==2) ? 13 : n.Layer).ThenBy(a => a.MaxHeight).ThenBy(n => n.Row).FirstOrDefault();

                                                        if (loc != null)
                                                        {
                                                            break;
                                                        }
                                                        else if (loc == null && locationDistribution.Type == DistributionType.静态库位)
                                                        {
                                                            Response.Code = 300;
                                                            Response.Status = false;
                                                            Response.Message = "未找到仓位!";
                                                            td.Error = "未找到仓位(分配仓位为静态)";
                                                            _unitWork.Update(td);
                                                            tran.Commit();
                                                            return Response;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (loc == null)
                                        {
                                            if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                            {
                                                loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => (a.Layer == 1 || a.Layer == 2)? 13 : 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 >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => n.DistributionType).ThenBy(b => (b.Layer == 1||b.Layer == 2)? 13 : b.Layer).ThenBy(a => a.MaxHeight).ThenByDescending(y => y.Row).FirstOrDefault();
                                            }
                                        }
                                    }
                                }
                                //仓位高度不一致的巷道 避免无仓位移库
                                else if (td.Roadway == 2)
                                {
                                    List<LocationDistribution> locationDistributions = _unitWork.Find<LocationDistribution>(n => n.RoadWay == td.Roadway).ToList();
                                    int locationDistributionType = LocationDistributionType.普通库位;
                                    if (locationDistributions.Count != 0)
                                    {
                                        Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode && n.NameDescription != "").FirstOrDefault();
                                        if (inventory != null)
                                        {
                                            foreach (LocationDistribution locationDistribution in locationDistributions)
                                            {
                                                if (inventory.NameDescription.Substring((int)locationDistribution.ContrastStart - 1, (int)locationDistribution.Length) == locationDistribution.ContrastName)
                                                {
                                                    //查找可用空仓位或者空容器仓位 
                                                    loc = _unitWork.Find<Location>(n => ((n.Status == LocationStatus.空仓位 && n.ContainerCode == "") || n.Status == LocationStatus.空容器) && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay
                                                               && 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.Layer).ThenBy(a => a.MaxHeight).ThenBy(n => n.Row).FirstOrDefault();

                                                    locationDistributionType = (int)locationDistribution.Type;
                                                    if (loc != null)
                                                    {
                                                        break;
                                                    }
                                                }
                                            }
                                            if (loc == null && locationDistributionType == DistributionType.静态库位)
                                            {
                                                Response.Code = 300;
                                                Response.Status = false;
                                                Response.Message = "未找到仓位!";
                                                td.Error = "未找到仓位(分配仓位为静态)";
                                                _unitWork.Update(td);
                                                tran.Commit();
                                                return Response;
                                            }
                                        }
                                    }
                                    if (loc == null)
                                    {
                                        if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                        {
                                            loc = _unitWork.Find<Location>(n => n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && ((n.Status == LocationStatus.空仓位 && n.ContainerCode == "") || n.Status == LocationStatus.空容器) && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                                        }
                                        else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                                        {
                                            loc = _unitWork.Find<Location>(n => n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && ((n.Status == LocationStatus.空仓位 && n.ContainerCode == "") || n.Status == LocationStatus.空容器) && n.DistributionType != LocationDistributionType.被分配库位静态库位).OrderBy(n => n.DistributionType).ThenBy(b => (b.Layer)).ThenBy(a => a.MaxHeight).ThenByDescending(y => y.Row).FirstOrDefault();
                                        }
                                    }

                                    if (loc != null) {
                                        //确定需要移库
                                        if (loc.Status == LocationStatus.空容器)
                                        {
                                            decimal? height = loc.MaxHeight;
                                            Location locEmpty = null;
                                            //根据空容器仓位的高度重新查找是否有符合高度的空仓位
                                            locEmpty = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight == height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.DistributionType == locationDistributionType).OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                                            if (locEmpty == null)
                                            {
                                                //在所有数据中查询移库目的地空仓位
                                                locEmpty = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "").OrderBy(n => n.DistributionType).ThenBy(b => b.MaxHeight).ThenByDescending(a => a.Layer).ThenByDescending(y => y.Row).FirstOrDefault();
                                                if (locEmpty == null)
                                                {
                                                    Response.Code = 300;
                                                    Response.Status = false;
                                                    Response.Message = loc.Code + "未找到移库仓位:" + td.ContainerCode + "不能入库!";
                                                    td.Error = loc.Code + "未找到移库仓位:" + td.ContainerCode + "不能入库!";
                                                    _unitWork.Update(td);
                                                    return Response;
                                                }
                                                else
                                                {
                                                    _unitWork.Update<Location>(n => n.Id == locEmpty.Id && n.Version == locEmpty.Version, n => new Location
                                                    {
                                                        Status = LocationStatus.任务锁定中,
                                                        ContainerCode = loc.ContainerCode,
                                                        UpdateTime = DateTime.Now
                                                    });
                                                }

                                                //建立移库任务
                                                Task task = new Task();
                                                string taskNo;
                                                TaskDetail taskDetail = new TaskDetail();
                                                taskNo = loc.Code + _apprh.GetTaskNo(TaskNo.移库);
                                                task.TaskNo = taskNo;
                                                task.OrderCode = taskNo;
                                                task.BusinessType = BusinessType.入库_其他入库单;
                                                task.FirstStatus = TaskStatus.下达任务;
                                                task.LastStatus = TaskStatus.下达任务;
                                                task.CreateTime = DateTime.Now;
                                                _unitWork.Add(task);

                                                taskDetail.TaskNo = taskNo;
                                                taskDetail.OrderCode = taskNo;
                                                taskDetail.SourceCode = td.TaskNo;
                                                taskDetail.TaskType = TaskType.移库;
                                                taskDetail.ContainerCode = loc.ContainerCode;
                                                taskDetail.SourceLocation = loc.Code;
                                                taskDetail.DestinationLocation = locEmpty.Code;
                                                taskDetail.OderQty = 0;
                                                taskDetail.ContainerQty = 0;
                                                taskDetail.HadQty = 0;
                                                taskDetail.Roadway = td.Roadway;
                                                taskDetail.Station = null;
                                                taskDetail.Status = TaskStatus.下达任务;
                                                taskDetail.Priority = 999;
                                                taskDetail.MaterialName = "1";
                                                taskDetail.CreateTime = DateTime.Now;
                                                _unitWork.Add(taskDetail);
                                                ApiRequest apiRequest = new ApiRequest("WCS");
                                                //WCSResponse<WcsTask> _WCSResponse = new WCSResponse<WcsTask>
                                                //{
                                                //    Code = 200
                                                //};
                                                WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "WcsWebApi/TaskAssign", "任务下发");
                                                if (_WCSResponse.Code != 200)
                                                {
                                                    Response.Code = 300;
                                                    Response.Status = false;
                                                    Response.Message = "移库:" + loc.Code + "下发失败:" + Response.Message;
                                                    td.Error = loc.Code + "未找到移库仓位:" + td.ContainerCode + "不能入库!" + Response.Message;
                                                    _unitWork.Update(td);
                                                    return Response;
                                                }
                                            }
                                            else if (locEmpty.Status == LocationStatus.空仓位 && locEmpty.ContainerCode == "")
                                            {
                                                loc = locEmpty;
                                            }
                                        }
                                    }
                                        
                                }
                                else if (td.Roadway == 5)
                                {
                                    List<LocationDistribution> locationDistributions = _unitWork.Find<LocationDistribution>(n => n.RoadWay == td.Roadway).ToList();
                                    if (locationDistributions.Count != 0)
                                    {
                                        Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode && n.NameDescription != "").FirstOrDefault();
                                        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.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.MaxHeight >= sTKStatusModel.Height && 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.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.MaxHeight >= sTKStatusModel.Height && 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).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.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).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.MaxHeight >= sTKStatusModel.Height && 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.静态库位)
                                                {
                                                    Response.Code = 300;
                                                    Response.Status = false;
                                                    Response.Message = "未找到仓位!";
                                                    td.Error = "未找到仓位(分配仓位为静态)";
                                                    _unitWork.Update(td);
                                                    tran.Commit();
                                                    return Response;
                                                }
                                            }
                                            }
                                        }
                                    }

                                    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.MaxHeight >= sTKStatusModel.Height && 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.MaxHeight >= sTKStatusModel.Height && 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).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).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.MaxHeight >= sTKStatusModel.Height && 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();
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (td.Roadway == 1)
                                {
                                    var tasks = _unitWork.Find<TaskDetail>(n => n.TaskNo != null).ToList();
                                    //if (_unitWork.GetCount<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "" && n.RowIndex == 2) < 8)
                                    //{
                                    //    loc = null;
                                    //    error = "仓位不足8个!";
                                    //    break;
                                    //}
                                    //RowIndex 1:里仓位,2:外仓位。查找到里仓位时外仓位要为空
                                    if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                    {
                                        loc = (from loca in _unitWork.Find<Location>(n => n.RowIndex == 1 && !tasks.Select(a => a.DestinationLocation).Contains(n.ContiguousCode) && !tasks.Select(a => a.SourceLocation).Contains(n.ContiguousCode) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                                               join cLoca in _unitWork.Find<Location>(n => n.ContainerCode == "" && n.Status != LocationStatus.任务锁定中 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay)
                                               on loca.ContiguousCode equals cLoca.Code
                                               select loca)
                                              .OrderBy(n => n.RowIndex == 1 ? 0 : n.RowIndex).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                                    }
                                    else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                                    {
                                        loc = (from loca in _unitWork.Find<Location>(n => n.RowIndex == 1 && !tasks.Select(a => a.DestinationLocation).Contains(n.ContiguousCode) && !tasks.Select(a => a.SourceLocation).Contains(n.ContiguousCode) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                                               join cLoca in _unitWork.Find<Location>(n => n.ContainerCode == "" && n.Status != LocationStatus.任务锁定中 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay)
                                               on loca.ContiguousCode equals cLoca.Code
                                               select loca)
                                               .OrderBy(n => n.RowIndex == 1 ? 0 : n.RowIndex).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenByDescending(y => y.Row).FirstOrDefault();
                                    }
                                    if (loc == null)
                                    {
                                        if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                        {
                                            loc = (from loca in _unitWork.Find<Location>(n => n.RowIndex == 2 && !tasks.Select(a => a.DestinationLocation).Contains(n.ContiguousCode) && !tasks.Select(a => a.SourceLocation).Contains(n.ContiguousCode) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                                                   join cLoca in _unitWork.Find<Location>(n => n.Status != LocationStatus.任务锁定中 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay)
                                                   on loca.ContiguousCode equals cLoca.Code
                                                   select loca)
                                                  .OrderBy(n => n.RowIndex == 1 ? 0 : n.RowIndex).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                                        }
                                        else if (stationRoadway.StationPlace == StationPlace.巷道南面)
                                        {
                                            loc = (from loca in _unitWork.Find<Location>(n => n.RowIndex == 2 && !tasks.Select(a => a.DestinationLocation).Contains(n.ContiguousCode) && !tasks.Select(a => a.SourceLocation).Contains(n.ContiguousCode) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                                                   join cLoca in _unitWork.Find<Location>(n => n.Status != LocationStatus.任务锁定中 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay)
                                                   on loca.ContiguousCode equals cLoca.Code
                                                   select loca)
                                                   .OrderBy(n => n.RowIndex == 1 ? 0 : n.RowIndex).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenByDescending(y => y.Row).FirstOrDefault();
                                        }
                                    }
                                }
                                else
                                {
                                    if (stationRoadway.StationPlace == StationPlace.巷道北面)
                                    {
                                        loc = _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= sTKStatusModel.Height && 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 >= sTKStatusModel.Height && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "").OrderBy(b => (b.MaxHeight)).ThenBy(a => a.Layer).ThenByDescending(y => y.Row).FirstOrDefault();
                                    }
                                }


                                //更新仓位对应的容器,如果回库分配的不是原仓位则更新仓位且锁定
                                if (loc == null)
                                {
                                    Response.Code = 300;
                                    Response.Status = false;
                                    Response.Message = "未找到仓位!";
                                    td.Error = "未找到仓位!" + error;
                                    _unitWork.Update(td);
                                    tran.Commit();
                                    return Response;
                                }
                                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 = td.ContainerCode,
                                        UpdateTime = DateTime.Now
                                    });

                                    container = _unitWork.Find<Container>(n => n.Code == td.ContainerCode).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 = td.ContainerCode,
                                                    Status = LocationStatus.任务锁定中,
                                                    UpdateBy = "wms",
                                                    UpdateTime = DateTime.Now
                                                });
                                                if (j != 1)
                                                {
                                                    Response.Code = 300;
                                                    Response.Status = false;
                                                    Response.Message = "邻仓位更改失败";
                                                    return Response;
                                                }
                                            }
                                        }
                                    }
                                    if (i == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode).ToList();
                        if (container == null)
                        {
                            Response.Code = 300;
                            Response.Status = false;
                            Response.Message = "未找到该容器!";
                            td.Error = "未找到该容器!";
                            _unitWork.Update(td);
                            return Response;
                        }
                        else
                        {
                            //更改容器
                            container.Height = sTKStatusModel.Height;
                            container.LocationCode = loc.Code;
                            container.LocationId = loc.Id;
                            _unitWork.Update(container);
                        }

                        //更新同个容器中其它物料对应新的回库仓位
                        if (inventories.Count > 0)
                        {
                            foreach (Inventory inv in inventories)
                            {
                                if (inv.LocationCode != loc.Code)
                                {
                                    inv.LocationCode = loc.Code;
                                    _appi.Update(inv);
                                }
                            }
                        }
                        //更改任务终点
                        td.DestinationLocation = loc.Code;
                        _unitWork.Update(td);
                        //如果入库起始站台为PP片排版房入库口,那么在请求仓位后就直接清除站台占用
                        Station stationOne = _unitWork.Find<Station>(n => n.Code == td.SourceLocation).FirstOrDefault();
                        if (stationOne.Containercode == td.ContainerCode && td.SourceLocation == "EntranceStationY01")
                        {
                            stationOne.Containercode = "";
                            stationOne.UpdateBy = "wms";
                            stationOne.UpdateTime = DateTime.Now;
                            _unitWork.Update(stationOne);
                        }
                        tran.Commit();
                        Response.Result = td;
                        return Response;
                    }
                    //满入处理
                    else if (sTKStatusModel.Status == 50)
                    {
                        TaskDetail taskDel = _unitWork.Find<TaskDetail>(n => n.TaskNo == sTKStatusModel.TaskNo && n.Status == TaskStatus.下达任务).FirstOrDefault();
                        if (taskDel != null)
                        {
                            Container container = _unitWork.Find<Container>(n => n.Code == taskDel.ContainerCode).FirstOrDefault();
                            Location location = _unitWork.Find<Location>(n => n.ContainerCode == taskDel.ContainerCode).FirstOrDefault();
                            if (location != null && container != null)
                            {
                                List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == container.Code).ToList();
                                location.ContainerCode = location.Code + "Abnormal";
                                _unitWork.Update<Location>(n => n.Id == location.Id && n.Version == location.Version, n => new Location
                                {
                                    Status = LocationStatus.有货,
                                    ContainerCode = location.ContainerCode,
                                    UpdateTime = DateTime.Now
                                });
                                if (!_unitWork.IsExist<Container>(n => n.Code == location.ContainerCode))
                                {
                                    Container containerAbnormal = new Container();
                                    containerAbnormal.Code = location.ContainerCode;
                                    containerAbnormal.LocationCode = location.Code;
                                    containerAbnormal.IsLock = ContainerLock.任务锁;
                                    containerAbnormal.Status = ContainerStatus.;
                                    containerAbnormal.Type = container.Type;
                                    containerAbnormal.PrintCount = 0;
                                    containerAbnormal.CreateTime = DateTime.Now;
                                    _unitWork.Add(containerAbnormal);

                                }
                                container.LocationCode = "Abnormal";
                                container.LocationId = 0;
                                _unitWork.Update(container);
                                container.LocationCode = "Abnormal";
                                container.UpdateTime = DateTime.Now;
                                if (inventories.Count > 0)
                                {
                                    foreach (Inventory inv in inventories)
                                    {
                                        inv.LocationCode = "Abnormal";
                                        _appi.Update(inv);
                                    }
                                }
                                taskDel.DestinationLocation = location.Type;
                                taskDel.Error = "满入:" + DateTime.Now;
                                _unitWork.Update(taskDel);
                            }
                        }
                        else
                        {
                            Response.Code = 500;
                            Response.Status = false;
                            Response.Message = "未找到该任务号任务" + sTKStatusModel.TaskNo;
                        }

                        tran.Commit();
                        return Response;
                    }
                    else
                    {
                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = "不识别的任务状态!";
                    }

                    if (td != null)
                    {
                        List<TaskDetail> taskDetailsShipments = _unitWork.Find<TaskDetail>(n => n.ContainerCode == td.ContainerCode && (n.TaskType == TaskType.整盘出库 || n.TaskType == TaskType.分拣出库) && n.Station == td.Station).ToList();
                        foreach (var taskDetailsShipment in taskDetailsShipments)
                        {
                            if (taskDetailsShipment.Status == TaskStatus.待下发任务)
                            {
                                taskDetailsShipment.Status = TaskStatus.下达任务;
                                _unitWork.Update(taskDetailsShipment);
                            }
                        }
                        List<TaskDetail> taskDetails = new List<TaskDetail>();
                        if (td.ContainerCode == "empty")
                        {
                            taskDetails = _unitWork.Find<TaskDetail>(u => u.TaskNo == td.TaskNo && u.Status > TaskStatus.待下发任务 && u.Status < TaskStatus.已经完成).ToList();
                        }
                        else
                        {
                            taskDetails = _unitWork.Find<TaskDetail>(u => u.ContainerCode == td.ContainerCode && u.Status >= TaskStatus.待下发任务 && u.Status < TaskStatus.已经完成).ToList();
                        }
                        if (sTKStatusModel.Status == 30)   //堆垛机出库完成容器到达站台
                        {
                            foreach (TaskDetail t in taskDetails)
                            {
                                
                                if (t.TaskType == TaskType.容器出库 || t.TaskType == TaskType.空容器出库 || t.TaskType == TaskType.盘点)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘出库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);
                                }
                                else if (t.TaskType == TaskType.站台到站台)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘出库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);

                                    //Task pouttask = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    //if (pouttask == null)
                                    //{
                                    //    throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    //}

                                    Station stationSource = _unitWork.Find<Station>(n => n.Code == t.SourceLocation).FirstOrDefault();
                                    if (stationSource != null)
                                    {
                                        if (stationSource.Containercode == td.ContainerCode)
                                        {
                                            stationSource.Containercode = "";
                                            _unitWork.Update(stationSource);
                                        }
                                    }
                                    //PP卷RGV入库完成修改状态
                                    if (t.DestinationLocation == "InStationB03")
                                    {
                                        StoreStation storeStation = _unitWork.Find<StoreStation>(n => n.ContainerCode == t.ContainerCode && n.StationCode == "In").FirstOrDefault();
                                        if (storeStation != null)
                                        {
                                            storeStation.Status = StoreStationStatus.到达站台;
                                            storeStation.UpdateTime = DateTime.Now;
                                            _unitWork.Update(storeStation);
                                        }
                                    }
                                    //PP卷从复核区出
                                    if (t.DestinationLocation.Contains("ProductStationB") || t.DestinationLocation.Contains("BufferStationB"))
                                    {
                                        StoreStation storeStation = _unitWork.Find<StoreStation>(n => n.ContainerCode == t.ContainerCode).FirstOrDefault();
                                        if (storeStation != null)
                                        {
                                            TaskHistory taskHistory = new TaskHistory
                                            {
                                                OrderCode = storeStation.ContainerCode,
                                                BusinessType = "Out",
                                                FirstStatus = 0,
                                                LastStatus = 0,
                                                CreateTime = DateTime.Now
                                            };
                                            _unitWork.Add(taskHistory);
                                            _unitWork.Delete(storeStation);
                                           
                                        }
                                    }
                                }
                                else if (t.TaskType == TaskType.直接出库)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘出库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);
                                }
                                else
                                {
                                    if ((t.TaskType == TaskType.整盘出库 || t.TaskType == TaskType.分拣出库) && td.Station == t.Station)//出库任务类型
                                    {
                                        List<InventoryOut> inventoryOuts = _unitWork.Find<InventoryOut>(n => n.ContainerCode == t.ContainerCode && n.ShipmentState == 0).ToList();
                                        foreach (InventoryOut inventoryOut in inventoryOuts)
                                        {
                                            inventoryOut.ShipmentState = 1;
                                            _unitWork.Update(inventoryOut);
                                            //更改出库明细状态
                                            //查找当前托盘的inventoryOut对应的出库单据明细,并将改明细状态修改
                                            ShipmentDetail InvshipmentDetail = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == inventoryOut.ReceiptCode && u.Project == inventoryOut.QrCode && u.Status == ShipmentHeaderStatus.分配完成).FirstOrDefault();
                                            if (InvshipmentDetail != null)
                                            {
                                                InterfaceLog interfaceLog = _unitWork.Find<InterfaceLog>(n => n.TaskNo == InvshipmentDetail.SourceCode).FirstOrDefault();
                                                if (interfaceLog != null)
                                                {
                                                    interfaceLog.ComNum += 1;
                                                    _unitWork.Update(interfaceLog);
                                                }
                                                InvshipmentDetail.Status = ShipmentHeaderStatus.过账;
                                                InvshipmentDetail.UpdateBy = "wms";
                                                InvshipmentDetail.UpdateTime = DateTime.Now;
                                                _unitWork.Update(InvshipmentDetail);
                                            }
                                        }

                                        ShipmentHeader shipment = _unitWork
                                            .Find<ShipmentHeader>(n => n.SourceCode == t.SourceCode).FirstOrDefault();
                                        List<ShipmentDetail> shipmentDetails = _unitWork
                                            .Find<ShipmentDetail>(n => n.SourceCode == t.SourceCode).OrderBy(a => a.Status).ToList();
                                        shipment.FirstStatus = shipmentDetails.Last().Status;
                                        shipment.LastStatus = shipmentDetails.First().Status;
                                        _unitWork.Update(shipment);

                                        //更新出库明细状态
                                        //List<ShipmentDetail> shipmentDetail = _context.Set<ShipmentDetail>().AsQueryable().Where(u => u.ShipmentCode == t.OrderCode && u.MaterialCode == t.MaterialCode && u.Status == ShipmentHeaderStatus.分配完成).ToList();
                                        ////任务表中不存在和当前任务同一个单据不同托盘的出库任务
                                        //if (shipmentDetail.Count > 0 && !_unitWork.IsExist<TaskDetail>(n => n.ContainerCode != t.ContainerCode && n.OrderCode == t.OrderCode && n.MaterialCode == t.MaterialCode  && n.Status < TaskStatus.已经完成 && (n.TaskType == TaskType.整盘出库 || n.TaskType == TaskType.分拣出库)))
                                        //{
                                        //    foreach (var ashipmentDetail in shipmentDetail)
                                        //    {
                                        //        InterfaceLog interfaceLog = _unitWork.Find<InterfaceLog>(n => n.TaskNo == ashipmentDetail.SourceCode).FirstOrDefault();
                                        //        if (interfaceLog != null)
                                        //        {
                                        //            interfaceLog.ComNum += 1;
                                        //            _unitWork.Update(interfaceLog);
                                        //        }
                                        //        ashipmentDetail.Status = ShipmentHeaderStatus.过账;
                                        //        ashipmentDetail.UpdateBy = "wms";
                                        //        ashipmentDetail.UpdateTime = DateTime.Now;
                                        //        _unitWork.Update(ashipmentDetail);
                                        //    }
                                        //}
                                    }
                                }

                                t.Status = TaskStatus.已经完成;  //更新任务明细状态为已完成
                                t.UpdateBy = "wms";
                                t.UpdateTime = DateTime.Now;
                                _unitWork.Update(t);
                                //更新主任务头尾状态
                                Task task = _context.Set<Task>().AsQueryable().Where(u => u.TaskNo == t.TaskNo).SingleOrDefault();
                                if (task == null)
                                {
                                    throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                }
                                // Task task = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                TaskDetail tdel = _unitWork.Find<TaskDetail>(u => u.TaskNo == t.TaskNo).OrderBy(a => a.Status).FirstOrDefault();//同一个任务号下面最小的任务状态,包含其它容器的任务,用于更新主任务的尾状态
                                if (tdel == null)
                                {
                                    throw new Exception("任务子表:" + t.TaskNo + "不存在!");
                                }
                                if (task.FirstStatus < t.Status)
                                {
                                    task.FirstStatus = t.Status;
                                }
                                if (task.LastStatus < tdel.Status)
                                {
                                    task.LastStatus = tdel.Status;
                                }
                                task.UpdateBy = "wms";
                                task.UpdateTime = DateTime.Now;
                                _unitWork.Update(task);


                                //已完成的单据任务迁移至历史任务表
                                if (task.LastStatus == TaskStatus.已经完成)
                                {
                                    TaskHistory taskhis = new TaskHistory();
                                    task.CopyTo(taskhis);
                                    taskhis.Id = null;
                                    _unitWork.Add(taskhis);

                                    List<TaskDetail> taskdtls = _context.Set<TaskDetail>().AsQueryable().Where(u => u.TaskNo == task.TaskNo).ToList();
                                    foreach (TaskDetail tdl in taskdtls)
                                    {
                                        TaskDetailHistory taskdthis = new TaskDetailHistory();
                                        tdl.CopyTo(taskdthis);
                                        taskdthis.Id = null;
                                        _unitWork.Add(taskdthis);

                                        _unitWork.Delete(tdl);
                                    }
                                    _unitWork.Delete(task);
                                }
                            }

                            if (td.TaskType == TaskType.直接出库 || td.TaskType == TaskType.空容器出库 || td.TaskType == TaskType.容器出库 || td.TaskType == TaskType.站台到站台 || td.TaskType == TaskType.盘点)
                            {
                                //托盘出库完成,容器占用站台
                                Station st = _unitWork.Find<Station>(u => u.Code == td.Station).FirstOrDefault();
                                if (st == null)
                                {
                                    throw new Exception("站台:" + td.Station + "不存在!");
                                }
                                else if (st.IsOccupy == 0 && td.IsOccupy != 1)
                                {
                                    st.Containercode = "";
                                    //if (cont != null && td.TaskType != TaskType.盘点)
                                    //{
                                    //    _appc.DeleteByTracking(cont);
                                    //}
                                }
                                else if (st.IsOccupy == 1 && td.IsOccupy != 0)
                                {
                                    st.Containercode = td.ContainerCode;
                                }

                                Container cont = _unitWork.Find<Container>(n => n.Code == td.ContainerCode && n.Code != "woodPallet").FirstOrDefault();
                                //成品库入库空板补给成功将板号给对应入库任务
                                TaskDetail detail = _unitWork.Find<TaskDetail>(n => n.TaskNo == td.OrderCode).FirstOrDefault();
                                if (td.TaskType == TaskType.空容器出库 && td.DestinationLocation == "OutStationE02")
                                {
                                    if (detail != null)
                                    {
                                        cont.Status = ContainerStatus.;
                                        cont.LocationCode = st.Code;
                                        _unitWork.Update(cont);
                                        detail.ContainerCode = td.ContainerCode;
                                        _unitWork.Update(detail);
                                    }

                                    StoreStation store = _unitWork.Find<StoreStation>(n => n.ContainerCode == td.ContainerCode).FirstOrDefault();
                                    if (store != null)
                                    {
                                        store.Status = StoreStationStatus.到达站台;
                                        _unitWork.Update(store);
                                    }
                                }
                                else if (cont != null && td.TaskType != TaskType.盘点 && st.Type != StationType.暂存区站台)
                                {
                                    if(td.TaskType != TaskType.站台到站台)
                                    {
                                        cont.Status = "empty";
                                    }
                                    cont.Height = 0;
                                    cont.LocationCode = st.Code;
                                    _unitWork.Update(cont);
                                }

                                st.UpdateBy = "wms";
                                st.UpdateTime = DateTime.Now;
                                _unitWork.Update(st);
                                List<Inventory> inventories = new List<Inventory>();
                                if (td.ContainerCode == "empty")
                                {
                                    inventories = _unitWork.Find<Inventory>(n => n.Id == td.InventoryId).ToList();
                                }
                                else
                                {
                                    inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode && n.ContainerCode != "woodPallet").ToList();
                                }
                                if (td.TaskType == TaskType.空容器出库 && (td.DestinationLocation == "InStationE07" || td.DestinationLocation == "InStationE08"))
                                {
                                    inventories = _unitWork.Find<Inventory>(n => n.LocationCode == td.Station).ToList();
                                    foreach (Inventory inv in inventories)
                                    {
                                        inv.ContainerCode = td.ContainerCode;
                                        inv.LocationCode = st.Code;
                                        _appi.UpdateByTracking(inv);
                                    }
                                }
                                else if ((st.Code == "InStationB03" || st.Type == StationType.暂存区站台 || st.Type == StationType.木栈板补给站台w_1 || st.Type == StationType.木栈板补给站台w_2) || td.TaskType == TaskType.盘点)
                                {
                                    foreach (Inventory inv in inventories)
                                    {
                                        if (st.Type == StationType.暂存区站台)
                                        {
                                            inv.TaskStatus = InventoryTaskType.无任务;
                                        }
                                        inv.LocationCode = st.Code;
                                        _appi.UpdateByTracking(inv);
                                        InventoryTransaction inventoryTransaction = new InventoryTransaction
                                        {
                                            Type = TransactionType.出库,
                                            SourceCode = inv.SourceCode,
                                            UserDef1 = td.OrderCode,
                                            ContainerCode = inv.ContainerCode,
                                            MaterialCode = inv.MaterialCode,
                                            Batch = inv.Batch,
                                            Lot = inv.Lot,
                                            Qty = inv.Qty,
                                            Supplier = inv.Supplier,
                                            ResidualWeight = inv.ResidualWeight,
                                            Retreat = inv.Retreat,
                                            Specification = inv.Specification,
                                            NameDescription = inv.NameDescription,
                                            Unit = inv.Unit,
                                            Size = inv.Size,
                                            NumberofSheets = inv.NumberofSheets,
                                            Groupnum = inv.Groupnum,
                                            OldBatch = inv.OldBatch,
                                            Combo = inv.Combo,
                                            Status = inv.Status,
                                            LocationCode = td.Station,
                                            StackingNumber = inv.StackingNumber,
                                            CreateTime = inv.CreateTime,
                                            UpdateTime = DateTime.Now
                                        };
                                        _unitWork.Add(inventoryTransaction);
                                    }
                                }
                                else
                                {
                                    if (td.ContainerCode == "empty")
                                    {
                                        foreach (Inventory inv in inventories)
                                        {
                                            inv.LocationCode = st.Code;
                                            _appi.UpdateByTracking(inv);
                                        }
                                    }
                                    else
                                    {
                                        foreach (Inventory inv in inventories)
                                        {
                                            _unitWork.Delete(inv);
                                            InventoryTransaction inventoryTransaction = new InventoryTransaction
                                            {
                                                Type = TransactionType.出库,
                                                UserDef1 = td.OrderCode,
                                                SourceCode = inv.SourceCode,
                                                ContainerCode = inv.ContainerCode,
                                                MaterialCode = inv.MaterialCode,
                                                Batch = inv.Batch,
                                                Lot = inv.Lot,
                                                Qty = inv.Qty,
                                                QrCode = inv.QrCode,
                                                Supplier = inv.Supplier,
                                                ResidualWeight = inv.ResidualWeight,
                                                Retreat = inv.Retreat,
                                                Specification = inv.Specification,
                                                NameDescription = inv.NameDescription,
                                                Unit = inv.Unit,
                                                Size = inv.Size,
                                                NumberofSheets = inv.NumberofSheets,
                                                Groupnum = inv.Groupnum,
                                                OldBatch = inv.OldBatch,
                                                Combo = inv.Combo,
                                                Status = inv.Status,
                                                LocationCode = td.Station,
                                                StackingNumber = inv.StackingNumber,
                                                CreateTime = inv.CreateTime,
                                                UpdateTime = DateTime.Now,
                                                UpdateBy = "IStackerStatusApp"
                                            };
                                            _unitWork.Add(inventoryTransaction);
                                        }
                                    }
                                    
                                }

                                if (td.TaskType != TaskType.盘点)
                                {
                                    //托盘出库完成,解锁并释放仓位
                                    Location loc = _unitWork.Find<Location>(u => u.Code == td.SourceLocation).FirstOrDefault();
                                    if (loc != null)
                                    {
                                        _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                                        {
                                            ContainerCode = "",
                                            Status = "empty",
                                            UpdateBy = "wms",
                                            UpdateTime = DateTime.Now
                                        });
                                    }

                                    //成品库大板仓位解锁
                                    if (cont != null && loc != null)
                                    {
                                        if (cont.Type == ContainerType.木栈板母板_2)
                                        {
                                            Location Cloc = _unitWork.Find<Location>(u => u.Code == loc.ContiguousCode).FirstOrDefault();
                                            if (Cloc != null)
                                            {
                                                _unitWork.Update<Location>(n => n.Id == Cloc.Id && n.Version == Cloc.Version, n => new Location
                                                {
                                                    ContainerCode = "",
                                                    Status = "empty",
                                                    UpdateBy = "wms",
                                                    UpdateTime = DateTime.Now
                                                });
                                            }
                                        }
                                    }
                                }
                            }

                            //   Response.Result = td;

                            //空板补给
                            if (td.TaskType == TaskType.空容器出库)
                            {
                                StoreStation storeStation = _unitWork.Find<StoreStation>(n => n.ContainerCode == td.ContainerCode && n.Status == StoreStationStatus.任务中).FirstOrDefault();
                                if (storeStation != null)
                                {
                                    storeStation.Status = StoreStationStatus.到达站台;
                                    storeStation.UpdateTime = DateTime.Now;
                                    _unitWork.Update(storeStation);
                                }
                                TaskDetail tdPP = _unitWork.Find<TaskDetail>(n => n.TaskType == TaskType.站台到站台 && n.Status == TaskStatus.新建任务 && n.SourceLocation == td.Station && n.ContainerCode == td.ContainerCode).FirstOrDefault();
                                if (tdPP != null)
                                {
                                    tdPP.Status = TaskStatus.待下发任务;
                                    _unitWork.Update(tdPP);
                                }
                                //PP卷B20空板补给
                                TaskDetail tdPJ = _unitWork.Find<TaskDetail>(n => n.TaskType == TaskType.站台到站台 && n.Status == TaskStatus.新建任务 && n.SourceLocation == "OutStationB03" && n.ContainerCode == td.ContainerCode).FirstOrDefault();
                                if (tdPJ != null)
                                {
                                    tdPJ.Status = TaskStatus.待下发任务;
                                    _unitWork.Update(tdPJ);
                                }
                            }
                            //PP卷机台入库
                            //else if (td.TaskType == TaskType.站台到站台 && td.DestinationLocation.Contains("InStationB03") && !td.SourceLocation.Contains("BufferStationB"))
                            //{
                            //    TaskDetail tdPJ = _unitWork.Find<TaskDetail>(n => n.TaskType == TaskType.容器回库 && n.Status == TaskStatus.新建任务 && n.SourceLocation == "InStationB04" && n.ContainerCode == td.ContainerCode).FirstOrDefault();
                            //    if (tdPJ != null)
                            //    {
                            //        tdPJ.Status = TaskStatus.待下发任务;
                            //        _unitWork.Update(tdPJ);
                            //    }
                            //}

                            if (td.TaskType == TaskType.容器出库 && (td.DestinationLocation == "InStationE07" || td.DestinationLocation == "InStationE08"))
                            {
                                TaskDetail tdPP = _unitWork.Find<TaskDetail>(n => n.TaskType == TaskType.站台到站台 && n.Status == TaskStatus.新建任务 && n.SourceLocation == td.Station && n.ContainerCode == td.ContainerCode).FirstOrDefault();
                                if (tdPP != null)
                                {
                                    tdPP.Status = TaskStatus.待下发任务;
                                    _unitWork.Update(tdPP);
                                }
                            }

                            ////成品库木栈板补给逻辑
                            //if (td.Station == "InStationE07" || td.Station == "InStationE08")
                            //{
                            //    TaskDetail detail = null;
                            //    string conCode = "";
                            //    if (td.TaskType == TaskType.空容器出库)
                            //    {
                            //        detail = _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.新建任务 && n.TaskType == TaskType.容器回库 && n.TaskNo == td.TaskNo).OrderBy(n => n.Id).FirstOrDefault();
                            //        conCode = td.ContainerCode;
                            //    }
                            //    else if (td.TaskType == TaskType.容器出库)
                            //    {
                            //        detail = _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.新建任务 && n.TaskType == TaskType.空容器入库 && n.TaskNo == td.TaskNo).OrderBy(n => n.Id).FirstOrDefault();
                            //        conCode = "woodPallet";
                            //    }

                            //    if (detail != null)
                            //    {
                            //        StationRoadway stationRoadway = _unitWork.Find<StationRoadway>(n => n.StationCode == detail.Station).FirstOrDefault();
                            //        Location loc = new Location();
                            //        //成品库找仓位逻辑
                            //        while (true)
                            //        {
                            //            loc = (from loca in _unitWork.Find<Location>(n => n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= 0 && n.Roadway == stationRoadway.RoadWay && n.ContainerCode == "")
                            //                   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(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                            //            //更新仓位对应的容器,如果回库分配的不是原仓位则更新仓位且锁定
                            //            if (loc == null)
                            //            {
                            //                detail.Error = "库内无空仓位";
                            //                _unitWork.Update(detail);
                            //                break;
                            //            }
                            //            else
                            //            {
                            //                int i = _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                            //                {
                            //                    Status = LocationStatus.任务锁定中,
                            //                    ContainerCode = td.ContainerCode,
                            //                    UpdateTime = DateTime.Now
                            //                });

                            //                Container container = _unitWork.Find<Container>(n => n.Code == td.ContainerCode).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)
                            //                        {
                            //                            _unitWork.Update<Location>(n => n.Id == Cloc.Id && n.Version == Cloc.Version, n => new Location
                            //                            {
                            //                                ContainerCode = td.ContainerCode,
                            //                                Status = LocationStatus.任务锁定中,
                            //                                UpdateBy = "wms",
                            //                                UpdateTime = DateTime.Now
                            //                            });
                            //                        }
                            //                    }
                            //                }

                            //                if (i == 1)
                            //                {
                            //                    break;
                            //                }
                            //            }
                            //        }

                            //        if (loc != null)
                            //        {
                            //            Container UpContainer = _unitWork.Find<Container>(n => n.Code == td.ContainerCode).FirstOrDefault();
                            //            if (td.TaskType == TaskType.空容器出库)
                            //            {
                            //                UpContainer.LocationCode = loc.Code;
                            //                UpContainer.Status = ContainerStatus.有;
                            //            }
                            //            _appc.UpdateByTracking(UpContainer);
                            //            List<Inventory> inventories = _unitWork.Find<Inventory>(n => (n.ContainerCode == td.ContainerCode) || (n.ContainerCode == ContainerStatus.空 && n.LocationCode == td.Station)).ToList();
                            //            //更新同个容器中其它物料对应新的回库仓位
                            //            if (inventories.Count > 0)
                            //            {
                            //                foreach (Inventory inv in inventories)
                            //                {
                            //                    if (inv.LocationCode != loc.Code)
                            //                    {
                            //                        if (td.TaskType == TaskType.空容器出库)
                            //                        {
                            //                            inv.LocationCode = loc.Code;
                            //                        }
                            //                        else if (td.TaskType == TaskType.容器出库)
                            //                        {
                            //                            inv.LocationCode = td.DestinationLocation;
                            //                        }

                            //                        if (td.TaskType == TaskType.空容器出库)
                            //                        {
                            //                            inv.ContainerCode = conCode;
                            //                        }
                            //                        else if (td.TaskType == TaskType.容器出库)
                            //                        {
                            //                            inv.ContainerCode = conCode;
                            //                        }
                            //                        inv.SourceCode = detail.OrderCode;
                            //                        _appi.UpdateByTracking(inv);
                            //                    }
                            //                }
                            //            }

                            //            detail.DestinationLocation = loc.Code;
                            //            detail.Status = TaskStatus.待下发任务;
                            //            _unitWork.Update(detail);
                            //        }
                            //    }
                            //}
                        }
                        else if (sTKStatusModel.Status == 40)  //堆垛机入库完成容器到达仓位
                        {
                            string locationStatus = ContainerStatus.;
                            Container container = _unitWork.Find<Container>(u => u.Code == td.ContainerCode).FirstOrDefault();
                            foreach (TaskDetail t in taskDetails)
                            {
                                if (t.TaskType == TaskType.容器回库)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘入库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);

                                    Task pintask = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    if (pintask == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    pintask.FirstStatus = TaskStatus.已经完成;
                                    pintask.LastStatus = TaskStatus.已经完成;
                                    pintask.UpdateBy = "wms";
                                    pintask.UpdateTime = DateTime.Now;
                                    _unitWork.Update(pintask);
                                    Station sta = _unitWork.Find<Station>(n => n.Code == t.SourceLocation).FirstOrDefault();
                                    //if (sta != null && (sta.Type == 10 || sta.Code == "EntranceStationY01") && sta.IsPass == 1)
                                    //{
                                    //    Inventory inv = _unitWork.Find<Inventory>(n => n.ContainerCode == t.ContainerCode).FirstOrDefault();
                                    //    if (inv != null && inv.MaterialCode != "000")
                                    //    {
                                    //        List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == inv.SourceCode).ToList();
                                    //        if (receiptDetails.Count() > 0)
                                    //        {
                                    //            foreach (var receiptDetail in receiptDetails)
                                    //            {
                                    //                if (inv.QrCode != receiptDetail.Project)
                                    //                {
                                    //                    receiptDetail.QtyCompleted = receiptDetail.Qty;
                                    //                    receiptDetail.Status = ReceiptHeaderStatus.过账;
                                    //                    _unitWork.Update(receiptDetail);
                                    //                    string[] qrcodeArr = receiptDetail.Project.Split('%');
                                    //                    var material = qrcodeArr[0];
                                    //                    var Lot = qrcodeArr[1];
                                    //                    var Batch = qrcodeArr[2];
                                    //                    var NewMoCode = "";
                                    //                    if (receiptDetail.MoCode == "余料退库")
                                    //                    {
                                    //                        NewMoCode = "余料退库";
                                    //                    }
                                    //                    if (receiptDetail.MoCode == "叫料退库")
                                    //                    {
                                    //                        NewMoCode = "叫料退库";
                                    //                    }
                                    //                    if (receiptDetail.MoCode == "D621")
                                    //                    {
                                    //                        NewMoCode = "D621";
                                    //                    }
                                    //                    //创建库存
                                    //                    Inventory inventory = new Inventory
                                    //                    {
                                    //                        Batch = Batch,
                                    //                        SourceCode = inv.SourceCode,
                                    //                        ReceiptCode = inv.ReceiptCode,
                                    //                        QrCode = receiptDetail.Project,
                                    //                        ReceiptId = receiptDetail.Id,
                                    //                        ReceiptDetailId = receiptDetail.ReceiptId,
                                    //                        Lot = Lot,
                                    //                        Retreat = NewMoCode,
                                    //                        MaterialCode = material,
                                    //                        Status = inv.Status,
                                    //                        ContainerStatus = inv.ContainerStatus,
                                    //                        Qty = receiptDetail.Qty,
                                    //                        ContainerCode = inv.ContainerCode,
                                    //                        Combo = inv.Combo,
                                    //                        IsFold = inv.IsFold,
                                    //                        IsPack = inv.IsPack,
                                    //                        TaskStatus = InventoryTaskType.任务锁定中,
                                    //                        OldBatch = Batch
                                    //                    };
                                    //                    _appi.Add(inventory);
                                    //                    //创建库存异动
                                    //                    InventoryTransaction inventoryTransaction = new InventoryTransaction
                                    //                    {
                                    //                        Type = TransactionType.入库,
                                    //                        Lot = inventory.Lot,
                                    //                        QrCode = inventory.QrCode,
                                    //                        ContainerCode = inventory.ContainerCode,
                                    //                        UserDef1 = t.TaskNo,
                                    //                        SourceCode = inventory.SourceCode,
                                    //                        LocationCode = t.DestinationLocation,
                                    //                        MaterialCode = inventory.MaterialCode,
                                    //                        Batch = inventory.Batch,
                                    //                        Qty = inventory.Qty,
                                    //                        Status = inventory.Status,
                                    //                        IsPack = inventory.IsPack,
                                    //                        IsFold = inventory.IsFold,
                                    //                        Supplier = inventory.Supplier,
                                    //                        ResidualWeight = inventory.ResidualWeight,
                                    //                        Retreat = inventory.Retreat,
                                    //                        Specification = inventory.Specification,
                                    //                        NameDescription = inventory.NameDescription,
                                    //                        Unit = inventory.Unit,
                                    //                        Size = inventory.Size,
                                    //                        OldBatch = inventory.OldBatch,
                                    //                        Combo = inventory.Combo,
                                    //                        NumberofSheets = inventory.NumberofSheets,
                                    //                        Groupnum = inventory.Groupnum,
                                    //                        StackingNumber = inventory.StackingNumber,
                                    //                        CreateTime = DateTime.Now,
                                    //                        UpdateTime = DateTime.Now,
                                    //                    };
                                    //                    _unitWork.Add(inventoryTransaction);
                                    //                }
                                    //            }
                                    //        }
                                    //    }
                                    //}

                                    //更新库存交易仓位
                                    List<InventoryTransaction> inventoryTransactions = _unitWork.Find<InventoryTransaction>(u => u.ContainerCode == t.ContainerCode && u.UserDef1 == t.TaskNo).ToList();
                                    if (inventoryTransactions.Count > 0)
                                    {
                                        foreach (InventoryTransaction i in inventoryTransactions)
                                        {
                                            i.LocationCode = t.DestinationLocation;
                                            i.UpdateTime = DateTime.Now;
                                            _unitWork.Update(i);
                                        }
                                    }
                                    

                                    //已完成的托盘入库任务迁移至历史任务表
                                    TaskHistory taskHistory = new TaskHistory();
                                    pintask.CopyTo(taskHistory);
                                    taskHistory.Id = null;
                                    _unitWork.Add(taskHistory);
                                    TaskDetailHistory taskDetailHistory = new TaskDetailHistory();
                                    t.CopyTo(taskDetailHistory);
                                    taskDetailHistory.Id = null;
                                    _unitWork.Add(taskDetailHistory);

                                    //删除已完成的托盘入库任务
                                    _unitWork.Delete(pintask);
                                    _unitWork.Delete(t);
                                }
                                else
                                {
                                    t.Status = TaskStatus.已经完成;  //更新任务明细状态为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);
                                    //更新主任务头尾状态
                                    Task task = _context.Set<Task>().AsQueryable().Where(u => u.TaskNo == t.TaskNo).SingleOrDefault();
                                    if (task == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    // Task task = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    TaskDetail tdel = _unitWork.Find<TaskDetail>(u => u.TaskNo == t.TaskNo).OrderBy(a => a.Status).FirstOrDefault();//同一个任务号下面最小的任务状态,包含其它容器的任务,用于更新主任务的尾状态
                                    if (tdel == null)
                                    {
                                        throw new Exception("任务子表:" + t.TaskNo + "不存在!");
                                    }
                                    if (task.FirstStatus < t.Status)
                                    {
                                        task.FirstStatus = t.Status;
                                    }
                                    if (task.LastStatus < tdel.Status)
                                    {
                                        task.LastStatus = tdel.Status;
                                    }
                                    task.UpdateBy = "wms";
                                    task.UpdateTime = DateTime.Now;
                                    _unitWork.Update(task);

                                    //已完成的单据任务迁移至历史任务表
                                    if (task.LastStatus == TaskStatus.已经完成)
                                    {
                                        TaskHistory taskhis = new TaskHistory();
                                        task.CopyTo(taskhis);
                                        taskhis.Id = null;
                                        _unitWork.Add(taskhis);

                                        List<TaskDetail> taskdtls = _context.Set<TaskDetail>().AsQueryable().Where(u => u.TaskNo == task.TaskNo).ToList();
                                        foreach (TaskDetail tdl in taskdtls)
                                        {
                                            TaskDetailHistory taskdthis = new TaskDetailHistory();
                                            tdl.CopyTo(taskdthis);
                                            taskdthis.Id = null;
                                            _unitWork.Add(taskdthis);

                                            _unitWork.Delete(tdl);
                                        }
                                        _unitWork.Delete(task);
                                    }

                                    if (t.TaskType == TaskType.整盘入库 || t.TaskType == TaskType.补充入库)//入库任务类型
                                    {
                                        //更新入库明细单据状态
                                        ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == t.SourceCode && u.ReceiptCode == t.OrderCode && u.MaterialCode == t.MaterialCode && t.OderQty == t.HadQty).FirstOrDefault();
                                        if (receiptDetail != null)
                                        {
                                            receiptDetail.Status = ReceiptHeaderStatus.过账;
                                            receiptDetail.UpdateBy = "wms";
                                            receiptDetail.UpdateTime = DateTime.Now;
                                            _apprd.UpdateByTracking(receiptDetail);
                                            //更新入库主单据头尾状态
                                            // ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(u => u.Code == t.OrderCode).FirstOrDefault();
                                            ReceiptHeader receiptHeader = _context.Set<ReceiptHeader>().AsQueryable().Where(u => u.SourceCode == t.SourceCode).SingleOrDefault();
                                            ReceiptDetail recepdt = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == t.SourceCode).OrderBy(a => a.Status).FirstOrDefault();
                                            if (receiptHeader.FirstStatus < receiptDetail.Status)
                                            {
                                                receiptHeader.FirstStatus = receiptDetail.Status;
                                            }
                                            if (receiptHeader.LastStatus < recepdt.Status)
                                            {
                                                receiptHeader.LastStatus = recepdt.Status;
                                            }
                                            receiptHeader.UpdateBy = "wms";
                                            receiptHeader.UpdateTime = DateTime.Now;
                                            _apprh.UpdateByTracking(receiptHeader);
                                            //已完成的入库单据迁移至历史入库单据,暂以过账状态,后期与ERP对接后以回传状态
                                            if (receiptHeader.LastStatus == ReceiptHeaderStatus.过账)
                                            {
                                                ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                                                receiptHeader.CopyTo(receiptHeaderHistory);
                                                receiptHeaderHistory.Id = null;
                                                _unitWork.Add(receiptHeaderHistory);

                                                List<ReceiptDetail> recepdtls = _context.Set<ReceiptDetail>().AsQueryable().Where(u => u.ReceiptCode == receiptHeader.Code).ToList();
                                                // List<ReceiptDetail> recepdtls = _unitWork.Find<ReceiptDetail>(u => u.ReceiptCode == receiptHeader.Code).ToList();
                                                foreach (ReceiptDetail rd in recepdtls)
                                                {
                                                    ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                                                    rd.CopyTo(receiptDetailHistory);
                                                    receiptDetailHistory.Id = null;
                                                    receiptDetailHistory.ReceiptId = receiptHeaderHistory.Id;
                                                    _unitWork.Add(receiptDetailHistory);

                                                    _unitWork.Delete(rd);
                                                }
                                                _unitWork.Delete(receiptHeader);
                                            }
                                        }
                                    }
                                    else if (t.TaskType == TaskType.空容器入库) //空容器入库任务类型
                                    {
                                        container.Status = ContainerStatus.;
                                        locationStatus = LocationStatus.空容器;
                                    }
                                    else if (t.TaskType == TaskType.移库)
                                    {
                                        if (container.Status == ContainerStatus.)
                                        {
                                            locationStatus = LocationStatus.空容器;
                                        }
                                    }
                                    else if (t.TaskType == TaskType.出库查看)
                                    {

                                    }
                                    //else if (t.TaskType == TaskType.盘点)
                                    //{
                                    //    Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == t.ContainerCode).FirstOrDefault();
                                    //    List<Inventory> invs = _unitWork.Find<Inventory>(n => n.ContainerCode == inventory.ContainerCode).ToList();
                                    //    foreach (Inventory inv in invs)
                                    //    {
                                    //        inv.TaskStatus = "cyclecountLock";
                                    //        _appi.UpdateByTracking(inv);
                                    //    }
                                    //    CyclecountHeader cycher = _unitWork.Find<CyclecountHeader>(n => n.Code == t.OrderCode).FirstOrDefault();
                                    //    CyclecountDetail cycdel = _unitWork.Find<CyclecountDetail>(n => n.Status == ReceiptHeaderStatus.过账 && n.MaterialCode == t.MaterialCode && n.ContainerCode == t.ContainerCode && n.Code == t.OrderCode).FirstOrDefault();
                                    //    CyclecountDetail cycdelEnd = _unitWork.Find<CyclecountDetail>(n => n.Code == t.OrderCode && n.Id != cycdel.Id).OrderBy(n => n.Status).FirstOrDefault();
                                    //    if (cycdel != null)
                                    //    {
                                    //        cycdel.Status = ReceiptHeaderStatus.回传;
                                    //        _unitWork.Update(cycdel);
                                    //        if (cycdelEnd.Status >= cycdel.Status)
                                    //        {
                                    //            cycher.Status = (short)cycdel.Status;
                                    //            _unitWork.Update(cycher);
                                    //        }
                                    //        if (t.OderQty < t.HadQty)
                                    //        {
                                    //            //erp传数据
                                    //        }
                                    //        else if (t.OderQty > t.HadQty)
                                    //        {
                                    //            //erp传数据
                                    //        }
                                    //    }
                                    //}
                                    else
                                    {
                                        throw new Exception("未知任务类型:" + t.TaskType);
                                    }
                                }
                            }

                            //清除该托盘占用的站台
                            Station s = _unitWork.Find<Station>(n => n.Code == td.SourceLocation && n.Containercode == td.ContainerCode && (td.TaskType == TaskType.容器回库 || td.TaskType == TaskType.空容器入库)).FirstOrDefault();
                            if (s != null)
                            {
                                s.Containercode = "";
                                _unitWork.Update(s);
                            }
                            //托盘入库完成,解锁并释放仓位
                            Location Fromloc = _unitWork.Find<Location>(u => u.Code == td.SourceLocation && u.ContainerCode == td.ContainerCode).FirstOrDefault();
                            Location loc = _unitWork.Find<Location>(u => u.Code == td.DestinationLocation).FirstOrDefault();
                            if (loc == null)
                            {
                                throw new Exception("目的仓位:" + td.DestinationLocation + "不存在!");
                            }
                            if(Fromloc != null && td.TaskType == TaskType.移库)
                            {
                                int fromiLoc = _unitWork.Update<Location>(n => n.Id == Fromloc.Id && n.Version == Fromloc.Version, n => new Location
                                {
                                    ContainerCode = "",
                                    Status = "empty",
                                    UpdateBy = "wcs",
                                    UpdateTime = DateTime.Now
                                });
                            }

                            int iLoc = _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                            {
                                ContainerCode = td.ContainerCode,
                                Status = locationStatus,
                                UpdateBy = "wms",
                                UpdateTime = DateTime.Now
                            });

                            //成品库大板仓位上锁
                            if (container != null && loc != null)
                            {
                                if (container.Type == ContainerType.木栈板母板_2)
                                {
                                    Location Cloc = _unitWork.Find<Location>(u => u.Code == loc.ContiguousCode).FirstOrDefault();
                                    if (Cloc != null)
                                    {
                                        _unitWork.Update<Location>(n => n.Id == Cloc.Id && n.Version == Cloc.Version, n => new Location
                                        {
                                            ContainerCode = td.ContainerCode,
                                            Status = locationStatus,
                                            UpdateBy = "wms",
                                            UpdateTime = DateTime.Now
                                        });
                                    }
                                }
                            }

                            List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == td.ContainerCode && n.Status != "cyclecountLock").ToList();
                            //更新同个容器中其它物料对应新的回库仓位
                            if (inventories.Count > 0)
                            {
                                foreach (Inventory inv in inventories)
                                {
                                    if (loc.Roadway == 1)
                                    {
                                        inv.WarehouseType = "YCL_WareCell";
                                    }
                                    if (loc.Roadway == 2)
                                    {
                                        inv.WarehouseType = "PPJ_WareCell";
                                    }
                                    else if (loc.Roadway == 3)
                                    {
                                        inv.WarehouseType = "PPP_WareCell";
                                    }
                                    else if (loc.Roadway == 4)
                                    {
                                        inv.WarehouseType = "TB_WareCell";
                                    }
                                    else if (loc.Roadway == 5)
                                    {
                                        inv.WarehouseType = "CP_WareCell";
                                    }

                                    if (inv.LocationCode != loc.Code)
                                    {
                                        inv.LocationCode = loc.Code;
                                    }

                                    if (inv.TaskStatus == InventoryTaskType.任务锁定中)
                                    {
                                        inv.TaskStatus = InventoryTaskType.无任务;
                                    }
                                    _appi.UpdateByTracking(inv);
                                    InterfaceLog inter = _unitWork.Find<InterfaceLog>(n => n.TaskNo == inv.SourceCode && n.Status != 1).FirstOrDefault();
                                    if (inter != null)
                                    {
                                        inter.ComNum += 1;
                                        _unitWork.Update(inter);
                                    }
                                }
                                
                            }
                            //托盘入库完成,解锁并释放容器
                            if (container == null)
                            {
                                throw new Exception("容器:" + td.ContainerCode + "不存在!");
                            }
                            container.LocationCode = td.DestinationLocation;
                            container.IsLock = ContainerLock.未锁;
                            container.UpdateBy = "wms";
                            container.UpdateTime = DateTime.Now;
                            _unitWork.Update(container);

                            //成品库木栈板补给逻辑
                            if (td.Station == "InStationE07" || td.Station == "InStationE08")
                            {
                                TaskDetail detail = null;
                                if (td.TaskType == TaskType.空容器入库)
                                {
                                    detail = _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.新建任务 && n.TaskType == TaskType.站台到站台 && n.TaskNo == td.TaskNo).OrderBy(n => n.Id).FirstOrDefault();
                                    if (detail != null)
                                    {
                                        detail.Status = TaskStatus.待下发任务;
                                        _unitWork.Update(detail);
                                    }
                                }
                            }

                        }
                        else
                        {
                            Response.Code = 500;
                            Response.Status = false;
                            Response.Message = "不识别的任务状态!";
                        }
                    }
                    else
                    {
                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = "获取任务号错误,未找到此容器的任务!";
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = ex.Message;
                }

                return Response;
            }
        }
    }
}