From 04503f8c26c955c18a31efae2bb6d3bd533e39a2 Mon Sep 17 00:00:00 2001 From: 10001392 <1055202292@qq.com> Date: Sat, 12 Oct 2024 17:39:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9B=BF=E4=BB=A3BOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/bomnew/api/user/BatchBomApi.java | 35 +++ .../product/bomnew/pojo/vo/BaseBomVO.java | 3 + .../bomnew/service/BatchBomService.java | 283 +++++++++++++++++- 3 files changed, 316 insertions(+), 5 deletions(-) diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/BatchBomApi.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/BatchBomApi.java index 8b7a3ade..a5515ede 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/BatchBomApi.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/BatchBomApi.java @@ -1,12 +1,16 @@ package com.nflg.product.bomnew.api.user; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import com.nflg.product.base.core.api.BaseApi; import com.nflg.product.bomnew.pojo.query.BatchBomQuery; +import com.nflg.product.bomnew.pojo.query.PbomImportToSAPQuery; import com.nflg.product.bomnew.pojo.vo.BaseBomVO; import com.nflg.product.bomnew.pojo.vo.BaseMaterialVO; import com.nflg.product.bomnew.service.BatchBomService; +import com.nflg.product.bomnew.service.BomNewEbomExportToSAP; +import com.nflg.product.bomnew.service.BomNewPbomParentService; import com.nflg.product.bomnew.service.MaterialMainService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -33,6 +37,8 @@ public class BatchBomApi extends BaseApi { private MaterialMainService materialMainService; @Resource private BatchBomService batchBomService; + @Resource + private BomNewPbomParentService bomNewPbomParentService; @GetMapping("getMaterialInfo") @ApiOperation("查询物料信息") @@ -53,4 +59,33 @@ public class BatchBomApi extends BaseApi { return ResultVO.success(batchBomService.getParentBomList(batchBomQuery)); } + @PostMapping("updateEBom") + @ApiOperation("更新EBOM并导入SAP") + public ResultVO> updateEBom(@RequestBody List baseBomVOList) { + List addRowIds = batchBomService.updateEBom(baseBomVOList); + // 导入SAP + if (CollUtil.isNotEmpty(addRowIds)) { + addRowIds.forEach(rootRowId -> { + BomNewEbomExportToSAP exportToSAP = new BomNewEbomExportToSAP(); + exportToSAP.export(rootRowId); + }); + } + return ResultVO.success(); + } + + @PostMapping("updatePBom") + @ApiOperation("更新PBOM并导入SAP") + public ResultVO> updatePBom(@RequestBody List baseBomVOList) { + List addRowIds = batchBomService.updatePBom(baseBomVOList); + // 导入SAP + if (CollUtil.isNotEmpty(addRowIds)) { + addRowIds.forEach(rootRowId -> { + PbomImportToSAPQuery query = new PbomImportToSAPQuery(); + query.setRootBomRowId(rootRowId); + query.setIsForSale(false); + bomNewPbomParentService.importToSAP2(query); + }); + } + return ResultVO.success(); + } } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/vo/BaseBomVO.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/vo/BaseBomVO.java index 4ab2c7dc..84fcc9d6 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/vo/BaseBomVO.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/vo/BaseBomVO.java @@ -36,6 +36,9 @@ public class BaseBomVO { @ApiModelProperty("父级BOM版本号") private String parentVersion; + @ApiModelProperty("子级BOM ID") + private Long childRowId; + @ApiModelProperty("原物料编码") private String materialNo; diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BatchBomService.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BatchBomService.java index 4a6e253c..7ff62470 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BatchBomService.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BatchBomService.java @@ -1,10 +1,22 @@ package com.nflg.product.bomnew.service; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.nflg.product.base.core.exception.NflgBusinessException; import com.nflg.product.bomnew.constant.EBomStatusEnum; +import com.nflg.product.bomnew.constant.PBomStatusEnum; +import com.nflg.product.bomnew.constant.SapStatusEnum; +import com.nflg.product.bomnew.mapper.master.BomNewEbomChildMapper; +import com.nflg.product.bomnew.mapper.master.BomNewEbomParentMapper; +import com.nflg.product.bomnew.mapper.master.BomNewPbomChildMapper; +import com.nflg.product.bomnew.mapper.master.BomNewPbomParentMapper; import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity; import com.nflg.product.bomnew.pojo.entity.BomNewEbomParentEntity; +import com.nflg.product.bomnew.pojo.entity.BomNewPbomChildEntity; +import com.nflg.product.bomnew.pojo.entity.BomNewPbomParentEntity; import com.nflg.product.bomnew.pojo.query.BatchBomQuery; import com.nflg.product.bomnew.pojo.vo.BaseBomVO; import com.nflg.product.bomnew.pojo.vo.BaseMaterialVO; @@ -13,14 +25,13 @@ import com.nflg.product.bomnew.util.VersionUtil; import lombok.extern.slf4j.Slf4j; import nflg.product.common.constant.STATE; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.time.LocalDateTime; +import java.util.*; import java.util.stream.Collectors; @Service @@ -32,10 +43,18 @@ public class BatchBomService { @Resource private BomNewEbomChildService bomNewEbomChildService; @Resource + private BomNewEbomParentMapper bomNewEbomParentMapper; + @Resource + private BomNewEbomChildMapper bomNewEbomChildMapper; + @Resource private BomNewPbomParentService bomNewPbomParentService; @Resource private BomNewPbomChildService bomNewPbomChildService; @Resource + private BomNewPbomParentMapper bomNewPbomParentMapper; + @Resource + private BomNewPbomChildMapper bomNewPbomChildMapper; + @Resource private MaterialMainService materialMainService; public List getParentBomList(BatchBomQuery batchBomQuery) { @@ -74,6 +93,7 @@ public class BatchBomService { Optional first = ebomChildEntities.stream().filter(item -> item.getParentRowId().equals(parent.getRowId())).findFirst(); if (first.isPresent()) { BomNewEbomChildEntity ebomChild = first.get(); + baseBomVO.setChildRowId(ebomChild.getRowId()); baseBomVO.setMaterialNo(ebomChild.getMaterialNo()); baseBomVO.setNum(ebomChild.getNum()); baseBomVO.setUnit(ebomChild.getMaterialUnit()); @@ -87,7 +107,49 @@ public class BatchBomService { } } } else if ("pbom".equals(bomType)) { - + String materialNo = batchBomQuery.getMaterialNo(); + // 查询出子级 + List pbomChildEntities = bomNewPbomChildService.lambdaQuery() + .eq(BomNewPbomChildEntity::getMaterialNo, materialNo) + .eq(BomNewPbomChildEntity::getFacCode, batchBomQuery.getFactory()) // 工厂查询 + .list(); + if (CollectionUtil.isNotEmpty(pbomChildEntities)) { + // 根据子级的parentRowId查询父级(状态是>=已发布,即正式表数据) + List parentRowIds = pbomChildEntities.stream().map(BomNewPbomChildEntity::getParentRowId).collect(Collectors.toList()); + List pbomParentEntities = bomNewPbomParentService.lambdaQuery() + .in(BomNewPbomParentEntity::getRowId, parentRowIds) + .ge(BomNewPbomParentEntity::getStatus, PBomStatusEnum.PUBLISH.getValue()) + .orderByDesc(BomNewPbomParentEntity::getCreatedTime) + .list(); + if (CollectionUtil.isNotEmpty(pbomParentEntities)) { + long counter = 0; + for (BomNewPbomParentEntity parent : pbomParentEntities) { + BaseBomVO baseBomVO = new BaseBomVO(); + baseBomVO.setOrderNum(++counter); + baseBomVO.setParentRowId(parent.getRowId()); + baseBomVO.setParentMaterialNo(parent.getMaterialNo()); + baseBomVO.setParentVersion(parent.getCurrentVersion()); + baseBomVO.setCreatedBy(parent.getCreatedBy()); + baseBomVO.setCreatedTime(parent.getCreatedTime()); + baseBomVO.setExpireEndTime(parent.getExpireEndTime()); + baseBomVO.setDeviseUserCode(parent.getDeviseUserCode()); + baseBomVO.setDeviseName(parent.getDeviseName()); + Optional first = pbomChildEntities.stream().filter(item -> item.getParentRowId().equals(parent.getRowId())).findFirst(); + if (first.isPresent()) { + BomNewPbomChildEntity pbomChild = first.get(); + baseBomVO.setChildRowId(pbomChild.getRowId()); + baseBomVO.setMaterialNo(pbomChild.getMaterialNo()); + baseBomVO.setNum(pbomChild.getNum()); + baseBomVO.setUnit(pbomChild.getMaterialUnit()); + } + baseBomVO.setNewMaterialNo(batchBomQuery.getNewMaterialNo()); + baseBomVO.setNewNum(batchBomQuery.getNewReplaceTimes().divide(batchBomQuery.getReplaceTimes(), 3, RoundingMode.HALF_UP).multiply(baseBomVO.getNum())); + baseBomVO.setNewUnit(batchBomQuery.getNewMaterialUnit()); + baseBomVO.setNewVersion(VersionUtil.getNextVersion(baseBomVO.getParentVersion())); + resultList.add(baseBomVO); + } + } + } } if (CollectionUtil.isEmpty(resultList)) { return null; @@ -111,4 +173,215 @@ public class BatchBomService { return resultList; } + /** + * 更新BOM并导入SAP(但是EBOM的话,不生成PBOM工作表) + * @param baseBomVOList + */ + @Transactional(rollbackFor = Exception.class) + public List updateEBom(List baseBomVOList) { + if (CollectionUtil.isEmpty(baseBomVOList)) { + return null; + } + List materialBaseInfo = materialMainService.getMaterialBaseInfo(Collections.singletonList(baseBomVOList.get(0).getNewMaterialNo())); + if (CollectionUtil.isEmpty(materialBaseInfo)) { + throw new NflgBusinessException(STATE.ParamErr, "新物料编码不存在"); + } + BaseMaterialVO newMaterialInfo= materialBaseInfo.get(0); + List addRowIds = new ArrayList<>(baseBomVOList.size()); // 需要加入历史表的父级BOM ID + List delRowIds = new ArrayList<>(baseBomVOList.size()); // 需要从正式表删除的父级BOM ID + for (BaseBomVO baseBomVO: baseBomVOList) { + // 找到父级 + BomNewEbomParentEntity ebomParentEntity = bomNewEbomParentService.lambdaQuery().eq(BomNewEbomParentEntity::getRowId, baseBomVO.getParentRowId()).one(); + if (ObjectUtil.isNotEmpty(ebomParentEntity)) { + LocalDateTime now = LocalDateTime.now(); + // 找到该父级的所有子级列表 + List ebomChildEntities = bomNewEbomChildService.lambdaQuery().eq(BomNewEbomChildEntity::getParentRowId, baseBomVO.getParentRowId()).list(); + // 在子级列表中找到要被替换的那条子级 + List collect = ebomChildEntities.stream().filter(item -> item.getRowId().equals(baseBomVO.getChildRowId())).collect(Collectors.toList()); + BomNewEbomChildEntity replaceChild = collect.get(0); + // 其他子级 + List otherChildren = ebomChildEntities.stream().filter(item -> !item.getRowId().equals(baseBomVO.getChildRowId())).collect(Collectors.toList()); + // 构建新版的父级规则:大版本号+1(如果该BOM还有编辑中的草稿版本,则草稿版本号也+1) + BomNewEbomParentEntity newParent = new BomNewEbomParentEntity(); + BeanUtil.copyProperties(ebomParentEntity, newParent); + newParent.setRowId(IdWorker.getId()); + newParent.setCurrentVersion(baseBomVO.getNewVersion()); // 新版本号 + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newParent.setCreatedTime(now); + newParent.setModifyTime(now); + newParent.setSapState(SapStatusEnum.UNPUB_SAP.getValue()); + newParent.setSapTime(null); + // 构建新版的子级规则:其他子级复制一份,被替换的子级改物料编码,数量,单位等 + List newChildList = new ArrayList<>(ebomChildEntities.size()); + otherChildren.forEach(item -> { + BomNewEbomChildEntity newChild = new BomNewEbomChildEntity(); + BeanUtil.copyProperties(item, newChild); + newChild.setRowId(IdWorker.getId()); + newChild.setParentRowId(newParent.getRowId()); + newChild.setIdentityNo(newParent.getRowId() + "_" + newChild.getRowId()); + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newChild.setCreatedTime(now); + newChild.setModifyTime(now); + newChildList.add(newChild); + }); + BomNewEbomChildEntity newReplaceChild = new BomNewEbomChildEntity(); + BeanUtil.copyProperties(replaceChild, newReplaceChild); + newReplaceChild.setRowId(IdWorker.getId()); + newReplaceChild.setParentRowId(newParent.getRowId()); + newReplaceChild.setIdentityNo(newParent.getRowId() + "_" + newReplaceChild.getRowId()); + newReplaceChild.setMaterialNo(baseBomVO.getNewMaterialNo()); // 新物料编码 + newReplaceChild.setDrawingNo(newMaterialInfo.getDrawingNo()); + newReplaceChild.setMaterialName(newMaterialInfo.getMaterialName()); + newReplaceChild.setMaterialDesc(newMaterialInfo.getMaterialDesc()); // 新物料描述 + newReplaceChild.setMaterialTexture(newMaterialInfo.getMaterialTexture()); + newReplaceChild.setMaterialUnit(baseBomVO.getNewUnit()); // 新单位 + newReplaceChild.setNum(baseBomVO.getNewNum()); // 新数量 + newReplaceChild.setMaterialCategoryCode(newMaterialInfo.getMaterialCategoryCode()); + // TODO 新单重?新总重?要不要更新 + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newReplaceChild.setCreatedTime(now); + newReplaceChild.setModifyTime(now); + newReplaceChild.setRemark("【批量替代BOM】由" + baseBomVO.getMaterialNo() + "替代为" + baseBomVO.getNewMaterialNo()); + newChildList.add(newReplaceChild); + bomNewEbomParentService.save(newParent); + bomNewEbomChildService.saveBatch(newChildList); + addRowIds.add(newParent.getRowId()); + } + delRowIds.add(ebomParentEntity.getRowId()); + } + // 转入历史表 + if (CollUtil.isNotEmpty(addRowIds)) { + bomNewEbomParentMapper.insertEBomFormalParent(addRowIds); + bomNewEbomParentMapper.insertEBomFormalChild(addRowIds); + } + if (CollUtil.isNotEmpty(delRowIds)){ + // 转移后删除 + bomNewEbomParentService.delEBomHistory(delRowIds); + } + // TODO 如果有草稿中数据,草稿版本+1 + for (BaseBomVO baseBomVO: baseBomVOList) { + BomNewEbomParentEntity draftParent = bomNewEbomParentService.lambdaQuery() + .eq(BomNewEbomParentEntity::getMaterialNo, baseBomVO.getParentMaterialNo()) + .lt(BomNewEbomParentEntity::getStatus, EBomStatusEnum.PUBLISHED.getValue()) + .one(); + if (ObjectUtil.isNotEmpty(draftParent)) { + // 草稿版本 A01 -> A02,A01.1 -> A02.1 + String[] currentVersionArr = draftParent.getCurrentVersion().split("."); + String newVersion = VersionUtil.getNextVersion(currentVersionArr[0]); + if (currentVersionArr.length > 1) { + newVersion += "." + currentVersionArr[1]; + } + draftParent.setCurrentVersion(newVersion); + draftParent.setModifyTime(LocalDateTime.now()); + bomNewEbomParentService.updateById(draftParent); + } + } + return addRowIds; + } + + /** + * 更新BOM并导入SAP + * @param baseBomVOList + */ + @Transactional(rollbackFor = Exception.class) + public List updatePBom(List baseBomVOList) { + if (CollectionUtil.isEmpty(baseBomVOList)) { + return null; + } + List materialBaseInfo = materialMainService.getMaterialBaseInfo(Collections.singletonList(baseBomVOList.get(0).getNewMaterialNo())); + if (CollectionUtil.isEmpty(materialBaseInfo)) { + throw new NflgBusinessException(STATE.ParamErr, "新物料编码不存在"); + } + BaseMaterialVO newMaterialInfo= materialBaseInfo.get(0); + List addRowIds = new ArrayList<>(baseBomVOList.size()); // 需要加入历史表的父级BOM ID + List delRowIds = new ArrayList<>(baseBomVOList.size()); // 需要从正式表删除的父级BOM ID + for (BaseBomVO baseBomVO: baseBomVOList) { + // 找到父级 + BomNewPbomParentEntity pbomParentEntity = bomNewPbomParentService.lambdaQuery().eq(BomNewPbomParentEntity::getRowId, baseBomVO.getParentRowId()).one(); + if (ObjectUtil.isNotEmpty(pbomParentEntity)) { + LocalDateTime now = LocalDateTime.now(); + // 找到该父级的所有子级列表 + List pbomChildEntities = bomNewPbomChildService.lambdaQuery().eq(BomNewPbomChildEntity::getParentRowId, baseBomVO.getParentRowId()).list(); + // 在子级列表中找到要被替换的那条子级 + List collect = pbomChildEntities.stream().filter(item -> item.getRowId().equals(baseBomVO.getChildRowId())).collect(Collectors.toList()); + BomNewPbomChildEntity replaceChild = collect.get(0); + // 其他子级 + List otherChildren = pbomChildEntities.stream().filter(item -> !item.getRowId().equals(baseBomVO.getChildRowId())).collect(Collectors.toList()); + // 构建新版的父级规则:大版本号+1(如果该BOM还有编辑中的草稿版本,则草稿版本号也+1) + BomNewPbomParentEntity newParent = new BomNewPbomParentEntity(); + BeanUtil.copyProperties(pbomParentEntity, newParent); + newParent.setRowId(IdWorker.getId()); + newParent.setCurrentVersion(baseBomVO.getNewVersion()); // 新版本号 + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newParent.setCreatedTime(now); + newParent.setModifyTime(now); + newParent.setSapState(SapStatusEnum.UNPUB_SAP.getValue()); + newParent.setSapTime(null); + // 构建新版的子级规则:其他子级复制一份,被替换的子级改物料编码,数量,单位等 + List newChildList = new ArrayList<>(pbomChildEntities.size()); + otherChildren.forEach(item -> { + BomNewPbomChildEntity newChild = new BomNewPbomChildEntity(); + BeanUtil.copyProperties(item, newChild); + newChild.setRowId(IdWorker.getId()); + newChild.setParentRowId(newParent.getRowId()); + newChild.setIdentityNo(newParent.getRowId() + "_" + newChild.getRowId()); + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newChild.setCreatedTime(now); + newChild.setModifyTime(now); + newChildList.add(newChild); + }); + BomNewPbomChildEntity newReplaceChild = new BomNewPbomChildEntity(); + BeanUtil.copyProperties(replaceChild, newReplaceChild); + newReplaceChild.setRowId(IdWorker.getId()); + newReplaceChild.setParentRowId(newParent.getRowId()); + newReplaceChild.setIdentityNo(newParent.getRowId() + "_" + newReplaceChild.getRowId()); + newReplaceChild.setMaterialNo(baseBomVO.getNewMaterialNo()); // 新物料编码 + newReplaceChild.setDrawingNo(newMaterialInfo.getDrawingNo()); + newReplaceChild.setMaterialName(newMaterialInfo.getMaterialName()); + newReplaceChild.setMaterialDesc(newMaterialInfo.getMaterialDesc()); // 新物料描述 + newReplaceChild.setMaterialTexture(newMaterialInfo.getMaterialTexture()); + newReplaceChild.setMaterialUnit(baseBomVO.getNewUnit()); // 新单位 + newReplaceChild.setNum(baseBomVO.getNewNum()); // 新数量 + newReplaceChild.setMaterialCategoryCode(newMaterialInfo.getMaterialCategoryCode()); + // TODO 新单重?新总重?要不要更新 + // 新版本创建时间要更新,创建人要不要更新待定(根据权限管理考虑) + newReplaceChild.setCreatedTime(now); + newReplaceChild.setModifyTime(now); + newReplaceChild.setRemark("【批量替代BOM】由" + baseBomVO.getMaterialNo() + "替代为" + baseBomVO.getNewMaterialNo()); + newChildList.add(newReplaceChild); + bomNewPbomParentService.save(newParent); + bomNewPbomChildService.saveBatch(newChildList); + addRowIds.add(newParent.getRowId()); + } + delRowIds.add(pbomParentEntity.getRowId()); + } + // 转入历史表 + if (CollUtil.isNotEmpty(addRowIds)) { + bomNewPbomParentMapper.insertPBomParentToFormal(addRowIds); + bomNewPbomParentMapper.insertPBomChildToFormal(addRowIds); + } + if (CollUtil.isNotEmpty(delRowIds)){ + // 转移后删除 + bomNewPbomParentService.delPBom(delRowIds); + } + // TODO 如果有草稿中数据,草稿版本+1 + for (BaseBomVO baseBomVO: baseBomVOList) { + BomNewPbomParentEntity draftParent = bomNewPbomParentService.lambdaQuery() + .eq(BomNewPbomParentEntity::getMaterialNo, baseBomVO.getParentMaterialNo()) + .lt(BomNewPbomParentEntity::getStatus, PBomStatusEnum.PUBLISH.getValue()) + .one(); + if (ObjectUtil.isNotEmpty(draftParent)) { + // 草稿版本 A01 -> A02,A01.1 -> A02.1 + String[] currentVersionArr = draftParent.getCurrentVersion().split("."); + String newVersion = VersionUtil.getNextVersion(currentVersionArr[0]); + if (currentVersionArr.length > 1) { + newVersion += "." + currentVersionArr[1]; + } + draftParent.setCurrentVersion(newVersion); + draftParent.setModifyTime(LocalDateTime.now()); + bomNewPbomParentService.updateById(draftParent); + } + } + return addRowIds; + } }