feat: ebom从excel导入数据
This commit is contained in:
parent
ac19a53988
commit
862a88dbf6
|
|
@ -2,6 +2,7 @@ package com.nflg.product.bomnew.mapper.master;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity;
|
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 com.nflg.product.bomnew.pojo.vo.ChildMaxExceptionStateVO;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
|
@ -26,4 +27,8 @@ public interface BomNewEbomChildMapper extends BaseMapper<BomNewEbomChildEntity>
|
||||||
List<ChildMaxExceptionStateVO> getChildMaxExceptionState(@Param("bomRowIds") List<Long> bomRowIds);
|
List<ChildMaxExceptionStateVO> getChildMaxExceptionState(@Param("bomRowIds") List<Long> bomRowIds);
|
||||||
|
|
||||||
Integer shouldSetRootIs(String materialNo);
|
Integer shouldSetRootIs(String materialNo);
|
||||||
|
|
||||||
|
List<BomNewEbomParentVO> getBom(Long parentRowId, String materialNo);
|
||||||
|
|
||||||
|
void deleteAllChildren(Long rowId);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ package com.nflg.product.bomnew.service;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.nflg.product.bomnew.mapper.master.BomNewEbomChildMapper;
|
import com.nflg.product.bomnew.mapper.master.BomNewEbomChildMapper;
|
||||||
import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity;
|
import com.nflg.product.bomnew.pojo.entity.BomNewEbomChildEntity;
|
||||||
|
import com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* t_bom_new_ebom_child 表服务实现类
|
* t_bom_new_ebom_child 表服务实现类
|
||||||
|
|
@ -16,4 +19,11 @@ import org.springframework.stereotype.Service;
|
||||||
@Service
|
@Service
|
||||||
public class BomNewEbomChildService extends ServiceImpl<BomNewEbomChildMapper, BomNewEbomChildEntity> {
|
public class BomNewEbomChildService extends ServiceImpl<BomNewEbomChildMapper, BomNewEbomChildEntity> {
|
||||||
|
|
||||||
|
public List<BomNewEbomParentVO> getBom(Long parentRowId, String materialNo) {
|
||||||
|
return baseMapper.getBom(parentRowId, materialNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAllChildren(Long parentRowId) {
|
||||||
|
baseMapper.deleteAllChildren(parentRowId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2313,4 +2313,26 @@ public class BomNewEbomParentService extends ServiceImpl<BomNewEbomParentMapper,
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BomNewEbomParentEntity getLatestByMaterialNo(String materialNo) {
|
||||||
|
return this.lambdaQuery()
|
||||||
|
.eq(BomNewEbomParentEntity::getMaterialNo, materialNo)
|
||||||
|
.orderByDesc(BomNewEbomParentEntity::getRowId)
|
||||||
|
.last("limit 1")
|
||||||
|
.one();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BomNewEbomParentEntity> getLatestByMaterialNo(Collection<String> materialNos) {
|
||||||
|
return this.lambdaQuery()
|
||||||
|
.in(BomNewEbomParentEntity::getMaterialNo, materialNos)
|
||||||
|
.eq(BomNewEbomParentEntity::getLastVersionIs, 1)
|
||||||
|
.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastVersionIs0(Set<String> pMaterialNos) {
|
||||||
|
this.lambdaUpdate().in(BomNewEbomParentEntity::getMaterialNo, pMaterialNos)
|
||||||
|
.set(BomNewEbomParentEntity::getLastVersionIs, 0)
|
||||||
|
.set(BomNewEbomParentEntity::getModifyTime, LocalDateTime.now())
|
||||||
|
.update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,36 @@
|
||||||
package com.nflg.product.bomnew.service;
|
package com.nflg.product.bomnew.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
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.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;
|
||||||
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;
|
||||||
import com.nflg.product.bomnew.pojo.vo.BaseMaterialVO;
|
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.pojo.vo.OperationErrorMsgVO;
|
||||||
import com.nflg.product.bomnew.util.BomUtil;
|
import com.nflg.product.bomnew.util.*;
|
||||||
import com.nflg.product.bomnew.util.EecExcelUtil;
|
|
||||||
import com.nflg.product.bomnew.util.StringUtil;
|
|
||||||
import com.nflg.product.bomnew.util.VUtils;
|
|
||||||
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.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -71,10 +73,10 @@ 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)
|
VUtils.isTure(pcs.getLeft().stream().filter(p -> p.getRootIs() == 1).count() > 1)
|
||||||
.throwMessage("文件中存在多个根节点");
|
.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());
|
||||||
|
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
} finally {
|
} finally {
|
||||||
|
|
@ -82,6 +84,122 @@ public class EBomImportService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
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());
|
||||||
|
List<BomNewEbomParentEntity> 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<BomNewEbomParentEntity> parents,
|
||||||
|
List<BomNewEbomChildEntity> 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<BomNewEbomChildEntity> cc = children.stream()
|
||||||
|
.filter(c -> c.getParentRowId().equals(parent.getRowId()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
List<BomNewEbomChildEntity> 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<BomNewEbomParentEntity> parents,
|
||||||
|
List<BomNewEbomChildEntity> children) throws JsonProcessingException {
|
||||||
|
log.debug("checkInconsistentData,entity:" + JsonUtil.toJson(child));
|
||||||
|
List<BomNewEbomParentVO> 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<BomNewEbomChildEntity> cc = children.stream()
|
||||||
|
.filter(c -> c.getParentRowId().equals(cp.getRowId()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
List<BomNewEbomChildEntity> oc = bomNewEbomChildService.lambdaQuery()
|
||||||
|
.eq(BomNewEbomChildEntity::getParentRowId, oldChild.getBomRowId())
|
||||||
|
.list();
|
||||||
|
Set<String> 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) {
|
private void handlerExcelRow(EbomExcelDTO dQbomExcelVO) {
|
||||||
rowNum.set(rowNum.get() + 1);
|
rowNum.set(rowNum.get() + 1);
|
||||||
log.debug("ebom导入excel,第{}行,处理前:{}", rowNum.get(), JSON.toJSONString(dQbomExcelVO));
|
log.debug("ebom导入excel,第{}行,处理前:{}", rowNum.get(), JSON.toJSONString(dQbomExcelVO));
|
||||||
|
|
|
||||||
|
|
@ -86,4 +86,21 @@
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END AS result;
|
END AS result;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getBom" resultType="com.nflg.product.bomnew.pojo.vo.BomNewEbomParentVO">
|
||||||
|
SELECT c.*, p.row_id AS bomRowId
|
||||||
|
FROM t_bom_new_ebom_child c
|
||||||
|
LEFT JOIN t_bom_new_ebom_parent p ON p.material_no = c.material_no
|
||||||
|
WHERE c.parent_row_id = #{parentRowId}
|
||||||
|
AND c.material_no = #{materialNo}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<delete id="deleteAllChildren">
|
||||||
|
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};
|
||||||
|
</delete>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue