mamingxu 2 days ago
parent
commit
c177c0d844

+ 48 - 51
easier-report-biz/src/main/java/com/yaoyicloud/controller/CsoReportController.java

@@ -15,18 +15,14 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import com.yaoyicloud.dto.ReportDTO;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
 
-import java.io.ByteArrayOutputStream;
 
+import java.io.ByteArrayOutputStream;
 
 import static com.yaoyicloud.config.SessionInterceptor.SESSION_MAP;
 
-
 /**
  * 报告控制器
  *
@@ -38,58 +34,59 @@ import static com.yaoyicloud.config.SessionInterceptor.SESSION_MAP;
 @Slf4j
 public class CsoReportController {
     private final CsoReportService csoReportService;
-	/**
-	 * 创建Plus版本审核报告
-	 *
-	 * @param resource 请求参数
-	 * @return {@link Boolean } 结果
-	 */
-	@PostMapping("/report/create-report-cso")
-	public Map<String, Object>
-	createPlusVersionCheckReport(@Validated @RequestBody ReportDTO.CsoReport resource,
-	                             HttpServletRequest request) throws Exception {
+    private final RelationCounterRedisUtil relationCounterRedisUtil;
+    private final CommonDataCache commonDataCache;
 
-		String relationId = request.getHeader("relationId");
-		Boolean mergeflag = resource.getMergeflag();
-		synchronized (this) {
-			ReportGenerationResult plusVersionCheckReport = csoReportService.createCsoCheckReport(
-					resource.getData(),
-					Long.valueOf(relationId),
-					resource.getModuleType(),
-					mergeflag
-			);
-			String sessionId = SESSION_MAP.get(relationId);
+    /**
+     * 创建Plus版本审核报告
+     *
+     * @param resource 请求参数
+     * @return {@link Boolean } 结果
+     */
+    @PostMapping("/report/create-report-cso")
+    public Map<String, Object>
+        createPlusVersionCheckReport(@Validated @RequestBody ReportDTO.CsoReport resource,
+            HttpServletRequest request) throws Exception {
 
-			// 3. 构建响应
-			Map<String, Object> response = new HashMap<>();
-			// 根据是否是最后一个模块决定返回哪个路径
-			if (mergeflag) {
-				response.put("reportResult", plusVersionCheckReport.getMergedReportPath());
-				commonDataCache.removeSessionData(relationId);
-				relationCounterRedisUtil.delete(Long.valueOf(relationId));
-			} else {
-				response.put("reportResult", plusVersionCheckReport.getModulePath());
+        String relationId = request.getHeader("relationId");
+        Boolean mergeflag = resource.getMergeflag();
+        synchronized (this) {
+            ReportGenerationResult plusVersionCheckReport = csoReportService.createCsoCheckReport(
+                resource.getData(),
+                Long.valueOf(relationId),
+                resource.getModuleType(),
+                mergeflag);
+            String sessionId = SESSION_MAP.get(relationId);
 
-			}
+            // 3. 构建响应
+            Map<String, Object> response = new HashMap<>();
+            // 根据是否是最后一个模块决定返回哪个路径
+            if (mergeflag) {
+                response.put("reportResult", plusVersionCheckReport.getMergedReportPath());
+                commonDataCache.removeSessionData(relationId);
+                relationCounterRedisUtil.delete(Long.valueOf(relationId));
+            } else {
+                response.put("reportResult", plusVersionCheckReport.getModulePath());
 
-			response.put("sessionId", sessionId);
-			return response;
-		}
-	}
+            }
 
+            response.put("sessionId", sessionId);
+            return response;
+        }
+    }
 
