PDAServiceImpl.java 249 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 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265
package com.huaheng.api.erp.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.erp.domain.BoxInfo;
import com.huaheng.api.erp.domain.ChipInfos;
import com.huaheng.api.erp.domain.Document;
import com.huaheng.api.erp.domain.push.PushModel;
import com.huaheng.api.wcs.service.WcsService;
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.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.bosAssistantDetail.domain.BosAssistantDetail;
import com.huaheng.pc.config.bosAssistantDetail.service.IBosAssistantDetailService;
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.proPackaging.domain.ProPackaging;
import com.huaheng.pc.config.proPackaging.service.IProPackagingService;
import com.huaheng.pc.config.proPackaging.service.impl.ProPackagingServiceImpl;
import com.huaheng.pc.config.productScheduleHeader.domain.ProductScheduleHeader;
import com.huaheng.pc.config.productScheduleHeader.service.IProductScheduleHeaderService;
import com.huaheng.pc.inventory.InventoryHistoryDetail.domain.InventoryHistoryDetail;
import com.huaheng.pc.inventory.InventoryHistoryDetail.mapper.InventoryHistoryDetailMapper;
import com.huaheng.pc.inventory.InventoryHistoryDetail.service.impl.InventoryHistoryDetailServiceImpl;
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.inventory.inventoryHistoryHeader.domain.InventoryHistoryHeader;
import com.huaheng.pc.inventory.inventoryHistoryHeader.service.impl.InventoryHistoryHeaderServiceImpl;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
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.reservationSubmission.domain.ReservationSubmission;
import com.huaheng.pc.reservationSubmission.service.IReservationSubmissionService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
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.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
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 com.huaheng.pc.tool.ChooseWrapperService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 游杰
 */
@Service
public class PDAServiceImpl implements PDAService {

