| | |
| | | import com.ruoyi.basic.vo.ProductModelVo; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | 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; |
| | |
| | | } |
| | | |
| | | @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)); |
| | | } |
| | | |
| | | } |