| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | 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.production.service.ProductBomService; |
| | | import com.ruoyi.production.service.ProductProcessService; |
| | | import com.ruoyi.production.service.ProductStructureService; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult copy(ProductBomDto productBom) { |
| | | Long copyId = productBom.getCopyId(); |
| | | if (copyId == null) { |
| | | throw new ServiceException("复制源BOM ID不能为空"); |
| | | } |
| | | ProductBom sourceBom = productBomMapper.selectById(copyId); |
| | | if (sourceBom == null) { |
| | | throw new ServiceException("复制源BOM不存在"); |
| | | } |
| | | |
| | | |
| | | ProductBom newBom = getProductBom(productBom, sourceBom); |
| | | productBomMapper.insert(newBom); |
| | | newBom.setBomNo("BM." + String.format("%05d", newBom.getId())); |
| | | productBomMapper.updateById(newBom); |
| | | |
| | | |
| | | |
| | | ProductModel productModel = productModelService.getById(newBom.getProductModelId()); |
| | | if (productModel == null) { |
| | | throw new ServiceException("选择的产品模型不存在"); |
| | | } |
| | | |
| | | ProductStructure newRoot = getProductStructure(newBom, productModel); |
| | | productStructureService.save(newRoot); |
| | | List<ProductStructure> sourceStructures = productStructureMapper.selectList( |
| | | Wrappers.<ProductStructure>lambdaQuery() |
| | | .eq(ProductStructure::getBomId, copyId.intValue())); |
| | | if (sourceStructures == null || sourceStructures.isEmpty()) { |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | ProductStructure oldRoot = sourceStructures.stream() |
| | | .filter(s -> s.getParentId() == null) |
| | | .findFirst().orElse(new ProductStructure()); |
| | | |
| | | List<ProductStructure> children = sourceStructures |
| | | .stream() |
| | | .filter(s -> !s.getId().equals(oldRoot.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | Map<Long, Long> oldNewIdMap = new HashMap<>(); |
| | | |
| | | oldNewIdMap.put(oldRoot.getId(), newRoot.getId()); |
| | | |
| | | List<ProductStructure> insertList = children |
| | | .stream() |
| | | .map(item -> getProductStructures(item, newBom)) |
| | | .collect(Collectors.toList()); |
| | | |
| | | productStructureService.saveBatch(insertList); |
| | | |
| | | for (int i = 0; i < children.size(); i++) { |
| | | oldNewIdMap.put( |
| | | children.get(i).getId(), |
| | | insertList.get(i).getId() |
| | | ); |
| | | } |
| | | |
| | | List<ProductStructure> updateList = new ArrayList<>(); |
| | | for (int i = 0; i < children.size(); i++) { |
| | | ProductStructure source = children.get(i); |
| | | ProductStructure inserted = insertList.get(i); |
| | | Long newParentId = oldNewIdMap.get(source.getParentId()); |
| | | if (newParentId != null) { |
| | | inserted.setParentId(newParentId); |
| | | updateList.add(inserted); |
| | | } |
| | | } |
| | | if (!updateList.isEmpty()) { |
| | | productStructureService.updateBatchById(updateList); |
| | | } |
| | | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @NotNull |
| | | private static ProductStructure getProductStructures(ProductStructure item, ProductBom newBom) { |
| | | ProductStructure copy = new ProductStructure(); |
| | | copy.setProductModelId(item.getProductModelId()); |
| | | copy.setProcessId(item.getProcessId()); |
| | | copy.setUnitQuantity(item.getUnitQuantity()); |
| | | copy.setDemandedQuantity(item.getDemandedQuantity()); |
| | | copy.setUnit(item.getUnit()); |
| | | copy.setBomId(newBom.getId()); |
| | | return copy; |
| | | } |
| | | |
| | | @NotNull |
| | | private static ProductStructure getProductStructure(ProductBom newBom, ProductModel productModel) { |
| | | ProductStructure newRoot = new ProductStructure(); |
| | | newRoot.setProductModelId(newBom.getProductModelId()); |
| | | newRoot.setUnitQuantity(BigDecimal.valueOf(1)); |
| | | newRoot.setUnit(productModel.getUnit()); |
| | | newRoot.setBomId(newBom.getId()); |
| | | return newRoot; |
| | | } |
| | | |
| | | @NotNull |
| | | private static ProductBom getProductBom(ProductBomDto productBom, ProductBom sourceBom) { |
| | | ProductBom newBom = new ProductBom(); |
| | | newBom.setProductModelId(productBom.getProductModelId() != null ? productBom.getProductModelId() : sourceBom.getProductModelId()); |
| | | newBom.setRemark(productBom.getRemark()); |
| | | newBom.setVersion(productBom.getVersion() != null ? productBom.getVersion() : sourceBom.getVersion()); |
| | | return newBom; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult uploadBom(MultipartFile file) { |
| | | ExcelUtil<BomImportDto> util = new ExcelUtil<>(BomImportDto.class); |
| | | List<BomImportDto> list; |