-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
get_user_list_logic.go
79 lines (63 loc) · 1.91 KB
/
get_user_list_logic.go
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
package user
import (
"context"
"github.com/suyuan32/simple-admin-core/pkg/ent/predicate"
"github.com/suyuan32/simple-admin-core/pkg/ent/user"
"github.com/suyuan32/simple-admin-core/pkg/i18n"
"github.com/suyuan32/simple-admin-core/pkg/statuserr"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserListLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
return &GetUserListLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetUserListLogic) GetUserList(in *core.GetUserListReq) (*core.UserListResp, error) {
var predicates []predicate.User
if in.Mobile != "" {
predicates = append(predicates, user.MobileEQ(in.Mobile))
}
if in.Username != "" {
predicates = append(predicates, user.UsernameContains(in.Username))
}
if in.Email != "" {
predicates = append(predicates, user.EmailEQ(in.Email))
}
if in.Nickname != "" {
predicates = append(predicates, user.NicknameContains(in.Nickname))
}
if in.RoleId != 0 {
predicates = append(predicates, user.RoleIDEQ(in.RoleId))
}
users, err := l.svcCtx.DB.User.Query().Where(predicates...).Page(l.ctx, in.Page, in.PageSize)
if err != nil {
logx.Error(err.Error())
return nil, statuserr.NewInternalError(i18n.DatabaseError)
}
resp := &core.UserListResp{}
resp.Total = users.PageDetails.Total
for _, v := range users.List {
resp.Data = append(resp.Data, &core.UserInfoResp{
Id: v.ID.String(),
Avatar: v.Avatar,
RoleId: v.RoleID,
Mobile: v.Mobile,
Email: v.Email,
Status: uint64(v.Status),
Username: v.Username,
Nickname: v.Nickname,
CreatedAt: v.CreatedAt.UnixMilli(),
UpdatedAt: v.UpdatedAt.UnixMilli(),
})
}
return resp, nil
}