-	@PostMapping(value = "/report/create-report", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
-	public ResponseEntity<byte[]> handleReport(@RequestBody byte[] fileBytes) throws Exception {
-		// 1. 直接获取PDF字节流
-		ByteArrayOutputStream pdfStream = csoReportService.saveToTempFile(fileBytes);
+    @PostMapping(value = "/report/create-report", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
+    public ResponseEntity<byte[]> handleReport(@RequestBody byte[] fileBytes) throws Exception {
+        // 1. 直接获取PDF字节流
+        ByteArrayOutputStream pdfStream = csoReportService.saveToTempFile(fileBytes);
 
-		// 2. 转换为字节数组
-		byte[] pdfBytes = pdfStream.toByteArray();
+        // 2. 转换为字节数组
+        byte[] pdfBytes = pdfStream.toByteArray();
 
-		// 3. 直接返回PDF字节流
-		return ResponseEntity.ok()
-				.contentType(MediaType.APPLICATION_PDF)  // 关键:设置为PDF类型
-				.header("Content-Disposition", "attachment; filename=report.pdf")  // 设置下载文件名
-				.body(pdfBytes);
-	}
+        // 3. 直接返回PDF字节流
+        return ResponseEntity.ok()
+            .contentType(MediaType.APPLICATION_PDF) // 关键:设置为PDF类型
+            .header("Content-Disposition", "attachment; filename=report.pdf") // 设置下载文件名
+            .body(pdfBytes);
+    }
 }

+ 6 - 8
easier-report-biz/src/main/java/com/yaoyicloud/service/impl/CsoReportServiceImpl.java

@@ -25,13 +25,12 @@ import com.yaoyicloud.entity.ReportGenerationResult;
 import com.yaoyicloud.render.cso.EntHeaderSectionRender;
 import com.yaoyicloud.render.cso.EntPromotionSummaryRender;
 import com.yaoyicloud.service.CsoReportService;
-import com.yaoyicloud.tools.OfficeUtil1;
+import com.yaoyicloud.tools.OfficeUtil;
 import com.yaoyicloud.tools.DocxUtil;
 
 import cn.hutool.core.util.IdUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
 
 import java.io.BufferedWriter;
 import java.io.ByteArrayOutputStream;
@@ -40,7 +39,6 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.Arrays;
-import java.util.List;
 
 @Slf4j
 @Service
@@ -166,22 +164,22 @@ public class CsoReportServiceImpl implements CsoReportService {
 			reportTempWordFile = File.createTempFile("temp_word_", ".docx");
 			Files.write(reportTempWordFile.toPath(), fileBytes);
 
-			String html = OfficeUtil1.convert(reportTempWordFile.getAbsolutePath(), imagePath);
+			String html = OfficeUtil.convert(reportTempWordFile.getAbsolutePath(), imagePath);
 			try (BufferedWriter writer2 = new BufferedWriter(new FileWriter("1.html"))) {
 				writer2.write(html);
 			} catch (IOException e) {
 				System.err.println("写入 1.html 文件时发生错误: " + e.getMessage());
 				e.printStackTrace();
 			}
-			String html1 = OfficeUtil1.formatHtmlCso(html);
+			String html1 = OfficeUtil.formatHtmlCso(html);
 			try (BufferedWriter writer2 = new BufferedWriter(new FileWriter("2.html"))) {
 				writer2.write(html1);
 			} catch (IOException e) {
 				System.err.println("写入 2.html 文件时发生错误: " + e.getMessage());
 				e.printStackTrace();
 			}
-			OfficeUtil1.convertHtmlToPdfCso(html1, fontLists, imagePath, true);
-			String html2 = OfficeUtil1.formatHtml(html);
+			OfficeUtil.convertHtmlToPdfCso(html1, fontLists, imagePath, true);
+			String html2 = OfficeUtil.formatHtml(html);
 
 			try (BufferedWriter writer2 = new BufferedWriter(new FileWriter("3.html"))) {
 				writer2.write(html2);
@@ -189,7 +187,7 @@ public class CsoReportServiceImpl implements CsoReportService {
 				System.err.println("写入 3.html 文件时发生错误: " + e.getMessage());
 				e.printStackTrace();
 			}
-			return OfficeUtil1.convertHtmlToPdfCso(html2, fontLists, imagePath, false);
+			return OfficeUtil.convertHtmlToPdfCso(html2, fontLists, imagePath, false);
 		} catch (Exception e) {
 			log.error("文件转换失败", e);
 			throw e;

+ 5 - 3
easier-report-biz/src/main/java/com/yaoyicloud/tools/OfficeUtil.java

@@ -3,6 +3,9 @@ import com.lowagie.text.Image;
 import com.lowagie.text.PageSize;
 import com.lowagie.text.pdf.BaseFont;
 import com.lowagie.text.pdf.PdfContentByte;
+import com.lowagie.text.pdf.PdfDictionary;
+import com.lowagie.text.pdf.PdfName;
+import com.lowagie.text.pdf.PdfObject;
 import com.lowagie.text.pdf.PdfReader;
 import com.lowagie.text.pdf.PdfStamper;
 import com.lowagie.text.pdf.parser.PdfTextExtractor;
@@ -24,7 +27,6 @@ import org.jsoup.select.Elements;
 import org.xhtmlrenderer.pdf.ITextFontResolver;
 import org.xhtmlrenderer.pdf.ITextRenderer;
 import java.awt.Color;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -43,8 +45,8 @@ import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class OfficeUtil1 {
-    private static final org.slf4j.Logger OFFICE_UTIL_LOGGER = org.slf4j.LoggerFactory.getLogger(OfficeUtil1.class);
+public class OfficeUtil {
+    private static final org.slf4j.Logger OFFICE_UTIL_LOGGER = org.slf4j.LoggerFactory.getLogger(OfficeUtil.class);
     private static Map<String, Integer> pageNumberMap = new LinkedHashMap<>();