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


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.referenceCode.domain.U8Reference;
import com.huaheng.pc.referenceCode.service.U8ReferenceService;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.company.domain.CompanyU8;
import com.huaheng.pc.config.company.service.CompanyU8Service;
import com.huaheng.pc.u8.domain.*;
import com.huaheng.api.general.domain.ReceiptDomain;
import com.huaheng.api.general.service.ReceiptApiService;
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.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.service.CompanyService;
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.WarehouseService;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

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

/**
 * u8-wms入库下发或回传接口服务层
 *  @author huaheng
 *  @date 2019-1-4
 *
 */

@Service
public class ICSReceiptAPIService {

    @Resource
    private WarehouseU8Service warehouseU8Service;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    @Resource
    private IDocumentWarningService documentWarningService;
    @Resource
    private ICSReceiptHeaderService icsReceiptHeaderService;
    @Resource
    private ICSReceiptDetailService icsReceiptDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ReceiptApiService receiptApiService;
    @Resource
    private CompanyU8Service companyU8Service;
    @Resource
    private AddressService addressService;
    @Resource
    private U8ReferenceService u8ReferenceService;




    //入库单下发
    //采购入库
    @Transactional
    public AjaxResult procureReceipt(@RequestBody ICSReceiptModel icsReceiptModel) {
        icsReceiptModel = assign(icsReceiptModel);
        ICSReceiptHeader icsReceiptHeader=icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> icsReceiptDetails=icsReceiptModel.getIcsReceiptDetails();

        //是否更新
        if(StringUtils.isEmpty(icsReceiptHeader.getIsNew())){
            return AjaxResult.error("isNew为空");
        }
        boolean isFlag = "0".equals(icsReceiptHeader.getIsNew()) ? true : false;
        //仓库编码
        String currentWarehouseCode = icsReceiptHeader.getWarehouseCode();
        if(StringUtils.isEmpty(currentWarehouseCode)){
            return AjaxResult.error("仓库编码warehouseCode:不能为空");
        }
        //入库类型
        String type=icsReceiptHeader.getType();
        if (receiptTypeCheck(type)) {
            return AjaxResult.error("入库类型不匹配");
        }
        String typeName = getReceiptTypeName(type);
        String stock=icsReceiptHeader.getStock();
        //U8companyCode
        String U8CompanyCode = icsReceiptHeader.getCompanyCode();
        //判断U8货主
        CompanyU8 companyU8 = companyU8Service.getCompanyByU8CodeWarehouseCode(U8CompanyCode, currentWarehouseCode);
        if(companyU8==null){
            return AjaxResult.error("系统没有此货主:【"+U8CompanyCode+"】");
        }
        companyService.checkwarehouseCompany(companyU8.getCompanyCode());
        //判断U8仓库
        if(!StringUtils.isEmpty(stock)){
            LambdaQueryWrapper<WarehouseU8> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(WarehouseU8::getUCompanyCode,icsReceiptHeader.getCompanyCode())
                    .eq(WarehouseU8::getUWarehouseCode,stock);
            WarehouseU8 one = warehouseU8Service.getOne(queryWrapper);
            if(one==null){
                return AjaxResult.error("系统没有此仓库:【"+stock+"】");
            }
        }
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsReceiptHeader.getReferCode())
                .eq(U8Reference::getReferType, icsReceiptHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
        //入库单表 检查上游单号是否重复
        ReceiptHeader receiptHeader1 = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>()
                .eq(ReceiptHeader::getReferCode, icsReceiptHeader.getReferCode()));
        if(one != null && !isFlag && receiptHeader1 != null){
            return AjaxResult.error(typeName + ":请不要重复下发 "+icsReceiptHeader.getReferCode());
        }
        //判断是否更新
        checkReceiptBill(isFlag, icsReceiptHeader.getReferCode(),icsReceiptHeader.getType());

