| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | 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.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.service.IProductModelService; |
| | | import com.ruoyi.basic.service.IProductService; |
| | | import com.ruoyi.production.pojo.ProductMaterial; |
| | | import com.ruoyi.production.pojo.ProductMaterialSku; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | |
| | | import com.ruoyi.production.pojo.ProductBom; |
| | | import com.ruoyi.production.pojo.ProductProcess; |
| | | import com.ruoyi.production.pojo.ProductStructure; |
| | | import com.ruoyi.production.service.ProductBomService; |
| | | import com.ruoyi.production.service.ProductProcessService; |
| | | import com.ruoyi.production.service.ProductStructureService; |
| | | import com.ruoyi.production.service.*; |
| | | import com.ruoyi.project.system.domain.SysDictData; |
| | | import com.ruoyi.project.system.mapper.SysDictDataMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | public class ProductBomServiceImpl extends ServiceImpl<ProductBomMapper, ProductBom> implements ProductBomService { |
| | | |
| | | @Autowired |
| | | private IProductService productService; |
| | | |
| | | @Autowired |
| | | private ProductBomMapper productBomMapper; |
| | | |
| | | |
| | | @Autowired |
| | | private IProductModelService productModelService; |
| | | |
| | | @Autowired |
| | | private ProductStructureService productStructureService; |
| | |
| | | @Autowired |
| | | private ProductProcessService productProcessService; |
| | | |
| | | @Autowired |
| | | private ProductMaterialService productMaterialService; |
| | | |
| | | @Autowired |
| | | private ProductMaterialSkuService productMaterialSkuService; |
| | | |
| | | @Autowired |
| | | private SysDictDataMapper sysDictDataMapper; |
| | | |
| | | @Override |
| | | public IPage<ProductBomDto> listPage(Page page, ProductBomDto productBomDto) { |
| | | public IPage<ProductBomDto> listPage(Page<ProductBom> page, ProductBomDto productBomDto) { |
| | | return productBomMapper.listPage(page, productBomDto); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult add(ProductBom productBom) { |
| | | if (productBom == null || productBom.getDictCode() == null) { |
| | | throw new ServiceException("新增BOM失败,产品类型不能为空"); |
| | | } |
| | | |
| | | SysDictData sysDictData = sysDictDataMapper.selectDictDataById(productBom.getDictCode()); |
| | | if (sysDictData == null) { |
| | | throw new ServiceException("新增BOM失败,产品类型不存在"); |
| | | } |
| | | |
| | | boolean save = productBomMapper.insert(productBom) > 0; |
| | | if (save) { |
| | | String no = "BM." + String.format("%05d", productBom.getId()); |
| | | productBom.setBomNo(no); |
| | | productBomMapper.updateById(productBom); |
| | | |
| | | // 查询出产品模型信息 |
| | | if (productBom.getProductModelId() == null) { |
| | | throw new ServiceException("请选择产品模型"); |
| | | } |
| | | |
| | | ProductModel productModel = productModelService.getById(productBom.getProductModelId()); |
| | | if (productModel == null) { |
| | | throw new ServiceException("选择的产品模型不存在"); |
| | | } |
| | | |
| | | // 添加初始的产品结构 |
| | | ProductStructure productStructure = new ProductStructure(); |
| | | productStructure.setProductModelId(productBom.getProductModelId()); |
| | | productStructure.setUnit(productModel.getUnit()); |
| | | productStructure.setUnitQuantity(BigDecimal.valueOf(1)); |
| | | productStructure.setBomId(productBom.getId()); |
| | | |
| | | productStructureService.save(productStructure); |
| | | |
| | | return AjaxResult.success(); |
| | | } |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult uploadBom(MultipartFile file) { |
| | | public AjaxResult uploadBom(MultipartFile file, Long dictCode) { |
| | | if (dictCode == null) { |
| | | return AjaxResult.error("导入失败,产品类型不能为空"); |
| | | } |
| | | SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictCode); |
| | | if (sysDictData == null) { |
| | | return AjaxResult.error("导入失败,产品类型不存在"); |
| | | } |
| | | ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class); |
| | | List<BomImportDto> list; |
| | | try { |
| | |
| | | return AjaxResult.error("Excel解析失败"); |
| | | } |
| | | |
| | | if (list == null || list.isEmpty()) return AjaxResult.error("数据为空"); |
| | | if (list == null || list.isEmpty()) { |
| | | return AjaxResult.error("数据为空"); |
| | | } |
| | | |
| | | // 处理工序 |
| | | list.forEach(dto -> { |
| | | dto.setParentName(clean(dto.getParentName())); |
| | | 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()); |
| | | Set<String> processNames = list.stream() |
| | | .map(BomImportDto::getProcess) |
| | | .filter(StringUtils::isNotBlank) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Map<String, Long> processMap = new HashMap<>(); |
| | | if (!processNames.isEmpty()) { |
| | | List<ProductProcess> exists = productProcessService.list( |
| | | new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames) |
| | | ); |
| | | processMap = exists.stream().collect(Collectors.toMap(ProductProcess::getName, ProductProcess::getId, (k1, k2) -> k1)); |
| | | |
| | | Map<String, Long> finalProcessMap = processMap; |
| | | List<String> missingProcesses = processNames.stream() |
| | | .filter(n -> !finalProcessMap.containsKey(n)) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!missingProcesses.isEmpty()) { |
| | | throw new ServiceException("导入失败,以下工序不存在:" + String.join(", ", missingProcesses)); |
| | | } |
| | | } |
| | | |
| | | Set<String> materialKeys = new HashSet<>(); |
| | | for (BomImportDto dto : list) { |
| | | if (StringUtils.isNotBlank(dto.getParentName())) { |
| | | materialKeys.add(dto.getParentName() + "|" + (dto.getParentSpec() == null ? "" : dto.getParentSpec())); |
| | | } |
| | | if (StringUtils.isNotBlank(dto.getChildName())) { |
| | | materialKeys.add(dto.getChildName() + "|" + (dto.getChildSpec() == null ? "" : dto.getChildSpec())); |
| | | } |
| | | } |
| | | |
| | | Map<String, ProductMaterialSku> skuMap = validateAndGetSkuMap(materialKeys); |
| | | |
| | | ProductBom bom = new ProductBom(); |
| | | bom.setProductModelId(rootModel.getId()); |
| | | bom.setDictCode(dictCode); |
| | | bom.setVersion("1.0"); |
| | | productBomMapper.insert(bom); |
| | | bom.setBomNo("BM." + String.format("%05d", bom.getId())); |
| | | productBomMapper.updateById(bom); |
| | | |
| | | // 记录已经插入结构的节点:Key = "名称+规格", Value = structure_id |
| | | // 记录已经插入结构的节点: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(); |
| | | for (BomImportDto dto : list) { |
| | | String parentName = dto.getParentName(); |
| | | String parentSpec = dto.getParentSpec(); |
| | | String childName = dto.getChildName(); |
| | | String childSpec = 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); |
| | | String parentKey = parentName + "|" + (parentSpec == null ? "" : parentSpec); |
| | | String childKey = childName + "|" + (childSpec == null ? "" : childSpec); |
| | | |
| | | treePathMap.put(parentKey, rootNode.getId()); |
| | | // 处理顶级节点 (子项及其规格均为空当作顶级节点定义) |
| | | if (StringUtils.isBlank(childName) && StringUtils.isBlank(childSpec)) { |
| | | if (!treePathMap.containsKey(parentKey)) { |
| | | ProductMaterialSku model = skuMap.get(parentKey); |
| | | ProductStructure node = saveStructure(bom.getId(), null, model, dto.getUnitQty(), null); |
| | | treePathMap.put(parentKey, node.getId()); |
| | | } else { |
| | | // 如果已经存在,仅在它是顶级节点时更新 |
| | | Long structureId = treePathMap.get(parentKey); |
| | | ProductStructure node = productStructureService.getById(structureId); |
| | | if (node != null && node.getParentId() == null && dto.getUnitQty() != null) { |
| | | node.setUnitQuantity(dto.getUnitQty()); |
| | | productStructureService.updateById(node); |
| | | } |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | // 处理子层级节点 |
| | | // 找到父节点在数据库里的 ID |
| | | // 处理父子层级关系 |
| | | Long parentStructureId = treePathMap.get(parentKey); |
| | | if (parentStructureId == null) { |
| | | // 如果 Map 里找不到,说明 Excel 顺序乱了或者数据有误 |
| | | throw new ServiceException("导入失败: 父项[" + dto.getParentName() + "]必须在其子项之前定义"); |
| | | // 如果 Map 里找不到父节点,视为顶级父节点 |
| | | ProductMaterialSku parentModel = skuMap.get(parentKey); |
| | | ProductStructure parentNode = saveStructure(bom.getId(), null, parentModel, BigDecimal.ONE, null); |
| | | parentStructureId = parentNode.getId(); |
| | | treePathMap.put(parentKey, parentStructureId); |
| | | } |
| | | |
| | | // 获取子项模型信息 |
| | | 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())); |
| | | ProductMaterialSku childModel = skuMap.get(childKey); |
| | | Long processId = null; |
| | | if (StringUtils.isNotBlank(dto.getProcess())) { |
| | | processId = processMap.get(dto.getProcess()); |
| | | } |
| | | productStructureService.save(node); |
| | | |
| | | // 把当前子项记录到 Map,作为以后更深层级的父项查找依据 |
| | | // 同一父项下的同名子项不需要重复记录 |
| | | treePathMap.put(childKey, node.getId()); |
| | | // 插入子节点结构表 |
| | | ProductStructure childNode = saveStructure(bom.getId(), parentStructureId, childModel, dto.getUnitQty(), processId); |
| | | // 把当前子项记录到 Map,作为以后更深层级的父项查找依据 |
| | | treePathMap.put(childKey, childNode.getId()); |
| | | } |
| | | |
| | | return AjaxResult.success("BOM导入成功"); |
| | | } |
| | | |
| | | /** |
| | | * 保存结构节点 |
| | | */ |
| | | private ProductStructure saveStructure(Integer bomId, Long parentId, ProductMaterialSku model, BigDecimal qty, Long processId) { |
| | | ProductStructure node = new ProductStructure(); |
| | | node.setBomId(bomId); |
| | | node.setParentId(parentId); |
| | | node.setProductModelId(model.getId()); |
| | | node.setUnitQuantity(qty != null ? qty : BigDecimal.ONE); |
| | | ProductMaterial material = productMaterialService.getById(model.getProductId()); |
| | | node.setUnit(material != null ? material.getUnit() : null); |
| | | node.setProcessId(processId); |
| | | productStructureService.save(node); |
| | | return node; |
| | | } |
| | | |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | List<ProductStructureDto> treeData = productStructureService.listBybomId(bomId); |
| | | List<ProductStructureDto> treeData = productStructureService.listByBomId(bomId); |
| | | if (treeData == null || treeData.isEmpty()) { |
| | | return; |
| | | } |
| | |
| | | util.exportExcel(response, exportList, "BOM结构导出"); |
| | | } |
| | | |
| | | @Override |
| | | public String strengthById(Long bomId) { |
| | | if (bomId == null) { |
| | | return null; |
| | | } |
| | | return baseMapper.selectStrengthById(bomId); |
| | | } |
| | | |
| | | 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); |
| | | |
| | | ProductModel model = productModelService.getOne(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getProductId, product.getId()) |
| | | .eq(ProductModel::getModel, spec).last("limit 1")); |
| | | if (model == null) throw new ServiceException("规格未维护:" + name + "[" + spec + "]"); |
| | | return model; |
| | | } |
| | | |
| | | private void handleProcess(List<BomImportDto> list) { |
| | | |
| | | Set<String> processNames = list.stream() |
| | | .map(BomImportDto::getProcess) |
| | | .filter(StringUtils::isNotBlank) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | if (processNames.isEmpty()) { |
| | | return; |
| | | /** |
| | | * 批量验证并获取产品SKU信息 |
| | | */ |
| | | private Map<String, ProductMaterialSku> validateAndGetSkuMap(Set<String> materialKeys) { |
| | | if (materialKeys == null || materialKeys.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | List<ProductProcess> exists = productProcessService.list( |
| | | new LambdaQueryWrapper<ProductProcess>().in(ProductProcess::getName, processNames) |
| | | ); |
| | | |
| | | Set<String> existNames = exists.stream() |
| | | .map(ProductProcess::getName) |
| | | // 1. 获取所有产品名称并查询 |
| | | Set<String> productNames = materialKeys.stream() |
| | | .map(k -> k.split("\\|")[0]) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | List<ProductProcess> needSave = processNames.stream() |
| | | .filter(n -> !existNames.contains(n)) |
| | | .map(n -> { |
| | | ProductProcess p = new ProductProcess(); |
| | | p.setName(n); |
| | | return p; |
| | | List<ProductMaterial> products = productMaterialService.list( |
| | | new LambdaQueryWrapper<ProductMaterial>().in(ProductMaterial::getProductName, productNames) |
| | | ); |
| | | Map<String, ProductMaterial> productMap = products.stream() |
| | | .collect(Collectors.toMap(ProductMaterial::getProductName, p -> p, (k1, k2) -> k1)); |
| | | |
| | | // 检查缺失的产品 |
| | | List<String> missingProducts = productNames.stream() |
| | | .filter(n -> !productMap.containsKey(n)) |
| | | .collect(Collectors.toList()); |
| | | if (!missingProducts.isEmpty()) { |
| | | throw new ServiceException("导入失败,以下产品未维护:" + String.join(", ", missingProducts)); |
| | | } |
| | | |
| | | // 2. 查询所有相关的 SKU |
| | | // 构造查询:(productId = ? AND model = ?) OR (productId = ? AND model = ?) ... |
| | | // 由于 MyBatis Plus 的 Or 嵌套可能较慢且此处数量通常可控,我们可以先查出所有这些 Product 的所有 SKU |
| | | List<Long> productIds = products.stream().map(ProductMaterial::getId).collect(Collectors.toList()); |
| | | List<ProductMaterialSku> skus = productMaterialSkuService.list( |
| | | new LambdaQueryWrapper<ProductMaterialSku>().in(ProductMaterialSku::getProductId, productIds) |
| | | ); |
| | | |
| | | // 构建 Map: "产品名称|规格" -> Sku |
| | | Map<String, ProductMaterialSku> skuMap = new HashMap<>(); |
| | | for (ProductMaterialSku sku : skus) { |
| | | ProductMaterial p = productMap.values().stream() |
| | | .filter(pm -> pm.getId().equals(sku.getProductId())) |
| | | .findFirst().orElse(null); |
| | | if (p != null) { |
| | | String key = p.getProductName() + "|" + (sku.getModel() == null ? "" : sku.getModel()); |
| | | skuMap.put(key, sku); |
| | | } |
| | | } |
| | | |
| | | // 检查缺失的规格 |
| | | List<String> missingSkus = materialKeys.stream() |
| | | .filter(k -> !skuMap.containsKey(k)) |
| | | .map(k -> { |
| | | String[] parts = k.split("\\|"); |
| | | return parts[0] + "[" + (parts.length > 1 ? parts[1] : "") + "]"; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!needSave.isEmpty()) { |
| | | productProcessService.saveBatch(needSave); |
| | | needSave.forEach(p -> p.setNo("GX" + String.format("%08d", p.getId()))); |
| | | productProcessService.updateBatchById(needSave); |
| | | if (!missingSkus.isEmpty()) { |
| | | throw new ServiceException("导入失败,以下产品规格未维护:" + String.join(", ", missingSkus)); |
| | | } |
| | | |
| | | return skuMap; |
| | | } |
| | | |
| | | private String clean(String s) { |
| | |
| | | return s.replaceAll("[\\u00A0\\u3000]", "").trim(); |
| | | } |
| | | } |
| | | |
| | | |