package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"
)

type GetMyClassifyLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMyClassifyLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyClassifyLogLogic {
	return &GetMyClassifyLogLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *GetMyClassifyLogLogic) GetMyClassifyLog(in *plant.GetProfileReq) (*plant.ClassifyLogListResp, error) {
	var logs []plantModel.OcrLog
	if err := l.svcCtx.DB.Where("user_id = ?", in.UserId).Order("created_at desc").Limit(50).Find(&logs).Error; err != nil {
		return nil, err
	}
	list := make([]*plant.ClassifyLogInfo, 0, len(logs))
	for _, og := range logs {
		list = append(list, &plant.ClassifyLogInfo{
			Id: og.ID, UserId: og.UserID, ImageUrl: og.ImageUrl, Result: og.Result,
			CreatedAt: og.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	return &plant.ClassifyLogListResp{List: list, Total: int64(len(list))}, nil
}
