package dao import ( "encoding/json" "gorm.io/gorm" ) type JSON []byte // ContentCodec 定义内容编解码接口 type ContentCodec interface { Encode() (JSON, error) Decode() error GetContent() interface{} // 更明确的方法名 } type FormulaRecognitionContent struct { content JSON Latex string `json:"latex"` AdjustLatex string `json:"adjust_latex"` EnhanceLatex string `json:"enhance_latex"` } func (c *FormulaRecognitionContent) Encode() (JSON, error) { b, err := json.Marshal(c) if err != nil { return nil, err } return b, nil } func (c *FormulaRecognitionContent) Decode() error { return json.Unmarshal(c.content, c) } // GetPreferredContent 按优先级返回公式内容 func (c *FormulaRecognitionContent) GetContent() interface{} { c.Decode() if c.EnhanceLatex != "" { return c.EnhanceLatex } else if c.AdjustLatex != "" { return c.AdjustLatex } else { return c.Latex } } type RecognitionResult struct { BaseModel TaskID int64 `gorm:"column:task_id;bigint;not null;default:0;comment:任务ID" json:"task_id"` TaskType TaskType `gorm:"column:task_type;varchar(16);not null;comment:任务类型;default:''" json:"task_type"` Content JSON `gorm:"column:content;type:json;not null;comment:识别内容" json:"content"` } // NewContentCodec 创建对应任务类型的内容编解码器 func (r *RecognitionResult) NewContentCodec() ContentCodec { switch r.TaskType { case TaskTypeFormula: return &FormulaRecognitionContent{content: r.Content} default: return nil } } type RecognitionResultDao struct { } func NewRecognitionResultDao() *RecognitionResultDao { return &RecognitionResultDao{} } // 模型方法 func (dao *RecognitionResultDao) Create(tx *gorm.DB, data RecognitionResult) error { return tx.Create(&data).Error } func (dao *RecognitionResultDao) GetByTaskID(tx *gorm.DB, taskID int64) (result *RecognitionResult, err error) { result = &RecognitionResult{} err = tx.Where("task_id = ?", taskID).First(result).Error if err != nil && err == gorm.ErrRecordNotFound { return nil, nil } return } func (dao *RecognitionResultDao) Update(tx *gorm.DB, id int64, updates map[string]interface{}) error { return tx.Model(&RecognitionResult{}).Where("id = ?", id).Updates(updates).Error }