/
b_menu.go
165 lines (140 loc) · 3.92 KB
/
b_menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package internal
import (
"context"
"github.com/wanhello/iris-admin/internal/app/errors"
"github.com/wanhello/iris-admin/internal/app/model"
"github.com/wanhello/iris-admin/internal/app/schema"
"github.com/wanhello/iris-admin/pkg/util"
)
// NewMenu 创建菜单管理实例
func NewMenu(
trans model.ITrans,
mMenu model.IMenu,
) *Menu {
return &Menu{
TransModel: trans,
MenuModel: mMenu,
}
}
// Menu 菜单管理
type Menu struct {
TransModel model.ITrans
MenuModel model.IMenu
}
// Query 查询数据
func (a *Menu) Query(ctx context.Context, params schema.MenuQueryParam, opts ...schema.MenuQueryOptions) (*schema.MenuQueryResult, error) {
return a.MenuModel.Query(ctx, params, opts...)
}
// Get 查询指定数据
func (a *Menu) Get(ctx context.Context, recordID string, opts ...schema.MenuQueryOptions) (*schema.Menu, error) {
item, err := a.MenuModel.Get(ctx, recordID, opts...)
if err != nil {
return nil, err
} else if item == nil {
return nil, errors.ErrNotFound
}
return item, nil
}
func (a *Menu) getSep() string {
return "/"
}
func (a *Menu) joinParentPath(ppath, code string) string {
if ppath != "" {
ppath += a.getSep()
}
return ppath + code
}
// 获取父级路径
func (a *Menu) getParentPath(ctx context.Context, parentID string) (string, error) {
if parentID == "" {
return "", nil
}
pitem, err := a.MenuModel.Get(ctx, parentID)
if err != nil {
return "", err
} else if pitem == nil {
return "", errors.ErrInvalidParent
}
return a.joinParentPath(pitem.ParentPath, pitem.RecordID), nil
}
func (a *Menu) getUpdate(ctx context.Context, recordID string) (*schema.Menu, error) {
return a.Get(ctx, recordID, schema.MenuQueryOptions{
IncludeActions: true,
IncludeResources: true,
})
}
// Create 创建数据
func (a *Menu) Create(ctx context.Context, item schema.Menu) (*schema.Menu, error) {
parentPath, err := a.getParentPath(ctx, item.ParentID)
if err != nil {
return nil, err
}
item.ParentPath = parentPath
item.RecordID = util.MustUUID()
err = a.MenuModel.Create(ctx, item)
if err != nil {
return nil, err
}
return a.getUpdate(ctx, item.RecordID)
}
// Update 更新数据
func (a *Menu) Update(ctx context.Context, recordID string, item schema.Menu) (*schema.Menu, error) {
if recordID == item.ParentID {
return nil, errors.ErrInvalidParent
}
oldItem, err := a.MenuModel.Get(ctx, recordID)
if err != nil {
return nil, err
} else if oldItem == nil {
return nil, errors.ErrNotFound
}
item.ParentPath = oldItem.ParentPath
err = ExecTrans(ctx, a.TransModel, func(ctx context.Context) error {
// 如果父级更新,需要更新当前节点及节点下级的父级路径
if item.ParentID != oldItem.ParentID {
parentPath, err := a.getParentPath(ctx, item.ParentID)
if err != nil {
return err
}
item.ParentPath = parentPath
opath := a.joinParentPath(oldItem.ParentPath, oldItem.RecordID)
result, err := a.MenuModel.Query(ctx, schema.MenuQueryParam{
PrefixParentPath: opath,
})
if err != nil {
return err
}
npath := a.joinParentPath(item.ParentPath, item.RecordID)
for _, menu := range result.Data {
npath2 := npath + menu.ParentPath[len(opath):]
err = a.MenuModel.UpdateParentPath(ctx, menu.RecordID, npath2)
if err != nil {
return err
}
}
}
return a.MenuModel.Update(ctx, recordID, item)
})
if err != nil {
return nil, err
}
return a.getUpdate(ctx, recordID)
}
// Delete 删除数据
func (a *Menu) Delete(ctx context.Context, recordID string) error {
oldItem, err := a.MenuModel.Get(ctx, recordID)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
}
result, err := a.MenuModel.Query(ctx, schema.MenuQueryParam{
ParentID: &recordID,
}, schema.MenuQueryOptions{PageParam: &schema.PaginationParam{PageSize: -1}})
if err != nil {
return err
} else if result.PageResult.Total > 0 {
return errors.ErrNotAllowDeleteWithChild
}
return a.MenuModel.Delete(ctx, recordID)
}