From 7835e8444c81e6728d90c86426a2f18154f98f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A2=A6=E8=BF=9C?= <1063331231@qq.com> Date: Tue, 29 Jul 2025 17:05:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handacid/app/common/utils/UsbDriverUtil.java | 58 +++++++++++++++++ .../app/controller/AuditRecordController.java | 74 +++++++++++++++++++-- .../app/controller/FormulationController.java | 11 ++-- .../app/controller/ReceiveRecordController.java | 75 ++++++++++++++++++++-- 4 files changed, 206 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/iflytop/handacid/app/common/utils/UsbDriverUtil.java diff --git a/src/main/java/com/iflytop/handacid/app/common/utils/UsbDriverUtil.java b/src/main/java/com/iflytop/handacid/app/common/utils/UsbDriverUtil.java new file mode 100644 index 0000000..6eb59d2 --- /dev/null +++ b/src/main/java/com/iflytop/handacid/app/common/utils/UsbDriverUtil.java @@ -0,0 +1,58 @@ +package com.iflytop.handacid.app.common.utils; + +import java.io.File; + +public class UsbDriverUtil { + /** + * 查找 win U盘路径 + * @return U盘路径,如果找不到返回null + */ + public static String findWinPath() { + // Windows系统查找可移动驱动器 + File[] roots = File.listRoots(); + for (File root : roots) { + // 检查是否为可移动驱动器(U盘) + if (root.getTotalSpace() > 0 && root.getTotalSpace() < 64L * 1024 * 1024 * 1024) { // 小于64GB认为是U盘 + String rootPath = root.getAbsolutePath(); + // 检查是否可写 + if (root.canWrite()) { + return rootPath; + } + } + } + return null; + } + + /** + * 查找Linux U盘路径 + * @return U盘路径,如果找不到返回null + */ + public static String findLinuxPath(){ + // Linux/Mac系统查找/media或/run/media下的可移动设备 + String[] mediaPaths = {"/media", "/run/media"}; + for (String mediaPath : mediaPaths) { + File mediaDir = new File(mediaPath); + if (mediaDir.exists() && mediaDir.isDirectory()) { + File[] subDirs = mediaDir.listFiles(); + if (subDirs != null) { + for (File subDir : subDirs) { + if (subDir.isDirectory() && subDir.canWrite()) { + return subDir.getAbsolutePath(); + } + } + } + } + } + return null; + } + + public static String findPath() { + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("win")) { + return findWinPath(); + } else { + return findLinuxPath(); + } + } + +} diff --git a/src/main/java/com/iflytop/handacid/app/controller/AuditRecordController.java b/src/main/java/com/iflytop/handacid/app/controller/AuditRecordController.java index 01a7a96..1a17d5c 100644 --- a/src/main/java/com/iflytop/handacid/app/controller/AuditRecordController.java +++ b/src/main/java/com/iflytop/handacid/app/controller/AuditRecordController.java @@ -1,6 +1,7 @@ package com.iflytop.handacid.app.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.iflytop.handacid.app.common.utils.UsbDriverUtil; import com.iflytop.handacid.common.base.BasePageQuery; import com.iflytop.handacid.common.model.entity.AuditRecord; import com.iflytop.handacid.common.model.entity.Formulation; @@ -11,9 +12,20 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; import java.util.List; /** * 审计 @@ -59,11 +71,65 @@ public class AuditRecordController { return flag ? Result.success("修改成功"): Result.failed("修改失败"); } - @DeleteMapping("/{id}") + @DeleteMapping("/{ids}") @Operation(summary = "删除记录") - public Result delete(@PathVariable Integer id) { - boolean flag = auditRecordService.removeById(id); - return flag ? Result.success("删除成功"): Result.failed("删除失败"); + public Result delete(@PathVariable String ids) { + boolean success = auditRecordService.removeBatchByIds( + Arrays.stream(ids.split(",")).map(Long::valueOf).toList() + ); + return success ? Result.success("删除成功"): Result.failed("删除失败"); + } + + @GetMapping("/export") + @Operation(summary = "导出记录") + public Result export() { + String usbPath = UsbDriverUtil.findPath(); + if(usbPath==null){ + } + List list= auditRecordService.list(); + if(list.isEmpty()){ + return Result.failed("无审计记录"); + } + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("审计记录"); + Row headerRow = sheet.createRow(0); + String[] headers = {"ID", "用户名", "溶液名称", "浓度", "通道ID", "体积", "创建时间", "更新时间"}; + for (int i = 0; i < headers.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(headers[i]); + } + // 填充数据 + for (int i = 0; i < list.size(); i++) { + AuditRecord record = list.get(i); + Row row = sheet.createRow(i + 1); + row.createCell(0).setCellValue(record.getId()); + row.createCell(1).setCellValue(record.getUserName()); + row.createCell(2).setCellValue(record.getSolutionName()); + row.createCell(3).setCellValue(record.getConcentration()); + row.createCell(4).setCellValue(record.getChannelId() != null ? record.getChannelId() : 0); + row.createCell(5).setCellValue(record.getVolume()); + row.createCell(6).setCellValue(record.getCreateTime() != null ? record.getCreateTime().toString() : ""); + row.createCell(7).setCellValue(record.getUpdateTime() != null ? record.getUpdateTime().toString() : ""); + } + // 自动调整列宽 + for (int i = 0; i < headers.length; i++) { + sheet.autoSizeColumn(i); + } + // 生成文件名 + String fileName = "审计记录_" + System.currentTimeMillis() + ".xlsx"; + // 尝试保存到U盘 + String filePath = usbPath + File.separator + fileName; + try (FileOutputStream fileOut = new FileOutputStream(filePath)) { + workbook.write(fileOut); + workbook.close(); + return Result.success("导出成功,文件已保存到: " + filePath); + } catch (IOException e) { + log.error(e.getMessage()); + return Result.failed("导出失败"+e.getMessage()); + //throw new RuntimeException(e); + } + + } } diff --git a/src/main/java/com/iflytop/handacid/app/controller/FormulationController.java b/src/main/java/com/iflytop/handacid/app/controller/FormulationController.java index 67b171b..badfe37 100644 --- a/src/main/java/com/iflytop/handacid/app/controller/FormulationController.java +++ b/src/main/java/com/iflytop/handacid/app/controller/FormulationController.java @@ -14,6 +14,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -54,11 +55,13 @@ public class FormulationController { boolean flag = formulationService.save(formulation); return flag? Result.success("添加成功") : Result.failed("添加失败"); } - @DeleteMapping("/{id}") + @DeleteMapping("/{ids}") @Operation(summary = "删除配方") - public Result delete(@PathVariable Integer id) { - boolean flag = formulationService.removeById(id); - return flag? Result.success("删除成功") : Result.failed("删除失败"); + public Result delete(@PathVariable String ids) { + boolean success = formulationService.removeBatchByIds( + Arrays.stream(ids.split(",")).map(Long::valueOf).toList() + ); + return success? Result.success("删除成功") : Result.failed("删除失败"); } @GetMapping("/concentration/{id}") diff --git a/src/main/java/com/iflytop/handacid/app/controller/ReceiveRecordController.java b/src/main/java/com/iflytop/handacid/app/controller/ReceiveRecordController.java index c372e2c..54c5252 100644 --- a/src/main/java/com/iflytop/handacid/app/controller/ReceiveRecordController.java +++ b/src/main/java/com/iflytop/handacid/app/controller/ReceiveRecordController.java @@ -1,7 +1,9 @@ package com.iflytop.handacid.app.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.iflytop.handacid.app.common.utils.UsbDriverUtil; import com.iflytop.handacid.common.base.BasePageQuery; +import com.iflytop.handacid.common.model.entity.AuditRecord; import com.iflytop.handacid.common.model.entity.ReceiveRecord; import com.iflytop.handacid.common.result.PageResult; import com.iflytop.handacid.common.result.Result; @@ -10,9 +12,19 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; import java.util.List; /** * 领取 @@ -58,11 +70,66 @@ public class ReceiveRecordController { return flag? Result.success("修改成功") : Result.failed("修改失败"); } - @DeleteMapping("/{id}") + @DeleteMapping("/{ids}") @Operation(summary = "删除记录") - public Result delete(@PathVariable Integer id) { - boolean flag = receiveRecordService.removeById(id); - return flag? Result.success("删除成功") : Result.failed("删除失败"); + public Result delete(@PathVariable String ids) { + boolean success = receiveRecordService.removeBatchByIds( + Arrays.stream(ids.split(",")).map(Long::valueOf).toList() + ); + return success? Result.success("删除成功") : Result.failed("删除失败"); + } + + @GetMapping("/export") + @Operation(summary = "导出记录") + public Result export() { + String usbPath = UsbDriverUtil.findPath(); + if (usbPath == null) { + return Result.failed("请插入U盘"); + } + List list = receiveRecordService.list(); + if (list.isEmpty()) { + return Result.failed("无领取记录"); + } + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("领取记录"); + Row headerRow = sheet.createRow(0); + String[] headers = {"ID", "发放人", "领取人", "酸液名称", "酸液浓度", "通道", "领取量", "创建时间", "更新时间"}; + for (int i = 0; i < headers.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(headers[i]); + } + // 填充数据 + for (int i = 0; i < list.size(); i++) { + ReceiveRecord record = list.get(i); + Row row = sheet.createRow(i + 1); + row.createCell(0).setCellValue(record.getId()); + row.createCell(1).setCellValue(record.getUserName()); + row.createCell(2).setCellValue(record.getReceiver()); + row.createCell(3).setCellValue(record.getSolutionName()); + row.createCell(4).setCellValue(record.getConcentration()); + row.createCell(5).setCellValue(record.getChannelId()); + row.createCell(6).setCellValue(record.getVolume()); + row.createCell(7).setCellValue(record.getCreateTime() != null ? record.getCreateTime().toString() : ""); + row.createCell(8).setCellValue(record.getUpdateTime() != null ? record.getUpdateTime().toString() : ""); + } + // 自动调整列宽 + for (int i = 0; i < headers.length; i++) { + sheet.autoSizeColumn(i); + } + // 生成文件名 + String fileName = "领取记录_" + System.currentTimeMillis() + ".xlsx"; + // 尝试保存到U盘 + String filePath = usbPath + File.separator + fileName; + try (FileOutputStream fileOut = new FileOutputStream(filePath)) { + workbook.write(fileOut); + workbook.close(); + return Result.success("导出成功,文件已保存到: " + filePath); + } catch (IOException e) { + log.error(e.getMessage()); + return Result.failed("导出失败" + e.getMessage()); + //throw new RuntimeException(e); + } + } }