    @Resource
    private InventoryDetailService inventoryDetailService;
    @Autowired
    private InventoryHistoryDetailMapper inventoryHistoryDetailMapper;
    @Resource
    private ConfigService configService;
    @Resource
    private IProductScheduleHeaderService productScheduleHeaderService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryHistoryHeaderServiceImpl inventoryHistoryHeaderService;
    @Resource
    private InventoryHistoryDetailServiceImpl inventoryHistoryDetailService;
    @Resource
    private ChooseWrapperService chooseWrapper;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ERPService erpService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ProPackagingServiceImpl proPackingService;
    @Resource
    private LocationService locationService;
    @Resource
    private ContainerService containerService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private WcsService wcsService;
    @Resource
    private IProPackagingService proPackagingService;
    @Resource
    private IBosAssistantDetailService bosAssistantDetailService;
    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private IReservationSubmissionService reservationSubmissionService;
    @Resource
    private PDAService pdaService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult clearInventory(String warehouseCode, String invetoryContaienrCode) {
        Container inventoryContainer = containerService.getContainerByCode(invetoryContaienrCode, warehouseCode);
        if (inventoryContainer == null) {
            return AjaxResult.error("没有找到容器数据:" + invetoryContaienrCode);
        }
        String locationCode = inventoryContainer.getLocationCode();
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error("容器没有对应的库位数据:" + locationCode);
        }
        Location location = locationService.getLocationByCode(locationCode, warehouseCode);
        String shipmentCode = location.getShipmentCode();
        if (StringUtils.isEmpty(shipmentCode)) {
            return AjaxResult.error("库位" + locationCode + "没有找到对应的出库订单组");
        }


        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper1.eq(ShipmentHeader::getCode, shipmentCode);
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper1);
        if (shipmentHeader1 == null) {
            return AjaxResult.error("当前出库单不存在 是不是被误删除了");
        }

        if (shipmentHeader1.getPseudoShipment()&&!shipmentHeader1.getCode().contains("PS") && shipmentHeader1.getDestStockId().contains("CK00") && shipmentHeader1.getFSrcStockId().contains("CK00")) {
            if (!shipmentHeader1.getDestStockId().contains("CK007") || !shipmentHeader1.getFSrcStockId().contains("CK007"))
            {
                throw new ServiceException("当前为内部调拨单 无需释放 移动至平库");
            }
        }

        if (StringUtils.isEmpty(shipmentHeader1.getFSrcStockId()) && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)
                && shipmentHeader1.getShipmentType().equals(QuantityConstant.SHIPMENT_BILL_TYPE_DN) && shipmentHeader1.getPseudoShipment()) {
            throw new ServiceException("当前订单在k3为调拨单 无需释放 请使用移库功能移动至平库");
        }
        if (shipmentHeader1.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_COMPLETED) {
            return AjaxResult.error("当前出库单状态不是拣选完成");
        }

        LambdaQueryWrapper<ReservationSubmission> reservationSubmissionLambdaQueryWrapper = Wrappers.lambdaQuery();
        reservationSubmissionLambdaQueryWrapper.eq(ReservationSubmission::getAuditStatus, "adopt").eq(ReservationSubmission::getOrderNumber,
                shipmentHeader1.getReferCode());
        int count = reservationSubmissionService.count(reservationSubmissionLambdaQueryWrapper);
        if (!locationCode.contains(QuantityConstant.VIRTUAL_PALLET_TYPE) && StringUtils.isEmpty(shipmentHeader1.getFSrcStockId()) && count < 1
                && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)) {
            return AjaxResult.error("预约审核信息查询不存在 请查看是不是还没审核");
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.select(TaskHeader::getId).eq(TaskHeader::getShipmentCode, shipmentCode).lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
        if (taskHeaderList.size() > 0) {
            List<Integer> taskIds = taskHeaderList.stream().map(TaskHeader::getId).collect(Collectors.toList());
            return AjaxResult.error("还有任务没有完成,任务号为:" + taskIds);
        }

        // 先查出不是P30的任务总数(第一阶段)
        taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getShipmentCode, shipmentCode).ne(TaskHeader::getPortGroup, QuantityConstant.PORTGROUP_P30)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_OVER_STATION);
        taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);

        List<String> collect = taskHeaderList.stream().map(TaskHeader::getContainerCode).collect(Collectors.toList());

        if (collect.size() != 0) {
            LambdaQueryWrapper<AgvTask> agvTaskLambdaQueryWrapper = Wrappers.lambdaQuery();
            agvTaskLambdaQueryWrapper.in(AgvTask::getContainerCode, collect).ne(AgvTask::getStatus, QuantityConstant.AGV_TASK_STATUS_COMPLETED);
            List<AgvTask> taskList = agvTaskService.list(agvTaskLambdaQueryWrapper);
            if (taskList.size() > 0) {
                return AjaxResult.error("还有没完成的agv任务的,任务号为:" + collect);
            }
        }

        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getShipmentCode, shipmentCode);
        List<Location> locations = locationService.list(locationLambdaQueryWrapper);
        if (locations.size() == 0) {
            return AjaxResult.error("没有找到库位组," + locationCode);
        }
        List<Location> locationList = locations.stream().filter(l -> StringUtils.isNotEmpty(l.getContainerCode())).collect(Collectors.toList());


        List<Container> containerList = new ArrayList<>();
        for (Location location1 : locations) {
            String containerCode = location1.getContainerCode();
            if (StringUtils.isNotEmpty(containerCode)) {
                Container container = containerService.getContainerByCode(containerCode, warehouseCode);
                if (container != null) {
                    containerList.add(container);
                }
            }
        }
        if (containerList.size() == 0) {
            return AjaxResult.error("没有匹配到容器组");
        }
        List<String> containerCodeList = containerList.stream().map(Container::getCode).distinct().collect(Collectors.toList());
        List<String> locationCodeList = locationList.stream().map(Location::getCode).collect(Collectors.toList());
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper
                .select(InventoryDetail::getId,InventoryDetail::getReceiptCode).in(InventoryDetail::getLocationCode, locationCodeList);
       List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存详情");
        }

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.in(InventoryHeader::getLocationCode, locationCodeList);
        List<InventoryHeader> inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeaderList.size() == 0) {
            return AjaxResult.error("没有找到库存头");
        }

        List<String> receiptCodeList = inventoryDetailList.stream().map(InventoryDetail::getReceiptCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper
                .select(ReceiptHeader::getCode,ReceiptHeader::getShipmentCode).in(ReceiptHeader::getCode, receiptCodeList);
        List<ReceiptHeader> receiptHeaderList = receiptHeaderService.list(receiptHeaderLambdaQueryWrapper);
        List<ShipmentHeader> shipmentHeaderList =new ArrayList<>();
        boolean result = false;

        //lty test 为什么这里查询了要修改没有做任何数据上改变
        if (!receiptHeaderList.isEmpty())
        {
            List<String> shipmentCodeList = receiptHeaderList.stream().map(ReceiptHeader::getShipmentCode).distinct().collect(Collectors.toList());
            LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentHeaderLambdaQueryWrapper.in(ShipmentHeader::getCode, shipmentCodeList);
            shipmentHeaderList=shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper);
            if (shipmentHeaderList.size() < 1 && !shipmentCode.contains("OS")) {
                return AjaxResult.error("没有找到出库单头");
            }
//            else if (!shipmentCode.contains("OS")) {
//                result = shipmentHeaderService.updateBatchById(shipmentHeaderList);
//                if (!result) {
//                    throw new ServiceException("更新出库表单头失败");
//                }
//            }
        }
        // 提取所有的 InventoryHeader ID
        List<Integer> inventoryHeaderIds = inventoryHeaderList.stream()
                .map(InventoryHeader::getId)
                .collect(Collectors.toList());

        // 一次性查询所有的 InventoryDetail
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapperx = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapperx.in(InventoryDetail::getInventoryHeaderId, inventoryHeaderIds);
        List<InventoryDetail> allInventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapperx);

        // 将 InventoryDetail 按 InventoryHeaderId 分组
        Map<Integer, List<InventoryDetail>> inventoryDetailsMap = allInventoryDetails.stream()
                .collect(Collectors.groupingBy(InventoryDetail::getInventoryHeaderId));

        // 创建两个列表,一个用于保存新的记录,一个用于更新已有记录
        List<InventoryHistoryHeader> saveList = new ArrayList<>();
        List<InventoryHistoryHeader> updateList = new ArrayList<>();

        for (InventoryHeader inventoryHeader : inventoryHeaderList) {
            InventoryHistoryHeader inventoryHistoryHeader = new InventoryHistoryHeader();

                inventoryHistoryHeader = new InventoryHistoryHeader();
                // 填充 inventoryHistoryHeader 的数据
                inventoryHistoryHeader.setId(inventoryHeader.getId());
                inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
                inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
                inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
                inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
                inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
                inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
                inventoryHistoryHeader.setStack(inventoryHeader.getStack());
                inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
                inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
                inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
                inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
                inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
                inventoryHistoryHeader.setColor(inventoryHeader.getColor());
                inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
                inventoryHistoryHeader.setProductSize(inventoryHeader.getProductSize());
                inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
                inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
                if (!receiptHeaderList.isEmpty()) {
                    inventoryHistoryHeader.setReceiptCode(receiptHeaderList.get(0).getCode());
                }
                if (!shipmentHeaderList.isEmpty()) {
                    inventoryHistoryHeader.setShipmentCode(shipmentHeaderList.get(0).getCode());
                    inventoryHistoryHeader.setOrderCode(shipmentHeaderList.get(0).getReferCode());
                    inventoryHistoryHeader.setOrderName(shipmentHeaderList.get(0).getCustomerName());
                }
                inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
                inventoryHistoryHeader.setTotalLines(0);
                inventoryHistoryHeader.setUserDef1("销售出库");

                // 将需要保存的记录添加到 saveList 中
                saveList.add(inventoryHistoryHeader);


            BigDecimal boxQty = new BigDecimal(0);
            BigDecimal totalQty = new BigDecimal(0);
            List<String> boxCodeList = new ArrayList<>();
            int totalLines = 0;

            // 从 Map 中获取当前 InventoryHeader 的 InventoryDetail 列表
            List<InventoryDetail> inventoryDetailList1 = inventoryDetailsMap.get(inventoryHeader.getId());
            if (inventoryDetailList1 != null) {
                for (InventoryDetail inventoryDetail : inventoryDetailList1) {
                    totalQty = totalQty.add(inventoryDetail.getQty());
                    totalLines++;
                    if (!boxCodeList.contains(inventoryDetail.getBoxCode())) {
                        boxCodeList.add(inventoryDetail.getBoxCode());
                    }
                }
                addHistoryInventoryDetail(inventoryDetailList1, inventoryHistoryHeader, "销售出库");
            }

            boxQty = new BigDecimal(boxCodeList.size());
            inventoryHistoryHeader.setBoxQty(boxQty);
            inventoryHistoryHeader.setTotalQty(totalQty);
            inventoryHistoryHeader.setTotalLines(totalLines);

            // 将需要更新的记录添加到 updateList 中
            updateList.add(inventoryHistoryHeader);
        }

        // 批量保存
        if (!saveList.isEmpty()) {
            inventoryHistoryHeaderService.saveBatch(saveList);
        }

        // 批量更新
        if (!updateList.isEmpty()) {
            inventoryHistoryHeaderService.updateBatchById(updateList);
        }


        if (!shipmentHeaderList.isEmpty()) {
            List<Integer> shipmentHeaderIdList = shipmentHeaderList.stream().map(ShipmentHeader::getId).collect(Collectors.toList());
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.in(ShipmentDetail::getShipmentId, shipmentHeaderIdList);
            List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
            for (ShipmentDetail shipmentDetail : shipmentDetailList) {
                BigDecimal qty = shipmentDetail.getQty();
                shipmentDetail.setTaskQty(qty);
            }
            result = shipmentDetailService.updateBatchById(shipmentDetailList);
            if (!result) {
                throw new ServiceException("更新出库表单详情头失败");
            }
        }

        List<Integer> inventoryDetailIds = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        result = inventoryDetailService.removeByIds(inventoryDetailIds);
        if (!result) {
            throw new ServiceException("删除库存失败");
        }

        List<Integer> inventoryHeaderIdsx = inventoryHeaderList.stream().map(InventoryHeader::getId).collect(Collectors.toList());
        result = inventoryHeaderService.removeByIds(inventoryHeaderIdsx);
        if (!result) {
            throw new ServiceException("删除库存失败");
        }

        // 提前定义一个 Map 用于存储查询到的 Container 对象,以便后续使用
        Map<String, Container> containerMap = new HashMap<>();
        Map<String, List<Container>> locationContainersMap = new HashMap<>();

        // 批量查询所有容器信息
        List<Container> allContainers = containerService.list();

        // 根据 containerCode 和 warehouseCode 将容器信息放入 Map 和 locationContainersMap 中
        for (Container container : allContainers) {
            containerMap.put(container.getCode(), container);
            if (StringUtils.isNotEmpty(container.getLocationCode())) {
                List<Container> containers = locationContainersMap.computeIfAbsent(container.getLocationCode(), k -> new ArrayList<>());
                containers.add(container);
            }
        }

        //lty test  for循环外做查询
        for (String containerCode : containerCodeList) {
            Container container = containerMap.get(containerCode);
            if (container == null) {
                continue; // 容器未找到,跳过该循环
            }

            if (containerCode.contains(QuantityConstant.VIRTUAL_PALLET_TYPE)) {
                if (StringUtils.isEmpty(container.getLocationCode())) {
                    throw new ServiceException("容器的库位不能为空 容器是" + container.getCode());
                }

                // 获取同一库位的容器列表
                List<Container> containersAtSameLocation = locationContainersMap.get(container.getLocationCode());
                if (containersAtSameLocation != null) {
                    for (Container container1 : containersAtSameLocation) {
                        containerService.removeById(container1.getId());
                    }
                }
            } else {
                containerService.updateLocationCodeAndStatus(containerCode, "", QuantityConstant.STATUS_CONTAINER_EMPTY, warehouseCode);
            }
        }


        List<Location> removenLocations = new ArrayList<>();
        for (Location location2 : locations) {
            location2.setContainerCode("");
            location2.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
            location2.setShipmentCode("");
            location2.setStack(0);
            location2.setContainerSpec(0);
            if (location2.getCode().contains(QuantityConstant.VIRTUAL_PALLET_TYPE)) {
                if (!locationService.removeById(location2.getId())) {
                    throw new ServiceException("删除库位失败");
                }
                removenLocations.add(location2);
            }
        }
        locations.removeAll(removenLocations);
        if (locations.size() > 0) {
            result = locationService.updateBatchById(locations);
            if (!result) {
                throw new ServiceException("批量更新库位失败");
            }
        }



        if (!shipmentHeaderList.isEmpty()) {
            shipmentHeaderList.removeIf(S -> S.getPseudoShipment());
            if (shipmentHeaderList.size() == 0) {
                shipmentHeaderList.add(shipmentHeader1);
            }
            if (shipmentHeaderList.size() > 0 && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)) {
                LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper2 = Wrappers.lambdaQuery();
                shipmentHeaderLambdaQueryWrapper2.eq(ShipmentHeader::getRelaNoticeCode,shipmentHeader1.getReferCode())
                .eq(ShipmentHeader::getUserDef3,"单托");
                List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper2);
                String qty = null;
                if (!shipmentHeaders.isEmpty()){
                    BigDecimal reduce = shipmentHeaderList.stream().map(ShipmentHeader::getTotalQty).reduce(BigDecimal.ZERO, BigDecimal::add);
                    qty = reduce.subtract(shipmentHeaders.stream().map(ShipmentHeader::getTotalQty).reduce(BigDecimal.ZERO, BigDecimal::add))+"";
                }


                if (shipmentHeaderList.size() > 1) {
                    shipmentHeaderList.removeIf(header -> !header.getCode().equals(shipmentCode));
                    if (shipmentHeaderList.size() != 1) {
                        throw new ServiceException("释放有问题 先卡住查bug 请赶紧联系下开发人员");
                    }
                }

                if(!shipmentHeader1.getCode().contains("OS"))
                {
                    pdaService.createSPShipment(shipmentHeaderList,qty,false); // 创建销售出库单
                }
            }

            if (shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS))
            {
                if (shipmentHeader1.getShipmentType().equals(QuantityConstant.SHIPMENT_BILL_TYPE_DN))
                {
                    PushModel model = new PushModel();
                    model.setHeaderId(shipmentHeader1.getId());
                    model.setReferType(QuantityConstant.SHIPMENT_BILL_TYPE_OS);
                    AjaxResult ajaxResult = erpService.push(model);
                    if (ajaxResult.getCode() != 200 )
                    {
                        shipmentHeader1.setReturnMessage(ajaxResult.getMsg());
                        shipmentHeader1.setReturnQty(shipmentHeader1.getReturnQty()+1);
                        shipmentHeaderService.updateById(shipmentHeader1);
                    }
                }
            }
        }

        return AjaxResult.success("释放库存成功 出库单号" + shipmentHeader1.getCode()).setData(shipmentHeader1.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createShipment(String platformCode, List<String> refereCodeList) {
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper.in(ShipmentHeader::getReferCode, refereCodeList);
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper);
        if (!(shipmentHeaders != null && shipmentHeaders.size() > 0)) {
            return AjaxResult.error("没有找到出库单");
        }
        for (ShipmentHeader shipmentHeader : shipmentHeaders) {
            int isCreated = shipmentHeader.getIsCreated();
            if (isCreated == 1) {
                return AjaxResult.error("出库单已经有创建单据");
            }
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentHeader.getCode());
            List<ShipmentDetail> shipmentDetails = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
            if (shipmentDetails.size() == 0) {
                return AjaxResult.error("没有找到出库单详情");
            }
            String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
            String companyCode = QuantityConstant.DEFAULT_COMPANY;
            ShipmentHeader shipmentHeader1 = new ShipmentHeader();
            String code = shipmentHeaderService.createCode("SO");
            shipmentHeader1.setCode(code);
            shipmentHeader1.setWarehouseCode(warehouseCode);
            shipmentHeader1.setCompanyCode(companyCode);
            shipmentHeader1.setTotalQty(BigDecimal.ZERO);
            shipmentHeader1.setTotalWeight(BigDecimal.ZERO);
            shipmentHeader1.setTotalLines(0);
            shipmentHeader1.setPriority(100);
            shipmentHeader1.setFirstStatus(0);
            shipmentHeader1.setLastStatus(0);
            shipmentHeader1.setShipmentType("SO");
            shipmentHeader1.setRelaNoticeCode(shipmentHeader.getReferCode());
            shipmentHeader1.setCreatedBy(QuantityConstant.PLATFORM_ERP);
            shipmentHeaderService.save(shipmentHeader1);
            for (ShipmentDetail shipmentDetail : shipmentDetails) {
                shipmentDetail.setCreatedBy(QuantityConstant.PLATFORM_WMS);
                shipmentDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_WMS);
                shipmentDetail.setSoEntryId(null);
                shipmentDetail.setFHTZDetail(null);
                shipmentDetail.setTaskQty(new BigDecimal(0));
                shipmentDetail.setId(null);
                shipmentDetail.setShipmentId(shipmentHeader1.getId());
                shipmentDetail.setShipmentCode(shipmentHeader1.getCode());
            }
            shipmentDetailService.saveBatch(shipmentDetails);
            shipmentHeader.setIsCreated(1);
            shipmentHeaderService.updateById(shipmentHeader);
        }
        return AjaxResult.success("创建销售出库单成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentContainerWhole(String containerCode) {
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, containerCode);
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeader == null) {
            return AjaxResult.error("没有找到库存头,托盘号为:" + containerCode);
        }
        String warehouseCode = inventoryHeader.getWarehouseCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("没有找到托盘,托盘号为:" + containerCode);
        }
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存明细,托盘号为:" + containerCode);
        }
        InventoryHistoryHeader inventoryHistoryHeader = inventoryHistoryHeaderService.getById(inventoryHeader.getId());
        if (inventoryHistoryHeader == null) {
            inventoryHistoryHeader = new InventoryHistoryHeader();
            inventoryHistoryHeader.setId(inventoryHeader.getId());
            inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
            inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
            inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
            inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
            inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            inventoryHistoryHeader.setStack(inventoryHeader.getStack());
            inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
            inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
            inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
            inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
            inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
            inventoryHistoryHeader.setColor(inventoryHeader.getColor());
            inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
            inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
            inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
            inventoryHistoryHeader.setOrderCode(inventoryHeader.getOrderCode());
            inventoryHistoryHeader.setOrderName(inventoryHeader.getOrderName());
            inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
            inventoryHistoryHeader.setUserDef1("质检到出库口");
            inventoryHistoryHeader.setTotalLines(0);
            inventoryHistoryHeaderService.save(inventoryHistoryHeader);
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT).eq(TaskHeader::getContainerCode, containerCode)
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader == null) {
            return AjaxResult.error("扣减库存 没有找到出库任务,托盘号:" + containerCode);
        }
        List<TaskDetail> taskDetailList = new ArrayList();
        BigDecimal shipQty = BigDecimal.ZERO;
        String shipmentCode = taskHeader.getShipmentCode();
        String materialCode = taskHeader.getMaterialCode();
        ShipmentDetail shipmentDetail = null;
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (inventoryHeader.getLevel().equals(QuantityConstant.ACCEPTABLE_PRODUCT_LEVEL)) {
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getMaterialCode, materialCode).eq(ShipmentDetail::getShipmentCode, shipmentCode)
                    .eq(ShipmentDetail::getColor, inventoryHeader.getColor()).eq(ShipmentDetail::getLevel, inventoryHeader.getLevel())
                    .eq(ShipmentDetail::getProPackaging, inventoryHeader.getProPackaging());
            shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);
        } else {
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getMaterialCode, materialCode).eq(ShipmentDetail::getShipmentCode, shipmentCode)
                    .eq(ShipmentDetail::getColor, inventoryHeader.getColor()).eq(ShipmentDetail::getLevel, inventoryHeader.getLevel())
                    .eq(ShipmentDetail::getProPackaging, inventoryHeader.getProPackaging()).eq(ShipmentDetail::getBatch, inventoryHeader.getBatch())
                    .eq(ShipmentDetail::getProductSchedule, inventoryHeader.getProductSchedule());
            shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);
        }
        if (shipmentDetail == null) {
            throw new ServiceException("根据出库单号:" + shipmentCode + "没有找到出库详情");
        }
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
        if (shipmentDetail == null) {
            throw new ServiceException("根据出库单号:" + shipmentCode + "没有找到出库头");
        }
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                .eq(ShipmentContainerHeader::getShipmentCode, shipmentCode).lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getOne(shipmentContainerHeaderLambdaQueryWrapper);
        if (shipmentContainerHeader == null) {
            shipmentContainerHeader = new ShipmentContainerHeader();
            shipmentContainerHeader.setContainerCode(containerCode);
            shipmentContainerHeader.setLocationCode(inventoryHeader.getLocationCode());
            shipmentContainerHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            shipmentContainerHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            shipmentContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
            shipmentContainerHeader.setContainerType(container.getContainerType());
            shipmentContainerHeader.setStatus(0);
            shipmentContainerHeader.setShipmentCode(shipmentCode);
            shipmentContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeaderService.save(shipmentContainerHeader);
        }

        List<InventoryTransaction> inventoryTransactionList = new ArrayList<>();
        List<ShipmentContainerDetail> shipmentContainerDetailList = new ArrayList<>();
        addHistoryInventoryDetail(inventoryDetailList, inventoryHistoryHeader, "整托出库");
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT);
            inventoryTransaction.setWarehouseCode(inventoryDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(inventoryDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(inventoryDetail.getLocationCode());
            inventoryTransaction.setContainerCode(inventoryDetail.getContainerCode());
            inventoryTransaction.setMaterialCode(inventoryDetail.getMaterialCode());
            inventoryTransaction.setZoneCode(inventoryDetail.getZoneCode());
            inventoryTransaction.setMaterialName(inventoryDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(inventoryDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(inventoryDetail.getMaterialUnit());
            inventoryTransaction.setChipCode1(inventoryDetail.getChipCode1());
            inventoryTransaction.setChipCode2(inventoryDetail.getChipCode2());
            inventoryTransaction.setLevel(inventoryDetail.getLevel());
            inventoryTransaction.setColor(inventoryDetail.getColor());
            inventoryTransaction.setBoxCode(inventoryDetail.getBoxCode());
            inventoryTransaction.setBoxPosition(inventoryDetail.getBoxPosition());
            inventoryTransaction.setBillCode(shipmentCode);
            inventoryTransaction.setBatch(inventoryDetail.getBatch());
            inventoryTransaction.setLot(inventoryDetail.getLot());
            inventoryTransaction.setInventorySts(inventoryDetail.getInventorySts());
            inventoryTransaction.setTaskQty(inventoryDetail.getQty());
            shipQty = shipQty.add(inventoryDetail.getQty());
            inventoryTransactionList.add(inventoryTransaction);


            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            shipmentContainerDetail.setWarehouseCode(warehouseCode);
            shipmentContainerDetail.setContainerCode(containerCode);
            shipmentContainerDetail.setLocationCode(inventoryDetail.getLocationCode());
            shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetail.setShipmentCode(shipmentCode);
            shipmentContainerDetail.setShipmentId(shipmentHeader.getId());
            shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setCompanyCode(inventoryDetail.getCompanyCode());
            shipmentContainerDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentContainerDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentContainerDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
            shipmentContainerDetail.setChipCode1(inventoryDetail.getChipCode1());
            shipmentContainerDetail.setChipCode2(inventoryDetail.getChipCode2());
            shipmentContainerDetail.setLevel(inventoryDetail.getLevel());
            shipmentContainerDetail.setColor(inventoryDetail.getColor());
            shipmentContainerDetail.setBoxCode(inventoryDetail.getBoxCode());
            shipmentContainerDetail.setBoxPosition(inventoryDetail.getBoxPosition());
            shipmentContainerDetail.setProPackaging(inventoryDetail.getProPackaging());
            shipmentContainerDetail.setProductSize(inventoryDetail.getProductSize());
            shipmentContainerDetailList.add(shipmentContainerDetail);

            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
            taskDetail.setCompanyCode(taskHeader.getCompanyCode());
            taskDetail.setContainerCode(taskHeader.getContainerCode());
            taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            taskDetail.setMaterialName(inventoryDetail.getMaterialName());
            taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            taskDetail.setBoxCode(inventoryDetail.getBoxCode());
            taskDetail.setBoxPosition(inventoryDetail.getBoxPosition());
            taskDetail.setChipCode1(inventoryDetail.getChipCode1());
            taskDetail.setChipCode2(inventoryDetail.getChipCode2());
            taskDetail.setLevel(inventoryDetail.getLevel());
            taskDetail.setColor(inventoryDetail.getColor());
            taskDetail.setQty(inventoryDetail.getQty());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
            taskDetail.setBatch(inventoryDetail.getBatch());
            taskDetail.setInventorySts(inventoryDetail.getInventorySts());
            taskDetail.setCreatedBy(taskHeader.getCreatedBy());
            taskDetail.setProPackaging(inventoryDetail.getProPackaging());
            taskDetail.setProductSchedule(inventoryDetail.getProductSchedule());
            taskDetailList.add(taskDetail);
        }
        if (!inventoryTransactionService.saveBatch(inventoryTransactionList)) {
            throw new ServiceException("新增库存记录失败");
        }
        if (!taskDetailService.saveBatch(taskDetailList)) {
            throw new ServiceException("新增任务详情失败");
        }
        if (!shipmentContainerDetailService.saveBatch(shipmentContainerDetailList)) {
            throw new ServiceException("新增出库组盘详情失败");
        }
        List<Integer> ids = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        boolean result = inventoryDetailService.removeByIds(ids);
        if (!result) {
            throw new ServiceException("删除库存明细失败, 托盘号为:" + containerCode);
        }
        BigDecimal taskQty = shipmentDetail.getTaskQty().add(shipQty);
        BigDecimal qty = shipmentDetail.getQty();
        ShipmentDetail shipmentDetailCop=new ShipmentDetail();
        shipmentDetailCop.setTaskQty(taskQty);
        shipmentDetailCop.setId(shipmentDetail.getId());
        shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_REVIEWSUCCESS);
        shipmentContainerHeaderService.updateById(shipmentContainerHeader);

        result = shipmentDetailService.updateById(shipmentDetailCop);
        if (!result) {
            throw new ServiceException("更新出库详情失败, 托盘号为:" + containerCode);
        }
        updateInventoryHeader(inventoryHeader);
        updateSihpmnentHeaderStatus(shipmentHeader);
        return AjaxResult.success("整托出库成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentContainer(String containerCode,String shipmentDetailId) {
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, containerCode);
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeader == null) {
            return AjaxResult.error("没有找到库存头,托盘号为:" + containerCode);
        }
        String warehouseCode = inventoryHeader.getWarehouseCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("没有找到托盘,托盘号为:" + containerCode);
        }
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存明细,托盘号为:" + containerCode);
        }
        InventoryHistoryHeader inventoryHistoryHeader = inventoryHistoryHeaderService.getById(inventoryHeader.getId());
        if (inventoryHistoryHeader == null) {
            inventoryHistoryHeader = new InventoryHistoryHeader();
            inventoryHistoryHeader.setId(inventoryHeader.getId());
            inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
            inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
            inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
            inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
            inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            inventoryHistoryHeader.setStack(inventoryHeader.getStack());
            inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
            inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
            inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
            inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
            inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
            inventoryHistoryHeader.setColor(inventoryHeader.getColor());
            inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
            inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
            inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
            inventoryHistoryHeader.setOrderCode(inventoryHeader.getOrderCode());
            inventoryHistoryHeader.setOrderName(inventoryHeader.getOrderName());
            inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
            inventoryHistoryHeader.setTotalLines(0);
            inventoryHistoryHeaderService.save(inventoryHistoryHeader);
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT).eq(TaskHeader::getContainerCode, containerCode)
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);

        List<TaskDetail> taskDetailList = new ArrayList();
        BigDecimal shipQty = BigDecimal.ZERO;
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getId,shipmentDetailId);
        ShipmentDetail shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);
        if (shipmentDetail == null) {
            throw new ServiceException("根据选择的出库明细id没有找到出库详情");
        }
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
        if (shipmentHeader==null)
        {
            throw new ServiceException("根据选择的出库明细id没有找到出库头");
        }

        String shipmentCode = shipmentHeader.getCode();
        String materialCode = shipmentDetail.getMaterialCode();

        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                .eq(ShipmentContainerHeader::getShipmentCode, shipmentCode).lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getOne(shipmentContainerHeaderLambdaQueryWrapper);
        if (shipmentContainerHeader == null) {
            shipmentContainerHeader = new ShipmentContainerHeader();
            shipmentContainerHeader.setContainerCode(containerCode);
            shipmentContainerHeader.setLocationCode(inventoryHeader.getLocationCode());
            shipmentContainerHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            shipmentContainerHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            shipmentContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
            shipmentContainerHeader.setContainerType(container.getContainerType());
            shipmentContainerHeader.setStatus(0);
            shipmentContainerHeader.setShipmentCode(shipmentCode);
            shipmentContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeaderService.save(shipmentContainerHeader);
        }

        List<InventoryTransaction> inventoryTransactionList = new ArrayList<>();
        List<ShipmentContainerDetail> shipmentContainerDetailList = new ArrayList<>();
        addHistoryInventoryDetail(inventoryDetailList, inventoryHistoryHeader, "整托出库");
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT);
            inventoryTransaction.setWarehouseCode(inventoryDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(inventoryDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(inventoryDetail.getLocationCode());
            inventoryTransaction.setContainerCode(inventoryDetail.getContainerCode());
            inventoryTransaction.setMaterialCode(inventoryDetail.getMaterialCode());
            inventoryTransaction.setZoneCode(inventoryDetail.getZoneCode());
            inventoryTransaction.setMaterialName(inventoryDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(inventoryDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(inventoryDetail.getMaterialUnit());
            inventoryTransaction.setChipCode1(inventoryDetail.getChipCode1());
            inventoryTransaction.setChipCode2(inventoryDetail.getChipCode2());
            inventoryTransaction.setLevel(inventoryDetail.getLevel());
            inventoryTransaction.setColor(inventoryDetail.getColor());
            inventoryTransaction.setBoxCode(inventoryDetail.getBoxCode());
            inventoryTransaction.setBoxPosition(inventoryDetail.getBoxPosition());
            inventoryTransaction.setBillCode(shipmentCode);
            inventoryTransaction.setBatch(inventoryDetail.getBatch());
            inventoryTransaction.setLot(inventoryDetail.getLot());
            inventoryTransaction.setInventorySts(inventoryDetail.getInventorySts());
            inventoryTransaction.setTaskQty(inventoryDetail.getQty());
            shipQty = shipQty.add(inventoryDetail.getQty());
            inventoryTransactionList.add(inventoryTransaction);



            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            shipmentContainerDetail.setWarehouseCode(warehouseCode);
            shipmentContainerDetail.setContainerCode(containerCode);
            shipmentContainerDetail.setLocationCode(inventoryDetail.getLocationCode());
            shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetail.setShipmentCode(shipmentCode);
            shipmentContainerDetail.setShipmentId(shipmentHeader.getId());
            shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setCompanyCode(inventoryDetail.getCompanyCode());
            shipmentContainerDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentContainerDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentContainerDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
            shipmentContainerDetail.setChipCode1(inventoryDetail.getChipCode1());
            shipmentContainerDetail.setChipCode2(inventoryDetail.getChipCode2());
            shipmentContainerDetail.setLevel(inventoryDetail.getLevel());
            shipmentContainerDetail.setColor(inventoryDetail.getColor());
            shipmentContainerDetail.setBoxCode(inventoryDetail.getBoxCode());
            shipmentContainerDetail.setBoxPosition(inventoryDetail.getBoxPosition());
            shipmentContainerDetail.setProPackaging(inventoryDetail.getProPackaging());
            shipmentContainerDetail.setProductSize(inventoryDetail.getProductSize());
            shipmentContainerDetailList.add(shipmentContainerDetail);

            if (taskHeader != null) {
                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetail.setContainerCode(taskHeader.getContainerCode());
                taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
                taskDetail.setMaterialName(inventoryDetail.getMaterialName());
                taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
                taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
                taskDetail.setBoxCode(inventoryDetail.getBoxCode());
                taskDetail.setBoxPosition(inventoryDetail.getBoxPosition());
                taskDetail.setChipCode1(inventoryDetail.getChipCode1());
                taskDetail.setChipCode2(inventoryDetail.getChipCode2());
                taskDetail.setLevel(inventoryDetail.getLevel());
                taskDetail.setColor(inventoryDetail.getColor());
                taskDetail.setQty(inventoryDetail.getQty());
                taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
                taskDetail.setBatch(inventoryDetail.getBatch());
                taskDetail.setInventorySts(inventoryDetail.getInventorySts());
                taskDetail.setCreatedBy(taskHeader.getCreatedBy());
                taskDetail.setProPackaging(inventoryDetail.getProPackaging());
                taskDetail.setProductSchedule(inventoryDetail.getProductSchedule());
                taskDetailList.add(taskDetail);
            }
        }
        if (!inventoryTransactionService.saveBatch(inventoryTransactionList)) {
            throw new ServiceException("新增库存记录失败");
        }
        if (!taskDetailList.isEmpty())
        {
            if (!taskDetailService.saveBatch(taskDetailList)) {
                throw new ServiceException("新增任务详情失败");
            }
        }

        if (!shipmentContainerDetailService.saveBatch(shipmentContainerDetailList)) {
            throw new ServiceException("新增出库组盘详情失败");
        }
        List<Integer> ids = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        boolean result = inventoryDetailService.removeByIds(ids);
        if (!result) {
            throw new ServiceException("删除库存明细失败, 托盘号为:" + containerCode);
        }
        ShipmentDetail shipmentDetailCop=new ShipmentDetail();
        BigDecimal taskQty = shipmentDetail.getTaskQty().add(shipQty);
        BigDecimal qty = shipmentDetail.getQty();
        shipmentDetailCop.setTaskQty(taskQty);
        shipmentDetailCop.setId(shipmentDetail.getId());
        if (taskQty.compareTo(qty) == 0) {
            shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_REVIEWSUCCESS);
            shipmentContainerHeaderService.updateById(shipmentContainerHeader);
        } else {
            if (taskQty.compareTo(qty) > 0) {
                throw new ServiceException("不允许超出单据设定的出库数量");
            }
        }
        result = shipmentDetailService.updateById(shipmentDetailCop);
        if (!result) {
            throw new ServiceException("更新出库详情失败, 托盘号为:" + containerCode);
        }
        updateInventoryHeader(inventoryHeader);
        updateSihpmnentHeaderStatus(shipmentHeader);
        return AjaxResult.success("整托出库成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentBox(String containerCode, String boxCode,String shipmentDetailId) {
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, containerCode);
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeader == null) {
            return AjaxResult.error("没有找到库存头,托盘号为:" + containerCode);
        }
        String warehouseCode = inventoryHeader.getWarehouseCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("没有找到托盘,托盘号为:" + containerCode);
        }
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode).eq(InventoryDetail::getBoxCode, boxCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存明细,托盘号为:" + containerCode + ", 箱码为:" + boxCode);
        }
        InventoryHistoryHeader inventoryHistoryHeader = inventoryHistoryHeaderService.getById(inventoryHeader.getId());
        if (inventoryHistoryHeader == null) {
            inventoryHistoryHeader = new InventoryHistoryHeader();
            inventoryHistoryHeader.setId(inventoryHeader.getId());
            inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
            inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
            inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
            inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
            inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            inventoryHistoryHeader.setStack(inventoryHeader.getStack());
            inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
            inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
            inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
            inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
            inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
            inventoryHistoryHeader.setColor(inventoryHeader.getColor());
            inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
            inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
            inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
            inventoryHistoryHeader.setOrderCode(inventoryHeader.getOrderCode());
            inventoryHistoryHeader.setOrderName(inventoryHeader.getOrderName());
            inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
            inventoryHistoryHeader.setTotalLines(0);
            inventoryHistoryHeaderService.save(inventoryHistoryHeader);
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT).eq(TaskHeader::getContainerCode, containerCode)
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);

        List<TaskDetail> taskDetailList = new ArrayList();
        BigDecimal shipQty = BigDecimal.ZERO;

        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getLevel, inventoryHeader.getLevel())
                .eq(ShipmentDetail::getColor, inventoryHeader.getColor())
                .eq(ShipmentDetail::getId,shipmentDetailId)
                .eq(ShipmentDetail::getProductSchedule, inventoryHeader.getProductSchedule())
                .eq(ShipmentDetail::getProPackaging, inventoryHeader.getProPackaging());


        ShipmentDetail shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);
        if (shipmentDetail == null) {
            throw new ServiceException("根据选择的出库单id没有找到出库详情");
        }
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
        if (shipmentHeader == null) {
            throw new ServiceException("根据出库单id没有找到出库头");
        }

        String shipmentCode = shipmentHeader.getCode();

        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                .eq(ShipmentContainerHeader::getShipmentCode, shipmentCode).lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getOne(shipmentContainerHeaderLambdaQueryWrapper);
        if (shipmentContainerHeader == null) {
            shipmentContainerHeader = new ShipmentContainerHeader();
            shipmentContainerHeader.setContainerCode(containerCode);
            shipmentContainerHeader.setLocationCode(inventoryHeader.getLocationCode());
            shipmentContainerHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
            shipmentContainerHeader.setCompanyCode(inventoryHeader.getCompanyCode());
            shipmentContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
            shipmentContainerHeader.setContainerType(container.getContainerType());
            shipmentContainerHeader.setStatus(0);
            shipmentContainerHeader.setShipmentCode(shipmentCode);
            shipmentContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_WMS);
            shipmentContainerHeaderService.save(shipmentContainerHeader);
        }

        List<InventoryTransaction> inventoryTransactionList = new ArrayList<>();
        List<ShipmentContainerDetail> shipmentContainerDetailList = new ArrayList<>();
        addHistoryInventoryDetail(inventoryDetailList, inventoryHistoryHeader, "整箱出库");
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT);
            inventoryTransaction.setWarehouseCode(inventoryDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(inventoryDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(inventoryDetail.getLocationCode());
            inventoryTransaction.setContainerCode(inventoryDetail.getContainerCode());
            inventoryTransaction.setMaterialCode(inventoryDetail.getMaterialCode());
            inventoryTransaction.setZoneCode(inventoryDetail.getZoneCode());
            inventoryTransaction.setMaterialName(inventoryDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(inventoryDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(inventoryDetail.getMaterialUnit());
            inventoryTransaction.setChipCode1(inventoryDetail.getChipCode1());
            inventoryTransaction.setChipCode2(inventoryDetail.getChipCode2());
            inventoryTransaction.setLevel(inventoryDetail.getLevel());
            inventoryTransaction.setColor(inventoryDetail.getColor());
            inventoryTransaction.setBoxCode(inventoryDetail.getBoxCode());
            inventoryTransaction.setBoxPosition(inventoryDetail.getBoxPosition());
            inventoryTransaction.setBillCode(shipmentCode);
            inventoryTransaction.setBatch(inventoryDetail.getBatch());
            inventoryTransaction.setUserDef1("整箱出库");
            inventoryTransaction.setLot(inventoryDetail.getLot());
            inventoryTransaction.setInventorySts(inventoryDetail.getInventorySts());
            inventoryTransaction.setTaskQty(inventoryDetail.getQty());
            shipQty = shipQty.add(inventoryDetail.getQty());
            inventoryTransactionList.add(inventoryTransaction);

            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            shipmentContainerDetail.setWarehouseCode(warehouseCode);
            shipmentContainerDetail.setContainerCode(containerCode);
            shipmentContainerDetail.setLocationCode(inventoryDetail.getLocationCode());
            shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetail.setShipmentCode(shipmentCode);
            shipmentContainerDetail.setShipmentId(shipmentHeader.getId());
            shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setCompanyCode(inventoryDetail.getCompanyCode());
            shipmentContainerDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentContainerDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentContainerDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
            shipmentContainerDetail.setChipCode1(inventoryDetail.getChipCode1());
            shipmentContainerDetail.setChipCode2(inventoryDetail.getChipCode2());
            shipmentContainerDetail.setLevel(inventoryDetail.getLevel());
            shipmentContainerDetail.setColor(inventoryDetail.getColor());
            shipmentContainerDetail.setBoxCode(inventoryDetail.getBoxCode());
            shipmentContainerDetail.setBoxPosition(inventoryDetail.getBoxPosition());
            shipmentContainerDetail.setProPackaging(inventoryDetail.getProPackaging());
            shipmentContainerDetail.setProductSize(inventoryDetail.getProductSize());
            shipmentContainerDetailList.add(shipmentContainerDetail);
            if (taskHeader != null) {
                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetail.setContainerCode(taskHeader.getContainerCode());
                taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
                taskDetail.setMaterialName(inventoryDetail.getMaterialName());
                taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
                taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
                taskDetail.setBoxCode(inventoryDetail.getBoxCode());
                taskDetail.setBoxPosition(inventoryDetail.getBoxPosition());
                taskDetail.setChipCode1(inventoryDetail.getChipCode1());
                taskDetail.setChipCode2(inventoryDetail.getChipCode2());
                taskDetail.setLevel(inventoryDetail.getLevel());
                taskDetail.setColor(inventoryDetail.getColor());
                taskDetail.setQty(inventoryDetail.getQty());
                taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
                taskDetail.setBatch(inventoryDetail.getBatch());
                taskDetail.setInventorySts(inventoryDetail.getInventorySts());
                taskDetail.setCreatedBy(taskHeader.getCreatedBy());
                taskDetail.setProPackaging(inventoryDetail.getProPackaging());
                taskDetail.setProductSchedule(inventoryDetail.getProductSchedule());
                taskDetailList.add(taskDetail);
            }
        }
        if (!inventoryTransactionService.saveBatch(inventoryTransactionList)) {
            throw new ServiceException("新增库存记录失败");
        }
        if (!taskDetailList.isEmpty())
        {
            if (!taskDetailService.saveBatch(taskDetailList)) {
                throw new ServiceException("新增任务详情失败");
            }
        }
        if (!shipmentContainerDetailService.saveBatch(shipmentContainerDetailList)) {
            throw new ServiceException("新增出库组盘详情失败");
        }
        List<Integer> ids = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        boolean result = inventoryDetailService.removeByIds(ids);
        if (!result) {
            throw new ServiceException("删除库存明细失败, 托盘号为:" + containerCode);
        }
        ShipmentDetail shipmentDetailCop=new ShipmentDetail();
        BigDecimal taskQty = shipmentDetail.getTaskQty().add(shipQty);
        BigDecimal qty = shipmentDetail.getQty();
        shipmentDetailCop.setTaskQty(taskQty);
        shipmentDetailCop.setId(shipmentDetail.getId());
        if (taskQty.compareTo(qty) == 0) {
            shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_REVIEWSUCCESS);
            shipmentContainerHeaderService.updateById(shipmentContainerHeader);
        } else {
            if (taskQty.compareTo(qty) > 0) {
                throw new ServiceException("不允许超出单据设定的出库数量");
            }
        }
        result = shipmentDetailService.updateById(shipmentDetailCop);
        if (!result) {
            throw new ServiceException("更新出库详情失败, 托盘号为:" + containerCode);
        }
        updateInventoryHeader(inventoryHeader);
        updateSihpmnentHeaderStatus(shipmentHeader);

        return AjaxResult.success("整箱出库成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult returnOfGoodsReceipt(String containerCode, String receiptCode, List<BoxInfo> listBoxInfo, String receiptDetailId) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("根据托盘号,没有找到托盘信息:" + containerCode);
        }
        String locationCode = container.getLocationCode();
        if (StringUtils.isNotEmpty(locationCode)) {
            return AjaxResult.error("托盘在库位上:" + locationCode);
        }
        if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
            return AjaxResult.error("托盘" + container.getCode() + "已锁定");
        }
        LambdaQueryWrapper<ReceiptContainerHeader> receiptHeaderControllerLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderControllerLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).lt(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.list(receiptHeaderControllerLambdaQueryWrapper);
        if (receiptContainerHeaders.size() > 0) {
            return AjaxResult.error("当前托盘已存在任务了");
        }

        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
        if (receiptHeader == null) {
            return AjaxResult.error("没有找到入库单");
        }
        BoxInfo boxInfo = listBoxInfo.get(0);
        String materialCode = boxInfo.getMaterialCode();
        String color = boxInfo.getColor();
        String level = boxInfo.getLevel();
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode).eq(ReceiptDetail::getMaterialCode, materialCode).eq(ReceiptDetail::getId,
                receiptDetailId);

        ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailLambdaQueryWrapper);
        if (receiptDetail == null) {
            return AjaxResult.error("没有匹配到入库单详情");
        }
        getBosAssistantNumber(color, "colour");
        getBosAssistantNumber(level, "level");
        BigDecimal total = BigDecimal.ZERO;
        String materialCode1 = null, batch1 = null, level1 = null, color1 = null, proPackage1 = null, productSize1 = null;
        for (BoxInfo boxInfo1 : listBoxInfo) {
            materialCode1 = boxInfo1.getMaterialCode();
            color1 = boxInfo1.getColor();
            level1 = boxInfo1.getLevel();
            batch1 = boxInfo1.getBatch();
            proPackage1 = boxInfo1.getProPackaging();
            productSize1 = boxInfo1.getSize();

            if (!boxInfo1.getBatch().equals(receiptDetail.getBatch())) {
                return AjaxResult.error("实际批次" + boxInfo1.getBatch() + "与订单批次" + receiptDetail.getBatch() + "不匹配。" + "箱码" + boxInfo1.getBoxCode());
            }

            if (!materialCode.equals(materialCode1)) {
                return AjaxResult.error("物料信息不匹配");
            }
            if (!color.equals(color1)) {
                return AjaxResult.error("色号信息不匹配");
            }
            if (!level.equals(level1)) {
                return AjaxResult.error("等级信息不匹配");
            }
            BigDecimal qty = boxInfo1.getQty();
            total = total.add(qty);
        }
        BigDecimal taskQty = receiptDetail.getTaskQty();
        BigDecimal qty = receiptDetail.getQty();
        taskQty = taskQty.add(total);
        if (taskQty.compareTo(qty) > 0) {
            return AjaxResult.error("入库数量不能大于单据数量");
        }
        receiptDetail.setTaskQty(taskQty);
        boolean result = receiptDetailService.updateById(receiptDetail);
        if (!result) {
            throw new RuntimeException("更新入库单详情失败");
        }
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(warehouseCode);
        receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
        receiptContainerHeader.setContainerCode(containerCode);
        receiptContainerHeader.setBatch(batch1);
        receiptContainerHeader.setOperatorName(ShiroUtils.getLoginName());
        receiptContainerHeader.setLevel(level1);
        receiptContainerHeader.setColor(color1);
        receiptContainerHeader.setProPackaging(proPackage1);
        receiptContainerHeader.setProductSchedule(receiptDetail.getProductSchedule());
        receiptContainerHeader.setOrderCode(receiptDetail.getProductSchedule());// 订单编号
        ProductScheduleHeader productScheduleHeader = productScheduleHeaderService
                .getOne(new LambdaQueryWrapper<ProductScheduleHeader>().eq(ProductScheduleHeader::getNumber, receiptDetail.getProductSchedule()));
        if (productScheduleHeader == null) {
            throw new ServiceException("排产订单不存在,请先更新排产订单。");
        }
        receiptContainerHeader.setOrderName(productScheduleHeader.getName());// 订单名称
        receiptContainerHeader.setMaterialCode(materialCode1);// 物料编码
