package logic import ( "context" plantModel "sundynix-micro-go/app/plant/model" "sundynix-micro-go/app/plant/rpc/internal/svc" "sundynix-micro-go/app/plant/rpc/plant" "github.com/zeromicro/go-zero/core/logx" ) type GetPostListLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostListLogic { return &GetPostListLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // 帖子列表 func (l *GetPostListLogic) GetPostList(in *plant.PostListReq) (*plant.PostListResp, error) { db := l.svcCtx.DB.Model(&plantModel.Post{}) if in.Keyword != "" { db = db.Where("title like ? OR content like ?", "%"+in.Keyword+"%", "%"+in.Keyword+"%") } if in.UserId != "" { db = db.Where("user_id = ?", in.UserId) } var total int64 db.Count(&total) pageSize := int(in.PageSize) if pageSize <= 0 { pageSize = 20 } offset := (int(in.Current) - 1) * pageSize if offset < 0 { offset = 0 } var list []plantModel.Post if err := db.Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list).Error; err != nil { return nil, err } var result []*plant.PostInfo for _, item := range list { result = append(result, &plant.PostInfo{ Id: item.ID, UserId: item.UserID, Title: item.Title, Content: item.Content, ViewCount: int32(item.ViewCount), CommentCount: int32(item.CommentCount), LikeCount: int32(item.LikeCount), StarCount: int32(item.StarCount), Location: item.Location, CreatedAt: item.CreatedAt.Unix(), }) } return &plant.PostListResp{List: result, Total: total}, nil }