-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
121 lines (111 loc) · 2.66 KB
/
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
package menu
import (
"errors"
"github.com/baiy/Cadmin-server-go/models"
"github.com/baiy/Cadmin-server-go/models/menuRelate"
"github.com/doug-martin/goqu/v9"
)
type Model struct {
models.Model
ParentId int `json:"parent_id"`
Name string `json:"name"`
Url string `json:"url"`
Icon string `json:"icon"`
Description string `json:"description"`
Sort int `json:"sort"`
}
func Add(parentId int, name, url, icon, description string) error {
if parentId != 0 {
exist, err := GetById(parentId)
if err != nil {
return errors.New("父菜单不存在")
}
if exist.Url != "" {
return errors.New("父菜单不是目录类型菜单")
}
}
_, err := models.Db.Insert("admin_menu").Rows(
goqu.Record{
"parent_id": parentId,
"name": name,
"url": url,
"icon": icon,
"description": description,
"sort": getNewSort(parentId),
},
).Executor().Exec()
return err
}
func Updata(id int, parentId int, name, url, icon, description string) error {
if parentId != 0 {
exist, err := GetById(parentId)
if err != nil {
return errors.New("父菜单不存在")
}
if exist.Url != "" {
return errors.New("父菜单不是目录类型菜单")
}
}
_, err := models.Db.Update("admin_menu").Where(goqu.Ex{"id": id}).Set(
goqu.Record{
"parent_id": parentId,
"name": name,
"url": url,
"icon": icon,
"description": description,
},
).Executor().Exec()
return err
}
func Remove(id int) error {
_, err := models.Db.Delete("admin_menu").Where(goqu.Ex{
"id": id,
}).Executor().Exec()
if err == nil {
_ = menuRelate.Remove(id, 0)
}
return err
}
func All() ([]*Model, error) {
m := make([]*Model, 0)
err := models.Db.From("admin_menu").ScanStructs(&m)
return m, err
}
func Sort(id, sort int) error {
_, err := models.Db.Update("admin_menu").Set(goqu.Record{"sort": sort}).Where(goqu.Ex{
"id": id,
}).Executor().Exec()
return err
}
func getNewSort(parentId int) int {
model := new(Model)
found, err := models.Db.From("admin_menu").Where(goqu.Ex{
"parent_id": parentId,
}).Order(goqu.I("sort").Desc()).ScanStruct(model)
if err == nil && found {
return model.Sort + 1
}
return 0
}
func GetById(id int) (model *Model, err error) {
model = new(Model)
found, err := models.Db.From("admin_menu").Where(goqu.Ex{
"id": id,
}).ScanStruct(model)
if err == nil {
if !found {
err = errors.New("菜单不存在")
}
}
return
}
func GetLists(ids []int) ([]*Model, error) {
model := make([]*Model, 0)
if len(ids) == 0 {
return model, nil
}
err := models.Db.From("admin_menu").Where(goqu.Ex{
"id": ids,
}).ScanStructs(&model)
return model, err
}