//        receiptContainerHeader1.setContainerType(containerTypeCode);
        receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
//        receiptContainerHeader1.setStack(stack);//设置垛型
        receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_CODING);
        receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
        result = receiptContainerHeaderService.save(receiptContainerHeader);
        if (!result) {
            throw new ServiceException("保存入库组盘头失败");
        }

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        List<InventoryHistoryDetail> inventoryHistoryDetailList = new ArrayList<>();
        for (BoxInfo boxInfo2 : listBoxInfo) {
            String boxCode2 = boxInfo2.getBoxCode();
//            int boxPosition2 = boxInfo2.getBoxPosition();
            List<ChipInfos> chipInfoList2 = boxInfo2.getChipInfos();
            String level2 = boxInfo2.getLevel();
            String proPackagingStr2 = boxInfo2.getProPackaging();
            String productSize2 = boxInfo2.getSize();
            String color2 = boxInfo2.getColor();
            String batch2 = boxInfo2.getBatch();
            String productSize = boxInfo2.getProductSize();
            for (ChipInfos chipInfos : chipInfoList2) {
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                String chipCode1 = chipInfos.getChipBarcode1();
                String chipCode2 = chipInfos.getChipBarcode2();
                receiptContainerDetail.setWarehouseCode(warehouseCode);
                receiptContainerDetail.setContainerCode(containerCode);
                receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                receiptContainerDetail.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
                receiptContainerDetail.setChipCode1(chipCode1);
                receiptContainerDetail.setChipCode2(chipCode2);
                receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
                receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
                receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                receiptContainerDetail.setProPackaging(proPackagingStr2);// 包装
                receiptContainerDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                receiptContainerDetail.setProductSize(productSize2);// 产品规格
                receiptContainerDetail.setLevel(level2);// 等级
                receiptContainerDetail.setColor(color2);// 色号
                receiptContainerDetail.setQty(BigDecimal.ONE);
                receiptContainerDetail.setBatch(batch2);
                receiptContainerDetail.setProductSize(productSize);
                receiptContainerDetail.setReceiptCode(receiptDetail.getProductSchedule());
                receiptContainerDetail.setBoxCode(boxCode2);
                receiptContainerDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetailList.add(receiptContainerDetail);

                InventoryHistoryDetail inventoryHistoryDetail = new InventoryHistoryDetail();
                inventoryHistoryDetail.setInventoryHistoryHeaderId(0);
                inventoryHistoryDetail.setWarehouseCode(warehouseCode);
                inventoryHistoryDetail.setContainerCode(containerCode);
                inventoryHistoryDetail.setLocationCode("");
                inventoryHistoryDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                inventoryHistoryDetail.setChipCode1(chipCode1);
                inventoryHistoryDetail.setChipCode2(chipCode2);
                inventoryHistoryDetail.setMaterialCode(receiptDetail.getMaterialCode());
                inventoryHistoryDetail.setMaterialName(receiptDetail.getMaterialName());
                inventoryHistoryDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                inventoryHistoryDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                inventoryHistoryDetail.setProPackaing(proPackagingStr2);
                inventoryHistoryDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                inventoryHistoryDetail.setProductSize(productSize2);// 产品规格
                inventoryHistoryDetail.setLevel(level2);// 等级
                inventoryHistoryDetail.setColor(color2);// 色号
                inventoryHistoryDetail.setQty(BigDecimal.ONE);
                inventoryHistoryDetail.setTaskQty(BigDecimal.ZERO);
                inventoryHistoryDetail.setBatch(batch2);
                inventoryHistoryDetail.setReceiptCode(receiptDetail.getProductSchedule());
                inventoryHistoryDetail.setBoxCode(boxCode2);
                inventoryHistoryDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                inventoryHistoryDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                inventoryHistoryDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                inventoryHistoryDetail.setReceiptDetailId(receiptDetail.getId());
                inventoryHistoryDetail.setUserDef1("退货入库");
                inventoryHistoryDetailList.add(inventoryHistoryDetail);
            }
        }
        result = chooseWrapper.receiptContainerDetailServiceSaveBatch(receiptContainerDetailList);
        if (!result) {
            throw new ServiceException("保存入库组盘详情失败");
        }

        result = inventoryHistoryDetailService.saveBatch(inventoryHistoryDetailList);
        if (!result) {
            throw new ServiceException("保存历史库存详情失败");
        }

        Integer receiptContainerHeaderId = receiptContainerHeader.getId();
        AjaxResult ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderId, warehouseCode);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("生成任务失败");
        }
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeaderService.updateById(receiptHeader);

        return AjaxResult.success("生成退货入库任务成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult bufferStorage(String containerCode, String location) {

        boolean result = false;
        String port = null;
        String toLocationCode = null;
        String toPort = null;
        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeader = Wrappers.lambdaQuery();
        receiptContainerHeader.eq(ReceiptContainerHeader::getContainerCode, containerCode).ne(ReceiptContainerHeader::getStatus, 20);
        ReceiptContainerHeader receiptContainerHeaderServiceOne = receiptContainerHeaderService.getOne(receiptContainerHeader);
        if (receiptContainerHeaderServiceOne == null) {
            return AjaxResult.error("找不到对应的入库任务");
        }

        AjaxResult ajaxResult = wcsService.getFromPort();
        if (ajaxResult.hasErr()) {
            throw new ServiceException("获取站台点失败");
        }
        JSONArray array = (JSONArray) ajaxResult.getData();

        if (array.size() == 0) {
            throw new ServiceException("入库口暂时都不可以");
        }

        for (int i = 0; i < array.size(); i++) {
            toPort = (String) array.get(i);
            if (toPort.equals(QuantityConstant.PORT_P1005)) {
                port = QuantityConstant.PORT_P1004;
            }
            if (toPort.equals(QuantityConstant.PORT_P1001)) {
                port = QuantityConstant.PORT_P1000;
            }

            LambdaQueryWrapper<AgvTask> agvTaskLambdaQueryWrapper = Wrappers.lambdaQuery();
            agvTaskLambdaQueryWrapper.eq(AgvTask::getToPort, port).ne(AgvTask::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            List<AgvTask> list = agvTaskService.list(agvTaskLambdaQueryWrapper);

            toLocationCode = toPort;
            if (list.size() > 0) {
                toLocationCode = "";
                continue;
            } else {
                break;
            }
        }

        if (StringUtils.isEmpty(toLocationCode)) {
            throw new ServiceException("暂时还没有可用的空闲入库口");
        }
        receiptContainerHeaderServiceOne.setPort(toLocationCode);
        receiptContainerHeaderService.updateById(receiptContainerHeaderServiceOne);

        if (toLocationCode.equals(QuantityConstant.PORT_P1001) || toLocationCode.equals(QuantityConstant.PORT_P1005)) {
            ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderServiceOne.getId(), QuantityConstant.DEFAULT_WAREHOUSE);
            if (ajaxResult.hasErr()) {
                throw new ServiceException("生成任务失败");
            }

            AjaxResult taskFromTo = agvTaskService.sendTaskFromTo(QuantityConstant.DEFAULT_WAREHOUSE, QuantityConstant.ZONE_LK, containerCode, location, port,
                    QuantityConstant.PLATFORM_CODING, 0);
            if (taskFromTo.hasErr()) {
                throw new ServiceException("生成agv任务失败" + ajaxResult.getMsg());
            }
        }

        return AjaxResult.success().setData("任务生成成功");
    }

    @Override
    public AjaxResult abnormalFeedback(String containerCode) {
        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerHeaderLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).eq(ReceiptContainerHeader::getStatus,
                QuantityConstant.CHECK_STATUS_BUILD);
        ReceiptContainerHeader receiptContainerHeaderServiceOne = receiptContainerHeaderService.getOne(receiptContainerHeaderLambdaQueryWrapper);
        if (receiptContainerHeaderServiceOne == null) {
            Container containerByCode = containerService.getContainerByCode(containerCode);
            if (containerByCode.getLocationCode().contains("B")) {
                return AjaxResult.success("没有找到组盘 但找到了对应绑定关系 应该是整盘出库到缓存区的,请用缓存区释放功能,不要用重新入库功能");
            }
            return AjaxResult.error("没有找到任务以及对应的关系");
        }
        if (receiptContainerHeaderServiceOne.getExceptionCode().isEmpty()){
            receiptContainerHeaderServiceOne.setExceptionCode("异常信息记录失败 请根据日志时间排查下相关问题");
        }
        return AjaxResult.success("当前托盘是因为" + receiptContainerHeaderServiceOne.getExceptionCode() + ",放到入库缓存区的");
    }

    @Override
    public AjaxResult complement(String warehouseCode, String containerCode, String boxCode) {
        long shipmentOrder = System.currentTimeMillis();
        boxCode = boxCode.trim();
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode).orderByDesc(InventoryDetail::getBoxCode);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetails.size() < 1) {
            return AjaxResult.error("该托盘下面没有库存了");
        }
        InventoryDetail inventoryDetail = inventoryDetails.get(0);
        LambdaQueryWrapper<ProPackaging> proPackagingLambdaQueryWrapper = Wrappers.lambdaQuery();
        proPackagingLambdaQueryWrapper.eq(ProPackaging::getNumber, inventoryDetail.getProPackaging());
        ProPackaging proPackaging = proPackagingService.getOne(proPackagingLambdaQueryWrapper);

        for (InventoryDetail inventory : inventoryDetails) {
            if (inventory.getBoxCode().equals(boxCode)) {
                return AjaxResult.error("当前输入的箱码,库存里面已经有了");
            }
        }
        inventoryDetails.removeIf(inventoryDetail1 -> inventoryDetail1.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK));
        inventoryDetails.removeIf(inventoryDetail1 -> inventoryDetail1.getStatus().equals(QuantityConstant.STATUS_CONTAINER_EMPTY));

        if (inventoryDetails.size() < 1) {
            return AjaxResult.error("该托盘下面没有库存了");
        }

        String BoxCode = "";

        for (int i = 0; i < proPackaging.getPieceBox(); i++) {
            String inventoryCode = inventoryDetails.get(i).getBoxCode();
            if (BoxCode == "") {
                BoxCode = inventoryCode;
            } else if (!BoxCode.equals(inventoryCode)) {
                throw new ServiceException("锁定片码出问题了,需要锁定的数量跟包装数量不一致,请人工维护");
            }
            inventoryDetails.get(i).setStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
            inventoryDetails.get(i).setShipmentOrder(shipmentOrder);
            inventoryDetailService.updateById(inventoryDetails.get(i));

            inventoryDetail.setId(null);
            inventoryDetail.setBoxCode(boxCode);
            inventoryDetail.setChipCode1("临时补充的虚拟片码");
            inventoryDetail.setShipmentOrder(shipmentOrder);
            inventoryDetail.setStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
            inventoryDetailService.save(inventoryDetail);
        }
        return AjaxResult.success("成功");
    }

    @Override
    public AjaxResult deleteReceiptDetails(String boxCode, String containerCode) {
        LambdaQueryWrapper<ReceiptContainerHeader> receipttContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receipttContainerHeaderLambdaQueryWrapper.le(ReceiptContainerHeader::getStatus,QuantityConstant.RECEIPT_CONTAINER_TASK).eq(ReceiptContainerHeader::getContainerCode, containerCode);
        ReceiptContainerHeader containerHeaderServiceOne = receiptContainerHeaderService.getOne(receipttContainerHeaderLambdaQueryWrapper);
        if (containerHeaderServiceOne == null) {
            return AjaxResult.error("没有对应的入库组盘订单");
        }

        LambdaQueryWrapper<ReceiptContainerDetail> receiptContainerDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerDetailLambdaQueryWrapper.eq(ReceiptContainerDetail::getContainerCode, containerCode).eq(ReceiptContainerDetail::getBoxCode, boxCode)
                .eq(ReceiptContainerDetail::getReceiptContainerId, containerHeaderServiceOne.getId());
        List<ReceiptContainerDetail> receiptContainerDetailList = chooseWrapper.receiptContainerDetailService(receiptContainerDetailLambdaQueryWrapper);

        if (receiptContainerDetailList.size() < 1) {
            return AjaxResult.error("没有对应详情信息");
        }

        for (ReceiptContainerDetail receiptContainerDetail : receiptContainerDetailList) {
            if (!chooseWrapper.receiptContainerDetailServiceRemoveById(receiptContainerDetail.getId())) {
                throw new ServiceException("删除入库组盘失败");
            }
            ReceiptDetail detail = receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId());
            detail.setQty(detail.getQty().subtract(BigDecimal.valueOf(1)));
            if (!receiptDetailService.updateById(detail)){
                throw new ServiceException("入库单明细更新失败");
            }
        }
        ReceiptHeader header = receiptHeaderService.getById(receiptContainerDetailList.get(0).getReceiptId());
        header.setTotalQty(header.getTotalQty().subtract(BigDecimal.valueOf(receiptContainerDetailList.size())));
        receiptHeaderService.updateById(header);
        return AjaxResult.success("删除成功");
    }


    @Override
    public AjaxResult supplementaryWarehousingOrder(String boxCode, String containerCode, String chipBarcode1) {
        LambdaQueryWrapper<ReceiptContainerHeader> receipttContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receipttContainerHeaderLambdaQueryWrapper.le(ReceiptContainerHeader::getStatus,QuantityConstant.RECEIPT_CONTAINER_TASK).eq(ReceiptContainerHeader::getContainerCode, containerCode);
        ReceiptContainerHeader containerHeaderServiceOne = receiptContainerHeaderService.getOne(receipttContainerHeaderLambdaQueryWrapper);
        if (containerHeaderServiceOne == null) {
            return AjaxResult.error("没有对应的入库组盘订单");
        }

        LambdaQueryWrapper<ReceiptContainerDetail> receiptContainerDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerDetailLambdaQueryWrapper.eq(ReceiptContainerDetail::getContainerCode, containerCode).eq(ReceiptContainerDetail::getReceiptContainerId,
        containerHeaderServiceOne.getId());
        List<ReceiptContainerDetail> receiptContainerDetailList = chooseWrapper.receiptContainerDetailService(receiptContainerDetailLambdaQueryWrapper);

        ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailList.get(0);

        LambdaQueryWrapper<ProPackaging> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.eq(ProPackaging::getNumber, receiptContainerDetail.getProPackaging());
        ProPackaging proPackaging = proPackagingService.getOne(queryWrapper2);

        int j = proPackaging.getPieceBox();
        int i = 0;
        for (ReceiptContainerDetail receiptContainer : receiptContainerDetailList) {
            if (receiptContainer.getChipCode1().equals(chipBarcode1)) {
                return AjaxResult.error("片码已存在");
            }
            if (receiptContainer.getBoxCode().equals(boxCode)) {
                i = i + 1;
                if (i >= j) {
                    return AjaxResult.error("箱码已经到达最大值,不能再加了");
                }

            }
        }

        receiptContainerDetail.setBoxCode(boxCode);
        receiptContainerDetail.setChipCode1(chipBarcode1);
        chooseWrapper.receiptContainerDetailServiceTool(-1).save(receiptContainerDetail);

        ReceiptDetail detail = receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId());
        detail.setQty(detail.getQty().add(BigDecimal.valueOf(1)));
        if (!receiptDetailService.updateById(detail)){
            throw new ServiceException("入库单明细更新失败");
        }
        ReceiptHeader header = receiptHeaderService.getById(receiptContainerDetailList.get(0).getReceiptId());
        header.setTotalQty(header.getTotalQty().add(BigDecimal.valueOf(1)));
        receiptHeaderService.updateById(header);

        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult SalesReceiptDoc(String boxCode, String totalQty) {

        LambdaQueryWrapper<InventoryHistoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryHistoryDetail::getBoxCode, boxCode);
        List<InventoryHistoryDetail> InventoryHistoryDetails = inventoryHistoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (InventoryHistoryDetails.size() < 1) {
            return AjaxResult.error("箱码不存在");
        }
        InventoryHistoryDetail inventoryHistoryDetail = InventoryHistoryDetails.get(0);

        InventoryHistoryHeader headerServiceById = inventoryHistoryHeaderService.getById(inventoryHistoryDetail.getInventoryHistoryHeaderId());

        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setCompanyCode("JY");
        receiptHeader.setSaleOrder(0);
        receiptHeader.setReceiptType("RN");
        AjaxResult ajaxResult = receiptHeaderService.saveReceiptHeader(receiptHeader);
        if (ajaxResult.getCode() != 200) {
            return AjaxResult.error(ajaxResult.getMsg());
        }

        ReceiptDetail receiptDetail = new ReceiptDetail();
        receiptDetail.setReceiptId(receiptHeader.getId());
        receiptDetail.setReceiptCode(receiptHeader.getCode());
        receiptDetail.setCompanyCode(receiptHeader.getCompanyCode());
        receiptDetail.setMaterialCode(inventoryHistoryDetail.getMaterialCode());
        receiptDetail.setBatch(inventoryHistoryDetail.getBatch());
        receiptDetail.setQcCheck("1");
        receiptDetail.setStatus(0);
        receiptDetail.setProcessStamp("200");
        receiptDetail.setQty(new BigDecimal(totalQty));
        receiptDetail.setInventorySts(inventoryHistoryDetail.getInventorySts());
        receiptDetail.setProPackaging(inventoryHistoryDetail.getProPackaing());
        receiptDetail.setProductSchedule(inventoryHistoryDetail.getProductSchedule());
        receiptDetail.setLevel(headerServiceById.getLevel());
        receiptDetail.setMaterialName(headerServiceById.getMaterialName());
        receiptDetail.setMaterialSpec(headerServiceById.getMaterialSpec());
        receiptDetail.setMaterialUnit(headerServiceById.getMaterialUnit());
        receiptDetail.setColor(inventoryHistoryDetail.getColor());
        receiptDetail.setBackQty(BigDecimal.ZERO);
        receiptDetail.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        receiptDetailService.save(receiptDetail);
        receiptHeader.setOrderCode(receiptHeader.getId() + "");
        receiptHeader.setTotalQty(new BigDecimal(totalQty));
        receiptHeader.setTotalLines(1);
        receiptHeaderService.updateById(receiptHeader);

        return AjaxResult.success("成功");
    }

    @Override
    public AjaxResult quickIssueTask(String port, String type, String material, String qty) {

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getMaterialCode, material);
        List<InventoryHeader> inventoryHeaders = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeaders.size() < 1) {
            return AjaxResult.error("物料没有对应的库存");
        }
        BigDecimal sum = new BigDecimal(0);

        for (InventoryHeader inventoryHeaderI : inventoryHeaders) {
            sum = sum.add(inventoryHeaderI.getTotalQty());
        }
        if (sum.compareTo(new BigDecimal(qty)) == -1) {
            return AjaxResult.error("库存数量不够");
        }

        InventoryHeader inventoryHeader = inventoryHeaders.get(0);

        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setCompanyCode("JY");
        shipmentHeader.setShipmentType("DN");
        shipmentHeader.setWarehouseCode("CS0001");
        String code = shipmentHeaderService.createCode((shipmentHeader.getShipmentType()));
        shipmentHeader.setId(null);
        shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        shipmentHeader.setLastUpdated(null);
        shipmentHeader.setLastUpdatedBy("调试专用pda");
        shipmentHeader.setCreated(null);
        shipmentHeader.setTotalQty(new BigDecimal(qty));
        shipmentHeader.setLastStatus(120);
        shipmentHeader.setFirstStatus(120);
        shipmentHeader.setPlatformCode("PG0001");
        shipmentHeader.setLastQty(1);
        shipmentHeader.setCreatedBy("调试专用pda");
        shipmentHeader.setCode(code);
        shipmentHeaderService.save(shipmentHeader);

        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail.setShipmentId(shipmentHeader.getId());
        shipmentDetail.setCompanyCode("JY");
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        shipmentDetail.setMaterialCode(inventoryHeader.getMaterialCode());
        shipmentDetail.setMaterialName(inventoryHeader.getMaterialName());
        shipmentDetail.setMaterialSpec(inventoryHeader.getMaterialSpec());
        shipmentDetail.setQty(new BigDecimal(qty));
        shipmentDetail.setBatch(inventoryHeader.getBatch());
        shipmentDetail.setInventorySts("good");
        shipmentDetail.setWarehouseCode("CS0001");
        shipmentDetail.setProPackaging(inventoryHeader.getProPackaging());
        shipmentDetail.setProductSchedule(inventoryHeader.getProductSchedule());
        shipmentDetail.setLevel(inventoryHeader.getLevel());
        shipmentDetail.setColor(inventoryHeader.getColor());
        shipmentDetailService.save(shipmentDetail);

        return AjaxResult.success("完成");
    }

    @Override
    public AjaxResult platformMonitoring() {

        int P1 = 0;
        String P3057 = " P3057还没到达";
        String P3058 = " P3058还没到达";
        String L3057 = " P3057无法获取数量";
        String L3058 = " P3058无法获取数量";

        int P11 = 0;
        String P4019 = " P4019还没到达";
        String P4015 = " P4015还没到达";
        String L4015 = " P4015无法获取数量";
        String L4019 = " P4019无法获取数量";

        int P2 = 0;
        String P3055 = " P3055还没到达";
        String P3056 = " P3056还没到达";
        String L3055 = " P3055无法获取数量";
        String L3056 = " P3056无法获取数量";

        int P12 = 0;
        String P4027 = " P4027还没到达";
        String P4023 = " P4023还没到达";
        String L4023 = " P4023无法获取数量";
        String L4027 = " P4027无法获取数量";

        int P3 = 0;
        String P3053 = " P3053还没到达";
        String P3054 = " P3054还没到达";
        String L3053 = " P3053无法获取数量";
        String L3054 = " P3054无法获取数量";

        int P13 = 0;
        String P4035 = " P4035还没到达";
        String P4031 = " P4031还没到达";
        String L4031 = " P4031无法获取数量";
        String L4035 = " P4035无法获取数量";

        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.ne(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT).or().ne(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_OVER_STATION);

        List<TaskHeader> taskHeaders = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
        for (TaskHeader taskHeader : taskHeaders) {
            if (taskHeader.getPortGroup().equals("P1")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P3057")) {
                        P3057 = " P3057已经到达 托盘号" + taskHeader.getContainerCode();
                        L3057 = "L3057应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P3058")) {
                        P3058 = " P3058已经到达 托盘号" + taskHeader.getContainerCode();
                        L3058 = "L3058应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";

                    }
                }
                P1 = P1 + 1;
            }

            if (taskHeader.getPortGroup().equals("P11")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P4019")) {
                        P4019 = " P4019已经到达 托盘号" + taskHeader.getContainerCode();
                        L4019 = "P4019应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P4015")) {
                        P4015 = " P4015已经到达 托盘号" + taskHeader.getContainerCode();
                        L4015 = "P4019应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }
                }
                P11 = P11 + 1;
            }

            if (taskHeader.getPortGroup().equals("P12")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P4027")) {
                        P4027 = " P4027已经到达 托盘号" + taskHeader.getContainerCode();
                        L4027 = "P4027应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P4023")) {
                        P4023 = " P4023已经到达 托盘号" + taskHeader.getContainerCode();
                        L4023 = "P4023应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }
                }
                P12 = P12 + 1;
            }

            if (taskHeader.getPortGroup().equals("P13")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P4035")) {
                        P4035 = " 4035已经到达 托盘号" + taskHeader.getContainerCode();
                        L4035 = "P4035应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P4031")) {
                        P4031 = " P4031已经到达 托盘号" + taskHeader.getContainerCode();
                        L4031 = "P4031应该组" + taskHeader.getQty() + "已经组了" + taskHeader.getTaskQty() + "箱";

                    }

                }
                P13 = P13 + 1;
            }

            if (taskHeader.getPortGroup().equals("P2")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P3055")) {
                        P3055 = " P3055已经到达 托盘号" + taskHeader.getContainerCode();
                        L3055 = "L3055应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P3056")) {
                        P3056 = " P3056已经到达 托盘号" + taskHeader.getContainerCode();
                        L3056 = "L3056应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";

                    }
                }
                P2 = P2 + 1;
            }
            if (taskHeader.getPortGroup().equals("P3")) {
                if (taskHeader.getPort() != null) {
                    if (taskHeader.getPort().equals("P3053")) {
                        P3053 = " P3053已经到达 托盘号" + taskHeader.getContainerCode();
                        L3053 = "L3053应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";

                    }
                    if (taskHeader.getPort().equals("P3054")) {
                        P3054 = " P3054已经到达 托盘号" + taskHeader.getContainerCode();
                        L3054 = "L3054应该拆垛" + taskHeader.getQty() + "已经拆了" + taskHeader.getTaskQty() + "箱";
                    }
                }
                P3 = P3 + 1;
            }

        }

        String result = "P1有" + P1 + "个拣选任务" + "\n" + P3057 + "\n" + P3058 + "\n" + L3057 + "\n" + L3058 + "\n" + "P11有" + P11 + "个拣选任务" + "\n" + P4019 + "\n"
                + P4015 + "\n" + L4019 + "\n" + L4015 + "\n\n" + "P2有" + P2 + "个拣选任务" + "\n" + P3055 + "\n" + P3056 + "\n" + L3055 + "\n" + L3056 + "\n" + "P12有" + P12
                + "个拣选任务" + "\n" + P4027 + "\n" + P4023 + "\n" + L4027 + "\n" + L4023 + "\n\n" + "P3有" + P3 + "个拣选任务" + "\n" + P3053 + "\n" + P3054 + "\n" + L3053 + "\n"
                + L3054 + "\n" + "P13有" + P13 + "个拣选任务" + "\n" + P4035 + "\n" + P4031 + "\n" + L4035 + "\n" + L4031;

