实现角色接口增删改查
This commit is contained in:
162
internal/logic/role/role.go
Normal file
162
internal/logic/role/role.go
Normal file
@ -0,0 +1,162 @@
|
||||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sRole struct {
|
||||
}
|
||||
|
||||
func New() service.IRole {
|
||||
return &sRole{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterRole(New())
|
||||
}
|
||||
|
||||
func (s *sRole) Create(ctx context.Context, in *model.RoleCreateInput) (out *model.CreateOut, err error) {
|
||||
// 检查角色编码是否已存在
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: in.Code}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("新增角色查重出现异常")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("角色编码已存在")
|
||||
}
|
||||
|
||||
// 创建角色
|
||||
id, err := dao.Roles.Ctx(ctx).InsertAndGetId(do.Roles{
|
||||
Name: in.Name,
|
||||
Code: in.Code,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
IsDeletable: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("创建角色失败")
|
||||
}
|
||||
return &model.CreateOut{Id: id}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) Delete(ctx context.Context, in *model.RoleDeleteInput) (out *model.DeleteOut, err error) {
|
||||
// 检查角色是否存在且可删除
|
||||
role, err := dao.Roles.Ctx(ctx).WherePri(in.Id).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if role.IsEmpty() {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
if !role["is_deletable"].Bool() {
|
||||
return nil, ecode.Params.Sub("该角色不可删除")
|
||||
}
|
||||
|
||||
if err = dao.Roles.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
// 删除角色
|
||||
if _, err = dao.Roles.Ctx(ctx).Where(do.Roles{Id: in.Id}).Delete(); err != nil {
|
||||
err = ecode.Fail.Sub("删除角色失败")
|
||||
}
|
||||
// TODO : 删除角色后续需要处理的操作
|
||||
return
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.DeleteOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) BatchDelete(ctx context.Context, in *model.BatchDeleteIn) (out *model.DeleteOut, err error) {
|
||||
|
||||
// 检查角色是否存在且可删除
|
||||
roles, err := dao.Roles.Ctx(ctx).WhereIn(dao.Roles.Columns().Id, in.Ids).All()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if len(roles) == 0 {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
// 检查是否有不可删除的角色
|
||||
for _, role := range roles {
|
||||
if !role["is_deletable"].Bool() {
|
||||
return nil, ecode.Params.Sub("存在不可删除的角色")
|
||||
}
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
if err = dao.Roles.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// 删除角色
|
||||
if _, err = dao.Roles.Ctx(ctx).WhereIn(dao.Roles.Columns().Id, in.Ids).Delete(); err != nil {
|
||||
return ecode.Fail.Sub("删除角色失败")
|
||||
}
|
||||
// TODO : 删除角色后续需要处理的操作
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.DeleteOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) Update(ctx context.Context, in *model.RoleUpdateInput) (out *model.UpdateOut, err error) {
|
||||
// 检查角色是否存在
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Id: in.Id}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
|
||||
// 检查角色编码是否已存在(排除自身)
|
||||
exist, err = dao.Roles.Ctx(ctx).
|
||||
Where(do.Roles{Code: in.Code}).
|
||||
WhereNot(dao.Roles.Columns().Id, in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色编码失败")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("角色编码已存在")
|
||||
}
|
||||
|
||||
// 更新角色
|
||||
if _, err = dao.Roles.Ctx(ctx).
|
||||
Where(do.Roles{Id: in.Id}).
|
||||
Update(do.Roles{
|
||||
Name: in.Name,
|
||||
Code: in.Code,
|
||||
Description: in.Description,
|
||||
Status: in.Status,
|
||||
}); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新角色失败")
|
||||
}
|
||||
|
||||
return &model.UpdateOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sRole) GetRoleList(ctx context.Context, in *model.RoleListInput) (out *model.RoleListOutput, err error) {
|
||||
list := make([]model.Role, 0)
|
||||
var total int
|
||||
// 构建查询条件
|
||||
orm := dao.Roles.Ctx(ctx)
|
||||
if in.Status > 0 {
|
||||
orm = orm.Where(dao.Roles.Columns().Status, in.Status)
|
||||
}
|
||||
|
||||
// 获取分页数据
|
||||
if err = orm.Page(in.Page, in.Size).ScanAndCount(&list, &total, false); err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色列表失败")
|
||||
}
|
||||
return &model.RoleListOutput{
|
||||
List: list,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user