| | |
| | | package com.ruoyi.basic.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | 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.dto.ProductDto; |
| | | import com.ruoyi.basic.dto.ProductModelDto; |
| | | import com.ruoyi.basic.dto.*; |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.pojo.Product; |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.service.IProductModelService; |
| | | import com.ruoyi.common.utils.OrderUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 【请填写功能名称】Service业务层处理 |
| | |
| | | @AllArgsConstructor |
| | | public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService { |
| | | |
| | | private final ProductMapper productMapper; |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | private ProductModelMapper productModelMapper; |
| | | |
| | | @Override |
| | | public int addOrEditProductModel(ProductModelDto productModelDto) { |
| | | |
| | | if(StringUtils.isEmpty(productModelDto.getProductName())){ |
| | | throw new RuntimeException("产品名称不能为空"); |
| | | } |
| | | Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>() |
| | | .eq(Product::getProductName, productModelDto.getProductName())); |
| | | if (productModelDto.getId() == null) { |
| | | if(product == null){ |
| | | product = new Product(); |
| | | product.setProductName(productModelDto.getProductName()); |
| | | productMapper.insert(product); |
| | | } |
| | | ProductModel productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelDto,productModel); |
| | | productModel.setProductId(product.getId()); |
| | | return productModelMapper.insert(productModel); |
| | | } else { |
| | | if(product != null){ |
| | | product.setProductName(productModelDto.getProductName()); |
| | | productMapper.updateById(product); |
| | | } |
| | | |
| | | return productModelMapper.updateById(productModelDto); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | public int delProductModel(Long[] ids) { |
| | | List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>() |
| | | .lambda().in(SalesLedgerProduct::getProductModelId, ids)); |
| | | if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) { |
| | | |
| | | throw new RuntimeException("已经存在该产品的销售台账和采购台账"); |
| | | } |
| | | return productModelMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | |
| | |
| | | queryWrapper.eq(ProductModel::getProductId, productDto.getId()); |
| | | return productModelMapper.selectList(queryWrapper); |
| | | } |
| | | |
| | | /** |
| | | * 根据id查询产品规格分页查询 |
| | | * @param page |
| | | * @param productDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<ProductModel> modelListPage(Page page, ProductDto productDto) { |
| | | LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProductModel::getProductId, productDto.getId()); |
| | | return productModelMapper.selectPage(page, queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Boolean importProduct(MultipartFile file) { |
| | | try { |
| | | ExcelUtil<ProductModelExcelCopyDto> productModelExcelUtil = new ExcelUtil<>(ProductModelExcelCopyDto.class); |
| | | List<ProductModelExcelCopyDto> productModelList = productModelExcelUtil.importExcel(file.getInputStream()); |
| | | List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>() |
| | | .isNull(Product::getParentId)); |
| | | if(CollectionUtils.isEmpty(productList)) { |
| | | throw new RuntimeException("请先添加父级产品"); |
| | | } |
| | | if(CollectionUtils.isNotEmpty(productModelList)){ |
| | | // 2. 按产品名称分组 |
| | | Map<String, List<ProductModelExcelCopyDto>> groupedByProductName = productModelList.stream() |
| | | .collect(Collectors.groupingBy(ProductModelExcelCopyDto::getProductName)); |
| | | for (Map.Entry<String, List<ProductModelExcelCopyDto>> entry : groupedByProductName.entrySet()) { |
| | | // 根据名称查询是否存在产品 |
| | | Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>() |
| | | .eq(Product::getProductName, entry.getKey()) |
| | | .last("limit 1")); |
| | | if(product == null){ |
| | | product = new Product(); |
| | | product.setProductName(entry.getKey()); |
| | | product.setParentId(null); // 父节点ID |
| | | // 2. 插入当前节点,MyBatis会自动回填product的id属性 |
| | | productMapper.insert(product); |
| | | } |
| | | Product finalProduct = product; |
| | | entry.getValue().forEach(productModelExcelDto -> { |
| | | // 根据图纸编号查询 |
| | | ProductModel productModel = productModelMapper.selectOne(new LambdaQueryWrapper<ProductModel>() |
| | | .eq(ProductModel::getModel, productModelExcelDto.getModel()) |
| | | .last("limit 1")); |
| | | if(productModel == null){ |
| | | productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelExcelDto,productModel); |
| | | if(productModelExcelDto.getProductType() == null) { |
| | | productModel.setProductType(1); |
| | | } |
| | | productModel.setProductId(finalProduct.getId()); |
| | | productModelMapper.insert(productModel); |
| | | }else{ |
| | | BeanUtils.copyProperties(productModelExcelDto,productModel); |
| | | productModel.setProductId(finalProduct.getId()); |
| | | productModelMapper.updateById(productModel); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | // List<ProductModelExcelDto> productModelExcelDtos = OrderUtils.buildTree(productModelList); |
| | | // if(CollectionUtils.isNotEmpty(productModelExcelDtos)){ |
| | | // recursiveSaveProduct(productModelExcelDtos.get(0),null); |
| | | // } |
| | | return true; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 递归导入树形产品数据 |
| | | * @param excelDto Excel解析后的树形节点 |
| | | * @param parentId 父节点ID(顶级节点传null/0) |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) // 事务保证,失败则回滚 |
| | | public void recursiveSaveProduct(ProductModelExcelDto excelDto, Long parentId) { |
| | | // 1. 构建当前节点的Product实体 |
| | | Product product = new Product(); |
| | | product.setProductName(excelDto.getProductName()); |
| | | product.setParentId(parentId); // 父节点ID |
| | | |
| | | // 2. 插入当前节点,MyBatis会自动回填product的id属性 |
| | | productMapper.insert(product); |
| | | Long currentId = product.getId(); // 获取当前节点主键 |
| | | |
| | | // 3. 递归处理子节点 |
| | | if (excelDto.getChildren() != null && !excelDto.getChildren().isEmpty()) { |
| | | for (ProductModelExcelDto childDto : excelDto.getChildren()) { |
| | | recursiveSaveProduct(childDto, currentId); // 父ID为当前节点ID |
| | | } |
| | | } |
| | | |
| | | // 4. (可选)处理items数据(如果需要插入到关联表) |
| | | if (excelDto.getItems() != null && !excelDto.getItems().isEmpty()) { |
| | | for (ProductModelExcelItemDto item : excelDto.getItems()) { |
| | | // 插入item关联数据,关联currentId |
| | | // 构建ProductModel实体 |
| | | ProductModel productModel = new ProductModel(); |
| | | productModel.setProductId(currentId); // 关联当前产品节点ID |
| | | productModel.setModel(item.getModel()); // 从ItemDTO获取型号 |
| | | productModel.setUnit(item.getUnit()); // 从ItemDTO获取单位 |
| | | productModel.setDrawingNumber(item.getDrawingNumber()); // 图纸编号 |
| | | productModel.setProductType(item.getProductType()); |
| | | // 插入product_model表 |
| | | productModelMapper.insert(productModel); |
| | | } |
| | | } |
| | | } |
| | | } |