138 lines
4.0 KiB
Go
138 lines
4.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"html/template"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
"tyapi-server/internal/application/api/commands"
|
|
"tyapi-server/internal/domains/api/dto"
|
|
api_repositories "tyapi-server/internal/domains/api/repositories"
|
|
api_services "tyapi-server/internal/domains/api/services"
|
|
"tyapi-server/internal/domains/api/services/processors"
|
|
"tyapi-server/internal/domains/api/services/processors/qygl"
|
|
)
|
|
|
|
// QYGLReportHandler 企业全景报告页面渲染处理器
|
|
// 使用 QYGLJ1U9 聚合接口生成企业报告数据,并通过模板引擎渲染 qiye.html
|
|
type QYGLReportHandler struct {
|
|
apiRequestService *api_services.ApiRequestService
|
|
logger *zap.Logger
|
|
|
|
reportRepo api_repositories.ReportRepository
|
|
}
|
|
|
|
// NewQYGLReportHandler 创建企业报告页面处理器
|
|
func NewQYGLReportHandler(
|
|
apiRequestService *api_services.ApiRequestService,
|
|
logger *zap.Logger,
|
|
reportRepo api_repositories.ReportRepository,
|
|
) *QYGLReportHandler {
|
|
return &QYGLReportHandler{
|
|
apiRequestService: apiRequestService,
|
|
logger: logger,
|
|
reportRepo: reportRepo,
|
|
}
|
|
}
|
|
|
|
// GetQYGLReportPage 企业全景报告页面
|
|
// GET /reports/qygl?ent_code=xxx&ent_name=yyy&ent_reg_no=zzz
|
|
func (h *QYGLReportHandler) GetQYGLReportPage(c *gin.Context) {
|
|
// 读取查询参数
|
|
entCode := c.Query("ent_code")
|
|
entName := c.Query("ent_name")
|
|
entRegNo := c.Query("ent_reg_no")
|
|
|
|
// 组装 QYGLUY3S 入参
|
|
req := dto.QYGLUY3SReq{
|
|
EntName: entName,
|
|
EntRegno: entRegNo,
|
|
EntCode: entCode,
|
|
}
|
|
|
|
params, err := json.Marshal(req)
|
|
if err != nil {
|
|
h.logger.Error("序列化企业全景报告入参失败", zap.Error(err))
|
|
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后重试")
|
|
return
|
|
}
|
|
|
|
// 通过 ApiRequestService 调用 QYGLJ1U9 聚合处理器
|
|
options := &commands.ApiCallOptions{}
|
|
callCtx := &processors.CallContext{}
|
|
|
|
ctx := c.Request.Context()
|
|
respBytes, err := h.apiRequestService.PreprocessRequestApi(ctx, "QYGLJ1U9", params, options, callCtx)
|
|
if err != nil {
|
|
h.logger.Error("调用企业全景报告处理器失败", zap.Error(err))
|
|
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后重试")
|
|
return
|
|
}
|
|
|
|
var report map[string]interface{}
|
|
if err := json.Unmarshal(respBytes, &report); err != nil {
|
|
h.logger.Error("解析企业全景报告结果失败", zap.Error(err))
|
|
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后重试")
|
|
return
|
|
}
|
|
|
|
reportJSONBytes, err := json.Marshal(report)
|
|
if err != nil {
|
|
h.logger.Error("序列化企业全景报告结果失败", zap.Error(err))
|
|
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后重试")
|
|
return
|
|
}
|
|
|
|
// 使用 template.JS 避免在脚本中被转义,直接作为 JS 对象字面量注入
|
|
reportJSON := template.JS(reportJSONBytes)
|
|
|
|
c.HTML(http.StatusOK, "qiye.html", gin.H{
|
|
"ReportJSON": reportJSON,
|
|
})
|
|
}
|
|
|
|
// GetQYGLReportPageByID 通过编号查看企业全景报告页面
|
|
// GET /reports/qygl/:id
|
|
func (h *QYGLReportHandler) GetQYGLReportPageByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.String(http.StatusBadRequest, "报告编号不能为空")
|
|
return
|
|
}
|
|
|
|
// 优先从数据库中查询报告记录
|
|
if h.reportRepo != nil {
|
|
if entity, err := h.reportRepo.FindByReportID(c.Request.Context(), id); err == nil && entity != nil {
|
|
reportJSON := template.JS(entity.ReportData)
|
|
c.HTML(http.StatusOK, "qiye.html", gin.H{
|
|
"ReportJSON": reportJSON,
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
// 回退到进程内存缓存(兼容老的访问方式)
|
|
report, ok := qygl.GetQYGLReport(id)
|
|
if !ok {
|
|
c.String(http.StatusNotFound, "报告不存在或已过期")
|
|
return
|
|
}
|
|
|
|
reportJSONBytes, err := json.Marshal(report)
|
|
if err != nil {
|
|
h.logger.Error("序列化企业全景报告结果失败", zap.Error(err))
|
|
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后再试")
|
|
return
|
|
}
|
|
|
|
reportJSON := template.JS(reportJSONBytes)
|
|
|
|
c.HTML(http.StatusOK, "qiye.html", gin.H{
|
|
"ReportJSON": reportJSON,
|
|
})
|
|
}
|
|
|