//        System.out.println("P1有"+P1+"个拣选任务,"+P3057+P3058+"\n"+L3057+"\n"+L3058);
//
//        System.out.println("P11有"+P11+"个拣选任务,"+P4019+P4015+"\n"+L4019+"\n"+L4015);
//        System.out.println();
//        System.out.println();
//        System.out.println();
//
//        System.out.println("P2有"+P2+"个拣选任务,"+P3055+P3056+"\n"+L3055+"\n"+L3056);
//
//        System.out.println("P12有"+P12+"个拣选任务,"+P4027+P4023+"\n"+L4027+"\n"+L4023);
//        System.out.println();
//        System.out.println();
//        System.out.println();
//
//        System.out.println("P3有"+P3+"个拣选任务,"+P3053+P3054+"\n"+L3053+"\n"+L3054);
//
//        System.out.println("P13有"+P13+"个拣选任务,"+P4035+P4031+"\n"+L4035+"\n"+L4031);

        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult supplementaryWarehousing(String containerCode, String receiptCode, List<BoxInfo> listBoxInfo, String orderCode, String orderName,
                                               String receiptDetailId) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("根据托盘号,没有找到托盘信息:" + containerCode);
        }
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
        if (receiptHeader == null) {
            return AjaxResult.error("没有找到入库单");
        }

        LambdaQueryWrapper<ReceiptContainerHeader> receiptHeaderControllerLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderControllerLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).lt(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.list(receiptHeaderControllerLambdaQueryWrapper);
        if (receiptContainerHeaders.size() > 0) {
            return AjaxResult.error("当前托盘已存在任务了");
        }

        BoxInfo boxInfo = listBoxInfo.get(0);
        String materialCode = boxInfo.getMaterialCode();
        String color = boxInfo.getColor();
        String level = boxInfo.getLevel();
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode).eq(ReceiptDetail::getMaterialCode, materialCode).eq(ReceiptDetail::getId,
                receiptDetailId);
        ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailLambdaQueryWrapper);
        if (receiptDetail == null) {
            return AjaxResult.error("没有匹配到入库单详情");
        }
        getBosAssistantNumber(color, "colour");
        getBosAssistantNumber(level, "level");
        BigDecimal total = BigDecimal.ZERO;
        String materialCode1 = null, batch1 = null, level1 = null, color1 = null, proPackage1 = null, productSize1 = null;
        for (BoxInfo boxInfo1 : listBoxInfo) {
            materialCode1 = boxInfo1.getMaterialCode();
            color1 = boxInfo1.getColor();
            level1 = boxInfo1.getLevel();
            batch1 = boxInfo1.getBatch();
            proPackage1 = boxInfo1.getProPackaging();
            productSize1 = boxInfo1.getSize();
            if (!materialCode.equals(materialCode1)) {
                return AjaxResult.error("物料信息不匹配");
            }
            if (!boxInfo1.getBatch().equals(receiptDetail.getBatch())) {
                return AjaxResult.error("实际批次" + boxInfo1.getBatch() + "与订单批次" + receiptDetail.getBatch() + "不匹配。" + "箱码" + boxInfo1.getBoxCode());
            }
            if (!color.equals(color1)) {
                return AjaxResult.error("色号信息不匹配");
            }
            if (!level.equals(level1)) {
                return AjaxResult.error("等级信息不匹配");
            }
            BigDecimal qty = boxInfo1.getQty();
            total = total.add(qty);
        }
        BigDecimal taskQty = receiptDetail.getTaskQty();
        BigDecimal qty = receiptDetail.getQty();
        taskQty = taskQty.add(total);
        if (taskQty.compareTo(qty) > 0) {
            return AjaxResult.error("入库数量不能大于单据数量");
        }
        receiptDetail.setTaskQty(taskQty);
        boolean result = receiptDetailService.updateById(receiptDetail);
        if (!result) {
            throw new RuntimeException("更新入库单详情失败");
        }
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(warehouseCode);
        receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
        receiptContainerHeader.setContainerCode(containerCode);
        receiptContainerHeader.setBatch(batch1);
        receiptContainerHeader.setLevel(level1);
        receiptContainerHeader.setColor(color1);
        receiptContainerHeader.setProPackaging(proPackage1);
        receiptContainerHeader.setProductSchedule(receiptDetail.getProductSchedule());
        receiptContainerHeader.setOrderCode(orderCode);// 订单编号
        ProductScheduleHeader productScheduleHeader =
                productScheduleHeaderService.getOne(new LambdaQueryWrapper<ProductScheduleHeader>().eq(ProductScheduleHeader::getNumber, orderCode));
        if (productScheduleHeader == null) {
            return AjaxResult.error("排产订单不存在,请先更新排产订单。");
        }
        receiptContainerHeader.setOrderName(productScheduleHeader.getName());// 订单名称
        receiptContainerHeader.setMaterialCode(materialCode1);// 物料编码
        receiptContainerHeader.setFromLocation(container.getLocationCode());
        receiptContainerHeader.setToLocation(container.getLocationCode());
        receiptContainerHeader.setContainerType(container.getContainerType());
//        receiptContainerHeader1.setContainerType(containerTypeCode);
        receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT);
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
//        receiptContainerHeader1.setStack(stack);//设置垛型
        receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_CODING);
        receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
        result = receiptContainerHeaderService.save(receiptContainerHeader);
        if (!result) {
            throw new ServiceException("保存入库组盘头失败");
        }

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        for (BoxInfo boxInfo2 : listBoxInfo) {
            String boxCode2 = boxInfo2.getBoxCode();
//            int boxPosition2 = boxInfo2.getBoxPosition();
            List<ChipInfos> chipInfoList2 = boxInfo2.getChipInfos();
            String level2 = boxInfo2.getLevel();
            String proPackagingStr2 = boxInfo2.getProPackaging();
            String productSize2 = boxInfo2.getSize();
            String color2 = boxInfo2.getColor();
            String batch2 = boxInfo2.getBatch();
            for (ChipInfos chipInfos : chipInfoList2) {
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                String chipCode1 = chipInfos.getChipBarcode1();
                String chipCode2 = chipInfos.getChipBarcode2();
                receiptContainerDetail.setWarehouseCode(warehouseCode);
                receiptContainerDetail.setContainerCode(containerCode);
                receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                receiptContainerDetail.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
                receiptContainerDetail.setChipCode1(chipCode1);
                receiptContainerDetail.setChipCode2(chipCode2);
                receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
                receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
                receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                receiptContainerDetail.setProPackaging(proPackagingStr2);// 包装
                receiptContainerDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                receiptContainerDetail.setProductSize(productSize2);// 产品规格
                receiptContainerDetail.setLevel(level2);// 等级
                receiptContainerDetail.setColor(color2);// 色号
                receiptContainerDetail.setQty(BigDecimal.ONE);
                receiptContainerDetail.setContainerType(container.getContainerType());
                receiptContainerDetail.setLocationCode(container.getLocationCode());
                receiptContainerDetail.setReceiptType(receiptHeader.getReceiptType());
                receiptContainerDetail.setBatch(batch2);
                receiptContainerDetail.setReceiptCode(orderCode);
                receiptContainerDetail.setBoxCode(boxCode2);
                receiptContainerDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetailList.add(receiptContainerDetail);
            }
        }
        result = chooseWrapper.receiptContainerDetailServiceSaveBatch(receiptContainerDetailList);
        if (!result) {
            throw new ServiceException("保存入库组盘详情失败");
        }

        Integer receiptContainerHeaderId = receiptContainerHeader.getId();
        AjaxResult ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderId, warehouseCode);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("生成任务失败");
        }
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeaderService.updateById(receiptHeader);
        return AjaxResult.success("生成退货入库任务成功");
    }

    @Override
    public AjaxResult containerQuery(String materialCode, String level, String color, String proPackaging, String productSchedule, String batch, int boxQty) {
        String qty = configService.getKey(QuantityConstant.MAXIMIM_NUMBER_OF_SLICES);
        // 先暂时默认最大138片
        int qtyMax = Integer.valueOf(qty);
        InventoryHeader inventoryHeader = null;
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (level.equals(QuantityConstant.ACCEPTABLE_PRODUCT_LEVEL)) {
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getMaterialCode, materialCode).eq(InventoryHeader::getLevel, level)
                    .eq(InventoryHeader::getColor, color).eq(InventoryHeader::getProPackaging, proPackaging).le(InventoryHeader::getTotalQty, qtyMax - boxQty)
                    .last("LIMIT 1");
            inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        } else {
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getMaterialCode, materialCode).eq(InventoryHeader::getLevel, level)
                    .eq(InventoryHeader::getColor, color).eq(InventoryHeader::getProPackaging, proPackaging).eq(InventoryHeader::getProductSchedule, productSchedule)
                    .eq(InventoryHeader::getBatch, batch).le(InventoryHeader::getTotalQty, qtyMax - boxQty).last("LIMIT 1");
            inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        }
        if (inventoryHeader == null) {
            return AjaxResult.error("没有找到可用补盘");
        }
        return AjaxResult.success(inventoryHeader.getContainerCode());
    }

    @Override
    @Transactional
    public AjaxResult compositePatching(String boxCode, String containerCode) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getBoxCode, boxCode).eq(InventoryDetail::getZoneCode, QuantityConstant.ZONE_LK);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);

        if (inventoryDetails.size() < 1) {
            return AjaxResult.error("没有这个库存明细,请检查是不是前端给错了入库");
        }

        if (inventoryDetails.get(0).getContainerCode().equals(containerCode)) {
            return AjaxResult.error("箱码跟托盘码本来就是在一个盘,无需组盘");
        }

        // 这个是原箱码所在的库存主表
        InventoryHeader inventoryHeader = inventoryHeaderService.getById(inventoryDetails.get(0).getInventoryHeaderId());

        if (inventoryHeader == null) {
            return AjaxResult.error("不存在原箱码数据,请检查数据,看看是不是扫错了或者前端给的码不对");
        }
        LambdaQueryWrapper<ProPackaging> proPackagingLambdaQueryWrapper = Wrappers.lambdaQuery();
        proPackagingLambdaQueryWrapper.eq(ProPackaging::getNumber, inventoryDetails.get(0).getProPackaging());
        ProPackaging proPackaging = proPackagingService.getOne(proPackagingLambdaQueryWrapper);
        int pieceBox = proPackaging.getPieceBox();

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, containerCode).eq(InventoryHeader::getZoneCode, QuantityConstant.ZONE_LK);

        // 这个是要补充进去的库存
        InventoryHeader inventoryHeader1 = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);

        if (inventoryHeader1 == null) {
            return AjaxResult.error("不存待补充托盘数据,请检查数据,看看是不是扫错了或者前端给的码不对");
        }

        if (!inventoryHeader.getLevel().equals(inventoryHeader1.getLevel())  || !inventoryHeader.getColor() .equals(inventoryHeader1.getColor()) || !inventoryHeader.getMaterialCode().equals(inventoryHeader1.getMaterialCode())
                || !inventoryHeader.getProPackaging() .equals(inventoryHeader1.getProPackaging()) || !inventoryHeader.getProductSchedule() .equals(inventoryHeader1.getProductSchedule())
                || !inventoryHeader.getBatch() .equals( inventoryHeader1.getBatch())) {
            return AjaxResult.error("两种物料不一致");
        }

        for (InventoryDetail inventoryDetail : inventoryDetails) {
            inventoryDetail.setContainerCode(containerCode);
            inventoryDetail.setInventoryHeaderId(inventoryHeader1.getId());
            inventoryDetailService.updateById(inventoryDetail);
        }

        inventoryHeader.setTotalQty(inventoryHeader.getTotalQty().subtract(BigDecimal.valueOf(inventoryDetails.size())));
        inventoryHeader.setBoxQty(inventoryHeader.getTotalQty().divide(BigDecimal.valueOf(pieceBox)));
        inventoryHeaderService.updateById(inventoryHeader);

        inventoryHeader1.setTotalQty(inventoryHeader1.getTotalQty().add(BigDecimal.valueOf(inventoryDetails.size())));
        inventoryHeader1.setBoxQty(inventoryHeader1.getTotalQty().divide(BigDecimal.valueOf(pieceBox)));
        inventoryHeaderService.updateById(inventoryHeader1);


        inventoryDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getInventoryHeaderId, inventoryHeader.getId())
                .eq(InventoryDetail::getZoneCode, QuantityConstant.ZONE_LK).ne(InventoryDetail::getStatus, QuantityConstant.STATUS_LOCATION_LOCK);
        inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetails.size() < 1) {
            inventoryHeaderService.removeById(inventoryHeader.getId());
        }

        return AjaxResult.success("转移成功");
    }

    @Override
    @Transactional
    public AjaxResult qualityInspectionReceipt(String containerCode, String receiptCode, List<BoxInfo> listBoxInfo, String orderCode, String receiptDetailId) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("根据托盘号,没有找到托盘信息:" + containerCode);
        }
        String locationCode = container.getLocationCode();
        if (StringUtils.isNotEmpty(locationCode)) {
            return AjaxResult.error("托盘在库位上:" + locationCode);
        }

        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
        if (receiptHeader == null) {
            return AjaxResult.error("没有找到入库单");
        }

        LambdaQueryWrapper<ReceiptContainerHeader> receiptHeaderControllerLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderControllerLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).lt(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.list(receiptHeaderControllerLambdaQueryWrapper);
        if (receiptContainerHeaders.size() > 0) {
            return AjaxResult.error("当前托盘已存在任务了");
        }

        BoxInfo boxInfo = listBoxInfo.get(0);
        String materialCode = boxInfo.getMaterialCode();
        String color = boxInfo.getColor();
        String level = boxInfo.getLevel();
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode).eq(ReceiptDetail::getMaterialCode, materialCode).eq(ReceiptDetail::getId,
                receiptDetailId);

        ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailLambdaQueryWrapper);
        if (receiptDetail == null) {
            return AjaxResult.error("没有匹配到入库单详情");
        }
        getBosAssistantNumber(color, "colour");
        getBosAssistantNumber(level, "level");
        BigDecimal total = BigDecimal.ZERO;
        String materialCode1 = null, batch1 = null, level1 = null, color1 = null, proPackage1 = null, productSize1 = null;
        for (BoxInfo boxInfo1 : listBoxInfo) {
            materialCode1 = boxInfo1.getMaterialCode();
            color1 = boxInfo1.getColor();
            level1 = boxInfo1.getLevel();
            batch1 = boxInfo1.getBatch();
            proPackage1 = boxInfo1.getProPackaging();
            productSize1 = boxInfo1.getSize();

            if (!boxInfo1.getBatch().equals(receiptDetail.getBatch())) {
                return AjaxResult.error("实际批次" + boxInfo1.getBatch() + "与订单批次" + receiptDetail.getBatch() + "不匹配。" + "箱码" + boxInfo1.getBoxCode());
            }

            if (!materialCode.equals(materialCode1)) {
                return AjaxResult.error("物料信息不匹配");
            }
            if (!color.equals(color1)) {
                return AjaxResult.error("色号信息不匹配");
            }
            if (!level.equals(level1)) {
                return AjaxResult.error("等级信息不匹配");
            }
            BigDecimal qty = boxInfo1.getQty();
            total = total.add(qty);
        }
        BigDecimal taskQty = receiptDetail.getTaskQty();
        BigDecimal qty = receiptDetail.getQty();
        taskQty = taskQty.add(total);
        if (taskQty.compareTo(qty) > 0) {
            return AjaxResult.error("入库数量不能大于单据数量");
        }
        receiptDetail.setTaskQty(taskQty);
        boolean result = receiptDetailService.updateById(receiptDetail);
        if (!result) {
            throw new RuntimeException("更新入库单详情失败");
        }
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(warehouseCode);
        receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
        receiptContainerHeader.setContainerCode(containerCode);
        receiptContainerHeader.setBatch(batch1);
        receiptContainerHeader.setLevel(level1);
        receiptContainerHeader.setColor(color1);
        receiptContainerHeader.setProPackaging(proPackage1);
        receiptContainerHeader.setProductSchedule(receiptDetail.getProductSchedule());
        receiptContainerHeader.setOrderCode(receiptCode);// 订单编号
        ProductScheduleHeader productScheduleHeader = productScheduleHeaderService
                .getOne(new LambdaQueryWrapper<ProductScheduleHeader>().eq(ProductScheduleHeader::getNumber, receiptDetail.getProductSchedule()));
        if (productScheduleHeader == null) {
            throw new ServiceException("排产订单不存在,请先更新排产订单。");
        }
        receiptContainerHeader.setOrderName(productScheduleHeader.getName());// 订单名称
        receiptContainerHeader.setMaterialCode(materialCode1);// 物料编码
