检测报告管理

This commit is contained in:
hayu 2024-07-28 13:20:51 +08:00
parent 167b872ee6
commit 0032a783b7
4 changed files with 269 additions and 121 deletions

View File

@ -13,6 +13,7 @@ import com.bonus.aqgqj.basis.service.TestReportManageService;
import com.bonus.aqgqj.system.vo.EncryptedReq;
import com.bonus.aqgqj.utils.DateTimeHelper;
import com.bonus.aqgqj.utils.ServerResponse;
import com.bonus.aqgqj.utils.StringHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
@ -26,6 +27,10 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@ -36,6 +41,8 @@ import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
@ -156,23 +163,23 @@ public class TestReportManageController {
}
}
/**
* @description 一级页面下载
*/
@PostMapping("download")
// @DecryptAndVerify(decryptedClass = TestReportManageDto.class)//加解密统一管理
@LogAnnotation(operModul = "检测报告管理", operation = "下载", operDesc = "业务级事件", operType = "新增")
@PreAuthorize("@pms.hasPermission('sys:samples:dispatch')")
public ServerResponse download(EncryptedReq<TestReportManageDto> vo) {
try {
TestReportManageDto voo=new TestReportManageDto();
voo.setId(25);
return testReportManageService.download(voo);
} catch (Exception e) {
log.error(e.toString(), e);
return ServerResponse.createErroe("操作失败");
}
}
// /**
// * @description 一级页面下载
// */
// @PostMapping("download")
//// @DecryptAndVerify(decryptedClass = TestReportManageDto.class)//加解密统一管理
// @LogAnnotation(operModul = "检测报告管理", operation = "下载", operDesc = "业务级事件", operType = "新增")
// @PreAuthorize("@pms.hasPermission('sys:samples:dispatch')")
// public ServerResponse download(EncryptedReq<TestReportManageDto> vo) {
// try {
// TestReportManageDto voo=new TestReportManageDto();
// voo.setId(25);
// return testReportManageService.download(voo);
// } catch (Exception e) {
// log.error(e.toString(), e);
// return ServerResponse.createErroe("操作失败");
// }
// }
@PostMapping(value = "viewTestData")
@DecryptAndVerify(decryptedClass = TestReportManageDto.class)//加解密统一管理
@ -289,60 +296,29 @@ public class TestReportManageController {
}
@RequestMapping(value = "/export", method = RequestMethod.GET)
public void exportWorkerAttendance(HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = "D:\\poi\\";
//总列数
int all = 0;
//动态生成列数
int col = 0;
// 1.创建一个工作簿03
Workbook workbook = new HSSFWorkbook();
// 2.创建一个工作表
Sheet sheet = workbook.createSheet("统计表");
// 3.创建行第一行
Row row = sheet.createRow(0);
// 4.创建列
// (1,1) 第一行第一列的单元格
Cell cell = row.createCell(0);
cell.setCellValue("安全帽检测报告附页");
// 合并单元格从第一个到第十个单元格
sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(
0, // 起始行
0, // 结束行
0, // 起始列
all - 1 // 结束列
));
// 第二行(1,0)
Row row1 = sheet.createRow(1);
//2,1第二行第一列的单元格
Cell cell1 = row1.createCell(0);
cell1.setCellValue("报告编号");
Cell cell2 = row1.createCell(1);
cell1.setCellValue("报告编号");
// 判断文件是否存在不存在就创建
if (FileUtil.isEmpty(new File(path))) {
FileUtil.mkdir(path);
@GetMapping("/download")
public ResponseEntity<FileSystemResource> downloadFile(HttpServletRequest request) {
File tempZipFile = null;
try {
// Return the ZIP file
return testReportManageService.downloadFile(request, tempZipFile);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}finally {
// Ensure the temporary file is deleted if not already done
if (tempZipFile != null && tempZipFile.exists()) {
tempZipFile.delete();
}
}
// 5.生成一张表03版本的工作簿是以.xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + "03.xls");
// 输出
workbook.write(fileOutputStream);
// 6.关闭流
fileOutputStream.close();
System.out.println("03表生成成功");
}
private byte[] generateExcelData(String fileName) {
// Implement this method to generate Excel file data
// For now, returning empty byte array as placeholder
return new byte[0];
}
}

View File

@ -3,7 +3,12 @@ package com.bonus.aqgqj.basis.service;
import com.bonus.aqgqj.basis.entity.dto.ParamsDto;
import com.bonus.aqgqj.basis.entity.dto.TestReportManageDto;
import com.bonus.aqgqj.utils.ServerResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
@ -77,4 +82,12 @@ public interface TestReportManageService {
* @return
*/
ServerResponse download(TestReportManageDto data);
/**
* 检测报告下载
* @param request
* @param tempZipFile
* @return
*/
ResponseEntity<FileSystemResource> downloadFile(HttpServletRequest request, File tempZipFile) throws IOException;
}

