system_user_search_logic.go
2.0 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
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/pkg/xerr"
"strings"
"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 SystemUserSearchLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUserSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserSearchLogic {
return &SystemUserSearchLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUserSearchLogic) SystemUserSearch(req *types.SystemUserSearchRequest) (resp *types.SystemUserSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
users []*domain.User
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
total int64
roleMap = make(map[int64]*domain.Role)
)
queryOptions := domain.IndexCompanyId(userToken.CompanyId)().WithOffsetLimit(req.Page, req.Size).WithOrder("id desc").
WithKV("likeName", req.Name).
WithKV("likePhone", req.Phone).
WithKV("likePosition", req.Position).
WithKV("enable", req.Enable).
WithKV("departmentId", req.DepartmentId)
if total, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, queryOptions); err != nil {
return nil, xerr.NewErr(err)
}
resp = &types.SystemUserSearchResponse{
Total: total,
List: make([]types.SystemUser, 0),
}
lo.ForEach(users, func(item *domain.User, index int) {
var roles []string
for _, id := range item.Roles {
if role, _ := domain.LazyLoad(roleMap, l.ctx, conn, id, l.svcCtx.RoleRepository.FindOne); role != nil {
roles = append(roles, role.Name)
}
}
user := NewSystemUser(item)
user.RolesDesc = strings.Join(roles, "、")
resp.List = append(resp.List, user)
})
return
}