feat: 植物识别百科ai助手迁移
This commit is contained in:
@@ -3,9 +3,11 @@ package ocr
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
)
|
||||
|
||||
type GetMyClassifyLogLogic struct {
|
||||
@@ -14,11 +16,53 @@ type GetMyClassifyLogLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
type ClassifyRecordResp struct {
|
||||
List []ClassifyRecordInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type ClassifyRecordInfo struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"userId"`
|
||||
LogID uint64 `json:"logId"`
|
||||
AllResults plantModel.ResultsArray `json:"allResults"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
}
|
||||
|
||||
func NewGetMyClassifyLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyClassifyLogLogic {
|
||||
return &GetMyClassifyLogLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyClassifyLogLogic) GetMyClassifyLog() (*plantPb.ClassifyLogListResp, error) {
|
||||
func (l *GetMyClassifyLogLogic) GetMyClassifyLog() (*ClassifyRecordResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetMyClassifyLog(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
|
||||
var records []plantModel.ClassifyRecord
|
||||
var total int64
|
||||
|
||||
db := l.svcCtx.DB.Model(&plantModel.ClassifyRecord{}).Where("user_id = ?", userId)
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := db.Order("created_at desc").Limit(50).Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := make([]ClassifyRecordInfo, 0, len(records))
|
||||
for _, og := range records {
|
||||
list = append(list, ClassifyRecordInfo{
|
||||
ID: og.ID,
|
||||
UserID: og.UserID,
|
||||
LogID: og.LogID,
|
||||
AllResults: og.AllResults,
|
||||
CreatedAt: og.CreatedAt.Format(time.RFC3339),
|
||||
CreatedAtStr: og.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &ClassifyRecordResp{
|
||||
List: list,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -83,7 +84,51 @@ func (l *OcrClassifyLogic) OcrClassify(req *types.OcrReq) (interface{}, error) {
|
||||
return nil, fmt.Errorf("读取识别结果失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 直接返回百度原始结果(前端自行解析 result 字段)
|
||||
// 3. 解析为结构化识别结果并写入 ClassifyRecord 表
|
||||
var baiduResp struct {
|
||||
LogId uint64 `json:"log_id"`
|
||||
Result []struct {
|
||||
Score float64 `json:"score"`
|
||||
Name string `json:"name"`
|
||||
BaikeInfo *struct {
|
||||
BaikeUrl string `json:"baike_url"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Description string `json:"description"`
|
||||
} `json:"baike_info"`
|
||||
} `json:"result"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &baiduResp)
|
||||
|
||||
if baiduResp.LogId > 0 {
|
||||
var dbResults plantModel.ResultsArray = make(plantModel.ResultsArray, 0, len(baiduResp.Result))
|
||||
for _, item := range baiduResp.Result {
|
||||
var baikeInfo *plantModel.BaikeInfo
|
||||
if item.BaikeInfo != nil {
|
||||
baikeInfo = &plantModel.BaikeInfo{
|
||||
BaikeUrl: item.BaikeInfo.BaikeUrl,
|
||||
ImageUrl: item.BaikeInfo.ImageUrl,
|
||||
Description: item.BaikeInfo.Description,
|
||||
}
|
||||
}
|
||||
dbResults = append(dbResults, plantModel.ResultItem{
|
||||
Score: item.Score,
|
||||
Name: item.Name,
|
||||
BaikeInfo: baikeInfo,
|
||||
})
|
||||
}
|
||||
|
||||
userID := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
record := plantModel.ClassifyRecord{
|
||||
UserID: userID,
|
||||
LogID: baiduResp.LogId,
|
||||
AllResults: dbResults,
|
||||
}
|
||||
if errDb := l.svcCtx.DB.Create(&record).Error; errDb != nil {
|
||||
l.Logger.Errorf("植物识别记录写入数据库失败: %v", errDb)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 直接返回百度原始结果
|
||||
var result interface{}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("解析识别结果失败: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user