diff --git a/internal/infrastructure/http/handlers/qygl_report_handler.go b/internal/infrastructure/http/handlers/qygl_report_handler.go index 29848a7..83eb0d8 100644 --- a/internal/infrastructure/http/handlers/qygl_report_handler.go +++ b/internal/infrastructure/http/handlers/qygl_report_handler.go @@ -1,6 +1,7 @@ package handlers import ( + "context" "encoding/json" "fmt" "html/template" @@ -116,6 +117,7 @@ func (h *QYGLReportHandler) GetQYGLReportPageByID(c *gin.Context) { // 优先从数据库中查询报告记录 if h.reportRepo != nil { if entity, err := h.reportRepo.FindByReportID(c.Request.Context(), id); err == nil && entity != nil { + h.maybeScheduleQYGLPDFPregen(c.Request.Context(), id) reportJSON := template.JS(entity.ReportData) c.HTML(http.StatusOK, "qiye.html", gin.H{ "ReportJSON": reportJSON, @@ -140,11 +142,36 @@ func (h *QYGLReportHandler) GetQYGLReportPageByID(c *gin.Context) { reportJSON := template.JS(reportJSONBytes) + h.maybeScheduleQYGLPDFPregen(c.Request.Context(), id) + c.HTML(http.StatusOK, "qiye.html", gin.H{ "ReportJSON": reportJSON, }) } +// qyglReportExists 报告是否仍在库或本进程内存中(用于决定是否补开预生成) +func (h *QYGLReportHandler) qyglReportExists(ctx context.Context, id string) bool { + if h.reportRepo != nil { + if e, err := h.reportRepo.FindByReportID(ctx, id); err == nil && e != nil { + return true + } + } + _, ok := qygl.GetQYGLReport(id) + return ok +} + +// maybeScheduleQYGLPDFPregen 在已配置公网基址时异步预生成 PDF;Schedule 内部会去重。 +// 解决:服务重启 / 多实例后内存队列为空,用户打开报告页或轮询状态时仍应能启动预生成。 +func (h *QYGLReportHandler) maybeScheduleQYGLPDFPregen(ctx context.Context, id string) { + if id == "" || h.qyglPDFPregen == nil || !h.qyglPDFPregen.Enabled() { + return + } + if !h.qyglReportExists(ctx, id) { + return + } + h.qyglPDFPregen.ScheduleQYGLReportPDF(context.Background(), id) +} + // GetQYGLReportPDFStatusByID 查询企业报告 PDF 预生成状态(供前端轮询) // GET /reports/qygl/:id/pdf/status func (h *QYGLReportHandler) GetQYGLReportPDFStatusByID(c *gin.Context) { @@ -164,6 +191,10 @@ func (h *QYGLReportHandler) GetQYGLReportPDFStatusByID(c *gin.Context) { return } st, msg := h.qyglPDFPregen.Status(id) + if st == pdf.QYGLReportPDFStatusNone && h.qyglReportExists(c.Request.Context(), id) { + h.qyglPDFPregen.ScheduleQYGLReportPDF(context.Background(), id) + st, msg = h.qyglPDFPregen.Status(id) + } c.JSON(http.StatusOK, gin.H{"status": string(st), "message": userFacingPDFStatusMessage(st, msg)}) } diff --git a/resources/qiye.html b/resources/qiye.html index 27cd17b..403fcf3 100644 --- a/resources/qiye.html +++ b/resources/qiye.html @@ -3434,6 +3434,14 @@ break; } } + if ( + !reportId && + reportData && + reportData.reportId && + typeof reportData.reportId === "string" + ) { + reportId = reportData.reportId; + } if (!reportId) { console.error( "无法从当前 URL 解析报告编号,路径为",