65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
sysModel "sundynix-micro-go/app/system/model"
|
|
"sundynix-micro-go/app/system/rpc/internal/svc"
|
|
"sundynix-micro-go/app/system/rpc/system"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type GetRolesByUserIdLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetRolesByUserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRolesByUserIdLogic {
|
|
return &GetRolesByUserIdLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 获取用户角色列表
|
|
func (l *GetRolesByUserIdLogic) GetRolesByUserId(in *system.GetRolesByUserIdReq) (*system.GetRolesByUserIdResp, error) {
|
|
// 查询用户角色关联
|
|
var userRoles []sysModel.SundynixUserRole
|
|
if err := l.svcCtx.DB.Where("user_id = ?", in.UserId).Find(&userRoles).Error; err != nil {
|
|
l.Errorf("查询用户角色失败: %v", err)
|
|
return nil, status.Error(codes.Internal, "查询用户角色失败")
|
|
}
|
|
|
|
if len(userRoles) == 0 {
|
|
return &system.GetRolesByUserIdResp{Roles: []*system.RoleInfo{}}, nil
|
|
}
|
|
|
|
roleIds := make([]string, 0, len(userRoles))
|
|
for _, ur := range userRoles {
|
|
roleIds = append(roleIds, ur.RoleID)
|
|
}
|
|
|
|
var roles []sysModel.SundynixRole
|
|
if err := l.svcCtx.DB.Where("id IN ?", roleIds).Find(&roles).Error; err != nil {
|
|
l.Errorf("查询角色信息失败: %v", err)
|
|
return nil, status.Error(codes.Internal, "查询角色信息失败")
|
|
}
|
|
|
|
roleInfos := make([]*system.RoleInfo, 0, len(roles))
|
|
for _, r := range roles {
|
|
roleInfos = append(roleInfos, &system.RoleInfo{
|
|
Id: r.ID,
|
|
Name: r.Name,
|
|
Code: r.Code,
|
|
Sort: int32(r.Sort),
|
|
})
|
|
}
|
|
|
|
return &system.GetRolesByUserIdResp{Roles: roleInfos}, nil
|
|
}
|