v1.1
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CreateRoleLogic struct {
|
||||
@@ -37,37 +38,36 @@ func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleReq) (resp *types.Crea
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("角色名称已存在"), "创建角色失败, 角色名称已存在: %v", err)
|
||||
}
|
||||
// 创建角色
|
||||
role := &model.AdminRole{
|
||||
RoleName: req.RoleName,
|
||||
RoleCode: req.RoleCode,
|
||||
Description: req.Description,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
var roleId int64
|
||||
role := &model.AdminRole{
|
||||
Id: uuid.NewString(),
|
||||
RoleName: req.RoleName,
|
||||
RoleCode: req.RoleCode,
|
||||
Description: req.Description,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
}
|
||||
var roleId string
|
||||
// 使用事务创建角色和关联菜单
|
||||
err = l.svcCtx.AdminRoleModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 创建角色
|
||||
result, err := l.svcCtx.AdminRoleModel.Insert(ctx, session, role)
|
||||
if err != nil {
|
||||
return errors.New("插入新角色失败")
|
||||
}
|
||||
roleId, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return errors.New("获取新角色ID失败")
|
||||
}
|
||||
_, err := l.svcCtx.AdminRoleModel.Insert(ctx, session, role)
|
||||
if err != nil {
|
||||
return errors.New("插入新角色失败")
|
||||
}
|
||||
roleId = role.Id
|
||||
|
||||
// 创建角色菜单关联
|
||||
if len(req.MenuIds) > 0 {
|
||||
for _, menuId := range req.MenuIds {
|
||||
roleMenu := &model.AdminRoleMenu{
|
||||
RoleId: roleId,
|
||||
MenuId: menuId,
|
||||
}
|
||||
_, err = l.svcCtx.AdminRoleMenuModel.Insert(ctx, session, roleMenu)
|
||||
if err != nil {
|
||||
return errors.New("插入角色菜单关联失败")
|
||||
}
|
||||
roleMenu := &model.AdminRoleMenu{
|
||||
Id: uuid.NewString(),
|
||||
RoleId: roleId,
|
||||
MenuId: menuId,
|
||||
}
|
||||
_, err = l.svcCtx.AdminRoleMenuModel.Insert(ctx, session, roleMenu)
|
||||
if err != nil {
|
||||
return errors.New("插入角色菜单关联失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleReq) (resp *types.Crea
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建角色失败: %v", err)
|
||||
}
|
||||
|
||||
return &types.CreateRoleResp{
|
||||
Id: roleId,
|
||||
}, nil
|
||||
return &types.CreateRoleResp{
|
||||
Id: roleId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user