ICSShipmentApiService.java 56.2 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
package com.huaheng.pc.u8.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.api.general.service.ShipmentApiService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.CompanyU8;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.company.service.CompanyU8Service;
import com.huaheng.pc.config.documentWarning.service.IDocumentWarningService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.referenceCode.domain.U8Reference;
import com.huaheng.pc.referenceCode.service.U8ReferenceService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.u8.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class ICSShipmentApiService {

    @Resource
    private IDocumentWarningService documentWarningService;
    @Resource
    private WarehouseU8Service warehouseU8Service;
    @Resource
    private ShipmentApiService shipmentApiService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ICSShipmentDetailService icsShipmentDetailService;
    @Resource
    private ICSShipmentHeaderService icsShipmentHeaderService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private CompanyU8Service companyU8Service;
    @Resource
    private ICSReceiptAPIService icsReceiptAPIService;
    @Resource
    private IUserService userService;
    @Resource
    private U8ReferenceService u8ReferenceService;
    @Resource
    private CompanyService companyService;
    @Resource
    private WarehouseService warehouseService;


    //u8-wms出库下发
    @Transactional
    public AjaxResult procureShipment(ICSShipmentModel icsShipmentModel) {
        //获取主单和子单列
        icsShipmentModel = assign(icsShipmentModel);
        ICSShipmentHeader icsShipmentHeader = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails = icsShipmentModel.getIcsShipmentDetails();
        String currentWarehouseCode = icsShipmentHeader.getWarehouseCode();
        if (StringUtils.isEmpty(currentWarehouseCode)) {
            return AjaxResult.error("仓库编码不能为空");
        }
        if (StringUtils.isEmpty(icsShipmentHeader.getIsNew())) {
            return AjaxResult.error("IsNew不能为空");
        }
        boolean isFlag = "0".equals(icsShipmentHeader.getIsNew()) ? true : false;
        String type = icsShipmentHeader.getType();
        String typeName = getShipmentTypeName(type);
        switch (type) {
            case "QO":
            case "MO":
            case "OO":
            case "PKO":
            case "TO":
                break;
            default:
                return AjaxResult.error("出库类型不匹配");
        }
        //U8仓库编码
        String u8WarehouseCode = icsShipmentHeader.getStock();
        //U8公司编码
        String u8CompanyCode = icsShipmentHeader.getCompanyCode();
        //判断U8货主
        CompanyU8 companyU8 = companyU8Service.getCompanyByU8CodeWarehouseCode(u8CompanyCode, currentWarehouseCode);
        if (companyU8 == null) {
            return AjaxResult.error("系统没有此货主:【" + u8CompanyCode + "】");
        }
        if (StringUtils.isEmpty(u8WarehouseCode)) {
            return AjaxResult.error("仓库编码为空:【" + u8WarehouseCode + "】");
        }
        companyService.checkwarehouseCompany(companyU8.getCompanyCode());
        //通过u8的货主和仓库匹配wms的仓库
        WarehouseU8 warehouseU8 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                .eq(WarehouseU8::getUCompanyCode, icsShipmentHeader.getCompanyCode())
                .eq(WarehouseU8::getUWarehouseCode, u8WarehouseCode));
        if (warehouseU8 == null) {
            return AjaxResult.error("系统没有此仓库");
        }
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsShipmentHeader.getReferCode())
                .eq(U8Reference::getReferType, icsShipmentHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode)
                .last("LIMIT 1"));
        //入库单表 检查上游单号是否重复
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, icsShipmentHeader.getReferCode())
                .eq(ShipmentHeader::getShipmentType, icsShipmentHeader.getType())
                .eq(ShipmentHeader::getWarehouseCode, currentWarehouseCode)
                .last("LIMIT 1"));
        if (!isFlag) {
            if (one != null || shipmentHeader1 != null) {
                return AjaxResult.error(":请不要重复下发 " + icsShipmentHeader.getReferCode());
            }
        }

        if (icsShipmentHeader.getIsFlag()) {
            checkShipmentBill(isFlag, icsShipmentHeader.getReferCode(), icsShipmentHeader.getType(), currentWarehouseCode);
        }

        boolean save = icsShipmentHeaderService.save(icsShipmentHeader);
        if (!save) {
            throw new ServiceException("下发失败");
        }
        for (ICSShipmentDetail icsShipmentDetail : icsShipmentDetails) {
            icsShipmentDetail.setHeaderId(icsShipmentHeader.getSId());
        }
        save = icsShipmentDetailService.saveBatch(icsShipmentDetails);
        if (!save) {
            throw new ServiceException("下发失败");
        }
        if (!icsShipmentHeader.getIsFlag()) {
            return AjaxResult.success();
        }

        //根据icsShipmentHeader生成shipmentHeader
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        String maker = icsShipmentHeader.getCMaker();
        shipmentHeader.setShipmentType(type);
        shipmentHeader.setReferCode(icsShipmentHeader.getReferCode());
        shipmentHeader.setReferId(icsShipmentHeader.getReferId());
        shipmentHeader.setId(null);
        shipmentHeader.setLastUpdatedBy(maker);
        shipmentHeader.setCreatedBy(maker);
        shipmentHeader.setCreated(icsShipmentHeader.getCreated());
        shipmentHeader.setCustomerCode(icsShipmentHeader.getCCusCode());
        shipmentHeader.setShipmentNote(icsShipmentHeader.getCMemo());
        shipmentHeader.setWarehouseCode(warehouseU8.getWarehouseCode());
        shipmentHeader.setUWarehouseCode(icsShipmentHeader.getStock());
        shipmentHeader.setCompanyCode(warehouseU8.getCompanyCode());
        shipmentHeader.setCDepName(icsShipmentHeader.getCDepName());
        shipmentHeader.setRecMobile(icsShipmentHeader.getRecMobile());
        shipmentHeader.setRecCompany(icsShipmentHeader.getRecCompany());
        shipmentHeader.setRecName(icsShipmentHeader.getRecName());
        shipmentHeader.setRecAddr(icsShipmentHeader.getRecAddr());
        shipmentHeader.setSendAddr(icsShipmentHeader.getSendAddr());
        shipmentHeader.setSendCompany(icsShipmentHeader.getSendCompany());
        shipmentHeader.setSendMobile(icsShipmentHeader.getSendMobile());
        shipmentHeader.setSendName(icsShipmentHeader.getSendName());
        switch (type) {
            case "OO":
                shipmentHeader.setReferCode(icsShipmentHeader.getMoCode());
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
                break;
            case "TO":
                shipmentHeader.setReferCode(icsShipmentHeader.getCTVCode());
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
                break;
            default:
                shipmentHeader.setReferCode(icsShipmentHeader.getCCode());
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
                break;
        }
        if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            throw new ServiceException("没有单号");
        }
        //根据icsShipmentDetail生成shipmentAetail
        List<ShipmentDetail> shipmentDetails = new ArrayList<ShipmentDetail>();
        for (ICSShipmentDetail icsShipmentDetail : icsShipmentDetails) {
            //查找物料
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>()
                    .eq(Material::getCode, icsShipmentDetail.getCInvCode())
                    .eq(Material::getWarehouseCode, warehouseU8.getWarehouseCode()));
            if (material == null) {
                throw new ServiceException("该物料不存在:" + icsShipmentDetail.getCInvCode());
            }
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setId(null);
            shipmentDetail.setReferId(icsShipmentDetail.getAutoID());
            shipmentDetail.setReferCode(shipmentHeader.getReferCode());
            shipmentDetail.setMoCode(icsShipmentDetail.getMoCode());
            shipmentDetail.setLot(icsShipmentDetail.getCDefine23());
            shipmentDetail.setWarehouseCode(warehouseU8.getWarehouseCode());
            shipmentDetail.setMaterialCode(icsShipmentDetail.getCInvCode());
            shipmentDetail.setMaterialName(icsShipmentDetail.getCInvName());
            shipmentDetail.setMaterialSpec(icsShipmentDetail.getCInvStd());
