ICSShipmentApiService.java 44.6 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
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.WarehouseU8;
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.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;




        //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::getWarehouseCode,currentWarehouseCode));
        if(!isFlag){
            if(one != null && shipmentHeader1 != null){
                return AjaxResult.error(":请不要重复下发 "+icsShipmentHeader.getReferCode());
            }
        }
        if(icsShipmentHeader.getIsFlag()){
            checkShipmentBill(isFlag,icsShipmentHeader.getReferCode(),icsShipmentHeader.getType());
        }

        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());
        }
        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));
        //入库单表 检查上游单号是否重复
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, icsShipmentHeader.getReferCode()));
        if(one != null && !isFlag && shipmentHeader1 != null){
            return AjaxResult.error(typeName + ":请不要重复下发 "+icsShipmentHeader.getReferCode());
        }
        //判断出库单是否可以更新
        checkShipmentBill(isFlag,code,icsShipmentHeader.getType());

        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("下发失败");
        }


        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(icsShipmentHeader.getStock());
        shipmentHeader.setCompanyCode(companyWu.getCompanyCode());
        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) {
            //查找物料
            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(companyWu.getCompanyCode());
            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);
        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(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){
        //U8单据类型
        LambdaQueryWrapper<ShipmentHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShipmentHeader::getReferCode,code)
                .eq(ShipmentHeader::getShipmentType,type);
        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())
                            .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())
                            .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()));
                }
            }
            ICSShipmentHeader icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                    .eq(ICSShipmentHeader::getReferCode, code)
                    .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;
    }

}