From 862a88dbf69fcc933f3d6d16201a19c0b281d43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E9=B9=8F=E9=A3=9E?= Date: Wed, 8 May 2024 17:30:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ebom=E4=BB=8Eexcel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/master/BomNewEbomChildMapper.java | 5 + .../service/BomNewEbomChildService.java | 10 ++ .../service/BomNewEbomParentService.java | 22 +++ .../bomnew/service/EBomImportService.java | 140 ++++++++++++++++-- .../mapper/master/BomNewEbomChildMapper.xml | 17 +++ 5 files changed, 183 insertions(+), 11 deletions(-) diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/mapper/master/BomNewEbomChildMapper.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/mapper/master/BomNewEbomChildMapper.java index fca209eb..96a104a1 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/mapper/master/BomNewEbomChildMapper.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/mapper/master/BomNewEbomChildMapper.java @@ -2,6 +2,7 @@ package com.nflg.product.bomnew.mapper.master; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity; +import com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO; import com.nflg.product.bomnew.pojo.vo.ChildMaxExceptionStateVO; import org.apache.ibatis.annotations.Param; @@ -26,4 +27,8 @@ public interface BomNewEbomChildMapper extends BaseMapper List getChildMaxExceptionState(@Param("bomRowIds") List bomRowIds); Integer shouldSetRootIs(String materialNo); + + List getBom(Long parentRowId, String materialNo); + + void deleteAllChildren(Long rowId); } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomChildService.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomChildService.java index 4e6741ee..5049338a 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomChildService.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomChildService.java @@ -3,8 +3,11 @@ package com.nflg.product.bomnew.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.nflg.product.bomnew.mapper.master.BomNewEbomChildMapper; import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity; +import com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO; import org.springframework.stereotype.Service; +import java.util.List; + /** * t_bom_new_ebom_child 表服务实现类 @@ -16,4 +19,11 @@ import org.springframework.stereotype.Service; @Service public class BomNewEbomChildService extends ServiceImpl { + public List getBom(Long parentRowId, String materialNo) { + return baseMapper.getBom(parentRowId, materialNo); + } + + public void deleteAllChildren(Long parentRowId) { + baseMapper.deleteAllChildren(parentRowId); + } } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomParentService.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomParentService.java index cfca73b8..ebfd6fa6 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomParentService.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/BomNewEbomParentService.java @@ -2313,4 +2313,26 @@ public class BomNewEbomParentService extends ServiceImpl getLatestByMaterialNo(Collection materialNos) { + return this.lambdaQuery() + .in(BomNewEbomParentEntity::getMaterialNo, materialNos) + .eq(BomNewEbomParentEntity::getLastVersionIs, 1) + .list(); + } + + public void setLastVersionIs0(Set pMaterialNos) { + this.lambdaUpdate().in(BomNewEbomParentEntity::getMaterialNo, pMaterialNos) + .set(BomNewEbomParentEntity::getLastVersionIs, 0) + .set(BomNewEbomParentEntity::getModifyTime, LocalDateTime.now()) + .update(); + } } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/EBomImportService.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/EBomImportService.java index 7d56ecaf..2ef5c6c0 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/EBomImportService.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/EBomImportService.java @@ -1,34 +1,36 @@ package com.nflg.product.bomnew.service; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.common.collect.Sets; import com.nflg.product.base.core.conmon.util.SessionUtil; +import com.nflg.product.base.core.exception.NflgBusinessException; import com.nflg.product.bomnew.constant.*; import com.nflg.product.bomnew.pojo.dto.BaseImportExcelDTO; import com.nflg.product.bomnew.pojo.dto.EbomExcelDTO; import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity; import com.nflg.product.bomnew.pojo.entity.BomNewEbomParentEntity; import com.nflg.product.bomnew.pojo.vo.BaseMaterialVO; +import com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO; import com.nflg.product.bomnew.pojo.vo.OperationErrorMsgVO; -import com.nflg.product.bomnew.util.BomUtil; -import com.nflg.product.bomnew.util.EecExcelUtil; -import com.nflg.product.bomnew.util.StringUtil; -import com.nflg.product.bomnew.util.VUtils; +import com.nflg.product.bomnew.util.*; import lombok.extern.slf4j.Slf4j; +import nflg.product.common.constant.STATE; import org.apache.commons.lang3.tuple.Pair; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; /** @@ -71,10 +73,10 @@ public class EBomImportService { Pair, List> pcs = convertToBom(datas); VUtils.isTure(pcs.getLeft().stream().filter(p -> p.getRootIs() == 1).count() > 1) .throwMessage("文件中存在多个根节点"); - //VUtils.isTure(checkInconsistentData(pcs.getLeft().get(0), pcs.getLeft(), pcs.getRight())) - // .throwMessage("导入的数据已存在,请勿重复导入"); + VUtils.isTure(checkInconsistentData(pcs.getLeft().get(0), pcs.getLeft(), pcs.getRight())) + .throwMessage("导入的数据已存在,请勿重复导入"); - //save(pcs.getLeft(), pcs.getRight()); + save(pcs.getLeft(), pcs.getRight()); return Collections.emptyList(); } finally { @@ -82,6 +84,122 @@ public class EBomImportService { } } + @Transactional(rollbackFor = Exception.class) + public void save(List parents, List children) { + BomNewEbomParentEntity root = parents.stream() + .filter(p -> p.getRootIs() == 1) + .findFirst() + .orElseThrow(() -> new NflgBusinessException(STATE.BusinessError, "根节点不存在")); + Set pMaterialNos = parents.stream().map(BomNewEbomParentEntity::getMaterialNo).collect(Collectors.toSet()); + List oldParents = bomNewEbomParentService.getLatestByMaterialNo(pMaterialNos); + parents.forEach(p -> { + BomNewEbomParentEntity oldParent = oldParents.stream() + .filter(op -> op.getMaterialNo().equals(p.getMaterialNo())) + .findFirst() + .orElse(null); + if (!Objects.isNull(oldParent) && !Objects.equals(oldParent.getStatus(), EBomStatusEnum.PUBLISHED.getValue())) { + bomNewEbomParentService.getBaseMapper().deleteById(oldParent.getRowId()); + bomNewEbomChildService.deleteAllChildren(oldParent.getRowId()); + } + }); + bomNewEbomParentService.setLastVersionIs0(pMaterialNos); + bomNewEbomParentService.saveBatch(parents); + bomNewEbomChildService.saveBatch(children); + } + + private boolean checkInconsistentData(BomNewEbomParentEntity parent, List parents, + List children) throws JsonProcessingException { + log.debug("checkInconsistentData,entity:" + JsonUtil.toJson(parent)); + BomNewEbomParentEntity oldParent = bomNewEbomParentService.getLatestByMaterialNo(parent.getMaterialNo()); + boolean isSame = true; + if (Objects.isNull(oldParent)) { + return false; + } + isSame = Objects.equals(parent.getMaterialUnit(), oldParent.getMaterialUnit()) + && NumberUtil.equals(parent.getNum(), oldParent.getNum()) + && Objects.equals(parent.getMaterialTexture(), oldParent.getMaterialTexture()); + + if (isSame) { + List cc = children.stream() + .filter(c -> c.getParentRowId().equals(parent.getRowId())) + .collect(Collectors.toList()); + List oc = bomNewEbomChildService.lambdaQuery() + .eq(BomNewEbomChildEntity::getParentRowId, oldParent.getRowId()) + .list(); + + isSame = cc.size() == oc.size() + && Sets.difference(cc.stream().map(BomNewEbomChildEntity::getMaterialNo).collect(Collectors.toSet()) + , oc.stream().map(BomNewEbomChildEntity::getMaterialNo).collect(Collectors.toSet())).isEmpty(); + for (BomNewEbomChildEntity ccc : cc) { + if (!checkInconsistentData(parent.getRowId(), ccc, parents, children)) { + isSame = false; + } + } + } + if (!isSame) { + if (Objects.equals(oldParent.getStatus(), EBomStatusEnum.PUBLISHED.getValue())) { + parent.setCurrentVersion(VersionUtil.getNextVersion(oldParent.getCurrentVersion())); + } else { + VUtils.isTure(!Objects.equals(oldParent.getCreatedBy(), SessionUtil.getUserCode())) + .throwMessage(StrUtil.format("{}正由{}编辑中,请联系Ta处理", oldParent.getMaterialNo(), oldParent.getCreatedBy())); + } + } else { + parents.remove(parent); + children.removeIf(c -> c.getParentRowId().equals(parent.getRowId())); + } + return isSame; + } + + private boolean checkInconsistentData(Long parentRowId, BomNewEbomChildEntity child, List parents, + List children) throws JsonProcessingException { + log.debug("checkInconsistentData,entity:" + JsonUtil.toJson(child)); + List oldChildren = bomNewEbomChildService.getBom(parentRowId, child.getMaterialNo()); + boolean isSame = true; + if (CollUtil.isEmpty(oldChildren)) return false; + BomNewEbomParentVO oldChild; + if (oldChildren.size() == 1) { + oldChild = oldChildren.get(0); + } else { + oldChild = oldChildren.stream().filter(c -> Objects.equals(c.getOrderNumber(), child.getOrderNumber())).findFirst().orElse(null); + if (Objects.isNull(oldChild)) { + return false; + } + } + isSame = Objects.equals(child.getMaterialUnit(), oldChild.getMaterialUnit()) + && NumberUtil.equals(child.getNum(), oldChild.getNum()) + && Objects.equals(child.getMaterialTexture(), oldChild.getMaterialTexture()); + + if (isSame) { + BomNewEbomParentEntity cp = parents.stream().filter(p -> p.getMaterialNo().equals(child.getMaterialNo())).findFirst().orElse(null); + if (!Objects.isNull(cp)) { + List cc = children.stream() + .filter(c -> c.getParentRowId().equals(cp.getRowId())) + .collect(Collectors.toList()); + List oc = bomNewEbomChildService.lambdaQuery() + .eq(BomNewEbomChildEntity::getParentRowId, oldChild.getBomRowId()) + .list(); + Set ccm = cc.stream().map(BomNewEbomChildEntity::getMaterialNo).collect(Collectors.toSet()); + + isSame = cc.size() == oc.size() + && Sets.difference(ccm, oc.stream().map(BomNewEbomChildEntity::getMaterialNo).collect(Collectors.toSet())).isEmpty(); + //VUtils.isTure(isSame).throwMessage("导入的数据已存在,请勿重复导入"); + + for (BomNewEbomChildEntity ccc : cc) { + if (!checkInconsistentData(cp.getRowId(), ccc, parents, children)) { + isSame = false; + } + } + } + } + if (!isSame) { + if (!Objects.equals(oldChild.getStatus(), EBomStatusEnum.PUBLISHED.getValue())) { + VUtils.isTure(!Objects.equals(oldChild.getCreatedBy(), SessionUtil.getUserCode())) + .throwMessage(StrUtil.format("{}正由{}编辑中,请联系Ta处理", oldChild.getMaterialNo(), oldChild.getCreatedBy())); + } + } + return isSame; + } + private void handlerExcelRow(EbomExcelDTO dQbomExcelVO) { rowNum.set(rowNum.get() + 1); log.debug("ebom导入excel,第{}行,处理前:{}", rowNum.get(), JSON.toJSONString(dQbomExcelVO)); diff --git a/nflg_project_dev/nflg-bom-new/src/main/resources/mapper/master/BomNewEbomChildMapper.xml b/nflg_project_dev/nflg-bom-new/src/main/resources/mapper/master/BomNewEbomChildMapper.xml index 18caca7e..70c3f343 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/resources/mapper/master/BomNewEbomChildMapper.xml +++ b/nflg_project_dev/nflg-bom-new/src/main/resources/mapper/master/BomNewEbomChildMapper.xml @@ -86,4 +86,21 @@ ELSE 0 END AS result; + + + + + DELETE + FROM t_bom_new_ebom_child + WHERE parent_row_id = #{parentRowId}; + UPDATE t_bom_new_ebom_parent + SET bom_exist=0 + WHERE row_id = #{parentRowId}; +