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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.huaheng.api.erp.service.IErpBackService;
import com.huaheng.api.srm.domain.ICSPOArrive;
import com.huaheng.api.srm.domain.ICSPOArriveDetail;
import com.huaheng.api.srm.domain.ICSinspection;
import com.huaheng.api.srm.service.SrmReceiptService;
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.Wrappers;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.page.PageDomain;
import com.huaheng.framework.web.page.TableDataInfo;
import com.huaheng.framework.web.page.TableSupport;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptDetailHistory.domain.ReceiptDetailHistory;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import com.huaheng.pc.srm.domain.SrmDetail;
import com.huaheng.pc.srm.domain.SrmDetailHistory;
import com.huaheng.pc.srm.domain.SrmHeader;
import com.huaheng.pc.srm.domain.SrmHeaderHistory;
import com.huaheng.pc.srm.mapper.SrmHeaderMapper;
import com.huaheng.pc.system.role.domain.Role;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.u8.domain.ICSReceiptDetail;
import com.huaheng.pc.u8.domain.ICSReceiptHeader;
import com.huaheng.pc.u8.domain.ICSReceiptModel;
import com.huaheng.pc.u8.service.ICSReceiptAPIService;
import com.huaheng.pc.u8.service.ICSReceiptDetailService;
import com.huaheng.pc.u8.service.ICSReceiptHeaderService;
import io.swagger.models.auth.In;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class SrmHeaderService extends ServiceImpl<SrmHeaderMapper, SrmHeader> {

    private static final Logger logger = LoggerFactory.getLogger(SrmHeaderService.class);

    @Resource
    private SrmHeaderService srmHeaderService;
    @Resource
    private SrmHeaderMapper srmHeaderMapper;
    @Resource
    private SrmDetailService srmDetailService;
    @Resource
    private ICSASNService icsasnService;
    @Resource
    private ICSReceiptAPIService icsReceiptAPIService;
    @Resource
    private IUserService userService;
    @Resource
    private WarehouseU8Service warehouseU8Service;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ICSReceiptHeaderService icsReceiptHeaderService;
    @Resource
    private ICSReceiptDetailService icsReceiptDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private SrmDetailHistoryService srmDetailHistoryService;
    @Resource
    private SrmHeaderHistoryService srmHeaderHistoryService;
    @Resource
    private SrmReceiptService srmReceiptService;
    @Resource
    private IErpBackService erpBackService;

    public SrmHeader selectFirstModel(SrmHeader srmHeader) {
        LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getId()), SrmHeader::getId, srmHeader.getId());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getCode()), SrmHeader::getCode, srmHeader.getCode());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getWarehouseCode()), SrmHeader::getWarehouseCode, srmHeader.getWarehouseCode());
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    public List<SrmHeader> selectBySTNO(List<String> STNOS) {
        if (STNOS != null && STNOS.size() > 0) {
            String[] split = STNOS.toArray(new String[0]);
            LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(SrmHeader::getCode, split);
            return this.list(queryWrapper);
        }
        return null;
    }

    /**
     * 根据条件查询list
     *
     * @param srmHeader
     * @return
     */
    public List<SrmHeader> selectListAndTime(SrmHeader srmHeader,String createdBegin,String createdEnd) {
        LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()), SrmHeader::getCode, srmHeader.getCode());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getEnable()), SrmHeader::getEnable, srmHeader.getEnable());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getCheckCode()), SrmHeader::getCheckCode, srmHeader.getCheckCode());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getSupplierCode()), SrmHeader::getSupplierCode, srmHeader.getSupplierCode());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getMark()), SrmHeader::getMark, srmHeader.getMark());
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getUserDef2()), SrmHeader::getUserDef2, srmHeader.getUserDef2());
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getUserDef1()), SrmHeader::getUploadStatus, Convert.toIntArray(srmHeader.getUserDef1()));
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getLastUpdateBy()), SrmHeader::getLastUpdateBy,srmHeader.getLastUpdateBy());
        queryWrapper.eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        queryWrapper.gt(StringUtils.isNotEmpty(createdBegin), SrmHeader::getCreated, createdBegin)
                .lt(StringUtils.isNotEmpty(createdEnd), SrmHeader::getCreated, createdEnd);
        if (StringUtils.isNotEmpty(srmHeader.getProductionBatch())) {
            queryWrapper.apply(" id in(select headerId from srmdetail where productionBatch ='" + srmHeader.getProductionBatch() + "' )");
        }
        queryWrapper.orderByDesc(SrmHeader::getId);

        return this.list(queryWrapper);
    }
    /**
     * 根据条件查询list
     *
     * @param srmHeader
     * @return
     */
    public List<SrmHeader> selectDaijianListAndTime(SrmHeader srmHeader,String createdBegin,String createdEnd) {
        LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()), SrmHeader::getCode, srmHeader.getCode());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getEnable()), SrmHeader::getEnable, srmHeader.getEnable());
        queryWrapper.in( SrmHeader::getCheckCode,0,1);
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getUploadStatus()), SrmHeader::getUploadStatus,srmHeader.getUploadStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getSupplierCode()), SrmHeader::getSupplierCode, srmHeader.getSupplierCode());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getMark()), SrmHeader::getMark, srmHeader.getMark());
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getUserDef2()), SrmHeader::getUserDef2, srmHeader.getUserDef2());
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getLastUpdateBy()), SrmHeader::getLastUpdateBy,srmHeader.getLastUpdateBy());
        queryWrapper.eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        queryWrapper.gt(StringUtils.isNotEmpty(createdBegin), SrmHeader::getCreated, createdBegin)
                .lt(StringUtils.isNotEmpty(createdEnd), SrmHeader::getCreated, createdEnd);
        if (StringUtils.isNotEmpty(srmHeader.getProductionBatch())) {
            queryWrapper.apply(" id in(select headerId from srmdetail where productionBatch ='" + srmHeader.getProductionBatch() + "' )");
        }
        queryWrapper.orderByDesc(SrmHeader::getId);

        return this.list(queryWrapper);
    }

    /**
     * 根据条件查询list
     *
     * @param srmHeader
     * @return
     */
    public List<SrmHeader> selectList(SrmHeader srmHeader) {
        LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()), SrmHeader::getCode, srmHeader.getCode());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getEnable()), SrmHeader::getEnable, srmHeader.getEnable());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getCheckCode()), SrmHeader::getCheckCode, srmHeader.getCheckCode());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getSupplierCode()), SrmHeader::getSupplierCode, srmHeader.getSupplierCode());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getMark()), SrmHeader::getMark, srmHeader.getMark());
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getUserDef2()), SrmHeader::getUserDef2, srmHeader.getUserDef2());
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getUserDef1()), SrmHeader::getUploadStatus, Convert.toIntArray(srmHeader.getUserDef1()));
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getLastUpdateBy()), SrmHeader::getLastUpdateBy,srmHeader.getLastUpdateBy());
        queryWrapper.eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        if (StringUtils.isNotEmpty(srmHeader.getProductionBatch())) {
            queryWrapper.apply(" id in(select headerId from srmdetail where productionBatch ='" + srmHeader.getProductionBatch() + "' )");
        }
        queryWrapper.orderByDesc(SrmHeader::getId);

        return this.list(queryWrapper);
    }
    /**
     * 根据条件查询list
     *
     * @param srmHeader
     * @return
     */
    public List<SrmHeader> selectListView(SrmHeader srmHeader,String createdBegin,String createdEnd) {
        LambdaQueryWrapper<SrmHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()), SrmHeader::getCode, srmHeader.getCode());
        queryWrapper.ge(StringUtils.isNotNull(srmHeader.getEnable()), SrmHeader::getEnable, srmHeader.getEnable());
        queryWrapper.in( SrmHeader::getCheckCode, 1,2);
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getCheckCode()),SrmHeader::getCheckCode,srmHeader.getCheckCode());
        queryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getSupplierCode()), SrmHeader::getSupplierCode, srmHeader.getSupplierCode());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getMark()), SrmHeader::getMark, srmHeader.getMark());
        queryWrapper.like(StringUtils.isNotEmpty(srmHeader.getUserDef2()), SrmHeader::getUserDef2, srmHeader.getUserDef2());
