| | |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.service.IProductService; |
| | | import com.ruoyi.basic.vo.ProductModelVo; |
| | | 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.AjaxResult; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | |
| | | |
| | | @Override |
| | | public int addOrEditProduct(ProductDto productDto) { |
| | | if (ObjectUtils.isEmpty(productDto.getParentId())) { |
| | | throw new IllegalArgumentException("请选择父节点"); |
| | | String productName = StringUtils.trim(productDto.getProductName()); |
| | | if (StringUtils.isEmpty(productName)) { |
| | | throw new IllegalArgumentException("产品名称不能为空"); |
| | | } |
| | | // 父节点为空表示新增一级大类 |
| | | if (ObjectUtils.isEmpty(productDto.getParentId())) { |
| | | if (productDto.getId() != null) { |
| | | throw new IllegalArgumentException("请选择父节点"); |
| | | } |
| | | productDto.setParentId(null); |
| | | } |
| | | productDto.setProductName(productName); |
| | | checkProductNameUnique(productDto.getParentId(), productName, productDto.getId()); |
| | | if (productDto.getId() == null) { |
| | | // 新增产品逻辑 |
| | | if (productDto.getParentId() == null) { |
| | | // 若未指定父节点,默认为根节点(parentId 设为 null) |
| | | productDto.setParentId(null); |
| | | } else { |
| | | // 检查父节点是否存在(可选,根据业务需求) |
| | | // 检查父节点是否存在(一级大类无父节点) |
| | | if (productDto.getParentId() != null) { |
| | | Product parent = productMapper.selectById(productDto.getParentId()); |
| | | if (parent == null) { |
| | | throw new IllegalArgumentException("父节点不存在,无法添加子产品"); |
| | |
| | | } |
| | | } |
| | | |
| | | private void checkProductNameUnique(Long parentId, String productName, Long currentId) { |
| | | LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (parentId == null) { |
| | | queryWrapper.isNull(Product::getParentId); |
| | | } else { |
| | | queryWrapper.eq(Product::getParentId, parentId); |
| | | } |
| | | queryWrapper.eq(Product::getProductName, productName) |
| | | .ne(currentId != null, Product::getId, currentId) |
| | | .last("limit 1"); |
| | | Product duplicateProduct = productMapper.selectOne(queryWrapper); |
| | | if (duplicateProduct != null) { |
| | | throw new IllegalArgumentException("对应的" + productName + "已经存在"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int delProductByIds(Long[] ids) { |
| | | // 存在子类时不允许删除 |
| | | LambdaQueryWrapper<Product> childWrapper = new LambdaQueryWrapper<>(); |
| | | childWrapper.in(Product::getParentId, ids).last("limit 1"); |
| | | Product childProduct = productMapper.selectOne(childWrapper); |
| | | if (childProduct != null) { |
| | | throw new IllegalArgumentException("该大类下存在子类,不能删除"); |
| | | } |
| | | |
| | | // 大类下存在规格型号数据时不允许删除 |
| | | for (Product product : productMapper.selectBatchIds(Arrays.asList(ids))) { |
| | | if (product.getParentId() != null) { |
| | | continue; |
| | | } |
| | | LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>(); |
| | | modelWrapper.eq(ProductModel::getProductId, product.getId()).last("limit 1"); |
| | | if (productModelMapper.selectOne(modelWrapper) != null) { |
| | | throw new IllegalArgumentException("该大类下存在数据,不能删除"); |
| | | } |
| | | } |
| | | |
| | | // 1. 删除子表 product_model 中关联的数据 |
| | | LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.in(ProductModel::getProductId, ids); |
| | |
| | | return deleteCount; |
| | | } |
| | | |
| | | /** 台账手工录入的产品大类/规格型号统一挂到这个大类下 */ |
| | | private static final String TOP_CATEGORY_NAME = "成品"; |
| | | |
| | | @Override |
| | | public ProductModel ensureProductHierarchy(String productName, String modelName, String unit) { |
| | | productName = StringUtils.trim(productName); |
| | | modelName = StringUtils.trim(modelName); |
| | | if (StringUtils.isEmpty(productName) || StringUtils.isEmpty(modelName)) { |
| | | return null; |
| | | } |
| | | |
| | | // 一级大类:固定「成品」(不存在则建) |
| | | LambdaQueryWrapper<Product> categoryWrapper = new LambdaQueryWrapper<>(); |
| | | categoryWrapper.isNull(Product::getParentId) |
| | | .eq(Product::getProductName, TOP_CATEGORY_NAME) |
| | | .last("limit 1"); |
| | | Product category = productMapper.selectOne(categoryWrapper); |
| | | if (category == null) { |
| | | category = new Product(); |
| | | category.setProductName(TOP_CATEGORY_NAME); |
| | | productMapper.insert(category); |
| | | } |
| | | |
| | | // 二级:产品大类(成品下的子类) |
| | | LambdaQueryWrapper<Product> nameWrapper = new LambdaQueryWrapper<>(); |
| | | nameWrapper.eq(Product::getParentId, category.getId()) |
| | | .eq(Product::getProductName, productName) |
| | | .last("limit 1"); |
| | | Product nameProduct = productMapper.selectOne(nameWrapper); |
| | | if (nameProduct == null) { |
| | | nameProduct = new Product(); |
| | | nameProduct.setParentId(category.getId()); |
| | | nameProduct.setProductName(productName); |
| | | productMapper.insert(nameProduct); |
| | | } |
| | | |
| | | // 三级:规格型号(产品大类下的规格) |
| | | LambdaQueryWrapper<ProductModel> modelWrapper = new LambdaQueryWrapper<>(); |
| | | modelWrapper.eq(ProductModel::getProductId, nameProduct.getId()) |
| | | .eq(ProductModel::getModel, modelName) |
| | | .last("limit 1"); |
| | | ProductModel productModel = productModelMapper.selectOne(modelWrapper); |
| | | if (productModel == null) { |
| | | productModel = new ProductModel(); |
| | | productModel.setProductId(nameProduct.getId()); |
| | | productModel.setModel(modelName); |
| | | productModel.setUnit(unit); |
| | | productModelMapper.insert(productModel); |
| | | } |
| | | return productModel; |
| | | } |
| | | |
| | | } |