MesServiceImpl.java 37.9 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
package com.huaheng.api.mes.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.mes.dto.Detail;
import com.huaheng.api.mes.dto.Header;
import com.huaheng.api.mes.dto.ReceiptDTO;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.RetCode;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.flatWarehouseMaterial.domain.FlatWarehouseMaterial;
import com.huaheng.pc.config.flatWarehouseMaterial.service.FlatWarehouseMaterialService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptSave;
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.receipt.receiptHeaderHistory.service.ReceiptHeaderHistoryService;
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.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
import com.huaheng.pc.system.config.service.IConfigService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.huaheng.common.constant.QuantityConstant.DYNAMIC_ROADWAY;

@Service
public class MesServiceImpl implements IMesService {
    @Resource
    private ConfigService configService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;
    @Resource
    private ReceiptHeaderHistoryService receiptHeaderHistoryService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
    @Resource
    private MaterialService materialService;
    @Resource
    private FlatWarehouseMaterialService flatWarehouseMaterialService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult receipt(ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();
        List<ReceiptHeader> list = receiptHeaderService.list(new LambdaQueryWrapper<ReceiptHeader>()
                .eq(ReceiptHeader::getCode, header.getReferCode())
                .eq(ReceiptHeader::getReceiptType, header.getRefeCodeType()));
        if (!list.isEmpty()) {
            return AjaxResult.error("添加入库单: 上游单号referCode重复[ " + header.getReferCode() + " ],请勿重复添加。");
        }

        //校验
        AjaxResult ajaxResult = new AjaxResult();
        boolean isNull = enterIsNull(ajaxResult, header, details);
        if (isNull) {
            return ajaxResult;
        }
        List<Material> materialList = new ArrayList<>();
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setMOMID(header.getMOMID());
        receiptHeader.setReceiptNote(header.getReceiptNote());
        receiptHeader.setCode(header.getReferCode());
        receiptHeader.setReceiptType(header.getRefeCodeType());
        receiptHeader.setTotalQty(header.getTotalQty());
        receiptHeader.setTotalLines(details.size());
        receiptHeader.setTAID(header.getTAID());// MOM目标类型
        receiptHeader.setFAID(header.getFAID());  // MOM来源类型
        receiptHeader.setProductionWorker(header.getProductionWorker());
        receiptHeader.setProductionWorkerName(header.getProductionWorkerName());
        receiptHeader.setWarehouse(header.getWarehouse());
        receiptHeader.setWarehouseName(header.getWarehouseName());
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
        receiptHeader.setWarehouseCode("CS0001");
        receiptHeader.setCompanyCode("BHF");
        receiptHeader.setCreatedBy("MOM");
        receiptHeader.setPaintStatus(header.getPaintStatus());

        //一厂一期不需要颜色
        String version = configService.getKey(QuantityConstant.PROJECT_VERSION);
        if (!version.equals("1")) {
            receiptHeader.setMaterialColor(header.getMaterialColor());
        } else {
            receiptHeader.setMaterialColor("");
        }
        if (header.getIsFastReceipt() != null && header.getIsFastReceipt() == 1) {
            receiptHeader.setIsFastReceipt(1);
            if (StringUtils.isEmpty(header.getFastReceiptWarehouse())) {
                return AjaxResult.error("快速入库仓库不能为空");
            }
            receiptHeader.setFastReceiptWarehouse(header.getFastReceiptWarehouse());
        }


        boolean success = receiptHeaderService.save(receiptHeader);
        if (!success) {
            throw new ServiceException("生成入库单头失败");
        }

        List<ReceiptDetail> shipemtnDetailList = new ArrayList<>();
        for (Detail detail : details) {
            ReceiptDetail receiptDetail = new ReceiptDetail();
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode, detail.getMaterialCode()));
            if (StringUtils.isNotEmpty(detail.getDetailID())) {
                receiptDetail.setMOMID(detail.getDetailID());
            }
            receiptDetail.setIsVirtualBom(detail.getIsVirtual());
            receiptDetail.setMaterialCode(detail.getMaterialCode());
            receiptDetail.setMaterialName(detail.getMaterialName());
            if (material == null) {
                Material newMaterial = new Material();
                newMaterial.setCode(detail.getMaterialCode());
                newMaterial.setName(detail.getMaterialName());
                newMaterial.setWarehouseCode("CS0001");
                newMaterial.setCompanyCode("BHF");
                materialList.add(newMaterial);
            } else {
                if (!material.getCode().equals(receiptDetail.getMaterialCode())) {
                    throw new ServiceException("物料编码对不上,入库单:" + receiptDetail.getMaterialCode() + "物料表:" + material.getCode());
                }
                receiptDetail.setMaterialSpec(material.getSpec());
                receiptDetail.setMaterialUnit(material.getUnit());
                receiptDetail.setFrequency(material.getFrequency());
            }
            receiptDetail.setIsFlatWarehouse(detail.getIsFlatWarehouse());
            receiptDetail.setLMPKID(detail.getLMPKID());
            receiptDetail.setLSPKID(detail.getLSPKID());
            receiptDetail.setIfrom(detail.getIfrom());
            receiptDetail.setQty(detail.getQty());
            receiptDetail.setIsUrgent(detail.getIsUrgent());
            receiptDetail.setNoticeNo(detail.getNoticeNo());
            receiptDetail.setSNNO(detail.getSNNO());
            receiptDetail.setReceiptBarcode(detail.getReceiptBarcode());
            receiptDetail.setProductionDate(detail.getProductionDate());
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setStatusFlowCode("2");
            receiptDetail.setWarehouseCode("CS0001");
            receiptDetail.setCompanyCode("BHF");
            receiptDetail.setInventorySts("good");
            receiptDetail.setCreatedBy("MOM");

