feat(ebom): 优化导入到sap逻辑

This commit is contained in:
曹鹏飞 2024-05-30 16:52:13 +08:00
parent 8f53c2c1a7
commit 51ef53e7aa
4 changed files with 225 additions and 12 deletions

View File

@ -435,7 +435,9 @@ public class EbomApi extends BaseApi {
@ApiOperation("导入到SAP")
public ResultVO<List<OperationErrorMsgVO>> importToSAP(@Valid @RequestBody @NotEmpty List<Long> rootBomRowIds) {
VUtils.isTure(rootBomRowIds.size() > 1).throwMessage("每次只能导入1条");
return ResultVO.success(bomNewEbomParentService.importToSAP(rootBomRowIds.get(0)));
// return ResultVO.success(bomNewEbomParentService.importToSAP(rootBomRowIds.get(0)));
BomNewEbomExportToSAP exportToSAP = new BomNewEbomExportToSAP();
return ResultVO.success(exportToSAP.export(rootBomRowIds.get(0)));
}
/**

View File

@ -0,0 +1,209 @@
package com.nflg.product.bomnew.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nflg.product.base.core.conmon.util.SessionUtil;
import com.nflg.product.bomnew.constant.BomConstant;
import com.nflg.product.bomnew.constant.EBomStatusEnum;
import com.nflg.product.bomnew.constant.SapStatusEnum;
import com.nflg.product.bomnew.constant.VirtualPackageTypeEnum;
import com.nflg.product.bomnew.pojo.dto.sap.impart2.ImportSapParamDTO;
import com.nflg.product.bomnew.pojo.dto.sap.impart2.T1DTO;
import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity;
import com.nflg.product.bomnew.pojo.entity.BomNewEbomParentEntity;
import com.nflg.product.bomnew.pojo.entity.BomNewSapErrorMsgEntity;
import com.nflg.product.bomnew.pojo.vo.OperationErrorMsgVO;
import com.nflg.product.bomnew.util.VUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
/**
* @author 曹鹏飞
* @date 2024/5/30 14:32:26
*/
public class BomNewEbomExportToSAP {
private final static String TAG = "Ebom导入到SAP";
private final String dateYMD = DateUtil.format(new Date(), "yyyyMMdd");
private final Map<Long, List<T1DTO>> cmap = new HashMap<>();
private final List<T1DTO> children = new ArrayList<>();
private final static Logger LOGGER = LoggerFactory.getLogger(BomNewEbomExportToSAP.class);
private final BomNewEbomParentService bomNewEbomParentService = SpringUtil.getBean(BomNewEbomParentService.class);
private final BomNewEbomChildService bomNewEbomChildService = SpringUtil.getBean(BomNewEbomChildService.class);
private final BomNewSapErrorMsgService bomNewSapErrorMsgService = SpringUtil.getBean(BomNewSapErrorMsgService.class);
public List<OperationErrorMsgVO> export(Long rootBomRowId) {
BomNewEbomParentEntity root = bomNewEbomParentService.getById(rootBomRowId);
VUtils.isTure(Objects.isNull(root)).throwMessage("数据不存在");
VUtils.isTure(root.getUserRootIs() != 1).throwMessage("请选择根节点");
VUtils.isTure(Objects.equals(root.getSapState(), SapStatusEnum.PUB_RUNNING.getValue()))
.throwMessage("正在导入中,请等待操作完成");
List<OperationErrorMsgVO> liErrMsg = null;
updateSapState(rootBomRowId, SapStatusEnum.PUB_RUNNING);
SapStatusEnum state = SapStatusEnum.PUB_ERROR_ALL;
try {
List<BomNewEbomChildEntity> children = getChildren(root);
children.forEach(it -> buildChildrenForSap(0L, root, it));
ImportSapParamDTO sapDto = new ImportSapParamDTO();
sapDto.setZID(RandomUtil.randomNumbers(5));
sapDto.setI_WERKS("1");
sapDto.setI_STLAN("2");
sapDto.setI_EMPNO(root.getCreatedBy());
sapDto.setT1(this.children);
liErrMsg = SpringUtil.getBean(SapOpUtilService.class).importToSapV2(sapDto, null);
if (CollUtil.isEmpty(liErrMsg)) {
state = SapStatusEnum.PUB_SUCCESS;
} else if (sapDto.getT1().size() != liErrMsg.size()) {
state = SapStatusEnum.PUB_ERROR;
}
saveSapErrorMsg(rootBomRowId, liErrMsg);
} finally {
updateSapState(rootBomRowId, state);
}
return liErrMsg;
}
/**
* 构建child数据
* @param cRowId 上层的直发包rowId
* @param p 父级节点的parent
* @param c 父级节点的child
* @return 是否是空的直发包
*/
private boolean buildChildrenForSap(Long cRowId, BomNewEbomParentEntity p, BomNewEbomChildEntity c) {
List<BomNewEbomChildEntity> cc = getChildren(p);
if (CollUtil.isEmpty(cc)) {
return Objects.equals(c.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue());
}
boolean isEmpty = false;
for (BomNewEbomChildEntity it : cc) {
if (Objects.equals(c.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue())) {
if (Objects.nonNull(cRowId)) {
if (cRowId == 0L) {
LOGGER.debug("{} 添加物料 编号:{},父级编号:{},数量:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), it.getMaterialDesc());
children.add(convert(it, c.getMaterialNo()));
} else {
T1DTO cct = cmap.get(cRowId).stream()
.filter(ct -> (StrUtil.equals(ct.getPOSTP(), BomConstant.PROJECT_TYPE_TEMPORARY, true) && StrUtil.equals(ct.getPOTX1(), it.getMaterialDesc()))
|| (!StrUtil.equals(ct.getPOSTP(), BomConstant.PROJECT_TYPE_TEMPORARY, true) && StrUtil.equals(ct.getIDNRK(), it.getMaterialNo())))
.findFirst()
.orElse(null);
if (Objects.isNull(cct)) {
LOGGER.debug("{} 物料提层到上级发货包 编号:{},父级编号:{},数量:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), it.getMaterialDesc());
cct = convert(it, p.getMaterialNo());
cmap.get(cRowId).add(cct);
children.add(cct);
} else {
//合并计算数量
cct.setMENGE(new BigDecimal(cct.getMENGE()).add(it.getNum()).toString());
LOGGER.debug("{} 物料提层到上级发货包且合并 编号:{},父级编号:{},数量:{},合并后总数:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), cct.getMENGE(), it.getMaterialDesc());
}
}
}
isEmpty = true;
} else if (!Objects.equals(it.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue())) {
LOGGER.debug("{} 添加物料 编号:{},父级编号:{},数量:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), it.getMaterialDesc());
children.add(convert(it, c.getMaterialNo()));
}
if (it.getVirtualPartType() > VirtualPackageTypeEnum.UN_VIRTUAL_PACKAGE.getValue()
&& !Objects.equals(it.getVirtualPartType(), VirtualPackageTypeEnum.DELIVERY_PACKAGE.getValue())) {
cRowId = null;
}
BomNewEbomParentEntity cp = getParent(it);
if (Objects.nonNull(cp)) {
List<BomNewEbomChildEntity> pcs = getChildren(cp);
if (CollUtil.isNotEmpty(pcs)) {
BomNewEbomChildEntity zfc = pcs.stream()
.filter(pt -> Objects.equals(pt.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue()))
.findFirst()
.orElse(null);
if (Objects.nonNull(zfc)) {
BomNewEbomParentEntity zfp = getParent(zfc);
VUtils.isTure(Objects.isNull(zfp)).throwMessage(StrUtil.format("直发包({})对应的parent丢失", zfc.getMaterialNo()));
cRowId = zfp.getRowId();
cmap.put(cRowId, new ArrayList<>());
LOGGER.debug("{} 直发包 rowId{},编号:{},描述:{}", TAG, cRowId, zfc.getMaterialNo(), zfc.getMaterialDesc());
}
}
boolean b = buildChildrenForSap(cRowId, cp, it);
if (!b && Objects.equals(it.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue())) {
//非空的直发包
LOGGER.debug("{} 添加物料 编号:{},父级编号:{},数量:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), it.getMaterialDesc());
children.add(convert(it, c.getMaterialNo()));
}
}
}
return isEmpty;
}
private void saveSapErrorMsg(Long rootBomRowId, List<OperationErrorMsgVO> liErrMsg) {
bomNewSapErrorMsgService.getBaseMapper().delete(Wrappers.lambdaQuery(BomNewSapErrorMsgEntity.class)
.eq(BomNewSapErrorMsgEntity::getTargetRowId, rootBomRowId));
if (CollUtil.isNotEmpty(liErrMsg)) {
BomNewSapErrorMsgEntity sapErrorMsgEntity = new BomNewSapErrorMsgEntity();
sapErrorMsgEntity.setTargetRowId(rootBomRowId);
sapErrorMsgEntity.setType(0);
sapErrorMsgEntity.setData(JSON.toJSONString(liErrMsg));
sapErrorMsgEntity.setCreatedName(SessionUtil.getRealName());
sapErrorMsgEntity.setCreatedTime(LocalDateTime.now());
bomNewSapErrorMsgService.saveOrUpdate(sapErrorMsgEntity);
}
}
private T1DTO convert(BomNewEbomChildEntity child, String parentMaterialNo) {
T1DTO t = new T1DTO();
t.setID(RandomUtil.randomNumbers(5));
t.setMATNR(parentMaterialNo);
t.setMENGE(child.getNum().toString());
t.setPOSTP(child.getProjectType());
t.setDATUM(dateYMD);
t.setMEINS(child.getMaterialUnit());
t.setIDNRK(child.getMaterialNo());
if (BomConstant.PROJECT_TYPE_TEMPORARY.equals(child.getProjectType())) {
t.setIDNRK("");
t.setPOTX1(child.getMaterialDesc());
}
return t;
}
private BomNewEbomParentEntity getParent(BomNewEbomChildEntity c) {
BomNewEbomParentEntity p = bomNewEbomParentService.lambdaQuery()
.eq(BomNewEbomParentEntity::getMaterialNo, c.getMaterialNo())
.eq(BomNewEbomParentEntity::getStatus, EBomStatusEnum.PUBLISHED.getValue())
.orderByDesc(BomNewEbomParentEntity::getCurrentVersion)
.last(" limit 1")
.one();
if (Objects.nonNull(p) && p.getUserRootIs() == 0) {
return p;
}
return null;
}
private List<BomNewEbomChildEntity> getChildren(BomNewEbomParentEntity parent) {
return bomNewEbomChildService.lambdaQuery()
.eq(BomNewEbomChildEntity::getParentRowId, parent.getRowId())
.list();
}
private void updateSapState(Long rootBomRowId, SapStatusEnum sapState) {
bomNewEbomParentService.lambdaUpdate().eq(BomNewEbomParentEntity::getRowId, rootBomRowId)
.set(BomNewEbomParentEntity::getSapTime, LocalDateTime.now())
.set(BomNewEbomParentEntity::getSapState, sapState.getValue())
.update();
}
}

View File

@ -1016,6 +1016,7 @@ public class BomNewEbomParentService extends ServiceImpl<BomNewEbomParentMapper,
BomNewEbomParentEntity cp = lambdaQuery()
.eq(BomNewEbomParentEntity::getMaterialNo, c.getMaterialNo())
.eq(BomNewEbomParentEntity::getStatus, EBomStatusEnum.PUBLISHED.getValue())
.eq(BomNewEbomParentEntity::getUserRootIs, 0)
.orderByDesc(BomNewEbomParentEntity::getRowId)
.last(" limit 1")
.one();

View File

@ -28,7 +28,7 @@ import java.util.stream.Collectors;
*/
public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
private final static String TAG = "导入31物料到SAP";
private final static String TAG = "Pbom导入到SAP";
//是否是 一次性订单/配件销售订单
private boolean isForSale = false;
@ -74,7 +74,7 @@ public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
for (BomNewPbomChildEntity cptt : cpsc) {
if (Objects.equals(cptt.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue())) {
BomNewPbomParentEntity cpttp = getParent(cptt);
VUtils.isTure(Objects.isNull(cpttp)).throwMessage("顶层直发包parent丢失");
VUtils.isTure(Objects.isNull(cpttp)).throwMessage(StrUtil.format("顶层直发包({})对应的parent丢失", cptt.getMaterialNo()));
ddpRowId = cpttp.getRowId();
cmap.put(ddpRowId, new ArrayList<>());
LOGGER.debug("{} 直发包 rowId{},编号:{},描述:{}", TAG, ddpRowId, cpttp.getMaterialNo(), cpttp.getMaterialDesc());
@ -130,15 +130,10 @@ public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
t.setPOSTP(child.getProjectType());
t.setDATUM(dateYMD);
t.setMEINS(child.getMaterialUnit());
t.setIDNRK(child.getMaterialNo());
if (BomConstant.PROJECT_TYPE_TEMPORARY.equals(child.getProjectType())) {
t.setIDNRK("");
t.setPOTX1(child.getMaterialDesc());
} else {
t.setIDNRK(child.getMaterialNo());
// BaseMaterialVO bm = materialBaseInfos.stream().filter(m -> m.getMaterialNo().equals(d.getMaterialNo())).findFirst().orElse(null);
// if (!Objects.isNull(bm)) {
// t1.setMEINS(bm.getMaterialUnit());
// }
}
return t;
}
@ -149,7 +144,7 @@ public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
/**
* 构建child数据
* @param cRowId 顶层的直发包rowId
* @param cRowId 顶层的直发包rowId
* @param p 父级节点的parent
* @param c 父级节点的child
* @return
@ -182,7 +177,7 @@ public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
LOGGER.debug("{} 物料提层到顶级发货包且合并 编号:{},父级编号:{},数量:{},合并后总数:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), cct.getMENGE(), it.getMaterialDesc());
}
}
} else {
} else if (!Objects.equals(it.getVirtualPartType(), VirtualPackageTypeEnum.DIRECT_DELIVERY_PACKAGE.getValue())) {
LOGGER.debug("{} 添加物料 编号:{},父级编号:{},数量:{},描述:{}", TAG, it.getMaterialNo(), c.getMaterialNo(), it.getNum().toString(), it.getMaterialDesc());
children.add(convert(it, c.getMaterialNo()));
}
@ -200,11 +195,17 @@ public class BomNewPbomExportToSAPImpl implements IBomNewPbomExportToSAP {
}
private BomNewPbomParentEntity getParent(BomNewPbomChildEntity child) {
return bomNewPbomParentService.lambdaQuery()
BomNewPbomParentEntity p = bomNewPbomParentService.lambdaQuery()
.eq(BomNewPbomParentEntity::getMaterialNo, child.getMaterialNo())
.eq(BomNewPbomParentEntity::getStatus, PBomStatusEnum.FACTORY_CONFIRM.getValue())
.eq(BomNewPbomParentEntity::getFacCode, child.getFacCode())
.orderByDesc(BomNewPbomParentEntity::getCurrentVersion)
.last(" limit 1")
.one();
if (Objects.nonNull(p) && p.getUserRootIs() == 0) {
return p;
}
return null;
}
// private List<BomNewPbomChildEntity> getChildren(List<BomNewPbomParentEntity> parents) {