mini_user_following_logic.go
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package user
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/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"strings"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserFollowingLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserFollowingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserFollowingLogic {
return &MiniUserFollowingLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserFollowingLogic) MiniUserFollowing(req *types.MiniUserFollowedSearchRequest) (resp *types.MiniUserFollowedSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
user *domain.User
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
companyMap = make(map[int64]*domain.Company)
)
if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
return nil, xerr.NewErrMsgErr("用户不存在", err)
}
var (
users = user.Following
total = int64(len(users))
offset, limit = domain.OffsetLimit(req.Page, req.Size)
)
resp = &types.MiniUserFollowedSearchResponse{
Total: total,
List: make([]*types.UserFollowItem, 0),
}
for {
if offset > len(user.Following) {
break
}
users = lo.Slice(users, offset, offset+limit)
lo.ForEach(users, func(item int64, index int) {
if foundUser, _ := l.svcCtx.UserRepository.FindOne(l.ctx, conn, item); foundUser != nil {
if len(req.Name) > 0 && !strings.Contains(foundUser.Name, req.Name) {
return
}
var companyName = ""
if company, _ := domain.LazyLoad(companyMap, l.ctx, conn, foundUser.CompanyId, l.svcCtx.CompanyRepository.FindOne); company != nil {
companyName = company.Name
}
resp.List = append(resp.List, &types.UserFollowItem{
Id: foundUser.Id,
Name: foundUser.Name,
CompanyName: companyName,
Avatar: foundUser.Avatar,
Position: foundUser.Position,
Followed: true,
//MutualFollowed: lo.Contains(user.Following, foundUser.Id),
})
}
})
if len(resp.List) >= limit {
break
}
req.Page += 1
offset, limit = domain.OffsetLimit(req.Page, req.Size)
}
return
}