system_search_role_logic.go
3.1 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package role
import (
"context"
"fmt"
"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 SystemSearchRoleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemSearchRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSearchRoleLogic {
return &SystemSearchRoleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemSearchRoleLogic) SystemSearchRole(req *types.RoleSearchRequest) (resp *types.RoleSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
roles []*domain.Role
total int64
users []*domain.User
)
queryOptions := domain.IndexCompanyId(userToken.CompanyId)()
if req.Page != 0 && req.Size != 0 {
queryOptions.WithOffsetLimit(req.Page, req.Size)
}
total, roles, err = l.svcCtx.RoleRepository.Find(l.ctx, conn, queryOptions)
if err != nil {
return nil, xerr.NewErrMsgErr("搜索角色异常", err)
}
resp = &types.RoleSearchResponse{
Total: total,
List: make([]types.RoleItem, 0),
}
if req.Style == "simple" {
lo.ForEach(roles, func(item *domain.Role, index int) {
resp.List = append(resp.List, types.RoleItem{
Id: item.Id,
Name: item.Name,
})
})
return
}
roleIds := domain.Values(roles, func(item *domain.Role) int64 {
return item.Id
})
_, users, _ = l.svcCtx.UserRepository.FindByCompanyRoles(l.ctx, conn, userToken.CompanyId, roleIds, domain.NewQueryOptions().WithFindOnly())
lo.ForEach(roles, func(item *domain.Role, index int) {
role := NewTypesRole(item)
usersAll := make([]domain.User, 0)
for _, u := range users {
if lo.Contains(u.Roles, item.Id) {
usersAll = append(usersAll, *u)
}
}
role.UsersDesc = usersDesc(usersAll)
role.AuthsDesc = authsDesc(item)
resp.List = append(resp.List, role)
})
return
}
func usersDesc(usersAll []domain.User) string {
users := lo.Slice(usersAll, 0, 3)
var (
nameList = make([]string, 0)
desc string = " "
)
lo.ForEach(users, func(item domain.User, index int) {
nameList = append(nameList, item.Name)
})
if len(users) == 0 {
return desc
}
desc = fmt.Sprintf("%s%s", strings.Join(nameList, "、"),
lo.Ternary(len(usersAll) > 3, fmt.Sprintf("...共%d人", len(usersAll)), ""),
)
return desc
}
func authsDesc(role *domain.Role) string {
var (
nameList = make([]string, 0)
desc string = " "
)
lo.ForEach(role.Auths, func(item int64, index int) {
if auth := role.GetAuth(item); auth != nil {
nameList = append(nameList, auth.Name)
}
})
if len(nameList) == 0 {
return desc
}
desc = strings.Join(nameList, "、")
return desc
}