feat: ebom从excel导入数据
This commit is contained in:
parent
158dc0db42
commit
70dd9d0211
|
|
@ -24,4 +24,6 @@ public interface BomNewEbomChildMapper extends BaseMapper<BomNewEbomChildEntity>
|
|||
void updateEBomMaterialUse();
|
||||
|
||||
List<ChildMaxExceptionStateVO> getChildMaxExceptionState(@Param("bomRowIds") List<Long> bomRowIds);
|
||||
|
||||
Integer shouldSetRootIs(String materialNo);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
package com.nflg.product.bomnew.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import org.ttzero.excel.annotation.ExcelColumn;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author 曹鹏飞
|
||||
* @date 2024/5/8 09:07:23
|
||||
*/
|
||||
@Data
|
||||
public class EbomExcelDTO extends BaseImportExcelDTO {
|
||||
|
||||
/**
|
||||
* 父级物料编号
|
||||
*/
|
||||
@ExcelColumn("抬头物料")
|
||||
private String parentMaterialNo;
|
||||
|
||||
/**
|
||||
* 父级物料描述
|
||||
*/
|
||||
@ExcelColumn("物料描述")
|
||||
private String parentMaterialDesc;
|
||||
|
||||
/**
|
||||
* 项目类别
|
||||
*/
|
||||
@ExcelColumn("项目类别")
|
||||
private String projectType;
|
||||
|
||||
/**
|
||||
* 物料编号
|
||||
*/
|
||||
@ExcelColumn("项目物料")
|
||||
private String materialNo;
|
||||
|
||||
/**
|
||||
* 物料描述
|
||||
*/
|
||||
@ExcelColumn("项目物料描述")
|
||||
private String materialDesc;
|
||||
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
@ExcelColumn("数量")
|
||||
private BigDecimal num;
|
||||
|
||||
/**
|
||||
* 物料单位
|
||||
*/
|
||||
@ExcelColumn("单位")
|
||||
private String unit;
|
||||
|
||||
/**
|
||||
* 排序顺序
|
||||
*/
|
||||
@ExcelColumn("排序顺序")
|
||||
private String orderNum;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
@ExcelColumn("备注")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 图号
|
||||
*/
|
||||
private String drawingNo;
|
||||
}
|
||||
|
|
@ -0,0 +1,286 @@
|
|||
package com.nflg.product.bomnew.service;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.nflg.product.base.core.conmon.util.SessionUtil;
|
||||
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.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 lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.springframework.stereotype.Service;
|
||||
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.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 曹鹏飞
|
||||
* @date 2024/5/8 09:02:12
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class EBomImportService {
|
||||
|
||||
public static final ThreadLocal<Integer> rowNum = new ThreadLocal<>();
|
||||
|
||||
public static final ThreadLocal<List<EbomExcelDTO>> excelContextTL = new ThreadLocal<>();
|
||||
|
||||
@Resource
|
||||
private MaterialMainService materialMainService;
|
||||
|
||||
@Resource
|
||||
private UserRoleService userRoleService;
|
||||
|
||||
@Resource
|
||||
private BomNewEbomParentService bomNewEbomParentService;
|
||||
|
||||
@Resource
|
||||
private BomNewEbomChildService bomNewEbomChildService;
|
||||
|
||||
public List<OperationErrorMsgVO> importBom(MultipartFile file) throws IOException {
|
||||
try {
|
||||
rowNum.set(1);
|
||||
excelContextTL.set(new ArrayList<>());
|
||||
EecExcelUtil.handlerExcel(file.getInputStream(), EbomExcelDTO.class, this::handlerExcelRow);
|
||||
|
||||
List<EbomExcelDTO> datas = excelContextTL.get();
|
||||
|
||||
List<OperationErrorMsgVO> errorMsg = checkExcel(datas);
|
||||
if (!errorMsg.isEmpty()) {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
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()))
|
||||
// .throwMessage("导入的数据已存在,请勿重复导入");
|
||||
|
||||
//save(pcs.getLeft(), pcs.getRight());
|
||||
|
||||
return Collections.emptyList();
|
||||
} finally {
|
||||
excelContextTL.remove();
|
||||
}
|
||||
}
|
||||
|
||||
private void handlerExcelRow(EbomExcelDTO dQbomExcelVO) {
|
||||
rowNum.set(rowNum.get() + 1);
|
||||
log.debug("ebom导入excel,第{}行,处理前:{}", rowNum.get(), JSON.toJSONString(dQbomExcelVO));
|
||||
|
||||
dQbomExcelVO.setRowNum(rowNum.get());
|
||||
|
||||
excelContextTL.get().add(dQbomExcelVO);
|
||||
}
|
||||
|
||||
private List<OperationErrorMsgVO> checkExcel(List<EbomExcelDTO> datas) {
|
||||
List<OperationErrorMsgVO> errorMsg = new ArrayList<>();
|
||||
|
||||
List<Integer> numError = datas.stream().filter(u -> StrUtil.isBlank(u.getParentMaterialNo()))
|
||||
.map(BaseImportExcelDTO::getRowNum)
|
||||
.collect(Collectors.toList());
|
||||
if (!numError.isEmpty()) {
|
||||
errorMsg.addAll(numError.stream().map(n -> OperationErrorMsgVO.create("第" + n + "行", "抬头物料为空"))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
||||
numError = datas.stream().filter(u -> StrUtil.isBlank(u.getMaterialNo()))
|
||||
.map(BaseImportExcelDTO::getRowNum)
|
||||
.collect(Collectors.toList());
|
||||
if (!numError.isEmpty()) {
|
||||
errorMsg.addAll(numError.stream().map(n -> OperationErrorMsgVO.create("第" + n + "行", "项目物料为空"))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
||||
numError = datas.stream().filter(u -> Objects.isNull(u.getNum()) || BigDecimal.ZERO.compareTo(u.getNum()) >= 0)
|
||||
.map(BaseImportExcelDTO::getRowNum)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!numError.isEmpty()) {
|
||||
errorMsg.addAll(numError.stream().map(n -> OperationErrorMsgVO.create("第" + n + "行", "数量不正确"))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
||||
numError = datas.stream().filter(u -> StringUtil.CountForMysql(u.getRemark()) > 200)
|
||||
.map(BaseImportExcelDTO::getRowNum)
|
||||
.collect(Collectors.toList());
|
||||
if (!numError.isEmpty()) {
|
||||
errorMsg.addAll(numError.stream().map(n -> OperationErrorMsgVO.create("第" + n + "行", "备注超出200字"))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
private Pair<List<BomNewEbomParentEntity>, List<BomNewEbomChildEntity>> convertToBom(List<EbomExcelDTO> datas) {
|
||||
List<BomNewEbomParentEntity> parents = new ArrayList<>();
|
||||
List<BomNewEbomChildEntity> children = new ArrayList<>();
|
||||
|
||||
List<String> materialNos = datas.stream().map(EbomExcelDTO::getMaterialNo).collect(Collectors.toList());
|
||||
materialNos.addAll(datas.stream().map(EbomExcelDTO::getParentMaterialNo).collect(Collectors.toList()));
|
||||
materialNos = materialNos.stream().distinct().collect(Collectors.toList());
|
||||
List<BaseMaterialVO> materialBaseInfos = materialMainService.getMaterialBaseInfo(materialNos);
|
||||
|
||||
BomNewEbomParentEntity parent;
|
||||
for (EbomExcelDTO data : datas) {
|
||||
parent = parents.stream().filter(p -> p.getMaterialNo().equals(data.getParentMaterialNo()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (Objects.isNull(parent)) {
|
||||
parents.add(buildRoot(materialBaseInfos, data.getParentMaterialNo(), data.getParentMaterialDesc()));
|
||||
}
|
||||
|
||||
parent = parents.stream().filter(p -> p.getMaterialNo().equals(data.getMaterialNo()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (Objects.isNull(parent)) {
|
||||
parent = buildParent(materialBaseInfos, data);
|
||||
children.add(buildChild(materialBaseInfos, parent, data.getProjectType()));
|
||||
parents.add(parent);
|
||||
}
|
||||
}
|
||||
return Pair.of(parents, children);
|
||||
}
|
||||
|
||||
private BomNewEbomChildEntity buildChild(List<BaseMaterialVO> materialBaseInfos, BomNewEbomParentEntity parent, String projectType) {
|
||||
BomNewEbomChildEntity child = Convert.convert(BomNewEbomChildEntity.class, parent);
|
||||
parent.setOrderNumber("");
|
||||
child.setRowId(IdWorker.getId());
|
||||
child.setParentRowId(parent.getRowId());
|
||||
child.setProjectType(projectType);
|
||||
child.setProjectTypeInputType(ProjectTypeInputTypeEnum.MANUAL_INPUT.getValue());
|
||||
child.setIdentityNo(child.getParentRowId() + "_" + child.getRowId());
|
||||
BaseMaterialVO vo = materialBaseInfos.stream()
|
||||
.filter(m -> m.getMaterialNo().equals(child.getMaterialNo()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (!Objects.isNull(vo)) {
|
||||
child.setMaterialCategoryCode(vo.getMaterialCategoryCode());
|
||||
}
|
||||
return child;
|
||||
}
|
||||
|
||||
private BomNewEbomParentEntity buildParent(List<BaseMaterialVO> materialBaseInfos, EbomExcelDTO data) {
|
||||
BomNewEbomParentEntity parent = new BomNewEbomParentEntity();
|
||||
parent.setRowId(IdWorker.getId());
|
||||
|
||||
BaseMaterialVO vo = materialBaseInfos.stream()
|
||||
.filter(m -> m.getMaterialNo().equals(data.getMaterialNo()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (!Objects.isNull(vo)) {
|
||||
parent.setDrawingNo(vo.getDrawingNo());
|
||||
parent.setUnitWeight(vo.getMaterialWeight());
|
||||
}
|
||||
|
||||
parent.setMaterialNo(data.getMaterialNo());
|
||||
parent.setMaterialDesc(data.getMaterialDesc());
|
||||
parent.setMaterialTexture("");
|
||||
parent.setMaterialUnit(data.getUnit());
|
||||
parent.setNum(data.getNum());
|
||||
parent.setTotalWeight(BomUtil.calculateTotalWeight(parent.getNum(), parent.getUnitWeight()));
|
||||
//parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION);
|
||||
parent.setUserRootIs(0);
|
||||
parent.setRootIs(0);
|
||||
if (userRoleService.technician()) {
|
||||
//工艺人员
|
||||
parent.setStatus(EBomStatusEnum.CHECKED.getValue());
|
||||
parent.setCreatedJob(UserJobEnum.ENGINEER.getValue());
|
||||
parent.setAuditUserName(SessionUtil.getUserCode());
|
||||
parent.setAuditTime(LocalDateTime.now());
|
||||
} else {
|
||||
//设计人员
|
||||
parent.setStatus(EBomStatusEnum.WAIT_CHECK.getValue());
|
||||
parent.setCreatedJob(UserJobEnum.DESIGNER.getValue());
|
||||
parent.setRootIsForWaitReview(1);
|
||||
}
|
||||
parent.setDeviseName(SessionUtil.getRealName());
|
||||
parent.setDeviseUserCode(SessionUtil.getUserCode());
|
||||
parent.setLastVersionIs(1);
|
||||
parent.setEditStatus(EbomEditStatusEnum.HANDLER_CREATED.getValue());
|
||||
parent.setExceptionStatus(EBomExceptionStatusEnum.INIT.getValue());
|
||||
parent.setCreatedBy(SessionUtil.getUserCode());
|
||||
parent.setRemark(data.getRemark());
|
||||
parent.setOrderNumber(data.getOrderNum());
|
||||
parent.setDeptName(SessionUtil.getDepartName());
|
||||
parent.setSource(EBomSourceEnum.FROM_EXCE.getValue());
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建顶级
|
||||
* @param materialBaseInfos 主物料数据
|
||||
* @param materialNo 物料编号
|
||||
* @param materialDesc 物料描述
|
||||
* @return 父级
|
||||
*/
|
||||
private BomNewEbomParentEntity buildRoot(List<BaseMaterialVO> materialBaseInfos, String materialNo, String materialDesc) {
|
||||
BomNewEbomParentEntity parent = new BomNewEbomParentEntity();
|
||||
parent.setRowId(IdWorker.getId());
|
||||
|
||||
BaseMaterialVO vo = materialBaseInfos.stream()
|
||||
.filter(m -> m.getMaterialNo().equals(materialNo))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (!Objects.isNull(vo)) {
|
||||
parent.setDrawingNo(vo.getDrawingNo());
|
||||
parent.setUnitWeight(vo.getMaterialWeight());
|
||||
}
|
||||
|
||||
parent.setMaterialNo(materialNo);
|
||||
parent.setMaterialDesc(materialDesc);
|
||||
parent.setMaterialTexture("");
|
||||
parent.setMaterialUnit("PC");
|
||||
//parent.setUnitWeight(BigDecimal.ZERO);
|
||||
parent.setNum(BigDecimal.ONE);
|
||||
//parent.setTotalWeight(BomUtil.calculateTotalWeight(data.getNum(), data.getUnitWeight()));
|
||||
//parent.setCurrentVersion(OriginalConstant.DEFAULT_BOM_VERSION);
|
||||
parent.setUserRootIs(1);
|
||||
if (userRoleService.technician()) {
|
||||
//工艺人员
|
||||
parent.setStatus(EBomStatusEnum.CHECKED.getValue());
|
||||
parent.setCreatedJob(UserJobEnum.ENGINEER.getValue());
|
||||
parent.setAuditUserName(SessionUtil.getUserCode());
|
||||
parent.setAuditTime(LocalDateTime.now());
|
||||
parent.setRootIs(bomNewEbomChildService.getBaseMapper().shouldSetRootIs(materialNo));
|
||||
} else {
|
||||
//设计人员
|
||||
parent.setRootIs(1);
|
||||
parent.setStatus(EBomStatusEnum.WAIT_CHECK.getValue());
|
||||
parent.setCreatedJob(UserJobEnum.DESIGNER.getValue());
|
||||
parent.setRootIsForWaitReview(1);
|
||||
}
|
||||
parent.setDeviseName(SessionUtil.getRealName());
|
||||
parent.setDeviseUserCode(SessionUtil.getUserCode());
|
||||
parent.setLastVersionIs(1);
|
||||
parent.setEditStatus(EbomEditStatusEnum.HANDLER_CREATED.getValue());
|
||||
parent.setExceptionStatus(EBomExceptionStatusEnum.INIT.getValue());
|
||||
parent.setCreatedBy(SessionUtil.getUserCode());
|
||||
parent.setRemark("");
|
||||
parent.setDeptName(SessionUtil.getDepartName());
|
||||
parent.setSource(EBomSourceEnum.FROM_EXCE.getValue());
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
|
@ -73,4 +73,16 @@
|
|||
</foreach>
|
||||
group by parent_row_id
|
||||
</select>
|
||||
|
||||
<select id="shouldSetRootIs" resultType="java.lang.Integer">
|
||||
SELECT CASE
|
||||
WHEN EXISTS
|
||||
(SELECT *
|
||||
FROM t_bom_new_ebom_parent p
|
||||
INNER JOIN t_bom_new_ebom_child c ON p.row_id = c.parent_row_id
|
||||
AND p.STATUS < 4
|
||||
WHERE c.material_no = #{materialNo}) THEN 1
|
||||
ELSE 0
|
||||
END AS result;
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
|||
Loading…
Reference in New Issue