ErpServiceImpl.java 37.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
package com.huaheng.api.erp.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huaheng.api.erp.domain.*;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.ResultErp;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.supplier.service.SupplierService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
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.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.system.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
 * @author 游杰
 */
@Service
public class ErpServiceImpl implements IErpService {

    @Resource
    private IErpService erpService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptHeaderHistoryService receiptHeaderHistoryService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private AddressService addressService;
    @Resource
    private CustomerServiceImpl customerService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private IUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp receipt(ErpReceipt erpReceipt) {
        boolean success = false;
        if (erpReceipt == null) {
            return ResultErp.error("入库单下发,没有传receipt");
        }
        ErpReceiptHeader erpReceiptHeader = erpReceipt.getReceiptHeader();
        List<ErpReceiptDetail> erpReceiptDetailList = erpReceipt.getReceiptDetailList();
        if (erpReceiptHeader == null) {
            return ResultErp.error("入库单下发,没有传receiptHeader");
        }
        if (erpReceiptDetailList == null) {
            return ResultErp.error("入库单下发,没有传receiptDetailList");
        }
        String warehouseCode = erpReceiptHeader.getWarehouseCode();
        String receiptType = erpReceiptHeader.getReceiptType();
        String referCode = erpReceiptHeader.getReferCode();
        String companyCode = erpReceiptHeader.getCompanyCode();
        String remark = erpReceiptHeader.getRemark();
        String supplierCode = erpReceiptHeader.getSupplierCode();
        if (StringUtils.isEmpty(warehouseCode)) {
            return ResultErp.error("入库单下发,仓库编码为空");
        }
        String warehouse = warehouseService.getWarehouseNameByCode(warehouseCode);
        if (warehouse == null) {
            return ResultErp.error("入库单下发, 没有找到仓库");
        }
        if (ShiroUtils.getUser() == null){
            // 模拟mes登录
            userService.login(QuantityConstant.PLATFORM_MES, QuantityConstant.PLATFORM_MES_PASSWORD, erpReceiptHeader.getWarehouseCode(), false);
        }
        if (StringUtils.isEmpty(receiptType)) {
            return ResultErp.error("入库单下发, receiptType为空");
        }
        if (StringUtils.isEmpty(referCode)) {
            return ResultErp.error("入库单下发, referCode为空");
        }
        if (StringUtils.isEmpty(companyCode)) {
            Company company = companyService.getDefaultCompany();
            if (company == null) {
                return ResultErp.error("入库单下发, company为空");
            }
            companyCode = company.getCode();
        } else {
            Company company = companyService.getCompanyByCode(companyCode, warehouseCode);
            if (company == null) {
                return ResultErp.error("入库单下发, 没有找到货主");
            }
        }
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setWarehouseCode(warehouseCode);
        receiptHeader.setCompanyCode(companyCode);
        receiptHeader.setReceiptType(receiptType);
        receiptHeader.setReferCode(referCode);
        receiptHeader.setSupplierCode(supplierCode);
        receiptHeader.setReceiptNote(remark);
        AjaxResult result = receiptHeaderService.saveReceiptHeader(receiptHeader);
        if (result.hasErr()) {
            throw new ServiceException("创建入库单头失败" + result.getMsg());
        }
        List<ReceiptDetail> receiptDetailList=new ArrayList<>();
        for (ErpReceiptDetail erpReceiptDetail : erpReceiptDetailList) {
            BigDecimal qty = erpReceiptDetail.getQty();
            String materialCode = erpReceiptDetail.getMaterialCode();
            String materialName = erpReceiptDetail.getMaterialName();
            String materialSpec = erpReceiptDetail.getMaterialSpec();
            String materialUnit = erpReceiptDetail.getMaterialUnit();
            String inventoryStatus = erpReceiptDetail.getInventoryStatus();
            String batch = erpReceiptDetail.getBatch();
            String lot = erpReceiptDetail.getLot();
            String project = erpReceiptDetail.getProject();
            if (StringUtils.isEmpty(materialCode)) {
                throw new ServiceException("入库单下发, 物料编码为空");
            }
            if (StringUtils.isEmpty(inventoryStatus)) {
                inventoryStatus = QuantityConstant.GOOD;
            }
            if (qty.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("入库单下发, 单据数量必须大于0");
            }
            ReceiptDetail receiptDetail = new ReceiptDetail();
            receiptDetail.setWarehouseCode(warehouseCode);
            receiptDetail.setCompanyCode(companyCode);
            receiptDetail.setReferCode(referCode);
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
            receiptDetail.setQty(qty);
            receiptDetail.setMoCode(erpReceiptDetail.getMocode());
            receiptDetail.setReferLineNum(erpReceiptDetail.getReferLineNum());
            Material material = materialService.getMaterialByCode(materialCode);
            if (material == null) {
                material = new Material();
                material.setCode(materialCode);
                material.setName(materialName);
                material.setWarehouseCode(warehouseCode);
                material.setSpec(materialSpec);
                material.setUnit(materialUnit);
                material.setEnable(true);
                success = materialService.save(material);
                if (!success) {
                    throw new ServiceException("入库单下发, 保存物料失败");
                }
            }
            receiptDetail.setInventorySts(inventoryStatus);
            receiptDetail.setMaterialCode(materialCode);
            receiptDetail.setMaterialName(material.getName());
            receiptDetail.setMaterialSpec(material.getSpec());
            receiptDetail.setMaterialUnit(material.getUnit());
            receiptDetail.setBatch(batch);
            receiptDetail.setLot(lot);
            receiptDetail.setProjectNo(project);
            success = receiptDetailService.save(receiptDetail);
            if (!success) {
                throw new ServiceException("入库单下发, 保存入库单详情失败");
            }
            receiptDetailList.add(receiptDetail);
        }
        success = receiptHeaderService.updateReceiptHeader(receiptHeader.getId());
        if (!success) {
            throw new ServiceException("入库单下发, 更新入库单失败");
        }

        return ResultErp.OK("入库单下发成功");
    }

    @Override
    public ResultErp cancelReceipt(String referCode, String warehouseCode) {
        if (StringUtils.isEmpty(warehouseCode)) {
            return ResultErp.error("入库单取消,仓库编码为空");
        }
        String warehouse = warehouseService.getWarehouseNameByCode(warehouseCode);
        if (warehouse == null) {
            return ResultErp.error("入库单取消, 没有找到仓库");
        }
        ReceiptHeader receiptHeader=new ReceiptHeader();
        receiptHeader.setWarehouseCode(warehouseCode);
        receiptHeader.setReferCode(referCode);
        receiptHeader = receiptHeaderService.selectFirstEntity(receiptHeader);
        if (receiptHeader == null) {
            return ResultErp.error("入库单取消, 没有找到入库单");
        }
        AjaxResult ajaxResult = receiptHeaderService.deleteHeaderAndDetail(String.valueOf(receiptHeader.getId()));
        if (ajaxResult.hasErr()) {
            return ResultErp.error("入库单取消失败");
        }
        return ResultErp.OK("取消入库单成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp shipment(ErpShipment erpShipment) {
        if (erpShipment == null) {
            return ResultErp.error("出库单下发,没有传shipment");
        }
        ErpShipmentHeader erpShipmentHeader = erpShipment.getShipmentHeader();
        List<ErpShipmentDetail> erpShipmentDetailList = erpShipment.getShipmentDetailList();
        if (erpShipmentHeader == null) {
            return ResultErp.error("出库单下发,没有传shipmentHeader");
        }
        if (erpShipmentDetailList == null) {
            return ResultErp.error("出库单下发,没有传shipmentDetailList");
        }
        if (StringUtils.isEmpty(erpShipmentHeader.getWarehouseCode())) {
            return ResultErp.error("出库单下发,仓库编码为空");
        }
        if (ShiroUtils.getUser() == null){
            // 模拟mes登录
            userService.login(QuantityConstant.PLATFORM_MES, QuantityConstant.PLATFORM_MES_PASSWORD, erpShipmentHeader.getWarehouseCode(), false);
        }
        String shipmentType = erpShipmentHeader.getShipmentType();
        String referCode = erpShipmentHeader.getReferCode();
        String companyCode = erpShipmentHeader.getCompanyCode();
        String remark = erpShipmentHeader.getRemark();
        String customerCode = erpShipmentHeader.getCustomerCode();
        String warehouseCode = erpShipmentHeader.getWarehouseCode();

        String warehouse = warehouseService.getWarehouseNameByCode(warehouseCode);
        if (warehouse == null) {
            return ResultErp.error("出库单下发, 没有找到仓库");
        }
        if (StringUtils.isEmpty(shipmentType)) {
            return ResultErp.error("出库单下发, shipmentType为空");
        }
        if (StringUtils.isEmpty(referCode)) {
            return ResultErp.error("出库单下发, referCode为空");
        }
        if (StringUtils.isEmpty(companyCode)) {
            Company company = companyService.getDefaultCompany();
            if (company == null) {
                return ResultErp.error("出库单下发, company为空");
            }
            companyCode = company.getCode();
        } else {
            Company company = companyService.getCompanyByCode(companyCode, warehouseCode);
            if (company == null) {
                return ResultErp.error("出库单下发, 没有找到货主");
            }
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setWarehouseCode(warehouseCode);
        shipmentHeader.setCompanyCode(companyCode);
        shipmentHeader.setShipmentType(shipmentType);
        shipmentHeader.setReferCode(referCode);
        shipmentHeader.setCustomerCode(customerCode);
        shipmentHeader.setRemark(remark);

        AjaxResult result = shipmentHeaderService.saveHeader(shipmentHeader);
        if (result.hasErr()) {
            throw new ServiceException("创建出库单头失败" + result.getMsg());
        }

        for (ErpShipmentDetail erpShipmentDetail : erpShipmentDetailList) {
            BigDecimal qty = erpShipmentDetail.getQty();
            String materialCode = erpShipmentDetail.getMaterialCode();
            String inventoryStatus = erpShipmentDetail.getInventoryStatus();
            String batch = erpShipmentDetail.getBatch();
            String lot = erpShipmentDetail.getLot();
            String project = erpShipmentDetail.getProject();
            if (StringUtils.isEmpty(materialCode)) {
                throw new ServiceException("出库单下发, 物料编码为空");
            }
            if (StringUtils.isEmpty(inventoryStatus)) {
                inventoryStatus = QuantityConstant.GOOD;
            }
            if (qty.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("出库单下发, 单据数量必须大于0");
            }
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail.setCompanyCode(companyCode);
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
            shipmentDetail.setQty(qty);
            shipmentDetail.setMoCode(erpShipmentDetail.getMocode());
            shipmentDetail.setReferLineNum(erpShipmentDetail.getReferLineNum());
            Material material = materialService.getMaterialByCode(materialCode);
            if (material == null) {
                throw new ServiceException("出库单下发, 没有找到物料信息" + materialCode);
            }
            shipmentDetail.setInventorySts(inventoryStatus);
            shipmentDetail.setMaterialCode(materialCode);
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            shipmentDetail.setBatch(batch);
            shipmentDetail.setLot(lot);
            shipmentDetail.setProjectNo(project);
            boolean success = shipmentDetailService.save(shipmentDetail);
            if (!success) {
                throw new ServiceException("出库单下发, 保存入库单详情失败");
            }
        }
        boolean success = shipmentHeaderService.updateShipmentHeader(shipmentHeader.getId());
        if (!success) {
            throw new ServiceException("出库单下发, 更新入库单失败");
        }
        return ResultErp.OK("出库单下发成功");
    }

    @Override
    public ResultErp cancelShipment(String referCode, String warehouseCode) {
        if (StringUtils.isEmpty(warehouseCode)) {
            return ResultErp.error("出库单取消,仓库编码为空");
        }
        String warehouse = warehouseService.getWarehouseNameByCode(warehouseCode);
        if (warehouse == null) {
            return ResultErp.error("出库单取消, 没有找到仓库");
        }
        ShipmentHeader shipmentHeader = shipmentHeaderService.getShipmentHeaderByReferCode(referCode, warehouseCode);
        if (shipmentHeader == null) {
            return ResultErp.error("出库单取消, 没有找到出库单");
        }
        AjaxResult ajaxResult = shipmentHeaderService.deleteHeaderAndDetail(shipmentHeader.getId().toString());
        if (ajaxResult.hasErr()) {
            return ResultErp.error("出库单取消失败");
        }
        return ResultErp.OK("取消出库单成功");
    }

    @Override
    public ResultErp searchInventory(InventoryQueryParam inventoryQueryParam) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getWarehouseCode()), InventoryDetail::getWarehouseCode, inventoryQueryParam.getWarehouseCode())
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getMaterialCode()), InventoryDetail::getMaterialCode, inventoryQueryParam.getMaterialCode())
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getCompanyCode()), InventoryDetail::getCompanyCode, inventoryQueryParam.getCompanyCode())
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getZoneCode()), InventoryDetail::getZoneCode, inventoryQueryParam.getZoneCode())
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getContainerCode()), InventoryDetail::getContainerCode, inventoryQueryParam.getContainerCode())
            .eq(StringUtils.isNotEmpty(inventoryQueryParam.getLocationCode()), InventoryDetail::getLocationCode, inventoryQueryParam.getLocationCode());
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (inventoryDetailList.size() == 0) {
            return ResultErp.error("没有找到对应库存");
        }
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            BigDecimal numQty = BigDecimal.ZERO;
            BigDecimal numTaskQty = BigDecimal.ZERO;
            for (InventoryDetail t : inventoryDetailList) {
                if (inventoryDetail.getWarehouseCode().equals(t.getWarehouseCode()) && inventoryDetail.getCompanyCode().equals(t.getCompanyCode())
                    && inventoryDetail.getMaterialCode().equals(t.getMaterialCode())) {
                    numQty = numQty.add(t.getQty());
                    numTaskQty = numTaskQty.add(t.getTaskQty());
                    inventoryDetail.setQty(numQty);
                    inventoryDetail.setTaskQty(numTaskQty);
                }
            }
        }
        List<InventoryDetail> materialSummaryList = new ArrayList<>();
        inventoryDetailList.stream()
            .collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(inventoryMaterialSummary -> inventoryMaterialSummary.getCompanyCode() + ";"
                    + inventoryMaterialSummary.getMaterialCode() + ";" + inventoryMaterialSummary.getWarehouseCode()))),
                ArrayList::new))
            .forEach(list -> materialSummaryList.add(list));
        List<InventorySummary> inventorySummaryList = new ArrayList<>();
        for (InventoryDetail inventoryDetail : materialSummaryList) {
            InventorySummary inventorySummary = new InventorySummary();
            inventorySummary.setWarehouseCode(inventoryDetail.getWarehouseCode());
            inventorySummary.setCompanyCode(inventoryDetail.getCompanyCode());
            inventorySummary.setMaterialCode(inventoryDetail.getMaterialCode());
            BigDecimal availQty = inventoryDetail.getQty().subtract(inventoryDetail.getTaskQty());
            inventorySummary.setQty(inventoryDetail.getQty());
            inventorySummary.setTaskQty(inventoryDetail.getTaskQty());
            inventorySummary.setAvailQty(availQty);
            inventorySummaryList.add(inventorySummary);
        }
        return ResultErp.ok(inventorySummaryList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp addMaterial(ErpMaterial erpMaterial) {
        if (StringUtils.isEmpty(erpMaterial.getCode())) {
            return ResultErp.error("增加物料失败, 物料编码不能为空!!");
        }
        if (StringUtils.isEmpty(erpMaterial.getName())) {
            return ResultErp.error("增加物料失败, 物料名称不能为空!!");
        }
        Material material = new Material();
        BeanUtils.copyProperties(erpMaterial, material);
        boolean success = materialService.save(material);
        if (!success) {
            throw new ServiceException("增加物料失败, 保存时报错");
        }
        return ResultErp.ok("增加物料成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp backReceipt(ReceiptHeader receiptHeader) {
        if (receiptHeader == null) {
            return ResultErp.error("回传入库单, 入库单为空!!");
        }
        List<ReceiptDetail> receiptDetailList = receiptDetailService.findByReceiptId(receiptHeader.getId());
        if (receiptDetailList.size() == 0) {
            return ResultErp.error("回传入库单, 入库单详情为空!!");
        }
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_ERP_BACK_RECEIPT);
        ErpBackReceipt erpBackReceipt = new ErpBackReceipt();
        erpBackReceipt.setReceiptHeader(receiptHeader);
        erpBackReceipt.setReceiptDetailList(receiptDetailList);
        String jsonParam = JSON.toJSONString(erpBackReceipt);
        System.out.println(jsonParam);
        String body =OkHttpUtils.bodypost(url,jsonParam,receiptHeader.getWarehouseCode(),"入库单回传");
//        String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam);
        if (StringUtils.isEmpty(body)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        ResultErp result = JSON.parseObject(body, ResultErp.class);
        if (result.getCode() != 200) {
            receiptHeader.setBackMsg(result.getMessage());
            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN_ERROR);
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN_ERROR);
            receiptHeaderService.updateById(receiptHeader);
            return ResultErp.error(result.getMessage());
        }
        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
        boolean success = receiptHeaderService.updateById(receiptHeader);
        if (!success) {
            throw new ServiceException("回传入库单失败, 更新入库单头失败");
        }
        receiptHeaderService.deleteHeaderAndDetail(receiptHeader.getId().toString());
        return ResultErp.ok("回传入库单成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp backShipment(ShipmentHeader shipmentHeader) {
        if (shipmentHeader == null) {
            return ResultErp.error("回传出库单, 出库单为空!!");
        }
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.getListByShipmentHeaderId(shipmentHeader.getId(),null);
        if (shipmentDetailList.size() == 0) {
            return ResultErp.error("回传出库单, 出库单详情为空!!");
        }
        String url =addressService.selectAddress(QuantityConstant.ADDRESS_ERP_BACK_SHIPMENT);
        ErpBackShipment erpBackShipment = new ErpBackShipment();
        erpBackShipment.setShipmentHeader(shipmentHeader);
        erpBackShipment.setShipmentDetailList(shipmentDetailList);
        String jsonParam = JSON.toJSONString(erpBackShipment);
        System.out.println(jsonParam);
        String body =OkHttpUtils.bodypost(url,jsonParam,shipmentHeader.getWarehouseCode(),"出库单回传");
//        String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam);
        if (StringUtils.isEmpty(body)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        ResultErp result = JSON.parseObject(body, ResultErp.class);
        if (result.getCode() != 200) {
            return ResultErp.error(result.getMessage());
        }
        shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
        boolean success = shipmentHeaderService.updateById(shipmentHeader);
        if (!success) {
            throw new ServiceException("回传出库单失败, 更新出库单头失败");
        }
        return ResultErp.ok("回传出库单成功");
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp supplier(ErpSupplier erpSupplier) {
        if (StringUtils.isEmpty(erpSupplier.getCode())) {
            return ResultErp.error("增加供应商失败, 供应商编码不能为空!!");
        }
        if (StringUtils.isEmpty(erpSupplier.getName())) {
            return ResultErp.error("增加供应商失败, 供应商名称不能为空!!");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(erpSupplier, supplier);
        String supplierCode = supplier.getCode();
        if (StringUtils.isEmpty(supplierCode)) {
            return ResultErp.error("增加供应商失败, 供应商编码不能为空!!");
        }
        Supplier supplier1 = supplierService.getSuppierByCode(supplierCode);
        if (supplier1 != null) {
            supplier.setId(supplier1.getId());
            boolean success = supplierService.updateById(supplier);
            if (!success) {
                throw new ServiceException("更新供应商失败, 更新时报错");
            }
            return ResultErp.ok("更新供应商成功");
        } else {
            boolean success = supplierService.save(supplier);
            if (!success) {
                throw new ServiceException("增加供应商失败, 保存时报错");
            }
            return ResultErp.ok("增加供应商成功");
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp customer(ErpSupplier erpSupplier) {
        if (StringUtils.isEmpty(erpSupplier.getCode())) {
            return ResultErp.error("增加客户失败, 客户编码不能为空!!");
        }
        if (StringUtils.isEmpty(erpSupplier.getName())) {
            return ResultErp.error("增加客户失败, 客户名称不能为空!!");
        }
        Customer customer = new Customer();
        BeanUtils.copyProperties(erpSupplier, customer);
        String customerCode = customer.getCode();
        if (StringUtils.isEmpty(customerCode)) {
            return ResultErp.error("增加客户失败, 客户编码不能为空!!");
        }
        Customer customer1 = customerService.getCustomerDomainByCode(customerCode);
        if (customer1 != null) {
            customer.setId(customer1.getId());
            boolean success = customerService.updateById(customer);
            if (!success) {
                throw new ServiceException("更新客户失败, 更新时报错");
            }
            return ResultErp.ok("更新客户成功");
        } else {
            boolean success = customerService.save(customer);
            if (!success) {
                throw new ServiceException("增加客户失败, 保存时报错");
            }
            return ResultErp.ok("增加客户成功");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultErp transfer(ErpTransfer erpTransfer) {
        if (erpTransfer == null) {
            return ResultErp.error("调拨单下发,没有传transfer");
        }
        ErpTransferHeader erpTransferHeader = erpTransfer.getTransferHeader();
        List<ErpTransferDetail> erpTransferDetailList = erpTransfer.getTransferDetailList();
        if (erpTransferHeader == null) {
            return ResultErp.error("调拨单下发,没有传transferHeader");
        }
        if (erpTransferDetailList == null) {
            return ResultErp.error("调拨单下发,没有传transferDetailList");
        }
        String shipmentType = erpTransferHeader.getTransferType();
        String referCode = erpTransferHeader.getReferCode();
        String companyCode = erpTransferHeader.getCompanyCode();
        String remark = erpTransferHeader.getRemark();
        String customerCode = erpTransferHeader.getCustomerCode();
        String warehouseCode = erpTransferHeader.getWarehouseCode();
        if (StringUtils.isEmpty(warehouseCode)) {
            return ResultErp.error("调拨单下发,仓库编码为空");
        }
        String warehouse = warehouseService.getWarehouseNameByCode(warehouseCode);
        if (warehouse == null) {
            return ResultErp.error("调拨单下发, 没有找到仓库");
        }
        if (ShiroUtils.getUser() == null){
            // 模拟mes登录
            userService.login(QuantityConstant.PLATFORM_MES, QuantityConstant.PLATFORM_MES_PASSWORD, erpTransferHeader.getWarehouseCode(), false);
        }
        if (StringUtils.isEmpty(shipmentType)) {
            return ResultErp.error("调拨单下发, transferType为空");
        }
        if (StringUtils.isEmpty(referCode)) {
            return ResultErp.error("调拨单下发, referCode为空");
        }
        if (StringUtils.isEmpty(companyCode)) {
            Company company = companyService.getDefaultCompany();
            if (company == null) {
                return ResultErp.error("调拨单下发, company为空");
            }
            companyCode = company.getCode();
        } else {
            Company company = companyService.getCompanyByCode(companyCode, warehouseCode);
            if (company == null) {
                return ResultErp.error("调拨单下发, 没有找到货主");
            }
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setWarehouseCode(warehouseCode);
        shipmentHeader.setCompanyCode(companyCode);
        shipmentHeader.setShipmentType(QuantityConstant.TRANSFER_C);
        shipmentHeader.setReferCode(referCode);
        shipmentHeader.setCustomerCode(customerCode);
        shipmentHeader.setRemark(remark);
        AjaxResult result = shipmentHeaderService.saveHeader(shipmentHeader);
        if (result.hasErr()) {
            throw new ServiceException("创建调拨单头失败" + result.getMsg());
        }

        for (ErpTransferDetail erpTransferDetail : erpTransferDetailList) {
            BigDecimal qty = erpTransferDetail.getQty();
            String materialCode = erpTransferDetail.getMaterialCode();
            String inventoryStatus = erpTransferDetail.getInventoryStatus();
            String batch = erpTransferDetail.getBatch();
            String lot = erpTransferDetail.getLot();
            String project = erpTransferDetail.getProject();
            if (StringUtils.isEmpty(materialCode)) {
                throw new ServiceException("出调拨单下发, 物料编码为空");
            }
            if (StringUtils.isEmpty(inventoryStatus)) {
                inventoryStatus = QuantityConstant.GOOD;
            }
            if (qty.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("调拨单下发, 单据数量必须大于0");
            }
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail.setCompanyCode(companyCode);
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
            shipmentDetail.setQty(qty);
            shipmentDetail.setMoCode(erpTransferDetail.getMocode());
            shipmentDetail.setReferLineNum(erpTransferDetail.getReferLineNum());
            Material material = materialService.getMaterialByCode(materialCode);
            if (material == null) {
                throw new ServiceException("调拨单下发, 没有找到物料信息" + materialCode);
            }
            shipmentDetail.setInventorySts(inventoryStatus);
            shipmentDetail.setMaterialCode(materialCode);
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            shipmentDetail.setBatch(batch);
            shipmentDetail.setLot(lot);
            shipmentDetail.setProjectNo(project);
            boolean success = shipmentDetailService.save(shipmentDetail);
            if (!success) {
                throw new ServiceException("调拨单下发, 保存入库单详情失败");
            }
        }
        boolean success = shipmentHeaderService.updateShipmentHeader(shipmentHeader.getId());
        if (!success) {
            throw new ServiceException("调拨单下发, 更新出库单失败");
        }
        ResultErp resultErp=erpService.transferReceipt(erpTransfer);
        if (!resultErp.isSuccess()) {
            throw new ServiceException("调拨单下发, 更新入库单失败");
        }
        return ResultErp.OK("调拨单下发成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultErp transferReceipt(ErpTransfer erpTransfer) {
        boolean success = false;

        ErpTransferHeader erpReceiptHeader = erpTransfer.getTransferHeader();
        List<ErpTransferDetail> erpReceiptDetailList = erpTransfer.getTransferDetailList();

        String warehouseCode = erpReceiptHeader.getWarehouseCode();
        String referCode = erpReceiptHeader.getReferCode();
        String companyCode = erpReceiptHeader.getCompanyCode();
        String remark = erpReceiptHeader.getRemark();


        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setWarehouseCode(warehouseCode);
        receiptHeader.setCompanyCode(companyCode);
        receiptHeader.setReceiptType(QuantityConstant.TRANSFER_R);
        receiptHeader.setReferCode(referCode);
        receiptHeader.setReceiptNote(remark);
        AjaxResult result = receiptHeaderService.saveReceiptHeader(receiptHeader);
        if (result.hasErr()) {
            throw new ServiceException("调拨单:创建入库单头失败" + result.getMsg());
        }
        List<ReceiptDetail> receiptDetailList=new ArrayList<>();
        for (ErpTransferDetail erpReceiptDetail : erpReceiptDetailList) {

            BigDecimal qty = erpReceiptDetail.getQty();
            String materialCode = erpReceiptDetail.getMaterialCode();
            String inventoryStatus = erpReceiptDetail.getInventoryStatus();
            String batch = erpReceiptDetail.getBatch();
            String lot = erpReceiptDetail.getLot();
            String project = erpReceiptDetail.getProject();
            if (StringUtils.isEmpty(materialCode)) {
                throw new ServiceException("调拨单下发, 物料编码为空");
            }
            if (StringUtils.isEmpty(inventoryStatus)) {
                inventoryStatus = QuantityConstant.GOOD;
            }
            if (qty.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("调拨单下发, 单据数量必须大于0");
            }
            ReceiptDetail receiptDetail = new ReceiptDetail();
            receiptDetail.setWarehouseCode(warehouseCode);
            receiptDetail.setCompanyCode(companyCode);
            receiptDetail.setReferCode(referCode);
            receiptDetail.setReceiptCode(receiptHeader.getCode());
            receiptDetail.setReceiptId(receiptHeader.getId());
            receiptDetail.setStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
            receiptDetail.setQty(qty);
            receiptDetail.setMoCode(erpReceiptDetail.getMocode());
            receiptDetail.setReferLineNum(erpReceiptDetail.getReferLineNum());
            Material material = materialService.getMaterialByCode(materialCode);

            receiptDetail.setInventorySts(inventoryStatus);
            receiptDetail.setMaterialCode(materialCode);
            receiptDetail.setMaterialName(material.getName());
            receiptDetail.setMaterialSpec(material.getSpec());
            receiptDetail.setMaterialUnit(material.getUnit());
            receiptDetail.setBatch(batch);
            receiptDetail.setLot(lot);
            receiptDetail.setProjectNo(project);
            success = receiptDetailService.save(receiptDetail);
            if (!success) {
                throw new ServiceException("调拨单下发, 保存入库单详情失败");
            }
            receiptDetailList.add(receiptDetail);
        }
        success = receiptHeaderService.updateReceiptHeader(receiptHeader.getId());
        if (!success) {
            throw new ServiceException("调拨单下发, 更新入库单失败");
        }

        return ResultErp.OK("调拨单下发成功");
    }
}