            receiptDetail.setWarehouseName(receiptHeader.getWarehouseName());
            receiptDetail.setWarehouse(receiptHeader.getWarehouse());
            receiptDetail.setReceiptType(receiptHeader.getReceiptType());
            receiptDetail.setAdjustCode(receiptHeader.getUserDef3());
            receiptDetail.setReceiptNote(receiptHeader.getReceiptNote());
            receiptDetail.setProductionWorker(receiptHeader.getProductionWorker());
            receiptDetail.setProductionWorkerName(receiptHeader.getProductionWorkerName());
            receiptDetail.setPaintStatus(receiptHeader.getPaintStatus());
            receiptDetail.setMaterialColor(receiptHeader.getMaterialColor());

            //判断是否为必须入库平库物料,通知单号带“展会”两个字的物料也必须入平库
            if (material == null || !judgeIsFlatWarehouse(material, receiptDetail.getNoticeNo())) {
                receiptDetail.setIsMustFlatWarehouseMaterial(false);
            } else {
                receiptDetail.setIsMustFlatWarehouseMaterial(true);
            }
            shipemtnDetailList.add(receiptDetail);
        }
        success = receiptDetailService.saveBatch(shipemtnDetailList);
        if (!success) {
            throw new ServiceException("生成入库单详情失败");
        }
        if (!materialList.isEmpty()) {
            materialList = materialList.stream().distinct().collect(Collectors.toList());
            success = materialService.saveBatch(materialList);
            if (!success) {
                throw new ServiceException("保存物料信息失败");
            }
        }

        //快速入库,组盘,完成任务,无需回传MOM
        if (receiptHeader.getIsFastReceipt() != null && receiptHeader.getIsFastReceipt() == 1) {
            if (!quickStorage(receiptHeader, receiptHeader.getFastReceiptWarehouse())) {
                throw new ServiceException("快速入库失败");
            }
            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
            receiptHeader.setPushSuccessStatus(1);
            receiptHeaderService.updateById(receiptHeader);
        }
        return AjaxResult.success(receiptDTO);
    }

    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private TaskHeaderService taskHeaderService;

    @Transactional
    public boolean quickStorage(ReceiptHeader receiptHeader, String fastWarehouse) {
        Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getName, fastWarehouse));
        if (zone == null) {
            throw new ServiceException("快速入库平库不存在:" + fastWarehouse);
        }
        List<ReceiptSave> receiptSaves = new ArrayList<>();
        List<ReceiptDetail> receiptDetails = receiptDetailService.findByReceiptId(receiptHeader.getId());

        for (ReceiptDetail receiptDetail : receiptDetails) {
            ReceiptSave receiptSave = new ReceiptSave();
            receiptSave.setQty(receiptDetail.getQty());
            receiptSave.setReceiptCode(receiptDetail.getReceiptCode());
            receiptSave.setReceiptDetailId(receiptDetail.getId());
            receiptSave.setLocationCode(zone.getCode());
            receiptSaves.add(receiptSave);
        }
        AjaxResult ajaxResult = receiptContainerHeaderService.pdaFlatSaveContainer(receiptSaves);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("快速入库失败:" + ajaxResult.getMsg());
        }
        //无需回传MOM
        List<TaskDetail> list = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getBillCode, receiptHeader.getCode()));
        Set<Integer> uniqueTaskIds = new HashSet<>();
        for (TaskDetail taskDetail : list) {
            uniqueTaskIds.add(taskDetail.getTaskId());
        }
        for (Integer id : uniqueTaskIds) {
            TaskHeader taskHeader = taskHeaderService.getById(id);
            taskHeader.setBackMoM(1);
            taskHeaderService.updateById(taskHeader);
        }
        return true;
    }

    //判断是否为必须入库平库物料,通知单号带“展会”两个字的物料也必须入平库
    private boolean judgeIsFlatWarehouse(Material material, String noticeNo) {
        if (StringUtils.isNotBlank(noticeNo) && noticeNo.contains("展会")) {
            return true;
        }

        String cinvccode = material.getCinvccode();
        String defWareHouse = material.getDefWareHouse();
        if (cinvccode == null || defWareHouse == null) {
            return false;
        }
        List<FlatWarehouseMaterial> list = flatWarehouseMaterialService.list(new LambdaQueryWrapper<FlatWarehouseMaterial>()
                .eq(FlatWarehouseMaterial::getWarehouseCode, defWareHouse)
                .eq(FlatWarehouseMaterial::getClassificationCode, cinvccode));
        if (list.isEmpty()) {
            return false;
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipment(ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        List<Detail> details = receiptDTO.getShipemtnDetails();
        List<ShipmentHeader> list = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getCode, header.getReferCode())
                .eq(ShipmentHeader::getShipmentType, header.getRefeCodeType()));
        if (!list.isEmpty()) {
            return AjaxResult.error("添加出库单: 上游单号重复[ " + header.getReferCode() + " ],请勿重复添加。");
        }
        AjaxResult ajaxResult = new AjaxResult();
        //校验参数
        boolean inNull = enterIsNull(ajaxResult, header, details);
        if (inNull) {
            return ajaxResult;
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        if (StringUtils.isNotEmpty(header.getMOMID())) {
            shipmentHeader.setMOMID(header.getMOMID());
        }
        if (StringUtils.isNotEmpty(header.getISMOM())) {
            shipmentHeader.setISMOM(header.getISMOM());
        }
        shipmentHeader.setCode(header.getReferCode());
        shipmentHeader.setShipmentType(header.getRefeCodeType());
        shipmentHeader.setTotalQty(header.getTotalQty());
        // TAID MOM目标类型 // FAID MOM来源类型
        shipmentHeader.setTAID(header.getTAID());
        shipmentHeader.setFAID(header.getFAID());
        shipmentHeader.setNoticeNo(details.get(0).getNoticeNo());
        shipmentHeader.setWorkshop(header.getWorkshop());
        shipmentHeader.setWarehouse(header.getWarehouse());
        shipmentHeader.setWarehouseName(header.getWarehouseName());
        shipmentHeader.setCreatorCode(header.getCreatorCode());
        shipmentHeader.setCreatorName(header.getCreatorName());
        shipmentHeader.setRequestedStartDate(header.getRequestedStartDate());
        shipmentHeader.setPaintStatus(header.getPaintStatus());
        //一厂一期不需要颜色
        String version = configService.getKey(QuantityConstant.PROJECT_VERSION);
        if (!version.equals("1")) {
            shipmentHeader.setMaterialColor(header.getMaterialColor());
        } else {
            shipmentHeader.setMaterialColor("");
        }

        //母件编码和母件名称去重复
        String[] superiorCodeValue = header.getSuperiorCode() != null ? header.getSuperiorCode().split(",") : new String[0];
        String[] superiorNameValue = header.getSuperiorName() != null ? header.getSuperiorName().split(",") : new String[0];
        Set<String> uniqueSuperiorCodeValue = new HashSet<>();
        Set<String> uniqueSuperiorNameValue = new HashSet<>();
        for (String value : superiorCodeValue) {
            uniqueSuperiorCodeValue.add(value.trim());
        }
        for (String value : superiorNameValue) {
            uniqueSuperiorNameValue.add(value.trim());
        }
        String superiorCode = String.join(",", uniqueSuperiorCodeValue);
        String superiorName = String.join(",", uniqueSuperiorNameValue);
        shipmentHeader.setSuperiorCode(superiorCode);
        shipmentHeader.setSuperiorName(superiorName);


        if (StringUtils.isNotEmpty(header.getTransferWarehouseName())) {
            shipmentHeader.setTransferWarehouseName(header.getTransferWarehouseName());
        }
        if (StringUtils.isNotEmpty(header.getTransferWarehouseCode())) {
            shipmentHeader.setTransferWarehouseCode(header.getTransferWarehouseCode());
        }
        shipmentHeader.setShipmentNote(header.getShipmentNote());
        shipmentHeader.setRequestedDeliveryDate(header.getRequestedDeliveryDate());
        if (StringUtils.isNotEmpty(header.getTransferWarehouseCode())) {
            shipmentHeader.setTransferWarehouseCode(header.getTransferWarehouseCode());
        }
        shipmentHeader.setDailyScheduleImportDate(header.getDailyScheduleImportDate());
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentHeader.setTotalLines(details.size());
        shipmentHeader.setWarehouseCode("CS0001");
        shipmentHeader.setCompanyCode("BHF");
        shipmentHeader.setCreatedBy("MOM");
        boolean success = shipmentHeaderService.save(shipmentHeader);
        if (!success) {
            throw new ServiceException("生成出库单头失败");
        }
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        details.forEach(detail -> {
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            if (StringUtils.isNotEmpty(detail.getDetailID())) {
                shipmentDetail.setMOMID(detail.getDetailID());
            }
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode, detail.getMaterialCode()));
            if (material == null) {
                Material newMaterial = new Material();
                newMaterial.setCode(detail.getMaterialCode());
                newMaterial.setName(detail.getMaterialName());
                newMaterial.setWarehouseCode("CS0001");
                newMaterial.setCompanyCode("BHF");
                materialService.save(newMaterial);
            } else {
                shipmentDetail.setMaterialSpec(material.getSpec());
                shipmentDetail.setMaterialUnit(material.getUnit());
                shipmentDetail.setPolicy(material.getPolicy());
            }
            shipmentDetail.setReceiptCode(detail.getReceiptCode());
            shipmentDetail.setContainerCode(detail.getContainerCode());
            shipmentDetail.setMaterialCode(detail.getMaterialCode());
            shipmentDetail.setMaterialName(detail.getMaterialName());
            shipmentDetail.setPort(detail.getEndSation());
            shipmentDetail.setQty(detail.getQty());
            shipmentDetail.setIsUrgent(detail.getIsUrgent());
            shipmentDetail.setNoticeNo(detail.getNoticeNo());
            shipmentDetail.setSNNO(detail.getSNNO());

            if (detail.getIfrom() != null) {
                shipmentDetail.setIfrom(detail.getIfrom());
            }
            if (detail.getLSPKID() != null) {
                shipmentDetail.setLSPKID(detail.getLSPKID());
            }
            if (detail.getLMPKID() != null) {
                shipmentDetail.setLMPKID(detail.getLMPKID());
            }
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setCompanyCode("BHF");
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setInventorySts("good");
            shipmentDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            shipmentDetail.setCreatedBy("MOM");
            shipmentDetail.setLastUpdatedBy("MOM");
            shipmentDetailList.add(shipmentDetail);
        });
        success = shipmentDetailService.saveBatch(shipmentDetailList);
        if (!success) {
            throw new ServiceException("生成出库单详情失败");
        }

        //如果通知单号和车间、仓库相同,自动合并单据
        //String warehouse = shipmentHeader.getWarehouse();
        //String workshop = shipmentHeader.getWorkshop();
        //String noticeNo = shipmentHeader.getNoticeNo();
        //if (StringUtils.isNotEmpty(noticeNo) && StringUtils.isNotEmpty(warehouse) && StringUtils.isNotEmpty(workshop)) {
        //    List<ShipmentHeader> automaticMergeCount = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>()
        //            .ne(ShipmentHeader::getCode, shipmentHeader.getCode())
        //            .eq(ShipmentHeader::getFirstStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD)
        //            .eq(ShipmentHeader::getLastStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD)
        //            .eq(ShipmentHeader::getDeleted, 0)
        //            .eq(ShipmentHeader::getWarehouse, warehouse)
        //            .eq(ShipmentHeader::getWorkshop, workshop)
        //            .eq(ShipmentHeader::getNoticeNo, noticeNo));
        //    if (!automaticMergeCount.isEmpty()) {
        //        String ids = automaticMergeCount.get(0).getId() + "," + shipmentHeader.getId();
        //        AjaxResult analysis = shipmentHeaderService.analysis(ids);
        //        if (analysis.hasErr()) {
        //            throw new ServiceException("合并单据失败," + analysis.getMsg());
        //        }
        //    }
        //}
        return AjaxResult.success(receiptDTO);
    }


    /**
     * 取消入库
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelEnterWarehouse(ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        //List<Detail> details = receiptDTO.getShipemtnDetails();
        if (StringUtils.isEmpty(header.getReferCode())) {
            return AjaxResult.error().setMsg("单据单号不能为空!!!");
        }
        // 查询入库单据表头
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>().eq(ReceiptHeader::getCode, header.getReferCode()));
        if (receiptHeader == null) {
            return AjaxResult.error().setMsg("没有找到对应单据," + header.getReferCode());
        }
        //if (receiptHeader.getFirstStatus() >= QuantityConstant.RECEIPT_HEADER_POOL) {
        //    return AjaxResult.error("订单状态大于100不能删除");
        //}
        Integer firstStatus = receiptHeader.getFirstStatus();
        Integer lastStatus = receiptHeader.getLastStatus();
        boolean firstFlag = QuantityConstant.SHIPMENT_CONTAINER_BUILD.equals(firstStatus);
        boolean lastFlag = QuantityConstant.SHIPMENT_CONTAINER_BUILD.equals(lastStatus);
        if (!(firstFlag && lastFlag)) {
            return AjaxResult.error("入库单状态不是新建状态,不允许修改");
        }
        List<ReceiptDetail> list = receiptDetailService.list(new LambdaQueryWrapper<ReceiptDetail>().eq(ReceiptDetail::getReceiptId, receiptHeader.getId()));
        BigDecimal finishQty = BigDecimal.ZERO;
        for (ReceiptDetail receiptDetail : list) {
            finishQty = finishQty.add(receiptDetail.getTaskQty());
        }
        if (finishQty.compareTo(BigDecimal.ZERO) > 0) {
            return AjaxResult.error("入库单删除错误:已收数量不为零不能删除!");
        }
        String name = "未知";
        if (StringUtils.isNotEmpty(header.getDelete())) {
            name = header.getDelete();
        }
        //新增入库单历史
        receiptHeaderHistoryService.saveById(receiptHeader.getId().toString(), name);

        if (!receiptHeaderService.removeById(receiptHeader)) {
            throw new ServiceException("删除头表失败");
        }
        List<Integer> receiptDetailIds = new ArrayList<>();
        for (ReceiptDetail receiptDetail : list) {
            receiptDetailIds.add(receiptDetail.getId());
        }
        if (receiptDetailIds.size() > 0) {
            if (!receiptDetailService.removeByIds(receiptDetailIds)) {
                throw new ServiceException("删除明细表失败");
            }
        }
        return AjaxResult.success("取消入库单成功");
    }

    /**
     * 取消出库
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelOutWarehouse(ReceiptDTO receiptDTO) {
        Header header = receiptDTO.getShipmentHeader();
        if (StringUtils.isEmpty(header.getReferCode())) {
            return AjaxResult.error().setMsg("单据单号不能为空!!!");
        }
        // 查询入库单据表头
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getCode, header.getReferCode()));
        if (shipmentHeader == null) {
            return AjaxResult.error().setMsg("没有找到对应单据," + header.getReferCode());
        }
        Integer firstStatus = shipmentHeader.getFirstStatus();
        Integer lastStatus = shipmentHeader.getLastStatus();
        String shipmentType = shipmentHeader.getShipmentType();
        if ("101".equals(shipmentType)) {
            return AjaxResult.error("采购退货不允许取消");
        }
        boolean firstFlag = QuantityConstant.SHIPMENT_CONTAINER_BUILD.equals(firstStatus);
        boolean lastFlag = QuantityConstant.SHIPMENT_CONTAINER_BUILD.equals(lastStatus);
        if (!(firstFlag && lastFlag)) {
            return AjaxResult.error("出库单状态不是新建状态,不允许修改");
        }
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(new LambdaQueryWrapper<ShipmentDetail>().eq(ShipmentDetail::getShipmentId, shipmentHeader.getId()));
        BigDecimal finishQty = BigDecimal.ZERO;
        for (ShipmentDetail shipmentDetail : shipmentDetailList) {
            finishQty = finishQty.add(shipmentDetail.getTaskQty());
        }
        if (finishQty.compareTo(BigDecimal.ZERO) > 0) {
            return AjaxResult.error("出库单删除错误:单据出库数量不为零不能删除!");
        }

        String name = "未知";
        if (StringUtils.isNotEmpty(header.getDelete())) {
            name = header.getDelete();
        }
        //新增出库单历史
        shipmentHeaderHistoryService.saveById(shipmentHeader.getId().toString(), name);

        List<Integer> shipmentDetailIds = new ArrayList<>();
        if (!shipmentHeaderService.removeById(shipmentHeader)) {
            throw new ServiceException("删除头表失败");
        }
        for (ShipmentDetail shipmentDetail : shipmentDetailList) {
            shipmentDetailIds.add(shipmentDetail.getId());
        }
        if (shipmentDetailIds.size() > 0) {
            if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                throw new ServiceException("删除明细表失败");
            }
        }
        return AjaxResult.success("取消出库单成功");
    }


    ///**
    // * 取消入库
    // */
    //@Override
    //@Transactional(rollbackFor = Exception.class)
    //public AjaxResult cancelEnterWarehouse(ReceiptDTO receiptDTO) {
    //    Header header = receiptDTO.getShipmentHeader();
    //    List<Detail> details = receiptDTO.getShipemtnDetails();
    //
    //    if (StringUtils.isEmpty(header.getReferCode())){
    //        return AjaxResult.error().setMsg("单据单号不能为空!!!");
    //    }
    //    for (Detail detailIsNull:details){
    //        if(StringUtils.isEmpty(detailIsNull.getDetailID())){
    //            return AjaxResult.error().setMsg("单据明细ID不能为空!!!");
    //        }
    //    }
    //
    //    // 查询入库单据表头
    //    LambdaQueryWrapper<ReceiptHeader> receiptHeaderWrapper = Wrappers.lambdaQuery();
    //    receiptHeaderWrapper.eq(ReceiptHeader::getId, header.getId());
    //    ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptHeaderWrapper);
    //    if (receiptHeader == null) {
    //        return AjaxResult.error().setMsg("没有找到对应单据!!!");
    //    }
    //    // 查询入库单据明细
    //    LambdaQueryWrapper<ReceiptDetail> receiptDetailWrapper = Wrappers.lambdaQuery();
    //    receiptDetailWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
    //
    //    List<ReceiptDetail> receiptDetailList = receiptDetailService.list(receiptDetailWrapper);
    //    if (receiptDetailList == null) {
    //        return AjaxResult.error().setMsg("没有找到对应单据明细!!!");
    //    }
    //    // 获取删除明细
    //    List<ReceiptDetail> removeReceiptDetails = new ArrayList<>();
    //    receiptDetailList.forEach(receiptDetail-> details.forEach(detail->{
    //        if (detail.getDetailID().equals(receiptDetail.getMOMID())){
    //            removeReceiptDetails.add(receiptDetail);
    //        }
    //    }));
    //    ArrayList<Integer> ids = new ArrayList<>();
    //    if (removeReceiptDetails.size() > 0){
    //        removeReceiptDetails.forEach(receiptDetail->{
    //            ids.add(receiptDetail.getId());
    //        });
    //    }
    //    if (ids.size() < 1 || removeReceiptDetails.size() < 1){
    //        return AjaxResult.error().setMsg("没有匹配到对应要取消的入库单单据明细!!!");
    //    }
    //    boolean success = receiptDetailService.removeByIds(ids);
    //    if(!success) {
    //        throw new ServiceException("删除入库单详情失败");
    //    }
    //    // 出库单明细数量和要删除的明细数量一致,则删除主表
    //    boolean isRemove = removeReceiptDetails.size() == receiptDetailList.size();
    //    if (isRemove){
    //        success = receiptHeaderService.removeById(receiptHeader.getId());
    //        if(!success) {
    //            throw new ServiceException("删除入库单失败");
    //        }
    //    }else{
    //        receiptHeader.setCreatorName(header.getCreatorName());
    //        receiptHeader.setCreatorCode(header.getCreatorCode());
    //        receiptHeader.setLastUpdatedBy(header.getCreatorName());
    //        receiptHeader.setWarehouse(header.getWarehouse());
    //        receiptHeader.setWarehouseName(header.getWarehouseName());
    //        receiptHeaderService.updateById(receiptHeader);
    //    }
    //    return AjaxResult.success("取消入库单成功");
    //}
    //
    ///**
    // * 取消入库
    // */
    //@Override
    //@Transactional(rollbackFor = Exception.class)
    //public AjaxResult cancelOutWarehouse(ReceiptDTO receiptDTO) {
    //    Header header = receiptDTO.getShipmentHeader();
    //    List<Detail> details = receiptDTO.getShipemtnDetails();
    //
    //    AjaxResult ajaxResult = isNull(header, details);
    //    if (StringUtils.isNotEmpty(ajaxResult.getMsg())) {
    //        return ajaxResult;
    //    }
    //
    //    // 查询出库单据表头
    //    LambdaQueryWrapper<ShipmentHeader> shipmentHeaderWrapper = Wrappers.lambdaQuery();
    //    shipmentHeaderWrapper.eq(ShipmentHeader::getCode, header.getSuperiorCode());
    //    ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderWrapper);
    //    if (shipmentHeader == null) {
    //        return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据!!!");
    //    }
    //    // 查询出库单据明细
    //    LambdaQueryWrapper<ShipmentDetail> shipmentDetailWrapper = Wrappers.lambdaQuery();
    //    shipmentDetailWrapper.eq(ShipmentDetail::getShipmentId, header.getId());
    //    List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(shipmentDetailWrapper);
    //    if (shipmentDetailList == null) {
    //        return ajaxResult.setCode(RetCode.FAIL).setMsg("没有找到对应单据明细!!!");
    //    }
    //    // 获取删除明细
    //    List<ShipmentDetail> removeShipmentDetails = new ArrayList<>();
    //    shipmentDetailList.forEach(shipmentDetai-> details.forEach(detail->{
    //        if (detail.getMaterialCode().equals(shipmentDetai.getMaterialCode()) && detail.getTotalQty().intValue() == (shipmentDetai.getTaskQty().intValue())){
    //            removeShipmentDetails.add(shipmentDetai);
    //        }
    //    }));
    //    if (removeShipmentDetails.size() > 0) {
    //        ArrayList<Integer> ids = new ArrayList<>();
    //        removeShipmentDetails.forEach(shipmentDetail -> ids.add(shipmentDetail.getId()));
    //        shipmentDetailService.removeByIds(ids);
    //    } else {
    //        return ajaxResult.setCode(RetCode.FAIL).setMsg("没有匹配到对应要取消的出库单单据明细!!!");
    //    }
    //    // 出库单明细数量和要删除的明细数量一致,则删除主表
    //    boolean isRemove = removeShipmentDetails.size() == shipmentDetailList.size();
    //    if (isRemove) {
    //        shipmentHeaderService.removeById(shipmentHeader);
    //    } else {
    //        shipmentHeader.setCreatorName(header.getCreatorName());
    //        shipmentHeader.setCreatorCode(header.getCreatorCode());
    //        shipmentHeader.setLastUpdatedBy(header.getCreatorName());
    //        shipmentHeader.setWarehouse(header.getWarehouse());
    //        shipmentHeader.setWarehouseName(header.getWarehouseName());
    //        shipmentHeaderService.updateById(shipmentHeader);
    //    }
    //    return AjaxResult.success("取消出库单成功");
    //}


    /**
     * 出库单参数_判空处理
     *
     * @param ajaxResult
     * @param shipmentHeader
     * @param shipemtnDetails
     */
    private boolean outIsNull(AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails) {
        if (isNullData(ajaxResult, shipmentHeader, shipemtnDetails)) {
            return true;
        }

        if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据单号不能为空!!!");
            return true;
        }

        shipemtnDetails.forEach(shipemtnDetail -> {
            if (StringUtils.isEmpty(shipemtnDetail.getMaterialCode())) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("物料编码不能为空!!!");
            }
        });
        return StringUtils.isNotEmpty(ajaxResult.getMsg());
    }

    //private AjaxResult isNull(Header header, List<Detail> details) {
    //    AjaxResult ajaxResult = new AjaxResult();
    //    boolean isNull = outIsNull(ajaxResult, header, details);
    //    details.forEach(detail -> {
    //        if (detail.getTotalQty() == null || detail.getTotalQty().intValue() <= 0) {
    //            ajaxResult.setCode(RetCode.FAIL).setMsg("总数量不能为空!!!");
    //        }
    //    });
    //    if (isNull || StringUtils.isNotEmpty(ajaxResult.getMsg())) {
    //        return ajaxResult;
    //    }
    //    return ajaxResult;
    //}

    /**
     * 入库单参数_判空处理
     *
     * @param ajaxResult
     * @param shipmentHeader
     * @param shipemtnDetails
     */
    private boolean enterIsNull(AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails) {

        if (isNullData(ajaxResult, shipmentHeader, shipemtnDetails)) {
            return true;
        }

        if (StringUtils.isEmpty(shipmentHeader.getRefeCodeType())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据类型不能为空!!!");
            return true;
        } else if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("单据单号不能为空!!!");
            return true;
        } else if (shipmentHeader.getTotalQty() == null || shipmentHeader.getTotalQty().compareTo(BigDecimal.ZERO) <= 0) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("总数量不能为空,或小于0");
        } else if (StringUtils.isEmpty(shipmentHeader.getWarehouse())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("仓库不能为空!!!");
        } else if (StringUtils.isEmpty(shipmentHeader.getWarehouseName())) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("仓库名称不能为空!!!");
        }

        shipemtnDetails.forEach(shipemtnDetail -> {
            System.out.println(shipemtnDetail.getQty().intValue());
            if (StringUtils.isEmpty(shipemtnDetail.getMaterialCode())) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("物料编码不能为空!!!");
            } else if (shipemtnDetail.getQty() == null || shipemtnDetail.getQty().compareTo(BigDecimal.ZERO) <= 0) {
                ajaxResult.setCode(RetCode.FAIL).setMsg("数量不能为空,或小于0");
            }
        });
        return StringUtils.isNotEmpty(ajaxResult.getMsg());
    }

    /**
     * 判空处理
     */
    private boolean isNullData(AjaxResult ajaxResult, Header shipmentHeader, List<Detail> shipemtnDetails) {
        if (shipmentHeader == null) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("出库单头表不能为空!!!");
            return true;
        } else if (shipemtnDetails == null || shipemtnDetails.size() < 1) {
            ajaxResult.setCode(RetCode.FAIL).setMsg("出库单明细表集不能为空!!!");
            return true;
        }
        return false;
    }
}