| | |
| | | package com.ruoyi.technology.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.ruoyi.basic.service.IProductService; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.production.dto.BomImportDto; |
| | | import com.ruoyi.production.dto.ProductStructureDto; |
| | | import com.ruoyi.technology.bean.dto.BomImportDto; |
| | | import com.ruoyi.technology.bean.dto.TechnologyBomDto; |
| | | import com.ruoyi.technology.bean.dto.TechnologyBomStructureDto; |
| | | import com.ruoyi.technology.bean.vo.TechnologyBomStructureVo; |
| | | import com.ruoyi.technology.bean.vo.TechnologyBomVo; |
| | | import com.ruoyi.technology.mapper.TechnologyBomMapper; |
| | | import com.ruoyi.technology.mapper.TechnologyBomStructureMapper; |
| | | import com.ruoyi.technology.mapper.TechnologyRoutingMapper; |
| | | import com.ruoyi.technology.pojo.TechnologyBom; |
| | | import com.ruoyi.technology.pojo.TechnologyBomStructure; |
| | | import com.ruoyi.technology.pojo.TechnologyOperation; |
| | | import com.ruoyi.technology.pojo.TechnologyRouting; |
| | | import com.ruoyi.technology.service.TechnologyBomService; |
| | | import com.ruoyi.technology.service.TechnologyBomStructureService; |
| | | import com.ruoyi.technology.service.TechnologyOperationService; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | private final TechnologyBomStructureService technologyBomStructureService; |
| | | private final TechnologyRoutingMapper technologyRoutingMapper; |
| | | private final IProductService productService; |
| | | private final TechnologyBomStructureMapper technologyBomStructureMapper; |
| | | private final TechnologyOperationService technologyOperationService; |
| | | |
| | | /** |
| | | * 分页查询BOM列表。 |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R update(TechnologyBom technologyBom) { |
| | | if (technologyBom.getId() == null) { |
| | | throw new ServiceException("BOM id is required"); |
| | | throw new ServiceException("BOM ID不能为空"); |
| | | } |
| | | validateProductModel(technologyBom.getProductModelId()); |
| | | TechnologyBom oldBom = technologyBomMapper.selectById(technologyBom.getId()); |
| | | if (oldBom == null) { |
| | | throw new ServiceException("BOM not found"); |
| | | throw new ServiceException("BOM不存在"); |
| | | } |
| | | if (oldBom.getProductModelId() != null && !oldBom.getProductModelId().equals(technologyBom.getProductModelId())) { |
| | | technologyRoutingMapper.updateProductModelByBomId(technologyBom.getProductModelId(), technologyBom.getId().longValue()); |
| | |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean batchDelete(List<Integer> ids) { |
| | | public boolean batchDelete(List<Long> ids) { |
| | | if (ids == null || ids.isEmpty()) { |
| | | throw new ServiceException("Select at least one BOM"); |
| | | throw new ServiceException("请至少选择一个BOM"); |
| | | } |
| | | List<TechnologyRouting> list = technologyRoutingMapper.selectList(Wrappers.<TechnologyRouting>lambdaQuery() |
| | | .in(TechnologyRouting::getBomId, ids)); |
| | | if (!list.isEmpty()) { |
| | | throw new ServiceException("BOM is referenced by routing"); |
| | | throw new ServiceException("BOM已被工艺路线引用,不能删除"); |
| | | } |
| | | technologyBomStructureService.remove(Wrappers.<TechnologyBomStructure>lambdaQuery() |
| | | .in(TechnologyBomStructure::getBomId, ids)); |
| | |
| | | */ |
| | | private void validateProductModel(Long productModelId) { |
| | | if (productModelId == null) { |
| | | throw new ServiceException("Product model is required"); |
| | | throw new ServiceException("产品规格ID不能为空"); |
| | | } |
| | | ProductModel productModel = productModelService.getById(productModelId); |
| | | if (productModel == null) { |
| | | throw new ServiceException("Product model not found"); |
| | | throw new ServiceException("产品规格不存在"); |
| | | } |
| | | } |
| | | |
| | |
| | | dto.setParentSpec(clean(dto.getParentSpec())); |
| | | dto.setChildName(clean(dto.getChildName())); |
| | | dto.setChildSpec(clean(dto.getChildSpec())); |
| | | dto.setProcess(clean(dto.getProcess())); |
| | | }); |
| | | handleProcess(list); |
| | | // Map<String, Long> processMap = productProcessService.list().stream() |
| | | // .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1)); |
| | | |
| | | // 创建 BOM 数据 |
| | | BomImportDto first = list.get(0); |
| | | ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec()); |
| | | TechnologyBom bom = new TechnologyBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setVersion("1.0"); |
| | | technologyBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | technologyBomMapper.updateById(bom); |
| | | // 工序名称 -> 工序ID(technology_operation 表),导入时绑定到 BOM 结构节点 |
| | | Map<String, Long> processMap = technologyOperationService.list().stream() |
| | | .collect(Collectors.toMap(TechnologyOperation::getName, TechnologyOperation::getId, (k1, k2) -> k1)); |
| | | |
| | | // 记录已经插入结构的节点:Key = "名称+规格", Value = structure_id |
| | | // 每遇到一个根节点行会重置,支持一次导入多个 BOM |
| | | Map<String, Long> treePathMap = new HashMap<>(); |
| | | // 当前正在处理的 BOM,遇到根节点行时重新创建 |
| | | TechnologyBom bom = null; |
| | | |
| | | for (int i = 0; i < list.size(); i++) { |
| | | BomImportDto dto = list.get(i); |
| | | String parentKey = dto.getParentName() + "|" + dto.getParentSpec(); |
| | | String childKey = dto.getChildName() + "|" + dto.getChildSpec(); |
| | | |
| | | //处理根节点,第一行且子项为空 |
| | | if (i == 0 && StringUtils.isBlank(dto.getChildName())) { |
| | | // 根节点行: 工序、子项名称、子项规格全为空,属于一个新的 BOM 的父节点 |
| | | if (StringUtils.isBlank(dto.getProcess()) |
| | | && StringUtils.isBlank(dto.getChildName()) |
| | | && StringUtils.isBlank(dto.getChildSpec())) { |
| | | if (StringUtils.isBlank(dto.getParentName())) { |
| | | throw new ServiceException("导入失败: 第" + (i + 1) + "行父项名称为空"); |
| | | } |
| | | |
| | | // 创建新的 BOM |
| | | ProductModel rootModel = findModel(dto.getParentName(), dto.getParentSpec()); |
| | | bom = new TechnologyBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setVersion("1.0"); |
| | | technologyBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | technologyBomMapper.updateById(bom); |
| | | |
| | | // 插入根结构节点,重置节点 Map,开始记录这个 BOM 的树 |
| | | TechnologyBomStructure rootNode = new TechnologyBomStructure(); |
| | | rootNode.setBomId(bom.getId().longValue()); |
| | | rootNode.setParentId(null); // 顶层没有父节点 |
| | |
| | | rootNode.setUnit(rootModel.getUnit()); |
| | | technologyBomStructureService.save(rootNode); |
| | | |
| | | treePathMap = new HashMap<>(); |
| | | treePathMap.put(parentKey, rootNode.getId()); |
| | | continue; |
| | | } |
| | | |
| | | // 处理子层级节点 |
| | | if (bom == null) { |
| | | throw new ServiceException("导入失败: 首行必须是BOM父节点(父项信息行),子项前需先定义父项"); |
| | | } |
| | | // 找到父节点在数据库里的 ID |
| | | Long parentStructureId = treePathMap.get(parentKey); |
| | | if (parentStructureId == null) { |
| | |
| | | node.setProductModelId(childModel.getId()); |
| | | node.setUnitQuantity(dto.getUnitQty()); |
| | | node.setUnit(childModel.getUnit()); |
| | | // if (processMap.containsKey(dto.getProcess())) { |
| | | // node.setProcessId(processMap.get(dto.getProcess())); |
| | | // } |
| | | // 绑定投料工序(technology_operation),工序不存在时给出明确提示 |
| | | if (StringUtils.isNotBlank(dto.getProcess())) { |
| | | Long operationId = processMap.get(dto.getProcess()); |
| | | if (operationId == null) { |
| | | throw new ServiceException("导入失败: 工序[" + dto.getProcess() + "]不存在,请先维护工序"); |
| | | } |
| | | node.setOperationId(operationId); |
| | | } |
| | | technologyBomStructureService.save(node); |
| | | |
| | | // 把当前子项记录到 Map,作为以后更深层级的父项查找依据 |
| | |
| | | treePathMap.put(childKey, node.getId()); |
| | | } |
| | | |
| | | // 整个文件没有任何根节点行 |
| | | if (bom == null) { |
| | | return R.fail("导入失败: 未找到BOM父节点行"); |
| | | } |
| | | |
| | | return R.ok("BOM导入成功"); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void exportBom(HttpServletResponse response, Integer bomId) { |
| | | public void exportBom(HttpServletResponse response, Long bomId) { |
| | | if (bomId == null) { |
| | | throw new ServiceException("BOM ID不能为空"); |
| | | } |
| | | |
| | | List<TechnologyBomStructureVo> treeData = technologyBomStructureService.listByBomId(bomId); |
| | | if (treeData == null || treeData.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // List<ProductStructureDto> treeData = productStructureService.listBybomId(bomId); |
| | | // if (treeData == null || treeData.isEmpty()) { |
| | | // return; |
| | | // } |
| | | // |
| | | // // 将树形结构扁平化 使用 BFS算法 导出,按层级顺序 |
| | | // List<BomImportDto> exportList = new ArrayList<>(); |
| | | // |
| | | // // Map<ID, Node> idMap 用于查找父节点 |
| | | // Map<Long, ProductStructureDto> idMap = new HashMap<>(); |
| | | // populateMap(treeData, idMap); |
| | | // |
| | | // // treeData 的第一个是根节点 |
| | | // for (ProductStructureDto root : treeData) { |
| | | // // 添加根节点 |
| | | // BomImportDto rootRow = new BomImportDto(); |
| | | // rootRow.setParentName(root.getProductName()); |
| | | // rootRow.setParentSpec(root.getModel()); |
| | | // rootRow.setUnitQty(root.getUnitQuantity()); |
| | | // rootRow.setRemark(""); |
| | | // exportList.add(rootRow); |
| | | // |
| | | // // BFS 遍历-队列 |
| | | // Queue<ProductStructureDto> queue = new LinkedList<>(); |
| | | // if (root.getChildren() != null) { |
| | | // queue.addAll(root.getChildren()); |
| | | // } |
| | | // |
| | | // while (!queue.isEmpty()) { |
| | | // ProductStructureDto child = queue.poll(); |
| | | // |
| | | // // 查找父节点 |
| | | // ProductStructureDto parent = idMap.get(child.getParentId()); |
| | | // if (parent == null) { |
| | | // // 除了最外层节点,其他节点的父类肯定是不会为空的 |
| | | // continue; |
| | | // } |
| | | // |
| | | // BomImportDto row = new BomImportDto(); |
| | | // // 父类信息 |
| | | // row.setParentName(parent.getProductName()); |
| | | // row.setParentSpec(parent.getModel()); |
| | | // // 子类信息 |
| | | // row.setChildName(child.getProductName()); |
| | | // row.setChildSpec(child.getModel()); |
| | | // row.setUnitQty(child.getUnitQuantity()); |
| | | // row.setProcess(child.getProcessName()); |
| | | // |
| | | // exportList.add(row); |
| | | // |
| | | // // 将子节点的子节点加入队列-下一层 |
| | | // if (child.getChildren() != null && !child.getChildren().isEmpty()) { |
| | | // queue.addAll(child.getChildren()); |
| | | // } |
| | | // } |
| | | // } |
| | | // 将树形结构扁平化 使用 BFS算法 导出,按层级顺序 |
| | | List<BomImportDto> exportList = new ArrayList<>(); |
| | | |
| | | // Map<ID, Node> idMap 用于查找父节点 |
| | | Map<Long, TechnologyBomStructureVo> idMap = new HashMap<>(); |
| | | populateMap(treeData, idMap); |
| | | |
| | | // treeData 的第一个是根节点 |
| | | for (TechnologyBomStructureVo root : treeData) { |
| | | // 添加根节点 |
| | | BomImportDto rootRow = new BomImportDto(); |
| | | rootRow.setParentName(root.getProductName()); |
| | | rootRow.setParentSpec(root.getModel()); |
| | | rootRow.setUnitQty(root.getUnitQuantity()); |
| | | rootRow.setParentCode(root.getProductCode()); |
| | | rootRow.setRemark(""); |
| | | exportList.add(rootRow); |
| | | |
| | | // BFS 遍历-队列 |
| | | Queue<TechnologyBomStructureVo> queue = new LinkedList<>(); |
| | | if (root.getChildren() != null) { |
| | | queue.addAll(root.getChildren()); |
| | | } |
| | | |
| | | while (!queue.isEmpty()) { |
| | | TechnologyBomStructureVo child = queue.poll(); |
| | | |
| | | // 查找父节点 |
| | | TechnologyBomStructureVo parent = idMap.get(child.getParentId()); |
| | | if (parent == null) { |
| | | // 除了最外层节点,其他节点的父类肯定是不会为空的 |
| | | continue; |
| | | } |
| | | |
| | | BomImportDto row = new BomImportDto(); |
| | | // 父类信息 |
| | | row.setParentName(parent.getProductName()); |
| | | row.setParentSpec(parent.getModel()); |
| | | // 子类信息 |
| | | row.setChildName(child.getProductName()); |
| | | row.setChildSpec(child.getModel()); |
| | | row.setUnitQty(child.getUnitQuantity()); |
| | | row.setProcess(child.getOperationName()); |
| | | row.setChildCode(child.getProductCode()); |
| | | |
| | | exportList.add(row); |
| | | |
| | | // 将子节点的子节点加入队列-下一层 |
| | | if (child.getChildren() != null && !child.getChildren().isEmpty()) { |
| | | queue.addAll(child.getChildren()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class); |
| | | // util.exportExcel(response, exportList, "BOM结构导出"); |
| | | util.exportExcel(response, exportList, "BOM结构导出"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R copy(TechnologyBom technologyBom) { |
| | | TechnologyBom oldTechnologyBom = technologyBomMapper.selectById(technologyBom.getId()); |
| | | List<TechnologyBomStructureVo> oldTechnologyBomStructureVos = technologyBomStructureService.listByBomId(technologyBom.getId().longValue()); |
| | | //校验产品规格是否存在。 |
| | | validateProductModel(oldTechnologyBom.getProductModelId()); |
| | | TechnologyBom newTechnologyBom = new TechnologyBom(); |
| | | newTechnologyBom.setProductModelId(oldTechnologyBom.getProductModelId()); |
| | | newTechnologyBom.setVersion("FZ" + oldTechnologyBom.getVersion()); |
| | | newTechnologyBom.setRemark(oldTechnologyBom.getRemark()); |
| | | boolean saved = technologyBomMapper.insert(newTechnologyBom) > 0; |
| | | if (!saved) { |
| | | return R.fail("Copy BOM failed"); |
| | | } |
| | | newTechnologyBom.setBomNo("BM." + String.format("%05d", newTechnologyBom.getId())); |
| | | technologyBomMapper.updateById(newTechnologyBom); |
| | | //初始化BOM根节点结构。 |
| | | initRootStructure(newTechnologyBom.getId().longValue(), newTechnologyBom.getProductModelId()); |
| | | //把产品结构里面的数据也全部都复制 |
| | | TechnologyBomStructureVo technologyBomStructureVo = oldTechnologyBomStructureVos.get(0); |
| | | TechnologyBomStructureDto technologyBomStructureDto = convertTree(technologyBomStructureVo); |
| | | technologyBomStructureDto.setBomId(newTechnologyBom.getId().longValue()); |
| | | technologyBomStructureService.addTechnologyBomStructure(technologyBomStructureDto); |
| | | return R.ok(); |
| | | } |
| | | |
| | | private ProductModel findModel(String name, String spec) { |
| | |
| | | return s.replaceAll("[\\u00A0\\u3000]", "").trim(); |
| | | } |
| | | |
| | | private void populateMap(List<ProductStructureDto> nodes, Map<Long, ProductStructureDto> map) { |
| | | private void populateMap(List<TechnologyBomStructureVo> nodes, Map<Long, TechnologyBomStructureVo> map) { |
| | | if (nodes == null || nodes.isEmpty()) { |
| | | return; |
| | | } |
| | | for (ProductStructureDto node : nodes) { |
| | | for (TechnologyBomStructureVo node : nodes) { |
| | | map.put(node.getId(), node); |
| | | populateMap(node.getChildren(), map); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 递归转换树形结构 VO -> DTO |
| | | * 自动生成虚拟 tempId / parentTempId,保证任意层级树结构正确 |
| | | */ |
| | | public static TechnologyBomStructureDto convertTree(TechnologyBomStructureVo vo) { |
| | | if (vo == null) { |
| | | return null; |
| | | } |
| | | TechnologyBomStructureDto realDto = convertNode(vo, "0"); // 根节点父ID=0(纯数字) |
| | | TechnologyBomStructureDto rootDto = new TechnologyBomStructureDto(); |
| | | rootDto.setTempId("0"); |
| | | rootDto.setChildren(Collections.singletonList(realDto)); |
| | | |
| | | return rootDto; |
| | | } |
| | | |
| | | /** |
| | | * 核心递归方法 |
| | | * @param vo 原始节点 |
| | | * @param parentTempId 父节点 纯数字ID |
| | | * @return 转换后DTO |
| | | */ |
| | | private static TechnologyBomStructureDto convertNode(TechnologyBomStructureVo vo, String parentTempId) { |
| | | if (vo == null) { |
| | | return null; |
| | | } |
| | | |
| | | TechnologyBomStructureDto dto = new TechnologyBomStructureDto(); |
| | | BeanUtils.copyProperties(vo, dto); |
| | | |
| | | String currentTempId = getNumberId(); |
| | | dto.setTempId(currentTempId); |
| | | dto.setParentTempId(parentTempId); |
| | | |
| | | |
| | | dto.setId(null); |
| | | dto.setParentId(null); |
| | | |
| | | // ===================== 递归子节点 ===================== |
| | | List<TechnologyBomStructureVo> voChildren = vo.getChildren(); |
| | | if (CollUtil.isNotEmpty(voChildren)) { |
| | | List<TechnologyBomStructureDto> dtoChildren = new ArrayList<>(); |
| | | for (TechnologyBomStructureVo childVo : voChildren) { |
| | | // 子节点的父ID = 当前节点的数字ID |
| | | dtoChildren.add(convertNode(childVo, currentTempId)); |
| | | } |
| | | dto.setChildren(dtoChildren); |
| | | } else { |
| | | dto.setChildren(new ArrayList<>()); |
| | | } |
| | | |
| | | return dto; |
| | | } |
| | | |
| | | /** |
| | | * 生成 13位 纯数字随机ID(安全、不重复、高性能) |
| | | */ |
| | | private static String getNumberId() { |
| | | // 生成 1000000000000 ~ 9999999999999 之间的数字 |
| | | long min = 1000000000000L; |
| | | long max = 9999999999999L; |
| | | long randomNum = ThreadLocalRandom.current().nextLong(min, max + 1); |
| | | return String.valueOf(randomNum); |
| | | } |
| | | } |