mini_my_like_logic.go 3.1 KB
package user

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/contextdata"

	"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 MiniMyLikeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *MiniMyLikeLogic) MiniMyLike(req *types.MiniMyLikeRequest) (resp *types.MiniMyLikeResponse, err error) {
	var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
	var conn = l.svcCtx.DefaultDBConn()

	total, list, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, domain.NewQueryOptions().
		WithOffsetLimit(req.Page, req.Size).
		//WithKV("companyId", userToken.CompanyId).  不存在公司id
		WithKV("userId", userToken.UserId))
	if err != nil {
		return nil, err
	}

	resp = &types.MiniMyLikeResponse{}
	resp.Total = total
	resp.List = make([]types.MyLikeItem, 0)
	if total == 0 || len(list) == 0 {
		return resp, nil
	}

	var userMap = make(map[int64]*domain.User)
	var articleMap = make(map[int64]*domain.Article)
	var commentMap = make(map[int64]*domain.ArticleComment)

	lo.ForEach(list, func(item *domain.UserLoveFlag, index int) {
		var (
			user    *domain.User
			article *domain.Article
			comment *domain.ArticleComment
		)
		user, _ = domain.LazyLoad(userMap, l.ctx, conn, item.ToUserId, l.svcCtx.UserRepository.FindOne)
		article, _ = domain.LazyLoad(articleMap, l.ctx, conn, item.ArticleId, l.svcCtx.ArticleRepository.FindOne)
		// 点赞评论
		if item.CommentId != 0 {
			comment, _ = domain.LazyLoad(commentMap, l.ctx, conn, item.CommentId, l.svcCtx.ArticleCommentRepository.FindOne)
		}

		resp.List = append(resp.List, l.NewItemSimple(item, user, article, comment))
	})

	return resp, nil
}

func (l *MiniMyLikeLogic) NewItemSimple(love *domain.UserLoveFlag, user *domain.User, article *domain.Article, comment *domain.ArticleComment) types.MyLikeItem {
	item := types.MyLikeItem{
		UserId:    love.ToUserId,
		ArticleId: love.ArticleId,
		CommentId: love.CommentId,
		CreatedAt: love.CreatedAt,
	}

	if user != nil {
		item.User = &types.SimpleUser{
			Id:        user.Id,
			CompanyId: user.CompanyId,
			Name:      user.Name,
			Avatar:    user.Avatar,
			Position:  user.Position,
		}
	}

	if article != nil {
		item.Article = &types.SimpleArticle{
			Id:           article.Id,
			Title:        article.Title,
			Summary:      article.Summary,
			CountLove:    article.CountLove,
			CountComment: article.CountComment,
			Show:         int(article.Show),
		}
	}

	if comment != nil {
		item.Comment = &types.SimpleComment{
			Id:           comment.Id,
			Content:      comment.Content,
			CountLove:    comment.CountUserLove,
			CountComment: comment.CountReply,
			Show:         int(comment.Show),
		}
	}

	return item
}