Files
sundynix-micro-be/app/plant/api/internal/logic/myPlant/getMyPlantListLogic.go
T
2026-04-27 00:02:18 +08:00

44 lines
1.3 KiB
Go

// Code scaffolded by goctl. Safe to edit.
package myPlant
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"sundynix-micro-go/app/plant/api/internal/svc"
"sundynix-micro-go/app/plant/api/internal/types"
plantModel "sundynix-micro-go/app/plant/model"
)
type GetMyPlantListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetMyPlantListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyPlantListLogic {
return &GetMyPlantListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}
func (l *GetMyPlantListLogic) GetMyPlantList(req *types.PlantListReq) (resp interface{}, err error) {
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
var plants []plantModel.SundynixMyPlant
var total int64
db := l.svcCtx.DB.Model(&plantModel.SundynixMyPlant{}).Where("user_id = ?", userId)
if req.Name != "" {
db = db.Where("name LIKE ?", "%"+req.Name+"%")
}
db.Count(&total)
current, pageSize := req.Current, req.PageSize
if current <= 0 {
current = 1
}
if pageSize <= 0 {
pageSize = 10
}
if err := db.Offset((current - 1) * pageSize).Limit(pageSize).Order("created_at DESC").Find(&plants).Error; err != nil {
return nil, fmt.Errorf("查询植物列表失败")
}
return map[string]interface{}{"list": plants, "total": total, "current": current, "size": pageSize}, nil
}