system_get_role_logic.go 1.7 KB
package role

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"

	"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 SystemGetRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemGetRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemGetRoleLogic {
	return &SystemGetRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemGetRoleLogic) SystemGetRole(req *types.RoleGetRequest) (resp *types.RoleGetResponse, err error) {
	var (
		conn      = l.svcCtx.DefaultDBConn()
		role      *domain.Role
		users     []*domain.User
		userToken = contextdata.GetUserTokenFromCtx(l.ctx)
	)
	if role, err = l.svcCtx.RoleRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("角色不存在", err)
	}
	_, users, _ = l.svcCtx.UserRepository.FindByCompanyRoles(l.ctx, conn, userToken.CompanyId, []int64{role.Id}, domain.NewQueryOptions().WithFindOnly())
	typesRole := NewTypesRole(role)
	lo.ForEach(users, func(item *domain.User, index int) {
		typesRole.Users = append(typesRole.Users, types.RoleUser{
			Id:   item.Id,
			Name: item.Name,
		})
	})
	resp = &types.RoleGetResponse{
		Role: typesRole,
	}

	for _, auth := range domain.Auths {
		resp.AuthList = append(resp.AuthList, types.Auth{
			Id:   auth.Id,
			Name: auth.Name,
			Code: auth.Code,
		})
	}
	return
}