| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.pojo.Product; |
| | |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.production.dto.BomImportDto; |
| | | import com.ruoyi.production.dto.BomImportErrorDto; |
| | | import com.ruoyi.production.dto.ProductBomDto; |
| | | import com.ruoyi.production.dto.ProductStructureDto; |
| | | import com.ruoyi.production.mapper.ProductBomMapper; |
| | |
| | | import com.ruoyi.production.service.ProductBomService; |
| | | import com.ruoyi.production.service.ProductProcessService; |
| | | import com.ruoyi.production.service.ProductStructureService; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | @Autowired |
| | | private ProductProcessService productProcessService; |
| | | |
| | | |
| | | @Override |
| | | public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) { |
| | | return productBomMapper.listPage(page, productBomDto); |
| | | } |
| | | |
| | | /** |
| | | * 产品根据bom计算子项数量(只统计二级) |
| | | */ |
| | | public Long countChild(Long productModelId) { |
| | | ProductBom productBom = productBomMapper.selectOne(new LambdaQueryWrapper<ProductBom>() |
| | | .eq(ProductBom::getProductModelId, productModelId)); |
| | | if(productBom != null){ |
| | | return productStructureService.countBybomId(productBom.getId()); |
| | | } |
| | | return 0L; |
| | | } |
| | | |
| | | /** |
| | | * 判断产品是否存在bom false不存在 true存在 |
| | | * @param productBom |
| | | * @return |
| | | */ |
| | | public boolean checkBom(ProductBom productBom) { |
| | | ProductBom productBom1 = productBomMapper.selectOne(new LambdaQueryWrapper<ProductBom>() |
| | | .eq(ProductBom::getProductModelId, productBom.getProductModelId())); |
| | | return productBom1 == null ? false : true; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult add(ProductBom productBom) { |
| | | boolean b = checkBom(productBom); |
| | | if (b) { |
| | | return AjaxResult.error("产品模型已存在BOM"); |
| | | } |
| | | boolean save = productBomMapper.insert(productBom) > 0; |
| | | if (save) { |
| | | String no = "BM." + String.format("%05d", productBom.getId()); |
| | |
| | | return AjaxResult.error(); |
| | | } |
| | | |
| | | /** |
| | | * 递归保存 BOM:先保存父项 → 保存子项 → 建立关系 → 递归子项的子项 |
| | | * @param children 当前父项的子项列表 |
| | | * @return 保存后的父项产品ID |
| | | */ |
| | | private void saveBomRecursive(List<BomImportDto> children, |
| | | ProductBom bom,ProductModel rootModel, |
| | | Map<String, Long> processMap, |
| | | List<BomImportErrorDto> errorList ) { |
| | | // 1. 获取children中子项产品编号为空的数据 |
| | | List<BomImportDto> parentChildren = children |
| | | .stream() |
| | | .filter(child -> StringUtils.isEmpty(child.getChildCode())) |
| | | .collect(Collectors.toList()); |
| | | if(CollectionUtils.isEmpty(parentChildren)){ |
| | | return; |
| | | } |
| | | BomImportDto parentId = parentChildren.get(0); // 父级数据 |
| | | ProductStructure rootNode = new ProductStructure(); |
| | | rootNode.setBomId(bom.getId()); |
| | | rootNode.setParentId(null); // 顶层没有父节点 |
| | | rootNode.setProductModelId(rootModel.getId()); |
| | | rootNode.setUnitQuantity(BigDecimal.ONE); |
| | | rootNode.setUnit(rootModel.getUnit()); |
| | | productStructureService.save(rootNode); |
| | | |
| | | // 2. 遍历子项,逐个处理 |
| | | for (BomImportDto child : children) { |
| | | // 跳过没有子项的父项 |
| | | if(StringUtils.isEmpty(child.getChildCode())){ |
| | | continue; |
| | | } |
| | | // 获取子项模型信息 |
| | | ProductModel childModel = findModel(child.getChildName(), child.getChildSpec()); |
| | | if(childModel.getId() == null){ |
| | | BomImportErrorDto errorDto = new BomImportErrorDto(); |
| | | BeanUtils.copyProperties(child, errorDto); |
| | | errorDto.setErrorMsg(childModel.getErrorMsg()); |
| | | errorList.add(errorDto); |
| | | continue; |
| | | } |
| | | // 插入结构表 |
| | | ProductStructure node = new ProductStructure(); |
| | | node.setBomId(bom.getId()); |
| | | node.setParentId(rootNode.getId()); // 父节点ID |
| | | node.setProductModelId(childModel.getId()); |
| | | node.setUnitQuantity(child.getUnitQty()); |
| | | node.setUnit(childModel.getUnit()); |
| | | if (processMap.containsKey(child.getProcess())) { |
| | | node.setProcessId(processMap.get(child.getProcess())); |
| | | } |
| | | productStructureService.save(node); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult uploadBom(MultipartFile file) { |
| | | public void uploadBom(MultipartFile file,HttpServletResponse response) { |
| | | ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class); |
| | | List<BomImportDto> list; |
| | | try { |
| | | list = util.importExcel(file.getInputStream()); |
| | | } catch (Exception e) { |
| | | return AjaxResult.error("Excel解析失败"); |
| | | throw new ServiceException("Excel解析失败"); |
| | | } |
| | | |
| | | if (list == null || list.isEmpty()) return AjaxResult.error("数据为空"); |
| | | if (list == null || list.isEmpty()) throw new ServiceException("文件为空"); |
| | | |
| | | // 处理工序 |
| | | list.forEach(dto -> { |
| | |
| | | handleProcess(list); |
| | | Map<String, Long> processMap = productProcessService.list().stream() |
| | | .collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1)); |
| | | // 1. 按父项编码分组 |
| | | Map<String, List<BomImportDto>> parentMap = list.stream() |
| | | .filter(bom -> StringUtils.isNotBlank(bom.getParentCode())) |
| | | .collect(Collectors.groupingBy(BomImportDto::getParentCode)); |
| | | List<BomImportErrorDto> errorList = new ArrayList<>(); |
| | | // 2. 遍历所有父项,递归保存 |
| | | for (Map.Entry<String, List<BomImportDto>> entry : parentMap.entrySet()) { |
| | | |
| | | // 创建 BOM 数据 |
| | | BomImportDto first = list.get(0); |
| | | ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec()); |
| | | ProductBom bom = new ProductBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setVersion("1.0"); |
| | | productBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | productBomMapper.updateById(bom); |
| | | |
| | | // 记录已经插入结构的节点:Key = "名称+规格", Value = structure_id |
| | | Map<String, Long> treePathMap = new HashMap<>(); |
| | | |
| | | 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())) { |
| | | ProductStructure rootNode = new ProductStructure(); |
| | | rootNode.setBomId(bom.getId()); |
| | | rootNode.setParentId(null); // 顶层没有父节点 |
| | | rootNode.setProductModelId(rootModel.getId()); |
| | | rootNode.setUnitQuantity(BigDecimal.ONE); |
| | | rootNode.setUnit(rootModel.getUnit()); |
| | | productStructureService.save(rootNode); |
| | | |
| | | treePathMap.put(parentKey, rootNode.getId()); |
| | | // 创建 BOM 数据 |
| | | BomImportDto first = entry.getValue().get(0); |
| | | ProductModel rootModel = findModel(first.getParentName(), first.getParentSpec()); |
| | | if(rootModel.getId() == null){ |
| | | BomImportErrorDto error = new BomImportErrorDto(); |
| | | BeanUtils.copyProperties(first, error); |
| | | error.setErrorMsg(rootModel.getErrorMsg()); |
| | | errorList.add(error); |
| | | continue; |
| | | } |
| | | |
| | | // 处理子层级节点 |
| | | // 找到父节点在数据库里的 ID |
| | | Long parentStructureId = treePathMap.get(parentKey); |
| | | if (parentStructureId == null) { |
| | | // 如果 Map 里找不到,说明 Excel 顺序乱了或者数据有误 |
| | | throw new ServiceException("导入失败: 父项[" + dto.getParentName() + "]必须在其子项之前定义"); |
| | | ProductBom bom = new ProductBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setVersion("1.0"); |
| | | boolean b = checkBom(bom); |
| | | if(b){ |
| | | BomImportErrorDto error = new BomImportErrorDto(); |
| | | BeanUtils.copyProperties(first, error); |
| | | error.setErrorMsg("产品【"+ first.getParentSpec() + "】BOM已存在"); |
| | | errorList.add(error); |
| | | continue; |
| | | } |
| | | |
| | | // 获取子项模型信息 |
| | | ProductModel childModel = findModel(dto.getChildName(), dto.getChildSpec()); |
| | | |
| | | // 插入结构表 |
| | | ProductStructure node = new ProductStructure(); |
| | | node.setBomId(bom.getId()); |
| | | node.setParentId(parentStructureId); // 父节点ID |
| | | node.setProductModelId(childModel.getId()); |
| | | node.setUnitQuantity(dto.getUnitQty()); |
| | | node.setUnit(childModel.getUnit()); |
| | | if (processMap.containsKey(dto.getProcess())) { |
| | | node.setProcessId(processMap.get(dto.getProcess())); |
| | | } |
| | | productStructureService.save(node); |
| | | |
| | | // 把当前子项记录到 Map,作为以后更深层级的父项查找依据 |
| | | // 同一父项下的同名子项不需要重复记录 |
| | | treePathMap.put(childKey, node.getId()); |
| | | productBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | productBomMapper.updateById(bom); |
| | | // 处理bom子表数据 |
| | | List<BomImportDto> children = entry.getValue(); |
| | | saveBomRecursive(children,bom,rootModel,processMap,errorList); |
| | | } |
| | | |
| | | return AjaxResult.success("BOM导入成功"); |
| | | // 判断是否有错误数据,有就导出 |
| | | if(CollectionUtils.isNotEmpty(errorList)){ |
| | | ExcelUtil<BomImportErrorDto> utils = new ExcelUtil<>(BomImportErrorDto.class); |
| | | utils.exportExcel(response,errorList, "BOM错误数据"); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | util.exportExcel(response, exportList, "BOM结构导出"); |
| | | } |
| | | |
| | | @Override |
| | | public String updateBom(ProductBom productBom) { |
| | | ProductBom productBom1 = productBomMapper.selectById(productBom.getId()); |
| | | if(productBom.getProductModelId().equals(productBom1.getProductModelId())){ |
| | | productBomMapper.updateById(productBom); |
| | | }else{ |
| | | boolean b = checkBom(productBom); |
| | | if(b){ |
| | | throw new ServiceException("产品BOM已存在"); |
| | | }else{ |
| | | productBomMapper.updateById(productBom); |
| | | } |
| | | } |
| | | return "修改成功"; |
| | | } |
| | | |
| | | private void populateMap(List<ProductStructureDto> nodes, Map<Long, ProductStructureDto> map) { |
| | | if (nodes == null || nodes.isEmpty()) { |
| | | return; |
| | |
| | | } |
| | | |
| | | private ProductModel findModel(String name, String spec) { |
| | | Product product = productService.getOne(new LambdaQueryWrapper<Product>() |
| | | .eq(Product::getProductName, name).last("limit 1")); |
| | | if (product == null) throw new ServiceException("产品未维护:" + name); |
| | | // Product product = productService.getOne(new LambdaQueryWrapper<Product>() |
| | | // .eq(Product::getProductName, name).last("limit 1")); |
| | | // if (product == null) throw new ServiceException("产品未维护:" + name); |
| | | |
| | | ProductModel model = productModelService.getOne(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getProductId, product.getId()) |
| | | // .eq(ProductModel::getProductId, product.getId()) |
| | | .eq(ProductModel::getModel, spec).last("limit 1")); |
| | | if (model == null) throw new ServiceException("规格未维护:" + name + "[" + spec + "]"); |
| | | if (model == null){ |
| | | model = new ProductModel(); |
| | | model.setErrorMsg("图纸编号未维护:" + "[" + spec + "]"); |
| | | } |
| | | return model; |
| | | } |
| | | |