//                    shipmentDetail.setZoneCode(material.getZoneCode());
            shipmentDetail.setCreated(DateUtils.getNowDate());
            shipmentDetail.setBatch("");
            shipmentDetail.setUWarehouseCode(shipmentHeader.getUWarehouseCode());
            shipmentDetail.setMaterialUnit(icsShipmentDetail.getCComUnitName());
            shipmentDetail.setCreatedBy(maker);
            shipmentDetail.setLastUpdated(DateUtils.getNowDate());
            shipmentDetail.setLastUpdatedBy(maker);
            shipmentDetail.setMoCode(icsShipmentDetail.getMoCode());
            if (!icsShipmentDetail.getCompanyCode().equals(icsShipmentHeader.getCompanyCode())) {
//                documentWarningService.ErpSendWarning(shipmentHeader.getReferCode(), "子单货主与主单不对应");
                throw new ServiceException("子单货主与主单不对应");
            }
            shipmentDetail.setCompanyCode(warehouseU8.getCompanyCode());
            shipmentDetail.setAutoId(icsShipmentDetail.getAutoID());
            shipmentDetail.setProjectNo(icsShipmentDetail.getCItemName());
            shipmentDetail.setQty((icsShipmentDetail.getIQuantity()).abs());
            shipmentDetail.setQtyCompleted(icsShipmentDetail.getIQuantity());
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
            if (icsShipmentDetail.getIrowno() != null && StringUtils.isNotEmpty(icsShipmentDetail.getIrowno().toString())) {
                shipmentDetail.setReferLineNum(icsShipmentDetail.getIrowno().toString());
            }
            if (icsShipmentDetail.getSortSeq() != null && StringUtils.isNotEmpty(icsShipmentDetail.getSortSeq().toString())) {
                shipmentDetail.setReferLineNum(icsShipmentDetail.getSortSeq().toString());
            }
            shipmentDetails.add(shipmentDetail);
        }

        //调用公共API
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);
        AjaxResult ajaxResult = shipmentApiService.shipment(shipmentDomain);
        if (!ajaxResult.hasErr() && !isFlag) {
            U8Reference reference = new U8Reference();
            reference.setReferType(icsShipmentHeader.getType());
            reference.setReferCode(icsShipmentHeader.getReferCode());
            reference.setStock(icsShipmentHeader.getStock());
            reference.setCompanyCode(warehouseU8.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
        if (ajaxResult.hasErr()) {
            return ajaxResult;
        }
        if (!isFlag) {
            return AjaxResult.success("出库单下发成功");
        } else if (isFlag) {
            return AjaxResult.success("出库单更新成功");
        }
        return AjaxResult.success("出库单下发成功");
    }


    //出库单为销售出库或采购退货单时

    /**
     * 出库单为 销售出库单 | 采购退货单
     *
     * @param icsShipmentModel
     * @return
     */
    @Transactional
    public AjaxResult saleShipmentOrPurchReturn(ICSShipmentModel icsShipmentModel) {
        icsShipmentModel = assign(icsShipmentModel);
        ICSShipmentHeader icsShipmentHeader = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails = icsShipmentModel.getIcsShipmentDetails();
        if (StringUtils.isEmpty(icsShipmentHeader.getIsNew())) {
            return AjaxResult.error("isNew为空");
        }
        boolean isFlag = "0".equals(icsShipmentHeader.getIsNew()) ? true : false;
        String type = icsShipmentHeader.getType();
        String currentWarehouseCode = icsShipmentHeader.getWarehouseCode();
        if (StringUtils.isEmpty(currentWarehouseCode)) {
            return AjaxResult.error("仓库编码不能为空");
        }
        String typeName = getShipmentTypeName(type);
        switch (type) {
            case "SO":
            case "PRO":
                break;
            default:
                return AjaxResult.error("出库类型不匹配");
        }

        String code = icsShipmentHeader.getReferCode();
        if (StringUtils.isEmpty(code)) {
            return AjaxResult.error("没有单号");
        }

        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getUCompanyCode, icsShipmentHeader.getCompanyCode())
                .last("LIMIT 1"));
        if (companyWu == null) {
            return AjaxResult.error("单据货主不存在" + icsShipmentHeader.getCompanyCode());
        }
        Map<String, String> map = new HashMap<>();
        Set<String> cwhcodes = new HashSet<>();
        for (ICSShipmentDetail item : icsShipmentDetails) {
            //通过u8的货主和仓库匹配wms的仓库
            WarehouseU8 warehouseU8 = warehouseU8Service.getWarehouseU8ByUCompanyUWarehouseCode(item.getCompanyCode(), item.getCWHCode(), currentWarehouseCode);
            if (warehouseU8 != null) {
                map.put(warehouseU8.getUWarehouseCode(), warehouseU8.getCompanyCode());
                cwhcodes.add(warehouseU8.getUWarehouseCode());
            }
        }
       /* WarehouseU8 warehouseU81 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                .eq(WarehouseU8::getUCompanyCode, icsShipmentHeader.getCompanyCode())
                .eq(WarehouseU8::getUWarehouseCode, icsShipmentHeader.getStock())
                .eq(WarehouseU8::getWarehouseCode, currentWarehouseCode));
        if(warehouseU81==null){
            return AjaxResult.error("仓库编码不错误:"+icsShipmentHeader.getStock());
        }*/
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsShipmentHeader.getReferCode())
                .eq(U8Reference::getReferType, icsShipmentHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
//        入库单表 检查上游单号是否重复
        List<ShipmentHeader> shipmentHeader1 = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, icsShipmentHeader.getReferCode())
                .eq(ShipmentHeader::getWarehouseCode, icsShipmentHeader.getWarehouseCode()));
        if (one != null && shipmentHeader1.size() > 1) {
            return AjaxResult.error(typeName + ":请不要重复下发 " + icsShipmentHeader.getReferCode());
        }
        //判断出库单是否可以更新
        checkShipmentBill(isFlag, code, icsShipmentHeader.getType(), currentWarehouseCode);

        boolean save = icsShipmentHeaderService.save(icsShipmentHeader);
        if (!save) {
            throw new ServiceException("下发失败");
        }
        for (ICSShipmentDetail icsShipmentDetail : icsShipmentDetails) {
            icsShipmentDetail.setHeaderId(icsShipmentHeader.getSId());
        }
        save = icsShipmentDetailService.saveBatch(icsShipmentDetails);
        if (!save) {
            throw new ServiceException("下发失败");
        }

