feat(ebom): 修复从excel导入数据的bug

This commit is contained in:
曹鹏飞 2024-05-10 11:21:22 +08:00
parent 95cec05802
commit d92ecb727f
4 changed files with 57 additions and 35 deletions

View File

@ -9,7 +9,6 @@ import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.nflg.product.base.core.conmon.util.SessionUtil; 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.constant.*;
import com.nflg.product.bomnew.pojo.dto.BaseImportExcelDTO; import com.nflg.product.bomnew.pojo.dto.BaseImportExcelDTO;
import com.nflg.product.bomnew.pojo.dto.EbomExcelDTO; import com.nflg.product.bomnew.pojo.dto.EbomExcelDTO;
@ -20,7 +19,6 @@ import com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO;
import com.nflg.product.bomnew.pojo.vo.OperationErrorMsgVO; import com.nflg.product.bomnew.pojo.vo.OperationErrorMsgVO;
import com.nflg.product.bomnew.util.*; import com.nflg.product.bomnew.util.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import nflg.product.common.constant.STATE;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -71,13 +69,13 @@ public class EBomImportService {
} }
Pair<List<BomNewEbomParentEntity>, List<BomNewEbomChildEntity>> pcs = convertToBom(datas); Pair<List<BomNewEbomParentEntity>, List<BomNewEbomChildEntity>> 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())) VUtils.isTure(checkInconsistentData(pcs.getLeft().get(0), pcs.getLeft(), pcs.getRight()))
.throwMessage("导入的数据已存在,请勿重复导入"); .throwMessage("导入的数据已存在,请勿重复导入");
save(pcs.getLeft(), pcs.getRight()); save(pcs.getLeft(), pcs.getRight());
bomNewEbomParentService.resetAllBomExist();
return Collections.emptyList(); return Collections.emptyList();
} finally { } finally {
excelContextTL.remove(); excelContextTL.remove();
@ -86,10 +84,6 @@ public class EBomImportService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void save(List<BomNewEbomParentEntity> parents, List<BomNewEbomChildEntity> children) { public void save(List<BomNewEbomParentEntity> parents, List<BomNewEbomChildEntity> children) {
BomNewEbomParentEntity root = parents.stream()
.filter(p -> p.getRootIs() == 1)
.findFirst()
.orElseThrow(() -> new NflgBusinessException(STATE.BusinessError, "根节点不存在"));
Set<String> pMaterialNos = parents.stream().map(BomNewEbomParentEntity::getMaterialNo).collect(Collectors.toSet()); Set<String> pMaterialNos = parents.stream().map(BomNewEbomParentEntity::getMaterialNo).collect(Collectors.toSet());
List<BomNewEbomParentEntity> oldParents = bomNewEbomParentService.getLatestByMaterialNo(pMaterialNos); List<BomNewEbomParentEntity> oldParents = bomNewEbomParentService.getLatestByMaterialNo(pMaterialNos);
parents.forEach(p -> { parents.forEach(p -> {
@ -267,7 +261,7 @@ public class EBomImportService {
.findFirst() .findFirst()
.orElse(null); .orElse(null);
if (Objects.isNull(parent)) { if (Objects.isNull(parent)) {
parents.add(buildRoot(materialBaseInfos, data.getParentMaterialNo(), data.getParentMaterialDesc())); parents.add(buildTop(materialBaseInfos, data.getParentMaterialNo(), data.getParentMaterialDesc()));
} }
parent = parents.stream().filter(p -> p.getMaterialNo().equals(data.getMaterialNo())) parent = parents.stream().filter(p -> p.getMaterialNo().equals(data.getMaterialNo()))
@ -275,18 +269,33 @@ public class EBomImportService {
.orElse(null); .orElse(null);
if (Objects.isNull(parent)) { if (Objects.isNull(parent)) {
parent = buildParent(materialBaseInfos, data); parent = buildParent(materialBaseInfos, data);
children.add(buildChild(materialBaseInfos, parent, data.getProjectType())); buildChild(children, materialBaseInfos, parent, data.getProjectType()
, parents.stream().filter(p -> p.getMaterialNo().equals(data.getParentMaterialNo())).findFirst().get());
parents.add(parent); parents.add(parent);
} }
} }
return Pair.of(parents, children); return Pair.of(parents, children);
} }
private BomNewEbomChildEntity buildChild(List<BaseMaterialVO> materialBaseInfos, BomNewEbomParentEntity parent, String projectType) { private void buildChild(List<BomNewEbomChildEntity> children, List<BaseMaterialVO> materialBaseInfos, BomNewEbomParentEntity parent, String projectType, BomNewEbomParentEntity p) {
BomNewEbomChildEntity child = Convert.convert(BomNewEbomChildEntity.class, parent); BomNewEbomChildEntity child = Convert.convert(BomNewEbomChildEntity.class, parent);
parent.setOrderNumber("");
child.setRowId(IdWorker.getId()); child.setRowId(IdWorker.getId());
child.setParentRowId(parent.getRowId()); child.setParentRowId(p.getRowId());
parent.setOrderNumber("");
List<BomNewEbomChildEntity> pcs = children.stream()
.filter(c -> Objects.equals(c.getParentRowId(), p.getRowId()))
.collect(Collectors.toList());
if (CollUtil.isEmpty(pcs)) {
child.setOrderNumber("001");
} else {
String maxOrderNum = pcs.get(pcs.size() - 1).getOrderNumber();
child.setOrderNumber(OrderNoUtil.next(maxOrderNum));
}
child.setDrawingNo(parent.getDrawingNo());
child.setMaterialDesc(parent.getMaterialDesc());
child.setUnitWeight(parent.getUnitWeight());
child.setNum(parent.getNum());
child.setTotalWeight(parent.getTotalWeight());
child.setProjectType(projectType); child.setProjectType(projectType);
child.setProjectTypeInputType(ProjectTypeInputTypeEnum.MANUAL_INPUT.getValue()); child.setProjectTypeInputType(ProjectTypeInputTypeEnum.MANUAL_INPUT.getValue());
child.setIdentityNo(child.getParentRowId() + "_" + child.getRowId()); child.setIdentityNo(child.getParentRowId() + "_" + child.getRowId());
@ -297,13 +306,15 @@ public class EBomImportService {
if (!Objects.isNull(vo)) { if (!Objects.isNull(vo)) {
child.setMaterialCategoryCode(vo.getMaterialCategoryCode()); child.setMaterialCategoryCode(vo.getMaterialCategoryCode());
} }
return child; children.add(child);
} }
private BomNewEbomParentEntity buildParent(List<BaseMaterialVO> materialBaseInfos, EbomExcelDTO data) { private BomNewEbomParentEntity buildParent(List<BaseMaterialVO> materialBaseInfos, EbomExcelDTO data) {
BomNewEbomParentEntity parent = new BomNewEbomParentEntity(); BomNewEbomParentEntity parent = new BomNewEbomParentEntity();
parent.setRowId(IdWorker.getId()); parent.setRowId(IdWorker.getId());
parent.setMaterialNo(data.getMaterialNo());
parent.setMaterialDesc(data.getMaterialDesc());
BaseMaterialVO vo = materialBaseInfos.stream() BaseMaterialVO vo = materialBaseInfos.stream()
.filter(m -> m.getMaterialNo().equals(data.getMaterialNo())) .filter(m -> m.getMaterialNo().equals(data.getMaterialNo()))
.findFirst() .findFirst()
@ -311,15 +322,16 @@ public class EBomImportService {
if (!Objects.isNull(vo)) { if (!Objects.isNull(vo)) {
parent.setDrawingNo(vo.getDrawingNo()); parent.setDrawingNo(vo.getDrawingNo());
parent.setUnitWeight(vo.getMaterialWeight()); parent.setUnitWeight(vo.getMaterialWeight());
parent.setMaterialName(vo.getMaterialName());
parent.setMaterialTexture(vo.getMaterialTexture());
if (StrUtil.isBlank(data.getMaterialDesc())) {
parent.setMaterialDesc(vo.getMaterialDesc());
}
} }
parent.setMaterialNo(data.getMaterialNo());
parent.setMaterialDesc(data.getMaterialDesc());
parent.setMaterialTexture("");
parent.setMaterialUnit(data.getUnit()); parent.setMaterialUnit(data.getUnit());
parent.setNum(data.getNum()); parent.setNum(data.getNum());
parent.setTotalWeight(BomUtil.calculateTotalWeight(parent.getNum(), parent.getUnitWeight())); parent.setTotalWeight(BomUtil.calculateTotalWeight(parent.getNum(), parent.getUnitWeight()));
//parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION); parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION);
parent.setUserRootIs(0); parent.setUserRootIs(0);
parent.setRootIs(0); parent.setRootIs(0);
if (userRoleService.technician()) { if (userRoleService.technician()) {
@ -344,6 +356,7 @@ public class EBomImportService {
parent.setOrderNumber(data.getOrderNum()); parent.setOrderNumber(data.getOrderNum());
parent.setDeptName(SessionUtil.getDepartName()); parent.setDeptName(SessionUtil.getDepartName());
parent.setSource(EBomSourceEnum.FROM_EXCE.getValue()); parent.setSource(EBomSourceEnum.FROM_EXCE.getValue());
parent.setSourceRowId(0L);
return parent; return parent;
} }
@ -354,10 +367,12 @@ public class EBomImportService {
* @param materialDesc 物料描述 * @param materialDesc 物料描述
* @return 父级 * @return 父级
*/ */
private BomNewEbomParentEntity buildRoot(List<BaseMaterialVO> materialBaseInfos, String materialNo, String materialDesc) { private BomNewEbomParentEntity buildTop(List<BaseMaterialVO> materialBaseInfos, String materialNo, String materialDesc) {
BomNewEbomParentEntity parent = new BomNewEbomParentEntity(); BomNewEbomParentEntity parent = new BomNewEbomParentEntity();
parent.setRowId(IdWorker.getId()); parent.setRowId(IdWorker.getId());
parent.setMaterialNo(materialNo);
parent.setMaterialDesc(materialDesc);
BaseMaterialVO vo = materialBaseInfos.stream() BaseMaterialVO vo = materialBaseInfos.stream()
.filter(m -> m.getMaterialNo().equals(materialNo)) .filter(m -> m.getMaterialNo().equals(materialNo))
.findFirst() .findFirst()
@ -365,27 +380,33 @@ public class EBomImportService {
if (!Objects.isNull(vo)) { if (!Objects.isNull(vo)) {
parent.setDrawingNo(vo.getDrawingNo()); parent.setDrawingNo(vo.getDrawingNo());
parent.setUnitWeight(vo.getMaterialWeight()); parent.setUnitWeight(vo.getMaterialWeight());
parent.setMaterialName(vo.getMaterialName());
parent.setMaterialTexture(vo.getMaterialTexture());
if (StrUtil.isBlank(materialDesc)) {
parent.setMaterialDesc(vo.getMaterialDesc());
}
} }
parent.setMaterialNo(materialNo);
parent.setMaterialDesc(materialDesc);
parent.setMaterialTexture("");
parent.setMaterialUnit("PC"); parent.setMaterialUnit("PC");
//parent.setUnitWeight(BigDecimal.ZERO); //parent.setUnitWeight(BigDecimal.ZERO);
parent.setNum(BigDecimal.ONE); parent.setNum(BigDecimal.ONE);
//parent.setTotalWeight(BomUtil.calculateTotalWeight(data.getNum(), data.getUnitWeight())); //parent.setTotalWeight(BomUtil.calculateTotalWeight(data.getNum(), data.getUnitWeight()));
//parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION); parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION);
// List<Long> cps = bomNewEbomChildService.lambdaQuery().eq(BomNewEbomChildEntity::getMaterialNo, materialNo)
// .list()
// .stream()
// .map(BomNewEbomChildEntity::getParentRowId)
// .collect(Collectors.toList());
parent.setUserRootIs(1); parent.setUserRootIs(1);
parent.setRootIs(bomNewEbomChildService.getBaseMapper().shouldSetRootIs(materialNo));
if (userRoleService.technician()) { if (userRoleService.technician()) {
//工艺人员 //工艺人员
parent.setStatus(EBomStatusEnum.CHECKED.getValue()); parent.setStatus(EBomStatusEnum.CHECKED.getValue());
parent.setCreatedJob(UserJobEnum.ENGINEER.getValue()); parent.setCreatedJob(UserJobEnum.ENGINEER.getValue());
parent.setAuditUserName(SessionUtil.getUserCode()); parent.setAuditUserName(SessionUtil.getUserCode());
parent.setAuditTime(LocalDateTime.now()); parent.setAuditTime(LocalDateTime.now());
parent.setRootIs(bomNewEbomChildService.getBaseMapper().shouldSetRootIs(materialNo)); parent.setRootIsForWaitReview(0);
} else { } else {
//设计人员 //设计人员
parent.setRootIs(1);
parent.setStatus(EBomStatusEnum.WAIT_CHECK.getValue()); parent.setStatus(EBomStatusEnum.WAIT_CHECK.getValue());
parent.setCreatedJob(UserJobEnum.DESIGNER.getValue()); parent.setCreatedJob(UserJobEnum.DESIGNER.getValue());
parent.setRootIsForWaitReview(1); parent.setRootIsForWaitReview(1);
@ -399,6 +420,7 @@ public class EBomImportService {
parent.setRemark(""); parent.setRemark("");
parent.setDeptName(SessionUtil.getDepartName()); parent.setDeptName(SessionUtil.getDepartName());
parent.setSource(EBomSourceEnum.FROM_EXCE.getValue()); parent.setSource(EBomSourceEnum.FROM_EXCE.getValue());
parent.setSourceRowId(0L);
return parent; return parent;
} }
} }

View File

@ -6,10 +6,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.nflg.product.base.core.conmon.util.SessionUtil; import com.nflg.product.base.core.conmon.util.SessionUtil;
import com.nflg.product.bomnew.constant.EBomSourceEnum; import com.nflg.product.bomnew.constant.*;
import com.nflg.product.bomnew.constant.EBomStatusEnum;
import com.nflg.product.bomnew.constant.ProjectTypeInputTypeEnum;
import com.nflg.product.bomnew.constant.VirtualPackageTypeEnum;
import com.nflg.product.bomnew.pojo.dto.AddVirtrualMaterialDTO; import com.nflg.product.bomnew.pojo.dto.AddVirtrualMaterialDTO;
import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity; import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity;
import com.nflg.product.bomnew.pojo.entity.BomNewEbomParentEntity; import com.nflg.product.bomnew.pojo.entity.BomNewEbomParentEntity;
@ -20,8 +17,6 @@ import com.nflg.product.bomnew.service.MaterialMainService;
import com.nflg.product.bomnew.service.MaterialService; import com.nflg.product.bomnew.service.MaterialService;
import com.nflg.product.bomnew.util.VersionUtil; import com.nflg.product.bomnew.util.VersionUtil;
import lombok.Getter; import lombok.Getter;
import org.ttzero.excel.reader.Col;
import org.bouncycastle.LICENSE;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;

View File

@ -15,4 +15,9 @@ public class OrderNoUtil {
return StrUtil.format("{}",no); return StrUtil.format("{}",no);
} }
public static String next(String orderNo) {
int no = Integer.parseInt(orderNo) + 1;
return orderNo2Str(no);
}
} }

View File

@ -82,8 +82,8 @@
FROM t_bom_new_ebom_parent p FROM t_bom_new_ebom_parent p
INNER JOIN t_bom_new_ebom_child c ON p.row_id = c.parent_row_id INNER JOIN t_bom_new_ebom_child c ON p.row_id = c.parent_row_id
AND p.STATUS &lt; 4 AND p.STATUS &lt; 4
WHERE c.material_no = #{materialNo}) THEN 1 WHERE c.material_no = #{materialNo}) THEN 0
ELSE 0 ELSE 1
END AS result; END AS result;
</select> </select>