system_search_role_logic.go 2.7 KB
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
	)
	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
	}
	lo.ForEach(roles, func(item *domain.Role, index int) {
		role := NewTypesRole(item)
		role.UsersDesc = usersDesc(item)
		role.AuthsDesc = authsDesc(item)
		resp.List = append(resp.List, role)
	})
	return
}

func usersDesc(item *domain.Role) string {
	users := lo.Slice(item.Users, 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(item.Users) > 3, fmt.Sprintf("...共%d人", len(item.Users)), ""),
	)
	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
}