//        receiptContainerHeader1.setContainerType(containerTypeCode);
        receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
//        receiptContainerHeader1.setStack(stack);//设置垛型
        receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_CODING);
        receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
        result = receiptContainerHeaderService.save(receiptContainerHeader);
        if (!result) {
            throw new ServiceException("保存入库组盘头失败");
        }

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        for (BoxInfo boxInfo2 : listBoxInfo) {
            String boxCode2 = boxInfo2.getBoxCode();
//            int boxPosition2 = boxInfo2.getBoxPosition();
            List<ChipInfos> chipInfoList2 = boxInfo2.getChipInfos();
            String level2 = boxInfo2.getLevel();
            String proPackagingStr2 = boxInfo2.getProPackaging();
            String productSize2 = boxInfo2.getSize();
            String color2 = boxInfo2.getColor();
            String batch2 = boxInfo2.getBatch();
            for (ChipInfos chipInfos : chipInfoList2) {
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                String chipCode1 = chipInfos.getChipBarcode1();
                String chipCode2 = chipInfos.getChipBarcode2();
                receiptContainerDetail.setWarehouseCode(warehouseCode);
                receiptContainerDetail.setContainerCode(containerCode);
                receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                receiptContainerDetail.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
                receiptContainerDetail.setChipCode1(chipCode1);
                receiptContainerDetail.setChipCode2(chipCode2);
                receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
                receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
                receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                receiptContainerDetail.setProPackaging(proPackagingStr2);// 包装
                receiptContainerDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                receiptContainerDetail.setProductSize(productSize2);// 产品规格
                receiptContainerDetail.setLevel(level2);// 等级
                receiptContainerDetail.setColor(color2);// 色号
                receiptContainerDetail.setQty(BigDecimal.ONE);
                receiptContainerDetail.setBatch(batch2);
                receiptContainerDetail.setReceiptCode(receiptCode);
                receiptContainerDetail.setBoxCode(boxCode2);
                receiptContainerDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetailList.add(receiptContainerDetail);
            }
        }
        result = chooseWrapper.receiptContainerDetailServiceSaveBatch(receiptContainerDetailList);
        if (!result) {
            throw new ServiceException("保存入库组盘详情失败");
        }

        Integer receiptContainerHeaderId = receiptContainerHeader.getId();
        AjaxResult ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderId, warehouseCode);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("生成任务失败");
        }
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeaderService.updateById(receiptHeader);

        return AjaxResult.success("生成退货入库任务成功");
    }

    @Override
    public AjaxResult queryContainer(String boxCode) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper= Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getBoxCode, boxCode).last("LIMIT 1");
        InventoryDetail inventoryDetail = inventoryDetailService.getOne(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetail!=null){
            return  AjaxResult.success().setData("库存有 在库区:"+inventoryDetail.getZoneCode()+"\n托盘号:"+inventoryDetail.getContainerCode());
        }

        LambdaQueryWrapper<ReceiptContainerDetail> receiptContainerDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerDetailLambdaQueryWrapper.eq(ReceiptContainerDetail::getBoxCode, boxCode)
                .eq(ReceiptContainerDetail::getReceiptType, QuantityConstant.RECEIPT_BILL_TYPE_SC)
                .like(ReceiptContainerDetail::getContainerCode,"LS").orderByDesc(ReceiptContainerDetail::getId).last("LIMIT 1");
        ReceiptContainerDetail receiptContainerDetail = (ReceiptContainerDetail) chooseWrapper.receiptContainerDetailServiceGetOne(receiptContainerDetailLambdaQueryWrapper);
        return AjaxResult.success().setData("无库存 容器号:\n"+receiptContainerDetail.getContainerCode());
    }

    @Override
    public AjaxResult binding(String model, String locationCode) {
        String zoneCode = QuantityConstant.ZONE_WOOD_CONTAINER_CACHE;
        Location location = locationService.getLocationByCode(locationCode);
        if (location == null) {
            AjaxResult.error("库位不存在");
        }
        String containerCode = containerService.createContainer(location.getArea(), zoneCode);
        Container container = containerService.getContainerByCode(containerCode);
        location.setContainerCode(containerCode);

        locationService.updateById(location);
        container.setLocationCode(locationCode);
        container.setContainerSpec(Integer.valueOf(model));
        containerService.updateById(container);
        return AjaxResult.success("绑定成功");
    }

    @Override
    public AjaxResult shipmentDetails(String boxCode) {

        // 1.默认是在平库的才可以查 从平库查到这个箱码
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getZoneCode, QuantityConstant.ZONE_STOCK_UP).eq(InventoryDetail::getBoxCode, boxCode).last(" limit 1");
        InventoryDetail inventoryDetail = inventoryDetailService.getOne(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetail == null) {
            return AjaxResult.error("箱码不存在");
        }

        // 2.根据托盘任务查到出库单
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getContainerCode, inventoryDetail.getContainerCode())
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_OVER_STATION).ne(TaskHeader::getPortGroup, QuantityConstant.PORTGROUP_P30);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader == null) {
            return AjaxResult.error("找不到对应的木托盘任务");
        }

        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper.eq(ShipmentHeader::getCode, taskHeader.getShipmentCode());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper);

        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId())
                .eq(ShipmentDetail::getMaterialCode, inventoryDetail.getMaterialCode()).eq(ShipmentDetail::getColor, inventoryDetail.getColor())
                .eq(ShipmentDetail::getLevel, inventoryDetail.getLevel()).eq(ShipmentDetail::getProductSchedule, inventoryDetail.getProductSchedule())
                .eq(ShipmentDetail::getProPackaging, inventoryDetail.getProPackaging());
        ShipmentDetail shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);

        Document document = new Document();
        document.setShipmentCode(shipmentHeader.getCode());
        document.setReferCode(shipmentHeader.getReferCode());
        document.setCustomerCode(shipmentHeader.getCustomerCode());
        document.setCustomerName(shipmentHeader.getCustomerName());
        document.setMaterialCode(inventoryDetail.getMaterialCode());
        document.setMaterialSpec(inventoryDetail.getMaterialSpec());
        document.setQty(shipmentDetail.getQty());
        document.setBatch(inventoryDetail.getBatch());
        document.setLevel(inventoryDetail.getLevel());
        document.setProPackaging(inventoryDetail.getProPackaging());
        document.setColor(inventoryDetail.getColor());
        document.setProductSchedule(inventoryDetail.getProductSchedule());
        document.setLocation(inventoryDetail.getLocationCode());

        return AjaxResult.success(document);
    }

    @Override
    public AjaxResult releaseInventory(String containerCode) {
        Container container = containerService.getContainerByCode(containerCode);
        if (StringUtils.isEmpty(container.getLocationCode())) {
            return AjaxResult.error("当前容器不存在库位,检查数据是不是乱了");
        }

        if (!container.getLocationCode().contains("B")) {
            return AjaxResult.error("当前容器托盘不在入库缓存区");
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getContainerCode, containerCode).eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
                .orderByDesc(TaskHeader::getId).last(" limit 1");
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader != null) {
            LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentHeaderLambdaQueryWrapper.eq(ShipmentHeader::getCode, taskHeader.getShipmentCode());
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.error("其他出库单不存在");
            }
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            if (!shipmentHeaderService.updateById(shipmentHeader)) {
                return AjaxResult.error("修改其他出库单失败");
            }
            locationService.updateContainerCodeAndStatus(container.getLocationCode(), "", QuantityConstant.STATUS_LOCATION_EMPTY);
            containerService.updateLocationCodeAndStatus(container.getCode(), "", QuantityConstant.STATUS_LOCATION_EMPTY);
            return AjaxResult.success("操作成功");
        } else {
            //这里查入库组盘里面的去取消组盘  理论上是不需要改订单数量 有定时器自动同步
            LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            receiptContainerHeaderLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                    .eq(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_HEADER_BUILD);
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(receiptContainerHeaderLambdaQueryWrapper);
            if (receiptContainerHeaderService.cancelByIds(Collections.singletonList(receiptContainerHeader.getId()))) {
                locationService.updateContainerCodeAndStatus(container.getLocationCode(), "", QuantityConstant.STATUS_LOCATION_EMPTY);
                containerService.updateLocationCodeAndStatus(container.getCode(), "", QuantityConstant.STATUS_LOCATION_EMPTY);
                return AjaxResult.success("取消成功");
            }
        }
        return null;
    }

    private String getBosAssistantNumber(String number1, String id) {
        LambdaQueryWrapper<BosAssistantDetail> bosAssistantDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        bosAssistantDetailLambdaQueryWrapper.eq(BosAssistantDetail::getId, id).eq(BosAssistantDetail::getNumber, number1);
        BosAssistantDetail bosAssistantDetail = bosAssistantDetailService.getOne(bosAssistantDetailLambdaQueryWrapper);
        if (bosAssistantDetail == null) {
            throw new ServiceException("没有找到对应详情");
        }
        String number = bosAssistantDetail.getNumber();
        return number;
    }

    private void updateSihpmnentHeaderStatus(ShipmentHeader shipmentHeader) {
        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
        int complete = 0;
        for (ShipmentDetail shipmentDetail : shipmentDetailList) {
            BigDecimal qty = shipmentDetail.getQty();
            BigDecimal taskQty = shipmentDetail.getTaskQty();
            if (qty.compareTo(taskQty) > 0) {
                complete = 1;
            }
        }
        if (complete == 0) {
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeaderService.updateById(shipmentHeader);
        } else {
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_GROUPDISK);
            shipmentHeaderService.updateById(shipmentHeader);
        }
    }

    private void updateInventoryHeader(InventoryHeader inventoryHeader) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getInventoryHeaderId, inventoryHeader.getId());
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            inventoryHeaderService.removeById(inventoryHeader.getId());
        } else {
            BigDecimal totalQty = BigDecimal.ZERO;
            int size = inventoryDetailList.size();
            List<BigDecimal> qtyList = inventoryDetailList.stream().map(InventoryDetail::getQty).collect(Collectors.toList());
            for (BigDecimal qty : qtyList) {
                totalQty = totalQty.add(qty);
            }
            inventoryHeader.setTotalQty(totalQty);
            inventoryHeader.setBoxQty(inventoryHeader.getBoxQty().subtract(BigDecimal.valueOf(1)));
            inventoryHeader.setTotalLines(size);
            inventoryHeaderService.updateById(inventoryHeader);
        }
    }


    private void addHistoryInventoryDetail(List<InventoryDetail> inventoryDetailList, InventoryHistoryHeader inventoryHistoryHeader, String userDef1) {
        if (CollectionUtils.isEmpty(inventoryDetailList)) {
            return;
        }

        List<InventoryHistoryDetail> historyDetails = new ArrayList<>();
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            InventoryHistoryDetail inventoryHistoryDetail = new InventoryHistoryDetail();
            inventoryHistoryDetail.setInventoryHistoryHeaderId(inventoryHistoryHeader.getId());
            inventoryHistoryDetail.setWarehouseCode(inventoryDetail.getWarehouseCode());
            inventoryHistoryDetail.setZoneCode(inventoryDetail.getZoneCode());
            inventoryHistoryDetail.setCompanyCode(inventoryDetail.getCompanyCode());
            inventoryHistoryDetail.setLocationCode(inventoryDetail.getLocationCode());
            inventoryHistoryDetail.setContainerCode(inventoryDetail.getContainerCode());
            inventoryHistoryDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            inventoryHistoryDetail.setMaterialName(inventoryDetail.getMaterialName());
            inventoryHistoryDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            inventoryHistoryDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            inventoryHistoryDetail.setBoxCode(inventoryDetail.getBoxCode());
            inventoryHistoryDetail.setBoxPosition(inventoryDetail.getBoxPosition());
            inventoryHistoryDetail.setChipCode1(inventoryDetail.getChipCode1());
            inventoryHistoryDetail.setChipCode2(inventoryDetail.getChipCode2());
            inventoryHistoryDetail.setColor(inventoryDetail.getColor());
            inventoryHistoryDetail.setLevel(inventoryDetail.getLevel());
            inventoryHistoryDetail.setProductSchedule(inventoryDetail.getProductSchedule());
            inventoryHistoryDetail.setProPackaing(inventoryDetail.getProPackaging());
            inventoryHistoryDetail.setProductSize(inventoryDetail.getProductSize());
            inventoryHistoryDetail.setQty(inventoryDetail.getQty());
            inventoryHistoryDetail.setTaskQty(inventoryDetail.getTaskQty());
            inventoryHistoryDetail.setInventorySts(inventoryDetail.getInventorySts());
            inventoryHistoryDetail.setReferCode(inventoryDetail.getReferCode());
            inventoryHistoryDetail.setReferDetailId(inventoryDetail.getReferDetailId());
            inventoryHistoryDetail.setBatch(inventoryDetail.getBatch());
            inventoryHistoryDetail.setLot(inventoryDetail.getLot());
            inventoryHistoryDetail.setProjectNo(inventoryDetail.getProjectNo());
            inventoryHistoryDetail.setManufactureDate(inventoryDetail.getManufactureDate());
            inventoryHistoryDetail.setExpirationDate(inventoryDetail.getExpirationDate());
            inventoryHistoryDetail.setAgingDate(inventoryDetail.getAgingDate());
            inventoryHistoryDetail.setAttribute1(inventoryDetail.getAttribute1());
            inventoryHistoryDetail.setAttribute2(inventoryDetail.getAttribute2());
            inventoryHistoryDetail.setAttribute3(inventoryDetail.getAttribute3());
            inventoryHistoryDetail.setCreatedBy(QuantityConstant.PLATFORM_WMS);
            inventoryHistoryDetail.setUserDef1(userDef1);
            inventoryHistoryDetail.setReceiptCode(inventoryDetail.getReceiptCode());
            inventoryHistoryDetail.setReceiptDetailId(inventoryDetail.getReceiptDetailId());

            historyDetails.add(inventoryHistoryDetail);
        }

        // **批量插入**
        int rowsInserted = inventoryHistoryDetailMapper.batchInsert(historyDetails);
        if (rowsInserted <= 0) {
            throw new ServiceException("批量插入库存历史明细失败");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createSPShipment(List<ShipmentHeader> shipmentHeaderList,String qty,boolean isSingle) {
        if (qty!=null&&Integer.parseInt(qty)<=0)
        {
            qty=null;
        }
        //shipmentHeaderList长度是1才能给qty
        if (StringUtils.isNotEmpty(qty)&&shipmentHeaderList.size()!=1){
            throw new ServiceException("生成销售单方法,传的参数数据异常");
        }
        for (ShipmentHeader shipmentHeader : shipmentHeaderList) {
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
            List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
            shipmentHeader.setId(null);
            String code = shipmentHeaderService.createCode(QuantityConstant.SHIPMENT_BILL_TYPE_SP);
            shipmentHeader.setCode(code);
            shipmentHeader.setRelaNoticeCode(shipmentHeader.getReferCode());
            shipmentHeader.setReferCode(null);
            if (StringUtils.isNotEmpty(qty)){
                shipmentHeader.setTotalQty(new BigDecimal(qty));
            }
            shipmentHeader.setReferCodeType(null);
            shipmentHeader.setReferId(null);
            shipmentHeader.setReferCodeType(shipmentHeader.getQtcklx());
            shipmentHeader.setCreated(null);
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_BILL_TYPE_SP);
            if (isSingle){
                shipmentHeader.setUserDef3("单托");
            }
            if (!shipmentHeaderService.save(shipmentHeader)) {
                throw new ServiceException("保存出库单失败");
            }
            for (ShipmentDetail shipmentDetail : shipmentDetailList) {
                shipmentDetail.setId(null);
                shipmentDetail.setShipmentId(shipmentHeader.getId());
                shipmentDetail.setShipmentCode(shipmentHeader.getCode());
                shipmentDetail.setReferCode(null);
                shipmentDetail.setCreated(null);
                shipmentDetail.setReferId(null);
                if (StringUtils.isNotEmpty(qty)){
                    shipmentDetail.setQty(new BigDecimal(qty));
                    shipmentDetail.setTaskQty(new BigDecimal(qty));
                }else {shipmentDetail.setTaskQty(shipmentDetail.getQty());}
                if (!shipmentDetailService.save(shipmentDetail)) {
                    throw new ServiceException("保存出库详情失败");
                }
            }
        }
    }

    @Override
    public AjaxResult orderStatus(String warehouseCode, String containerCode, String orderCode, String billType) {
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper.eq(StringUtils.isNotNull(orderCode), ShipmentHeader::getCode, orderCode).eq(StringUtils.isNotEmpty(billType),
                ShipmentHeader::getShipmentType, billType);
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper);

        List<Map<String, Object>> result = shipmentHeaders.stream().map(shipmentHeader -> {
            Map<String, Object> map = new HashMap<>();
            map.put("code", shipmentHeader.getCode());
            map.put("lastStatus", shipmentHeader.getLastStatus());
            return map;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(StringUtils.isNotEmpty(orderCode), ReceiptHeader::getCode, orderCode).eq(StringUtils.isNotEmpty(billType),
                ReceiptHeader::getReceiptType, billType);
        List<ReceiptHeader> receiptHeaders = receiptHeaderService.list(receiptHeaderLambdaQueryWrapper);
        List<Map<String, Object>> results = receiptHeaders.stream().map(receiptHeader -> {
            Map<String, Object> map = new HashMap<>();
            map.put("code", receiptHeader.getCode());
            map.put("lastStatus", receiptHeader.getLastStatus());
            return map;
        }).collect(Collectors.toList());
        result.addAll(results);

        return AjaxResult.success(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult bulkTransfer(String inventoryContainerCode, String column) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container inventoryContainer = containerService.getContainerByCode(inventoryContainerCode, warehouseCode);
        if (inventoryContainer == null) {
            return AjaxResult.error("没有找到容器数据:" + inventoryContainerCode);
        }
        String locationCode = inventoryContainer.getLocationCode();
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error("容器没有对应的库位数据:" + locationCode);
        }
        Location location = locationService.getLocationByCode(locationCode, warehouseCode);
        String shipmentCode = location.getShipmentCode();
        if (StringUtils.isEmpty(shipmentCode)) {
            return AjaxResult.error("库位" + locationCode + "没有找到对应的出库订单组");
        }
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper1.eq(ShipmentHeader::getCode, shipmentCode);
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper1);
        if (shipmentHeader1 == null) {
            return AjaxResult.error("当前出库单不存在 是不是被误删除了");
        }

        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getShipmentCode, shipmentCode).lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
        if (taskHeaderList.size() > 0) {
            List<Integer> taskIds = taskHeaderList.stream().map(TaskHeader::getId).collect(Collectors.toList());
            return AjaxResult.error("还有任务没有完成,任务号为:" + taskIds);
        }

        // 先查出不是P30的任务总数(第一阶段)
        taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getShipmentCode, shipmentCode).ne(TaskHeader::getPortGroup, QuantityConstant.PORTGROUP_P30)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_OVER_STATION);
        taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);

        List<String> collect = taskHeaderList.stream().map(TaskHeader::getContainerCode).collect(Collectors.toList());
        if (!collect.isEmpty()){
            LambdaQueryWrapper<AgvTask> agvTaskLambdaQueryWrapper = Wrappers.lambdaQuery();
            agvTaskLambdaQueryWrapper.in(AgvTask::getContainerCode, collect).ne(AgvTask::getStatus, QuantityConstant.AGV_TASK_STATUS_COMPLETED);
            List<AgvTask> taskList = agvTaskService.list(agvTaskLambdaQueryWrapper);
            if (taskList.size() > 0) {
                return AjaxResult.error("还有没完成的agv任务的,任务号为:" + collect);
            }
        }

        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getShipmentCode, shipmentCode).eq(Location::getZoneCode, QuantityConstant.ZONE_STOCK_UP);
        List<Location> locations = locationService.list(locationLambdaQueryWrapper);
        if (locations.size() == 0) {
            return AjaxResult.error("没有找到库位组," + locationCode);
        }
        List<Location> locationList = locations.stream().filter(l -> StringUtils.isNotEmpty(l.getContainerCode())).collect(Collectors.toList());


        List<Container> containerList = new ArrayList<>();
        for (Location location1 : locations) {
            String containerCode = location1.getContainerCode();
            if (StringUtils.isNotEmpty(containerCode)) {
                Container container = containerService.getContainerByCode(containerCode, warehouseCode);
                if (container != null) {
                    containerList.add(container);
                }
            }
        }
        if (containerList.size() == 0) {
            return AjaxResult.error("没有匹配到容器组");
        }
        List<String> containerCodeList = containerList.stream().map(Container::getCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.in(InventoryDetail::getContainerCode, containerCodeList);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存详情");
        }

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.in(InventoryHeader::getContainerCode, containerCodeList);
        List<InventoryHeader> inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeaderList.size() == 0) {
            return AjaxResult.error("没有找到库存头");
        }

        List<String> receiptCodeList = inventoryDetailList.stream().map(InventoryDetail::getReceiptCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.in(ReceiptHeader::getCode, receiptCodeList);
        List<ReceiptHeader> receiptHeaderList = receiptHeaderService.list(receiptHeaderLambdaQueryWrapper);
        List<String> shipmentCodeList = receiptHeaderList.stream().map(ReceiptHeader::getShipmentCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper.in(ShipmentHeader::getCode, shipmentCodeList);
        List<ShipmentHeader> shipmentHeaderList = shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper);
        if (shipmentHeaderList == null) {
            return AjaxResult.error("没有找到出库单头");
        }

        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getCode, inventoryContainerCode);
        Container container = containerService.getOne(containerLambdaQueryWrapper);
        if (container == null) {
            return AjaxResult.error("批量移库 容器为空" + inventoryContainerCode);
        }
        String fromLocationCode = container.getLocationCode();
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper3 = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper3.eq(Location::getCode, fromLocationCode).eq(Location::getZoneCode, "D");
        Location fromLocation = locationService.getOne(locationLambdaQueryWrapper3);
        if (fromLocation == null) {
            return AjaxResult.error("批量移库 库位为空" + fromLocationCode);
        }
        String shipmentCoce = fromLocation.getShipmentCode();
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper4 = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper4.eq(Location::getShipmentCode, shipmentCoce).eq(Location::getZoneCode, "D");
        List<Location> fromLocationList = locationService.list(locationLambdaQueryWrapper4);
        if (fromLocationList.size() == 0) {
            return AjaxResult.error("批量移库  根据出库单号没有找到对应库位" + shipmentCoce);
        }
        List<String> locationCodeList = locationList.stream().map(Location::getCode).collect(Collectors.toList());
        List<String> containerCodelist = locationList.stream().map(Location::getContainerCode).collect(Collectors.toList());
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper2 = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper2.in(InventoryHeader::getLocationCode, locationCodeList).eq(InventoryHeader::getZoneCode, "D");
        List<InventoryHeader> fromInventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper2);
        if (fromInventoryHeaderList.size() == 0) {
            throw new ServiceException("没有找到库存头");
        }
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper5 = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper5.eq(Location::getIRow, column).eq(Location::getZoneCode, QuantityConstant.ZONE_PINGKU)
                .eq(Location::getContainerCode, QuantityConstant.EMPTY_STRING).last(" limit 1");
        Location toLocation = locationService.getOne(locationLambdaQueryWrapper5);
        if (toLocation == null) {
            return AjaxResult.error("批量移库 目地库位为空");
        }
        String toLocationCode = toLocation.getCode();
        if (StringUtils.isNotEmpty(toLocation.getContainerCode())) {
            return AjaxResult.error("批量移库 目地库位已经有托盘:" + toLocation.getContainerCode());
        }
        toLocation.setContainerCode(inventoryContainerCode);
        toLocation.setShipmentCode(shipmentCode);
        if (!locationService.updateById(toLocation)) {
            throw new ServiceException("批量移库, 更新库位失败");
        }
        for (InventoryHeader inventoryHeader : fromInventoryHeaderList) {
            inventoryHeader.setLocationCode(toLocationCode);
            inventoryHeader.setZoneCode(QuantityConstant.ZONE_PINGKU);
        }
        if (!inventoryHeaderService.updateBatchById(fromInventoryHeaderList)) {
            throw new ServiceException("更新库存头失败");
        }
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            inventoryDetail.setLocationCode(toLocationCode);
            inventoryDetail.setZoneCode(toLocation.getZoneCode());
        }
        if (!inventoryDetailService.updateBatchById(inventoryDetailList)) {
            throw new ServiceException("更新库存详情失败");
        }

        for (String containerCode1 : containerCodelist) {
            containerService.updateLocationCodeAndStatus(containerCode1, toLocationCode, QuantityConstant.STATUS_CONTAINER_EMPTY,
                    QuantityConstant.DEFAULT_WAREHOUSE);
        }

        for (Location location2 : locations) {
            location2.setContainerCode("");
            location2.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
            location2.setShipmentCode("");
            location2.setStack(0);
            location2.setContainerSpec(0);
        }
        if (!locationService.updateBatchById(locations)) {
            throw new ServiceException("批量释放库位失败");
        }

        return AjaxResult.success("批量移库成功");
    }

    @Override
    @Transactional
    public AjaxResult restockingOfFlatWarehouse(String containerCode, String receiptCode, List<BoxInfo> listBoxInfo, String orderCode, String receiptDetailId) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("根据托盘号,没有找到托盘信息:" + containerCode);
        }
        String locationCode = container.getLocationCode();
        if (StringUtils.isNotEmpty(locationCode)) {
            return AjaxResult.error("托盘在库位上:" + locationCode);
        }

        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
        if (receiptHeader == null) {
            return AjaxResult.error("没有找到入库单");
        }

        LambdaQueryWrapper<ReceiptContainerHeader> receiptHeaderControllerLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderControllerLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).lt(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.list(receiptHeaderControllerLambdaQueryWrapper);
        if (receiptContainerHeaders.size() > 0) {
            return AjaxResult.error("当前托盘已存在任务了");
        }

        BoxInfo boxInfo = listBoxInfo.get(0);
        String materialCode = boxInfo.getMaterialCode();
        String color = boxInfo.getColor();
        String level = boxInfo.getLevel();
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode).eq(ReceiptDetail::getMaterialCode, materialCode).eq(ReceiptDetail::getId,
                receiptDetailId);

        ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailLambdaQueryWrapper);
        if (receiptDetail == null) {
            return AjaxResult.error("没有匹配到入库单详情");
        }
        getBosAssistantNumber(color, "colour");
        getBosAssistantNumber(level, "level");
        BigDecimal total = BigDecimal.ZERO;
        String materialCode1 = null, batch1 = null, level1 = null, color1 = null, proPackage1 = null, productSize1 = null;
        for (BoxInfo boxInfo1 : listBoxInfo) {
            materialCode1 = boxInfo1.getMaterialCode();
            color1 = boxInfo1.getColor();
            level1 = boxInfo1.getLevel();
            batch1 = boxInfo1.getBatch();
            proPackage1 = boxInfo1.getProPackaging();
            productSize1 = boxInfo1.getSize();

            if (!boxInfo1.getBatch().equals(receiptDetail.getBatch())) {
                return AjaxResult.error("实际批次" + boxInfo1.getBatch() + "与订单批次" + receiptDetail.getBatch() + "不匹配。" + "箱码" + boxInfo1.getBoxCode());
            }

            if (!materialCode.equals(materialCode1)) {
                return AjaxResult.error("物料信息不匹配");
            }
            if (!color.equals(color1)) {
                return AjaxResult.error("色号信息不匹配");
            }
            if (!level.equals(level1)) {
                return AjaxResult.error("等级信息不匹配");
            }
            BigDecimal qty = boxInfo1.getQty();
            total = total.add(qty);
        }
        BigDecimal taskQty = receiptDetail.getTaskQty();
        BigDecimal qty = receiptDetail.getQty();
        taskQty = taskQty.add(total);
        if (taskQty.compareTo(qty) > 0) {
            return AjaxResult.error("入库数量不能大于单据数量");
        }
        receiptDetail.setTaskQty(taskQty);
        boolean result = receiptDetailService.updateById(receiptDetail);
        if (!result) {
            throw new RuntimeException("更新入库单详情失败");
        }
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(warehouseCode);
        receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
        receiptContainerHeader.setContainerCode(containerCode);
        receiptContainerHeader.setBatch(batch1);
        receiptContainerHeader.setLevel(level1);
        receiptContainerHeader.setColor(color1);
        receiptContainerHeader.setProPackaging(proPackage1);
        receiptContainerHeader.setProductSchedule(receiptDetail.getProductSchedule());
        receiptContainerHeader.setOrderCode(receiptCode);// 订单编号
        ProductScheduleHeader productScheduleHeader = productScheduleHeaderService
                .getOne(new LambdaQueryWrapper<ProductScheduleHeader>().eq(ProductScheduleHeader::getNumber, receiptDetail.getProductSchedule()));
        if (productScheduleHeader == null) {
            throw new ServiceException("排产订单不存在,请先更新排产订单。");
        }
        receiptContainerHeader.setOrderName(productScheduleHeader.getName());// 订单名称
        receiptContainerHeader.setMaterialCode(materialCode1);// 物料编码
