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 }