system_get_article_logic.go 4.1 KB
package article

import (
	"context"

	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

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

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

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

func (l *SystemGetArticleLogic) SystemGetArticle(req *types.SystemArticleGetRequest) (resp *types.SystemArticleGetResponse, err error) {
	var conn = l.svcCtx.DefaultDBConn()
	article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
	if err != nil {
		return nil, xerr.NewErrMsgErr("获取帖子异常", err)
	}
	images := make([]string, 0)
	lo.ForEach(article.Images, func(img domain.Image, n int) {
		images = append(images, img.Url)
	})
	resp = &types.SystemArticleGetResponse{
		Id:       article.Id,
		Title:    article.Title,
		AuthorId: article.AuthorId,
		Author: types.ArticleAuthor{
			Id:       article.Author.Id,
			Name:     article.Author.Name,
			Avatar:   article.Author.Avatar,
			Position: article.Author.Position,
			Company:  article.Author.Company,
		},
		CreatedAt:     article.CreatedAt,
		Section:       make([]types.ArticleSection, 0),
		Images:        images,
		WhoRead:       article.WhoRead,
		WhoReadInfo:   make([]types.UserShowName, 0),
		WhoReview:     article.WhoReview,
		WhoReviewInfo: make([]types.UserShowName, 0),
		Location: types.Location{
			Longitude: article.Location.Longitude,
			Latitude:  article.Location.Latitude,
			Descript:  article.Location.Descript,
		},
		CountLove:    article.CountLove,
		CountComment: article.CountComment,
		CountRead:    article.CountRead,
		Show:         int(article.Show),
		Tags:         make([]types.ArticleTagItem, 0),
		TargetUser:   int(article.TargetUser),
		Videos:       make([]types.Video, 0),
	}

	for _, val := range article.Videos {
		resp.Videos = append(resp.Videos, types.Video{
			Url:    val.Url,
			Cover:  val.Cover,
			Width:  val.Width,
			Height: val.Height,
		})
	}
	//标签
	if len(article.Tags) > 0 {
		_, tags, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, article.CompanyId, domain.NewQueryOptions().WithKV("ids", article.Tags))
		if err == nil && len(tags) > 0 {
			lo.ForEach(tags, func(tag *domain.ArticleTag, index int) {
				resp.Tags = append(resp.Tags, types.ArticleTagItem{
					Id:       tag.Id,
					Category: tag.Category,
					Name:     tag.Name,
					Image:    tag.Image.Url,
				})
			})
		}
	}
	//文章段落
	_, articleSections, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("articleId", req.Id))
	if err != nil {
		return nil, xerr.NewErrMsgErr("获取帖子异常", err)
	}
	lo.ForEach(articleSections, func(item *domain.ArticleSection, index int) {
		resp.Section = append(resp.Section, types.ArticleSection{
			Id:           item.Id,
			Content:      item.Content,
			SortBy:       item.SortBy,
			TotalComment: item.TotalComment,
		})
	})
	userIds := lo.Union(resp.WhoRead, resp.WhoReview)
	if len(userIds) > 0 {
		_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("ids", userIds))
		if err != nil {
			return nil, xerr.NewErrMsgErr("获取帖子异常", err)
		}
		userSlices := make(map[int64]types.UserShowName)
		lo.ForEach(users, func(user *domain.User, index int) {
			userSlices[user.Id] = types.UserShowName{
				Id:   int(user.Id),
				Name: user.Name,
			}
		})
		lo.ForEach(resp.WhoRead, func(userId int64, index int) {
			if value, ok := userSlices[userId]; ok {
				resp.WhoReadInfo = append(resp.WhoReadInfo, value)
			}
		})
		lo.ForEach(resp.WhoReview, func(userId int64, index int) {
			if value, ok := userSlices[userId]; ok {
				resp.WhoReviewInfo = append(resp.WhoReviewInfo, value)
			}
		})
	}
	return resp, nil
}