MesServiceImpl.java 167 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 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746
package com.huaheng.api.mes.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.mes.domain.*;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.common.utils.restful.RestUtil;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.vehicle.domain.Vehicle;
import com.huaheng.pc.config.vehicle.service.VehicleService;
import com.huaheng.pc.config.vehicleType.domain.VehicleType;
import com.huaheng.pc.config.vehicleType.service.VehicleTypeService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.receipt.workOrderDetail.domain.WorkOrderDetail;
import com.huaheng.pc.receipt.workOrderDetail.service.WorkOrderDetailService;
import com.huaheng.pc.receipt.workOrderHeader.domain.WorkOrderHeader;
import com.huaheng.pc.receipt.workOrderHeader.service.WorkOrderHeaderService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 游杰
 */
@Service
public class MesServiceImpl implements MesService {

    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private MaterialTypeService materialTypeService;
    @Resource
    private WorkOrderHeaderService workOrderHeaderService;
    @Resource
    private WorkOrderDetailService workOrderDetailService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private AddressService addressService;
    @Resource
    private MesService mesService;
    @Resource
    private VehicleTypeService vehicleTypeService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private ZoneService zoneService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult receiptOrder(MesOrder mesOrder) {
        List<ReceiptMaterialData> materialDataList = mesOrder.getMaterialDataList();
        BigDecimal totalQty = materialDataList.stream().map(ReceiptMaterialData::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<String> materialCodeList = materialDataList.stream().map(ReceiptMaterialData::getMaterialCode).distinct().collect(Collectors.toList());
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        receiptHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
        receiptHeader.setCode(receiptHeaderService.createCode(mesOrder.getReceiptType()));
        receiptHeader.setReceiptType(mesOrder.getReceiptType());
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setReferCode(mesOrder.getOrderCode());
        receiptHeader.setTotalQty(totalQty);
        receiptHeader.setTotalLines(materialCodeList.size());
        receiptHeader.setOrderCode(mesOrder.getOrderCode());
        receiptHeader.setCreatedBy(StringUtils.isEmpty(ShiroUtils.getLoginName()) ? QuantityConstant.PLATFORM_MES : ShiroUtils.getLoginName());
        receiptHeader.setLastUpdatedBy(StringUtils.isEmpty(ShiroUtils.getLoginName()) ? QuantityConstant.PLATFORM_MES : ShiroUtils.getLoginName());
        boolean success = receiptHeaderService.save(receiptHeader);
        if (!success) {
            throw new ServiceException("入库失败,保存入库单头表失败");
        }
        ReceiptMaterialData receiptMaterialData = materialDataList.stream().findFirst().orElse(null);
        if (receiptMaterialData == null) {
            throw new ServiceException("入库失败,入库物料信息为空");
        }
        Material material = materialService.getUsableByCode(receiptMaterialData.getMaterialCode());
        if (material == null) {
            throw new ServiceException(StringUtils.format("入库失败,根据物料编码 {} 没有找到对应物料信息", receiptMaterialData.getMaterialCode()));
        }
        Boolean qualityStatus = receiptMaterialData.getQualityStatus();
        ReceiptDetail receiptDetail = new ReceiptDetail();
        BeanUtils.copyBeanProp(receiptDetail, receiptMaterialData);
        receiptDetail.setReceiptId(receiptHeader.getId());
        receiptDetail.setReceiptCode(receiptHeader.getCode());
        receiptDetail.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        receiptDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
        receiptDetail.setInventorySts(Boolean.TRUE.equals(qualityStatus) ? QuantityConstant.INVENTORY_STATUS_NG : QuantityConstant.INVENTORY_STATUS_OK);
        receiptDetail.setQty(totalQty);
        receiptDetail.setTaskQty(totalQty);
        receiptDetail.setMaterialCode(material.getCode());
        receiptDetail.setMaterialName(material.getName());
        receiptDetail.setMaterialSpec(material.getSpec());
        receiptDetail.setMaterialUnit(material.getUnit());
        receiptDetail.setDescription(material.getDescription());
        receiptDetail.setHigh(material.getHigh());
        receiptDetail.setWeight(Objects.isNull(receiptMaterialData.getWeight()) ? material.getWeight() : receiptMaterialData.getWeight());
        receiptDetail.setCreatedBy(receiptHeader.getCreatedBy());
        receiptDetail.setLastUpdatedBy(receiptHeader.getLastUpdatedBy());
        success = receiptDetailService.save(receiptDetail);
        if (!success) {
            throw new ServiceException("入库失败,保存入库单明细表失败");
        }
        return AjaxResult.success("创建入库单成功").setData(receiptHeader.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentOrder(MesShipmentOrder mesShipmentOrder) {
        String orderCode = mesShipmentOrder.getOrderCode();
        String taskNo = mesShipmentOrder.getTaskNo();
        BigDecimal orderQty = mesShipmentOrder.getOrderQty();
        List<MesOrderMaterial> mesOrderMaterialList = mesShipmentOrder.getMaterialDataList();

        //工单领料单
        String shipmentType = QuantityConstant.SHIPMENT_BILL_TYPE_OTF;
        String shipmentCode = shipmentHeaderService.createCode(shipmentType);
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setCode(shipmentCode);
        shipmentHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        shipmentHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
        shipmentHeader.setShipmentType(shipmentType);
        shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        shipmentHeader.setChidOrderCode(mesShipmentOrder.getChidOrderCode());
        shipmentHeader.setProductCode(mesShipmentOrder.getProductCode());
        shipmentHeader.setTaskNo(taskNo);
        shipmentHeader.setOrderCode(orderCode);
        shipmentHeader.setOrderQty(orderQty);
        shipmentHeader.setTotalLines(mesOrderMaterialList.size());
        shipmentHeader.setTotalQty(mesOrderMaterialList.stream().map(MesOrderMaterial::getShipmentReferQty).reduce(BigDecimal.ZERO, BigDecimal::add));
        if (!shipmentHeaderService.save(shipmentHeader)) {
            throw new ServiceException("生成领料单失败,保存领料单失败");
        }

        List<ShipmentDetail> shipmentDetailList = new LinkedList<>();
        for (MesOrderMaterial mesOrderMaterial : mesOrderMaterialList) {
            Material material = materialService.getUsableByCode(mesOrderMaterial.getMaterialCode());
            if (material == null) {
                throw new ServiceException(StringUtils.format("生成领料单失败,物料:{} 不存在", mesOrderMaterial.getMaterialCode()));
            }
            // 物料是原材料就冻结库存
            /*if (Boolean.TRUE.equals(mesOrderMaterial.getIsMaterial())) {
                // 查询原材料可用库存
                List<InventoryDetail> inventoryList = inventoryDetailService.getUsableByMaterialCodeAndOrderCode(mesOrderMaterial.getMaterialCode(), orderCode);
                // 冻结库存
                mesService.lockRawInventory(material, inventoryList, mesOrderMaterial, shipmentHeader, orderCode);
            }*/
            /*LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(InventoryDetail::getMaterialCode, material.getCode())
                    .eq(InventoryDetail::getOrderCode, orderCode)
                    .apply("qty != taskQty and TIMESTAMPDIFF(HOUR, created, now()) >= solidifyTime");
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
            if (StringUtils.isEmpty(inventoryDetailList)) {
                throw new ServiceException(StringUtils.format("生成领料单失败,工单:{} 下的物料:{} 可用数量不足", orderCode, material.getCode()));
            }*/
            List<ShipmentDetail> shipmentDetails = createShipmentDetailByQty(material, mesOrderMaterial, shipmentHeader);
            shipmentDetailList.addAll(shipmentDetails);
        }
        if (!shipmentDetailService.saveBatch(shipmentDetailList)) {
            throw new ServiceException("生成领料单失败,保存领料单明细失败");
        }
        return AjaxResult.success("创建领料库单成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void lockRawInventory(Material material, List<InventoryDetail> inventoryList,
                                 MesOrderMaterial mesOrderMaterial, ShipmentHeader shipmentHeader, String orderCode) {
        // 1、数量校验
        if (StringUtils.isEmpty(inventoryList)) {
            throw new ServiceException(StringUtils.format("工单:{} 下的物料 {},{} 暂无可用库存", orderCode, material.getCode(), material.getName()));
        }
        BigDecimal invSumQty = inventoryList.stream()
                .map(InventoryDetail::getQty)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal invSumTaskQty = inventoryList.stream()
                .map(InventoryDetail::getTaskQty)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 可用数量和出库需求数量作比较
        BigDecimal availableQty = invSumQty.subtract(invSumTaskQty);
        BigDecimal shipmentReferQty = mesOrderMaterial.getShipmentReferQty().setScale(0, RoundingMode.CEILING);
        if (availableQty.compareTo(BigDecimal.ZERO) <= -1 || availableQty.compareTo(shipmentReferQty) <= -1) {
            throw new ServiceException(StringUtils.format("物料 [{},{}] 可用数量不足,当前可用数量: {}",
                    material.getCode(), material.getName(), availableQty));
        }
        // 2、优先 先进先出原则,冻结库存
        List<InventoryDetail> invList = new LinkedList<>();
        for (InventoryDetail inventoryDetail : inventoryList) {
            BigDecimal qty = inventoryDetail.getQty();
            if (shipmentReferQty.compareTo(BigDecimal.ZERO) == 0) {
                break;
            }
            if (shipmentReferQty.subtract(qty).compareTo(BigDecimal.ZERO) >= 0) {
                shipmentReferQty = shipmentReferQty.subtract(qty);
                invList.add(inventoryDetail);
            }
        }
        // 冻结一个即冻结整盘
        // 根据确认数量后的库存明细 获取对应的库存头表id,再查询该库存头表下的所有库存明细,接着过滤出与当前物料一致的库存明细,最后冻结
        List<Integer> inventoryHeaderIdList = invList.stream().map(InventoryDetail::getInventoryHeaderId).distinct().collect(Collectors.toList());
        inventoryHeaderIdList.forEach(headerId -> {
            InventoryHeader inventoryHeader = new InventoryHeader();
            inventoryHeader.setId(headerId);
            //inventoryHeader.setShipmentReferCode(shipmentHeader.getReferCode());
            inventoryHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            inventoryHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
            if (!inventoryHeaderService.updateById(inventoryHeader)) {
                throw new ServiceException("领料单下发失败,冻结库存头表失败");
            }
        });
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.getByHeaderIds(inventoryHeaderIdList);
        List<InventoryDetail> inventoryDetails = inventoryDetailList.stream().
                filter(detail -> detail.getMaterialCode().equals(material.getCode()))
                .collect(Collectors.toList());
        if (!inventoryDetailService.updateBatchById(inventoryDetails)) {
            throw new ServiceException("领料单下发失败,冻结库存明细失败");
        }
    }

    public List<ShipmentDetail> createShipmentDetailByQty(Material material, MesOrderMaterial mesOrderMaterial, ShipmentHeader shipmentHeader) {
        List<ShipmentDetail> shipmentDetailList = new LinkedList<>();
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail.setShipmentId(shipmentHeader.getId());
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
        shipmentDetail.setCompanyCode(shipmentHeader.getCompanyCode());
        shipmentDetail.setQty(mesOrderMaterial.getShipmentReferQty());
        shipmentDetail.setMaterialCode(material.getCode());
        shipmentDetail.setMaterialName(material.getName());
        shipmentDetail.setMaterialSpec(material.getSpec());
        shipmentDetail.setMaterialUnit(material.getUnit());
        shipmentDetail.setWeight(material.getWeight());
        // MES下发领料单时,物料没有质量状态,故默认且只能为合格品
        shipmentDetail.setInventorySts(QuantityConstant.INVENTORY_STATUS_OK);
        shipmentDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
        shipmentDetailList.add(shipmentDetail);
        return shipmentDetailList;
    }

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentProduct(MesShipmentProduct mesShipmentProduct) {
        String taskNo = mesShipmentProduct.getTaskNo();
        String orderCode = mesShipmentProduct.getOrderCode();
        String containerCode = mesShipmentProduct.getContainerCode();
        String vehicleCode = mesShipmentProduct.getVehicleCode();
        String fromPort = mesShipmentProduct.getFromPort();
        String toPort = mesShipmentProduct.getToPort();
        List<MaterialData> materialDataList = mesShipmentProduct.getMaterialDataList();

        return workTaskService.createOverStation(containerCode, vehicleCode, fromPort, toPort, taskNo, orderCode, materialDataList);
    }*/

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createOverStation(MesOverStationDto mesOverStationDto) {
        String containerCode = mesOverStationDto.getContainerCode();
        Container container = containerService.getByCode(containerCode);
        if (container == null) {
            throw new ServiceException(StringUtils.format("换站失败,根据载具编码:{} 未找到载具", containerCode));
        }
        if (QuantityConstant.STATUS_LOCATION_LOCK.equals(container.getStatus())) {
            throw new ServiceException(StringUtils.format("换站失败,载具:{} 已锁定", container.getCode()));
        }
        String vehicleCode = mesOverStationDto.getVehicleCode();
        Vehicle vehicle = vehicleService.getByCode(vehicleCode);
        if (vehicle == null) {
            throw new ServiceException(StringUtils.format("换站失败,根据盛具编码:{} 未找到盛具", vehicleCode));
        }
        if (StringUtils.isNotEmpty(container.getVehicleCode())) {
            throw new ServiceException(StringUtils.format("换站失败,载具:{} 已和盛具:{} 绑定", container.getCode(), vehicle.getCode()));
        }
        String fromPort = mesOverStationDto.getFromPort();
        String toPort = mesOverStationDto.getToPort();
        if (fromPort.equals(toPort)) {
            throw new ServiceException("换站失败,起始料点不可与目标料点一致");
        }

        boolean success = taskHeaderService.createOverStationTask(mesOverStationDto, container, vehicle);
        if (!success) {
            throw new ServiceException("换站失败,创建换站任务失败");
        }
        // 锁定载具
        success = containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_CONTAINER_LOCK);
        if (!success) {
            throw new ServiceException("换站失败,锁定载具失败");
        }
        // 载具和盛具绑定
        success = containerService.updateVehicle(container.getCode(), vehicle.getCode(), vehicle.getVehicleType());
        if (!success) {
            throw new ServiceException("换站失败,载具与盛具绑定失败");
        }
        return AjaxResult.success("创建换站任务成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult wmsReceipt(MesReceipt mesReceipt) {
        String orderCode = mesReceipt.getOrderCode();
        MesOrder mesOrder = new MesOrder();
        mesOrder.setOrderCode(orderCode);
        mesOrder.setTaskNo(mesReceipt.getTaskNo());
        mesOrder.setMaterialDataList(mesReceipt.getReceiptMaterialDataList());
        mesOrder.setReceiptType(mesReceipt.getReceiptType());
        AjaxResult ajaxResult = mesService.receiptOrder(mesOrder);
        if (Boolean.TRUE.equals(ajaxResult.hasErr())) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        Map<String, List<ReceiptMaterialData>> collect = mesReceipt.getReceiptMaterialDataList()
                .stream().collect(Collectors.groupingBy(ReceiptMaterialData::getVehicleCode));
        for (Map.Entry<String, List<ReceiptMaterialData>> receiptDataEntry : collect.entrySet()) {
            String vehicleCode = receiptDataEntry.getKey();
            List<ReceiptMaterialData> receiptMaterialDataList = receiptDataEntry.getValue();
            // 生成虚拟载具用于组盘
            Container virtualContainer = containerService.createVirtualContainer();
            // 校验盛具
            Vehicle vehicle = vehicleService.getVehicleByCode(vehicleCode, QuantityConstant.DEFAULT_WAREHOUSE);
            if (StringUtils.isNull(vehicle)) {
                throw new ServiceException(StringUtils.format("组盘失败,不存在盛具编码 {}", vehicleCode));
            }
            Container realContainer = containerService.getByVehicleCode(vehicle.getCode());
            if (realContainer != null) {
                throw new ServiceException(StringUtils.format("组盘失败,盛具:{} 已和载具:{} 绑定", vehicle.getCode(), realContainer.getCode()));
            }
            boolean success;
            // 新建保存组盘头表记录
            LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderQueryWrapper = Wrappers.lambdaQuery();
            receiptContainerHeaderQueryWrapper.eq(ReceiptContainerHeader::getVehicleCode, vehicleCode)
                    .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD);
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(receiptContainerHeaderQueryWrapper);

            // 若组过盘则删除原来的组盘记录和单据
            if (StringUtils.isNotNull(receiptContainerHeader)) {
                if (receiptContainerHeader.getStatus() >= QuantityConstant.RECEIPT_CONTAINER_TASK && receiptContainerHeader.getStatus() < QuantityConstant.RECEIPT_CONTAINER_FINISHED) {
                    throw new ServiceException("组盘失败,载具已经生成任务不能放物料了");
                }
                success = receiptContainerHeaderService.removeById(receiptContainerHeader);
                if (!success) {
                    throw new ServiceException("重新组盘失败,删除组盘头表失败");
                }
                List<ReceiptContainerDetail> receiptContainerDetailList = receiptContainerDetailService.getByHeaderId(receiptContainerHeader.getId());
                List<Integer> receiptContainerDetailIdList = receiptContainerDetailList.stream()
                        .map(ReceiptContainerDetail::getId)
                        .collect(Collectors.toList());
                success = receiptContainerDetailService.removeByIds(receiptContainerDetailIdList);
                if (!success) {
                    throw new ServiceException("重新组盘失败,删除组盘明细失败");
                }
                List<Integer> receiptHeaderIdList = receiptContainerDetailList.stream()
                        .map(ReceiptContainerDetail::getReceiptId)
                        .distinct()
                        .collect(Collectors.toList());
                for (Integer receiptHeaderId : receiptHeaderIdList) {
                    success = receiptHeaderService.removeById(receiptHeaderId);
                    if (!success) {
                        throw new ServiceException("重新组盘失败,删除单据头表失败");
                    }
                    LambdaQueryWrapper<ReceiptDetail> queryWrapper = Wrappers.lambdaQuery();
                    queryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeaderId);
                    success = receiptDetailService.remove(queryWrapper);
                    if (!success) {
                        throw new ServiceException("重新组盘失败,删除单据明细失败");
                    }
                }
            }

            // 组盘后,将虚拟载具和盛具绑定
            if (!containerService.updateVehicle(virtualContainer.getCode(), vehicle.getCode(), vehicle.getVehicleType())) {
                throw new ServiceException("传爆管入库组盘失败,虚拟载具与盛具绑定失败");
            }

            String orderNumber = mesReceipt.getOrderNumber();
            String batchNumber = mesReceipt.getBatchNumber();
            // 根据工单号查询工单物料明细,校验此次入库的物料是否在工单明细清单中,不在则提示异常。
            WorkOrderHeader workOrderHeader = workOrderHeaderService.getOrderByCode(orderCode);
            if (workOrderHeader == null) {
                throw new ServiceException(StringUtils.format("组盘失败,根据工单号:{} 未找到工单"));
            }
            if (!QuantityConstant.WORK_ORDER_BUILD.equals(workOrderHeader.getOrderStatus())
                    && !QuantityConstant.WORK_ORDER_RELEASED.equals(workOrderHeader.getOrderStatus())
                    && !QuantityConstant.WORK_ORDER_PRODUCING.equals(workOrderHeader.getOrderStatus())) {
                throw new ServiceException(StringUtils.format("组盘失败,工单:{} 当前状态不可进行入库操作",
                        workOrderHeader.getOrderCode()));
            }
            List<WorkOrderDetail> workOrderDetailList = workOrderDetailService.getByHeaderId(workOrderHeader.getId());
            if (StringUtils.isEmpty(workOrderDetailList)) {
                throw new ServiceException(StringUtils.format(StringUtils.format("组盘失败,工单:{} 下没有物料明细", orderCode)));
            }
            List<String> materialCodeList = workOrderDetailList.stream().map(WorkOrderDetail::getMaterialCode).collect(Collectors.toList());
            for (ReceiptMaterialData data : receiptMaterialDataList) {
                Material material = materialService.getMaterialByCode(data.getMaterialCode(), QuantityConstant.DEFAULT_WAREHOUSE);
                if (material == null) {
                    throw new ServiceException(StringUtils.format("组盘失败,根据物料编码:{} 没有找到对应物料信息", data.getMaterialCode()));
                }
                if (!materialCodeList.contains(material.getCode())) {
                    throw new ServiceException(StringUtils.format("组盘失败,工单:{} 下的物料明细中不存在入库物料:{}",
                            orderCode, material.getCode()));
                }
                if (StringUtils.isNull(data.getLocationNoX())) {
                    throw new ServiceException("组盘失败,物料有 x 坐标为空的,请检查");
                }
                if (StringUtils.isNull(data.getLocationNoY())) {
                    throw new ServiceException("组盘失败,物料有 y 坐标为空的,请检查");
                }
                String locationNoX = data.getLocationNoX().toString();
                String locationNoY = data.getLocationNoY().toString();
                if (StringUtils.isNull(data.getQty()) || data.getQty().equals(BigDecimal.ZERO)) {
                    throw new ServiceException(StringUtils.format("组盘失败,坐标 {},{} 的物料数量为空或为0", locationNoX
                            , locationNoY));
                }
                if (StringUtils.isEmpty(data.getTracingNo())) {
                    throw new ServiceException(StringUtils.format("组盘失败,坐标:{},{} 的物料追溯码为空",
                            locationNoX, locationNoY));
                }
                if (StringUtils.isNull(data.getQualityStatus())) {
                    throw new ServiceException(StringUtils.format("组盘失败,坐标:{},{} 的物料质量状态为空",
                            locationNoX, locationNoY));
                }
                // 校验物料绑定的盛具类型
                if (!vehicle.getVehicleType().equals(material.getVehicleTypeCode())) {
                    throw new ServiceException(StringUtils.format("组盘失败,入库盛具类型 {} 与物料 {} 绑定的盛具类型 {} 不一致",
                            vehicle.getVehicleType(), material.getName(), material.getVehicleTypeCode()));
                }
                // 校验物料是否入立库(根据是否物料分区判断)
                if (StringUtils.isEmpty(material.getType())) {
                    throw new ServiceException(StringUtils.format("组盘失败,物料:{} 未设置物料类别", material.getName()));
                }
                MaterialType materialType = materialTypeService.getByCode(material.getType());
                if (materialType == null) {
                    throw new ServiceException(StringUtils.format("组盘失败,物料类别编码 {} 不存在", material.getType()));
                }
                String materialAreaCode = materialType.getMaterialAreaCode();
                if (StringUtils.isEmpty(materialAreaCode)) {
                    throw new ServiceException(StringUtils.format("组盘失败,物料:{} 所属类别:{} 未配置物料分区", material.getName(), materialType.getName()));
                }
            }
            // 校验同一个xy坐标的物料 数量只能为1
            if (!QuantityConstant.VEHICLE_TYPE_COMMON.equals(vehicle.getVehicleType())) {
                ReceiptMaterialData qtyNotZeroMaterial = receiptMaterialDataList.stream()
                        .filter(x -> x.getQty().compareTo(BigDecimal.ONE) != 0)
                        .findAny()
                        .orElse(null);
                if (qtyNotZeroMaterial != null) {
                    throw new ServiceException(StringUtils.format("组盘失败,物料坐标 {},{} 的数量异常,请检查数量",
                            qtyNotZeroMaterial.getLocationNoX(),
                            qtyNotZeroMaterial.getLocationNoY()));
                }
                Map<String, List<ReceiptMaterialData>> materialGroupByXyMap = receiptMaterialDataList.stream()
                        .collect(Collectors.groupingBy(material -> material.getLocationNoX().toString().concat(",").concat(material.getLocationNoY().toString())));
                for (Map.Entry<String, List<ReceiptMaterialData>> entry : materialGroupByXyMap.entrySet()) {
                    if (entry.getValue().size() > 1) {
                        throw new ServiceException(StringUtils.format("组盘失败,物料坐标 {} 重复,请检查坐标", entry.getKey()));
                    }
                }
            }

            LambdaQueryWrapper<ReceiptHeader> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(ReceiptHeader::getCode, ajaxResult.getData());
            ReceiptHeader receiptHeader = receiptHeaderService.getOne(wrapper);
            if (receiptHeader == null) {
                throw new ServiceException(StringUtils.format("组盘失败,未找到上游单号为:{} 的入库单", mesReceipt.getTaskNo()));
            }
            //入库单id查询入库单详情
            LambdaQueryWrapper<ReceiptDetail> receiptDetailQueryWrapper = Wrappers.lambdaQuery();
            receiptDetailQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
            List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailQueryWrapper);
            if (StringUtils.isEmpty(receiptDetailList)) {
                throw new ServiceException(StringUtils.format("组盘失败,未找到入库单:{} 的详情", receiptHeader.getId()));
            }
            receiptContainerHeader = new ReceiptContainerHeader();
            receiptContainerHeader.setReceiptCode(ajaxResult.getData().toString());
            receiptContainerHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            receiptContainerHeader.setContainerCode(virtualContainer.getCode());
            receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
            receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setOrderCode(orderCode);
            receiptContainerHeader.setOrderNumber(orderNumber);
            receiptContainerHeader.setBatchNumber(batchNumber);
            receiptContainerHeader.setVehicleCode(vehicle.getCode());
            if (!receiptContainerHeaderService.save(receiptContainerHeader)) {
                throw new ServiceException("组盘失败,入库组盘头表保存失败");
            }
            List<ReceiptContainerDetail> detailList = new ArrayList<>();
            //入库组盘详情
            for (ReceiptMaterialData data : receiptMaterialDataList) {
                Material material = materialService.getUsableByCode(data.getMaterialCode());
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetail.setWarehouseCode(receiptContainerHeader.getWarehouseCode());
                ReceiptDetail receiptDetail = receiptDetailList.stream().filter(x -> x.getMaterialCode().equals(data.getMaterialCode())).findFirst().orElse(null);
                if (receiptDetail == null) {
                    throw new ServiceException(StringUtils.format("入库失败,入库单:{} 明细中不存在物料:{}", receiptHeader.getCode(), data.getMaterialCode()));
                }
                receiptContainerDetail.setReceiptId(receiptDetail.getReceiptId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptCode(receiptDetail.getReceiptCode());
                receiptContainerDetail.setSupplierCode(data.getSupplierCode());
                receiptContainerDetail.setSupplierBatch(data.getSupplierBatch());
                receiptContainerDetail.setSupplierTracingNo(data.getSupplierTracingNo());
                receiptContainerDetail.setLocationNoX(data.getLocationNoX());
                receiptContainerDetail.setLocationNoY(data.getLocationNoY());
                receiptContainerDetail.setTracingNo(data.getTracingNo());
                receiptContainerDetail.setFlowCode(data.getFlowCode());
                receiptContainerDetail.setTaskType(receiptContainerHeader.getTaskType());
                receiptContainerDetail.setContainerCode(receiptContainerHeader.getContainerCode());
                receiptContainerDetail.setCompanyCode(receiptContainerHeader.getCompanyCode());
                receiptContainerDetail.setMaterialCode(data.getMaterialCode());
                receiptContainerDetail.setMaterialName(material.getName());
                receiptContainerDetail.setMaterialSpec(material.getSpec());
                receiptContainerDetail.setMaterialUnit(material.getUnit());
                receiptContainerDetail.setDescription(material.getDescription());
                receiptContainerDetail.setWeight(receiptDetail.getWeight());
                receiptContainerDetail.setHigh(material.getHigh());
                receiptContainerDetail.setSolidifyTime(mesReceipt.getSolidifyTime());
                receiptContainerDetail.setQty(data.getQty());
                receiptContainerDetail.setInventorySts(Boolean.FALSE.equals(data.getQualityStatus()) ? QuantityConstant.INVENTORY_STATUS_OK : QuantityConstant.INVENTORY_STATUS_NG);
                detailList.add(receiptContainerDetail);

                ReceiptDetail updateReceiptDetail = new ReceiptDetail();
                updateReceiptDetail.setId(receiptDetail.getId());
                updateReceiptDetail.setProcessStamp(String.valueOf(QuantityConstant.RECEIPT_HEADER_WAIT));
                if (!receiptDetailService.updateById(updateReceiptDetail)) {
                    throw new ServiceException("入库失败,更新入库单详情失败");
                }
                //更新头表状态
                receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
            }
            if (!receiptContainerDetailService.saveBatch(detailList)) {
                throw new ServiceException("组盘失败,保存入库组盘详情失败");
            }
        }
        return AjaxResult.success("组盘成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult receipt(MesReceipt mesReceipt) {
        // 校验载具
        String containerCode = mesReceipt.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            throw new ServiceException("入库失败,载具编码为空");
        }
        Container container = containerService.getContainerByCode(containerCode, QuantityConstant.DEFAULT_WAREHOUSE);
        if (StringUtils.isNull(container)) {
            throw new ServiceException(StringUtils.format("入库失败,载具编码:{} 不存在", containerCode));
        }
        Location location = locationService.getLocationByContainerCode(containerCode);
        if (location != null) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已在库位:{} 上", containerCode, location.getCode()));
        }
        InventoryHeader inventoryHeader = inventoryHeaderService.getByContainerCode(containerCode);
        if (inventoryHeader != null) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已经存在库存", containerCode));
        }
        // 校验盛具
        Vehicle vehicle = vehicleService.getVehicleByCode(mesReceipt.getVehicleCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        if (StringUtils.isNull(vehicle)) {
            throw new ServiceException(StringUtils.format("入库失败,不存在盛具编码:{}", mesReceipt.getVehicleCode()));
        }
        if (StringUtils.isNotEmpty(container.getVehicleCode()) &&
                !container.getVehicleCode().equals(mesReceipt.getVehicleCode())) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已和盛具:{} 绑定",
                    container.getCode(), container.getVehicleCode()));
        }
        Container containerByVehicleCode = containerService.getByVehicleCode(mesReceipt.getVehicleCode());
        if (containerByVehicleCode != null) {
            throw new ServiceException(StringUtils.format("入库失败,盛具:{} 已和载具:{} 绑定",
                    mesReceipt.getVehicleCode(), containerByVehicleCode.getCode()));
        }
        List<ReceiptMaterialData> receiptMaterialDataList = mesReceipt.getReceiptMaterialDataList();
        receiptMaterialDataList = receiptMaterialDataList.stream().filter(x -> Boolean.TRUE.equals(x.getIsMaterial()))
                .collect(Collectors.toList());
        if (receiptMaterialDataList.isEmpty()) {
            throw new ServiceException("入库失败,没有有效的入库物料信息");
        }
        for (ReceiptMaterialData data : receiptMaterialDataList) {
            Material material = materialService.getMaterialByCode(data.getMaterialCode(), QuantityConstant.DEFAULT_WAREHOUSE);
            if (material == null) {
                throw new ServiceException(StringUtils.format("入库失败,根据物料编码:{} 没有找到对应物料信息", data.getMaterialCode()));
            }
            if (StringUtils.isNull(data.getLocationNoX())) {
                throw new ServiceException("入库失败,物料有 x 坐标为空的,请检查");
            }
            if (StringUtils.isNull(data.getLocationNoY())) {
                throw new ServiceException("入库失败,物料有 y 坐标为空的,请检查");
            }
            if (StringUtils.isNull(data.getQty()) || data.getQty().equals(BigDecimal.ZERO)) {
                throw new ServiceException(StringUtils.format("入库失败,坐标:{},{} 的物料数量为空或为0",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            if (StringUtils.isEmpty(data.getTracingNo())) {
                throw new ServiceException(StringUtils.format("入库失败,坐标:{},{} 的物料追溯码为空",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            if (StringUtils.isNull(data.getQualityStatus())) {
                throw new ServiceException(StringUtils.format("入库失败,坐标:{},{} 的物料质量状态为空",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            if (Objects.isNull(data.getMaterialInspectStatus())) {
                return AjaxResult.error("入库失败,坐标:{},{} 的质检状态为空");
            }
            // 校验物料绑定的盛具类型
            if (!vehicle.getVehicleType().equals(material.getVehicleTypeCode())) {
                throw new ServiceException(StringUtils.format("入库失败,入库盛具类型:{} 与物料:{} 绑定的盛具类型:{} 不一致",
                        vehicle.getVehicleType(), material.getName(), material.getVehicleTypeCode()));
            }
            if (StringUtils.isEmpty(material.getType())) {
                throw new ServiceException(StringUtils.format("入库失败,物料:{} 未设置物料类别", material.getName()));
            }
            MaterialType materialType = materialTypeService.getByCode(material.getType());
            if (materialType == null) {
                throw new ServiceException(StringUtils.format("入库失败,物料类别编码:{} 不存在", material.getType()));
            }
            String materialAreaCode = materialType.getMaterialAreaCode();
            if (StringUtils.isEmpty(materialAreaCode)) {
                throw new ServiceException(StringUtils.format("入库失败,物料:{} 所属类别:{} 未配置物料分区", material.getName(), materialType.getName()));
            }
        }

        // 校验同一个xy坐标的物料 数量只能为1
        if (!QuantityConstant.VEHICLE_TYPE_COMMON.equals(vehicle.getVehicleType())) {
            ReceiptMaterialData qtyNotZeroMaterial = receiptMaterialDataList.stream()
                    .filter(x -> x.getQty().compareTo(BigDecimal.ONE) != 0)
                    .findAny()
                    .orElse(null);
            if (StringUtils.isNotNull(qtyNotZeroMaterial)) {
                throw new ServiceException(StringUtils.format("入库失败,物料坐标:{},{} 的数量异常,请检查数量",
                        qtyNotZeroMaterial.getLocationNoX(),
                        qtyNotZeroMaterial.getLocationNoY()));
            }
            Map<String, List<ReceiptMaterialData>> materialGroupByXyMap = receiptMaterialDataList.stream()
                    .collect(Collectors.groupingBy(material -> material.getLocationNoX().toString().concat(",").concat(material.getLocationNoY().toString())));
            for (Map.Entry<String, List<ReceiptMaterialData>> entry : materialGroupByXyMap.entrySet()) {
                if (entry.getValue().size() > 1) {
                    throw new ServiceException(StringUtils.format("入库失败,物料坐标:{} 重复,请检查坐标", entry.getKey()));
                }
            }
        }
        MesOrder mesOrder = new MesOrder();
        mesOrder.setTaskNo(mesReceipt.getTaskNo());
        mesOrder.setOrderCode(mesReceipt.getOrderCode());
        mesOrder.setMaterialDataList(receiptMaterialDataList);
        mesOrder.setReceiptType(mesReceipt.getReceiptType());
        // 手动生成入库单
        AjaxResult ajaxResult = mesService.receiptOrder(mesOrder);
        if (Boolean.TRUE.equals(ajaxResult.hasErr())) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        // 保存组盘头表
        // 根据载具编码查询组盘表头记录
        LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptContainerHeader::getContainerCode, mesReceipt.getContainerCode())
                .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD);
        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(lambda);

        if (StringUtils.isNull(receiptContainerHeader)) {
            receiptContainerHeader = new ReceiptContainerHeader();
            receiptContainerHeader.setReceiptCode(ajaxResult.getData().toString());
            receiptContainerHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            receiptContainerHeader.setContainerCode(container.getCode());
            receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
            receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setTaskNo(mesReceipt.getTaskNo());
            receiptContainerHeader.setOrderCode(mesReceipt.getOrderCode());
            receiptContainerHeader.setVehicleCode(vehicle.getCode());
            receiptContainerHeader.setPort(mesReceipt.getFromPort());

            WorkOrderHeader workOrderHeader = workOrderHeaderService.getOrderByCode(mesReceipt.getOrderCode());
            if (workOrderHeader != null) {
                receiptContainerHeader.setBatchNumber(workOrderHeader.getBatchNumber());
                receiptContainerHeader.setOrderNumber(workOrderHeader.getOrderNumber());
            }
            if (!receiptContainerHeaderService.save(receiptContainerHeader)) {
                throw new ServiceException("入库失败,入库组盘头表保存失败");
            }
            // 组盘的时候,将载具和盛具绑定
            if (!containerService.updateVehicle(container.getCode(), vehicle.getCode(), vehicle.getVehicleType())) {
                throw new ServiceException("入库失败,载具与盛具绑定失败");
            }
        } else {
            if (receiptContainerHeader.getStatus() >= QuantityConstant.RECEIPT_CONTAINER_TASK && receiptContainerHeader.getStatus() < QuantityConstant.RECEIPT_CONTAINER_FINISHED) {
                throw new ServiceException("入库失败,载具已经生成任务,不能放物料了!");
            }
        }
        LambdaQueryWrapper<ReceiptHeader> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReceiptHeader::getCode, ajaxResult.getData());
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(wrapper);
        if (receiptHeader == null) {
            throw new ServiceException(StringUtils.format("入库失败,未找到上游单号为:{} 的入库单", mesReceipt.getTaskNo()));
        }
        //入库单id查询入库单详情
        LambdaQueryWrapper<ReceiptDetail> receiptDetailQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailQueryWrapper);
        if (StringUtils.isEmpty(receiptDetailList)) {
            throw new ServiceException(StringUtils.format("入库失败,未找到入库单:{} 的详情", receiptHeader.getId()));
        }
        //入库单详情的物料条码比较物料入库的物料条码
        for (ReceiptDetail receiptDetail : receiptDetailList) {
            //更新入库单详情的收货数量
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("入库失败,更新入库单详情失败");
            }
            //如果单据数量等于已收数量,更新入库详情状态和入库单状态
            if (receiptDetail.getQty().compareTo(receiptDetail.getTaskQty()) == 0) {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RECEIVING.toString());
                if (!receiptDetailService.updateById(receiptDetail)) {
                    throw new ServiceException("入库失败,更新入库详情处理标记失败");
                }
            }
            receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
        }
        List<ReceiptContainerDetail> detailList = new ArrayList<>();
        for (ReceiptMaterialData data : receiptMaterialDataList) {
            // 没有物料不算库存
            if (Boolean.FALSE.equals(data.getIsMaterial())) {
                continue;
            }
            Material material = materialService.getMaterialByCode(data.getMaterialCode(), QuantityConstant.DEFAULT_WAREHOUSE);
            ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
            BeanUtils.copyBeanProp(receiptContainerDetail, data);
            receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
            receiptContainerDetail.setWarehouseCode(receiptContainerHeader.getWarehouseCode());
            ReceiptDetail receiptDetail = receiptDetailList.stream().filter(x -> x.getMaterialCode().equals(data.getMaterialCode())).findFirst().orElse(null);
            if (receiptDetail == null) {
                throw new ServiceException(StringUtils.format("入库失败,入库单:{} 明细中不存在物料:{}", receiptHeader.getCode(), data.getMaterialCode()));
            }
            receiptContainerDetail.setReceiptId(receiptDetail.getReceiptId());
            receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
            receiptContainerDetail.setReceiptCode(receiptDetail.getReceiptCode());
            receiptContainerDetail.setTaskType(receiptContainerHeader.getTaskType());
            receiptContainerDetail.setContainerCode(receiptContainerHeader.getContainerCode());
            receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            receiptContainerDetail.setMaterialCode(material.getCode());
            receiptContainerDetail.setMaterialName(material.getName());
            receiptContainerDetail.setMaterialSpec(material.getSpec());
            receiptContainerDetail.setMaterialUnit(material.getUnit());
            receiptContainerDetail.setDescription(material.getDescription());
            receiptContainerDetail.setWeight(receiptDetail.getWeight());
            receiptContainerDetail.setHigh(material.getHigh());
            receiptContainerDetail.setSolidifyTime(mesReceipt.getSolidifyTime());
            receiptContainerDetail.setInventorySts(Boolean.FALSE.equals(data.getQualityStatus()) ? QuantityConstant.INVENTORY_STATUS_OK : QuantityConstant.INVENTORY_STATUS_NG);
            receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            detailList.add(receiptContainerDetail);
        }
        if (!receiptContainerDetailService.saveBatch(detailList)) {
            throw new ServiceException("入库失败,保存入库组盘详情失败");
        }
        //生成入库任务
        List<Integer> idList = Arrays.asList(Convert.toIntArray(String.valueOf(receiptContainerHeader.getId())));
        return receiptTaskService.createReceiptTask(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipment(MesShipment mesShipment) {
        ShipmentCombinationModel model = new ShipmentCombinationModel();
        model.setTaskNo(mesShipment.getTaskNo());
        model.setOrderCode(mesShipment.getOrderCode());
        model.setToPort(mesShipment.getToPort());
        model.setShipmentMaterialDataList(mesShipment.getShipmentMaterialDataList());
        List<ShipmentContainerHeader> containerHeaderList = shipmentContainerHeaderService.combination(model);
        if (StringUtils.isEmpty(containerHeaderList)) {
            throw new ServiceException("出库失败,出库配盘失败");
        }
        List<Integer> shipmentContainerHeaderIdList = containerHeaderList.stream()
                .map(ShipmentContainerHeader::getId)
                .collect(Collectors.toList());
        return shipmentContainerHeaderService.createTask(shipmentContainerHeaderIdList, mesShipment.getSequence(), mesShipment.getSequenceCount(), mesShipment.getSequenceGroup());
    }

    @Override
    public AjaxResult searchInventory(MesSearch mesSearch) {
        // 查询物料信息
        String orderCode = mesSearch.getOrderCode();
        List<MaterialDataQuery> mesSearchMaterialDataList = new ArrayList<>();
        List<MaterialDataQuery> materialDataList = mesSearch.getMaterialDataList();
        if (materialDataList != null && !materialDataList.isEmpty()) {
            for (MaterialDataQuery materialData : materialDataList) {
                // 计算固化时间
                LambdaQueryWrapper<InventoryDetail> inventoryDetailQueryWrapper = Wrappers.lambdaQuery();
                inventoryDetailQueryWrapper
                        .eq(InventoryDetail::getMaterialCode, materialData.getMaterialCode())
                        .and(StringUtils.isEmpty(materialData.getFlowCode()), wrapper -> wrapper.eq(InventoryDetail::getFlowCode, StringUtils.EMPTY).or().isNull(InventoryDetail::getFlowCode))
                        .eq(StringUtils.isNotEmpty(materialData.getFlowCode()), InventoryDetail::getFlowCode, materialData.getFlowCode())
                        .eq(!Objects.isNull(materialData.getMaterialInspectStatus()), InventoryDetail::getMaterialInspectStatus, materialData.getMaterialInspectStatus())
                        .eq(StringUtils.isNotEmpty(orderCode), InventoryDetail::getOrderCode, orderCode)
                        .apply("taskQty != qty AND TRUNCATE(TIME_TO_SEC(TIMEDIFF(now(), created)) / 3600, 2) >= solidifyTime");
                // 获取某物料的全部库存
                List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailQueryWrapper);
                BigDecimal allInventoryQty = BigDecimal.ZERO;
                if (StringUtils.isNotEmpty(inventoryDetailList)) {
                    allInventoryQty = inventoryDetailList.stream()
                            .map(InventoryDetail::getQty)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                }
                materialData.setUsableQty(allInventoryQty);
                materialData.setMaterialCode(materialData.getMaterialCode());
                mesSearchMaterialDataList.add(materialData);
            }
        }
        mesSearch.setMaterialDataList(mesSearchMaterialDataList);

        // 查询空盛具
        List<VehicleDataQuery> vehicleDataQueryList = mesSearch.getVehicleDataList();
        if (vehicleDataQueryList != null && !vehicleDataQueryList.isEmpty()) {
            List<VehicleDataQuery> mesSearchVehicleDataList = new ArrayList<>();
            vehicleDataQueryList.forEach(vehicleType -> {
                List<Container> containerList = containerService.getEmptyByVehicleType(vehicleType.getCode());
                VehicleDataQuery mesSearchVehicleData = new VehicleDataQuery();
                mesSearchVehicleData.setCode(vehicleType.getCode());
                mesSearchVehicleData.setUsableQty(containerList.size());
                mesSearchVehicleDataList.add(mesSearchVehicleData);
            });
            mesSearch.setVehicleDataList(mesSearchVehicleDataList);
        }
        return AjaxResult.success(mesSearch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult workOrder(MesWorkOrder mesWorkOrder) {
        String taskNo = mesWorkOrder.getTaskNo();
        if (StringUtils.isEmpty(taskNo)) {
            return AjaxResult.error("下发工单失败,任务号为空");
        }
        String orderCode = mesWorkOrder.getOrderCode();
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("下发工单失败,工单号为空");
        }
        String batchNumber = mesWorkOrder.getBatchNumber();
        if (StringUtils.isEmpty(batchNumber)) {
            return AjaxResult.error("生产批号为空");
        }
        String orderNumber = mesWorkOrder.getOrderNumber();
        if (StringUtils.isEmpty(orderNumber)) {
            return AjaxResult.error("生产令号为空");
        }
        Integer orderStatus = mesWorkOrder.getOrderStatus();
        if (orderStatus == null) {
            return AjaxResult.error("下发工单失败,工单状态为空");
        }
        WorkOrderHeader workOrderHeader = workOrderHeaderService.getOrderByCode(orderCode);
        if (workOrderHeader == null) {
            workOrderHeader = new WorkOrderHeader();
        } else {
            return AjaxResult.error(StringUtils.format("下发工单失败,工单:{} 已存在", orderCode));
            /*WorkOrderHeader updateWorkOrderHeader = new WorkOrderHeader();
            updateWorkOrderHeader.setId(workOrderHeader.getId());
            updateWorkOrderHeader.setOrderStatus(orderStatus);
            boolean success = workOrderHeaderService.updateById(updateWorkOrderHeader);
            if (success) {
                return AjaxResult.success("更新工单状态成功");
            } else {
                return AjaxResult.error(StringUtils.format("更新工单状态失败,工单号:{}", orderCode));
            }*/
        }
        workOrderHeader.setTaskNo(taskNo);
        workOrderHeader.setOrderCode(orderCode);
        workOrderHeader.setOrderNumber(orderNumber);
        workOrderHeader.setBatchNumber(batchNumber);
        workOrderHeader.setOrderStatus(orderStatus);
        if (!workOrderHeaderService.save(workOrderHeader)) {
            throw new ServiceException("下发工单失败,保存工单头表失败");
        }
        List<WorkOrderDetail> workOrderDetailList = new ArrayList<>();
        for (MesWorkOrderDetail mesWorkOrderDetail : mesWorkOrder.getMaterialDataList()) {
            WorkOrderDetail workOrderDetail = new WorkOrderDetail();
            workOrderDetail.setWorkId(workOrderHeader.getId());
            Material material = materialService.getMaterialByCode(mesWorkOrderDetail.getMaterialCode(), QuantityConstant.DEFAULT_WAREHOUSE);
            if (material == null) {
                throw new ServiceException(StringUtils.format("根据物料编码:{} 没有找到对应物料信息", mesWorkOrderDetail.getMaterialCode()));
            }
            workOrderDetail.setMaterialCode(mesWorkOrderDetail.getMaterialCode());
            workOrderDetail.setMaterialQty(mesWorkOrderDetail.getMaterialQty());
            workOrderDetail.setShipmentReferQty(mesWorkOrderDetail.getShipmentReferQty());
            workOrderDetailList.add(workOrderDetail);
        }
        boolean success = workOrderDetailService.saveBatch(workOrderDetailList);
        if (!success) {
            throw new ServiceException("下发工单失败,保存工单明细失败");
        }
        return AjaxResult.success("下发工单成功");
    }

    @Override
    public AjaxResult backShipment(Integer taskHeaderId) {
        TaskHeader taskHeader = taskHeaderService.getById(taskHeaderId);
        if (StringUtils.isNull(taskHeader)) {
            return AjaxResult.error(StringUtils.format("回传出库信息失败,没有找到任务,id:{}", taskHeaderId));
        }

        // 获取任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailQueryWrapper = Wrappers.lambdaQuery();
        taskDetailQueryWrapper.eq(TaskDetail::getTaskId, taskHeaderId);
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailQueryWrapper);
        TaskDetail taskDetailQuery = taskDetailList.stream().findAny().orElse(null);
        if (taskDetailQuery == null) {
            return AjaxResult.error(StringUtils.format("回传出库信息失败,任务:{} 没有明细", taskHeaderId));
        }
        MesReturn mesReturn = new MesReturn();
        List<MaterialData> materialDataList = new ArrayList<>();
        taskDetailList.forEach(taskDetail -> {
            MaterialData materialData = new MaterialData();
            BeanUtils.copyBeanProp(materialData, taskDetail);
            materialData.setIsMaterial(true);
            // FALSE 为 合格、0。  TRUE 为 不合格、1
            materialData.setQualityStatus(QuantityConstant.INVENTORY_STATUS_NG.equals(taskDetail.getInventorySts()));
            materialData.setFlowCode(StringUtils.isEmpty(taskDetail.getFlowCode()) ? QuantityConstant.EMPTY_STRING : taskDetail.getFlowCode());
            materialDataList.add(materialData);
        });
        Container container = containerService.getContainerByCode(taskHeader.getContainerCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        if (container == null) {
            throw new ServiceException(StringUtils.format("出库回传失败,不存在载具编码:{}", taskHeader.getContainerCode()));
        }
        Vehicle vehicle = vehicleService.getVehicleByCode(taskHeader.getVehicleCode());
        if (Objects.isNull(vehicle)) {
            throw new ServiceException(StringUtils.format("出库回传失败,不存在盛具编码: {}", taskHeader.getVehicleCode()));
        }
        List<String> collect1 = new ArrayList<>();
        VehicleType vehicleType = vehicleTypeService.getByCode(vehicle.getVehicleType());
        for (int y = 1; y <= vehicleType.getMaxX(); y++) {
            for (int x = 1; x <= vehicleType.getMaxY(); x++) {
                collect1.add(x + "_" + y);
            }
        }
        String materialCode = materialDataList.stream().map(MaterialData::getMaterialCode).findFirst().orElse(null);
        List<String> collect2 = materialDataList.stream()
                .map(item -> item.getLocationNoX() + "_" + item.getLocationNoY())
                .collect(Collectors.toList());
        // 没物料的坐标
        List<String> collect = collect1.stream().filter(item -> !collect2.contains(item)).collect(Collectors.toList());
        collect.forEach(item -> {
            String[] split = item.split("_");
            MaterialData newMaterialData = new MaterialData();
            newMaterialData.setMaterialCode(materialCode);
            newMaterialData.setTracingNo("");
            newMaterialData.setQty(BigDecimal.ONE);
            newMaterialData.setQualityStatus(true);
            newMaterialData.setFirstFlag(false);
            newMaterialData.setFlowCode("");
            newMaterialData.setLocationNoX(Integer.parseInt(split[0]));
            newMaterialData.setLocationNoY(Integer.parseInt(split[1]));
            newMaterialData.setIsMaterial(false);
            newMaterialData.setWeight((double) 0);
            newMaterialData.setSupplierCode("");
            newMaterialData.setSupplierBatch("");
            newMaterialData.setSupplierTracingNo("");
            materialDataList.add(newMaterialData);
        });

        mesReturn.setTaskNo(taskHeader.getTaskNo());
        mesReturn.setTaskStatus(QuantityConstant.MES_TASK_COMPLETE);
        mesReturn.setOrderCode(taskHeader.getOrderCode());
        mesReturn.setToPort(taskHeader.getPort());
        mesReturn.setRealSolidifyTime(taskDetailQuery.getRealSolidifyTime());
        // 因和MES那边载具和盛具字段定义不一致,所以要反转再回传
        mesReturn.setContainerTypeCode(vehicleType.getCode());
        mesReturn.setContainerCode(taskHeader.getVehicleCode());
        mesReturn.setVehicleCode(container.getCode());
        mesReturn.setVehicleTypeCode(container.getContainerType());
        mesReturn.setMaxX(vehicleType.getMaxX());
        mesReturn.setMaxY(vehicleType.getMaxY());
        mesReturn.setMaterialDataList(materialDataList);
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_MES, QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.DEFAULT_AREA);
        String jsonParam = JSON.toJSONString(mesReturn);
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, QuantityConstant.DEFAULT_WAREHOUSE, jsonParam);
        if (result != null && result.getBody() != null) {
            String code = result.getBody().getString("code");
            String msg = result.getBody().getString("msg");
            if (!HttpConstant.isSuccess(Integer.parseInt(code))) {
                return AjaxResult.error(msg);
            }
        } else {
            throw new ServiceException("出库回传失败,接口地址错误或返回为空");
        }

        taskHeader.setIsBack(QuantityConstant.MES_TASK_BACK);
        if (!taskHeaderService.updateById(taskHeader)) {
            throw new ServiceException("出库回传失败,更新任务头表失败");
        }
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderQueryWrapper.eq(ShipmentHeader::getCode, taskHeader.getShipmentCode());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderQueryWrapper);
        if (QuantityConstant.SHIPMENT_HEADER_COMPLETED.equals(shipmentHeader.getFirstStatus())
                && QuantityConstant.SHIPMENT_HEADER_COMPLETED.equals(shipmentHeader.getLastStatus())) {
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
            if (!shipmentHeaderService.updateById(shipmentHeader)) {
                throw new ServiceException("出库回传失败,更新出库单头表失败");
            }
        }
        return AjaxResult.success(mesReturn);
    }

    @Override
    public AjaxResult backReceipt(Integer taskHeaderId) {
        TaskHeader taskHeader = taskHeaderService.getById(taskHeaderId);
        if (taskHeader == null) {
            return AjaxResult.error("回传MES入库信息失败,任务头表为空");
        }

        MesReturn mesReturn = new MesReturn();
        // 获取任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailQueryWrapper = Wrappers.lambdaQuery();
        taskDetailQueryWrapper.eq(TaskDetail::getTaskId, taskHeaderId);
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailQueryWrapper);

        List<MaterialData> materialDataList = new ArrayList<>();
        taskDetailList.forEach(taskDetail -> {
            MaterialData materialData = new MaterialData();
            materialData.setMaterialCode(taskDetail.getMaterialCode());
            materialData.setQty(taskDetail.getQty());
            materialData.setTracingNo(taskDetail.getTracingNo());
            materialData.setSupplierCode(taskDetail.getSupplierCode());
            materialData.setSupplierTracingNo(taskDetail.getSupplierTracingNo());
            materialData.setSupplierBatch(taskDetail.getSupplierBatch());
            materialData.setQualityStatus(QuantityConstant.INVENTORY_STATUS_NG.equals(taskDetail.getInventorySts()));
            materialData.setLocationNoX(taskDetail.getLocationNoX());
            materialData.setLocationNoY(taskDetail.getLocationNoY());
            materialData.setFlowCode(StringUtils.isEmpty(taskDetail.getFlowCode()) ? QuantityConstant.EMPTY_STRING : taskDetail.getFlowCode());
            materialData.setFirstFlag(taskDetail.getFirstFlag());
            materialData.setIsMaterial(true);
            materialDataList.add(materialData);
        });
        Container container = containerService.getContainerByCode(taskHeader.getContainerCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        Vehicle vehicle = vehicleService.getVehicleByCode(taskHeader.getVehicleCode());
        if (Objects.isNull(vehicle)) {
            throw new ServiceException(StringUtils.format("入库回传失败,不存在盛具编码: {}", taskHeader.getVehicleCode()));
        }
        mesReturn.setTaskNo(taskHeader.getTaskNo());
        mesReturn.setTaskStatus(QuantityConstant.MES_TASK_COMPLETE);
        mesReturn.setOrderCode(taskHeader.getOrderCode());
        mesReturn.setToPort(taskHeader.getFromPort());
        // 因和MES那边载具和盛具字段定义不一致,所以要反转再回传
        mesReturn.setContainerTypeCode(vehicle.getVehicleType());
        mesReturn.setContainerCode(taskHeader.getVehicleCode());
        mesReturn.setVehicleCode(taskHeader.getContainerCode());
        mesReturn.setVehicleTypeCode(container.getContainerType());
        mesReturn.setMaterialDataList(materialDataList);
        mesReturn.setMaterialCount(materialDataList.stream().map(MaterialData::getQty).reduce(BigDecimal.ZERO, BigDecimal::add));
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_MES, QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.DEFAULT_AREA);
        String jsonParam = JSON.toJSONString(mesReturn);
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, QuantityConstant.DEFAULT_WAREHOUSE, jsonParam);
        if (result != null && result.getBody() != null) {
            String code = result.getBody().getString("code");
            String msg = result.getBody().getString("msg");
            if (!HttpConstant.isSuccess(Integer.parseInt(code))) {
                return AjaxResult.error(msg);
            }
        } else {
            throw new ServiceException("接口地址错误或返回为空");
        }

        TaskHeader updateTaskHeader = new TaskHeader();
        updateTaskHeader.setId(taskHeader.getId());
        updateTaskHeader.setIsBack(QuantityConstant.MES_TASK_BACK);
        if (!taskHeaderService.updateById(updateTaskHeader)) {
            throw new ServiceException("入库回传失败,更新任务头表失败");
        }
        // 一个任务一个入库单 所以可以直接修改库存头状态
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderQueryWrapper = Wrappers.lambdaQuery();
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderQueryWrapper);
        if (QuantityConstant.RECEIPT_HEADER_POSTING.equals(receiptHeader.getFirstStatus())
                && QuantityConstant.RECEIPT_HEADER_POSTING.equals(receiptHeader.getLastStatus())) {
            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            if (!receiptHeaderService.updateById(receiptHeader)) {
                throw new ServiceException("出库回传失败,更新入库单头表失败");
            }
        }
        return AjaxResult.success(mesReturn);
    }

    @Override
    public AjaxResult backEmpty(Integer id) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("任务id不能为空");
        }
        TaskHeader taskHeader = taskHeaderService.getById(id);
        Container container = containerService.getContainerByCode(taskHeader.getContainerCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        if (Objects.isNull(container)) {
            throw new ServiceException("回传空载具入出库时,没有找到载具: ".concat(taskHeader.getContainerCode()));
        }
        Vehicle vehicle = vehicleService.getVehicleByCode(taskHeader.getVehicleCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        if (Objects.isNull(vehicle)) {
            throw new ServiceException("回传空载具入出库时,没有找到盛具: ".concat(taskHeader.getVehicleCode()));
        }
        VehicleType vehicleType = vehicleTypeService.getByCode(vehicle.getVehicleType());
        if (Objects.isNull(vehicleType)) {
            throw new ServiceException("回传空载具入出库时,没有找到盛具类型: ".concat(vehicle.getVehicleType()));
        }
        MesReturn mesReturn = new MesReturn();
        mesReturn.setTaskNo(taskHeader.getTaskNo());
        mesReturn.setOrderCode(taskHeader.getOrderCode());
        mesReturn.setTaskStatus(QuantityConstant.MES_TASK_COMPLETE);
        if (QuantityConstant.TASK_TYPE_EMPTYRECEIPT == taskHeader.getTaskType()) {
            mesReturn.setToPort(taskHeader.getFromPort());
        }
        if (QuantityConstant.TASK_TYPE_EMPTYSHIPMENT == taskHeader.getTaskType()) {
            mesReturn.setToPort(taskHeader.getPort());
        }
        // 因和MES那边载具和盛具字段定义不一致,所以要反转再回传
        mesReturn.setContainerTypeCode(vehicle.getVehicleType());
        mesReturn.setContainerCode(vehicle.getCode());
        mesReturn.setVehicleCode(container.getCode());
        mesReturn.setVehicleTypeCode(container.getContainerType());

        Integer maxX = vehicleType.getMaxX();
        Integer maxY = vehicleType.getMaxY();
        mesReturn.setMaxX(maxX);
        mesReturn.setMaxY(maxY);
        // MES方需求,空盘也要给坐标
        List<MaterialData> materialDataList = new ArrayList<>();
        for (int x = 1; x <= maxY; x++) {
            for (int y = 1; y <= maxX; y++) {
                MaterialData materialData = new MaterialData();
                materialData.setMaterialCode("");
                materialData.setTracingNo("");
                materialData.setQty(BigDecimal.ONE);
                materialData.setSupplierCode("");
                materialData.setSupplierTracingNo("");
                materialData.setQualityStatus(true);
                materialData.setFirstFlag(false);
                materialData.setFlowCode("");
                materialData.setLocationNoX(x);
                materialData.setLocationNoY(y);
                materialData.setWeight((double) 0);
                materialData.setIsMaterial(false);
                materialDataList.add(materialData);
            }
        }
        mesReturn.setRealSolidifyTime((double) 0);
        mesReturn.setMaterialDataList(materialDataList);
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_MES, QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.DEFAULT_AREA);
        String jsonParam = JSON.toJSONString(mesReturn);
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, QuantityConstant.DEFAULT_WAREHOUSE, jsonParam);
        if (result != null && result.getBody() != null) {
            String code = result.getBody().getString("code");
            String msg = result.getBody().getString("msg");
            if (!HttpConstant.isSuccess(Integer.parseInt(code))) {
                return AjaxResult.error(msg);
            }
        } else {
            throw new ServiceException("接口地址错误或返回为空");
        }
        taskHeader.setIsBack(QuantityConstant.MES_TASK_BACK);
        taskHeaderService.updateById(taskHeader);
        return AjaxResult.success(mesReturn);
    }

    @Override
    public AjaxResult backChangeStation(Integer taskId) {
        if (Objects.isNull(taskId)) {
            return AjaxResult.error("任务id不能为空");
        }
        TaskHeader taskHeader = taskHeaderService.getById(taskId);
        Container container = containerService.getContainerByCode(taskHeader.getContainerCode(), QuantityConstant.DEFAULT_WAREHOUSE);
        if (Objects.isNull(container)) {
            throw new ServiceException("换站任务,没有找到载具: ".concat(taskHeader.getContainerCode()));
        }
        Vehicle vehicle = vehicleService.getByCode(taskHeader.getVehicleCode());
        if (Objects.isNull(vehicle)) {
            throw new ServiceException("换站任务,没有找到盛具具: ".concat(taskHeader.getVehicleCode()));
        }
        VehicleType vehicleType = vehicleTypeService.getByCode(vehicle.getVehicleType());
        if (vehicleType == null) {
            throw new ServiceException(StringUtils.format("换站任务回传失败,盛具类型:{} 未找到,任务id:{}: ", vehicle.getVehicleType(), taskId));
        }
        MesReturn mesReturn = new MesReturn();
        mesReturn.setTaskNo(taskHeader.getTaskNo());
        mesReturn.setTaskStatus(QuantityConstant.MES_TASK_COMPLETE);
        mesReturn.setToPort(taskHeader.getPort());
        // 因和MES那边载具和盛具字段定义不一致,所以要反转再回传
        mesReturn.setContainerTypeCode(vehicle.getVehicleType());
        mesReturn.setContainerCode(vehicle.getCode());
        mesReturn.setVehicleCode(container.getCode());
        mesReturn.setVehicleTypeCode(container.getContainerType());
        mesReturn.setMaxX(vehicleType.getMaxX());
        mesReturn.setMaxY(vehicleType.getMaxY());
        mesReturn.setOrderCode(taskHeader.getOrderCode());

        //回传物料信息
        List<MaterialData> materialDataList = new ArrayList<>();
        List<TaskDetail> taskDetailList = taskDetailService.getByHeaderId(taskHeader.getId());
        TaskDetail taskDetailQuery = taskDetailList.stream().findAny().orElse(null);
        taskDetailList.forEach(taskDetail -> {
            MaterialData materialData = new MaterialData();
            BeanUtils.copyBeanProp(materialData, taskDetail);
            materialData.setIsMaterial(true);
            materialData.setQualityStatus(QuantityConstant.INVENTORY_STATUS_NG.equals(taskDetail.getInventorySts()));
            materialData.setFlowCode(StringUtils.isEmpty(taskDetail.getFlowCode()) ? QuantityConstant.EMPTY_STRING : taskDetail.getFlowCode());
            materialDataList.add(materialData);
        });

        String materialCode = materialDataList.stream().map(MaterialData::getMaterialCode).findFirst().orElse(null);
        List<String> collect1 = new ArrayList<>();
        for (int y = 1; y <= vehicleType.getMaxX(); y++) {
            for (int x = 1; x <= vehicleType.getMaxY(); x++) {
                collect1.add(x + "_" + y);
            }
        }
        List<String> collect2 = materialDataList.stream()
                .map(item -> item.getLocationNoX() + "_" + item.getLocationNoY())
                .collect(Collectors.toList());
        List<String> collect = collect1.stream().filter(item -> !collect2.contains(item)).collect(Collectors.toList());

        // 没物料的坐标
        collect.forEach(item -> {
            String[] split = item.split("_");
            MaterialData newMaterialData = new MaterialData();
            newMaterialData.setMaterialCode(materialCode);
            newMaterialData.setTracingNo("");
            newMaterialData.setQty(BigDecimal.ONE);
            newMaterialData.setQualityStatus(true);
            newMaterialData.setFirstFlag(false);
            newMaterialData.setFlowCode("");
            newMaterialData.setLocationNoX(Integer.parseInt(split[0]));
            newMaterialData.setLocationNoY(Integer.parseInt(split[1]));
            newMaterialData.setIsMaterial(false);
            newMaterialData.setWeight((double) 0);
            newMaterialData.setSolidifyTime(null);
            newMaterialData.setSupplierCode("");
            newMaterialData.setSupplierBatch("");
            newMaterialData.setSupplierTracingNo("");
            materialDataList.add(newMaterialData);
        });

        mesReturn.setRealSolidifyTime(Objects.isNull(taskDetailQuery) ? 0 : taskDetailQuery.getRealSolidifyTime());
        mesReturn.setMaterialDataList(materialDataList);

        String url = addressService.selectAddress(QuantityConstant.ADDRESS_MES, QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.DEFAULT_AREA);
        String jsonParam = JSON.toJSONString(mesReturn);
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, QuantityConstant.DEFAULT_WAREHOUSE, jsonParam);
        if (result != null && result.getBody() != null) {
            String code = result.getBody().getString("code");
            String msg = result.getBody().getString("msg");
            if (!HttpConstant.isSuccess(Integer.parseInt(code))) {
                return AjaxResult.error(msg);
            }
        } else {
            throw new ServiceException("接口地址错误或返回为空");
        }
        taskHeader.setIsBack(QuantityConstant.MES_TASK_BACK);
        taskHeaderService.updateById(taskHeader);
        return AjaxResult.success(mesReturn);
    }

    @Override
    public AjaxResult searchInventoryByQC(MesSearchRequestByQCDto mesSearchRequestByQCDto) {
        // 参数校验
        String taskNo = mesSearchRequestByQCDto.getTaskNo();
        String orderCode = mesSearchRequestByQCDto.getOrderCode();
        String materialCode = mesSearchRequestByQCDto.getMaterialCode();
        if (StringUtils.isEmpty(taskNo)) {
            throw new ServiceException("任务号为空");
        }
        if (StringUtils.isEmpty(orderCode)) {
            throw new ServiceException("工单号为空");
        }
        if (StringUtils.isEmpty(materialCode)) {
            throw new ServiceException("物料编码为空");
        }
        Material material = materialService.getUsableByCode(materialCode);
        if (material == null) {
            return AjaxResult.error(StringUtils.format("物料编码:{} 不存在", materialCode));
        }
        // 根据工单号和物料编码查询库存,排除被领料单冻结的和已存在任务数量的
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InventoryDetail::getMaterialCode, materialCode)
                .eq(InventoryDetail::getOrderCode, orderCode)
                .ne(InventoryDetail::getZoneCode, QuantityConstant.ZONE_TYPE_CBG)
                .last("and taskQty != qty");
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        List<MaterialDataQueryByQCDto> materialDataQueryByQCDtoList = new LinkedList<>();
        inventoryDetailList.forEach(inventoryDetail -> {
            MaterialDataQueryByQCDto queryByQCDto = new MaterialDataQueryByQCDto();
            queryByQCDto.setLocationNoX(inventoryDetail.getLocationNoX());
            queryByQCDto.setLocationNoY(inventoryDetail.getLocationNoY());
            queryByQCDto.setTracingNo(inventoryDetail.getTracingNo());
            queryByQCDto.setQualityStatus(Objects.equals(inventoryDetail.getInventorySts(), QuantityConstant.INVENTORY_STATUS_OK));
            queryByQCDto.setContainerCode(inventoryDetail.getContainerCode());
            queryByQCDto.setContainerTypeCode(inventoryDetail.getContainerCode().substring(0, 5));//ZJ-01-00001 -> ZJ-01
            queryByQCDto.setVehicleCode(inventoryDetail.getVehicleCode());
            queryByQCDto.setVehicleTypeCode(inventoryDetail.getVehicleCode().substring(0, 7));//CJ00001-00001 -> CJ00001
            queryByQCDto.setLocationCode(inventoryDetail.getLocationCode());
            materialDataQueryByQCDtoList.add(queryByQCDto);
        });
        MesSearchResponseByQCDto mesSearchResponseByQCDto = new MesSearchResponseByQCDto();
        mesSearchResponseByQCDto.setMaterialCode(materialCode);
        mesSearchResponseByQCDto.setMaterialName(material.getName());
        mesSearchResponseByQCDto.setUsableQty(inventoryDetailList.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add));
        mesSearchResponseByQCDto.setMaterialDataQueryByQCDtoList(materialDataQueryByQCDtoList);
        return AjaxResult.success(mesSearchResponseByQCDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentByQC(MesShipmentByQCDto mesShipmentByQCDto) {
        String taskNo = mesShipmentByQCDto.getTaskNo();
        String vehicleCode = mesShipmentByQCDto.getVehicleCode();
        Integer materialInspectStatus = mesShipmentByQCDto.getMaterialInspectStatus();
        String toPort = mesShipmentByQCDto.getToPort();
        // 1、参数校验
        if (StringUtils.isEmpty(toPort)) {
            return AjaxResult.error("抽样质检出库失败,目标料点为空");
        }
        if (StringUtils.isEmpty(vehicleCode)) {
            return AjaxResult.error("抽样质检出库失败,盛具编码为空");
        }
        if (Objects.isNull(materialInspectStatus)) {
            return AjaxResult.error("抽样质检出库失败,物料状态为空");
        }
        // 2、获取库存信息
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .apply("qty > taskQty AND TRUNCATE(TIME_TO_SEC(TIMEDIFF(now(), created)) / 3600, 2) >= solidifyTime")
                .eq(InventoryDetail::getVehicleCode, vehicleCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        if (inventoryDetailList.isEmpty()) {
            return AjaxResult.error(StringUtils.format("抽样质检出库失败,根据盛具:{},物料状态:{} 没有找到库存信息",
                    vehicleCode, mesShipmentByQCDto.getMaterialInspectStatus()));
        }
        inventoryDetailList.forEach(inventoryDetail -> {
            if (!materialInspectStatus.equals(inventoryDetail.getMaterialInspectStatus())) {
                throw new ServiceException(StringUtils.format("抽样质检出库失败,根据盛具:{},坐标:{},{} 的物料质检状态:{} 与参数不一致",
                        vehicleCode, inventoryDetail.getLocationNoX(), inventoryDetail.getLocationNoY(), mesShipmentByQCDto.getMaterialInspectStatus()));
            }
        });
        String shipmentType;
        InventoryDetail tempInventoryDetail = inventoryDetailList.stream().findAny().orElseThrow(() -> new ServiceException("库存数据错误"));
        switch (tempInventoryDetail.getMaterialInspectStatus()) {
            case 1:
                shipmentType = QuantityConstant.SHIPMENT_TYPE_QC_SELF;
                break;
            case 3:
                shipmentType = QuantityConstant.SHIPMENT_TYPE_QC_OFFICE;
                break;
            case 4:
                shipmentType = QuantityConstant.SHIPMENT_TYPE_QC_ARMY;
                break;
            default:
                return AjaxResult.error("抽样质检出库失败,库存物料状态错误");
        }

        InventoryHeader inventoryHeader = inventoryHeaderService.getById(tempInventoryDetail.getInventoryHeaderId());
        if (inventoryHeader == null) {
            return AjaxResult.error(StringUtils.format("抽样质检出库失败,库存头信息为空"));
        }
        Container container = containerService.getByCode(inventoryHeader.getContainerCode());
        if (container == null) {
            return AjaxResult.error(StringUtils.format("抽样质检出库失败,根据库存头载具:{} 未找到载具", inventoryHeader.getContainerCode()));
        }
        String containerCode = container.getCode();
        if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
            return AjaxResult.error(StringUtils.format("抽样质检出库失败,载具:{} 已锁定", containerCode));
        }
        String locationCode = container.getLocationCode();
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error(StringUtils.format("抽样质检出库失败,载具:{} 不在库位上", containerCode));
        }
        Location location = locationService.getLocationByCode(locationCode);
        if (location == null) {
            throw new ServiceException(StringUtils.format("抽样质检出库失败,库位:{} 不存在", locationCode));
        }
        if (QuantityConstant.STATUS_LOCATION_LOCK.equals(location.getStatus())) {
            throw new ServiceException(StringUtils.format("抽样质检出库失败,库位:{} 已被锁定", location.getCode()));
        }
        // 3、创建出库单
        String code = shipmentHeaderService.createCode(shipmentType);
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        shipmentHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
        shipmentHeader.setCode(code);
        shipmentHeader.setShipmentType(shipmentType);
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
        shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        shipmentHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        boolean success = shipmentHeaderService.save(shipmentHeader);
        if (!success) {
            throw new ServiceException("抽样质检出库失败,保存出库单头表失败");
        }

        ShipmentDetail shipmentDetail = new ShipmentDetail();
        BeanUtils.copyBeanProp(shipmentDetail, tempInventoryDetail);
        shipmentDetail.setShipmentId(shipmentHeader.getId());
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        shipmentDetail.setStatus(shipmentHeader.getFirstStatus());
        shipmentDetail.setQty(inventoryDetailList.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add));
        shipmentDetail.setTaskQty(shipmentDetail.getQty());
        shipmentDetail.setCreatedBy(shipmentHeader.getCreatedBy());
        shipmentDetail.setCreated(new Date());
        shipmentDetail.setLastUpdatedBy(shipmentHeader.getLastUpdatedBy());
        shipmentDetail.setLastUpdated(new Date());
        shipmentDetail.setStatus(shipmentHeader.getFirstStatus());

        success = shipmentDetailService.save(shipmentDetail);
        if (!success) {
            throw new ServiceException(StringUtils.format("抽样质检出库失败,保存抽样检验出库单明细失败"));
        }
        // 更新抽样检验出库单头表
        ShipmentHeader updateShipmentHeader = new ShipmentHeader();
        updateShipmentHeader.setId(shipmentHeader.getId());
        updateShipmentHeader.setTotalLines(1);
        updateShipmentHeader.setTotalQty(shipmentDetail.getQty());
        updateShipmentHeader.setLastUpdatedBy(shipmentHeader.getLastUpdatedBy());
        success = shipmentHeaderService.updateById(updateShipmentHeader);
        if (!success) {
            throw new ServiceException(StringUtils.format("抽样质检出库失败,更新抽样检验出库单头表数量失败"));
        }

        // 4、生成配盘记录
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getUnCompleteByContainerCode(containerCode);
        if (shipmentContainerHeader == null) {
            shipmentContainerHeader = new ShipmentContainerHeader();
            BeanUtils.copyBeanProp(shipmentContainerHeader, inventoryHeader);
            shipmentContainerHeader.setShipmentCode(shipmentHeader.getCode());
            shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
            shipmentContainerHeader.setPort(toPort);
            boolean success2 = shipmentContainerHeaderService.save(shipmentContainerHeader);
            if (!success2) {
                throw new ServiceException("抽样检验出库失败,保存配盘头表失败");
            }
        }
        List<InventoryDetail> inventoryDetails = inventoryDetailList.stream()
                .filter(x -> x.getMaterialCode().equals(shipmentDetail.getMaterialCode()) &&
                        x.getInventorySts().equals(shipmentDetail.getInventorySts()))
                .collect(Collectors.toList());
        if (StringUtils.isEmpty(inventoryDetails)) {
            throw new ServiceException(StringUtils.format("抽样检验出库失败,检验出库单据明细中未找到状态为:{} 的物料:{}",
                    shipmentDetail.getInventorySts(), shipmentDetail.getMaterialCode()));
        }

        ShipmentContainerHeader finalShipmentContainerHeader = shipmentContainerHeader;
        inventoryDetails.forEach(inventoryDetail -> {
            // 计算实际固化时间
            // 此方法获取的是相差多少秒
            long differenceSecond = DateUtils.difference(new Date(), inventoryDetail.getCreated());
            // 转化为小时
            double realSolidifyTime = (double) differenceSecond / (60 * 60);
            realSolidifyTime = Double.parseDouble(String.format("%.1f", realSolidifyTime));
            // 配盘明细
            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            BeanUtils.copyBeanProp(shipmentContainerDetail, inventoryDetail);
            shipmentContainerDetail.setId(null);
            shipmentContainerDetail.setShippingContainerId(finalShipmentContainerHeader.getId());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentContainerDetail.setShipmentId(shipmentHeader.getId());
            shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
            shipmentContainerDetail.setRealSolidifyTime(inventoryDetail.getSolidifyTime() == 0 ? 0 : realSolidifyTime);
            shipmentContainerDetail.setCreated(new Date());
            shipmentContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
            shipmentContainerDetail.setLastUpdated(new Date());
            shipmentContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            if (!shipmentContainerDetailService.save(shipmentContainerDetail)) {
                throw new ServiceException("抽样检验出库失败,保存出库配盘明细失败");
            }
        });

        // 5、创建出库任务
        TaskHeader taskHeader = new TaskHeader();
        BeanUtils.copyBeanProp(taskHeader, shipmentContainerHeader);
        taskHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
        taskHeader.setAllocationHeadId(shipmentContainerHeader.getId());
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHeader.setFromLocation(shipmentContainerHeader.getLocationCode());
        taskHeader.setZoneCode(location.getZoneCode());
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        taskHeader.setPort(toPort);
        taskHeader.setCreated(new Date());
        taskHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        taskHeader.setLastUpdated(new Date());
        taskHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        taskHeader.setTaskNo(taskNo);
        taskHeader.setIsBack(QuantityConstant.MES_TASK_NOT_BACK);
        boolean success2 = taskHeaderService.save(taskHeader);
        if (!success2) {
            throw new ServiceException("抽样检验出库失败,保存任务头表失败");
        }
        List<TaskDetail> taskDetailList = new LinkedList<>();
        LambdaQueryWrapper<ShipmentContainerDetail> shipmentContainerQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, shipmentContainerHeader.getId());
        List<ShipmentContainerDetail> shipmentContainerDetailList = shipmentContainerDetailService.list(shipmentContainerQueryWrapper);
        shipmentContainerDetailList.forEach(shipmentContainerDetail -> {
            // 任务明细
            TaskDetail taskDetail = new TaskDetail();
            BeanUtils.copyBeanProp(taskDetail, shipmentContainerDetail);
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setBillCode(shipmentHeader.getCode());
            taskDetail.setBillDetailId(shipmentContainerDetail.getShipmentDetailId());
            taskDetail.setFromInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setToInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setAllocationId(shipmentContainerDetail.getId());
            taskDetail.setFromLocation(taskHeader.getFromLocation());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setCreated(new Date());
            taskDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
            taskDetail.setLastUpdated(new Date());
            taskDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            taskDetailList.add(taskDetail);
        });
        success2 = taskDetailService.saveBatch(taskDetailList);
        if (!success2) {
            throw new ServiceException("抽样检验出库失败,保存任务明细表失败");
        }
        // 锁定库位和载具
        success2 = containerService.updateStatus(shipmentContainerHeader.getContainerCode(), QuantityConstant.STATUS_CONTAINER_LOCK);
        if (!success2) {
            throw new ServiceException("抽样检验出库失败,锁定载具失败");
        }
        success2 = locationService.updateStatus(shipmentContainerHeader.getLocationCode(), QuantityConstant.STATUS_LOCATION_LOCK);
        if (!success2) {
            throw new ServiceException("抽样检验出库失败,锁定原库位失败");
        }
        // 更新库存任务数
        List<InventoryDetail> updateInventoryDetailList = new LinkedList<>();
        inventoryDetailList.forEach(inventoryDetail -> {
            InventoryDetail updateInventoryDetail = new InventoryDetail();
            updateInventoryDetail.setId(inventoryDetail.getId());
            updateInventoryDetail.setTaskQty(inventoryDetail.getQty());
            updateInventoryDetailList.add(updateInventoryDetail);
        });
        success = inventoryDetailService.updateBatchById(updateInventoryDetailList);
        if (!success) {
            throw new ServiceException("抽样检验出库失败,更新库存任务数量失败");
        }
        return AjaxResult.success("抽样检验出库成功");
    }

    @Override
    public AjaxResult receiptByQC(MesReceiptByQCDto mesReceiptByQCDto) {
        // 参数校验
        String taskNo = mesReceiptByQCDto.getTaskNo();
        String fromPort = mesReceiptByQCDto.getFromPort();
        String orderCode = mesReceiptByQCDto.getOrderCode();
        String containerCode = mesReceiptByQCDto.getContainerCode();
        String vehicleCode = mesReceiptByQCDto.getVehicleCode();
        List<ReceiptMaterialData> receiptMaterialDataList = mesReceiptByQCDto.getMaterialDataList();
        if (StringUtils.isEmpty(taskNo)) {
            return AjaxResult.error("抽样质检余料退回失败,任务号为空");
        }
        if (StringUtils.isEmpty(fromPort)) {
            return AjaxResult.error("抽样质检余料退回失败,起始料点为空");
        }
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("抽样质检余料退回失败,工单号为空");
        }
        if (StringUtils.isEmpty(containerCode)) {
            return AjaxResult.error("抽样质检余料退回失败,载具编码为空");
        }
        if (StringUtils.isEmpty(vehicleCode)) {
            return AjaxResult.error("抽样质检余料退回失败,盛具编码为空");
        }
        if (StringUtils.isEmpty(receiptMaterialDataList)) {
            return AjaxResult.error("抽样质检余料退回失败,物料信息为空");
        }
        // 校验载具
        Container container = containerService.getByCode(containerCode);
        if (StringUtils.isNull(container)) {
            throw new ServiceException(StringUtils.format("入库失败,载具编码:{} 不存在", containerCode));
        }
        if (StringUtils.isNotEmpty(container.getVehicleCode()) &&
                !container.getVehicleCode().equals(vehicleCode)) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已和盛具:{} 绑定",
                    container.getCode(), container.getVehicleCode()));
        }
        Location location = locationService.getLocationByContainerCode(containerCode);
        if (location != null) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已在库位:{} 上", containerCode, location.getCode()));
        }
        InventoryHeader inventoryHeader = inventoryHeaderService.getByContainerCode(containerCode);
        if (inventoryHeader != null) {
            throw new ServiceException(StringUtils.format("入库失败,载具:{} 已经存在库存", containerCode));
        }
        // 校验盛具
        Vehicle vehicle = vehicleService.getVehicleByCode(vehicleCode);
        if (StringUtils.isNull(vehicle)) {
            throw new ServiceException(StringUtils.format("入库失败,不存在盛具编码 {}", vehicleCode));
        }
        Container containerByVehicleCode = containerService.getByVehicleCode(vehicleCode);
        if (containerByVehicleCode != null) {
            throw new ServiceException(StringUtils.format("入库失败,盛具:{} 已和载具:{} 绑定",
                    vehicleCode, containerByVehicleCode.getCode()));
        }
        receiptMaterialDataList = receiptMaterialDataList.stream().filter(x -> Boolean.TRUE.equals(x.getIsMaterial()))
                .collect(Collectors.toList());
        for (ReceiptMaterialData data : receiptMaterialDataList) {
            if (Boolean.FALSE.equals(data.getIsMaterial())) {
                continue;
            }
            Material material = materialService.getUsableByCode(data.getMaterialCode());
            if (material == null) {
                throw new ServiceException(StringUtils.format("入库失败,根据物料编码:{} 没有找到对应物料信息", data.getMaterialCode()));
            }
            if (StringUtils.isNull(data.getLocationNoX())) {
                throw new ServiceException("入库失败,物料有 x 坐标为空的,请检查");
            }
            if (StringUtils.isNull(data.getLocationNoY())) {
                throw new ServiceException("入库失败,物料有 y 坐标为空的,请检查");
            }
            if (StringUtils.isNull(data.getMaterialInspectStatus())) {
                return AjaxResult.error("入库失败,物料状态为空");
            }
            if (StringUtils.isNull(data.getQty()) || data.getQty().equals(BigDecimal.ZERO)) {
                throw new ServiceException(StringUtils.format("入库失败,坐标 {},{} 的物料数量为空或为0",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            if (StringUtils.isEmpty(data.getTracingNo())) {
                throw new ServiceException(StringUtils.format("入库失败,坐标:{},{} 的物料追溯码为空",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            if (StringUtils.isNull(data.getQualityStatus())) {
                throw new ServiceException(StringUtils.format("入库失败,坐标:{},{} 的物料质量状态为空",
                        data.getLocationNoX().toString(), data.getLocationNoY().toString()));
            }
            // 校验物料绑定的盛具类型
            if (!vehicle.getVehicleType().equals(material.getVehicleTypeCode())) {
                throw new ServiceException(StringUtils.format("入库失败,入库盛具类型 {} 与物料 {} 绑定的盛具类型 {} 不一致",
                        vehicle.getVehicleType(), material.getName(), material.getVehicleTypeCode()));
            }
            // 校验物料是否入立库(根据是否物料分区判断)
            if (StringUtils.isEmpty(material.getType())) {
                throw new ServiceException(StringUtils.format("入库失败,物料:{} 未设置物料类别", material.getName()));
            }
            MaterialType materialType = materialTypeService.getByCode(material.getType());
            if (materialType == null) {
                throw new ServiceException(StringUtils.format("入库失败,物料类别编码 {} 不存在", material.getType()));
            }
            String materialAreaCode = materialType.getMaterialAreaCode();
            if (StringUtils.isEmpty(materialAreaCode)) {
                throw new ServiceException(StringUtils.format("入库失败,物料:{} 所属类别:{} 未配置物料分区", material.getName(), materialType.getName()));
            }
        }
        // 校验同一个xy坐标的物料 数量只能为1
        if (!QuantityConstant.VEHICLE_TYPE_COMMON.equals(vehicleCode)) {
            ReceiptMaterialData qtyNotZeroMaterial = receiptMaterialDataList.stream()
                    .filter(x -> x.getQty().compareTo(BigDecimal.ONE) != 0)
                    .findAny()
                    .orElse(null);
            if (StringUtils.isNotNull(qtyNotZeroMaterial)) {
                throw new ServiceException(StringUtils.format("入库失败,物料坐标 {},{} 的数量异常,请检查数量",
                        qtyNotZeroMaterial.getLocationNoX(),
                        qtyNotZeroMaterial.getLocationNoY()));
            }
            Map<String, List<ReceiptMaterialData>> materialGroupByXyMap = receiptMaterialDataList.stream()
                    .collect(Collectors.groupingBy(material -> material.getLocationNoX().toString().concat(",").concat(material.getLocationNoY().toString())));
            for (Map.Entry<String, List<ReceiptMaterialData>> entry : materialGroupByXyMap.entrySet()) {
                if (entry.getValue().size() > 1) {
                    throw new ServiceException(StringUtils.format("入库失败,物料坐标 {} 重复,请检查坐标", entry.getKey()));
                }
            }
        }
        Optional<ReceiptMaterialData> optional = receiptMaterialDataList.stream().findAny();
        if (!optional.isPresent()) {
            throw new ServiceException("入库失败,物料状态为空");
        }
        Integer materialInspectStatus = optional.get().getMaterialInspectStatus();
        String receiptType;
        switch (materialInspectStatus) {
            case 1:
                receiptType = QuantityConstant.RECEIPT_TYPE_QC_SELF;
                break;
            case 3:
                receiptType = QuantityConstant.RECEIPT_TYPE_QC_OFFICE;
                break;
            case 4:
                receiptType = QuantityConstant.RECEIPT_TYPE_QC_ARMY;
                break;
            default:
                throw new ServiceException(StringUtils.format("物料状态:{} 不存在", materialInspectStatus));
        }
        MesOrder mesOrder = new MesOrder();
        mesOrder.setTaskNo(taskNo);
        mesOrder.setOrderCode(orderCode);
        mesOrder.setMaterialDataList(receiptMaterialDataList);
        mesOrder.setReceiptType(receiptType);
        AjaxResult ajaxResult = mesService.receiptOrder(mesOrder);
        if (Boolean.TRUE.equals(ajaxResult.hasErr())) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        // 新建保存组盘头表记录
        LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_BUILD);
        ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(lambda);

        if (StringUtils.isNull(receiptContainerHeader)) {
            receiptContainerHeader = new ReceiptContainerHeader();
            receiptContainerHeader.setReceiptCode(ajaxResult.getData().toString());
            receiptContainerHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            receiptContainerHeader.setContainerCode(containerCode);
            receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
            receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerHeader.setTaskNo(taskNo);
            receiptContainerHeader.setOrderCode(orderCode);
            receiptContainerHeader.setVehicleCode(vehicleCode);
            receiptContainerHeader.setPort(fromPort);

            WorkOrderHeader workOrderHeader = workOrderHeaderService.getOrderByCode(orderCode);
            if (workOrderHeader != null) {
                receiptContainerHeader.setBatchNumber(workOrderHeader.getBatchNumber());
                receiptContainerHeader.setOrderNumber(workOrderHeader.getOrderNumber());
            }
            if (!receiptContainerHeaderService.save(receiptContainerHeader)) {
                throw new ServiceException("入库失败,入库组盘头表保存失败");
            }
            // 组盘的时候,将载具和盛具绑定
            if (!containerService.updateVehicle(container.getCode(), vehicle.getCode(), vehicle.getVehicleType())) {
                throw new ServiceException("入库失败,载具与盛具绑定失败");
            }
        } else {
            if (receiptContainerHeader.getStatus() >= QuantityConstant.RECEIPT_CONTAINER_TASK && receiptContainerHeader.getStatus() < QuantityConstant.RECEIPT_CONTAINER_FINISHED) {
                throw new ServiceException("入库失败,载具已经生成任务,不能放物料了!");
            }
        }
        LambdaQueryWrapper<ReceiptHeader> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReceiptHeader::getCode, ajaxResult.getData());
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(wrapper);
        if (receiptHeader == null) {
            throw new ServiceException(StringUtils.format("入库失败,未找到上游单号为 {} 的入库单", taskNo));
        }
        //入库单id查询入库单详情
        LambdaQueryWrapper<ReceiptDetail> receiptDetailQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
        List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailQueryWrapper);
        if (StringUtils.isEmpty(receiptDetailList)) {
            throw new ServiceException(StringUtils.format("入库失败,未找到 {} 入库单的详情", receiptHeader.getId()));
        }
        //入库单详情的物料条码比较物料入库的物料条码
        for (ReceiptDetail receiptDetail : receiptDetailList) {
            //更新入库单详情的收货数量
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("入库失败,更新入库单详情失败");
            }
            //如果单据数量等于已收数量,更新入库详情状态和入库单状态
            if (receiptDetail.getQty().compareTo(receiptDetail.getTaskQty()) == 0) {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RECEIVING.toString());
                if (!receiptDetailService.updateById(receiptDetail)) {
                    throw new ServiceException("入库失败,更新入库详情处理标记失败");
                }
            }
            receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
        }
        List<ReceiptContainerDetail> detailList = new ArrayList<>();
        //入库组盘详情
        for (ReceiptMaterialData data : receiptMaterialDataList) {
            Material material = materialService.getUsableByCode(data.getMaterialCode());
            ReceiptDetail receiptDetail = receiptDetailList.stream()
                    .filter(x -> x.getMaterialCode().equals(data.getMaterialCode()))
                    .findFirst().orElseThrow(() -> new ServiceException(StringUtils.format("入库失败,入库单:{} 明细中不存在物料:{}", receiptHeader.getCode(), data.getMaterialCode())));
            ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
            BeanUtils.copyBeanProp(receiptContainerDetail, data);
            receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
            receiptContainerDetail.setWarehouseCode(receiptContainerHeader.getWarehouseCode());
            receiptContainerDetail.setId(null);
            receiptContainerDetail.setReceiptId(receiptDetail.getReceiptId());
            receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
            receiptContainerDetail.setReceiptCode(receiptDetail.getReceiptCode());
            receiptContainerDetail.setBatch(receiptDetail.getBatch());
            receiptContainerDetail.setTaskType(receiptContainerHeader.getTaskType());
            receiptContainerDetail.setContainerCode(containerCode);
            receiptContainerDetail.setContainerType(container.getContainerType());
            receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            receiptContainerDetail.setMaterialCode(material.getCode());
            receiptContainerDetail.setMaterialName(material.getName());
            receiptContainerDetail.setMaterialSpec(material.getSpec());
            receiptContainerDetail.setMaterialUnit(material.getUnit());
            receiptContainerDetail.setWeight(material.getWeight());
            receiptContainerDetail.setHigh(material.getHigh());
            receiptContainerDetail.setDescription(material.getDescription());
            receiptContainerDetail.setInventorySts(Boolean.FALSE.equals(data.getQualityStatus()) ? QuantityConstant.INVENTORY_STATUS_OK : QuantityConstant.INVENTORY_STATUS_NG);
            receiptContainerDetail.setMaterialInspectStatus(materialInspectStatus);
            receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_MES);
            receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
            detailList.add(receiptContainerDetail);
        }
        if (!receiptContainerDetailService.saveBatch(detailList)) {
            throw new ServiceException("入库失败,保存入库组盘详情失败");
        }
        //生成入库任务
        List<Integer> idList = Arrays.asList(Convert.toIntArray(String.valueOf(receiptContainerHeader.getId())));
        ajaxResult = receiptTaskService.createReceiptTask(idList);
        if (Boolean.TRUE.equals(ajaxResult.hasErr())) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        return AjaxResult.success("抽样质检余料退回成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult movingMaterial(MesMovingMaterialDto mesMovingMaterialDto) {
        String materialCode = mesMovingMaterialDto.getMaterialCode();
        String sourceOrderCode = mesMovingMaterialDto.getSourceOrderCode();
        String targetOrderCode = mesMovingMaterialDto.getTargetOrderCode();
        BigDecimal qty = mesMovingMaterialDto.getQty();
        if (StringUtils.isEmpty(materialCode)) {
            return AjaxResult.error("挪料失败,物料编码为空");
        }
        if (StringUtils.isEmpty(sourceOrderCode)) {
            return AjaxResult.error("挪料失败,源工单号为空");
        }
        if (StringUtils.isEmpty(targetOrderCode)) {
            return AjaxResult.error("挪料失败,目标工单号为空");
        }
        if (Objects.isNull(qty)) {
            return AjaxResult.error("挪料失败,挪动数量为空");
        }

        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InventoryDetail::getOrderCode, sourceOrderCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        if (StringUtils.isEmpty(inventoryDetailList)) {
            return AjaxResult.error(StringUtils.format("挪料失败,工单号:{} 下没有对应库存", sourceOrderCode));
        }
        List<String> contaienrCodeList = inventoryDetailList.stream()
                .map(InventoryDetail::getContainerCode)
                .distinct()
                .collect(Collectors.toList());
        List<String> unAvailableContainerCodeList = new ArrayList<>();
        for (String containerCode : contaienrCodeList) {
            Container container = containerService.getByCode(containerCode);
            if (!Objects.isNull(container)) {
                if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
                    unAvailableContainerCodeList.add(containerCode);
                }
            } else {
                return AjaxResult.error(StringUtils.format("挪料失败,载具:{} 不存在", containerCode));
            }
        }
        List<InventoryDetail> availableInventoryDetailList = inventoryDetailList.stream()
                .filter(x -> !unAvailableContainerCodeList.contains(x.getContainerCode()))
                .collect(Collectors.toList());
        BigDecimal availableQty = availableInventoryDetailList.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (availableQty.compareTo(qty) < 0) {
            return AjaxResult.error(StringUtils.format("挪料失败,库存可用数量:{} 小于挪用数量:{}", availableQty, qty));
        }
        availableInventoryDetailList.sort(Comparator.comparing(InventoryDetail::getQty));
        Map<String, List<InventoryDetail>> map = availableInventoryDetailList.stream()
                .collect(Collectors.groupingBy(InventoryDetail::getContainerCode));
        List<InventoryDetail> updateInventoryDetailList = new ArrayList<>();
        for (Map.Entry<String, List<InventoryDetail>> entry : map.entrySet()) {
            List<InventoryDetail> value = entry.getValue();
            BigDecimal qtySum = value.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
            if (qtySum.compareTo(qty) <= 0) {
                updateInventoryDetailList.addAll(value);
            } else {
                qty = qty.subtract(qtySum);
            }
        }
        LambdaUpdateWrapper<InventoryDetail> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(InventoryDetail::getId, updateInventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList()))
                .set(InventoryDetail::getOrderCode, targetOrderCode);
        boolean success = inventoryDetailService.update(updateWrapper);
        if (!success) {
            throw new ServiceException("挪料失败,更新库存失败");
        }
        return AjaxResult.success("挪料成功");
    }

    @Override
    public AjaxResult syncWorkOrder(MesWorkOrder workOrder) {
        String taskNo = workOrder.getTaskNo();
        if (StringUtils.isEmpty(taskNo)) {
            return AjaxResult.error("同步工单状态失败,任务号为空");
        }
        String orderCode = workOrder.getOrderCode();
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("同步工单状态失败,工单号为空");
        }
        Integer orderStatus = workOrder.getOrderStatus();
        if (Objects.isNull(orderStatus)) {
            return AjaxResult.error("同步工单状态失败,工单状态为空");
        }
        WorkOrderHeader workOrderHeader = workOrderHeaderService.getOrderByCode(orderCode);
        if (workOrderHeader == null) {
            return AjaxResult.error(StringUtils.format("同步工单状态失败,工单:{} 不存在", orderCode));
        }
        WorkOrderHeader updateWorkOrderHeader = new WorkOrderHeader();
        updateWorkOrderHeader.setId(workOrderHeader.getId());
        updateWorkOrderHeader.setOrderStatus(orderStatus);
        boolean success = workOrderHeaderService.updateById(updateWorkOrderHeader);
        if (success) {
            return AjaxResult.success("同步工单状态成功");
        } else {
            return AjaxResult.error(StringUtils.format("同步工单状态失败,工单号:{}", orderCode));
        }
    }

    @Override
    public AjaxResult getMesTracingCode(String zs) {
        String url = addressService.selectAddress(QuantityConstant.MES_GET_TRACINGNO, QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.DEFAULT_AREA);
        /*Map<String, Object> map = new HashMap<>();
        map.put("tracingCode", zs);
        String jsonParam = JSON.toJSONString(map);*/
        String jsonParam = '"' + zs + '"';
        ResponseEntity<JSONObject> result = RestUtil.request_post(url, QuantityConstant.DEFAULT_WAREHOUSE, jsonParam);
        if (result != null && result.getBody() != null) {
            boolean status = result.getBody().getBoolean("status");
            String msg = result.getBody().getString("message");
            if (!status) {
                return AjaxResult.error(msg);
            }
            String data = result.getBody().getString("data");
            return AjaxResult.success(msg).setData(data);
        } else {
            throw new ServiceException("获取MES追溯码失败,接口地址错误或返回为空");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult clearStock(ClearStockDto clearStockDto) {
        String orderCode = clearStockDto.getOrderCode();
        String materialCode = clearStockDto.getMaterialCode();
        String port = clearStockDto.getPort();
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("清库失败,工单号为空");
        }
        if (StringUtils.isEmpty(materialCode)) {
            return AjaxResult.error("清库失败,物料编码为空");
        }
        if (StringUtils.isEmpty(port)) {
            return AjaxResult.error("清库失败,出库口为空");
        }
        Material material = materialService.getUsableByCode(materialCode);
        if (material == null) {
            return AjaxResult.error(StringUtils.format("清库失败,根据物料编码:{} 未找到物料", materialCode));
        }
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderQueryWrapper.eq(InventoryHeader::getOrderCode, orderCode)
                .ne(InventoryHeader::getZoneCode, QuantityConstant.ZONE_TYPE_CBG)
                .eq(InventoryHeader::getContainerStatus, QuantityConstant.STATUS_CONTAINER_SOME);
        List<InventoryHeader> inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderQueryWrapper);
        inventoryHeaderList = inventoryHeaderList.stream().filter(x -> x.getContainerStatus().equals(QuantityConstant.STATUS_CONTAINER_SOME)).collect(Collectors.toList());
        if (inventoryHeaderList.isEmpty()) {
            return AjaxResult.error(StringUtils.format("清库失败,库内暂无工单号为:{} 的可用库存", orderCode));
        }
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(InventoryDetail::getInventoryHeaderId, inventoryHeaderList.stream().map(InventoryHeader::getId).collect(Collectors.toList()))
                .eq(InventoryDetail::getMaterialCode, materialCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        if (inventoryDetailList.isEmpty()) {
            return AjaxResult.error(StringUtils.format("清库失败,库内暂无工单号为:{},物料编码为:{} 的可用库存", orderCode, materialCode));
        }
        BigDecimal clearSumQty = inventoryDetailList.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);

        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_BILL_TYPE_SCRAP);
        shipmentHeader.setTotalQty(clearSumQty);
        shipmentHeader.setTotalLines(1);
        // 生成单据头
        String code = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setCode(code);
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
        shipmentHeader.setLastUpdatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
        shipmentHeader.setCreatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
        shipmentHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        shipmentHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
        boolean success = shipmentHeaderService.save(shipmentHeader);
        ShipmentDetail shipmentDetail = this.createShipmentDetail(materialCode, clearSumQty, shipmentHeader);
        // 生成单据明细
        success = shipmentDetailService.save(shipmentDetail);
        if (!success) {
            throw new ServiceException("清库失败,保存出库单明细失败");
        }
        List<InventoryHeader> shipInventoryHeader = inventoryHeaderList.stream().filter(inventoryHeader -> inventoryDetailList.stream()
                        .map(InventoryDetail::getInventoryHeaderId)
                        .collect(Collectors.toList())
                        .contains(inventoryHeader.getId()))
                .collect(Collectors.toList());
        for (InventoryHeader inventoryHeader : shipInventoryHeader) {
            ShipmentContainerHeader shipmentContainerHeader = this.createShipmentContainerHeader(inventoryHeader, port, shipmentHeader);
            // 生成配盘头
            success = shipmentContainerHeaderService.save(shipmentContainerHeader);
            if (!success) {
                throw new ServiceException("清库失败,保存出库配盘头失败");
            }
            List<InventoryDetail> inventoryDetails = inventoryDetailList.stream()
                    .filter(inventoryDetail -> inventoryDetail.getInventoryHeaderId().equals(inventoryHeader.getId()))
                    .collect(Collectors.toList());
            List<ShipmentContainerDetail> shipmentContainerDetailList = this.createShipmentContainerDetail(shipmentContainerHeader, shipmentDetail, inventoryDetails);
            // 生成配盘明细
            success = shipmentContainerDetailService.saveBatch(shipmentContainerDetailList);
            if (!success) {
                throw new ServiceException("清库失败,保存出库配盘明细败");
            }
            TaskHeader taskHeader = this.createTaskHeader(shipmentContainerHeader, inventoryHeader);
            success = taskHeaderService.save(taskHeader);
            if (!success) {
                throw new ServiceException("清库失败,保存任务头表失败");
            }
            List<TaskDetail> taskDetailList = this.createTaskDetail(taskHeader, shipmentDetail, shipmentContainerDetailList);
            success = taskDetailService.saveBatch(taskDetailList);
            if (!success) {
                throw new ServiceException("清库失败,保存任务明细表失败");
            }
            // 锁定载具和库位
            success = containerService.updateStatus(inventoryHeader.getContainerCode(), QuantityConstant.STATUS_CONTAINER_LOCK);
            if (!success) {
                throw new ServiceException("清库失败,锁定载具失败");
            }
            success = locationService.updateStatus(inventoryHeader.getLocationCode(), QuantityConstant.STATUS_LOCATION_LOCK);
            if (!success) {
                throw new ServiceException("清库失败,锁定库位失败");
            }
            InventoryHeader updateInventoryHeader = new InventoryHeader();
            updateInventoryHeader.setId(inventoryHeader.getId());
            updateInventoryHeader.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
            updateInventoryHeader.setLastUpdatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
            success = inventoryHeaderService.updateById(updateInventoryHeader);
            if (!success) {
                throw new ServiceException("清库失败,锁定库存容器失败");
            }
            // 更新库存任务数量
            List<InventoryDetail> updateInventoryDetailList = new ArrayList<>();
            inventoryDetails.forEach(inventoryDetail -> {
                InventoryDetail updateInventoryDetail = new InventoryDetail();
                updateInventoryDetail.setId(inventoryDetail.getId());
                updateInventoryDetail.setTaskQty(inventoryDetail.getQty());
                updateInventoryDetailList.add(updateInventoryDetail);
            });
            success = inventoryDetailService.updateBatchById(updateInventoryDetailList);
            if (!success) {
                throw new ServiceException("清库失败,更新库存占用数量失败");
            }
        }
        return AjaxResult.success("清库成功");
    }

    private ShipmentDetail createShipmentDetail(String materialCode, BigDecimal qty, ShipmentHeader shipmentHeader) {
        Material material = materialService.getUsableByCode(materialCode);
        if (material == null) {
            throw new ServiceException(StringUtils.format("根据物料编码:{} 未找到物料", materialCode));
        }
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail.setShipmentId(shipmentHeader.getId());
        shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
        shipmentDetail.setCompanyCode(shipmentHeader.getCompanyCode());
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        shipmentDetail.setMaterialCode(material.getCode());
        shipmentDetail.setMaterialName(material.getName());
        shipmentDetail.setMaterialSpec(material.getSpec());
        shipmentDetail.setMaterialUnit(material.getUnit());
        shipmentDetail.setDescription(material.getDescription());
        shipmentDetail.setWeight(material.getWeight());
        shipmentDetail.setQty(qty);
        shipmentDetail.setTaskQty(qty);
        shipmentDetail.setInventorySts(QuantityConstant.INVENTORY_STATUS_OK);
        shipmentDetail.setStatus(shipmentHeader.getLastStatus());
        shipmentDetail.setCreated(new Date());
        shipmentDetail.setCreatedBy(shipmentHeader.getCreatedBy());
        shipmentDetail.setLastUpdated(new Date());
        shipmentDetail.setLastUpdatedBy(shipmentHeader.getLastUpdatedBy());
        return shipmentDetail;
    }

    private ShipmentContainerHeader createShipmentContainerHeader(InventoryHeader inventoryHeader, String port, ShipmentHeader shipmentHeader) {
        ShipmentContainerHeader shipmentContainerHeader = new ShipmentContainerHeader();
        BeanUtils.copyBeanProp(shipmentContainerHeader, inventoryHeader);
        shipmentContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
        shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
        shipmentContainerHeader.setPort(port);
        shipmentContainerHeader.setCreated(new Date());
        shipmentContainerHeader.setCreatedBy(shipmentHeader.getCreatedBy());
        shipmentContainerHeader.setLastUpdated(new Date());
        shipmentContainerHeader.setLastUpdatedBy(shipmentHeader.getLastUpdatedBy());
        shipmentContainerHeader.setShipmentCode(shipmentHeader.getCode());
        return shipmentContainerHeader;
    }

    private List<ShipmentContainerDetail> createShipmentContainerDetail(ShipmentContainerHeader shipmentContainerHeader,
                                                                        ShipmentDetail shipmentDetail, List<InventoryDetail> inventoryDetailList) {
        List<ShipmentContainerDetail> shipmentContainerDetailList = new ArrayList<>();
        inventoryDetailList.forEach(inventoryDetail -> {
            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            BeanUtils.copyBeanProp(shipmentContainerDetail, inventoryDetail);
            shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setShipmentCode(shipmentContainerHeader.getShipmentCode());
            shipmentContainerDetail.setShipmentId(shipmentDetail.getShipmentId());
            shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetail.setStatus(shipmentContainerHeader.getStatus());
            shipmentContainerDetail.setCreated(new Date());
            shipmentContainerDetail.setCreatedBy(shipmentContainerHeader.getCreatedBy());
            shipmentContainerDetail.setLastUpdated(new Date());
            shipmentContainerDetail.setLastUpdatedBy(shipmentContainerHeader.getLastUpdatedBy());
            shipmentContainerDetailList.add(shipmentContainerDetail);
        });
        return shipmentContainerDetailList;
    }

    private TaskHeader createTaskHeader(ShipmentContainerHeader shipmentContainerHeader, InventoryHeader inventoryHeader) {
        TaskHeader taskHeader = new TaskHeader();
        BeanUtils.copyBeanProp(taskHeader, shipmentContainerHeader);
        taskHeader.setZoneCode(inventoryHeader.getZoneCode());
        taskHeader.setAllocationHeadId(shipmentContainerHeader.getId());
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHeader.setFromLocation(shipmentContainerHeader.getLocationCode());
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        taskHeader.setPort(shipmentContainerHeader.getPort());
        taskHeader.setCreated(new Date());
        taskHeader.setLastUpdated(new Date());
        return taskHeader;
    }

    private List<TaskDetail> createTaskDetail(TaskHeader taskHeader, ShipmentDetail shipmentDetail, List<ShipmentContainerDetail> shipmentContainerDetailList) {
        List<TaskDetail> taskDetailList = new ArrayList<>();
        shipmentContainerDetailList.forEach(shipmentContainerDetail -> {
            TaskDetail taskDetail = new TaskDetail();
            BeanUtils.copyBeanProp(taskDetail, shipmentContainerDetail);
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setAllocationId(shipmentContainerDetail.getId());
            taskDetail.setBillDetailId(shipmentDetail.getId());
            taskDetail.setBillCode(shipmentDetail.getShipmentCode());
            taskDetail.setToInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setStatus(taskHeader.getStatus());
            taskDetail.setCreated(new Date());
            taskDetail.setLastUpdated(new Date());
            taskDetailList.add(taskDetail);
        });
        return taskDetailList;
    }

    @Override
    public AjaxResult syncMaterial(List<SyncMaterialDto> syncMaterialDtoList) {
        int materialCodeQty = 0, materialNameQty = 0, vehicleTypeQty = 0, materialTypeQty = 0, enableQty = 0, saveQty = 0, updateQty = 0;
        // 查询该物料是否已存在:存在则更新,不存在则新增
        for (SyncMaterialDto syncMaterialDto : syncMaterialDtoList) {
            // 数据校验
            String materialCode = syncMaterialDto.getMaterialCode();
            String materialName = syncMaterialDto.getMaterialName();
            //String materialSpec = syncMaterialDto.getMaterialSpec();
            //String materialDescription = syncMaterialDto.getDescription();
            String vehicleType = syncMaterialDto.getVehicleType();
            String materialType = syncMaterialDto.getMaterialType();
            //Integer supplyType = syncMaterialDto.getSupplyType();
            Boolean enable = syncMaterialDto.getEnable();
            if (StringUtils.isEmpty(materialCode)) {
                materialCodeQty++;
                continue;
            }
            if (StringUtils.isEmpty(materialName)) {
                materialNameQty++;
                continue;
            }
            if (StringUtils.isEmpty(vehicleType)) {
                vehicleTypeQty++;
                continue;
            }
            if (StringUtils.isEmpty(materialType)) {
                materialTypeQty++;
                continue;
            }
            if (Objects.isNull(enable)) {
                enableQty++;
                continue;
            }
            Material material = materialService.getMaterialByCode(syncMaterialDto.getMaterialCode());
            if (material != null) {
                LambdaUpdateWrapper<Material> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(Material::getCode, material.getCode())
                        .set(Material::getName, syncMaterialDto.getMaterialName())
                        .set(Material::getSpec, syncMaterialDto.getMaterialSpec())
                        .set(StringUtils.isNotEmpty(syncMaterialDto.getMaterialUnit()), Material::getSpec, syncMaterialDto.getMaterialUnit())
                        .set(Material::getDescription, syncMaterialDto.getDescription())
                        .set(Material::getVehicleTypeCode, syncMaterialDto.getVehicleType())
                        .set(Material::getType, syncMaterialDto.getMaterialType())
                        .set(Material::getSupplyType, syncMaterialDto.getSupplyType())
                        .set(StringUtils.isNotEmpty(syncMaterialDto.getTracingType()), Material::getTracingType, syncMaterialDto.getTracingType())
                        .set(!Objects.isNull(syncMaterialDto.getWeight()), Material::getWeight, syncMaterialDto.getWeight())
                        .set(!Objects.isNull(syncMaterialDto.getHigh()), Material::getHigh, syncMaterialDto.getHigh())
                        .set(!Objects.isNull(syncMaterialDto.getEnable()), Material::getEnable, syncMaterialDto.getEnable())
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncMaterialDto.getEnable()), Material::getDeleted, QuantityConstant.NOT_DELETED)
                        .set(Material::getLastUpdatedBy, QuantityConstant.PLATFORM_MES)
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncMaterialDto.getEnable()) && QuantityConstant.DELETED.equals(material.getDeleted()),
                                Material::getCreatedBy, QuantityConstant.PLATFORM_MES)
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncMaterialDto.getEnable()) && QuantityConstant.DELETED.equals(material.getDeleted()),
                                Material::getCreated, new Date());
                boolean success = materialService.update(updateWrapper);
                if (success) {
                    updateQty++;
                }
            } else {
                Material newMaterial = new Material();
                newMaterial.setCode(syncMaterialDto.getMaterialCode());
                newMaterial.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
                newMaterial.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
                newMaterial.setName(syncMaterialDto.getMaterialName());
                newMaterial.setSpec(syncMaterialDto.getMaterialSpec());
                newMaterial.setUnit(syncMaterialDto.getMaterialUnit());
                newMaterial.setSupplyType(syncMaterialDto.getSupplyType());
                newMaterial.setDescription(syncMaterialDto.getDescription());
                newMaterial.setType(syncMaterialDto.getMaterialType());
                newMaterial.setCategory(syncMaterialDto.getCategory());
                newMaterial.setVehicleTypeCode(syncMaterialDto.getVehicleType());
                newMaterial.setEnable(syncMaterialDto.getEnable());
                newMaterial.setCreatedBy(QuantityConstant.PLATFORM_MES);
                newMaterial.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
                newMaterial.setWeight(syncMaterialDto.getWeight());
                newMaterial.setHigh(syncMaterialDto.getHigh());
                newMaterial.setTracingType(syncMaterialDto.getTracingType());
                boolean success = materialService.save(newMaterial);
                if (success) {
                    saveQty++;
                }
            }
        }
        return AjaxResult.success(StringUtils.format("同步物料数据成功,同步数量:{}个,新增成功:{}个,更新成功:{}个。" +
                        "物料编码为空:{}个,物料名称为空:{}个,物料类别为空:{}个," +
                        "盛具类型为空:{}个,可用状态为空:{}个", syncMaterialDtoList.size(), saveQty, updateQty,
                materialCodeQty, materialNameQty, materialTypeQty, vehicleTypeQty, enableQty));
    }

    @Override
    public AjaxResult syncSupplier(List<SyncSupplierDto> syncSupplierDtoList) {
        int supplierCodeQty = 0, supplierNameQty = 0, enableQty = 0, saveQty = 0, updateQty = 0;
        // 查询该供应商是否已存在:存在则更新,不存在则新增
        for (SyncSupplierDto syncSupplierDto : syncSupplierDtoList) {
            // 数据校验
            String supplierCode = syncSupplierDto.getSupplierCode();
            String supplierName = syncSupplierDto.getSupplierName();
            Boolean enable = syncSupplierDto.getEnable();
            if (StringUtils.isEmpty(supplierCode)) {
                supplierCodeQty++;
                continue;
            }
            if (StringUtils.isEmpty(supplierName)) {
                supplierNameQty++;
                continue;
            }
            if (Objects.isNull(enable)) {
                enableQty++;
                continue;
            }
            Supplier supplier = supplierService.getByCode(syncSupplierDto.getSupplierCode());
            if (supplier != null) {
                LambdaUpdateWrapper<Supplier> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(Supplier::getCode, syncSupplierDto.getSupplierCode())
                        .set(Supplier::getName, syncSupplierDto.getSupplierName())
                        .set(StringUtils.isNotEmpty(syncSupplierDto.getState()), Supplier::getState, syncSupplierDto.getState())
                        .set(StringUtils.isNotEmpty(syncSupplierDto.getCity()), Supplier::getCity, syncSupplierDto.getCity())
                        .set(StringUtils.isNotEmpty(syncSupplierDto.getAddress()), Supplier::getAddress1, syncSupplierDto.getAddress())
                        .set(StringUtils.isNotEmpty(syncSupplierDto.getAttentionTo()), Supplier::getAttentionTo, syncSupplierDto.getAttentionTo())
                        .set(StringUtils.isNotEmpty(syncSupplierDto.getPhoneNum()), Supplier::getPhoneNum, syncSupplierDto.getPhoneNum())
                        .set(Supplier::getEnable, syncSupplierDto.getEnable())
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncSupplierDto.getEnable()), Supplier::getDeleted, QuantityConstant.NOT_DELETED)
                        .set(Supplier::getLastUpdatedBy, QuantityConstant.PLATFORM_MES)
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncSupplierDto.getEnable()) && QuantityConstant.DELETED.equals(supplier.getDeleted()),
                                Supplier::getCreatedBy, QuantityConstant.PLATFORM_MES)
                        .set(QuantityConstant.STATUS_ENABLE.equals(syncSupplierDto.getEnable()) && QuantityConstant.DELETED.equals(supplier.getDeleted()),
                                Supplier::getCreated, new Date());
                boolean success = supplierService.update(updateWrapper);
                if (success) {
                    updateQty++;
                }
            } else {
                Supplier newSupplier = new Supplier();
                newSupplier.setCode(syncSupplierDto.getSupplierCode());
                newSupplier.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
                newSupplier.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
                newSupplier.setName(syncSupplierDto.getSupplierName());
                newSupplier.setAddress1(syncSupplierDto.getAddress());
                newSupplier.setAddress2("");
                newSupplier.setCity(syncSupplierDto.getCity());
                newSupplier.setState(syncSupplierDto.getState());
                newSupplier.setAttentionTo(syncSupplierDto.getAttentionTo());
                newSupplier.setPhoneNum(syncSupplierDto.getPhoneNum());
                newSupplier.setEnable(syncSupplierDto.getEnable());
                newSupplier.setCreated(new Date());
                newSupplier.setCreatedBy(QuantityConstant.PLATFORM_MES);
                newSupplier.setLastUpdated(new Date());
                newSupplier.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
                boolean success = supplierService.save(newSupplier);
                if (success) {
                    saveQty++;
                }
            }
        }
        return AjaxResult.success(StringUtils.format("同步供应商数据成功,同步数量:{},新增成功:{}个,更新成功:{}个。" +
                        "供应商编码为空:{}个,供应商名称为空:{}个,物料类别为空:{}个," +
                        "盛具类型为空:{}个,可用状态为空:{}个", syncSupplierDtoList.size(), saveQty, updateQty,
                supplierCodeQty, supplierNameQty, enableQty));
    }

    @Override
    public AjaxResult syncVehicleType(List<SyncVehicleTypeDto> syncVehicleTypeDtoList) {
        int vehicleTypeCodeQty = 0, vehicleTypeNameQty = 0, maxXQty = 0, maxYQty = 0, enableQty = 0, saveQty = 0, updateQty = 0;
        for (SyncVehicleTypeDto syncVehicleTypeDto : syncVehicleTypeDtoList) {
            // 数据校验
            String vehicleTypeCode = syncVehicleTypeDto.getVehicleTypeCode();
            String vehicleTypeName = syncVehicleTypeDto.getVehicleTypeName();
            Integer maxX = syncVehicleTypeDto.getMaxX();
            Integer maxY = syncVehicleTypeDto.getMaxY();
            Boolean enable = syncVehicleTypeDto.getEnable();
            if (StringUtils.isEmpty(vehicleTypeCode)) {
                vehicleTypeCodeQty++;
            }
            if (StringUtils.isEmpty(vehicleTypeName)) {
                vehicleTypeNameQty++;
            }
            if (Objects.isNull(maxX)) {
                maxXQty++;
            }
            if (Objects.isNull(maxY)) {
                maxYQty++;
            }
            if (Objects.isNull(enable)) {
                enableQty++;
            }
            VehicleType vehicleType = vehicleTypeService.getByCode(syncVehicleTypeDto.getVehicleTypeCode());
            if (vehicleType != null) {
                LambdaUpdateWrapper<VehicleType> updateWrapper = Wrappers.lambdaUpdate();
                updateWrapper.eq(VehicleType::getCode, syncVehicleTypeDto.getVehicleTypeCode())
                        .set(VehicleType::getName, syncVehicleTypeDto.getVehicleTypeName())
                        .set(VehicleType::getMaxX, syncVehicleTypeDto.getMaxX())
                        .set(VehicleType::getMaxY, syncVehicleTypeDto.getMaxY())
                        .set(VehicleType::getEnable, syncVehicleTypeDto.getEnable());
                boolean success = vehicleTypeService.update(updateWrapper);
                if (success) {
                    updateQty++;
                }
            } else {
                VehicleType newVehicleType = new VehicleType();
                newVehicleType.setCode(syncVehicleTypeDto.getVehicleTypeCode());
                newVehicleType.setName(syncVehicleTypeDto.getVehicleTypeName());
                newVehicleType.setMaxX(syncVehicleTypeDto.getMaxX());
                newVehicleType.setMaxY(syncVehicleTypeDto.getMaxY());
                newVehicleType.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
                newVehicleType.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
                newVehicleType.setEnable(syncVehicleTypeDto.getEnable());
                newVehicleType.setCreated(new Date());
                newVehicleType.setCreatedBy(QuantityConstant.PLATFORM_MES);
                newVehicleType.setLastUpdated(new Date());
                newVehicleType.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
                boolean success = vehicleTypeService.save(newVehicleType);
                if (success) {
                    saveQty++;
                }
            }
        }
        return AjaxResult.success(StringUtils.format("同步盛具类型数据成功,同步数量:{}个,新增成功:{}个,更新成功:{}个。" +
                        "盛具类型编码为空:{}个,盛具类型名称为空:{}个,行为空:{}个," +
                        "列为空:{}个,可用状态为空:{}个", syncVehicleTypeDtoList.size(), saveQty, updateQty,
                vehicleTypeCodeQty, vehicleTypeNameQty, maxXQty, maxYQty, enableQty));
    }

    @Override
    public AjaxResult<List<EmptyLocationDto>> queryEmptyLocation() {
        List<EmptyLocationDto> emptyLocationDtoList = locationService.queryEmptyLocation();
        return AjaxResult.success(emptyLocationDtoList);
    }

    @Override
    public AjaxResult<?> queryPowder() {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Zone> zoneList = zoneService.list();
        zoneList.forEach(zone -> {
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.getByZoneCode(zone.getCode());
            double powder = inventoryDetailList.stream().mapToDouble(InventoryDetail::getWeight).sum();
            HashMap<String, Object> map = new HashMap<>();
            map.put("zoneCode", zone.getCode());
            map.put("zoneName", zone.getName());
            map.put("powder", powder);
            maps.add(map);
        });
        return AjaxResult.success(maps);
    }

    @Override
    public AjaxResult<?> querySolidifyTime(QuerySolidifyTimeDto querySolidifyTimeDto) {
        String orderCode = querySolidifyTimeDto.getOrderCode();
        String materialCode = querySolidifyTimeDto.getMaterialCode();
        String flowCode = querySolidifyTimeDto.getFlowCode();
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .eq(StringUtils.isNotEmpty(orderCode), InventoryDetail::getOrderCode, orderCode)
                .eq(StringUtils.isNotEmpty(materialCode), InventoryDetail::getMaterialCode, materialCode)
                .and(StringUtils.isEmpty(flowCode), wrapper -> wrapper.eq(InventoryDetail::getFlowCode, StringUtils.EMPTY).or().isNull(InventoryDetail::getFlowCode))
                .eq(StringUtils.isNotEmpty(flowCode), InventoryDetail::getFlowCode, flowCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        if (inventoryDetailList.isEmpty()) {
            return AjaxResult.success(StringUtils.format("根据条件,工单号:【{}】,物料编码:【{}】,工序号:【{}】 未找到库存",
                    orderCode, materialCode, flowCode));
        }
        List<HashMap<String, Object>> returnMapList = new ArrayList<>();
        Map<String, List<InventoryDetail>> map = inventoryDetailList.stream().collect(Collectors.groupingBy(InventoryDetail::getLocationCode));
        for (Map.Entry<String, List<InventoryDetail>> entry : map.entrySet()) {
            HashMap<String, Object> returnMap = new HashMap<>();
            List<InventoryDetail> value = entry.getValue();
            Optional<InventoryDetail> optional = value.stream().findFirst();
            if (optional.isPresent()) {
                InventoryDetail inventoryDetail = optional.get();
                returnMap.put("locationCode", inventoryDetail.getLocationCode());
                returnMap.put("solidifyTime", inventoryDetail.getSolidifyTime());
                returnMap.put("receiptTime", inventoryDetail.getCreated());
                // 此方法获取的是相差多少秒
                long differenceSecond = DateUtils.difference(new Date(), inventoryDetail.getCreated());
                // 转化为小时
                long differenceHour = differenceSecond / (60 * 60);
                // 计算剩余固化时间
                double surplusTime = (inventoryDetail.getSolidifyTime() - differenceHour) < 0 ? 0 : inventoryDetail.getSolidifyTime() - differenceHour;
                returnMap.put("surplusTime", surplusTime);
                returnMapList.add(returnMap);
            }
        }
        return AjaxResult.success(returnMapList);
    }

    @Override
    public AjaxResult<?> productShipmentDto(ProductShipmentDto productShipmentDto) {
        // 1、数据校验
        String shipmentType = productShipmentDto.getShipmentType();
        String referCode = productShipmentDto.getReferCode();
        String orderCode = productShipmentDto.getOrderCode();
        String toPort = productShipmentDto.getToPort();
        List<ProductShipmentDetailDto> productShipmentDetailList = productShipmentDto.getProductShipmentDetailList();
        if (StringUtils.isEmpty(shipmentType)) {
            return AjaxResult.error("出库失败,出库类型为空");
        }
        if (StringUtils.isEmpty(referCode)) {
            return AjaxResult.error("出库失败,上有单号为空");
        }
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("出库失败,工单号为空");
        }
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("出库失败,出库口为空");
        }
        if (productShipmentDetailList == null || productShipmentDetailList.isEmpty()) {
            return AjaxResult.error("出库失败,出库物料信息为空");
        }
        for (ProductShipmentDetailDto productShipmentDetailDto : productShipmentDetailList) {
            String materialCode = productShipmentDetailDto.getMaterialCode();
            if (StringUtils.isEmpty(materialCode)) {
                return AjaxResult.error("出库失败,物料信息中有物料编码为空");
            }
            Material material = materialService.getUsableByCode(materialCode);
            if (material == null) {
                return AjaxResult.error(StringUtils.format("出库失败,根据物料编码:{} 未找到物料", materialCode));
            }
            BigDecimal qty = productShipmentDetailDto.getQty();
            if (Objects.isNull(qty) || qty.compareTo(BigDecimal.ZERO) <= 0) {
                return AjaxResult.error("出库失败,物料信息中有物料需求数量为空或小于等于0");
            }

            // 2、出库
            String flowCode = productShipmentDetailDto.getFlowCode();
            Integer materialInspectStatus = productShipmentDetailDto.getMaterialInspectStatus();
            String supplierCode = productShipmentDetailDto.getSupplierCode();
            String supplierBatchNumber = productShipmentDetailDto.getSupplierBatchNumber();
            LambdaQueryWrapper<InventoryDetail> inventoryDetailQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailQueryWrapper
                    .ne(InventoryDetail::getZoneCode, QuantityConstant.ZONE_TYPE_CBG)
                    .eq(StringUtils.isNotEmpty(orderCode), InventoryDetail::getOrderCode, orderCode)
                    .and(StringUtils.isEmpty(flowCode), wrapper -> wrapper.eq(InventoryDetail::getFlowCode, StringUtils.EMPTY).or().isNull(InventoryDetail::getFlowCode))
                    .eq(StringUtils.isNotEmpty(flowCode), InventoryDetail::getFlowCode, flowCode)
                    .eq(StringUtils.isNotEmpty(supplierBatchNumber), InventoryDetail::getSupplierBatch, supplierBatchNumber)
                    .eq(StringUtils.isNotEmpty(supplierCode), InventoryDetail::getSupplierCode, supplierCode)
                    .eq(!Objects.isNull(materialInspectStatus), InventoryDetail::getMaterialInspectStatus, materialInspectStatus)
                    .apply("qty > taskQty")
                    .orderByAsc(InventoryDetail::getCreated);
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailQueryWrapper);
            if (inventoryDetailList.isEmpty()) {
                return AjaxResult.error("出库失败,根据出库条件未找到可用库存");
            }
            BigDecimal inventoryQty = inventoryDetailList.stream().map(InventoryDetail::getQty)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            if (inventoryQty.compareTo(qty) < 0) {
                return AjaxResult.error(StringUtils.format("出库失败,根据出库条件找到的可用库存数量不足,当前可用数量:{}", inventoryQty));
            }
            List<String> shipmentContainerCodeList = new LinkedList<>();
            Map<String, List<InventoryDetail>> inventoryMap = inventoryDetailList.stream().collect(Collectors.groupingBy(InventoryDetail::getContainerCode));
            for (Map.Entry<String, List<InventoryDetail>> entry : inventoryMap.entrySet()) {
                List<InventoryDetail> inventoryDetails = entry.getValue();
                BigDecimal shipmentQty = inventoryDetails.stream().map(InventoryDetail::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
                qty = qty.subtract(shipmentQty);
                shipmentContainerCodeList.add(entry.getKey());
                if (qty.compareTo(BigDecimal.ZERO) <= 0) {
                    break;
                }
            }
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderQueryWrapper.in(InventoryHeader::getContainerCode, shipmentContainerCodeList);
            List<InventoryHeader> inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderQueryWrapper);
            BigDecimal shipmentQty = inventoryHeaderList.stream().map(InventoryHeader::getTotalQty).reduce(BigDecimal.ZERO, BigDecimal::add);
            ShipmentHeader shipmentHeader = new ShipmentHeader();
            shipmentHeader.setShipmentType(shipmentType);
            shipmentHeader.setTotalQty(shipmentQty);
            shipmentHeader.setTotalLines(productShipmentDetailList.size());
            // 生成单据头
            String code = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
            shipmentHeader.setCode(code);
            shipmentHeader.setReferCode(referCode);
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
            shipmentHeader.setLastUpdatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
            shipmentHeader.setCreatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
            shipmentHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            shipmentHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY_CODE);
            boolean success = shipmentHeaderService.save(shipmentHeader);
            ShipmentDetail shipmentDetail = this.createShipmentDetail(materialCode, shipmentQty, shipmentHeader);
            // 生成单据明细
            success = shipmentDetailService.save(shipmentDetail);
            if (!success) {
                throw new ServiceException("出库失败,保存出库单明细失败");
            }

            for (InventoryHeader inventoryHeader : inventoryHeaderList) {
                ShipmentContainerHeader shipmentContainerHeader = this.createShipmentContainerHeader(inventoryHeader, toPort, shipmentHeader);
                // 生成配盘头
                success = shipmentContainerHeaderService.save(shipmentContainerHeader);
                if (!success) {
                    throw new ServiceException("出库失败,保存出库配盘头失败");
                }
                List<InventoryDetail> inventoryDetails = inventoryDetailList.stream()
                        .filter(inventoryDetail -> inventoryDetail.getInventoryHeaderId().equals(inventoryHeader.getId()))
                        .collect(Collectors.toList());
                List<ShipmentContainerDetail> shipmentContainerDetailList = this.createShipmentContainerDetail(shipmentContainerHeader, shipmentDetail, inventoryDetails);
                // 生成配盘明细
                success = shipmentContainerDetailService.saveBatch(shipmentContainerDetailList);
                if (!success) {
                    throw new ServiceException("出库失败,保存出库配盘明细败");
                }
                TaskHeader taskHeader = this.createTaskHeader(shipmentContainerHeader, inventoryHeader);
                success = taskHeaderService.save(taskHeader);
                if (!success) {
                    throw new ServiceException("出库失败,保存任务头表失败");
                }
                List<TaskDetail> taskDetailList = this.createTaskDetail(taskHeader, shipmentDetail, shipmentContainerDetailList);
                success = taskDetailService.saveBatch(taskDetailList);
                if (!success) {
                    throw new ServiceException("出库失败,保存任务明细表失败");
                }
                // 锁定载具和库位
                success = containerService.updateStatus(inventoryHeader.getContainerCode(), QuantityConstant.STATUS_CONTAINER_LOCK);
                if (!success) {
                    throw new ServiceException("出库失败,锁定载具失败");
                }
                success = locationService.updateStatus(inventoryHeader.getLocationCode(), QuantityConstant.STATUS_LOCATION_LOCK);
                if (!success) {
                    throw new ServiceException("出库失败,锁定库位失败");
                }
                InventoryHeader updateInventoryHeader = new InventoryHeader();
                updateInventoryHeader.setId(inventoryHeader.getId());
                updateInventoryHeader.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                updateInventoryHeader.setLastUpdatedBy(StringUtils.isNotEmpty(ShiroUtils.getLoginName()) ? ShiroUtils.getLoginName() : QuantityConstant.PLATFORM_MES);
                success = inventoryHeaderService.updateById(updateInventoryHeader);
                if (!success) {
                    throw new ServiceException("出库失败,锁定库存失败");
                }
                // 更新库存任务数量
                List<InventoryDetail> updateInventoryDetailList = new ArrayList<>();
                inventoryDetails.forEach(inventoryDetail -> {
                    InventoryDetail updateInventoryDetail = new InventoryDetail();
                    updateInventoryDetail.setId(inventoryDetail.getId());
                    updateInventoryDetail.setTaskQty(inventoryDetail.getQty());
                    updateInventoryDetailList.add(updateInventoryDetail);
                });
                success = inventoryDetailService.updateBatchById(updateInventoryDetailList);
                if (!success) {
                    throw new ServiceException("出库失败,更新库存占用数量失败");
                }
            }
        }
        return AjaxResult.success("出库成功");
    }

    @Override
    public AjaxResult<?> queryVehicleCodeByQC(List<QueryVehicleCodeByQCDto> queryVehicleCodeByQCList) {

        queryVehicleCodeByQCList.forEach(query -> {
            String flowCode = query.getFlowCode();
            String tracingNo = query.getTracingNo();
            LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper
                    .eq(StringUtils.isNotEmpty(tracingNo), InventoryDetail::getTracingNo, tracingNo)
                    .and(StringUtils.isEmpty(flowCode), wrapper -> wrapper.eq(InventoryDetail::getFlowCode, StringUtils.EMPTY).or().isNull(InventoryDetail::getFlowCode))
                    .eq(StringUtils.isNotEmpty(flowCode), InventoryDetail::getFlowCode, flowCode);
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
            if (inventoryDetailList.isEmpty()) {
                query.setVehicleCode(StringUtils.EMPTY);
            } else {
                String vehicleCode = inventoryDetailList.stream()
                        .map(InventoryDetail::getVehicleCode)
                        .distinct().findFirst().orElse("");
                query.setVehicleCode(vehicleCode);
            }
        });
        return AjaxResult.success(queryVehicleCodeByQCList);
    }

    @Override
    public AjaxResult<?> updateMaterialInspectStatus(UpdateMaterialInspectStatusDto updateMaterialInspectStatusDto) {
        String materialCode = updateMaterialInspectStatusDto.getMaterialCode();
        String flowCode = updateMaterialInspectStatusDto.getFlowCode();
        String orderCode = updateMaterialInspectStatusDto.getOrderCode();
        Integer vehicleQty = updateMaterialInspectStatusDto.getVehicleQty();
        Integer materialInspectStatus = updateMaterialInspectStatusDto.getMaterialInspectStatus();
        Integer changeStatus = updateMaterialInspectStatusDto.getChangeStatus();

        if (StringUtils.isEmpty(materialCode)) {
            return AjaxResult.error("更新物料质检状态失败,物料编码为空");
        }
        if (StringUtils.isEmpty(orderCode)) {
            return AjaxResult.error("更新物料质检状态失败,工单号为空");
        }
        if (StringUtils.isNull(vehicleQty)) {
            return AjaxResult.error("更新物料质检状态失败,盛具编码为空");
        }
        if (vehicleQty <= 0) {
            return AjaxResult.error("更新物料质检状态失败,盛具数量有误");
        }
        if (StringUtils.isNull(materialInspectStatus)) {
            return AjaxResult.error("更新物料质检状态失败,物料质检状态为空");
        }
        if (StringUtils.isNull(changeStatus)) {
            return AjaxResult.error("更新物料质检状态失败,更新后的物料质检状态为空");
        }
        LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .eq(InventoryDetail::getOrderCode, orderCode)
                .eq(StringUtils.isNotEmpty(flowCode), InventoryDetail::getFlowCode, flowCode)
                .eq(InventoryDetail::getMaterialCode, materialCode)
                .select(InventoryDetail::getVehicleCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(queryWrapper);
        List<String> vehicleCodeList = inventoryDetailList.stream()
                .map(InventoryDetail::getVehicleCode).distinct().collect(Collectors.toList());
        int vehicleSize = vehicleCodeList.size();
        if (vehicleSize != vehicleQty) {
            return AjaxResult.error(StringUtils.format("更新物料质检状态失败,统计出的盛具数量:{} 与参数数量:{} 不一致", vehicleSize, vehicleQty));
        }

        LambdaUpdateWrapper<InventoryDetail> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper
                .eq(InventoryDetail::getOrderCode, orderCode)
                .eq(StringUtils.isNotEmpty(flowCode), InventoryDetail::getFlowCode, flowCode)
                .eq(InventoryDetail::getMaterialCode, materialCode)
                .in(InventoryDetail::getVehicleCode, vehicleCodeList)
                .eq(InventoryDetail::getMaterialInspectStatus, materialInspectStatus)
                .set(InventoryDetail::getMaterialInspectStatus, changeStatus);
        if (!inventoryDetailService.update(updateWrapper)) {
            return AjaxResult.error("更新物料质检状态失败");
        }
        return AjaxResult.success("更新成物料质检状态成功");
    }
}