system_search_article_logic.go 2.5 KB
package article

import (
	"context"
	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *SystemSearchArticleLogic) SystemSearchArticle(req *types.SystemArticleSearchRequest) (resp *types.SystemArticleSearchResponse, err error) {
	var conn = l.svcCtx.DefaultDBConn()
	queryOptions := domain.NewQueryOptions().
		WithOffsetLimit(req.Page, req.Size).
		WithKV("title", req.Title).
		WithKV("authorId", req.Author).
		WithKV("beginCreatedAt", req.BeginTime).
		WithKV("endCreatedAt", req.EndTime)
	total, articles, err := l.svcCtx.ArticleRepository.Find(l.ctx, conn, req.CompanyId, queryOptions)
	if err != nil {
		return nil, xerr.NewErrMsgErr("搜索帖子异常", err)
	}
	resp = &types.SystemArticleSearchResponse{
		Total: int(total),
		List:  make([]types.SystemArticleSearch, 0),
	}
	authorIds := make([]int64, 0)
	lo.ForEach(articles, func(item *domain.Article, index int) {
		images := make([]string, 0)
		lo.ForEach(item.Images, func(img domain.Image, n int) {
			images = append(images, img.Url)
		})
		resp.List = append(resp.List, types.SystemArticleSearch{
			Id:           item.Id,
			Title:        item.Title,
			AuthorId:     item.AuthorId,
			Author:       item.Author.Name,
			Images:       images,
			CreatedAt:    item.CreatedAt,
			CountLove:    item.CountLove,
			CountComment: item.CountComment,
			Show:         int(item.Show),
			Tags:         nil,
			TargetUser:   int(item.TargetUser),
		})
		authorIds = append(authorIds, item.AuthorId)
	})
	//查询用户数据,重新赋值更新用户名称
	_, users, _ := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithFindOnly().WithKV("ids", authorIds))
	lo.ForEach(resp.List, func(item types.SystemArticleSearch, index int) {
		for _, user := range users {
			if user.Id == item.AuthorId {
				resp.List[index].Author = user.Name
			}
		}
	})
	return resp, nil
}