        boolean save = icsReceiptHeaderService.save(icsReceiptHeader);
        if(!save){
            throw new ServiceException("下发失败");
        }
        for (ICSReceiptDetail icsReceiptDetail : icsReceiptDetails) {
            icsReceiptDetail.setHeaderId(icsReceiptHeader.getRid());
        }
        save = icsReceiptDetailService.saveBatch(icsReceiptDetails);
        if(!save){
            throw new ServiceException("下发失败");
        }


        //根据icsReceiptHeader生成receiptHeader
        ReceiptHeader receiptHeader=new ReceiptHeader();
        String maker = icsReceiptHeader.getCMaker();
        receiptHeader.setReceiptType(type);
        receiptHeader.setReferId(icsReceiptHeader.getReferId());
        receiptHeader.setCreatedBy(maker);
        receiptHeader.setUWarehouseCode(icsReceiptHeader.getStock());
        receiptHeader.setCreated(icsReceiptHeader.getCreated());
        receiptHeader.setTypeName(icsReceiptHeader.getCBusType());
        receiptHeader.setDeptCode(icsReceiptHeader.getCDepCode());
        receiptHeader.setCDeptName(icsReceiptHeader.getCDepName());
        receiptHeader.setReceiptNote(icsReceiptHeader.getCMemo());
        receiptHeader.setSupplierCode(icsReceiptHeader.getCVenCode());
        receiptHeader.setWarehouseCode(currentWarehouseCode);
        receiptHeader.setReferCode(icsReceiptHeader.getReferCode());
        receiptHeader.setCompanyCode(companyU8.getCompanyCode());
        //根据icsShipmentDetail生成shipmentAetail
        List<ReceiptDetail> receiptDetails = new ArrayList<ReceiptDetail>();
        for (ICSReceiptDetail item : icsReceiptDetails) {
            //查找物料
            LambdaQueryWrapper<Material> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(Material::getCode,item.getCInvCode())
                    .eq(Material::getWarehouseCode,currentWarehouseCode);
            Material material = materialService.getOne(queryWrapper1);
            if (material == null) {
//                documentWarningService.ErpSendWarning(receiptHeader.getReferCode(), "该物料不存在:" + item.getCInvCode());
                throw new ServiceException("该物料不存在:" + item.getCInvCode());
            }

            ReceiptDetail receiptDetail = new ReceiptDetail();
            receiptDetail.setAutoId(item.getAutoID());
            receiptDetail.setReferId(item.getReferId());
            if (icsReceiptHeader.getType().equals("TI")) {
                receiptDetail.setAutoId(item.getITrIds());
            }
            receiptDetail.setReferCode(receiptHeader.getReferCode());
            receiptDetail.setMoCode(item.getMoCode());
            if(StringUtils.isNotEmpty(item.getCDefine22())) {
                Boolean flag = StringUtils.isChineseChar(item.getCDefine22());
                if (item.getCDefine22().length() == 19 && flag == false) {
                    receiptDetail.setMoCode(item.getCDefine22());
                } else {
                    receiptDetail.setBatch(item.getCDefine22());
                }
            }
            receiptDetail.setLot(item.getCDefine23());
            receiptDetail.setProjectNo(item.getCDefine23());
            receiptDetail.setWarehouseCode(currentWarehouseCode);
            receiptDetail.setMaterialCode(item.getCInvCode());
            receiptDetail.setZoneCode(material.getZoneCode());
            receiptDetail.setCreated(icsReceiptHeader.getCreated());
            receiptDetail.setMaterialUnit(item.getCComUnitName());
            receiptDetail.setCreatedBy(maker);
            receiptDetail.setCompanyCode(companyU8.getCompanyCode());
            receiptDetail.setProjectNo(item.getCItemName());
            receiptDetail.setQty(item.getIQuantity().abs());
            receiptDetail.setInventorySts(QuantityConstant.GOOD);
            if (item.getIrowno() != null && StringUtils.isNotEmpty(item.getIrowno().toString())) {
                receiptDetail.setReferLineNum(item.getIrowno().toString());
            }
            receiptDetails.add(receiptDetail);
        }