//        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getUserDef1()), SrmHeader::getUploadStatus, Convert.toIntArray(srmHeader.getUserDef1()));
        queryWrapper.in(StringUtils.isNotEmpty(srmHeader.getLastUpdateBy()), SrmHeader::getLastUpdateBy,srmHeader.getLastUpdateBy());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getQuatityStatus()), SrmHeader::getQuatityStatus,srmHeader.getQuatityStatus());
        queryWrapper.eq(StringUtils.isNotNull(srmHeader.getReceiptStatus()), SrmHeader::getReceiptStatus,srmHeader.getReceiptStatus());
        queryWrapper.eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        queryWrapper.gt(StringUtils.isNotEmpty(createdBegin), SrmHeader::getCreated, createdBegin)
                .lt(StringUtils.isNotEmpty(createdEnd), SrmHeader::getCreated, createdEnd);
        if (StringUtils.isNotEmpty(srmHeader.getProductionBatch())) {
            queryWrapper.apply(" id in(select headerId from srmdetail where productionBatch ='" + srmHeader.getProductionBatch() + "' )");
        }
        queryWrapper.orderByDesc(SrmHeader::getId);

        return this.list(queryWrapper);
    }

    /**
     * 回传srm
     *
     * @return
     */
    @Transactional
    public AjaxResult returnSrmModel() {
        //1、查找所有需要回传的明细
        List<SrmDetail> details = new ArrayList<>();
        SrmDetail srmDetail = new SrmDetail();
        srmDetail.setUploadStatus1(0);
        srmDetail.setEnable(1);
        List<SrmDetail> srmDetailList = srmDetailService.selectList(srmDetail);

        srmDetail.setUploadStatus1(2);
        List<SrmDetail> srmDetails = srmDetailService.selectList(srmDetail);

        if (srmDetailList.isEmpty() && srmDetails.isEmpty()) {
            return AjaxResult.success("没有目标数据");
        }

        if (!srmDetailList.isEmpty()) {
            details.addAll(srmDetailList);
        }
        if (!srmDetails.isEmpty()) {
            details.addAll(srmDetails);
        }

        if (details != null && details.size() > 50) {
            details = details.subList(0, 49);
        }
        return this.confirmSRMModel(details);
    }


    /**
     * 回传实体
     *
     * @param details
     * @return
     */
    @Transactional
    public AjaxResult comfirmSrmModel(List<SrmDetail> details) {
        //单据根据单号分组,组建回传实体
        //根据code去重
//        Set<String> codes = new HashSet<>();
//        String warehouseCode = null;
//        for (SrmDetail detail : details) {
//            codes.add(detail.getCode());
//        }
//        warehouseCode = details.get(0).getWarehouseCode();
////        List<SrmDetail> newlist = details.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SrmDetail :: getCode))), ArrayList::new));
//
//        Integer number = 0;
//        for (String code : codes) {
//            List<ICSPOArrive> icspoArrives = new ArrayList<>();
//
//            List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
//            //将相同code的放一个list
//            icspoArriveDetails = createIcspoArriveDetail(details, code, icspoArriveDetails);
//            //创建ICSPOArrive
//            ICSPOArrive icspoArrive = createIcspoArrive(details, code, icspoArriveDetails);
//
//            icspoArrives.add(icspoArrive);
//            AjaxResult ajaxResult = icsasnService.returnICSPOArrive(icspoArrives, warehouseCode);
//            if (ajaxResult.hasErr()) {
//                for (SrmDetail detail : details) {
//                    if (detail.getCode().equals(code)) {
//                        detail.setUploadStatus1(2);
//                        srmDetailService.updateById(detail);
//                    }
//                }
//                number++;
//            } else {
//                for (SrmDetail detail : details) {
//                    if (detail.getCode().equals(code)) {
//                        detail.setUploadStatus1(1);
//                        srmDetailService.updateById(detail);
//                    }
//                }
//            }
//        }
//        if (number == codes.size()) {
//            return AjaxResult.error("失败");
//        }
        return AjaxResult.success("成功");

    }

    //组建回传实体
    public List<ICSPOArriveDetail> createIcspoArriveDetail(List<SrmDetail> details, String code, List<ICSPOArriveDetail> icspoArriveDetails) {
        for (SrmDetail item : details) {
            if (item.getCode().equals(code)) {
                ICSPOArriveDetail icspoArriveDetail = new ICSPOArriveDetail();
                icspoArriveDetail.setLOTNO(item.getLineCode());
                icspoArriveDetail.setWHCode(item.getUWarehouseCode());
                icspoArriveDetail.setReceiveQty(item.getConfirmQty());
                icspoArriveDetails.add(icspoArriveDetail);
            }
        }
        return icspoArriveDetails;
    }

    //组建回传实体
    public ICSPOArrive createIcspoArrive(List<SrmDetail> details, String code, List<ICSPOArriveDetail> icspoArriveDetails) {
        ICSPOArrive icspoArrive = new ICSPOArrive();
        icspoArrive.setUser(details.get(0).getLastUpdateBy());
        User user = userService.selectUserByLoginName(details.get(0).getLastUpdateBy());
        if (user != null) {
            icspoArrive.setMUSERName(user.getUserName());
        }
        WarehouseU8 warehouseWu = new WarehouseU8();
        warehouseWu.setUWarehouseCode(details.get(0).getUWarehouseCode());
        warehouseWu.setCompanyCode(details.get(0).getCompanyCode());
        warehouseWu.setWarehouseCode(details.get(0).getWarehouseCode());
        List<WarehouseU8> warehouseWulist = warehouseU8Service.getByDomain(warehouseWu);
        if (warehouseWulist != null && warehouseWulist.size() > 0) {
            warehouseWu = warehouseWulist.get(0);
            icspoArrive.setCompanyCode(warehouseWu.getUCompanyCode().substring(7, 10));
        } else {
            icspoArrive.setCompanyCode("");
        }

        icspoArrive.setMTime(DateUtils.getTime());
        icspoArrive.setASNNO(code);
        icspoArrive.setCompanyCode("");
        icspoArrive.setType(QuantityConstant.PLATFORM_SRM);
        icspoArrive.setDetails(icspoArriveDetails);
        return icspoArrive;
    }


    /**
     * srm拒收单、erp验退单
     * @return
     */
    @Transactional
    public AjaxResult returnSrmERPCheck() {
        try {
            returnSrmCheck();
        }catch (Exception e) {
        }
        returnERPCheck();

        return AjaxResult.success();
    }

    /**
     * erp验退单:只要查询不合格的数据
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult returnERPCheck() {
        //1、查找所有需要回传的明细
        LambdaQueryWrapper<SrmDetail> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SrmDetail::getCheckCode,2);
        wrapper.eq(SrmDetail::getUploadStatus1,1);
        wrapper.in(SrmDetail::getUploadStatus3,0);
        wrapper.eq(SrmDetail::getEnable,1);
        wrapper.gt(SrmDetail::getUnqualifiedQty,0);
        String startTime= DateUtils.getNowPreDays("yyyy-MM-dd", 10);
        wrapper.ge(SrmDetail::getCreated,startTime);
        List<SrmDetail>  details=srmDetailService.list(wrapper);
        if (details.isEmpty() ) {
            return AjaxResult.success("没有目标数据");
        }
        if (details != null && details.size() > 100) {
            details = details.subList(0, 99);
        }
        //过滤分开srmheader
        List<String> codes = details.stream().map(SrmDetail::getCode).distinct().collect(Collectors.toList());
        for (String code:codes){
            List<SrmDetail> srmDetails = details.stream().filter(srmDetail -> srmDetail.getCode().equals(code)).collect(Collectors.toList());
            if(srmDetails==null||srmDetails.size()==0){
                continue;
            }
            Integer headerId=srmDetails.get(0).getHeaderId();
            SrmHeader srmHeader=srmHeaderService.getById(headerId);
            AjaxResult ajaxResult=erpBackService.createErpYanTui(srmDetails,srmHeader);
            if (ajaxResult.hasErr()) {
                for (SrmDetail detail : srmDetails) {
                    detail.setUploadStatus3(2);
                    srmDetailService.updateById(detail);
                }
            } else {
                for (SrmDetail detail : srmDetails) {
                    detail.setUploadStatus3(1);
                    srmDetailService.updateById(detail);
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * srm拒收单:所有已质检数据
     * @return
     */
    @Transactional
    public AjaxResult returnSrmCheck() {
        //1、查找所有需要回传的明细
//        List<SrmDetail> details = new ArrayList<>();
//        SrmDetail srmDetail = new SrmDetail();
//        srmDetail.setCheckCode(2);
//        srmDetail.setUploadStatus1(1);
//        srmDetail.setUploadStatus2(0);
//        srmDetail.setEnable(1);
//        List<SrmDetail> srmDetailList = srmDetailService.selectList(srmDetail);
        LambdaQueryWrapper<SrmDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SrmDetail::getEnable, 1);
        queryWrapper.eq(SrmDetail::getCheckCode, 2);
        queryWrapper.eq(SrmDetail::getUploadStatus1, 1);
        queryWrapper.in(SrmDetail::getUploadStatus2, 0);
        String startTime= DateUtils.getNowPreDays("yyyy-MM-dd", 10);
        queryWrapper.ge(SrmDetail::getCreated,startTime);
        List<SrmDetail> srmDetailList = srmDetailService.list(queryWrapper);
//        srmDetail.setUploadStatus2(2);
//        List<SrmDetail> srmDetails = srmDetailService.selectList(srmDetail);
//        LambdaQueryWrapper<SrmDetail> queryWrapper1 = Wrappers.lambdaQuery();
//        queryWrapper1.eq(SrmDetail::getEnable, 1);
//        queryWrapper1.eq(SrmDetail::getCheckCode, 2);
//        queryWrapper1.eq(SrmDetail::getUploadStatus1, 1);
//        queryWrapper1.eq(SrmDetail::getUploadStatus2, 2);
//        queryWrapper1.ge(SrmDetail::getCreated,startTime);
//        List<SrmDetail> srmDetails = srmDetailService.list(queryWrapper);


        if (srmDetailList.isEmpty() ) {
            return AjaxResult.success("没有目标数据");
        }

