You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
63 lines
1.5 KiB
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package role
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/youruser/base/internal/svc"
|
|
"github.com/youruser/base/internal/types"
|
|
"github.com/youruser/base/model"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreateRoleLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 创建角色
|
|
func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
|
|
return &CreateRoleLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleRequest) (resp *types.RoleInfo, err error) {
|
|
// 检查编码唯一性
|
|
existing, _ := model.RoleFindOneByCode(l.ctx, l.svcCtx.DB, req.Code)
|
|
if existing != nil {
|
|
return nil, fmt.Errorf("角色编码 %s 已存在", req.Code)
|
|
}
|
|
|
|
role := &model.Role{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Description: req.Description,
|
|
SortOrder: req.SortOrder,
|
|
Status: 1,
|
|
}
|
|
|
|
_, err = model.RoleInsert(l.ctx, l.svcCtx.DB, role)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建角色失败: %v", err)
|
|
}
|
|
|
|
return &types.RoleInfo{
|
|
Id: role.Id,
|
|
Name: role.Name,
|
|
Code: role.Code,
|
|
Description: role.Description,
|
|
IsSystem: role.IsSystem,
|
|
SortOrder: role.SortOrder,
|
|
Status: role.Status,
|
|
CreatedAt: role.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: role.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}, nil
|
|
}
|
|
|