//        receiptContainerHeader1.setContainerType(containerTypeCode);
        receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
//        receiptContainerHeader1.setStack(stack);//设置垛型
        receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_CODING);
        receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
        result = receiptContainerHeaderService.save(receiptContainerHeader);
        if (!result) {
            throw new ServiceException("保存入库组盘头失败");
        }

        LambdaQueryWrapper<ProPackaging> proPackagingLambdaQueryWrapper = Wrappers.lambdaQuery();
        proPackagingLambdaQueryWrapper.eq(ProPackaging::getNumber, proPackage1);
        ProPackaging proPackaging = proPackingService.getOne(proPackagingLambdaQueryWrapper);
        if (proPackaging == null) {
            return AjaxResult.error("没有包装信息");
        }
        int pieceBox = proPackaging.getPieceBox();

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        for (BoxInfo boxInfo2 : listBoxInfo) {
            String boxCode2 = boxInfo2.getBoxCode();
//            int boxPosition2 = boxInfo2.getBoxPosition();
            List<ChipInfos> chipInfoList2 = boxInfo2.getChipInfos();
            String level2 = boxInfo2.getLevel();
            String proPackagingStr2 = boxInfo2.getProPackaging();
            String productSize2 = boxInfo2.getSize();
            String color2 = boxInfo2.getColor();
            String batch2 = boxInfo2.getBatch();
            for (ChipInfos chipInfos : chipInfoList2) {
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                String chipCode1 = chipInfos.getChipBarcode1();
                String chipCode2 = chipInfos.getChipBarcode2();
                receiptContainerDetail.setWarehouseCode(warehouseCode);
                receiptContainerDetail.setContainerCode(containerCode);
                receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                receiptContainerDetail.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
                receiptContainerDetail.setChipCode1(chipCode1);
                receiptContainerDetail.setChipCode2(chipCode2);
                receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
                receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
                receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                receiptContainerDetail.setProPackaging(proPackagingStr2);// 包装
                receiptContainerDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                receiptContainerDetail.setProductSize(productSize2);// 产品规格
                receiptContainerDetail.setLevel(level2);// 等级
                receiptContainerDetail.setColor(color2);// 色号
                receiptContainerDetail.setQty(BigDecimal.ONE);
                receiptContainerDetail.setBatch(batch2);
                receiptContainerDetail.setReceiptCode(receiptCode);
                receiptContainerDetail.setBoxCode(boxCode2);
                receiptContainerDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetailList.add(receiptContainerDetail);
            }
        }
        result = chooseWrapper.receiptContainerDetailServiceSaveBatch(receiptContainerDetailList);
        if (!result) {
            throw new ServiceException("保存入库组盘详情失败");
        }

        Integer receiptContainerHeaderId = receiptContainerHeader.getId();
        AjaxResult ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderId, warehouseCode);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("生成任务失败");
        }
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeaderService.updateById(receiptHeader);

        List<String> boxCodes = listBoxInfo.stream().map(BoxInfo::getBoxCode).collect(Collectors.toList());

        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.in(InventoryDetail::getBoxCode, boxCodes);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetails.size() < 1) {
            throw new ServiceException("库存不存在");
        }
        if (!inventoryDetailService.remove(inventoryDetailLambdaQueryWrapper)) {
            throw new ServiceException("删除库存明细失败");
        }

        List<Integer> inventorId = inventoryDetails.stream().map(InventoryDetail::getInventoryHeaderId).collect(Collectors.toList());
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.in(InventoryHeader::getId, inventorId);
        List<InventoryHeader> inventoryHeaders = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);

        List<InventoryHeader> inventoryRemove = new ArrayList<>();

        for (int i = 0; i < inventoryHeaders.size(); i++) {
            InventoryHeader inventoryHeader = inventoryHeaders.get(i);

            Integer Quantity = Math.toIntExact(inventoryDetails.stream().filter(detail -> detail.getInventoryHeaderId().equals(inventoryHeader.getId())).count());

            inventoryHeader.setBoxQty(inventoryHeader.getBoxQty().subtract(BigDecimal.valueOf(Quantity / pieceBox)));
            inventoryHeader.setTotalQty(inventoryHeader.getTotalQty().subtract(BigDecimal.valueOf(Quantity)));

            if (inventoryHeader.getBoxQty().intValue() == 0 || inventoryHeader.getTotalQty().intValue() == 0) {
                inventoryRemove.add(inventoryHeader);
                continue;
            }
            inventoryHeaders.set(i, inventoryHeader); // 将修改后的对象放回列表中
        }

        inventoryHeaders.removeAll(inventoryRemove);
        if (!inventoryHeaders.isEmpty()) {
            inventoryHeaderService.updateBatchById(inventoryHeaders);
        }

        if (!inventoryRemove.isEmpty()) {
            List<Integer> integers = inventoryRemove.stream().map(InventoryHeader::getId).collect(Collectors.toList());
            inventoryHeaderService.removeByIds(integers);
            //拿到入库单的主单局id 去重一下 因为按箱码查会有多条
            List<String> containerCodeA = inventoryRemove.stream()
                    .map(InventoryHeader::getContainerCode)  // 将TaskDetail对象转换为ReceiptId
                    .distinct()                  // 去除重复的ReceiptId
                    .collect(Collectors.toList());// 收集到List中
            LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
            containerLambdaQueryWrapper.in(Container::getCode,containerCodeA)
                    .like(Container::getCode,"LS");
            containerService.remove(containerLambdaQueryWrapper);
        }

        return AjaxResult.success("生成退货入库任务成功");
    }

    @Override
    @Transactional
    public AjaxResult PSTStockingArea(String containerCode, List<String> boxCodes,String shipmentDetailId) {
        ShipmentDetail detail1 = shipmentDetailService.getById(shipmentDetailId);

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, containerCode);
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeader == null) {
            return AjaxResult.error("没有找到库存头,托盘号为:" + containerCode);
        }
        String warehouseCode = inventoryHeader.getWarehouseCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("没有找到托盘,托盘号为:" + containerCode);
        }
        String code = containerService.createContainer("5", "D");
        Container container1 = containerService.getContainerByCode(code, warehouseCode);
        if (container1 == null) {
            throw new ServiceException("没有找到托盘,托盘号为:" + container1);
        }
        container1.setLocationCode(code);
        containerService.updateById(container1);

        InventoryHeader copiedInventoryHeader = new InventoryHeader();
        try {
            BeanUtils.copyProperties(inventoryHeader, copiedInventoryHeader);
        } catch (Exception e) {
            // 复制出错时的处理
            throw new ServiceException(e + "");
        }
        //这里要重新来一份进行复制
        copiedInventoryHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        copiedInventoryHeader.setZoneCode(QuantityConstant.ZONE_STOCK_UP);
        copiedInventoryHeader.setId(null);
        copiedInventoryHeader.setLocationCode(code);
        copiedInventoryHeader.setContainerCode(code);
        copiedInventoryHeader.setContainerStatus("some");
        copiedInventoryHeader.setBoxQty(BigDecimal.valueOf(boxCodes.size()));
        copiedInventoryHeader.setTotalQty(BigDecimal.valueOf(boxCodes.size() * 2));
        copiedInventoryHeader.setTotalLines(1);
        inventoryHeaderService.save(copiedInventoryHeader);


        List<InventoryDetail> inventoryDetails = new ArrayList<>();
        for (String boxCode : boxCodes) {
            //查库存明细
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode).eq(InventoryDetail::getBoxCode, boxCode);
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            if (inventoryDetailList.size() == 0) {
                throw new ServiceException("没有找到库存明细,托盘号为:" + containerCode + ", 箱码为:" + boxCode);
            }
            InventoryHistoryHeader inventoryHistoryHeader = inventoryHistoryHeaderService.getById(inventoryHeader.getId());
            if (inventoryHistoryHeader == null) {
                inventoryHistoryHeader = new InventoryHistoryHeader();
                inventoryHistoryHeader.setId(inventoryHeader.getId());
                inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
                inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
                inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
                inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
                inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
                inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
                inventoryHistoryHeader.setStack(inventoryHeader.getStack());
                inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
                inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
                inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
                inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
                inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
                inventoryHistoryHeader.setColor(inventoryHeader.getColor());
                inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
                inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
                inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
                inventoryHistoryHeader.setOrderCode(inventoryHeader.getOrderCode());
                inventoryHistoryHeader.setOrderName(inventoryHeader.getOrderName());
                inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
                inventoryHistoryHeader.setTotalLines(0);
                inventoryHistoryHeaderService.save(inventoryHistoryHeader);
            }
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT).eq(TaskHeader::getContainerCode, containerCode)
                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if (taskHeader == null) {
                throw new ServiceException("扣减库存 没有找到出库任务,托盘号:" + containerCode);
            }
            LambdaQueryWrapper<TaskDetail> taskDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskDetailLambdaQueryWrapper.eq(TaskDetail::getBoxCode, boxCode).eq(TaskDetail::getTaskId, taskHeader.getId());
            List<TaskDetail> taskDetails = taskDetailService.list(taskDetailLambdaQueryWrapper);
            if (taskDetails.size() > 0) {
                throw new ServiceException("当前箱码已经出库" + boxCode);
            }
            List<TaskDetail> taskDetailList = new ArrayList<>();
            BigDecimal shipQty = BigDecimal.ZERO;
            String shipmentCode = taskHeader.getShipmentCode();
            String materialCode = taskHeader.getMaterialCode();
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getMaterialCode, materialCode).eq(ShipmentDetail::getShipmentCode, shipmentCode)
                    .eq(ShipmentDetail::getLevel, inventoryHeader.getLevel())
                    .eq(ShipmentDetail::getColor, inventoryHeader.getColor())
                    .eq(ShipmentDetail::getProductSchedule, inventoryHeader.getProductSchedule())
                    .eq(ShipmentDetail::getId,shipmentDetailId)
                    .eq(ShipmentDetail::getProPackaging, inventoryHeader.getProPackaging());


            ShipmentDetail shipmentDetail = shipmentDetailService.getOne(shipmentDetailLambdaQueryWrapper);
            if (shipmentDetail == null) {
                throw new ServiceException("根据出库单号:" + shipmentCode + "没有找到出库详情");
            }
            ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentDetail.getShipmentId());
            if (shipmentHeader == null) {
                throw new ServiceException("根据出库单号:" + shipmentCode + "没有找到出库头");
            }
            Location location = new Location();
            location.setId(null);
            location.setShipmentCode(shipmentHeader.getCode());
            location.setCode(code);
            location.setZoneCode(QuantityConstant.ZONE_STOCK_UP);
            location.setWarehouseCode(warehouseCode);
            location.setContainerCode(code);

            //如果已存在库位,不再创建
            Location location1 = locationService.getOne(new LambdaQueryWrapper<Location>().eq(Location::getCode, code));
            if (location1 == null) {
                if (!locationService.save(location)) {
                    throw new ServiceException("更新库位失败");
                }
            }


            LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getContainerCode, containerCode)
                    .eq(ShipmentContainerHeader::getShipmentCode, shipmentCode).lt(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getOne(shipmentContainerHeaderLambdaQueryWrapper);
            if (shipmentContainerHeader == null) {
                shipmentContainerHeader = new ShipmentContainerHeader();
                shipmentContainerHeader.setContainerCode(containerCode);
                shipmentContainerHeader.setLocationCode(inventoryHeader.getLocationCode());
                shipmentContainerHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
                shipmentContainerHeader.setCompanyCode(inventoryHeader.getCompanyCode());
                shipmentContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_SORTINGSHIPMENT);
                shipmentContainerHeader.setContainerType(container.getContainerType());
                shipmentContainerHeader.setStatus(0);
                shipmentContainerHeader.setShipmentCode(shipmentCode);
                shipmentContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_WMS);
                shipmentContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_WMS);
                shipmentContainerHeaderService.save(shipmentContainerHeader);
            }

            List<InventoryTransaction> inventoryTransactionList = new ArrayList<>();
            List<ShipmentContainerDetail> shipmentContainerDetailList = new ArrayList<>();

            for (InventoryDetail inventoryDetail : inventoryDetailList) {
                InventoryTransaction inventoryTransaction = new InventoryTransaction();
                inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT);
                inventoryTransaction.setWarehouseCode(inventoryDetail.getWarehouseCode());
                inventoryTransaction.setCompanyCode(inventoryDetail.getCompanyCode());
                inventoryTransaction.setLocationCode(inventoryDetail.getLocationCode());
                inventoryTransaction.setContainerCode(inventoryDetail.getContainerCode());
                inventoryTransaction.setMaterialCode(inventoryDetail.getMaterialCode());
                inventoryTransaction.setZoneCode(inventoryDetail.getZoneCode());
                inventoryTransaction.setMaterialName(inventoryDetail.getMaterialName());
                inventoryTransaction.setMaterialSpec(inventoryDetail.getMaterialSpec());
                inventoryTransaction.setMaterialUnit(inventoryDetail.getMaterialUnit());
                inventoryTransaction.setChipCode1(inventoryDetail.getChipCode1());
                inventoryTransaction.setChipCode2(inventoryDetail.getChipCode2());
                inventoryTransaction.setLevel(inventoryDetail.getLevel());
                inventoryTransaction.setColor(inventoryDetail.getColor());
                inventoryTransaction.setBoxCode(inventoryDetail.getBoxCode());
                inventoryTransaction.setBoxPosition(inventoryDetail.getBoxPosition());
                inventoryTransaction.setBillCode(shipmentCode);
                inventoryTransaction.setBatch(inventoryDetail.getBatch());
                inventoryTransaction.setLot(inventoryDetail.getLot());
                inventoryTransaction.setInventorySts(inventoryDetail.getInventorySts());
                inventoryTransaction.setTaskQty(inventoryDetail.getQty());
                shipQty = shipQty.add(inventoryDetail.getQty());
                inventoryTransactionList.add(inventoryTransaction);

                ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
                shipmentContainerDetail.setWarehouseCode(warehouseCode);
                shipmentContainerDetail.setContainerCode(containerCode);
                shipmentContainerDetail.setLocationCode(inventoryDetail.getLocationCode());
                shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
                shipmentContainerDetail.setShipmentCode(shipmentCode);
                shipmentContainerDetail.setShipmentId(shipmentHeader.getId());
                shipmentContainerDetail.setShipmentDetailId(shipmentDetail.getId());
                shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
                shipmentContainerDetail.setCompanyCode(inventoryDetail.getCompanyCode());
                shipmentContainerDetail.setMaterialCode(inventoryDetail.getMaterialCode());
                shipmentContainerDetail.setMaterialName(inventoryDetail.getMaterialName());
                shipmentContainerDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
                shipmentContainerDetail.setStatus(QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
                shipmentContainerDetail.setChipCode1(inventoryDetail.getChipCode1());
                shipmentContainerDetail.setChipCode2(inventoryDetail.getChipCode2());
                shipmentContainerDetail.setLevel(inventoryDetail.getLevel());
                shipmentContainerDetail.setColor(inventoryDetail.getColor());
                shipmentContainerDetail.setBoxCode(inventoryDetail.getBoxCode());
                shipmentContainerDetail.setBoxPosition(inventoryDetail.getBoxPosition());
                shipmentContainerDetail.setProPackaging(inventoryDetail.getProPackaging());
                shipmentContainerDetail.setProductSize(inventoryDetail.getProductSize());
                shipmentContainerDetailList.add(shipmentContainerDetail);

                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(taskHeader.getId());
                taskDetail.setTaskType(taskHeader.getTaskType());
                taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
                taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
                taskDetail.setCompanyCode(taskHeader.getCompanyCode());
                taskDetail.setContainerCode(taskHeader.getContainerCode());
                taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
                taskDetail.setMaterialName(inventoryDetail.getMaterialName());
                taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
                taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
                taskDetail.setBoxCode(inventoryDetail.getBoxCode());
                taskDetail.setBoxPosition(inventoryDetail.getBoxPosition());
                taskDetail.setChipCode1(inventoryDetail.getChipCode1());
                taskDetail.setChipCode2(inventoryDetail.getChipCode2());
                taskDetail.setLevel(inventoryDetail.getLevel());
                taskDetail.setColor(inventoryDetail.getColor());
                taskDetail.setQty(inventoryDetail.getQty());
                taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
                taskDetail.setBatch(inventoryDetail.getBatch());
                taskDetail.setInventorySts(inventoryDetail.getInventorySts());
                taskDetail.setCreatedBy(taskHeader.getCreatedBy());
                taskDetail.setProPackaging(inventoryDetail.getProPackaging());
                taskDetail.setProductSchedule(inventoryDetail.getProductSchedule());
                taskDetail.setUserDef3(String.valueOf(shipmentHeader.getId()));
                taskDetailList.add(taskDetail);
            }

            addHistoryInventoryDetail(inventoryDetailList, inventoryHistoryHeader, "整箱出库");
            if (!inventoryTransactionService.saveBatch(inventoryTransactionList)) {
                throw new ServiceException("新增库存记录失败");
            }
            if (!taskDetailService.saveBatch(taskDetailList)) {
                throw new ServiceException("新增任务详情失败");
            }
            if (!shipmentContainerDetailService.saveBatch(shipmentContainerDetailList)) {
                throw new ServiceException("新增出库组盘详情失败");
            }
            List<Integer> ids = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
            boolean result = inventoryDetailService.removeByIds(ids);
            if (!result) {
                throw new ServiceException("删除库存明细失败, 托盘号为:" + containerCode);
            }
            ShipmentDetail shipmentDetailCop=new ShipmentDetail();
            BigDecimal taskQty = shipmentDetail.getTaskQty().add(shipQty);
            BigDecimal qty = shipmentDetail.getQty();
            shipmentDetailCop.setTaskQty(taskQty);
            shipmentDetailCop.setId(shipmentDetail.getId());
            if (taskQty.compareTo(qty) == 0) {
                shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_REVIEWSUCCESS);
                shipmentContainerHeaderService.updateById(shipmentContainerHeader);
            } else {
                if (taskQty.compareTo(qty) > 0) {
                    throw new ServiceException("不允许超出单据设定的出库数量");
                }
            }
            //更新出库单明细数量
            result = shipmentDetailService.updateById(shipmentDetailCop);
            if (!result) {
                throw new ServiceException("更新出库详情失败, 托盘号为:" + containerCode);
            }
            //生成调拨
            AjaxResult ajaxResult = receiptHeaderService.createDirectTransferReceipt(shipmentHeader);
            ReceiptHeader receiptHeader = receiptHeaderService.getById((int) ajaxResult.getData());

            LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
            List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambdaQueryWrapper);
            List<ReceiptDetail> receiptDetailList = receiptDetails.stream().filter(r -> r.getTaskQty().compareTo(r.getQty()) != 0).collect(Collectors.toList());
            if (receiptDetailList.isEmpty()) {
                receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_POSTING);
                receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_POSTING);
                receiptHeaderService.updateById(receiptHeader);
            }


            updateInventoryHeader(inventoryHeader);
            updateSihpmnentHeaderStatus(shipmentHeader);

            inventoryDetailList = inventoryDetailList.stream()
                    .map(detail -> {
                        detail.setInventoryHeaderId(copiedInventoryHeader.getId());
                        detail.setLocationCode(copiedInventoryHeader.getLocationCode());
                        detail.setContainerCode(copiedInventoryHeader.getContainerCode());
                        detail.setZoneCode(copiedInventoryHeader.getZoneCode());

                        detail.setReceiptDetailId(receiptHeader.getId());
                        detail.setReceiptCode(receiptHeader.getCode());
                        return detail;
                    })
                    .collect(Collectors.toList());

            inventoryDetails.addAll(inventoryDetailList);
        }

        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(detail1.getShipmentId());



        LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
        List<ShipmentDetail> shipmentDetails = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
        if (shipmentDetails.stream()
                .allMatch(detail -> detail.getQty().equals(detail.getTaskQty())))
        {
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_COMPLETED);
        }else
        {
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_PREPARE);
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_PREPARE);
        }

        if (!shipmentHeaderService.updateById(shipmentHeader)) {
            throw new ServiceException("修改出库单站台失败");
        }
        if (!inventoryDetailService.saveBatch(inventoryDetails)) {
            throw new ServiceException("增加散片明细失败");
        }
        return AjaxResult.success("整箱出库成功");
    }

    @Override
    public AjaxResult returnToFlatWarehouse(String containerCode, String locationCode, String receiptCode, List<BoxInfo> listBoxInfo, String receiptDetailId) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("根据托盘号,没有找到托盘信息:" + containerCode);
        }

        LambdaQueryWrapper<Location> locationLambdaQueryWrapper5 = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper5.eq(Location::getIRow, locationCode).eq(Location::getZoneCode, QuantityConstant.ZONE_PINGKU)
                .eq(Location::getContainerCode, QuantityConstant.EMPTY_STRING).last(" limit 1");
        Location toLocation = locationService.getOne(locationLambdaQueryWrapper5);
        if (toLocation == null) {
            return AjaxResult.error("批量移库 目地库位为空");
        }
        if (StringUtils.isNotEmpty(toLocation.getContainerCode())) {
            return AjaxResult.error("批量移库 目地库位已经有托盘:" + toLocation.getContainerCode());
        }
        toLocation.setContainerCode(containerCode);
        toLocation.setShipmentCode("退货入平库的");
        toLocation.setStatus(QuantityConstant.STATUS_LOCATION_LOCK);
        if (!locationService.updateById(toLocation)) {
            throw new ServiceException("批量移库, 更新库位失败");
        }


        if (QuantityConstant.STATUS_CONTAINER_LOCK.equals(container.getStatus())) {
            return AjaxResult.error("托盘" + container.getCode() + "已锁定");
        }
        LambdaQueryWrapper<ReceiptContainerHeader> receiptHeaderControllerLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderControllerLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode).lt(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.list(receiptHeaderControllerLambdaQueryWrapper);
        if (receiptContainerHeaders.size() > 0) {
            return AjaxResult.error("当前托盘已存在任务了");
        }

        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getCode, receiptCode);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
        if (receiptHeader == null) {
            return AjaxResult.error("没有找到入库单");
        }
        BoxInfo boxInfo = listBoxInfo.get(0);
        String materialCode = boxInfo.getMaterialCode();
        String color = boxInfo.getColor();
        String level = boxInfo.getLevel();
        LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptCode, receiptCode).eq(ReceiptDetail::getMaterialCode, materialCode).eq(ReceiptDetail::getId,
                receiptDetailId);

        ReceiptDetail receiptDetail = receiptDetailService.getOne(receiptDetailLambdaQueryWrapper);
        if (receiptDetail == null) {
            return AjaxResult.error("没有匹配到入库单详情");
        }
        getBosAssistantNumber(color, "colour");
        getBosAssistantNumber(level, "level");
        BigDecimal total = BigDecimal.ZERO;
        String materialCode1 = null, batch1 = null, level1 = null, color1 = null, proPackage1 = null, productSize1 = null;
        for (BoxInfo boxInfo1 : listBoxInfo) {
            materialCode1 = boxInfo1.getMaterialCode();
            color1 = boxInfo1.getColor();
            level1 = boxInfo1.getLevel();
            batch1 = boxInfo1.getBatch();
            proPackage1 = boxInfo1.getProPackaging();
            productSize1 = boxInfo1.getSize();

            if (!boxInfo1.getBatch().equals(receiptDetail.getBatch())) {
                return AjaxResult.error("实际批次" + boxInfo1.getBatch() + "与订单批次" + receiptDetail.getBatch() + "不匹配。" + "箱码" + boxInfo1.getBoxCode());
            }

            if (!materialCode.equals(materialCode1)) {
                return AjaxResult.error("物料信息不匹配");
            }
            if (!color.equals(color1)) {
                return AjaxResult.error("色号信息不匹配");
            }
            if (!level.equals(level1)) {
                return AjaxResult.error("等级信息不匹配");
            }
            BigDecimal qty = boxInfo1.getQty();
            total = total.add(qty);
        }
        BigDecimal taskQty = receiptDetail.getTaskQty();
        BigDecimal qty = receiptDetail.getQty();
        taskQty = taskQty.add(total);
        if (taskQty.compareTo(qty) > 0) {
            return AjaxResult.error("入库数量不能大于单据数量");
        }
        receiptDetail.setTaskQty(taskQty);
        boolean result = receiptDetailService.updateById(receiptDetail);
        if (!result) {
            throw new RuntimeException("更新入库单详情失败");
        }
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(warehouseCode);
        receiptContainerHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
        receiptContainerHeader.setContainerCode(containerCode);
        receiptContainerHeader.setBatch(batch1);
        receiptContainerHeader.setOperatorName(ShiroUtils.getLoginName());
        receiptContainerHeader.setLevel(level1);
        receiptContainerHeader.setColor(color1);
        receiptContainerHeader.setProPackaging(proPackage1);
        receiptContainerHeader.setProductSchedule(receiptDetail.getProductSchedule());
        receiptContainerHeader.setOrderCode(receiptDetail.getProductSchedule());// 订单编号
        ProductScheduleHeader productScheduleHeader = productScheduleHeaderService
                .getOne(new LambdaQueryWrapper<ProductScheduleHeader>().eq(ProductScheduleHeader::getNumber, receiptDetail.getProductSchedule()));
        if (productScheduleHeader == null) {
            throw new ServiceException("排产订单不存在,请先更新排产订单。");
        }
        receiptContainerHeader.setOrderName(productScheduleHeader.getName());// 订单名称
        receiptContainerHeader.setMaterialCode(materialCode1);// 物料编码
