| | |
| | | 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 jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | @Slf4j |
| | | public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService { |
| | | |
| | | private ProductMapper productMapper; |
| | |
| | | if (ObjectUtils.isEmpty(productDto.getParentId())) { |
| | | throw new IllegalArgumentException("请选择父节点"); |
| | | } |
| | | String productName = StringUtils.trim(productDto.getProductName()); |
| | | if (StringUtils.isEmpty(productName)) { |
| | | throw new IllegalArgumentException("产品名称不能为空"); |
| | | } |
| | | productDto.setProductName(productName); |
| | | checkProductNameUnique(productDto.getParentId(), productName, productDto.getId()); |
| | | if (productDto.getId() == null) { |
| | | // 新增产品逻辑 |
| | | if (productDto.getParentId() == null) { |
| | | // 若未指定父节点,默认为根节点(parentId 设为 null) |
| | | productDto.setParentId(null); |
| | | } else { |
| | | // 检查父节点是否存在(可选,根据业务需求) |
| | | Product parent = productMapper.selectById(productDto.getParentId()); |
| | | if (parent == null) { |
| | | throw new IllegalArgumentException("父节点不存在,无法添加子产品"); |
| | | } |
| | | // 检查父节点是否存在(可选,根据业务需求) |
| | | Product parent = productMapper.selectById(productDto.getParentId()); |
| | | if (parent == null) { |
| | | throw new IllegalArgumentException("父节点不存在,无法添加子产品"); |
| | | } |
| | | return productMapper.insert(productDto); |
| | | } else { |
| | |
| | | } |
| | | } |
| | | |
| | | private void checkProductNameUnique(Long parentId, String productName, Long currentId) { |
| | | LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Product::getParentId, parentId) |
| | | .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 void exportImportTemplate(HttpServletResponse response) { |
| | | String templatePath = "static/产品导入模板.xlsx"; |
| | | try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath)) { |
| | | if (inputStream == null) { |
| | | throw new FileNotFoundException("模板文件不存在:" + templatePath); |
| | | } |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | String fileName = URLEncoder.encode("产品导入模板.xlsx", "utf-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName); |
| | | try (OutputStream outputStream = response.getOutputStream()) { |
| | | byte[] buffer = new byte[1024]; |
| | | int len; |
| | | while ((len = inputStream.read(buffer)) > 0) { |
| | | outputStream.write(buffer, 0, len); |
| | | } |
| | | outputStream.flush(); |
| | | } |
| | | } catch (IOException e) { |
| | | log.error("导出产品导入模板失败", e); |
| | | try { |
| | | response.getWriter().write("模板导出失败:" + e.getMessage()); |
| | | } catch (IOException ex) { |
| | | log.error("响应输出错误", ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int delProductByIds(Long[] ids) { |
| | | // 1. 删除子表 product_model 中关联的数据 |
| | | LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.in(ProductModel::getProductId, ids); |
| | | productModelMapper.delete(queryWrapper); |
| | | |
| | | // 2. 删除主表 product 数据 |
| | | int deleteCount = productMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | |
| | | return deleteCount; |
| | | return productMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | |
| | | } |