feat: 植物识别记录
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package plant
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
)
|
||||
|
||||
type ClassifyRecord struct {
|
||||
global.BaseModel
|
||||
UserId string `gorm:"uniqueIndex" json:"userId"`
|
||||
LogId uint64 `gorm:"uniqueIndex" json:"logId"`
|
||||
AllResults ResultsArray `gorm:"type:json" json:"allResults"`
|
||||
}
|
||||
|
||||
// BaikeInfo 植物百科信息(baike_info子对象)
|
||||
type BaikeInfo struct {
|
||||
BaikeUrl string `json:"baike_url"` // 百度百科链接
|
||||
ImageUrl string `json:"image_url"` // 植物图片链接
|
||||
Description string `json:"description"` // 植物百科描述文本
|
||||
}
|
||||
|
||||
// ResultItem 识别结果项(result数组中的单个元素)
|
||||
type ResultItem struct {
|
||||
Score float64 `json:"score"` // 匹配相似度得分(0-1)
|
||||
Name string `json:"name"` // 植物名称
|
||||
BaikeInfo *BaikeInfo `json:"baike_info"` // 植物百科信息(部分结果可能无此字段,用指针避免空值解析问题)
|
||||
}
|
||||
|
||||
type ResultsArray []ResultItem
|
||||
|
||||
// Scan 实现 sql.Scanner 接口:JSON String -> Go Struct (读库)
|
||||
// 3. 实现 sql.Scanner 接口 (读库)
|
||||
// 必须是指针接收者,否则无法修改 r 的值
|
||||
func (r *ResultsArray) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*r = make([]ResultItem, 0)
|
||||
return nil
|
||||
}
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("type assertion to []byte failed")
|
||||
}
|
||||
return json.Unmarshal(bytes, r)
|
||||
}
|
||||
|
||||
// Value 实现 driver.Valuer 接口:Go Struct -> JSON String (存库)
|
||||
func (r ResultsArray) Value() (driver.Value, error) {
|
||||
// 如果是 nil 或空数组,存为空 JSON 数组
|
||||
if len(r) == 0 {
|
||||
return "[]", nil
|
||||
}
|
||||
// 序列化为 JSON 字符串
|
||||
return json.Marshal(r)
|
||||
}
|
||||
Reference in New Issue
Block a user