作者 tangxvhui

获取评论信息失败

... ... @@ -3,8 +3,10 @@ package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"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/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"
... ... @@ -16,6 +18,7 @@ type MiniArticleMarkListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
userCache map[int64]*domain.User
}
func NewMiniArticleMarkListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleMarkListLogic {
... ... @@ -23,6 +26,7 @@ func NewMiniArticleMarkListLogic(ctx context.Context, svcCtx *svc.ServiceContext
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
userCache: make(map[int64]*domain.User, 0),
}
}
... ... @@ -36,10 +40,16 @@ func (l *MiniArticleMarkListLogic) MiniArticleMarkList(req *types.MiniArticleMar
if err != nil {
return nil, err
}
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, l.svcCtx.DefaultDBConn(), userToken.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
resp = &types.MiniArticleMarkListResponse{}
resp.Total = total
resp.List = make([]types.MiniArticleMarkItem, 0)
var author domain.User
for _, item := range list {
author = l.getAuthor(l.svcCtx.DefaultDBConn(), item.Author.Id)
to := types.MiniArticleMarkItem{
Id: item.Id,
CompanyId: item.CompanyId,
... ... @@ -48,10 +58,10 @@ func (l *MiniArticleMarkListLogic) MiniArticleMarkList(req *types.MiniArticleMar
Author: types.SimpleUser{
Id: item.Author.Id,
CompanyId: item.Author.CompanyId,
CompanyName: item.Author.Company,
Name: item.Author.Name,
Avatar: item.Author.Avatar,
Position: item.Author.Position,
CompanyName: companyInfo.Name,
Name: author.Name,
Avatar: author.Avatar,
Position: author.Position,
},
UpdatedAt: item.UpdatedAt,
}
... ... @@ -59,3 +69,15 @@ func (l *MiniArticleMarkListLogic) MiniArticleMarkList(req *types.MiniArticleMar
}
return resp, nil
}
func (l *MiniArticleMarkListLogic) getAuthor(conn transaction.Conn, userid int64) domain.User {
if u, ok := l.userCache[userid]; ok {
return *u
}
toUser, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userid)
if err == nil {
l.userCache[toUser.Id] = toUser
return *toUser
}
return domain.User{}
}
... ...