//        Set<String> cwhcodes = icsShipmentDetails.stream().map(ICSShipmentDetail::getCWHCode).collect(Collectors.toSet());
        List<ShipmentDomain> domains = new ArrayList<>();
        for (String cwhcode : cwhcodes) {
            String maker = icsShipmentHeader.getCMaker();
            ShipmentHeader shipmentHeader = new ShipmentHeader();
            shipmentHeader.setShipmentType(icsShipmentHeader.getType());
            shipmentHeader.setId(null);
            if (StringUtils.isNotEmpty(icsShipmentHeader.getFromCompanyCode())) {
                shipmentHeader.setUserDef3("跨账套");
            }
            shipmentHeader.setReferId(icsShipmentHeader.getReferId());
            shipmentHeader.setReferCode(icsShipmentHeader.getReferCode());
            shipmentHeader.setLastUpdatedBy(maker);
            shipmentHeader.setCreatedBy(maker);
            shipmentHeader.setCreated(icsShipmentHeader.getCreated());
            shipmentHeader.setCustomerCode(icsShipmentHeader.getCCusCode());
            shipmentHeader.setLastUpdated(icsShipmentHeader.getCreated());
            shipmentHeader.setReferCode(code);
            shipmentHeader.setShipmentNote(icsShipmentHeader.getCMemo());
            shipmentHeader.setWarehouseCode(currentWarehouseCode);
            shipmentHeader.setUWarehouseCode(cwhcode);
            String companyCode = map.get(cwhcode);
            shipmentHeader.setCompanyCode(companyCode);
            shipmentHeader.setSupplierCode(icsShipmentHeader.getCVenCode());
            shipmentHeader.setDeptCode(icsShipmentHeader.getCDepCode());
            shipmentHeader.setCDepName(icsShipmentHeader.getCDepName());
            shipmentHeader.setRecMobile(icsShipmentHeader.getRecMobile());
            shipmentHeader.setRecCompany(icsShipmentHeader.getRecCompany());
            shipmentHeader.setRecName(icsShipmentHeader.getRecName());
            shipmentHeader.setRecAddr(icsShipmentHeader.getRecAddr());
            shipmentHeader.setSendAddr(icsShipmentHeader.getSendAddr());
            shipmentHeader.setSendCompany(icsShipmentHeader.getSendCompany());
            shipmentHeader.setSendMobile(icsShipmentHeader.getSendMobile());
            shipmentHeader.setSendName(icsShipmentHeader.getSendName());
            //销售单向wms主单存入发货信息和收货信息
            if (!StringUtils.isEmpty(icsShipmentHeader.getCSCName()) && "快递".equals(icsShipmentHeader.getCSCName()) && StringUtils.isNotEmpty(icsShipmentHeader.getRecName())) {
                shipmentHeader.setKdEable(1);
            }
            List<ShipmentDetail> shipmentDetails = new ArrayList<>();
            for (ICSShipmentDetail item : icsShipmentDetails) {
                if (!cwhcode.equals(item.getCWHCode())) {
                    continue;
                }
                //查找物料
                Material material = materialService.getOne(new LambdaQueryWrapper<Material>()
                        .eq(Material::getCode, item.getCInvCode())
                        .eq(Material::getWarehouseCode, currentWarehouseCode));
                if (material == null) {
                    //                documentWarningService.ErpSendWarning(code, "该物料不存在:"+item.getCInvCode());
                    throw new ServiceException("该物料不存在:" + item.getCInvCode());
                }
                ShipmentDetail shipmentDetail = new ShipmentDetail();
                shipmentDetail.setId(null);
                shipmentDetail.setReferCode(code);
                shipmentDetail.setAutoId(item.getAutoID());
                shipmentDetail.setReferLineNum(item.getIrowno() + "");
                shipmentDetail.setWarehouseCode(currentWarehouseCode);
                shipmentDetail.setUWarehouseCode(icsShipmentHeader.getUWarehouseCode());
                shipmentDetail.setMaterialCode(item.getCInvCode());
                shipmentDetail.setZoneCode(material.getZoneCode());
                shipmentDetail.setCreated(DateUtils.getNowDate());
                shipmentDetail.setUWarehouseCode(item.getCWHCode());
                shipmentDetail.setCreatedBy(maker);
                shipmentDetail.setMaterialSpec(item.getCComUnitName());
                shipmentDetail.setLastUpdated(DateUtils.getNowDate());
                shipmentDetail.setLastUpdatedBy(maker);
                shipmentDetail.setPoId(item.getPOID());
                shipmentDetail.setPodId(item.getPODID());
                shipmentDetail.setPoCode(item.getPOCode());
                if (!item.getCompanyCode().equals(icsShipmentHeader.getCompanyCode())) {
                    //                documentWarningService.ErpSendWarning(code, "子单货主与主单不对应");
                    throw new ServiceException("子单货主与主单不对应");
                }
                //            shipmentDetail.setMoCode(item.getCDefine22());
                shipmentDetail.setLot(item.getCDefine23());
                shipmentDetail.setCompanyCode(companyCode);
//                shipmentDetail.setProjectNo(item.getCItemName());
                shipmentDetail.setQty((item.getIQuantity()).abs());
                shipmentDetail.setQtyCompleted(item.getIQuantity());
                shipmentDetail.setInventorySts(QuantityConstant.GOOD);
                shipmentDetail.setSoCode(item.getCSOCode());
                shipmentDetail.setSoId(item.getCSOID());
                shipmentDetail.setSoAutoId(item.getCSOAutoID());
                shipmentDetail.setBatch("");
                shipmentDetails.add(shipmentDetail);
            }
            //调用公共API
            ShipmentDomain shipmentDomain = new ShipmentDomain();
            shipmentDomain.setShipmentHeader(shipmentHeader);
            shipmentDomain.setShipmentDetails(shipmentDetails);
            domains.add(shipmentDomain);
        }
        AjaxResult ajaxResult = shipmentApiService.shipment(domains);
        if (!ajaxResult.hasErr() && !isFlag) {
            U8Reference reference = new U8Reference();
            reference.setReferType(icsShipmentHeader.getType());
            reference.setReferCode(icsShipmentHeader.getReferCode());
            reference.setStock(icsShipmentHeader.getStock());
            reference.setCompanyCode(companyWu.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
        if (ajaxResult.hasErr()) {
            return ajaxResult;
        }
        String isNew = icsShipmentHeader.getIsNew();
        if ("1".equals(isNew)) {
            return AjaxResult.success("出库单下发成功");
        } else if ("0".equals(isNew)) {
            return AjaxResult.success("出库单更新成功");
        } else {
            return ajaxResult;
        }
    }


    //wms-u8出库回传

    /**
     * 出库单回传
     *
     * @param url
     * @param status
     * @return
     */
    @Transactional
    public AjaxResult shipmentReturn(String url, Integer status) {
        //找出所有首状态为1000的出库单
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>()
                .lt(ShipmentHeader::getFirstStatus, 1000));
        //获取code集合
        List<String> codeList = shipmentHeaders.stream().map(ShipmentHeader::getCode).collect(Collectors.toList());

        //先找出处于完成状态的taskDetail
        List<TaskDetail> taskDetails = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, status)
                .eq(TaskDetail::getInternalTaskType, QuantityConstant.SHIPMENT_CONTAINER_FINISHED)
                .in(TaskDetail::getBillCode, codeList));

        if (taskDetails == null || taskDetails.size() < 1) {
            return AjaxResult.error("没有完成状态的子任务");
        }

        Set<String> billCodes = new HashSet<>();
        final List<String> collect = taskDetails.stream().map(t -> t.getBillCode()).distinct().collect(Collectors.toList());


        for (TaskDetail taskDetail : taskDetails) {
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                    .eq(ShipmentHeader::getCode, taskDetail.getBillCode()));
            if (shipmentHeader == null) {
                continue;
            }
            if (shipmentHeader != null && shipmentHeader.getFirstStatus() == 1000) {
                continue;
            }
            shipmentHeader.setFirstStatus(1000);
            //更新状态为1000
            shipmentHeaderService.updateById(shipmentHeader);
            //修改任务明细状态
            taskDetail.setStatus(200);
            taskDetailService.updateById(taskDetail);
        }
        return AjaxResult.success();
    }


    @Transactional
    public AjaxResult ConfirmShipment(TaskDetail taskDetail, String url, Integer status) {
        String currentWarehouseCode = taskDetail.getWarehouseCode();
        AjaxResult ajaxResult = new AjaxResult();
        //根据子任务查找主单
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getCode, taskDetail.getBillCode()));
        if (shipmentHeader == null) {
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_REMOVE);
            taskDetailService.updateById(taskDetail);
            return AjaxResult.success("出库单已删除");
        }
        //查找相同单号的状态为100的子任务
        List<TaskDetail> taskDetails = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, status)
                .eq(TaskDetail::getBillCode, shipmentHeader.getCode()));
        if (taskDetails == null || taskDetails.size() < 1) {
            return AjaxResult.success("已完成");
        }
        //查找相同单号状态为10的子任务
        List<TaskDetail> taskDetailList = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, QuantityConstant.TASK_STATUS_RELEASE)
                .eq(TaskDetail::getBillCode, shipmentHeader.getCode()));

        if (taskDetailList != null && taskDetailList.size() > 0) {
            return AjaxResult.error("该单据还有任务未完成,请先完成该单据的任务");
        }

        String type = shipmentHeader.getShipmentType();
        String code = shipmentHeader.getReferCode();

        if (!type.equals("PRO") && !type.equals("MO") && !type.equals("QO") && !type.equals("SO")) {
            for (TaskDetail item : taskDetails) {
                item.setStatus(110);
                taskDetailService.updateById(item);
            }
            if (shipmentHeader.getFirstStatus() == 300 && shipmentHeader.getLastStatus() == 300) {
                shipmentHeaderService.removeShipmentByHeaderId(shipmentHeader.getId());
            }
            return AjaxResult.error("该子任务所属单据无需上传");
        }


        //根据子任务查找子单
        Set<String> uwarehouseCodes = new HashSet<>();
        List<ShipmentDetail> shipmentDetails = new ArrayList<>();
        Integer number = 0;
        for (TaskDetail item : taskDetails) {
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(item.getBillDetailId());

            if (shipmentDetails != null && shipmentDetails.size() > 0) {
                for (ShipmentDetail detail1 : shipmentDetails) {
                    if (shipmentDetail.getId() == detail1.getId()) {
                        detail1.setTaskQty(detail1.getTaskQty().add(item.getQty()));
                        number = 1;
                        break;
                    }
                }
            }
            if (number == 0) {
                shipmentDetail.setTaskQty(item.getQty());
                shipmentDetails.add(shipmentDetail);
                uwarehouseCodes.add(shipmentDetail.getUWarehouseCode());
            }
            number = 0;

        }

        Iterator<String> iterator = uwarehouseCodes.iterator();
        String uWarehouseCode = iterator.next();

        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getCompanyCode, shipmentHeader.getCompanyCode()));


        boolean isSameDate = icsReceiptAPIService.isSameMonth(taskDetail.getLastUpdated());

        if (isSameDate == false) {
            for (TaskDetail item : taskDetails) {
                item.setStatus(180);
                taskDetailService.updateById(item);
            }
//            documentWarningService.ErpReturnWarning(shipmentHeader.getCode(), shipmentHeader.getReferCode(),
//                "回传超时" + shipmentHeader.getUserDef2());
            return AjaxResult.error("取消回传");
        }

        List<ICSShipmentDetail> icsShipmentDetails = new ArrayList<ICSShipmentDetail>();
        String dt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS_S, taskDetail.getLastUpdated());
        String userName = taskDetail.getLastUpdatedBy();
        User user = new User();
        user = userService.selectUserByLoginName(userName);
        if (user != null) {
            userName = user.getUserName();
        }
        ICSShipmentHeader icsShipmentHeader = null;
        //出库类型为采购的主单和子单
        switch (type) {
            case "PRO":
                icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCCode, code)
                        .eq(ICSShipmentHeader::getCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setDPODate(dt);
                icsShipmentHeader.setCWHCode(uWarehouseCode);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setID(shipmentHeader.getId());
                icsShipmentHeader.setVoucherType("CreateICSRdRecord_01_Return");

                //子单
                for (ShipmentDetail item : shipmentDetails) {
                    if (uWarehouseCode.equals(item.getUWarehouseCode())) {
                        ICSShipmentDetail icsShipmentDetail = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                                .eq(ICSShipmentDetail::getCCode, code)
                                .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                                .eq(ICSShipmentDetail::getCInvCode, item.getMaterialCode()));
                        icsShipmentDetail.setID(item.getId());
                        icsShipmentDetail.setIArrsId(icsShipmentDetail.getAutoID());
                        icsShipmentDetail.setIPOsID(icsShipmentDetail.getPODID());
                        icsShipmentDetail.setIQuantity(item.getQtyCompleted().negate());
                        icsShipmentDetails.add(icsShipmentDetail);
                    }
                }
                break;
            case "MO":
                icsShipmentHeader.setVoucherType("UpdateICSRdRecord_11_Confirm");
            case "QO":
                //出库类型为材料或其他的主单和子单
                icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCCode, code)
                        .eq(ICSShipmentHeader::getCWHCode, shipmentHeader.getUWarehouseCode())
                        .eq(ICSShipmentHeader::getCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setDDate(dt);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setVoucherType("UpdateICSRdRecord_09_Confirm");
                for (ShipmentDetail item : shipmentDetails) {

                    ICSShipmentDetail icsShipmentDetailA = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                            .eq(ICSShipmentDetail::getCCode, code)
                            .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                            .eq(ICSShipmentDetail::getAutoID, item.getAutoId()));
                    icsShipmentDetailA.setIQuantity(item.getTaskQty());
                    icsShipmentDetails.add(icsShipmentDetailA);
                }
                break;
            case "SO":
                icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCDLCode, code)
                        .eq(ICSShipmentHeader::getCompanyCode, companyWu.getUCompanyCode()));

                icsShipmentHeader.setDDate(dt);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setCWHCode(shipmentHeader.getUWarehouseCode());

                WarehouseU8 warehouseU8 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                        .eq(WarehouseU8::getUWarehouseCode, shipmentHeader.getUWarehouseCode())
                        .eq(WarehouseU8::getUCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setCWHName(warehouseU8.getUWarehouseName());
                icsShipmentHeader.setCCode(shipmentHeader.getSourceCode());
                icsShipmentHeader.setVoucherType("CreateICSRdRecord_32");
                icsShipmentHeader.setID(icsShipmentHeader.getDLID());
                for (ShipmentDetail item : shipmentDetails) {

                    ICSShipmentDetail icsShipmentDetailB = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                            .eq(ICSShipmentDetail::getCCode, code)
                            .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                            .eq(ICSShipmentDetail::getCInvCode, item.getMaterialCode())
                            .eq(ICSShipmentDetail::getAutoID, item.getAutoId()));

                    icsShipmentDetailB.setCCode(code);
                    icsShipmentDetailB.setID(icsShipmentDetailB.getDLID());
                    icsShipmentDetailB.setAutoID(icsShipmentDetailB.getAutoID());
                    icsShipmentDetailB.setIQuantity(item.getTaskQty());
                    icsShipmentDetails.add(icsShipmentDetailB);
                }
                break;
        }


        if (type.equals("MO")) {
            icsShipmentHeader.setVoucherType("UpdateICSRdRecord_11_Confirm");
        }
        if (type.equals("QO")) {
            icsShipmentHeader.setVoucherType("UpdateICSRdRecord_09_Confirm");
        }

        ICSShipmentModel model = new ICSShipmentModel();
        model.setIcsShipmentHeader(icsShipmentHeader);
        model.setIcsShipmentDetails(icsShipmentDetails);
        String JsonParam = JSON.toJSONString(model);
        String result = HttpUtils.bodypost(url, JsonParam, currentWarehouseCode);
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
        }
        ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.getCode() == 200 || ajaxResult.getCode() == 400) {


            if (shipmentHeader.getShipmentType().equals(QuantityConstant.U8_SHIPMENT_TYPE_SO) || shipmentHeader.getShipmentType().equals(QuantityConstant.U8_SHIPMENT_TYPE_PRO)) {
                shipmentHeader.setUserDef2(ajaxResult.getMsg());
                shipmentHeaderService.updateById(shipmentHeader);
            }

            for (TaskDetail item : taskDetails) {
                item.setStatus(120);
                taskDetailService.updateById(item);
            }

            /**
             *  生成快递单
             *  1、U8返回值为200
             *  2、wms出库单快递信息存在
             *  3、存在就生成快递单
             *
             */
//            if(ajaxResult.getCode() == 200 && shipmentHeader.getKdEable() == 1){
//                AjaxResult ajaxResult1 = kuaidiHeaderService.saveKD(shipmentDetails,shipmentHeader,ajaxResult.getMsg());
//            }

            if (ajaxResult.getCode() == 400) {
//                documentWarningService.ErpReturnWarning(shipmentHeader.getCode(), shipmentHeader.getSourceCode(), ajaxResult.getMsg());
            }

            if (shipmentHeader.getFirstStatus() == QuantityConstant.SHIPMENT_HEADER_GROUPDISK && shipmentHeader.getLastStatus() != QuantityConstant.SHIPMENT_HEADER_GROUPDISK) {
                shipmentHeader.setUploadStatus(15);
                shipmentHeaderService.updateById(shipmentHeader);
            }
            TaskDetail condition = new TaskDetail();
            condition.setBillCode(shipmentHeader.getCode());

            //select  * from task_detail where billCode = #{billCode} and billId = #{billId} and (status &lt;= 100 or status = 150 or status= 160)
            LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(TaskDetail::getBillCode, shipmentHeader.getCode())
                    .lt(TaskDetail::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                    .or()
                    .eq(TaskDetail::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                    .or()
                    .eq(TaskDetail::getStatus, 150)
                    .or()
                    .eq(TaskDetail::getStatus, 160);
            List<TaskDetail> taskDetails1 = taskDetailService.list(queryWrapper);
            if (shipmentHeader.getFirstStatus() == QuantityConstant.SHIPMENT_HEADER_GROUPDISK && shipmentHeader.getLastStatus() == QuantityConstant.SHIPMENT_HEADER_GROUPDISK && taskDetails1.isEmpty()) {
                shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                shipmentHeader.setUploadStatus(20);
                shipmentHeaderService.updateById(shipmentHeader);
            }
        }

        if (ajaxResult.getCode() == 300) {
            shipmentHeader.setUserDef2(ajaxResult.getMsg());
            shipmentHeaderService.updateById(shipmentHeader);
            for (TaskDetail item : taskDetails) {
                item.setStatus(160);
                taskDetailService.updateById(item);
            }
        }
        return ajaxResult;

    }


    /**
     * 判断出库单是否可以更新
     *
     * @param code U8单号
     */
    private void checkShipmentBill(boolean isFlag, String code, String type, String warehouseCode) {
        //U8单据类型
        LambdaQueryWrapper<ShipmentHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShipmentHeader::getReferCode, code)
                .eq(ShipmentHeader::getShipmentType, type)
                .eq(ShipmentHeader::getWarehouseCode, warehouseCode);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(queryWrapper);

        //WMS出库单 和 U8出库单 都不能为空 才能更新
        if (isFlag) {
            if (shipmentHeader != null) {
                if (StringUtils.isNotNull(shipmentHeader)) {
                    //找出 出库单未完成的任务
                    List<TaskDetail> taskDetailList = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                            .eq(TaskDetail::getBillCode, shipmentHeader.getCode())
                            .eq(TaskDetail::getWarehouseCode, shipmentHeader.getWarehouseCode())
                            .le(TaskDetail::getStatus, 100));
                    if (taskDetailList != null && taskDetailList.size() > 0) {
                        throw new ServiceException("更新失敗,此出库单存在任务");
                    }
                    //找出库配盘明细 未配盘的单据
                    List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(new LambdaQueryWrapper<ShipmentContainerDetail>()
                            .eq(ShipmentContainerDetail::getShipmentCode, shipmentHeader.getCode())
                            .eq(ShipmentContainerDetail::getWarehouseCode, shipmentHeader.getWarehouseCode())
                            .le(ShipmentContainerDetail::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED));

                    if (shipmentContainerDetails != null && shipmentContainerDetails.size() > 0) {
                        throw new ServiceException("更新失敗,此出库单已组盘");
                    }
                    //删除wms出库单头表
                    shipmentHeaderService.removeById(shipmentHeader.getId());
                    //删除wms出库单明细表
                    shipmentDetailService.remove(new LambdaQueryWrapper<ShipmentDetail>()
                            .eq(ShipmentDetail::getShipmentCode, shipmentHeader.getCode())
                            .eq(ShipmentDetail::getWarehouseCode, shipmentHeader.getWarehouseCode()));
                }
            }
            ICSShipmentHeader icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                    .eq(ICSShipmentHeader::getReferCode, code)
                    .eq(ICSShipmentHeader::getWarehouseCode, warehouseCode)
                    .eq(ICSShipmentHeader::getType, type));
            if (icsShipmentHeader != null) {
                icsShipmentHeaderService.removeById(icsShipmentHeader.getSId());
                icsShipmentDetailService.remove(new LambdaQueryWrapper<ICSShipmentDetail>()
                        .eq(ICSShipmentDetail::getHeaderId, icsShipmentHeader.getSId()));
            }
        }
    }

    private ICSShipmentModel assign(ICSShipmentModel icsShipmentModel) {
        ICSShipmentHeader header = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> details = icsShipmentModel.getIcsShipmentDetails();
        String type = header.getType();
        Date created = null;
        if (header.getDDate() != null) {
            try {
                created = DateUtils.parseDate(header.getDDate(), DateUtils.YYYY_MM_DD_HH_MM_SS);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        switch (type) {
            case "MO":
                header.setReferId(header.getID());
                header.setReferCode(header.getCCode());
                header.setCreated(created);
                header.setStock(header.getCWHCode());
                for (ICSShipmentDetail detail : details) {
                    detail.setReferId(detail.getAutoID());
                    detail.setReferCode(detail.getPOCode());
                }
                break;
            case "SO":
                header.setReferId(header.getDLID());
                header.setReferCode(header.getCDLCode());
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(), DateUtils.YYYY_MM_DD_HH_MM_SS_S));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String stock = "";
                stock = header.getCWHCode();
                for (ICSShipmentDetail detail : details) {
                    detail.setReferId(detail.getAutoID());
                    detail.setReferCode(detail.getPOCode());
                    if (StringUtils.isEmpty(stock)) {
                        stock = detail.getCWHCode();
                    }
                }
                header.setStock(stock);
                break;
            case "OO":
                header.setIsFlag(false);
                header.setStock("20");
                header.setCreated(created);
                header.setReferId(header.getMoId());
                header.setReferCode(header.getMoCode());
                for (ICSShipmentDetail detail : details) {
                    detail.setCWHCode("20");
                    detail.setReferId(detail.getMoDId());
                    detail.setAutoID(detail.getMoDId());
                }
                break;
            case "PRO":
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(), DateUtils.YYYY_MM_DD_HH_MM_SS_S));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                header.setReferId(header.getID());
                header.setReferCode(header.getCCode());
                String stock1 = "";
                for (ICSShipmentDetail detail : details) {
                    stock1 = detail.getCWHCode();
                    detail.setPOCode(detail.getPOCode());
                    detail.setPODID(detail.getPODID());
                    detail.setPOID(detail.getPOID());
                }
                header.setStock(stock1);
                header.setCWHCode(stock1);
                break;
        }
        icsShipmentModel.setIcsShipmentHeader(header);
        icsShipmentModel.setIcsShipmentDetails(details);
        return icsShipmentModel;
    }

    private String getShipmentTypeName(String code) {
        String typeName = "";
        switch (code) {
            case "MO":
                typeName = "生产领料";
                break;
            case "SO":
                typeName = "普通销售";
                break;
            case "OO":
                typeName = "生产订单";
                break;
        }
        return typeName;
    }

    /**
     * 其他出库单/调拨出库单
     *
     * @param icsShipmentModel
     * @return
     * @throws ParseException
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult otherShipment(ICSShipmentModel icsShipmentModel){

        //校验null
        checkOtherShipmentNull(icsShipmentModel);

        ICSShipmentHeader icsShipmentHeader = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails = icsShipmentModel.getIcsShipmentDetails();
        String shipmentHeaderType = icsShipmentHeader.getType();
        String warehouseCode = icsShipmentHeader.getWarehouseCode();
        String whCode = null;
        String referCode = null;
        String companyCode = null;
        String u8CompanyCode = icsShipmentHeader.getCompanyCode();
        Date created = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,icsShipmentHeader.getDDate());
        String createdBy = icsShipmentHeader.getCMaker();
        String shipmentNote = icsShipmentHeader.getCMemo();
        Integer referHeaderId = icsShipmentHeader.getID();
        BigDecimal totalQty = BigDecimal.ZERO;
        int totalLines = BigDecimal.ZERO.intValue();
        switch (icsShipmentHeader.getType()){
            case "TO": // 调拨其他出库单
                whCode = icsShipmentHeader.getIWHCode();
                referCode = icsShipmentHeader.getCTVCode();
                break;
            case "QO": //其他出库单
                whCode = icsShipmentHeader.getCWHCode();
                referCode = icsShipmentHeader.getCCode();
                break;
            default:
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        List<ShipmentDetail> shipmentDetails = new ArrayList<>();

        WarehouseU8 warehouseU8 = warehouseU8Service.getWarehouseU8ByUCompanyUWarehouseCode(u8CompanyCode,whCode,warehouseCode);
        if(warehouseU8 == null){
            throw new ServiceException("未找到库房代码:"+whCode);
        }
        companyCode = warehouseU8.getCompanyCode();

        shipmentHeader.setReferCode(referCode);
        shipmentHeader.setReferId(referHeaderId);
        shipmentHeader.setShipmentType(shipmentHeaderType);
        shipmentHeader.setWarehouseCode(icsShipmentHeader.getWarehouseCode());
        shipmentHeader.setUWarehouseCode(warehouseU8.getUWarehouseCode());
        shipmentHeader.setCompanyCode(companyCode);
        shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
        shipmentHeader.setCreated(created);
        shipmentHeader.setCreatedBy(createdBy);
        shipmentHeader.setShipmentNote(shipmentNote);

        for (ICSShipmentDetail icsShipmentDetail : icsShipmentDetails) {
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setReferId(icsShipmentDetail.getAutoID()); //其他出库单/调拨单 子表id
            shipmentDetail.setAutoId(icsShipmentDetail.getAutoID());
            shipmentDetail.setProjectNo(icsShipmentDetail.getCItemName());
            shipmentDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_BUILD.toString());
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail.setUWarehouseCode(whCode);
            shipmentDetail.setCompanyCode(companyCode);
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
            shipmentDetail.setCreatedBy(createdBy);
            shipmentDetail.setCreated(created);
            Material material = materialService.getMaterialByCode(icsShipmentDetail.getCInvCode(), warehouseCode);
            if(material == null){
                throw new ServiceException("物料编码未找到"+icsShipmentDetail.getCInvCode());
            }
            shipmentDetail.setMaterialCode(material.getCode());
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            BigDecimal qty = icsShipmentDetail.getIQuantity();
            totalQty = totalQty.add(qty);
            shipmentDetail.setQty(qty);
            totalLines += 1;
            shipmentDetails.add(shipmentDetail);
        }
        shipmentHeader.setTotalQty(totalQty);
        shipmentHeader.setTotalLines(totalLines);

        ShipmentDomain shipmentDomain = new ShipmentDomain();
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);

        AjaxResult result = shipmentApiService.shipment(shipmentDomain);
        if(result.hasErr()){
            throw new ServiceException(result.getMsg());
        }
        if("0".equals(icsShipmentHeader.getIsNew())){
            return AjaxResult.success("出库单更新成功");
        }
        return AjaxResult.success("出库单下发成功");
    }


    public void checkOtherShipmentNull(ICSShipmentModel icsShipmentModel){
        ICSShipmentHeader icsShipmentHeader = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails = icsShipmentModel.getIcsShipmentDetails();

        String type = icsShipmentHeader.getType();
        String currentWarehouseCode = icsShipmentHeader.getWarehouseCode();
        if (StringUtils.isEmpty(currentWarehouseCode)) {
            throw new ServiceException("仓库编码warehouseCode:不能为空");
        }
        if(StringUtils.isEmpty(icsShipmentHeader.getCompanyCode())){
            throw new ServiceException("公司账套CompanyCode 不能为空");
        }
        if(StringUtils.isEmpty(type) || shipmentTypeCheck(type)){
            throw new ServiceException("出库库类型不存在");
        }
        if (StringUtils.isEmpty(icsShipmentHeader.getIsNew())) {
            throw new ServiceException("isNew为空");
        }

        String referCode = null;
        Integer referId = icsShipmentHeader.getID();
        String dDateStr = icsShipmentHeader.getDDate();
        String whCode = null;
        String busTypeName = getShipmentTypeName(type);
        switch (type){
            case "TO": //
                whCode = icsShipmentHeader.getIWHCode();
                referCode = icsShipmentHeader.getCTVCode();
                break;
            case "QO": //其他出库单
                whCode = icsShipmentHeader.getCWHCode();
                referCode = icsShipmentHeader.getCCode();
                break;
            default:
        }
        if(StringUtils.isEmpty(referCode)){
            throw new ServiceException(busTypeName + "业务Code 不能为空:"+referCode);
        }
        if(StringUtils.isNull(referId)){
            throw new ServiceException(busTypeName + "业务ID 不能为空:"+referId);
        }
        if(StringUtils.isEmpty(dDateStr)){
            throw new ServiceException(busTypeName + "业务入库日期 不能为空:"+dDateStr);
        }
        if(StringUtils.isEmpty(whCode)){
            throw new ServiceException(busTypeName + "业务入库库房代码 不能为空:"+whCode);
        }

        boolean isUpdate = "0".equals(icsShipmentHeader.getIsNew()) ? true : false;

        Warehouse warehouse = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>()
                .eq(Warehouse::getCode, currentWarehouseCode));
        if (warehouse == null) {
            throw new ServiceException("WMS系统中没有仓库编号:【" + currentWarehouseCode + "】的仓库。");
        }
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, referCode)
                .eq(U8Reference::getReferType, type)
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
        //入库单表 检查上游单号是否重复
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, referCode)
                .eq(ShipmentHeader::getWarehouseCode, currentWarehouseCode)
                .last("LIMIT 1"));
        if (one != null && !isUpdate && shipmentHeader1 != null) {
            throw new ServiceException(busTypeName + ":请不要重复下发 " + referCode);
        }

        U8Reference u8Reference = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferType, icsShipmentHeader.getType())
                .eq(U8Reference::getReferCode, referCode)
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode)
                .last(" LIMIT 1"));
        if(u8Reference == null){
            U8Reference reference = new U8Reference();
            reference.setReferType(icsShipmentHeader.getType());
            reference.setReferCode(referCode);
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(busTypeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }

    }

    private boolean shipmentTypeCheck(String code) {
        switch (code) {
            case "TO":
            case "QO":
                return false;
            default:
                return true;
        }
    }


}