mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-02-02 18:28:41 +08:00
fix 角色移动
This commit is contained in:
parent
c967a5c8f0
commit
5aa7ace31b
@ -7,6 +7,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
"github.com/gogf/gf/v2/encoding/gjson"
|
"github.com/gogf/gf/v2/encoding/gjson"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
@ -217,7 +218,24 @@ func (s *sAdminRole) Edit(ctx context.Context, in *role.EditReq) (err error) {
|
|||||||
|
|
||||||
// 修改
|
// 修改
|
||||||
if in.Id > 0 {
|
if in.Id > 0 {
|
||||||
_, err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
// 获取父级tree
|
||||||
|
var pTree gdb.Value
|
||||||
|
pTree, err = dao.AdminRole.Ctx(ctx).Where("id", in.Pid).Fields("tree").Value()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
in.Tree = fmt.Sprintf("%str_%v ", pTree.String(), in.Id)
|
||||||
|
|
||||||
|
err = dao.AdminRole.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||||
|
// 更新数据
|
||||||
|
_, err = dao.AdminRole.Ctx(ctx).Where("id", in.Id).Data(in).Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果当前角色有子级,更新子级tree关系树
|
||||||
|
return updateRoleChildrenTree(ctx, in.Id, in.Level, in.Tree)
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,6 +244,27 @@ func (s *sAdminRole) Edit(ctx context.Context, in *role.EditReq) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateRoleChildrenTree(ctx context.Context, _id int64, _level int, _tree string) (err error) {
|
||||||
|
var list []*entity.AdminDept
|
||||||
|
err = dao.AdminRole.Ctx(ctx).Where("pid", _id).Scan(&list)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, child := range list {
|
||||||
|
child.Level = _level + 1
|
||||||
|
child.Tree = fmt.Sprintf("%str_%v ", _tree, child.Id)
|
||||||
|
_, err = dao.AdminRole.Ctx(ctx).Where("id", child.Id).Data("level", child.Level, "tree", child.Tree).Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = updateRoleChildrenTree(ctx, child.Id, child.Level, child.Tree)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sAdminRole) Delete(ctx context.Context, in *role.DeleteReq) (err error) {
|
func (s *sAdminRole) Delete(ctx context.Context, in *role.DeleteReq) (err error) {
|
||||||
if in.Id <= 0 {
|
if in.Id <= 0 {
|
||||||
return gerror.New("ID不正确!")
|
return gerror.New("ID不正确!")
|
||||||
|
Loading…
Reference in New Issue
Block a user