2025-12-10 18:33:37 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-31 14:17:44 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
2025-12-10 18:33:37 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-31 14:17:44 +08:00
|
|
|
|
// FormulaContent 公式识别的 content 字段结构
|
|
|
|
|
|
type FormulaContent struct {
|
|
|
|
|
|
Latex string `json:"latex"`
|
|
|
|
|
|
Markdown string `json:"markdown"`
|
|
|
|
|
|
MathML string `json:"mathml"`
|
|
|
|
|
|
MML string `json:"mml"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PDFPageContent PDF 单页识别结果
|
|
|
|
|
|
type PDFPageContent struct {
|
|
|
|
|
|
PageNumber int `json:"page_number"`
|
|
|
|
|
|
Markdown string `json:"markdown"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ResultMetaData recognition_results.meta_data 字段结构
|
|
|
|
|
|
type ResultMetaData struct {
|
|
|
|
|
|
TotalNum int `json:"total_num"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RecognitionResult recognition_results 表模型
|
2025-12-10 18:33:37 +08:00
|
|
|
|
type RecognitionResult struct {
|
|
|
|
|
|
BaseModel
|
2026-03-31 14:17:44 +08:00
|
|
|
|
TaskID int64 `gorm:"column:task_id;bigint;not null;default:0;index;comment:任务ID" json:"task_id"`
|
2025-12-10 18:33:37 +08:00
|
|
|
|
TaskType TaskType `gorm:"column:task_type;varchar(16);not null;comment:任务类型;default:''" json:"task_type"`
|
2026-03-31 14:17:44 +08:00
|
|
|
|
MetaData string `gorm:"column:meta_data;type:json;comment:元数据" json:"meta_data"`
|
|
|
|
|
|
Content string `gorm:"column:content;type:json;comment:识别内容JSON" json:"content"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetMetaData 序列化并写入 MetaData 字段
|
|
|
|
|
|
func (r *RecognitionResult) SetMetaData(meta ResultMetaData) error {
|
|
|
|
|
|
b, err := json.Marshal(meta)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
r.MetaData = string(b)
|
|
|
|
|
|
return nil
|
2025-12-10 18:33:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 14:17:44 +08:00
|
|
|
|
// GetFormulaContent 从 Content 字段反序列化公式结果
|
|
|
|
|
|
func (r *RecognitionResult) GetFormulaContent() (*FormulaContent, error) {
|
|
|
|
|
|
var c FormulaContent
|
|
|
|
|
|
if err := json.Unmarshal([]byte(r.Content), &c); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &c, nil
|
2025-12-10 18:33:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 14:17:44 +08:00
|
|
|
|
// GetPDFContent 从 Content 字段反序列化 PDF 分页结果
|
|
|
|
|
|
func (r *RecognitionResult) GetPDFContent() ([]PDFPageContent, error) {
|
|
|
|
|
|
var pages []PDFPageContent
|
|
|
|
|
|
if err := json.Unmarshal([]byte(r.Content), &pages); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return pages, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MarshalFormulaContent 将公式结果序列化为 JSON 字符串(供写入 Content)
|
|
|
|
|
|
func MarshalFormulaContent(c FormulaContent) (string, error) {
|
|
|
|
|
|
b, err := json.Marshal(c)
|
|
|
|
|
|
return string(b), err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MarshalPDFContent 将 PDF 分页结果序列化为 JSON 字符串(供写入 Content)
|
|
|
|
|
|
func MarshalPDFContent(pages []PDFPageContent) (string, error) {
|
|
|
|
|
|
b, err := json.Marshal(pages)
|
|
|
|
|
|
return string(b), err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type RecognitionResultDao struct{}
|
|
|
|
|
|
|
2025-12-10 18:33:37 +08:00
|
|
|
|
func NewRecognitionResultDao() *RecognitionResultDao {
|
|
|
|
|
|
return &RecognitionResultDao{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (dao *RecognitionResultDao) Create(tx *gorm.DB, data RecognitionResult) error {
|
|
|
|
|
|
return tx.Create(&data).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 14:17:44 +08:00
|
|
|
|
func (dao *RecognitionResultDao) GetByTaskID(tx *gorm.DB, taskID int64) (*RecognitionResult, error) {
|
|
|
|
|
|
result := &RecognitionResult{}
|
|
|
|
|
|
err := tx.Where("task_id = ?", taskID).First(result).Error
|
2025-12-10 18:33:37 +08:00
|
|
|
|
if err != nil && err == gorm.ErrRecordNotFound {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
2026-03-31 14:17:44 +08:00
|
|
|
|
return result, err
|
2025-12-18 12:39:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-10 18:33:37 +08:00
|
|
|
|
func (dao *RecognitionResultDao) Update(tx *gorm.DB, id int64, updates map[string]interface{}) error {
|
|
|
|
|
|
return tx.Model(&RecognitionResult{}).Where("id = ?", id).Updates(updates).Error
|
|
|
|
|
|
}
|
2026-03-31 14:17:44 +08:00
|
|
|
|
|
|
|
|
|
|
func (dao *RecognitionResultDao) GetByTaskIDs(tx *gorm.DB, taskIDs []int64) ([]*RecognitionResult, error) {
|
|
|
|
|
|
var results []*RecognitionResult
|
|
|
|
|
|
err := tx.Where("task_id IN (?)", taskIDs).Find(&results).Error
|
|
|
|
|
|
return results, err
|
|
|
|
|
|
}
|