//        receiptContainerHeader1.setContainerType(containerTypeCode);
        receiptContainerHeader.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
//        receiptContainerHeader1.setStack(stack);//设置垛型
        receiptContainerHeader.setCreatedBy(QuantityConstant.PLATFORM_CODING);
        receiptContainerHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
        result = receiptContainerHeaderService.save(receiptContainerHeader);
        if (!result) {
            throw new ServiceException("保存入库组盘头失败");
        }

        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        List<InventoryHistoryDetail> inventoryHistoryDetailList = new ArrayList<>();
        for (BoxInfo boxInfo2 : listBoxInfo) {
            String boxCode2 = boxInfo2.getBoxCode();
//            int boxPosition2 = boxInfo2.getBoxPosition();
            List<ChipInfos> chipInfoList2 = boxInfo2.getChipInfos();
            String level2 = boxInfo2.getLevel();
            String proPackagingStr2 = boxInfo2.getProPackaging();
            String productSize2 = boxInfo2.getSize();
            String color2 = boxInfo2.getColor();
            String batch2 = boxInfo2.getBatch();
            String productSize = boxInfo2.getProductSize();
            for (ChipInfos chipInfos : chipInfoList2) {
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                String chipCode1 = chipInfos.getChipBarcode1();
                String chipCode2 = chipInfos.getChipBarcode2();
                receiptContainerDetail.setWarehouseCode(warehouseCode);
                receiptContainerDetail.setContainerCode(containerCode);
                receiptContainerDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                receiptContainerDetail.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
                receiptContainerDetail.setChipCode1(chipCode1);
                receiptContainerDetail.setChipCode2(chipCode2);
                receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
                receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
                receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                receiptContainerDetail.setProPackaging(proPackagingStr2);// 包装
                receiptContainerDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                receiptContainerDetail.setProductSize(productSize2);// 产品规格
                receiptContainerDetail.setLevel(level2);// 等级
                receiptContainerDetail.setColor(color2);// 色号
                receiptContainerDetail.setQty(BigDecimal.ONE);
                receiptContainerDetail.setBatch(batch2);
                receiptContainerDetail.setProductSize(productSize);
                receiptContainerDetail.setReceiptCode(receiptDetail.getProductSchedule());
                receiptContainerDetail.setBoxCode(boxCode2);
                receiptContainerDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                receiptContainerDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetailList.add(receiptContainerDetail);

                InventoryHistoryDetail inventoryHistoryDetail = new InventoryHistoryDetail();
                inventoryHistoryDetail.setInventoryHistoryHeaderId(0);
                inventoryHistoryDetail.setWarehouseCode(warehouseCode);
                inventoryHistoryDetail.setContainerCode(containerCode);
                inventoryHistoryDetail.setLocationCode("");
                inventoryHistoryDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
                inventoryHistoryDetail.setChipCode1(chipCode1);
                inventoryHistoryDetail.setChipCode2(chipCode2);
                inventoryHistoryDetail.setMaterialCode(receiptDetail.getMaterialCode());
                inventoryHistoryDetail.setMaterialName(receiptDetail.getMaterialName());
                inventoryHistoryDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
                inventoryHistoryDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
                inventoryHistoryDetail.setProPackaing(proPackagingStr2);
                inventoryHistoryDetail.setProductSchedule(receiptDetail.getProductSchedule());// 排产订单
                inventoryHistoryDetail.setProductSize(productSize2);// 产品规格
                inventoryHistoryDetail.setLevel(level2);// 等级
                inventoryHistoryDetail.setColor(color2);// 色号
                inventoryHistoryDetail.setQty(BigDecimal.ONE);
                inventoryHistoryDetail.setTaskQty(BigDecimal.ZERO);
                inventoryHistoryDetail.setBatch(batch2);
                inventoryHistoryDetail.setReceiptCode(receiptDetail.getProductSchedule());
                inventoryHistoryDetail.setBoxCode(boxCode2);
                inventoryHistoryDetail.setInventorySts(QuantityConstant.QUALITY_GOOD);
                inventoryHistoryDetail.setCreatedBy(QuantityConstant.PLATFORM_CODING);
                inventoryHistoryDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_CODING);
                inventoryHistoryDetail.setReceiptDetailId(receiptDetail.getId());
                inventoryHistoryDetail.setUserDef1("退货入平库");
                inventoryHistoryDetailList.add(inventoryHistoryDetail);
            }
        }
        result = chooseWrapper.receiptContainerDetailServiceSaveBatch(receiptContainerDetailList);
        if (!result) {
            throw new ServiceException("保存入库组盘详情失败");
        }

        result = inventoryHistoryDetailService.saveBatch(inventoryHistoryDetailList);
        if (!result) {
            throw new ServiceException("保存历史库存详情失败");
        }

        Integer receiptContainerHeaderId = receiptContainerHeader.getId();
        AjaxResult<Integer> ajaxResult = receiptTaskService.createReceiptTask(receiptContainerHeaderId, warehouseCode);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("生成任务失败");
        }


        TaskHeader task = taskHeaderService.getById(ajaxResult.getData());

        LambdaUpdateWrapper<TaskHeader> taskHeaderLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        taskHeaderLambdaUpdateWrapper.eq(TaskHeader::getId, ajaxResult.getData())
                .set(TaskHeader::getToLocation, toLocation.getCode());
        if (!taskHeaderService.update(taskHeaderLambdaUpdateWrapper)) {
            throw new ServiceException("容器状态更新失败");
        }


        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        receiptHeaderService.updateById(receiptHeader);

        ajaxResult = taskHeaderService.completeTask(task);

        return AjaxResult.success("生成退货入库任务成功");
    }

    @Override
    public AjaxResult flatLibraryAdjustment(String boxCode, String containerCode, String remove,String chipCode) {
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode,containerCode)
                .eq(InventoryHeader::getZoneCode,QuantityConstant.ZONE_PINGKU);
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeader==null)
        {
            return AjaxResult.error("库存不存在");
        }
        String[] chipCodes = Convert.toStrArray(chipCode);
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode,containerCode)
                .eq(InventoryDetail::getZoneCode,QuantityConstant.ZONE_PINGKU);

        //两种操作集成一个方法 remove给0是增加库存 其他是删除库存
        if (remove.equals("0")) {
            inventoryDetailLambdaQueryWrapper.last(" limit 1");
            InventoryDetail inventoryDetail = inventoryDetailService.getOne(inventoryDetailLambdaQueryWrapper);
            if (inventoryDetail==null)
            {
                return AjaxResult.error("容器在库存明细不存在数据");
            }
            //因为木托盘只存在一种类型物料特性 直接复制一个明细然后改变箱码 片码即可 循环片码来增加
            List<InventoryDetail> inventoryDetails=new ArrayList<>();
            for (String chip:chipCodes)
            {
                inventoryDetail.setId(null);
                inventoryDetail.setBoxCode(boxCode);
                inventoryDetail.setChipCode1(chip);
                inventoryDetails.add(inventoryDetail);
            }
            if (!inventoryDetailService.saveBatch(inventoryDetails))
            {
                throw new ServiceException("增加失败");
            }
            //新增的 回传其他入库单
            ReceiptHeader receiptHeader=new ReceiptHeader();
            receiptHeader.setCompanyCode("JY");
            receiptHeader.setReceiptType("OR");
            receiptHeader.setLastStatus(500);
            receiptHeader.setFirstStatus(500);
            receiptHeader.setUserDef1(configService.getKey(QuantityConstant.QT_R_DEF_DEPT_V));
            receiptHeader.setInType("081");
            receiptHeaderService.saveReceiptHeader(receiptHeader);

            ReceiptDetail receiptDetail=new ReceiptDetail();
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setCompanyCode(receiptHeader.getCompanyCode());
            receiptDetail.setMaterialName(inventoryDetails.get(0).getMaterialName());
            receiptDetail.setMaterialCode(inventoryDetails.get(0).getMaterialCode());
            receiptDetail.setBatch(inventoryDetails.get(0).getBatch());
            receiptDetail.setQcCheck("1");
            receiptDetail.setQty(BigDecimal.valueOf(inventoryDetails.size()));
            receiptDetail.setTaskQty(receiptDetail.getQty());
            receiptDetail.setInventorySts("good");
            receiptDetail.setProPackaging(inventoryDetails.get(0).getProPackaging());
            receiptDetail.setProductSchedule(inventoryDetails.get(0).getProductSchedule());
            receiptDetail.setLevel(inventoryDetails.get(0).getLevel());
            receiptDetail.setColor(inventoryDetails.get(0).getColor());
            receiptDetailService.saveReceiptDetaial(receiptDetail);

        } else {//需要删除的代表库里面有 增加查询条件查出来
            inventoryDetailLambdaQueryWrapper
                    .eq(InventoryDetail::getBoxCode,boxCode).in(InventoryDetail::getChipCode1,chipCodes);
            List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            if (!inventoryDetailService.remove(inventoryDetailLambdaQueryWrapper))
            {
                throw new ServiceException("删除失败");
            }

            ShipmentHeader shipmentHeader = new ShipmentHeader();
            shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_BILL_TYPE_OS);
            shipmentHeader.setCode(shipmentHeaderService.createCode(QuantityConstant.SHIPMENT_BILL_TYPE_OS));
            shipmentHeader.setReferCodeType(QuantityConstant.BILL_TYPE_SHIPMENT_QTCK);
            shipmentHeader.setQtcklx("001");
            String key = configService.getKey(QuantityConstant.QT_DEF_DEPT);
            shipmentHeader.setUserDef1(key);
            shipmentHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
            shipmentHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
            shipmentHeader.setTotalQty(BigDecimal.valueOf(chipCodes.length));
            shipmentHeader.setTotalLines(1);
            shipmentHeader.setFirstStatus(500);
            shipmentHeader.setLastStatus(500);
            shipmentHeader.setCreatedBy(ShiroUtils.getLoginName());
            shipmentHeaderService.save(shipmentHeader);

            ShipmentDetail newDetail = new ShipmentDetail();
            BeanUtils.copyProperties(inventoryDetails.get(0),newDetail);
            newDetail.setId(null);
            newDetail.setFHTZDetail(null);
            newDetail.setSclllx(null);
            newDetail.setReferLineNum(null);
            newDetail.setReferCode(null);
            newDetail.setReferId(null);
            newDetail.setTaskQty(BigDecimal.valueOf(inventoryDetails.size()));
            newDetail.setDamageQty(null);
            newDetail.setSplitQty(null);
            newDetail.setShipmentId(shipmentHeader.getId());
            newDetail.setShipmentCode(shipmentHeader.getCode());
            newDetail.setQty(BigDecimal.valueOf(inventoryDetails.size()));
            newDetail.setCreatedBy(ShiroUtils.getLoginName());
            newDetail.setCreated(DateUtils.getNowDate());
            newDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            newDetail.setLastUpdated(DateUtils.getNowDate());

            boolean b = shipmentDetailService.save(newDetail);
            if(!b){
                throw new ServiceException("新增报损:新增失败。");
            }
        }

        //查询明细长度
        inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode,containerCode)
                .eq(InventoryDetail::getZoneCode,QuantityConstant.ZONE_PINGKU);
        int count = inventoryDetailService.count(inventoryDetailLambdaQueryWrapper);


        if (count<=0)
        {
            if (inventoryHeader.getLocationCode().contains(QuantityConstant.VIRTUAL_PALLET_TYPE))
            {
                //删除库位
                if (!locationService.removeById(inventoryHeader.getLocationCode())) {
                    throw new ServiceException("删除库位失败");
                }
            }else
            {
                locationService.updateContainerCodeAndStatus(inventoryHeader.getLocationCode(), "",
                        QuantityConstant.STATUS_LOCATION_EMPTY);
            }
            //清理容器
            containerService.updateLocationCodeAndStatus(containerCode, "", QuantityConstant.STATUS_CONTAINER_EMPTY);

            inventoryHeaderService.removeById(inventoryHeader.getId());
        }else {
        //查询明细头 并且修改明细头数量
        LambdaUpdateWrapper<InventoryHeader> inventoryHeaderLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        inventoryHeaderLambdaUpdateWrapper.eq(InventoryHeader::getContainerCode,containerCode)
                .eq(InventoryHeader::getZoneCode,QuantityConstant.ZONE_PINGKU)
                        .set(InventoryHeader::getBoxQty,count/2).set(InventoryHeader::getTotalQty,count);
        if (!inventoryHeaderService.update(inventoryHeaderLambdaUpdateWrapper))
        {
            throw new ServiceException("修改库存头失败");
        }
        }
        return AjaxResult.success("操作成功");
    }

    @Override
    @Transactional
    public AjaxResult clearPartInventory(String warehouseCode, String contaienrCodes) {
        String[] codeArray = Convert.toStrArray(contaienrCodes);

        LambdaUpdateWrapper<Container> containerLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        containerLambdaUpdateWrapper.in(Container::getCode,codeArray);

        List<Container> containerList = containerService.list(containerLambdaUpdateWrapper);
        if (containerList.size() != codeArray.length) {
            return AjaxResult.error("没有找到容器数据:" + contaienrCodes);
        }


        String locationCode = containerList.get(0).getLocationCode();
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error("容器没有对应的库位数据:" + locationCode);
        }

        Location location = locationService.getLocationByCode(locationCode, warehouseCode);
        String shipmentCode = location.getShipmentCode();
        if (StringUtils.isEmpty(shipmentCode)) {
            return AjaxResult.error("库位" + locationCode + "没有找到对应的出库订单组");
        }
        if (StringUtils.isEmpty(location.getZoneCode())||!location.getZoneCode().equals(QuantityConstant.ZONE_PINGKU)){
            return AjaxResult.error("当前库存不在平库");
        }
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper1.eq(ShipmentHeader::getCode, shipmentCode);
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper1);
        if (shipmentHeader1 == null) {
            return AjaxResult.error("当前出库单不存在 是不是被误删除了");
        }


        if (shipmentHeader1.getPseudoShipment()&&!shipmentHeader1.getCode().contains("PS") && shipmentHeader1.getDestStockId().contains("CK00") && shipmentHeader1.getFSrcStockId().contains("CK00")) {
            throw new ServiceException("当前为内部调拨单 无需释放");
        }

        if (StringUtils.isEmpty(shipmentHeader1.getFSrcStockId()) && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)
                && shipmentHeader1.getShipmentType().equals(QuantityConstant.SHIPMENT_BILL_TYPE_DN) && shipmentHeader1.getPseudoShipment()) {
            throw new ServiceException("当前订单在k3为调拨单 无需释放");
        }
        if (shipmentHeader1.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_COMPLETED) {
            return AjaxResult.error("当前出库单状态不是拣选完成");
        }

        LambdaQueryWrapper<ReservationSubmission> reservationSubmissionLambdaQueryWrapper = Wrappers.lambdaQuery();
        reservationSubmissionLambdaQueryWrapper.eq(ReservationSubmission::getAuditStatus, "adopt").eq(ReservationSubmission::getOrderNumber,
                shipmentHeader1.getReferCode());
        int reservationCount = reservationSubmissionService.count(reservationSubmissionLambdaQueryWrapper);
        if (!locationCode.contains(QuantityConstant.VIRTUAL_PALLET_TYPE) && StringUtils.isEmpty(shipmentHeader1.getFSrcStockId()) && reservationCount < 1
                && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)) {
            return AjaxResult.error("预约审核信息查询不存在 请查看是不是还没审核");
        }
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getShipmentCode, shipmentCode).lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
        if (taskHeaderList.size() > 0) {
            List<Integer> taskIds = taskHeaderList.stream().map(TaskHeader::getId).collect(Collectors.toList());
            return AjaxResult.error("还有任务没有完成,任务号为:" + taskIds);
        }

        // 先查出不是P30的任务总数(第一阶段)
        taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getShipmentCode, shipmentCode).ne(TaskHeader::getPortGroup, QuantityConstant.PORTGROUP_P30)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_OVER_STATION);
        taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);

        List<String> collect = taskHeaderList.stream().map(TaskHeader::getContainerCode).collect(Collectors.toList());

        if (collect.size() != 0) {
            LambdaQueryWrapper<AgvTask> agvTaskLambdaQueryWrapper = Wrappers.lambdaQuery();
            agvTaskLambdaQueryWrapper.in(AgvTask::getContainerCode, collect).ne(AgvTask::getStatus, QuantityConstant.AGV_TASK_STATUS_COMPLETED);
            List<AgvTask> taskList = agvTaskService.list(agvTaskLambdaQueryWrapper);
            if (taskList.size() > 0) {
                return AjaxResult.error("还有没完成的agv任务的,任务号为:" + collect);
            }
        }

        if (!location.getShipmentCode().equals(shipmentCode)){
            return AjaxResult.error("订单组状态不对," + locationCode);
        }



        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getLocationCode, location.getCode())
                .in(InventoryDetail::getContainerCode, codeArray);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return AjaxResult.error("没有找到库存详情");
        }

        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getLocationCode, location.getCode());
        List<InventoryHeader> inventoryHeaders  = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
        if (inventoryHeaders.size() == 0) {
            return AjaxResult.error("没有找到库存头");
        }

        List<InventoryHeader> inventoryHeaderList = inventoryHeaders.stream().filter(i -> contaienrCodes.contains(i.getContainerCode())).collect(Collectors.toList());


        List<String> receiptCodeList = inventoryDetailList.stream().map(InventoryDetail::getReceiptCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptHeaderLambdaQueryWrapper.in(ReceiptHeader::getCode, receiptCodeList);
        List<ReceiptHeader> receiptHeaderList = receiptHeaderService.list(receiptHeaderLambdaQueryWrapper);
        List<String> shipmentCodeList = receiptHeaderList.stream().map(ReceiptHeader::getShipmentCode).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentHeaderLambdaQueryWrapper.in(ShipmentHeader::getCode, shipmentCodeList);
        List<ShipmentHeader> shipmentHeaderList = shipmentHeaderService.list(shipmentHeaderLambdaQueryWrapper);
        boolean result = false;
        if (shipmentHeaderList.size() < 1 && !shipmentCode.contains("OS")) {
            return AjaxResult.error("没有找到出库单头");
        }

        //给历史表
        for (InventoryHeader inventoryHeader : inventoryHeaderList) {
            InventoryHistoryHeader inventoryHistoryHeader = inventoryHistoryHeaderService.getById(inventoryHeader.getId());
            if (inventoryHistoryHeader == null) {
                inventoryHistoryHeader = new InventoryHistoryHeader();
                inventoryHistoryHeader.setId(inventoryHeader.getId());
                inventoryHistoryHeader.setContainerCode(inventoryHeader.getContainerCode());
                inventoryHistoryHeader.setWarehouseCode(inventoryHeader.getWarehouseCode());
                inventoryHistoryHeader.setLocationCode(inventoryHeader.getLocationCode());
                inventoryHistoryHeader.setZoneCode(inventoryHeader.getZoneCode());
                inventoryHistoryHeader.setContainerStatus(inventoryHeader.getContainerStatus());
                inventoryHistoryHeader.setCompanyCode(inventoryHeader.getCompanyCode());
                inventoryHistoryHeader.setStack(inventoryHeader.getStack());
                inventoryHistoryHeader.setMaterialCode(inventoryHeader.getMaterialCode());
                inventoryHistoryHeader.setMaterialName(inventoryHeader.getMaterialName());
                inventoryHistoryHeader.setMaterialSpec(inventoryHeader.getMaterialSpec());
                inventoryHistoryHeader.setMaterialUnit(inventoryHeader.getMaterialUnit());
                inventoryHistoryHeader.setLevel(inventoryHeader.getLevel());
                inventoryHistoryHeader.setColor(inventoryHeader.getColor());
                inventoryHistoryHeader.setProPackaging(inventoryHeader.getProPackaging());
                inventoryHistoryHeader.setProductSize(inventoryHeader.getProductSize());
                inventoryHistoryHeader.setProductSchedule(inventoryHeader.getProductSchedule());
                inventoryHistoryHeader.setBatch(inventoryHeader.getBatch());
                if (!receiptHeaderList.isEmpty()) {
                    inventoryHistoryHeader.setReceiptCode(receiptHeaderList.get(0).getCode());
                }
                if (!shipmentHeaderList.isEmpty()) {
                    inventoryHistoryHeader.setShipmentCode(shipmentHeaderList.get(0).getCode());
                    inventoryHistoryHeader.setOrderCode(shipmentHeaderList.get(0).getReferCode());
                    inventoryHistoryHeader.setOrderName(shipmentHeaderList.get(0).getCustomerName());
                }
                inventoryHistoryHeader.setTotalQty(BigDecimal.ZERO);
                inventoryHistoryHeader.setTotalLines(0);
                inventoryHistoryHeader.setUserDef1("销售出库");
                inventoryHistoryHeaderService.save(inventoryHistoryHeader);
            }
            BigDecimal boxQty = new BigDecimal(0);
            BigDecimal totalQty = new BigDecimal(0);
            List<String> boxCodeList = new ArrayList<>();
            int totalLines = 0;
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper1 = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper1.eq(InventoryDetail::getInventoryHeaderId, inventoryHeader.getId());
            List<InventoryDetail> inventoryDetailList1 = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper1);
            addHistoryInventoryDetail(inventoryDetailList1, inventoryHistoryHeader, "销售出库");
            for (InventoryDetail inventoryDetail : inventoryDetailList1) {
                totalQty = totalQty.add(inventoryDetail.getQty());
                totalLines++;
                if (!boxCodeList.contains(inventoryDetail.getBoxCode())) {
                    boxCodeList.add(inventoryDetail.getBoxCode());
                }
            }
            boxQty = new BigDecimal(boxCodeList.size());
            inventoryHistoryHeader.setBoxQty(boxQty);
            inventoryHistoryHeader.setTotalQty(totalQty);
            inventoryHistoryHeader.setTotalLines(totalLines);
            inventoryHistoryHeaderService.updateById(inventoryHistoryHeader);
        }

        if (!shipmentHeaderList.isEmpty()) {
            List<Integer> shipmentHeaderIdList = shipmentHeaderList.stream().map(ShipmentHeader::getId).collect(Collectors.toList());
            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.in(ShipmentDetail::getShipmentId, shipmentHeaderIdList);
            List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
            //删除shipmentDetailList里面物料,色号不包含inventoryHeaderList里面物料,色号的数据
            shipmentDetailList.removeIf(S ->
                    !inventoryHeaderList.stream()
                            .anyMatch(header ->
                                    header.getMaterialCode().equals(S.getMaterialCode()) &&
                                            header.getColor().equals(S.getColor()) &&
                                            header.getLevel().equals(S.getLevel()) &&
                                            header.getProductSchedule().equals(S.getProductSchedule()) &&
                                            header.getProPackaging().equals(S.getProPackaging()) &&
                                            header.getBatch().equals(S.getBatch())
                            )
            );
            for (ShipmentDetail shipmentDetail : shipmentDetailList) {
                BigDecimal qty = shipmentDetail.getQty();
                shipmentDetail.setTaskQty(qty);
            }
            result = shipmentDetailService.updateBatchById(shipmentDetailList);
            if (!result) {
                throw new ServiceException("更新出库表单详情头失败");
            }
        }

        List<Integer> inventoryDetailIds = inventoryDetailList.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        result = inventoryDetailService.removeByIds(inventoryDetailIds);
        if (!result) {
            throw new ServiceException("删除库存失败");
        }

        List<Integer> inventoryHeaderIds = inventoryHeaderList.stream().map(InventoryHeader::getId).collect(Collectors.toList());
        result = inventoryHeaderService.removeByIds(inventoryHeaderIds);
        if (!result) {
            throw new ServiceException("删除库存失败");
        }

        for (Container container : containerList)
        {
            containerService.updateLocationCodeAndStatus(container.getCode(), "", QuantityConstant.STATUS_CONTAINER_EMPTY, warehouseCode);
            if (container.getCode().contains(QuantityConstant.VIRTUAL_PALLET_TYPE)) {

                containerService.removeById(container.getId());
            }
        }

        if (inventoryHeaders.size()<=inventoryHeaderList.size()){
            location.setContainerCode("");
            location.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
            location.setShipmentCode("");
            location.setStack(0);
            location.setContainerSpec(0);
            if (location.getCode().contains(QuantityConstant.VIRTUAL_PALLET_TYPE)) {
                if (!locationService.removeById(location.getId())) {
                    throw new ServiceException("删除库位失败");
                }
            }else {
                if (!locationService.updateById(location)) {
                    throw new ServiceException("批量更新库位失败");
                }
            }
        }



        if (!shipmentHeaderList.isEmpty()) {
            shipmentHeaderList.removeIf(S -> S.getPseudoShipment());
            if (shipmentHeaderList.size() == 0) {
                shipmentHeaderList.add(shipmentHeader1);
            }
            if (shipmentHeaderList.size() > 0 && !shipmentHeader1.getCode().contains(QuantityConstant.SHIPMENT_TYPE_PS)) {
                if (shipmentHeaderList.size() > 1) {
                    throw new ServiceException("释放有问题 先卡住查bug 请赶紧联系下开发人员");
                }
                pdaService.createSPShipment(shipmentHeaderList,inventoryDetailList.size()+"",true); // 创建销售出库单
            }
        }

        return AjaxResult.success("释放库存成功 出库单号" + shipmentHeader1.getCode()).setData(shipmentHeader1.getCode());
    }

    @Override
    public AjaxResult tileTheLibrary(String location, String chipCode) {

        String[] chipCodes = Convert.toStrArray(chipCode);

        Location locationByCode = locationService.getLocationByCode(location);
        if (locationByCode==null)
        {
            return AjaxResult.error("库位不存在");
        }

        //先查出片码对应的库存
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.in(InventoryDetail::getChipCode1,chipCodes).eq(InventoryDetail::getZoneCode,QuantityConstant.ZONE_E);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);

        List<InventoryDetail> details = inventoryDetails.stream().filter(i -> i.getLocationCode().equals(location)).collect(Collectors.toList());
        if (!details.isEmpty()){
            return AjaxResult.error("当前物流已存在此库位"+details);
        }
        //把inventoryDetails里面的inventoryHeaderId提取为一个list<String>并且不重复
        List<String> locationCodes = inventoryDetails.stream()
                .map(InventoryDetail::getLocationCode)  // 将TaskDetail对象转换为ReceiptId
                .distinct()                  // 去除重复的ReceiptId
                .collect(Collectors.toList());// 收集到List中


        //根据库存的库位查询有多少 如果是唯一就不清空库位 如果一个库位绑定了多个库存就不动
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.in(InventoryHeader::getLocationCode,locationCodes);
        List<InventoryHeader> inventoryHeaders = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);

        //把inventoryHeaders里面的LocationCode提取为一个list<String>并且不重复
        List<String> LocationCodes = inventoryHeaders.stream()
                .map(InventoryHeader::getLocationCode)  // 将TaskDetail对象转换为ReceiptId
                .distinct()                  // 去除重复的ReceiptId
                .collect(Collectors.toList());// 收集到List中

        List<InventoryHeader> inventoryHeaderList = inventoryHeaders.stream()
                .filter(header -> inventoryDetails.stream()
                        .anyMatch(detail -> detail.getInventoryHeaderId().equals(header.getId())))
                .collect(Collectors.toList());


        List<String> containerCodes = inventoryHeaderList.stream()
                .map(InventoryHeader::getContainerCode)  // 将TaskDetail对象转换为ReceiptId
                .distinct()                  // 去除重复的ReceiptId
                .collect(Collectors.toList());// 收集到List中


        if (inventoryHeaders.size()==chipCodes.length){
            //如果库位只绑定了一个库存头 那么就清空库位
            LambdaUpdateWrapper<Location> locationLambdaUpdateWrapper = Wrappers.lambdaUpdate();
            locationLambdaUpdateWrapper.eq(Location::getCode,inventoryHeaders.get(0).getLocationCode()).set(Location::getContainerCode,"");
            locationService.update(locationLambdaUpdateWrapper);
        }


        //修改库存头的库位
        LambdaUpdateWrapper<Container> containerLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        containerLambdaUpdateWrapper.in(Container::getCode,containerCodes).set(Container::getLocationCode,location);
        containerService.update(containerLambdaUpdateWrapper);

        inventoryDetails.forEach(i->i.setLocationCode(location));
        if (!inventoryDetailService.updateBatchById(inventoryDetails))
        {
            throw new ServiceException("修改库存明细失败");
        }

        inventoryHeaderList.forEach(i->i.setLocationCode(location));
        if (!inventoryHeaderService.updateBatchById(inventoryHeaderList))
        {
            throw new ServiceException("修改库存头失败");
        }

        //让传过来的location绑定一下容器
        LambdaUpdateWrapper<Location> locationLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        locationLambdaUpdateWrapper.eq(Location::getCode,location).set(Location::getContainerCode,inventoryHeaderList.get(0).getContainerCode());
        if (!locationService.update(locationLambdaUpdateWrapper))
        {
            throw new ServiceException("修改库位失败");
        }

        for (String d:LocationCodes)
        {
            Location code = locationService.getLocationByCode(d);
            if (code.getCode().contains("LS"))
            {
                //如果是临时库位就删除
                if (!locationService.removeById(code))
                {
                    throw new ServiceException("删除库位失败");
                }
            }
        }

        return  AjaxResult.success("移库成功");

    }

    @Override
    @Transactional
    public AjaxResult moveTheLibraryInsideTheFlatLibrary(String ContainerCode, String column) {
        //1.搜索新库位
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper5 = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper5.eq(Location::getIRow, column).eq(Location::getZoneCode, QuantityConstant.ZONE_PINGKU)
                .eq(Location::getContainerCode, QuantityConstant.EMPTY_STRING).last(" limit 1");
        Location toLocation = locationService.getOne(locationLambdaQueryWrapper5);
        if (toLocation == null) {
            return AjaxResult.error("批量移库 目地库位为空");
        }
        String toLocationCode = toLocation.getCode();
        if (StringUtils.isNotEmpty(toLocation.getContainerCode())) {
            return AjaxResult.error("批量移库 目地库位已经有托盘:" + toLocation.getContainerCode());
        }


        //1.根据容器查库位 原库位
        //修改容器绑定新库位
        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getCode,ContainerCode);
        Container container = containerService.getOne(containerLambdaQueryWrapper);
        if (container==null)
        {
            return AjaxResult.error("库位不存在");
        }
        //原库位
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getCode,container.getLocationCode());
        Location location = locationService.getOne(locationLambdaQueryWrapper);

        List<InventoryHeader> inventoryHeaderList= new ArrayList<>();
        List<Integer> inventoryHeaderIds =new ArrayList<>();

        if (location!=null)
        {
            //2.拿到容器查库存
            //先查出容器对应的库存
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getLocationCode,location.getCode()).eq(InventoryHeader::getZoneCode,QuantityConstant.ZONE_PINGKU);
            inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
            inventoryHeaderIds = inventoryHeaderList.stream().map(InventoryHeader::getId).collect(Collectors.toList());

        }else
        {
            //2.拿到容器查库存
            //先查出容器对应的库存
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode,container.getCode()).eq(InventoryHeader::getZoneCode,QuantityConstant.ZONE_PINGKU)
                    .last(" limit 1");
            InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
            if (inventoryHeader==null)
            {
                throw new ServiceException("库位不存在库存");
            }
            //原库位
            locationLambdaQueryWrapper = Wrappers.lambdaQuery();
            locationLambdaQueryWrapper.eq(Location::getCode,inventoryHeader.getLocationCode());
            Location location1 = locationService.getOne(locationLambdaQueryWrapper);
            if (location1!=null)
            {
                    throw new ServiceException("库位不存在库存");
            }
            inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getLocationCode,location1.getCode()).eq(InventoryHeader::getZoneCode,QuantityConstant.ZONE_PINGKU);
            inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
            inventoryHeaderIds = inventoryHeaderList.stream().map(InventoryHeader::getId).collect(Collectors.toList());

        }


        if (inventoryHeaderIds.isEmpty())
        {
            throw new ServiceException("库位不存在库存");
        }
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.in(InventoryDetail::getInventoryHeaderId,inventoryHeaderIds);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);

        inventoryHeaderList.forEach(i->i.setLocationCode(toLocationCode));
        inventoryHeaderService.updateBatchById(inventoryHeaderList);
        inventoryDetails.forEach(i->i.setLocationCode(toLocationCode));
        inventoryDetailService.updateBatchById(inventoryDetails);


        //修改容器绑定新库位 老库位改新库位
        LambdaUpdateWrapper<Container> containerLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        containerLambdaUpdateWrapper.eq(Container::getLocationCode,location.getCode()).set(Container::getLocationCode,toLocationCode);
        if (!containerService.update(containerLambdaUpdateWrapper))
        {
            throw new ServiceException("修改容器组失败");
        }

        //修改新库位信息
        toLocation.setContainerCode(ContainerCode);
        toLocation.setShipmentCode(location.getShipmentCode());
        if (!locationService.updateById(toLocation))
        {
            throw new ServiceException("修改库位失败");
        }


        //原库位清空
        location.setShipmentCode("");
        location.setContainerCode("");
        if (location.getContainerCode().contains("LS"))
        {
            if (!locationService.removeById(location.getId()))
            {
                throw new ServiceException("删除原库位失败");
            }
        }else
        {
            if (!locationService.updateById(location))
            {
                throw new ServiceException("修改原库位失败");
            }
        }

        return AjaxResult.success("批量移库成功");
    }

    @Override
    public AjaxResult queryTheFragmentDetails(String locationCode, String materialCode, String materialName, String boxCode, String chipCode) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(StringUtils.isNotEmpty(locationCode),InventoryDetail::getLocationCode,locationCode)
                .like(StringUtils.isNotEmpty(materialCode),InventoryDetail::getMaterialCode,materialCode)
                .like(StringUtils.isNotEmpty(materialName),InventoryDetail::getMaterialName,materialName)
                .eq(InventoryDetail::getZoneCode,QuantityConstant.ZONE_E)
                .eq(StringUtils.isNotEmpty(boxCode), InventoryDetail::getBoxCode, boxCode)
                .eq(StringUtils.isNotEmpty(chipCode), InventoryDetail::getChipCode1, chipCode)
                .orderByDesc(InventoryDetail::getLastUpdated);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetails.isEmpty())
        {
            return AjaxResult.error("没有找到库存明细");
        }
        return AjaxResult.success(inventoryDetails);
    }

}