//        if (!srmDetailList.isEmpty()) {
//            details.addAll(srmDetailList);
//        }
//        if (!srmDetails.isEmpty()) {
//            details.addAll(srmDetails);
//        }

        if (srmDetailList != null && srmDetailList.size() > 100) {
            srmDetailList = srmDetailList.subList(0, 99);
        }

        //组建回传实体
        List<ICSinspection> icSinspections = createICSinspection(srmDetailList);
        //srm拒收单
        AjaxResult ajaxResult = icsasnService.returnICSinspection(icSinspections, srmDetailList.get(0).getWarehouseCode());
        List<SrmDetail> srmDetailList1=new ArrayList<>();
        if (ajaxResult.hasErr()) {
            for (SrmDetail detail : srmDetailList) {
                SrmDetail srmDetail=new SrmDetail();
                srmDetail.setId(detail.getId());
                srmDetail.setUploadStatus2(2);
                srmDetailList1.add(srmDetail);
            }
            if(srmDetailList1!=null&&srmDetailList1.size()>0){
                srmDetailService.updateBatchById(srmDetailList1);
            }

            return AjaxResult.error("失败");


        } else {
            for (SrmDetail detail : srmDetailList) {
                SrmDetail srmDetail=new SrmDetail();
                srmDetail.setId(detail.getId());
                srmDetail.setUploadStatus2(1);
                srmDetailList1.add(srmDetail);
//                detail.setUploadStatus2(1);
//                srmDetailService.updateById(detail);
            }
            if(srmDetailList1!=null&&srmDetailList1.size()>0){
                srmDetailService.updateBatchById(srmDetailList1);
            }
            return AjaxResult.success("成功");
        }
    }

    //组建回传实体
    public List<ICSinspection> createICSinspection(List<SrmDetail> details) {
        List<ICSinspection> icSinspections = new ArrayList<>();
        for (SrmDetail detail : details) {
            ICSinspection icSinspection = new ICSinspection();
            icSinspection.setLotNo(detail.getLineCode());
            icSinspection.setYLOTQTY(detail.getQualifiedQty());
            icSinspection.setNLOTQTY(detail.getUnqualifiedQty());
            if (StringUtils.isEmpty(detail.getReason())) {
                icSinspection.setBadCause("");
            } else {
                icSinspection.setBadCause(detail.getReason());
            }
            if (StringUtils.isEmpty(detail.getCheckUser())) {
                icSinspection.setUser("");
                icSinspection.setMUSERName("");
            } else {
                icSinspection.setUser(detail.getCheckUser());
                User user = userService.selectUserByLoginName(detail.getCheckUser());
                if (user != null) {
                    icSinspection.setMUSERName(user.getUserName());
                }
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNull(detail.getCheckTime())) {
                String dt = simpleDateFormat.format(new Date());
                icSinspection.setMTime(dt);
            } else {
                String dt = simpleDateFormat.format(detail.getCheckTime());
                icSinspection.setMTime(dt);
            }
            icSinspections.add(icSinspection);
        }
        return icSinspections;
    }


    @Transactional(rollbackFor = Exception.class)
    public AjaxResult getReceipt(String[] ids) throws ParseException {
        //分别获取主单和未上传的明细
//        for (String id : ids) {
//            SrmHeader srmHeader = this.getById(id);
//            if (srmHeader == null) {
//                return AjaxResult.error("id为" + id + "的到货单不存在");
//            }
//            SrmDetail srmDetail = new SrmDetail();
//            //查询未提交的明细
//            srmDetail.setUploadStatus4(0);
//            srmDetail.setEnable(1);
//            srmDetail.setHeaderId(srmHeader.getId());
//            List<SrmDetail> srmDetailList = srmDetailService.selectList(srmDetail);
//            if (srmDetailList.isEmpty()) {
//                break;
//            }
//            //转换为接口实体,发送获取ERP单据
//            List<ICSPOArrive> icspoArrives = new ArrayList<>();
//            ICSPOArrive icspoArrive = new ICSPOArrive();
//            List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
//            for (SrmDetail item : srmDetailList) {
//                if (item.getConfirmQty().compareTo(new BigDecimal(0)) == 1) {
//                    ICSPOArriveDetail icspoArriveDetail = new ICSPOArriveDetail();
//                    icspoArriveDetail.setLOTNO(item.getLineCode());
//                    icspoArriveDetail.setReceiveQty(item.getConfirmQty());
//                    icspoArriveDetail.setWHCode(item.getUWarehouseCode());
//                    icspoArriveDetails.add(icspoArriveDetail);
//                }
//            }
//            if (icspoArriveDetails.isEmpty()) {
//                break;
//            }
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String dt = simpleDateFormat.format(new Date());
//            icspoArrive.setUser(ShiroUtils.getLoginName());
//            User user = userService.selectUserByLoginName(ShiroUtils.getLoginName());
//            if (user != null) {
//                icspoArrive.setMUSERName(user.getUserName());
//            }
//            icspoArrive.setCompanyCode(srmDetailList.get(0).getCompanyCode());
//            icspoArrive.setMTime(dt);
//            icspoArrive.setASNNO(srmHeader.getCode());
//            icspoArrive.setType("ERP");
//            icspoArrive.setDetails(icspoArriveDetails);
//            icspoArrives.add(icspoArrive);
//
//            //获取ERP到货单
//            AjaxResult ajaxResult = icsasnService.returnICSPOArrive(icspoArrives, srmHeader.getWarehouseCode());
//            List<ICSReceiptModel> icsReceiptModels = new ArrayList<>();
//            if (ajaxResult.hasErr()) {
//                // 获取到货单失败改为从数据库获取
//                icsReceiptModels = this.getICSReceiptModelList(srmHeader);
//            } else {
//                try {
//                    icsReceiptModels = this.parseResults(ajaxResult, srmHeader, srmDetailList.get(0).getCompanyCode());
//                } catch (Exception e) {
//                    this.updateErrMsg(srmHeader, e.getMessage());
//                    return AjaxResult.error(e.getMessage());
//                }
//            }
//            if (icsReceiptModels.size() == 0) {
//                this.updateErrMsg(srmHeader, ajaxResult.getMsg());
//                return ajaxResult;
//            }
//            for (ICSReceiptModel icsReceiptModel : icsReceiptModels) {
//                // 生成入库信息
//                try {
//                    ajaxResult = icsReceiptAPIService.insertICSReceipts(icsReceiptModel);
//                    if (ajaxResult.hasErr()) {
//                        return ajaxResult;
//                    }
//                } catch (Exception e) {
//                    this.updateErrMsg(srmHeader, e.getMessage());
//                    return AjaxResult.error(e.getMessage());
//                } finally {
//                    ajaxResult = this.saveResult(icsReceiptModel);
//                }
//                String json = JSON.toJSONString(icsReceiptModel);
//                logger.info("WMS到货单实体:{}", json);
//                if (ajaxResult.hasErr()) {
//                    this.updateErrMsg(srmHeader, ajaxResult.getMsg());
//                    return ajaxResult;
//                }
//            }
//            srmHeader.setUploadStatus(1);
//            boolean isMandatory = false;
//            for (SrmDetail item : srmDetailList) {
//                if (item.getConfirmQty().compareTo(new BigDecimal(0)) == 1) {
//                    item.setUploadStatus4(1);
//                    // 物料编码不为2开头认为不检
//                    Material materialall = materialService.getMaterialByCode(item.getMaterialCode(), ShiroUtils.getWarehouseCode());
//                    boolean flag = false;
//                    if (materialall != null && materialall.getQcFlag() != null) {
//                        flag = materialall.getQcFlag().equals("0");
//                    }
//                    if (!("2".equals(item.getMaterialCode().substring(0, 1))) || flag) {
//                        item.setCheckCode(2);
//                        // 不合格数量是否等于0
//                        if (item.getUnqualifiedQty().compareTo(new BigDecimal(0)) == 0) {
//                            item.setQualifiedQty(item.getConfirmQty());
//                            item.setCheckTime(new Date());
//                            item.setCheckUser(ShiroUtils.getLoginName());
//                        }
//                    } else {
//                        isMandatory = true;
//                    }
//                    srmDetailService.updateById(item);
//                }
//            }
//            if (isMandatory == false) {
//                srmHeader.setCheckCode(2);
//            }
//            this.updateById(srmHeader);
//            if (srmHeader.getCheckCode() < 2) {
//                //修改入库单,不显示
//                ReceiptHeader receiptHeader = new ReceiptHeader();
//                receiptHeader.setUserDef3(srmHeader.getCode());
//                List<ReceiptHeader> receiptHeaderlist = receiptHeaderService.getByDomain(receiptHeader);
//                if (receiptHeaderlist != null && receiptHeaderlist.size() > 0) {
//                    receiptHeader = receiptHeaderlist.get(0);
//                    receiptHeader.setDeleted(true);
//                    receiptHeaderService.updateById(receiptHeader);
//                }
//            }
//
//
//        }
        return AjaxResult.success("获取ERP到货单成功");
    }

    private List<ICSReceiptModel> getICSReceiptModelList(SrmHeader srmHeader) {
        List<ICSReceiptModel> icsReceiptModels = new ArrayList<>();
        if (srmHeader == null) {
            return icsReceiptModels;
        }
        // 获取erp到货单失败,从另一个接口获取
        ICSReceiptHeader query = new ICSReceiptHeader();
        query.setCTVCode(srmHeader.getCode());
        query.setWarehouseCode(srmHeader.getWarehouseCode());
        List<ICSReceiptHeader> icsReceiptHeaderList = icsReceiptHeaderService.selectModelList(query);
        if (icsReceiptHeaderList.isEmpty()) {
            return icsReceiptModels;
        }
        if (icsReceiptHeaderList.size() == 0) {
            return icsReceiptModels;
        }
        for (ICSReceiptHeader header : icsReceiptHeaderList) {
            LambdaQueryWrapper<ICSReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ICSReceiptDetail::getID, header.getID());
            lambdaQueryWrapper.eq(ICSReceiptDetail::getCompanyCode, header.getCompanyCode());
            lambdaQueryWrapper.eq(ICSReceiptDetail::getWarehouseCode, header.getWarehouseCode());
            List<ICSReceiptDetail> details = icsReceiptDetailService.list(lambdaQueryWrapper);
            ICSReceiptModel icsReceiptModel = new ICSReceiptModel();
            icsReceiptModel.setIcsReceiptHeader(header);
            icsReceiptModel.setIcsReceiptDetails(details);
            icsReceiptModels.add(icsReceiptModel);
        }
        return icsReceiptModels;
    }

