|
@@ -0,0 +1,377 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.aspose.cells.*;
|
|
|
+import com.aspose.cells.Picture;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.zxing.BarcodeFormat;
|
|
|
+import com.google.zxing.EncodeHintType;
|
|
|
+import com.google.zxing.MultiFormatWriter;
|
|
|
+import com.google.zxing.WriterException;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
+import com.google.zxing.common.BitMatrix;
|
|
|
+import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
+import com.rongwei.bscommon.sys.dao.ZhcxSeaBindProofDao;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxSeaBindProofDetalService;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxSeaBindProofService;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxSeaBindProofDetalDo;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxSeaBindProofDo;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.aspose.ExcelUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.utils.ExcelHelpUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.file.service.SysFileItemService;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
+import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import com.rongwei.bscommon.sys.utils.QRCodeGenerator;
|
|
|
+@Service
|
|
|
+public class ZhcxSeaBindProofServiceImpl extends ServiceImpl<ZhcxSeaBindProofDao, ZhcxSeaBindProofDo> implements ZhcxSeaBindProofService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysFileItemService sysFileItemService;
|
|
|
+ @Autowired
|
|
|
+ private ExcelUtils excelUtils;
|
|
|
+ @Autowired
|
|
|
+ private ZhcxSeaBindProofDetalService zhcxSeaBindProofDetalService;
|
|
|
+ @Value("${hb-provider.url}")
|
|
|
+ private String PreviewUrl;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void seabingdownloadExcel(HttpServletResponse response, ZhcxSeaBindProofDo zhcxSeaBindProofDo) throws Exception {
|
|
|
+ String id = zhcxSeaBindProofDo.getId();
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ throw new CustomException("参数异常,请联系管理员排查");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取模板文件
|
|
|
+ SysFileItemDo templateFile = sysFileItemService.getById("71fee567a23e410fb0210362e5b58017");
|
|
|
+
|
|
|
+ if (!excelUtils.GetLicense()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载工作簿
|
|
|
+ Workbook wb = ExcelHelpUtils.getWorkbook(templateFile.getFullpath());
|
|
|
+ Worksheet sheet0 = wb.getWorksheets().get(0);
|
|
|
+ Cells cells = sheet0.getCells();
|
|
|
+
|
|
|
+ // 要生成二维码的内容
|
|
|
+// String text = "http://61.177.40.178:4300/mlApi/qualityInspection/zhcxSeaBindProof/previewSeaExcel/"+id;
|
|
|
+ String text = PreviewUrl + "/mlApi/qualityInspection/zhcxSeaBindProof/previewSeaExcel/"+id;
|
|
|
+ // 设置二维码的参数
|
|
|
+ int width = 100; // 宽度
|
|
|
+ int height = 100; // 高度
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ BufferedImage image1 =QRCodeGenerator.generateQRCode(text,width,height);
|
|
|
+ // 保存二维码图片到文件
|
|
|
+ File outputFile = new File("QRCode.png");
|
|
|
+ ImageIO.write(image1, "PNG", outputFile);
|
|
|
+
|
|
|
+ PictureCollection pictures1 = sheet0.getPictures();
|
|
|
+
|
|
|
+// 检查第二张图片是否存在
|
|
|
+ if (pictures1.getCount() > 1) {
|
|
|
+ pictures1.removeAt(1); // 删除第二张图片
|
|
|
+ }
|
|
|
+
|
|
|
+ int pictureIndex = pictures1.add(1, 6, outputFile.getAbsolutePath());
|
|
|
+ Picture picture = pictures1.get(pictureIndex);
|
|
|
+
|
|
|
+ // 设置图片的精确位置(偏移)
|
|
|
+ picture.setUpperDeltaX(350);
|
|
|
+ picture.setUpperDeltaY(0);
|
|
|
+ // 设置插入图片的显示大小
|
|
|
+ int newWidth = 80; // 设置图片显示的宽度
|
|
|
+ int newHeight = 80; // 设置图片显示的高度
|
|
|
+ picture.setWidth(newWidth);
|
|
|
+ picture.setHeight(newHeight);
|
|
|
+// pictures1.add(1, 6, outputFile.getAbsolutePath());
|
|
|
+
|
|
|
+
|
|
|
+ } catch (WriterException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> map = this.getMap(Wrappers.<ZhcxSeaBindProofDo>lambdaQuery()
|
|
|
+ .eq(ZhcxSeaBindProofDo::getId, id)
|
|
|
+ .eq(ZhcxSeaBindProofDo::getDeleted, "0"));
|
|
|
+ List<Map<String, Object>> map2 = zhcxSeaBindProofDetalService.listMaps(
|
|
|
+ Wrappers.<ZhcxSeaBindProofDetalDo>lambdaQuery()
|
|
|
+ .eq(ZhcxSeaBindProofDetalDo::getPid, id)
|
|
|
+ .eq(ZhcxSeaBindProofDetalDo::getDeleted, "0")
|
|
|
+ );
|
|
|
+
|
|
|
+ // 根据 map2 的长度来动态决定新增多少行
|
|
|
+ int numRowsToInsert = map2.size() - 6;
|
|
|
+
|
|
|
+ if (numRowsToInsert < 0) {
|
|
|
+ numRowsToInsert = 0; // 如果 map2 的长度小于 7,不插入任何行
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入多行:在第 7 行开始插入多行
|
|
|
+ int insertRowIndex = 7; // 插入行的位置,7 表示插入到第 7 行之后
|
|
|
+
|
|
|
+ for (int i = 0; i < numRowsToInsert; i++) {
|
|
|
+ // 插入一行
|
|
|
+ cells.insertRow(insertRowIndex + i);
|
|
|
+
|
|
|
+ // 复制上一行的格式到新插入的行
|
|
|
+ // 这里的 insertRowIndex - 1 是上一行的索引,insertRowIndex + i 是当前插入行的索引
|
|
|
+ cells.copyRow(cells, insertRowIndex + i - 1, insertRowIndex + i);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < map2.size(); i++) {
|
|
|
+ Map<String, Object> rowData = map2.get(i);
|
|
|
+
|
|
|
+ // 获取当前行号,插入时填充的数据行
|
|
|
+ int currentRowIndex = insertRowIndex + i - 1;
|
|
|
+
|
|
|
+ // 填充每个占位符的值
|
|
|
+ cells.get(currentRowIndex, 1).setValue(rowData.get("ITEMS")); // 填充 ITEMS 列
|
|
|
+ cells.get(currentRowIndex, 2).setValue(rowData.get("CHECKRESULT")); // 填充 CHECKRESULT 列
|
|
|
+ cells.get(currentRowIndex, 4).setValue(rowData.get("QCINSPECTOR")); // 填充 QCINSPECTOR 列
|
|
|
+// cells.get(currentRowIndex, 6).setValue(rowData.get("DATA"));
|
|
|
+ // 获取 CHECKDATE 并格式化为 "yyyy-MM-dd"
|
|
|
+ Object checkDateObj = rowData.get("DATE");
|
|
|
+ if (checkDateObj instanceof Date) {
|
|
|
+ SimpleDateFormat rq = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String formattedDate = rq.format((Date) checkDateObj); // 格式化日期
|
|
|
+ cells.get(currentRowIndex, 6).setValue(formattedDate); // 填充 CHECKDATE 列
|
|
|
+ } else {
|
|
|
+ cells.get(currentRowIndex, 6).setValue(""); // 如果 CHECKDATE 不是日期,填充为空字符串
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int row = 0; row < cells.getMaxDataRow() + 1; row++) {
|
|
|
+ for (int col = 0; col < cells.getMaxDataColumn() + 1; col++) {
|
|
|
+ Cell cell = cells.get(row, col);
|
|
|
+ String cellValue = cell.getStringValue();
|
|
|
+ // 检查占位符并替换对应的数据
|
|
|
+ if (cellValue.startsWith("${") && cellValue.endsWith("}")) {
|
|
|
+ String key = cellValue.substring(2, cellValue.length() - 1);
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value != null) {
|
|
|
+ // 将占位符替换为实际值
|
|
|
+ if(key.equals("CREATEDATE")){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String format = sdf.format((Date) value);
|
|
|
+ cell.putValue(format);
|
|
|
+ } else {
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cell.putValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 设置响应头,准备将 Excel 文件输出给客户端
|
|
|
+ response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=测试下载");
|
|
|
+
|
|
|
+ // 将模板内容输出到响应流
|
|
|
+ wb.save(response.getOutputStream(), SaveFormat.XLSX);
|
|
|
+ wb.dispose(); // 释放工作簿资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理模板失败:{}", e);
|
|
|
+ throw new CustomException("处理模板失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void seabingPreviewExcel(HttpServletResponse response, ZhcxSeaBindProofDo zhcxSeaBindProofDo) throws Exception {
|
|
|
+ String id = zhcxSeaBindProofDo.getId();
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ throw new CustomException("参数异常,请联系管理员排查");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取模板文件
|
|
|
+ SysFileItemDo templateFile = sysFileItemService.getById("71fee567a23e410fb0210362e5b58017");
|
|
|
+
|
|
|
+ if (!excelUtils.GetLicense()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载工作簿
|
|
|
+ Workbook wb = ExcelHelpUtils.getWorkbook(templateFile.getFullpath());
|
|
|
+ Worksheet sheet0 = wb.getWorksheets().get(0);
|
|
|
+ Cells cells = sheet0.getCells();
|
|
|
+
|
|
|
+ // 要生成二维码的内容
|
|
|
+// String text = "http://127.0.0.1:4200/mlApi/qualityInspection/zhcxSeaBindProof/previewSeaExcel/"+id;
|
|
|
+ String text = PreviewUrl + "/mlApi/qualityInspection/zhcxSeaBindProof/previewSeaExcel/"+id;
|
|
|
+ // 设置二维码的参数
|
|
|
+ int width = 100; // 宽度
|
|
|
+ int height = 100; // 高度
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ BufferedImage image =QRCodeGenerator.generateQRCode(text,width,height);
|
|
|
+ // 保存二维码图片到文件
|
|
|
+ File outputFile = new File("QRCode.png");
|
|
|
+ ImageIO.write(image, "PNG", outputFile);
|
|
|
+
|
|
|
+ PictureCollection pictures1 = sheet0.getPictures();
|
|
|
+
|
|
|
+// 检查第二张图片是否存在
|
|
|
+ if (pictures1.getCount() > 1) {
|
|
|
+ pictures1.removeAt(1); // 删除第二张图片
|
|
|
+ }
|
|
|
+
|
|
|
+ int pictureIndex = pictures1.add(1, 6, outputFile.getAbsolutePath());
|
|
|
+ Picture picture = pictures1.get(pictureIndex);
|
|
|
+
|
|
|
+ // 设置图片的精确位置(偏移)
|
|
|
+ picture.setUpperDeltaX(380);
|
|
|
+ picture.setUpperDeltaY(0);
|
|
|
+ // 设置插入图片的显示大小
|
|
|
+ int newWidth = 80; // 设置图片显示的宽度
|
|
|
+ int newHeight = 80; // 设置图片显示的高度
|
|
|
+ picture.setWidth(newWidth);
|
|
|
+ picture.setHeight(newHeight);
|
|
|
+ } catch (WriterException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> map = this.getMap(Wrappers.<ZhcxSeaBindProofDo>lambdaQuery()
|
|
|
+ .eq(ZhcxSeaBindProofDo::getId, id)
|
|
|
+ .eq(ZhcxSeaBindProofDo::getDeleted, "0"));
|
|
|
+ List<Map<String, Object>> map2 = zhcxSeaBindProofDetalService.listMaps(
|
|
|
+ Wrappers.<ZhcxSeaBindProofDetalDo>lambdaQuery()
|
|
|
+ .eq(ZhcxSeaBindProofDetalDo::getPid, id)
|
|
|
+ .eq(ZhcxSeaBindProofDetalDo::getDeleted, "0")
|
|
|
+ );
|
|
|
+
|
|
|
+ // 根据 map2 的长度来动态决定新增多少行
|
|
|
+ int numRowsToInsert = map2.size() - 6;
|
|
|
+
|
|
|
+ if (numRowsToInsert < 0) {
|
|
|
+ numRowsToInsert = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入多行:在第 7 行开始插入多行
|
|
|
+ int insertRowIndex = 7;
|
|
|
+
|
|
|
+ for (int i = 0; i < numRowsToInsert; i++) {
|
|
|
+ // 插入一行
|
|
|
+ cells.insertRow(insertRowIndex + i);
|
|
|
+
|
|
|
+ // 复制上一行的格式到新插入的行
|
|
|
+ // 这里的 insertRowIndex - 1 是上一行的索引,insertRowIndex + i 是当前插入行的索引
|
|
|
+ cells.copyRow(cells, insertRowIndex + i - 1, insertRowIndex + i);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < map2.size(); i++) {
|
|
|
+ Map<String, Object> rowData = map2.get(i);
|
|
|
+
|
|
|
+ // 获取当前行号,插入时填充的数据行
|
|
|
+ int currentRowIndex = insertRowIndex + i -1;
|
|
|
+
|
|
|
+ // 填充每个占位符的值
|
|
|
+ cells.get(currentRowIndex, 1).setValue(rowData.get("ITEMS")); // 填充 ITEMS 列
|
|
|
+ cells.get(currentRowIndex, 2).setValue(rowData.get("CHECKRESULT")); // 填充 CHECKRESULT 列
|
|
|
+ cells.get(currentRowIndex, 4).setValue(rowData.get("QCINSPECTOR")); // 填充 QCINSPECTOR 列
|
|
|
+ // 获取 CHECKDATE 并格式化为 "yyyy-MM-dd"
|
|
|
+ Object checkDateObj = rowData.get("DATE");
|
|
|
+ if (checkDateObj instanceof Date) {
|
|
|
+ SimpleDateFormat rq = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String formattedDate = rq.format((Date) checkDateObj); // 格式化日期
|
|
|
+ cells.get(currentRowIndex, 6).setValue(formattedDate); // 填充 CHECKDATE 列
|
|
|
+ } else {
|
|
|
+ cells.get(currentRowIndex, 6).setValue(""); // 如果 CHECKDATE 不是日期,填充为空字符串
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int row = 0; row < cells.getMaxDataRow() + 1; row++) {
|
|
|
+ for (int col = 0; col < cells.getMaxDataColumn() + 1; col++) {
|
|
|
+ Cell cell = cells.get(row, col);
|
|
|
+ String cellValue = cell.getStringValue();
|
|
|
+ // 检查占位符并替换对应的数据
|
|
|
+ if (cellValue.startsWith("${") && cellValue.endsWith("}")) {
|
|
|
+ String key = cellValue.substring(2, cellValue.length() - 1);
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value != null) {
|
|
|
+ // 将占位符替换为实际值
|
|
|
+ if(key.equals("CREATEDATE")){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String format = sdf.format((Date) value);
|
|
|
+ cell.putValue(format);
|
|
|
+ }
|
|
|
+
|
|
|
+ else{
|
|
|
+ if(key.equals("remark")){
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ cell.putValue("");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String ename = (map.containsKey("PROJECTCODE") ? String.valueOf(map.get("PROJECTCODE")) : "") +
|
|
|
+ (map.containsKey("PROJECTNAME") ? String.valueOf(map.get("PROJECTNAME")) : "") +
|
|
|
+ (map.containsKey("CRANENO") ? String.valueOf(map.get("CRANENO")) : "");
|
|
|
+
|
|
|
+// ename = ename.replace("#", "");
|
|
|
+
|
|
|
+ String fileName = ename + "号机海绑证明";
|
|
|
+ String encodedFileName = URLEncoder.encode(fileName+".pdf", "UTF-8").replaceAll("\\+", "%20"); // 替换空格为 %20
|
|
|
+
|
|
|
+ // 设置响应头
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+
|
|
|
+ response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", encodedFileName));
|
|
|
+ // 转换为PDF
|
|
|
+ ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
|
|
|
+ wb.save(pdfOutputStream, SaveFormat.PDF);
|
|
|
+ // 将PDF文件写入响应输出流
|
|
|
+ response.getOutputStream().write(pdfOutputStream.toByteArray());
|
|
|
+ response.getOutputStream().flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException("生成PDF失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|