        //调用公共API
        ReceiptDomain receiptDomain = new ReceiptDomain();
        receiptDomain.setReceiptHeader(receiptHeader);
        receiptDomain.setReceiptDetails(receiptDetails);
        AjaxResult ajaxResult = receiptApiService.receipt(receiptDomain);
        if(!ajaxResult.hasErr() && !isFlag){
            U8Reference reference = new U8Reference();
            reference.setReferType(icsReceiptHeader.getType());
            reference.setReferCode(icsReceiptHeader.getReferCode());
            reference.setStock(stock);
            reference.setCompanyCode(companyU8.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
        if(ajaxResult.getCode()==200&& !isFlag ){
            return AjaxResult.success("入库单下发成功");
        }else if (ajaxResult.getCode()==200 && isFlag) {
            return AjaxResult.success("入库单更新成功");
        }else {
            return ajaxResult;
        }
    }

    /**
     * 采购到货 or 退货入库
     * 采购到货单 | 销售退货入库单
     * @param icsReceiptModel
     * @return
     * @throws ParseException
     */
    @Transactional
    public AjaxResult procureArrive(ICSReceiptModel icsReceiptModel){
        icsReceiptModel = assign(icsReceiptModel);
        ICSReceiptHeader icsReceiptHeader = icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> icsReceiptDetails = icsReceiptModel.getIcsReceiptDetails();
        //是否更新
        if(StringUtils.isEmpty(icsReceiptHeader.getIsNew())){
            return AjaxResult.error("isNew为空");
        }
        boolean isUpdate = "0".equals(icsReceiptHeader.getIsNew()) ? true : false;
        String currentWarehouseCode = icsReceiptHeader.getWarehouseCode();
        if (StringUtils.isEmpty(currentWarehouseCode)) {
            return AjaxResult.error("仓库编码warehouseCode:不能为空");
        }
        Warehouse warehouse = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>()
                .eq(Warehouse::getCode, currentWarehouseCode));
        if(warehouse==null){
            return AjaxResult.error("本系统中没有编号:【"+currentWarehouseCode+"】的仓库。");
        }
        //入库类型
        String type = icsReceiptHeader.getType();
        if (receiptTypeCheck(type)) {
            return AjaxResult.error("入库类型不匹配");
        }
        String typeName = getReceiptTypeName(type);
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsReceiptHeader.getReferCode())
                .eq(U8Reference::getReferType, icsReceiptHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
        //入库单表 检查上游单号是否重复
        ReceiptHeader receiptHeader1 = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>()
                .eq(ReceiptHeader::getReferCode, icsReceiptHeader.getReferCode()));
        if(one != null && !isUpdate && receiptHeader1 != null){
            return AjaxResult.error(typeName + ":请不要重复下发 "+icsReceiptHeader.getReferCode());
        }
        checkReceiptBill(isUpdate, icsReceiptHeader.getReferCode(),icsReceiptHeader.getType());

        boolean save = icsReceiptHeaderService.save(icsReceiptHeader);
        if(!save){
            throw new ServiceException("下发失败");
        }
        for (ICSReceiptDetail icsReceiptDetail : icsReceiptDetails) {
            icsReceiptDetail.setHeaderId(icsReceiptHeader.getRid());
        }
        save = icsReceiptDetailService.saveBatch(icsReceiptDetails);
        if(!save){
            throw new ServiceException("下发失败");
        }


        String maker=icsReceiptHeader.getCMaker();



        CompanyU8 companyU81 = companyU8Service.getCompanyByU8CodeWarehouseCode(icsReceiptHeader.getCompanyCode(),currentWarehouseCode);

        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setReferId(icsReceiptHeader.getReferId());
        receiptHeader.setReceiptType(icsReceiptHeader.getType());
        receiptHeader.setCreatedBy(maker);
        receiptHeader.setUWarehouseCode(icsReceiptHeader.getCWHCode());
        receiptHeader.setCreated(icsReceiptHeader.getCreated());
        receiptHeader.setReferCode(icsReceiptHeader.getReferCode());
        receiptHeader.setSupplierCode(icsReceiptHeader.getCVenCode());
        receiptHeader.setSupplierName(icsReceiptHeader.getCVenName());
        receiptHeader.setWarehouseCode(companyU81.getWarehouseCode());
        receiptHeader.setCompanyCode(companyU81.getCompanyCode());
        receiptHeader.setCustomerCode(icsReceiptHeader.getCCusCode());
        receiptHeader.setDeptCode(icsReceiptHeader.getCDepCode());
        receiptHeader.setCDeptName(icsReceiptHeader.getCDepName());
        receiptHeader.setPersonCode(icsReceiptHeader.getCPersonCode());
        List<ReceiptDetail> receiptDetails = new ArrayList<>();

