Browse Source

增加导出功能

master
王梦远 6 days ago
parent
commit
7835e8444c
  1. 58
      src/main/java/com/iflytop/handacid/app/common/utils/UsbDriverUtil.java
  2. 74
      src/main/java/com/iflytop/handacid/app/controller/AuditRecordController.java
  3. 11
      src/main/java/com/iflytop/handacid/app/controller/FormulationController.java
  4. 75
      src/main/java/com/iflytop/handacid/app/controller/ReceiveRecordController.java

58
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();
}
}
}

74
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<String> delete(@PathVariable Integer id) {
boolean flag = auditRecordService.removeById(id);
return flag ? Result.success("删除成功"): Result.failed("删除失败");
public Result<String> 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<String> export() {
String usbPath = UsbDriverUtil.findPath();
if(usbPath==null){
}
List<AuditRecord> 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);
}
}
}

11
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<String> delete(@PathVariable Integer id) {
boolean flag = formulationService.removeById(id);
return flag? Result.success("删除成功") : Result.failed("删除失败");
public Result<String> 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}")

75
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<String> delete(@PathVariable Integer id) {
boolean flag = receiveRecordService.removeById(id);
return flag? Result.success("删除成功") : Result.failed("删除失败");
public Result<String> 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<String> export() {
String usbPath = UsbDriverUtil.findPath();
if (usbPath == null) {
return Result.failed("请插入U盘");
}
List<ReceiveRecord> 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);
}
}
}
Loading…
Cancel
Save