| | |
| | | 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.TechnologyRouting; |
| | | import com.ruoyi.technology.service.TechnologyBomService; |
| | | import com.ruoyi.technology.service.TechnologyBomStructureService; |
| | | 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; |
| | | |
| | | /** |
| | | * 分页查询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("产品规格不存在"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | @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); |
| | | } |
| | | } |