        for (ICSReceiptDetail icsReceiptDetail : icsReceiptDetails) {
            ReceiptDetail receiptDetail = new ReceiptDetail();
            if(!StringUtils.isEmpty(icsReceiptDetail.getCWHCode())){
                WarehouseU8 warehouseU8 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                        .eq(WarehouseU8::getWarehouseCode, currentWarehouseCode)
                        .eq(WarehouseU8::getUWarehouseCode,icsReceiptDetail.getCWHCode())
                        .eq(WarehouseU8::getUCompanyCode, icsReceiptDetail.getCompanyCode()));
                receiptDetail.setUWarehouseCode(warehouseU8.getUWarehouseCode());
            }
            receiptDetail.setLot(icsReceiptDetail.getCDefine23());
            receiptDetail.setReferCode(icsReceiptDetail.getReferCode());
            receiptDetail.setSoCode(icsReceiptDetail.getCSOCode());
            receiptDetail.setSoId(icsReceiptDetail.getCSOID());
            receiptDetail.setSoAutoId(icsReceiptDetail.getCSOAutoID());
            receiptDetail.setReferId(icsReceiptDetail.getReferId());
            receiptDetail.setAutoId(icsReceiptDetail.getAutoID());
            if (icsReceiptDetail.getIrowno() != null && StringUtils.isNotEmpty(icsReceiptDetail.getIrowno().toString())) {
                receiptDetail.setReferLineNum(icsReceiptDetail.getIrowno().toString());
            }

            if(StringUtils.isNotEmpty(icsReceiptDetail.getCDefine22())) {
                Boolean flag = StringUtils.isChineseChar(icsReceiptDetail.getCDefine22());
                if (icsReceiptDetail.getCDefine22().length() == 19 && flag == false) {
                    receiptDetail.setMoCode(icsReceiptDetail.getCDefine22());
                } else {
                    receiptDetail.setBatch(icsReceiptDetail.getCDefine22());
                }
            }
            receiptDetail.setWarehouseCode(currentWarehouseCode);
            receiptDetail.setMaterialCode(icsReceiptDetail.getCInvCode());
            receiptDetail.setCreated(icsReceiptHeader.getCreated());
            receiptDetail.setMaterialUnit(icsReceiptDetail.getCComUnitName());
            receiptDetail.setCreatedBy(maker);
            if(icsReceiptHeader.getType().equals("SRI")){
                receiptDetail.setCPoCode(icsReceiptDetail.getCSOCode());
            }else {
                receiptDetail.setCPoCode(icsReceiptDetail.getPOCode());
            }
            receiptDetail.setCompanyCode(companyU81.getCompanyCode());
            receiptDetail.setProjectNo(icsReceiptDetail.getCItemName());
            receiptDetail.setQty(icsReceiptDetail.getIQuantity().abs());
            receiptDetail.setInventorySts(QuantityConstant.GOOD);
            receiptDetails.add(receiptDetail);