//    public AjaxResult getReceiptlist(List<SrmDetail> srmDetails) throws ParseException {
//        if (srmDetails.isEmpty()) {
//            return AjaxResult.error("请勿提交空数据!");
//        }
//        List<ICSPOArrive> list = dataConversion(srmDetails);
//        if (list.size() == 0) {
//            return AjaxResult.success("已成功获取erp到货单!");
//        }
//        SrmHeader srmHeader = new SrmHeader();
//        srmHeader.setCode(srmDetails.get(0).getCode());
//        srmHeader.setWarehouseCode(srmDetails.get(0).getWarehouseCode());
//        srmHeader = this.selectFirstModel(srmHeader);
//        if (srmHeader == null) {
//            return AjaxResult.error("未查询到送货单主表!");
//        }
//        // 获取ERP到货单
//        AjaxResult ajaxResult = icsasnService.returnICSPOArrive(list, srmHeader.getWarehouseCode());
//        List<ICSReceiptModel> icsReceiptModels;
//        if (ajaxResult.hasErr()) {
//            this.updateErrMsg(srmHeader, ajaxResult.getMsg());
//            return ajaxResult;
//        } else {
//            // 解析erp到货单数据
//            try {
//                icsReceiptModels = this.parseResults(ajaxResult, srmHeader, srmDetails.get(0).getCompanyCode());
//                // 修改送货单主表
//                srmHeader.setUploadStatus(1);
//                this.updateById(srmHeader);
//            } catch (Exception e) {
//                this.updateErrMsg(srmHeader, e.getMessage());
//                return AjaxResult.error(e.getMessage());
//            }
//            if (icsReceiptModels.size() == 0) {
//                this.updateErrMsg(srmHeader, ajaxResult.getMsg());
//                return ajaxResult;
//            }
//            for (ICSReceiptModel icsReceiptModel : icsReceiptModels) {
//                // 生成入库信息
//                try {
//                    ajaxResult = icsReceiptAPIService.insertICSReceipts(icsReceiptModel);
//                    if (ajaxResult.hasErr()) {
//                        return ajaxResult;
//                    }
//                } catch (Exception e) {
//                    this.updateErrMsg(srmHeader, e.getMessage());
//                    return AjaxResult.error(e.getMessage());
//                } finally {
//                    ajaxResult = this.saveResult(icsReceiptModel);
//                }
//                String json = JSON.toJSONString(icsReceiptModel);
//                logger.info("WMS到货单实体:{}", json);
//                if (ajaxResult.hasErr()) {
//                    this.updateErrMsg(srmHeader, ajaxResult.getMsg());
//                    return ajaxResult;
//                }
//            }
//            // 是否必检查
//            boolean isMandatory = false;
//            for (SrmDetail item : srmDetails) {
//                if (item.getConfirmQty().compareTo(new BigDecimal(0)) == 1) {
//                    item.setUploadStatus4(1);
//                    // 物料编码不为2开头认为不检
//                    if (!("2".equals(item.getMaterialCode().substring(0, 1)))) {
//                        item.setCheckCode(2);
//                        // 不合格数量是否等于0
//                        if (item.getUnqualifiedQty().compareTo(new BigDecimal(0)) == 0) {
//                            item.setQualifiedQty(item.getConfirmQty());
//                            item.setCheckTime(new Date());
//                            item.setCheckUser(ShiroUtils.getLoginName());
//                        }
//                    } else {
//                        isMandatory = true;
//                    }
//                    srmDetailService.updateById(item);
//                }
//            }
//            if (isMandatory) {
//                //修改入库单,不显示
//                ReceiptHeader receiptHeader = new ReceiptHeader();
//                receiptHeader.setUserDef3(srmHeader.getCode());
//                receiptHeader.setReferCode(icsReceiptModels.get(0).getIcsReceiptHeader().getCCode());
//                List<ReceiptHeader> receiptHeaderList = receiptHeaderService.getByDomain(receiptHeader);
//                if (receiptHeaderList.size() > 0) {
//                    receiptHeaderList.forEach(item -> {
//                        item.setDeleted(true);
//                        receiptHeaderService.updateById(item);
//                    });
//                }
//            }
//        }
//        return AjaxResult.success("送货单提交成功!");
//    }

    public List<ICSPOArrive> dataConversion(List<SrmDetail> srmDetails) {
        //转换为接口实体,发送获取ERP单据
        List<ICSPOArrive> icspoArrives = new ArrayList<>();
        ICSPOArrive icspoArrive = new ICSPOArrive();
        List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
        for (SrmDetail srmDetail : srmDetails) {
            srmDetail.setUploadStatus4(0);
            if (srmDetail.getUploadStatus4() == 0 && srmDetail.getEnable() == 1 &&
                    srmDetail.getConfirmQty().compareTo(new BigDecimal(0)) == 1) {
                ICSPOArriveDetail icspoArriveDetail = new ICSPOArriveDetail();
                icspoArriveDetail.setLOTNO(srmDetail.getLineCode());
                icspoArriveDetail.setReceiveQty(srmDetail.getConfirmQty());
                icspoArriveDetail.setWHCode(srmDetail.getUWarehouseCode());
                icspoArriveDetails.add(icspoArriveDetail);
            } else {
                break;
            }
        }
        if (icspoArriveDetails.isEmpty()) {
            return icspoArrives;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dt = format.format(new Date());
        icspoArrive.setUser(ShiroUtils.getLoginName());
        User user = userService.selectUserByLoginName(ShiroUtils.getLoginName());
        if (user != null) {
            icspoArrive.setMUSERName(user.getUserName());
        }
        icspoArrive.setCompanyCode(srmDetails.get(0).getCompanyCode());
        icspoArrive.setMTime(dt);
        icspoArrive.setASNNO(srmDetails.get(0).getCode());
        icspoArrive.setType("ERP");
        icspoArrive.setDetails(icspoArriveDetails);
        icspoArrives.add(icspoArrive);
        return icspoArrives;
    }

    public Boolean updateErrMsg(SrmHeader srmHeader, String msg) {
        srmHeader.setUploadStatus(2);
        srmHeader.setUserDef2("");
        srmHeader.setUserDef1(msg);
        return this.updateById(srmHeader);
    }

    @Transactional
    public AjaxResult saveResult(ICSReceiptModel icsReceiptModel) {
        if (icsReceiptModel == null) {
            return AjaxResult.error("erp到货单接口返回结果为空值!");
        }
        // 查询数据是否已经存在
        ICSReceiptHeader query = new ICSReceiptHeader();
        query.setID(icsReceiptModel.getIcsReceiptHeader().getID());
        query.setCCode(icsReceiptModel.getIcsReceiptHeader().getCCode());
        query.setCTVCode(icsReceiptModel.getIcsReceiptHeader().getCTVCode());

        List<ICSReceiptHeader> list = icsReceiptHeaderService.selectModelList(query);
        if (list.size() > 0) {
            return AjaxResult.success();
        }
        // 先保存数据在进行处理
        Boolean result = icsReceiptHeaderService.save(icsReceiptModel.getIcsReceiptHeader());
        if (!result) {
            return AjaxResult.error("插入出库下发或回传主单失败!");
        }
        ICSReceiptDetail icsReceiptDetail = new ICSReceiptDetail();
        // 主单id与送货单号是否已经存在
        icsReceiptDetail.setID(query.getID());
        icsReceiptDetail.setCCode(query.getCCode());
        List<ICSReceiptDetail> icsReceiptDetailList = icsReceiptDetailService.selectModelList(icsReceiptDetail);
        // 以存在不进行新增
        if (icsReceiptDetailList.size() > 0) {
            return AjaxResult.success();
        }
        result = icsReceiptDetailService.saveBatch(icsReceiptModel.getIcsReceiptDetails());
        if (!result) {
            return AjaxResult.error("插入出库下发或回传单子表失败!");
        }
        return AjaxResult.success();
    }

    public List<ICSReceiptModel> parseResults(AjaxResult ajaxResult, SrmHeader srmHeader, String companyCode) {
        List<ICSReceiptModel> icsReceiptModels = new ArrayList<>();
        //调用ERP单据存储和wms采购到货单生成
        Object object = ajaxResult.getData();
        String ja = String.valueOf(object);

        List<Map> icsReceipts = JSON.parseObject(ja, ArrayList.class);
        for (Map map : icsReceipts) {
            ICSReceiptHeader icsReceiptHeader = new ICSReceiptHeader();
            if (StringUtils.isNotNull(map.get("cWhCode"))) {
                icsReceiptHeader.setCWHCode(map.get("cWhCode").toString());
            }
            if (StringUtils.isNotNull(map.get("cVenCode"))) {
                icsReceiptHeader.setCVenCode(map.get("cVenCode").toString());
            }
            if (StringUtils.isNotNull(map.get("POCode"))) {
                icsReceiptHeader.setDPODate(map.get("POCode").toString());
            }
            if (StringUtils.isNotNull(map.get("cMaker"))) {
                icsReceiptHeader.setCMaker(map.get("cMaker").toString());
            }
            if (StringUtils.isNotNull(map.get("cBusType"))) {
                icsReceiptHeader.setCBusType(map.get("cBusType").toString());
            }
            if (StringUtils.isNotNull(map.get("cDepCode"))) {
                icsReceiptHeader.setCDepCode(map.get("cDepCode").toString());
            }
            if (StringUtils.isNotNull(map.get("dPODate"))) {
                icsReceiptHeader.setDPODate(map.get("dPODate").toString());
            }
            if (StringUtils.isNotNull(map.get("cMemo"))) {
                icsReceiptHeader.setCMemo(map.get("cMemo").toString());
            }
            if (StringUtils.isNotNull(map.get("ID"))) {
                icsReceiptHeader.setID(Integer.valueOf(map.get("ID").toString()));
            }
            if (StringUtils.isNotNull(map.get("cCode"))) {
                icsReceiptHeader.setCCode(map.get("cCode").toString());
            }
            if (StringUtils.isNotNull(map.get("cPersonCode"))) {
                icsReceiptHeader.setCPersonCode(map.get("cPersonCode").toString());
            }
            if (StringUtils.isNotNull(map.get("cCode"))) {
                if (StringUtils.isEmpty(srmHeader.getUserDef2())) {
                    srmHeader.setUserDef2(map.get("cCode").toString());
                } else {
                    srmHeader.setUserDef2(srmHeader.getUserDef2() + ";" + map.get("cCode").toString());
                }
            }

            icsReceiptHeader.setType(QuantityConstant.U8_RECEIPT_TYPE_PII);
            icsReceiptHeader.setCTVCode(srmHeader.getCode());
            icsReceiptHeader.setSourcePlatform(QuantityConstant.PLATFORM_SRM);
            icsReceiptHeader.setCompanyCode(companyCode);
            icsReceiptHeader.setWarehouseCode(srmHeader.getWarehouseCode());

            List<Map> list = JSON.parseObject(map.get("list").toString(), ArrayList.class);
            List<ICSReceiptDetail> icsReceiptDetails = new ArrayList<>();

            for (Map item : list) {
                ICSReceiptDetail icsReceiptDetail = new ICSReceiptDetail();
                if (StringUtils.isNotNull(item.get("cInvCode"))) {
                    icsReceiptDetail.setCInvCode(item.get("cInvCode").toString());
                }
                if (StringUtils.isNotNull(item.get("iQuantity"))) {
                    icsReceiptDetail.setIQuantity(new BigDecimal(item.get("iQuantity").toString()));
                }
                if (StringUtils.isNotNull(item.get("POCode"))) {
                    icsReceiptDetail.setPOCode(item.get("POCode").toString());
                }
                if (StringUtils.isNotNull(item.get("Autoid"))) {
                    icsReceiptDetail.setAutoID(Integer.valueOf(item.get("Autoid").toString()));
                }
                if (StringUtils.isNotNull(item.get("POID"))) {
                    icsReceiptDetail.setPOID(Integer.valueOf(item.get("POID").toString()));
                }
                if (StringUtils.isNotNull(item.get("PODID"))) {
                    icsReceiptDetail.setPODID(Integer.valueOf(item.get("PODID").toString()));
                }
                if (StringUtils.isNotNull(item.get("cCode"))) {
                    icsReceiptDetail.setCCode(item.get("cCode").toString());
                }
                if (StringUtils.isNotNull(item.get("ID"))) {
                    icsReceiptDetail.setID(Integer.valueOf(item.get("ID").toString()));
                }
                if (StringUtils.isNotNull(item.get("irowno"))) {
                    icsReceiptDetail.setIrowno(Integer.valueOf(item.get("irowno").toString()));
                }
                if (StringUtils.isNotNull(item.get("cwhcode"))) {
                    icsReceiptDetail.setCWHCode(item.get("cwhcode").toString());
                }
                if (StringUtils.isNotNull(item.get("lotno"))) {
                    icsReceiptDetail.setLotno(item.get("lotno").toString());
                }
                icsReceiptDetail.setCompanyCode(icsReceiptHeader.getCompanyCode());
                icsReceiptDetail.setWarehouseCode(icsReceiptHeader.getWarehouseCode());
                icsReceiptDetails.add(icsReceiptDetail);
            }
            ICSReceiptModel icsReceiptModel = new ICSReceiptModel();
            icsReceiptModel.setIcsReceiptHeader(icsReceiptHeader);
            icsReceiptModel.setIcsReceiptDetails(icsReceiptDetails);
            icsReceiptModels.add(icsReceiptModel);
        }
        return icsReceiptModels;
    }

//    @Transactional(rollbackFor = Exception.class)
//    public AjaxResult confirm(String ids,String warehouseCode) {
//        if (StringUtils.isEmpty(ids)){
//            return AjaxResult.error("没有选中明细行");
//        }
//        Integer[] list = Convert.toIntArray(ids);
//        List<SrmDetail> srmDetails = new ArrayList<>();
//        Integer headId = 0;
//        List<SrmDetail> failedSrmDetail = new ArrayList<>();
//        for (Integer id : list) {
//            SrmDetail srmDetail = srmDetailService.getOne(new LambdaQueryWrapper<SrmDetail>().eq(SrmDetail::getId,id).eq(SrmDetail::getWarehouseCode,ShiroUtils.getWarehouseCode()));
//            if(srmDetail == null){
//                return AjaxResult.error("id为"+id+"的送货单明细不存在");
//            }
//            if(StringUtils.isEmpty(srmDetail.getUWarehouseCode())){
//                return AjaxResult.error("id为"+id+"的送货单明细没有选择仓库");
//            }
//            srmDetail.setConfirmQty(srmDetail.getQty());
//            srmDetail.setEnable(1);
//            srmDetail.setLastUpdateBy(ShiroUtils.getLoginName());
//            srmDetail.setLastUpdate(new Date());
//            // 如果不合格数量不等于0
//            if (srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0)) != 0){
//                failedSrmDetail.add(srmDetail);
//            }
//            srmDetailService.updateById(srmDetail);
//            srmDetails.add(srmDetail);
//            headId=srmDetail.getHeaderId();
//        }
//        SrmHeader srmHeader = srmHeaderService.getOne(new LambdaQueryWrapper<SrmHeader>().eq(SrmHeader::getId,headId).eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode()));
//        if (srmHeader != null) {
//            SrmDetail srmDetail = new SrmDetail();
//            srmDetail.setCode(srmHeader.getCode());
//            srmDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
//            List<SrmDetail> srmDetailList = srmDetailService.selectList(srmDetail);
//            srmDetail.setEnable(1);
//            List<SrmDetail> details = srmDetailService.selectList(srmDetail);
//            if (srmDetailList.size() == details.size()) {
//                srmHeader.setEnable(2);
//            } else {
//                srmHeader.setEnable(1);
//            }
//            srmHeader.setLastUpdate(new Date());
//            srmHeader.setLastUpdateBy(ShiroUtils.getLoginName());
//            srmHeaderService.updateById(srmHeader);
//        }
//        //默认质检的物料是不做到货的
//
//        //SRM提示到货失败的需要去定时任务
//        if(!srmDetails.isEmpty()){
//            AjaxResult result =srmHeaderService.confirmSRMModel(srmDetails);
//            if(result!=null&&result.hasErr()){
//                return result;
//            }
//        }
//        AjaxResult result = srmHeaderService.confirmU8Model(srmDetails);
//        if (result.hasErr()){
//            result.setMsg("系统自动获取erp到货单失败请手动获取:" + result.getMsg());
//            return result;
//        }
//        // 存在不合格数量直接调用退料单接口
//        if (failedSrmDetail.size() > 0){
//            srmDetailService.checkWhetherQualified(failedSrmDetail, srmHeader);
//        }
//        return AjaxResult.success("成功");
//    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmU8Model(List<SrmDetail> srmDetails) {
        if (srmDetails.isEmpty()) {
            return AjaxResult.error("请勿提交空数据!");
        }
        List<SrmDetail> icspoArrives = formU8EntitiesV(srmDetails);
        if (icspoArrives.size() == 0) {
            throw new ServiceException("已提交U8,不用重复提交");
        }
        SrmHeader srmHeader = this.getOne(new LambdaQueryWrapper<SrmHeader>().eq(SrmHeader::getCode, srmDetails.get(0).getCode()).eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        icspoArrives= icspoArrives.stream()
                .filter(detail -> detail.getQualifiedQty().compareTo(new BigDecimal(0))>0)
                .collect(Collectors.toList());
        if(icspoArrives==null||icspoArrives.size()==0){
            return AjaxResult.error("数据合格为空,不用提交t100");
        }
        //SRM提示到货失败的需要去定时任务
        if(!icspoArrives.isEmpty()){
            List<String> codes = icspoArrives.stream().map(SrmDetail::getPurchaseCode).distinct().collect(Collectors.toList());
            for (String code:codes) {
                List<SrmDetail> srmDetailPurchase = icspoArrives.stream().filter(srmDetaill -> srmDetaill.getPurchaseCode().equals(code)).collect(Collectors.toList());
                if (srmDetailPurchase == null || srmDetailPurchase.size() == 0) {
                    continue;
                }
                AjaxResult ajaxResult=srmReceiptService.sendErpRecipt(srmDetailPurchase, srmHeader);
                if(ajaxResult!=null&&ajaxResult.hasErr()){
                    throw new ServiceException(ajaxResult.getMsg());
                }
                String msg=ajaxResult.getMsg();
                String[] strsplit=msg.split(",");
                String refercode=strsplit[0];
                AjaxResult ajaxResult1=srmReceiptService.addWmsReceiptSrm(srmDetailPurchase,srmHeader, refercode);
                if(ajaxResult1!=null&&ajaxResult1.hasErr()){
//                    throw new ServiceException(ajaxResult1.getMsg());
                    continue;
                }

            }
//            AjaxResult ajaxResult=srmReceiptService.sendErpRecipt(icspoArrives, srmHeader);
//            if(ajaxResult!=null&&ajaxResult.hasErr()){
//                throw new ServiceException(ajaxResult.getMsg());
//            }
//            String msg=ajaxResult.getMsg();
//            String[] strsplit=msg.split(",");
//            String refercode=strsplit[0];
//            AjaxResult ajaxResult1=srmReceiptService.addWmsReceiptSrm(icspoArrives,srmHeader, refercode);
//            if(ajaxResult1!=null&&ajaxResult1.hasErr()){
//                throw new ServiceException(ajaxResult1.getMsg());
//            }
//            return ajaxResult1;

        }

//        srmHeader.setUploadStatus(1);
//        this.updateById(srmHeader);
        return AjaxResult.success("送货单提交成功!");
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmU8ModelSrm(List<SrmDetail> details,String code,String warehouseCode) {
        List<ICSPOArrive> icspoArrives = new ArrayList<>();
        List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
        //将相同code的放一个list
        icspoArriveDetails = createIcspoArriveDetail(details, code, icspoArriveDetails);
        //创建ICSPOArrive
        ICSPOArrive icspoArrive = createIcspoArrive(details, code, icspoArriveDetails);

        icspoArrives.add(icspoArrive);
        AjaxResult ajaxResult = icsasnService.returnICSPOArrive(icspoArrives, warehouseCode);
        if (ajaxResult.hasErr()) {
            for (SrmDetail detail : details) {
                if (detail.getCode().equals(code)) {
                    detail.setUploadStatus1(2);
                    srmDetailService.updateById(detail);
                }
            }
        } else {
            for (SrmDetail detail : details) {
                if (detail.getCode().equals(code)) {
                    detail.setUploadStatus1(1);
                    srmDetailService.updateById(detail);
                }
            }
        }
        return AjaxResult.success("回传SRM成功");
    }
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmU8Model1(List<SrmDetail> srmDetails) {
        if (srmDetails.isEmpty()) {
            return AjaxResult.error("请勿提交空数据!");
        }
        List<ICSPOArrive> icspoArrives = formU8Entities(srmDetails);
        if (icspoArrives.size() == 0) {
            throw new ServiceException("已提交U8,不用重复提交");
        }
        SrmHeader srmHeader = this.getOne(new LambdaQueryWrapper<SrmHeader>().eq(SrmHeader::getCode, srmDetails.get(0).getCode()).eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        List<SrmDetail> srmDetailsAll = srmDetailService.list(new LambdaQueryWrapper<SrmDetail>().eq(SrmDetail::getCode, srmHeader.getCode()).eq(SrmDetail::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        // 获取ERP到货单
        AjaxResult ajaxResult = icsasnService.returnICSPOArrive(icspoArrives, ShiroUtils.getWarehouseCode());
        String JsonParam = JSON.toJSONString(ajaxResult);
        logger.info("获取ERP到货单1:"+JsonParam);
//        AjaxResult ajaxResult =new AjaxResult();
//        ajaxResult.setCode(200);
//        ajaxResult.setData("[{\"cWhCode\":null,\"cVenCode\":\"990001\",\"POCode\":null,\"cMaker\":\"司海蛟\",\"cBusType\":\"普通采购\",\"cDepCode\":\"300303\",\"dPODate\":\"2023-11-21\",\"cMemo\":null,\"ID\":\"1000050965\",\"cPersonCode\":\"\",\"cCode\":\"2023111373\",\"list\":[{\"cInvCode\":\"20800000289\",\"iQuantity\":100.0,\"cBatch\":null,\"POCode\":\"J2023100016\",\"PORow\":\"1\",\"Autoid\":\"1000187038\",\"POID\":\"1000054347\",\"PODID\":\"1000186553\",\"ID\":\"1000050965\",\"cItemName\":null,\"cCode\":\"2023111373\",\"irowno\":\"1\",\"cwhcode\":\"02\",\"lotno\":\"J202310001600100001\"}]}]");
//       ajaxResult.setData("[{\"cWhCode\":null,\"cVenCode\":\"250026\",\"POCode\":null,\"cMaker\":\"周培杰\",\"cBusType\":\"普通采购\",\"cDepCode\":\"1002\",\"dPODate\":\"2024-04-25\",\"cMemo\":null,\"ID\":\"1000021919\",\"cPersonCode\":\"\",\"cCode\":\"2024040289\",\"list\":[{\"cInvCode\":\"12005000426\",\"iQuantity\":1.0,\"cBatch\":null,\"POCode\":\"HH-CSG2024041903\",\"PORow\":\"2\",\"Autoid\":\"1000124265\",\"POID\":\"1000020960\",\"PODID\":\"1000122713\",\"ID\":\"1000021919\",\"cItemName\":null,\"cCode\":\"2024040289\",\"irowno\":\"1\",\"cwhcode\":\"10\",\"lotno\":\"HH-CSG202404190300200001\"},{\"cInvCode\":\"12005000426\",\"iQuantity\":1.0,\"cBatch\":null,\"POCode\":\"HH-CSG2024041903\",\"PORow\":\"1\",\"Autoid\":\"1000124266\",\"POID\":\"1000020960\",\"PODID\":\"1000122712\",\"ID\":\"1000021919\",\"cItemName\":null,\"cCode\":\"2024040289\",\"irowno\":\"2\",\"cwhcode\":\"10\",\"lotno\":\"HH-CSG202404190300100001\"},{\"cInvCode\":\"12005000426\",\"iQuantity\":1.0,\"cBatch\":null,\"POCode\":\"HH-CSG2024041903\",\"PORow\":\"2\",\"Autoid\":\"1000124267\",\"POID\":\"1000020960\",\"PODID\":\"1000122713\",\"ID\":\"1000021919\",\"cItemName\":null,\"cCode\":\"2024040289\",\"irowno\":\"3\",\"cwhcode\":\"10\",\"lotno\":\"HH-CSG202404190300200002\"}]}]");
        List<ICSReceiptModel> icsReceiptModels = null;
        if (ajaxResult.hasErr()) {
            // 获取到货单失败改为从数据库获取
//            icsReceiptModels = this.getICSReceiptModelList(srmHeader);
//            icsReceiptModels=null;
        } else {
            // 解析erp到货单数据
            try {
                logger.info("获取ERP到货单3:"+srmDetails.get(0).getCode());
                icsReceiptModels = this.parseResults(ajaxResult, srmHeader, srmDetails.get(0).getCompanyCode());
                logger.info("获取ERP到货单4:"+srmDetails.get(0).getCode());
            } catch (Exception e) {
                logger.info("获取ERP到货单5:"+srmDetails.get(0).getCode());
                this.updateErrMsg(srmHeader, e.getMessage());
                return AjaxResult.error(e.getMessage());
            }
        }
        if (icsReceiptModels == null || icsReceiptModels.size() == 0) {
            this.updateErrMsg(srmHeader, ajaxResult.getMsg());
            return ajaxResult;
        }
        AjaxResult result = new AjaxResult<>();
        // 修改送货单主表
        logger.info("获取ERP到货单6:"+srmDetails.get(0).getCode());
        for (ICSReceiptModel icsReceiptModel : icsReceiptModels) {
            try {
                // 生成WMS采购入库单
                result = icsReceiptAPIService.insertICSReceipts(icsReceiptModel,srmDetailsAll);
                if (result.hasErr()) {
                    return ajaxResult;
                }
            } catch (Exception e) {
                this.updateErrMsg(srmHeader, e.getMessage());
                return AjaxResult.error(e.getMessage());
            } finally {
                // 生成U8
                result = saveResult(icsReceiptModel);
            }
        }
        if (result.hasErr()) {
            this.updateErrMsg(srmHeader, ajaxResult.getMsg());
            return ajaxResult;
        }
        logger.info("获取ERP到货单7:"+srmDetails.get(0).getCode());
        icspoArrives.get(0).getDetails().forEach(icspoArriveDetail -> {
            srmDetails.forEach(srmDetail -> {
                if (icspoArriveDetail.getLOTNO().equals(srmDetail.getLineCode())) {
                    srmDetail.setUploadStatus4(1);
                    srmDetailService.updateById(srmDetail);
                }
            });
        });
        logger.info("获取ERP到货单8:"+srmDetails.get(0).getCode());
        List<SrmDetail> srmDetailsCheck = srmDetailService.list(new LambdaQueryWrapper<SrmDetail>().eq(SrmDetail::getCode, srmHeader.getCode()).eq(SrmDetail::getWarehouseCode, ShiroUtils.getWarehouseCode()).eq(SrmDetail::getCheckCode, 2));
        if (CollectionUtils.isNotEmpty(srmDetailsCheck) && srmDetailsCheck.size() == srmDetailsAll.size()) {
            srmHeader.setCheckCode(2);
        } else {
            ReceiptHeader receiptHeader = new ReceiptHeader();
            receiptHeader.setUserDef3(srmHeader.getCode());
            receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
            List<ReceiptHeader> receiptHeaderlist = receiptHeaderService.getByDomain(receiptHeader);
            if (receiptHeaderlist != null && receiptHeaderlist.size() > 0) {
                for (ReceiptHeader header : receiptHeaderlist) {
                    LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
                    receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, header.getId());
                    List<ReceiptDetail> list = receiptDetailService.list(receiptDetailLambdaQueryWrapper);
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<String> collect = srmDetailsCheck.stream().map(s -> s.getMaterialCode()).collect(Collectors.toList());
                        List<ReceiptDetail> receiptDetails = list.stream().filter(l -> collect.contains(l.getMaterialCode())).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(receiptDetails) && receiptDetails.size() == list.size()) {
                            header.setDeleted(false);
                            receiptHeaderService.updateById(receiptHeader);
                        } else {
                            header.setDeleted(true);
                            receiptHeaderService.updateById(receiptHeader);
                        }
                    }
                }
            }
            srmHeader.setCheckCode(1);
        }
        srmHeader.setUploadStatus(1);
        this.updateById(srmHeader);
        return AjaxResult.success("送货单提交成功!");
    }


    public List<SrmDetail> formU8EntitiesV(List<SrmDetail> srmDetails) {
        //转换为接口实体,发送获取ERP单据
        List<SrmDetail> srmDetailList = new ArrayList<>();
        for (SrmDetail srmDetail : srmDetails) {
            if ((srmDetail.getQualifiedQty().compareTo(new BigDecimal(0))>0||srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0))>0)&&srmDetail.getUploadStatus4() == 0 && srmDetail.getEnable() == 1) {
                srmDetailList.add(srmDetail);
            }
        }
        return srmDetailList;
    }
    public List<ICSPOArrive> formU8Entities(List<SrmDetail> srmDetails) {
        //转换为接口实体,发送获取ERP单据
        List<ICSPOArrive> icspoArrives = new ArrayList<>();
        ICSPOArrive icspoArrive = new ICSPOArrive();
        List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
        for (SrmDetail srmDetail : srmDetails) {
//            String materialCode = srmDetail.getMaterialCode();
//            Material material = materialService.getMaterialByCode(materialCode, ShiroUtils.getWarehouseCode());
//            if(material==null){
//                throw new ServiceException("物料编码"+materialCode+"不存在");
//            }
//            //外观检
//            if(materialCode.startsWith("1")){
//                BigDecimal qualifiedQty = srmDetail.getConfirmQty().subtract(srmDetail.getUnqualifiedQty());
//                srmDetail.setQualifiedQty(qualifiedQty);
//            }
//            //品质检设置不质检属性了的
//            if(materialCode.startsWith("2")&&StringUtils.isNotEmpty(material.getQcFlag())&&material.getQcFlag().equals("0")){
//                srmDetail.setQualifiedQty(srmDetail.getConfirmQty());
//            }
//            if (srmDetail.getUploadStatus4() == 0 && srmDetail.getEnable() == 1 &&
//                    srmDetail.getQualifiedQty().compareTo(new BigDecimal(0)) == 1){
            if (srmDetail.getUploadStatus4() == 0 && srmDetail.getEnable() == 1) {
                ICSPOArriveDetail icspoArriveDetail = new ICSPOArriveDetail();
                icspoArriveDetail.setLOTNO(srmDetail.getLineCode());
                icspoArriveDetail.setReceiveQty(srmDetail.getConfirmQty());
                icspoArriveDetail.setWHCode(srmDetail.getUWarehouseCode());
                icspoArriveDetails.add(icspoArriveDetail);
                srmDetailService.updateById(srmDetail);
            }
        }
        if (icspoArriveDetails.isEmpty()) {
            return icspoArrives;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dt = format.format(new Date());
        icspoArrive.setUser(srmDetails.get(0).getLastUpdateBy());
        User user = userService.selectUserByLoginName(srmDetails.get(0).getLastUpdateBy());
        if (user != null) {
            icspoArrive.setMUSERName(user.getUserName());
        }
        icspoArrive.setCompanyCode(srmDetails.get(0).getCompanyCode());
        icspoArrive.setMTime(dt);
        icspoArrive.setASNNO(srmDetails.get(0).getCode());
        icspoArrive.setType("ERP");
        icspoArrive.setDetails(icspoArriveDetails);
        icspoArrives.add(icspoArrive);
        return icspoArrives;
    }
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirm(String ids,String warehouseCode) {
        if (StringUtils.isEmpty(ids)){
            return AjaxResult.error("没有选中明细行");
        }
        Integer[] list = Convert.toIntArray(ids);
        List<SrmDetail> srmDetails = new ArrayList<>();
        Integer headId = 0;
        List<SrmDetail> failedSrmDetail = new ArrayList<>();
        for (Integer id : list) {
            SrmDetail srmDetail = srmDetailService.getOne(new LambdaQueryWrapper<SrmDetail>().eq(SrmDetail::getId,id).eq(SrmDetail::getWarehouseCode,ShiroUtils.getWarehouseCode()));
            if(srmDetail == null){
                return AjaxResult.error("id为"+id+"的送货单明细不存在");
            }
            if(StringUtils.isEmpty(srmDetail.getUWarehouseCode())){
                return AjaxResult.error("id为"+id+"的送货单明细没有选择仓库");
            }
            srmDetail.setConfirmQty(srmDetail.getQty());
            srmDetail.setEnable(1);
            srmDetail.setLastUpdateBy(ShiroUtils.getLoginName());
            srmDetail.setLastUpdate(new Date());
            // 如果不合格数量不等于0
            if (srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0)) != 0){
                failedSrmDetail.add(srmDetail);
            }
            srmDetailService.updateById(srmDetail);
            srmDetails.add(srmDetail);
            headId=srmDetail.getHeaderId();
        }
        SrmHeader srmHeader = srmHeaderService.getOne(new LambdaQueryWrapper<SrmHeader>().eq(SrmHeader::getId,headId).eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        if (srmHeader != null) {
            SrmDetail srmDetail = new SrmDetail();
            srmDetail.setCode(srmHeader.getCode());
            srmDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            List<SrmDetail> srmDetailList = srmDetailService.selectList(srmDetail);
            srmDetail.setEnable(1);
            List<SrmDetail> details = srmDetailService.selectList(srmDetail);
            if (srmDetailList.size() == details.size()) {
                srmHeader.setEnable(2);
            } else {
                srmHeader.setEnable(1);
            }
            srmHeader.setLastUpdate(new Date());
            srmHeader.setLastUpdateBy(ShiroUtils.getLoginName());
            srmHeaderService.updateById(srmHeader);
        }
        //默认质检的物料是不做到货的

        //SRM提示到货失败的需要去定时任务
        if(!srmDetails.isEmpty()){
            //通知erp
            AjaxResult result =srmHeaderService.confirmSRMModelV(srmDetails,srmHeader);
            if(result!=null&&result.hasErr()){
                return result;
            }

        }

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


    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmSRMModelV(List<SrmDetail> details,SrmHeader srmHeader) {
        //单据根据单号分组,组建回传实体
        //根据code去重
        Set<String> codes = new HashSet<>();
        String warehouseCode = null;
        for (SrmDetail detail : details) {
            codes.add(detail.getCode());
        }
        warehouseCode = details.get(0).getWarehouseCode();
        Integer number = 0;
        int needCheck=0;
        for (String code : codes) {
            for (SrmDetail srmDetail : details) {
                if (srmDetail.getCode().equals(code)) {
                    String materialCode = srmDetail.getMaterialCode();
                    Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
                    if (material == null) {
                        throw new ServiceException("物料编码" + materialCode + "不存在");
                    }
                    //质检标志:为0需要质检;质检标志为空,code开头1不需要质检,2开头需要质检
                    if (StringUtils.isNotEmpty(material.getQcFlag()) && material.getQcFlag().equals("0")) {
                        if (materialCode.startsWith("1")) {
                            //外观检
                            BigDecimal qualifiedQty = srmDetail.getConfirmQty().subtract(srmDetail.getUnqualifiedQty());
                            srmDetail.setQualifiedQty(qualifiedQty);
                            srmDetail.setCheckCode(2);
                        } else {
                            srmDetail.setQualifiedQty(srmDetail.getConfirmQty());
                            srmDetail.setCheckCode(2);
                        }
                    } else if (StringUtils.isEmpty(material.getQcFlag())) {
                        if (materialCode.startsWith("1")) {
                            BigDecimal qualifiedQty = srmDetail.getConfirmQty().subtract(srmDetail.getUnqualifiedQty());
                            srmDetail.setQualifiedQty(qualifiedQty);
                            srmDetail.setCheckCode(2);
//                            needCheck++;
                        }else{
                            needCheck++;
                        }
                    }else{
                        needCheck++;
                    }
                    srmDetailService.updateById(srmDetail);
                }
            }

        }
        if(needCheck==0){
            //成功的回传erp采购到货单,不合格的回传erp和srm,成功的生成采购入库单,将srm的code保存起来
            List<SrmDetail> icspoArrives = srmHeaderService.formU8EntitiesV(details);
            if(icspoArrives==null||icspoArrives.size()==0){
                return AjaxResult.error("没有符合规定的送货单,请检查是否质检");
            }
            icspoArrives= icspoArrives.stream()
                    .filter(detail1 -> detail1.getQualifiedQty().compareTo(new BigDecimal(0))>0)
                    .collect(Collectors.toList());
            if(icspoArrives==null||icspoArrives.size()==0){
                return AjaxResult.error("没有符合规定的送货单,请检查是否质检");
            }
            List<String> codess = icspoArrives.stream().map(SrmDetail::getPurchaseCode).distinct().collect(Collectors.toList());
            for (String code:codess) {
                if(StringUtils.isEmpty(code)){
                    continue;
                }
                List<SrmDetail> srmDetailPurchase = icspoArrives.stream().filter(srmDetaill -> srmDetaill.getPurchaseCode().equals(code)).collect(Collectors.toList());
                if (srmDetailPurchase == null || srmDetailPurchase.size() == 0) {
                    continue;
                }
                AjaxResult ajaxResult=srmReceiptService.sendErpRecipt(srmDetailPurchase, srmHeader);
                if(ajaxResult!=null&&ajaxResult.hasErr()){
                    throw new ServiceException(ajaxResult.getMsg());
                }
                String msg=ajaxResult.getMsg();
                String[] strsplit=msg.split(",");
                String refercode=strsplit[0];
                AjaxResult ajaxResult1=srmReceiptService.addWmsReceiptSrm(srmDetailPurchase,srmHeader, refercode);
                if(ajaxResult1!=null&&ajaxResult1.hasErr()){
//                    throw new ServiceException(ajaxResult1.getMsg());
                    continue;
                }
            }
//            AjaxResult ajaxResult=srmReceiptService.sendErpRecipt(icspoArrives, srmHeader);
//            if(ajaxResult!=null&&ajaxResult.hasErr()){
//                throw new ServiceException(ajaxResult.getMsg());
//            }
//            String msg=ajaxResult.getMsg();
//            String[] strsplit=msg.split(",");
//            String refercode=strsplit[0];
//            AjaxResult ajaxResult1=srmReceiptService.addWmsReceiptSrm(icspoArrives,srmHeader, refercode);
//            if(ajaxResult1!=null&&ajaxResult1.hasErr()){
//                throw new ServiceException(ajaxResult1.getMsg());
//            }
        }
        return AjaxResult.success("成功");
    }

    /**
     * 老版本接口==废弃
     * @param details
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmSRMModel(List<SrmDetail> details) {
        //单据根据单号分组,组建回传实体
        //根据code去重
        Set<String> codes = new HashSet<>();
        String warehouseCode = null;
        for (SrmDetail detail : details) {
            codes.add(detail.getCode());
        }
        warehouseCode = details.get(0).getWarehouseCode();
        Integer number = 0;
        int needCheck=0;
        for (String code : codes) {
            for (SrmDetail srmDetail : details) {
                if (srmDetail.getCode().equals(code)) {
                    String materialCode = srmDetail.getMaterialCode();
                    Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
                    if (material == null) {
                        throw new ServiceException("物料编码" + materialCode + "不存在");
                    }
                    if (StringUtils.isNotEmpty(material.getQcFlag()) && material.getQcFlag().equals("0")) {
                        if (materialCode.startsWith("1")) {
                            //外观检
                            BigDecimal qualifiedQty = srmDetail.getConfirmQty().subtract(srmDetail.getUnqualifiedQty());
                            srmDetail.setQualifiedQty(qualifiedQty);
                            srmDetail.setCheckCode(2);
                        } else {
                            srmDetail.setQualifiedQty(srmDetail.getConfirmQty());
                            srmDetail.setCheckCode(2);
                        }
                    } else if (StringUtils.isEmpty(material.getQcFlag())) {
                        if (materialCode.startsWith("1")) {
                            BigDecimal qualifiedQty = srmDetail.getConfirmQty().subtract(srmDetail.getUnqualifiedQty());
                            srmDetail.setQualifiedQty(qualifiedQty);
                            srmDetail.setCheckCode(2);
                        }
                    }else{
                        needCheck++;
                    }
                    srmDetailService.updateById(srmDetail);
                }
            }
            List<ICSPOArrive> icspoArrives = new ArrayList<>();
            List<ICSPOArriveDetail> icspoArriveDetails = new ArrayList<>();
            //将相同code的放一个list
            icspoArriveDetails = createIcspoArriveDetail(details, code, icspoArriveDetails);
            //创建ICSPOArrive
            ICSPOArrive icspoArrive = createIcspoArrive(details, code, icspoArriveDetails);

            icspoArrives.add(icspoArrive);
            AjaxResult ajaxResult = icsasnService.returnICSPOArrive(icspoArrives, warehouseCode);
            if (ajaxResult.hasErr()) {
                for (SrmDetail detail : details) {
                    if (detail.getCode().equals(code)) {
                        detail.setUploadStatus1(2);
                        srmDetailService.updateById(detail);
                    }
                }
                number++;
            } else {
                for (SrmDetail detail : details) {
                    if (detail.getCode().equals(code)) {
                        detail.setUploadStatus1(1);
                        srmDetailService.updateById(detail);
                    }
                }
            }
        }
        if (number == codes.size()) {
            return AjaxResult.error("失败");
        }
        return AjaxResult.success("成功");
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setData(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    /**
     * mybatis-plus响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getMpDataTable(List<?> list, Long total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setData(list);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 已到待检
     * @param srmHeader
     * @return
     */
    public TableDataInfo songHuoDaiJianList(SrmHeader srmHeader)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<SrmHeader> list = this.selectList(srmHeader) ;
        if(StringUtils.isNotEmpty(srmHeader.getMaterialCode())){
            Material material = materialService.getMaterialByCode(srmHeader.getMaterialCode(), ShiroUtils.getWarehouseCode());
            if(material == null){
                throw new ServiceException("物料编码不存在");
            }
            LambdaQueryWrapper<SrmDetail> lambdaQueryWrapper= Wrappers.lambdaQuery();
            lambdaQueryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()),SrmDetail::getCode,srmHeader.getCode());
            lambdaQueryWrapper.eq(SrmDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
            lambdaQueryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getMaterialCode()),SrmDetail::getMaterialCode,srmHeader.getMaterialCode());
            List<SrmDetail> srmDetailList = srmDetailService.list(lambdaQueryWrapper);
            if(com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(srmDetailList)){
                List<String> codes = srmDetailList.stream().map(s -> s.getCode()).collect(Collectors.toList());
                List<String> collect = codes.stream().distinct().collect(Collectors.toList());
                List<SrmHeader> srmHeaders = list.stream().filter(l -> collect.contains(l.getCode())).collect(Collectors.toList());
                srmHeaders = srmHeaders.stream().filter(l ->
                        (l.getEnable().equals(1)||l.getEnable().equals(2))&&(l.getCheckCode().equals(0)||l.getCheckCode().equals(1))
                ).collect(Collectors.toList());
                return getDataTable(srmHeaders);
            }else {
                return getDataTable(new ArrayList<>());
            }
        }
        list = list.stream().filter(l ->(l.getEnable().equals(1)||l.getEnable().equals(2))&&(l.getCheckCode().equals(0)||l.getCheckCode().equals(1))).collect(Collectors.toList());
        if(com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(list)){
            return getDataTable(new ArrayList<>());
        }

        if(list.size()>49){
            return getMpDataTable(list.subList(50*pageNum-50,pageNum*50-1),Long.valueOf(list.size()));
        }
        return getDataTable(list);

    }

    /**
     * 已到不合格
     * @param srmHeader
     * @return
     */
    public TableDataInfo yiDaoBuHeGeList(SrmHeader srmHeader)
    {
        SrmHeader srmHeader1=new SrmHeader();
        BeanUtils.copyBeanProp(srmHeader1, srmHeader);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        srmHeader1.setCheckCode(null);
        List<SrmHeader> list = srmHeaderService.selectList(srmHeader1) ;
        if(StringUtils.isNotEmpty(srmHeader.getMaterialCode())){
            Material material = materialService.getMaterialByCode(srmHeader.getMaterialCode(), ShiroUtils.getWarehouseCode());
            if(material == null){
                throw new ServiceException("物料编码不存在");
            }
        }

        LambdaQueryWrapper<SrmDetail> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(srmHeader.getCode()),SrmDetail::getCode,srmHeader.getCode());
        lambdaQueryWrapper.eq(SrmDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(srmHeader.getMaterialCode()),SrmDetail::getMaterialCode,srmHeader.getMaterialCode());
        lambdaQueryWrapper.gt(SrmDetail::getUnqualifiedQty,0);
        List<SrmDetail> srmDetailList = srmDetailService.list(lambdaQueryWrapper);
        if(com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(srmDetailList)){
            List<String> codes = srmDetailList.stream().map(s -> s.getCode()).collect(Collectors.toList());
            List<String> collect = codes.stream().distinct().collect(Collectors.toList());
            List<SrmHeader> srmHeaders = list.stream().filter(l -> collect.contains(l.getCode())).collect(Collectors.toList());
            srmHeaders = srmHeaders.stream().filter(l ->
                    (l.getEnable().equals(1)||l.getEnable().equals(2))&&(l.getCheckCode().equals(1)||l.getCheckCode().equals(2))
            ).collect(Collectors.toList());
            srmHeaders = srmHeaders.stream().map(srmHeader2 -> {
                srmHeader2.setQuatityStatus(1);
                return srmHeader2;
            }).collect(Collectors.toList());
            if(srmHeaders.size()>49){
                return getMpDataTable(srmHeaders.subList(50*pageNum-50,pageNum*50-1),Long.valueOf(srmHeaders.size()));
            }
            return getDataTable(srmHeaders);
        }
        return getDataTable(new ArrayList<>());
    }

    /**
     * @param referCodes
     * @return
     */
    public Boolean isReceiptByReferCodes(String referCodes,String warehouseCode)
    {
        String[] codess=referCodes.split(";");
//        String[] codes=Convert.toStrArray(referCodes);
        LambdaQueryWrapper<ReceiptHeader> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptHeader::getWarehouseCode,warehouseCode);
        lambdaQueryWrapper.in(StringUtils.isNotEmpty(referCodes),ReceiptHeader::getReferCode,codess);
        lambdaQueryWrapper.lt(ReceiptHeader::getLastStatus,QuantityConstant.RECEIPT_HEADER_POSTING);
        Integer count=receiptHeaderService.count(lambdaQueryWrapper);
        if(count!=0){
            return false;
        }
        return true;

    }

    @Transactional(rollbackFor = Exception.class)
    public int cleanDocuments() {
        //查找过期的入库单据
        String expireTime=DateUtils.getNowPreDays("yyyy-MM-dd 00:00:00", 150);
        LambdaQueryWrapper<SrmHeader>  wrapper=new LambdaQueryWrapper<>();
        wrapper.lt(SrmHeader::getCreated,expireTime);
//        receWrapper.ge(ReceiptHeader::getLastStatus,800);
        wrapper.last("limit 20");
        List<SrmHeader> srmHeaderList= this.list(wrapper);
//        List<ReceiptHeader> receiptHeaderList = this.getExpiredBill();
        if (srmHeaderList.isEmpty()) {
            return 1;
        }
        for (SrmHeader srmHeader : srmHeaderList) {
            this.deleteHeaderHistory(srmHeader.getId());
        }
        return 1;
    }
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteHeaderHistory(Integer id){
        SrmHeader srmHeader = this.getById(id);
        if (srmHeader == null) {
            return AjaxResult.error("没有找到单据" + String.valueOf(id));
        }
        LambdaQueryWrapper<SrmDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SrmDetail::getHeaderId, id);
        List<SrmDetail> list = srmDetailService.list(lambdaQueryWrapper);

        List<SrmDetailHistory> detailHistories = new ArrayList<>();
        if (!this.removeById(srmHeader)) {
            throw new ServiceException("删除头表失败");
        }
        List<Integer> detailIds = new ArrayList<>();
        SrmHeaderHistory headerHistory = new SrmHeaderHistory();
        org.springframework.beans.BeanUtils.copyProperties(srmHeader, headerHistory);
        headerHistory.setLastUpdate(new Date());
        if (!srmHeaderHistoryService.save(headerHistory)) {
            throw new ServiceException("新增历史头表失败");
        }
        for (SrmDetail srmDetail: list) {
            detailIds.add(srmDetail.getId());
            SrmDetailHistory srmDetailHistory = new SrmDetailHistory();
            org.springframework.beans.BeanUtils.copyProperties(srmDetail, srmDetailHistory);
            srmDetailHistory.setHeaderId(headerHistory.getId());
            detailHistories.add(srmDetailHistory);
        }
        if(detailIds.size() > 0) {
            if (!srmDetailService.removeByIds(detailIds)) {
                throw new ServiceException("删除明细表失败");
            }
        }
        if(detailHistories.size() > 0) {
            if (!srmDetailHistoryService.saveBatch(detailHistories)) {
                throw new ServiceException("新增历史入库明细失败");
            }
        }
        return AjaxResult.success("删除成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveSrmReceipt(List<SrmDetail> srmDetails) {
        if (srmDetails.isEmpty()) {
            return AjaxResult.error("请勿提交空数据!");
        }
        List<SrmDetail> srmDetailList = new ArrayList<>();
        for (SrmDetail srmDetail : srmDetails) {
//            if ((srmDetail.getQualifiedQty().compareTo(new BigDecimal(0))>0||srmDetail.getUnqualifiedQty().compareTo(new BigDecimal(0))>0) && srmDetail.getEnable() == 1) {
//                srmDetailList.add(srmDetail);
//            }
//            if(srmDetail.getId().equals(93380)||srmDetail.getId().equals(93386)||srmDetail.getId().equals(93399)||
//                    srmDetail.getId().equals(93408)){
//                srmDetailList.add(srmDetail);
//            }
            if(StringUtils.isNotEmpty(srmDetail.getReferCode())&&srmDetail.getReferCode().equals("HH-PR01-241024000002")){
                srmDetailList.add(srmDetail);
            }
        }

        SrmHeader srmHeader = this.getOne(new LambdaQueryWrapper<SrmHeader>().eq(SrmHeader::getCode, srmDetails.get(0).getCode()).eq(SrmHeader::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        //SRM提示到货失败的需要去定时任务
        AjaxResult ajaxResult1 =AjaxResult.success("送货单提交成功!");
        if (!srmDetailList.isEmpty()) {

            List<String> codes = srmDetailList.stream().map(SrmDetail::getPurchaseCode).distinct().collect(Collectors.toList());
            for (String code : codes) {
                if(StringUtils.isEmpty(code)){
                    continue;
                }

                List<SrmDetail> srmDetailPurchase = srmDetailList.stream().filter(srmDetaill -> srmDetaill.getPurchaseCode().equals(code)).collect(Collectors.toList());
                if (srmDetailPurchase == null || srmDetailPurchase.size() == 0) {
                    continue;
                }
                String refercode=srmDetailPurchase.get(0).getReferCode();
                if(StringUtils.isEmpty(refercode)){
                    continue;
                }
//                String refercode="HH-PR01-241024000001";
                ajaxResult1 = srmReceiptService.addWmsReceiptSrm(srmDetailPurchase, srmHeader, refercode);
                if (ajaxResult1 != null && ajaxResult1.hasErr()) {
                    continue;
                }
            }
        }
        return ajaxResult1;
    }
}