View File

@ -14,12 +14,16 @@ import com.bonus.aqgqj.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -28,6 +32,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipOutputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
/**
* @description 工器具个体管理
@ -231,24 +238,65 @@ public class TestReportManageServiceImpl implements TestReportManageService {
//根据试验id设备类型是否合格部门id来查询每个检测报告
ServerResponse serverResponse = viewTestData(dto);
if (serverResponse.isSuccess()){
TestReportManageDto testVo = (TestReportManageDto) serverResponse.getData();
log.info("sj--->{}",testVo);
//将数据传给检测报告生成的方法
try {
generateReport(testVo);
} catch (Exception e){
File zipFile = new File("检测报告_"+DateTimeHelper.getNowDMS()+".zip");
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
TestReportManageDto testVo = (TestReportManageDto) serverResponse.getData();
log.info("sj--->{}",testVo);
//将数据传给检测报告生成的方法
try {
generateReport(testVo,zos);
} catch (Exception e){
e.printStackTrace();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
return null;
}
@Override
public ResponseEntity<FileSystemResource> downloadFile(HttpServletRequest request, File tempZipFile) throws IOException {
String ids = request.getParameter("ids");
if (StringHelper.isNotEmpty(ids)) {
String fileNames="检测报告_" + DateTimeHelper.getNowDMS();
tempZipFile = File.createTempFile(fileNames, ".zip");
tempZipFile.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(tempZipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
//拆分用逗号分隔的数据
String[] idsArray = ids.split(",");
for (String id : idsArray) {
TestReportManageDto dto = new TestReportManageDto();
dto.setId(Integer.parseInt(id));
//查询每条数据里面的详细数据
List<TestReportManageDto> list = getDetailsList(dto);
//查询详情里所有数据的检测报告
for (TestReportManageDto dtos : list) {
//根据试验id设备类型是否合格部门id来查询每个检测报告
ServerResponse serverResponse = viewTestData(dtos);
if (serverResponse.isSuccess()) {
TestReportManageDto testVo = (TestReportManageDto) serverResponse.getData();
log.info("sj--->{}", testVo);
generateReport(testVo, zos);
}
}
}
}
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileNames+".zip");
return new ResponseEntity<>(new FileSystemResource(tempZipFile), headers, HttpStatus.OK);
}
return null;
}
/**
* 生成检测报告
*/
public void generateReport(TestReportManageDto bean) throws IOException {
public void generateReport(TestReportManageDto bean,ZipOutputStream zos) throws IOException {
String path = "D:\\poi\\";
//先计算出总共有多少列和动态生成的有多少列
//总列数
@ -273,6 +321,26 @@ public class TestReportManageServiceImpl implements TestReportManageService {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("检测报告");
// 创建样式
XSSFCellStyle style = workbook.createCellStyle();
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置每列的宽度为20个字符的宽度
int widthInCharacters = 15;
int columnCount = totalNum;
for (int i = 0; i < columnCount; i++) {
// 1/256字符的宽度单位
sheet.setColumnWidth(i, widthInCharacters * 256);
}
//第一行
Row row1 = sheet.createRow(0);
//单元格-创建第一个头
@ -280,7 +348,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell11.setCellValue(bean.getSampleTools()+"检测报告附页");
//合并全部列
addMergedRegion(0,0,0,totalNum-1,sheet);
XSSFCellStyle style= centerStyle(workbook);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(0, 0, 0, totalNum-1), style);
cell11.setCellStyle(style);
@ -293,7 +362,10 @@ public class TestReportManageServiceImpl implements TestReportManageService {
int num21=(totalNum-4)/2;
Cell cell22 = row2.createCell(1);
addMergedRegion(1,1,1,num21,sheet);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(1, 1, 1, num21), style);
cell22.setCellValue(bean.getReportCode());
cell22.setCellStyle(style);
Cell cell23 = row2.createCell(num21+1);
cell23.setCellValue("收样日期");
@ -301,8 +373,10 @@ public class TestReportManageServiceImpl implements TestReportManageService {
Cell cell24 = row2.createCell(num21+2);
cell24.setCellValue(bean.getCollectSamplesTime());
cell24.setCellStyle(style);
addMergedRegion(1,1,num21+2,totalNum-3,sheet);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(1,1,num21+2,totalNum-3), style);
cell24.setCellStyle(style);
Cell cell25 = row2.createCell(totalNum-2);
cell25.setCellValue("样品数量");
@ -323,6 +397,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell32.setCellValue(bean.getDevTypeName());
cell32.setCellStyle(style);
addMergedRegion(2,2,1,totalNum-1,sheet);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(2,2,1,totalNum-1), style);
@ -337,6 +413,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell42.setCellValue(yj);
cell42.setCellStyle(style);
addMergedRegion(3,3,1,totalNum-1,sheet);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(3,3,1,totalNum-1), style);
//第5行 //第六行
Row row5= sheet.createRow(4);
@ -346,16 +424,21 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell51.setCellValue("序号");
cell51.setCellStyle(style);
addMergedRegion(4,5,0,0,sheet);
// 应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(4,5,0,0), style);
Cell cell52 = row5.createCell(1);
cell52.setCellValue("样品编号");
cell52.setCellStyle(style);
addMergedRegion(4,5,1,1,sheet);
//
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(4,5,1,1), style);
Cell cell53 = row5.createCell(2);
cell53.setCellValue("样品信息");
cell53.setCellStyle(style);
addMergedRegion(4,4,2,5,sheet);//
addMergedRegion(4,4,2,5,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(4,4,2,5), style);
//第六行固定数据
Cell cell62= row6.createCell(2);
cell62.setCellStyle(style);
@ -381,6 +464,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell54.setCellValue(vo.getExperTypeName());
cell54.setCellStyle(style);
addMergedRegion(4,4,num.get(),num.get()+rowNum-1,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(4,4,num.get(),num.get()+rowNum-1), style);
num.addAndGet(rowNum-1);
childList.forEach(child->{
rowNums.set(rowNums.get() + 1);
@ -390,19 +475,27 @@ public class TestReportManageServiceImpl implements TestReportManageService {
});
});
AtomicInteger row= new AtomicInteger(5);
AtomicInteger cellNum= new AtomicInteger(0);
if(ListHelper.isNotEmpty(devList)){
devList.forEach(dev->{
AtomicInteger row = new AtomicInteger(5);
AtomicInteger cellNum = new AtomicInteger(0);
AtomicInteger serialNumber = new AtomicInteger(1); // 用于追踪序号
if (ListHelper.isNotEmpty(devList)) {
devList.forEach(dev -> {
row.set(row.get() + 1);
Row row7= sheet.createRow(row.get());
cellNum.set(0); // 每次处理新行时重置 cellNum
Row row7 = sheet.createRow(row.get());
// 创建序号列
Cell row71 = row7.createCell(cellNum.get());
cellNum.set(cellNum.get() + 1);
row71.setCellValue(cellNum.get());
row71.setCellValue(serialNumber.get()); // 设置序号
row71.setCellStyle(style);
cellNum.set(cellNum.get() + 1);
// 更新序号
serialNumber.set(serialNumber.get() + 1);
// 处理其他列
Cell row72 = row7.createCell(cellNum.get());
row72.setCellValue(dev.getCustomerCode());
row72.setCellStyle(style);
@ -418,27 +511,25 @@ public class TestReportManageServiceImpl implements TestReportManageService {
row74.setCellStyle(style);
cellNum.set(cellNum.get() + 1);
Cell row75= row7.createCell(cellNum.get());
Cell row75 = row7.createCell(cellNum.get());
row75.setCellValue(dev.getManufactureDate());
row75.setCellStyle(style);
cellNum.set(cellNum.get() + 1);
Cell row76= row7.createCell(cellNum.get());
Cell row76 = row7.createCell(cellNum.get());
row76.setCellValue(dev.getDevModule());
row76.setCellStyle(style);
cellNum.set(cellNum.get() + 1);
String val=dev.getDevData();
JSONArray jsonArray=JSON.parseArray(val);
String val = dev.getDevData();
JSONArray jsonArray = JSON.parseArray(val);
jsonArray.forEach(json->{
Cell row77= row7.createCell(cellNum.get());
jsonArray.forEach(json -> {
Cell row77 = row7.createCell(cellNum.get());
row77.setCellValue(json.toString());
row77.setCellStyle(style);
cellNum.set(cellNum.get() + 1);
});
});
}
//
@ -452,6 +543,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell82.setCellValue(bean.getExperStand());
cell82.setCellStyle(style);
addMergedRegion(row.get(),row.get(),1,totalNum-1,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(row.get(),row.get(),1,totalNum-1), style);
row.set(row.get() + 1);
@ -465,6 +558,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell92.setCellValue(bean.getExperConclu());
cell92.setCellStyle(style);
addMergedRegion(row.get(),row.get(),1,totalNum-1,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(row.get(),row.get(),1,totalNum-1), style);
row.set(row.get() + 1);
@ -478,6 +573,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell102.setCellValue(bean.getRemarks());
cell102.setCellStyle(style);
addMergedRegion(row.get(),row.get(),1,totalNum-1,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(row.get(),row.get(),1,totalNum-1), style);
row.set(row.get() + 1);
@ -492,6 +589,8 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell112.setCellStyle(style);
int num112=(totalNum-2)/2;
addMergedRegion(row.get(),row.get(),1,num112,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(row.get(),row.get(),1,num112), style);
Cell cell113 = row11.createCell(num112+1);
cell113.setCellValue("下次检测日期");
@ -501,25 +600,34 @@ public class TestReportManageServiceImpl implements TestReportManageService {
cell114.setCellValue(bean.getNextExperTime());
cell114.setCellStyle(style);
addMergedRegion(row.get(),row.get(),num112+2,totalNum-1,sheet);
//应用边框到合并区域
applyBordersToMergedRegion(sheet, new CellRangeAddress(row.get(),row.get(),num112+2,totalNum-1), style);
// 判断文件是否存在不存在就创建
if (FileUtil.isEmpty(new File(path))) {
FileUtil.mkdir(path);
// 保存到 ZIP 文件
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
if (StringHelper.isNotEmpty(bean.getReportCode())){
//将字符串中ML/BG-去除
bean.setReportCode(bean.getReportCode().replace("ML/BG-",""));
}
double math=Math.random();
// 5.生成一张表03版本的工作簿是以.xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + math+".xls");
// 输出
workbook.write(fileOutputStream);
// 6.关闭流
fileOutputStream.close();
System.out.println("03表生成成功");
ZipEntry entry = new ZipEntry("检测报告_" + bean.getReportCode() + "_"+DateTimeHelper.getNowDMS()+".xlsx");
zos.putNextEntry(entry);
zos.write(baos.toByteArray());
zos.closeEntry();
workbook.close();
}
/**
* 单元格样式--居中
*/
public static XSSFCellStyle centerStyle(XSSFWorkbook workbook){
XSSFCellStyle commonStyle = workbook.createCellStyle();
commonStyle.setBorderTop(BorderStyle.THIN);
commonStyle.setBorderBottom(BorderStyle.THIN);
commonStyle.setBorderLeft(BorderStyle.THIN);
commonStyle.setBorderRight(BorderStyle.THIN);
// 左右居中
commonStyle.setAlignment(HorizontalAlignment.CENTER);
// 上下居中
@ -530,6 +638,35 @@ public class TestReportManageServiceImpl implements TestReportManageService {
return commonStyle;
}
/** 应用边框到合并区域
*
* @param sheet
* @param range
* @param style
*/
// 应用边框到合并区域
private static void applyBordersToMergedRegion(Sheet sheet, CellRangeAddress range, CellStyle style) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
// 遍历合并区域的所有单元格并设置边框
for (int row = firstRow; row <= lastRow; row++) {
Row xssfRow = sheet.getRow(row);
if (xssfRow == null) {
xssfRow = sheet.createRow(row);
}
for (int col = firstCol; col <= lastCol; col++) {
Cell cell = xssfRow.getCell(col);
if (cell == null) {
cell = xssfRow.createCell(col);
}
cell.setCellStyle(style);
}
}
}
/**
* 合并单元格
* @param startRow 开始行

View File

@ -248,18 +248,40 @@ function downloadClick() {
if (ids==='') {
return layer.msg('请选择需要下载的数据', {icon: 7})
}
let loadingMsg = layer.msg('数据下载中,请稍候...', {icon: 16, scrollbar: false, time: 0});
let url = dataUrl + '/testReport/download';
let params = {
'ids': ids
}
params = {
encryptedData: encryptCBC(JSON.stringify(params))
}
ajaxRequest(url, "POST", params, true, function () {
layer.close(loadingMsg);
window.location.href = dataUrl + '/testReport/download?ids=' + ids;
// 显示提示
let loadingMsg = layer.msg('数据下载中,请稍候...', {
icon: 16,
scrollbar: false,
time: 0, // time: 0 表示提示框不会自动关闭
offset: ['45%', '57%']
});
// 获取 token
let tokens = localStorage.getItem("token");
// 发起下载请求
fetch(dataUrl + '/testReport/download?ids=' + ids + '&token=' + tokens)
.then(response => response.blob())
.then(blob => {
// 生成下载链接并触发下载
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = '检测报告.zip'; // 文件名
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
// 下载完成后关闭提示
layer.close(loadingMsg);
})
.catch(error => {
// 处理错误并关闭提示
console.error('Error downloading file:', error);
layer.close(loadingMsg);
});
}
/**