            if(icsReceiptDetail.getMoCode()!=null){
                try{
                    JSONObject json=new JSONObject();
                    json.put("code",icsReceiptHeader);
                    json.put("materialCode",icsReceiptDetail.getCInvCode().trim());
                    json.put("workNo",icsReceiptDetail.getMoCode().trim());
                    json.put("status","1");
                    String JsonParam = JSON.toJSONString(json);
                    System.out.println(JsonParam);
                    //调用mms更改采购状态
                    if(currentWarehouseCode.equals("CS0001")){
                        String url = addressService.selectAddress("mms",currentWarehouseCode);
                        String result2 = HttpUtils.bodypost(url, JsonParam,currentWarehouseCode);
                    }

                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //调用公共API
        ReceiptDomain receiptModel = new ReceiptDomain();
        receiptModel.setReceiptHeader(receiptHeader);
        receiptModel.setReceiptDetails(receiptDetails);
        AjaxResult ajaxResult = receiptApiService.receipt(receiptModel);
        if(!ajaxResult.hasErr() && !isUpdate){
            U8Reference reference = new U8Reference();
            reference.setReferType(icsReceiptHeader.getType());
            reference.setReferCode(icsReceiptHeader.getReferCode());
            reference.setCompanyCode(companyU81.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
        if(ajaxResult.getCode()==200&& !isUpdate ){
            return AjaxResult.success("入库单下发成功");
        }else if (ajaxResult.getCode()==200&& isUpdate) {
            return AjaxResult.success("入库单更新成功");
        }else {
            return ajaxResult;
        }
    }

    private ICSReceiptModel assign(ICSReceiptModel icsReceiptModel){
        ICSReceiptHeader header = icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> details = icsReceiptModel.getIcsReceiptDetails();
        String type = header.getType();
        Date created = null;
        if(header.getDDate() !=null){
            try {
                created = DateUtils.parseDate(header.getDDate(),DateUtils.YYYY_MM_DD_HH_MM_SS_S);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        switch (type){
            case "PI" :
                header.setReferCode(header.getCCode());
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(),DateUtils.YYYY_MM_DD_HH_MM_SS));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String uwarehouseCode = "";
                for (ICSReceiptDetail detail : details) {
                    detail.setReferId(detail.getPODID());
                    detail.setReferCode(detail.getPOCode());
                    if(StringUtils.isEmpty(detail.getCWHCode())){
                        detail.setCWHCode(header.getIWHCode());
                    }
                    uwarehouseCode = detail.getCWHCode();
                }
                if(StringUtils.isEmpty(header.getIWHCode())){
                    header.setCWHCode(uwarehouseCode);
                }
                break;

            case "SRI" :
                header.setReferId(header.getDLID());
                header.setReferCode(header.getCDLCode());
                header.setCreated(created);
                String uwarehouseCode1 = "";
                for (ICSReceiptDetail detail : details) {
                    detail.setReferId(detail.getCSOAutoID());
                    detail.setReferCode(detail.getCSOCode());
                    if(StringUtils.isEmpty(detail.getCWHCode())){
                        detail.setCWHCode(header.getIWHCode());
                    }
                    uwarehouseCode1 = detail.getCWHCode();
                }
                if(StringUtils.isEmpty(header.getIWHCode())){
                    header.setIWHCode(uwarehouseCode1);
                    header.setCWHCode(uwarehouseCode1);
                }
                break;
            case "PCI" :
                header.setReferCode(header.getCPOID());
                header.setReferId(header.getPOID());
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(),DateUtils.YYYY_MM_DD_HH_MM_SS_S));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String uwarehouseCode2 = "";
                for (ICSReceiptDetail detail : details) {
                    detail.setReferCode(header.getReferCode());
                    detail.setReferId(detail.getPODID());
                    if(StringUtils.isEmpty(detail.getCWHCode())){
                        detail.setCWHCode(header.getIWHCode());
                    }
                }
                if(StringUtils.isEmpty(header.getIWHCode())){
                    header.setIWHCode(uwarehouseCode2);
                    header.setCWHCode(uwarehouseCode2);
                }
                break;
            case "CI" :
            default:
                header.setReferId(header.getID());
                header.setReferCode(header.getCCode());
                header.setStock(header.getCWHCode());
                header.setCreated(created);
                for (ICSReceiptDetail detail : details) {
                    detail.setReferCode(header.getReferCode());
                }
                break;
        }
        icsReceiptModel.setIcsReceiptHeader(header);
        icsReceiptModel.setIcsReceiptDetails(details);
        return icsReceiptModel;
    }

    /**
     * 判断入库单是否可以更新
     * @param code  U8单号
     */
    private  void  checkReceiptBill(boolean isFlag,String code,String type){
        LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReceiptHeader::getReferCode,code)
                .eq(ReceiptHeader::getReceiptType,type);
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(queryWrapper);

        if (isFlag) {
            if(receiptHeader!=null){
                LambdaQueryWrapper<TaskDetail> queryWrapper3 = Wrappers.lambdaQuery();
                queryWrapper3.eq(TaskDetail::getBillCode,receiptHeader.getCode())
                        .le(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
                List<TaskDetail> taskDetailList = taskDetailService.list(queryWrapper3);
                if(taskDetailList==null || taskDetailList.size()>0){
                    throw  new ServiceException("更新失敗,此入库单存在任务");
                }
                LambdaQueryWrapper<ReceiptContainerDetail> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.eq(ReceiptContainerDetail::getReceiptCode,receiptHeader.getCode())
                        .le(ReceiptContainerDetail::getStatus,QuantityConstant.RECEIPT_CONTAINER_FINISHED);
                List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(queryWrapper1);
                if(receiptContainerDetails==null || receiptContainerDetails.size()>0){
                    throw  new ServiceException("更新失敗,此入库单已组盘");
                }
                receiptHeaderService.removeById(receiptHeader.getId());
                List<ReceiptDetail> receiptDetails = receiptDetailService.findByReceiptId(receiptHeader.getId());
                List<Integer> ids = receiptDetails.stream().map(ReceiptDetail::getId).collect(Collectors.toList());
                receiptDetailService.removeByIds(ids);
            }
            ICSReceiptHeader icsReceiptHeader = icsReceiptHeaderService.getOne(new LambdaQueryWrapper<ICSReceiptHeader>()
                    .eq(ICSReceiptHeader::getReferCode, code)
                    .eq(ICSReceiptHeader::getType, type));
            if(icsReceiptHeader!=null){
                icsReceiptHeaderService.removeById(icsReceiptHeader.getRid());
                icsReceiptDetailService.remove(new LambdaQueryWrapper<ICSReceiptDetail>()
                        .eq(ICSReceiptDetail::getHeaderId,icsReceiptHeader.getRid()));
            }
        }
    }

    private String getReceiptTypeName(String code){
        String typeName = "";
        switch (code){
            case "PCI" :
                typeName = "采购订单";
                break;
            case "CI" :
                typeName = "生产退料单";
                break;
            case "QI" :
                typeName = "其他入库单";
                break;
            case "PPI" :
                typeName = "手持入库单";
                break;
            case "TI" :
                typeName = "调拨入库单";
                break;
            case "SRI" :
                typeName = "销售退货单";
                break;
            case "SI" :
                typeName = "产成品入库单";
                break;
            case "PI" :
                typeName = "采购到货单";
                break;
            case "AI" :
                typeName = "调整单";
                break;
        }
        return typeName;
    }

    private boolean receiptTypeCheck(String code){
        switch (code){
            case "PI" :
            case "SRI" :
            case "PCI" :
            case "CI" :
                return false;
            default:
                return true;
        }
    }

    public Boolean isSameMonth(Date date){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(new Date());
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }

    //采购到货单和销售退货入库单
    @Transactional
    public synchronized AjaxResult insertICSReceipts(ICSReceiptModel icsReceiptModel) throws ParseException {

        ICSReceiptHeader icsReceiptHeader = icsReceiptModel.getIcsReceiptHeader();
        List<ICSReceiptDetail> icsReceiptDetails = icsReceiptModel.getIcsReceiptDetails();

        if (!QuantityConstant.U8_RECEIPT_TYPE_SRI.equals(icsReceiptHeader.getType()) && !QuantityConstant.U8_RECEIPT_TYPE_PI.equals(icsReceiptHeader.getType())) {
            return AjaxResult.error("入库类型不匹配");
        }
        String code=null;
        if(icsReceiptHeader.getType().equals("SRI")){
            code=icsReceiptHeader.getCDLCode();
        }else {
            code=icsReceiptHeader.getCCode();
        }
        if(StringUtils.isEmpty(code)){
            if(icsReceiptHeader.getType().equals("SRI")){
//                documentWarningService.ErpSendWarning("", "销售退货入库单没有单号");
            }else {
//                documentWarningService.ErpSendWarning("", "采购到货单下发没有单号");
            }
            throw new ServiceException("没有单号");
        }

        CompanyU8 companyWu = new CompanyU8();
        companyWu.setUCompanyCode(icsReceiptHeader.getCompanyCode());
        companyWu =companyU8Service.getByCode(companyWu);
        if(companyWu == null){
            return AjaxResult.error("单据货主不存在"+icsReceiptHeader.getCompanyCode());
        }
        ReceiptHeader receiptHeaderB=new ReceiptHeader();
        receiptHeaderB.setReferCode(code);
        receiptHeaderB.setCompanyCode(companyWu.getCompanyCode());
        receiptHeaderB = receiptHeaderService.selectFirstEntity(receiptHeaderB);
        if(receiptHeaderB !=null){
            return AjaxResult.success("单号重复,已跳过");
        }
        //判断仓库是否存在单据的物料编码
        ReceiptHeader receiptHeader;
        List<ReceiptDetail> receiptDetails;
        Set<String> warehouseCodes = new HashSet<>();
        Set<String> uwarehouseCodes = new HashSet<>();
        String companyCode =null;
        Map<String, String> companyCodeMap = new HashMap<>();
        for (ICSReceiptDetail item : icsReceiptDetails) {
            //通过u8的货主和仓库匹配wms的仓库
            WarehouseU8 warehouseWu=new WarehouseU8();
            warehouseWu.setUCompanyCode(icsReceiptHeader.getCompanyCode());
            warehouseWu.setUWarehouseCode(item.getCWHCode());
            List<WarehouseU8> list=warehouseU8Service.getByDomain(warehouseWu);
            if(list!=null&&list.size()>0){
                warehouseWu= list.get(0);
                companyCodeMap.put(item.getCWHCode() + icsReceiptHeader.getCompanyCode(), warehouseWu.getCompanyCode());
                companyCode =warehouseWu.getCompanyCode();
                uwarehouseCodes.add(item.getCWHCode());
                warehouseCodes.add(warehouseWu.getWarehouseCode());
            }
        }
        if(companyCode != null && !companyCode.equals(companyWu.getCompanyCode())){
            ReceiptHeader receiptHeader1=new ReceiptHeader();
            receiptHeader1.setReferCode(code);
            receiptHeader1.setCompanyCode(companyCode);
            receiptHeader1 = receiptHeaderService.selectFirstEntity(receiptHeader1);
            if(receiptHeader1 !=null){
                return AjaxResult.success("单号重复,已跳过");
            }
        }
        if(warehouseCodes == null || uwarehouseCodes.size()<1){
            return AjaxResult.success("没有此仓库");
        }

//        //将主表插入到新的接收主表里去
//        int result = icsReceiptHeaderService.insertModel(icsReceiptHeader);
//        if (result < 1) {
//            documentWarningService.ErpSendWarning(code, "入库主单传输失败!");
//            throw new ServiceException("入库主单传输失败!");
//        }

//        //将子表插入到新的接收子表里去
//        for (ICSReceiptDetail item : icsReceiptDetails) {
//            item.setcCode(code);
//            int result1 = icsReceiptDetailService.insertModel(item);
//            if (result1 < 1) {
//                documentWarningService.ErpSendWarning(code, "入库主单传输失败!");
//                throw new ServiceException("入库子单传输失败!");
//            }
//        }

        String maker=icsReceiptHeader.getCMaker();
        for (String warehouseCode : warehouseCodes) {
            for (String uwarehouseCode : uwarehouseCodes) {
                receiptDetails = new ArrayList<>();
                //出库主单
                receiptHeader = new ReceiptHeader();
                if(!StringUtils.isEmpty(icsReceiptHeader.getCTVCode())){
                    receiptHeader.setUserDef3(icsReceiptHeader.getCTVCode());
                }
                receiptHeader.setReceiptType(icsReceiptHeader.getType());
                receiptHeader.setId(null);
                receiptHeader.setLastUpdatedBy(maker);
                receiptHeader.setCreatedBy(maker);
                receiptHeader.setLastUpdated(new Date());
                receiptHeader.setCreated(new Date());
                receiptHeader.setReferCode(code);
                receiptHeader.setSupplierCode(icsReceiptHeader.getCVenCode());
                receiptHeader.setSupplierName(icsReceiptHeader.getCVenName());
                receiptHeader.setWarehouseCode(warehouseCode);
                receiptHeader.setUWarehouseCode(uwarehouseCode);
                receiptHeader.setCompanyCode(companyCodeMap.get(uwarehouseCode + icsReceiptHeader.getCompanyCode()));
//                receiptHeader.setSourcePlatform(icsReceiptHeader.getSourcePlatform());
                receiptHeader.setCDeptName(icsReceiptHeader.getCDepName());


                //细单
                for (ICSReceiptDetail item : icsReceiptDetails) {
                    //匹配仓库
                    if (uwarehouseCode.equals(item.getCWHCode())) {
                        //查找物料
                        Material material = new Material();
                        material.setCode(item.getCInvCode());
                        material.setWarehouseCode(warehouseCode);
                        material=materialService.getMaterialByDomain(material);
                        if(material==null){
//                            documentWarningService.ErpSendWarning(code, "该物料不存在:"+item.getCInvCode());
                            throw new ServiceException("该物料不存在:"+item.getCInvCode());
                        }
                        ReceiptDetail receiptDetail = new ReceiptDetail();
                        receiptDetail.setId(null);
                        receiptDetail.setLot(item.getCDefine23());
                        receiptDetail.setReferCode(code);
                        receiptDetail.setAutoId(item.getAutoID());
                        if(StringUtils.isNotEmpty(item.getCDefine22())) {
                            Boolean
                                    flag
                                    = StringUtils.isChineseChar(item.getCDefine22());
                            if (item.getCDefine22().length() == 19 && flag == false) {
                                receiptDetail.setMoCode(item.getCDefine22());
                            } else {
                                receiptDetail.setBatch(item.getCDefine22());
                            }
                        }
                        receiptDetail.setWarehouseCode(warehouseCode);
                        receiptDetail.setMaterialCode(item.getCInvCode());
                        receiptDetail.setZoneCode(material.getZoneCode());
                        receiptDetail.setCreated(new Date());
                        receiptDetail.setUWarehouseCode(uwarehouseCode);
                        receiptDetail.setMaterialUnit(item.getCComUnitName());
                        receiptDetail.setCreatedBy(maker);
                        receiptDetail.setLastUpdated(new Date());
                        receiptDetail.setLastUpdatedBy(maker);
                        if (!item.getCompanyCode().equals(icsReceiptHeader.getCompanyCode())) {
//                            documentWarningService.ErpSendWarning(code, "子单货主与主单不对应");
                            throw new ServiceException("子单货主与主单不对应");
                        }
                        if(icsReceiptHeader.getType().equals("SRI")){
                            receiptDetail.setCPoCode(item.getCSOCode());
                        }else {
                            receiptDetail.setCPoCode(item.getPOCode());
                        }
                        receiptDetail.setCompanyCode(companyCodeMap.get(item.getCWHCode() + icsReceiptHeader.getCompanyCode()));
                        receiptDetail.setProjectNo(item.getCItemName());
                        receiptDetail.setQty(item.getIQuantity().abs());
//                    receiptDetail.setQtyCompleted(item.getiQuantity());
                        receiptDetail.setInventorySts("good");
                        if(StringUtils.isNotEmpty(item.getLotno())){
                            receiptDetail.setUserDef2(item.getLotno());
                        }
                        receiptDetails.add(receiptDetail);
                    }
                }
                //调用公共API
                ReceiptDomain receiptModel = new ReceiptDomain();
                receiptModel.setReceiptHeader(receiptHeader);
                receiptModel.setReceiptDetails(receiptDetails);
                receiptApiService.receipt(receiptModel);
            }
        }
        return AjaxResult.success("入货单下发成功");
    }



}