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.
69 lines
1.4 KiB
69 lines
1.4 KiB
4 years ago
|
package models
|
||
|
|
||
|
import (
|
||
|
"github.com/beego/beego/v2/client/orm"
|
||
|
"github.com/beego/beego/v2/core/logs"
|
||
|
"yixiang.co/yshop/untils"
|
||
|
)
|
||
|
|
||
4 years ago
|
type SysDept struct {
|
||
4 years ago
|
Id int64 `json:"id"`
|
||
|
Name string `json:"name" valid:"Required;"`
|
||
|
Pid int64 `json:"pid"`
|
||
|
Enabled int8 `json:"enabled" valid:"Required;"`
|
||
4 years ago
|
Children []SysDept `orm:"-" json:"children"`
|
||
4 years ago
|
Label string `orm:"-" json:"label"`
|
||
|
BaseModel
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
4 years ago
|
orm.RegisterModel(new(SysDept))
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func GetAllDepts(name string, enabled int8) []SysDept {
|
||
|
var depts []SysDept
|
||
4 years ago
|
o := orm.NewOrm()
|
||
4 years ago
|
qs := o.QueryTable("sys_dept").Filter("is_del",0)
|
||
4 years ago
|
if name != "" {
|
||
|
qs = qs.Filter("name",name)
|
||
|
}
|
||
|
if enabled > -1 {
|
||
|
qs = qs.Filter("enabled",enabled)
|
||
|
}
|
||
|
qs.All(&depts)
|
||
|
return RecursionDeptList(depts,0)
|
||
|
}
|
||
|
|
||
|
//递归函数
|
||
4 years ago
|
func RecursionDeptList(data []SysDept, pid int64) []SysDept {
|
||
|
var listTree = make([]SysDept,0)
|
||
4 years ago
|
for _, value := range data {
|
||
|
value.Label = value.Name
|
||
|
if value.Pid == pid {
|
||
|
value.Children = RecursionDeptList(data, value.Id)
|
||
|
listTree = append(listTree, value)
|
||
|
}
|
||
|
}
|
||
|
return listTree
|
||
|
}
|
||
|
|
||
4 years ago
|
func AddDept(m *SysDept) (id int64, err error) {
|
||
4 years ago
|
o := orm.NewOrm()
|
||
|
id, err = o.Insert(m)
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
func UpdateByDept(m *SysDept) (err error) {
|
||
4 years ago
|
o := orm.NewOrm()
|
||
|
_, err = o.Update(m)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func DelByDept(ids []int64) (err error) {
|
||
|
str := untils.ReturnQ(len(ids))
|
||
|
logs.Info(str)
|
||
|
o := orm.NewOrm()
|
||
4 years ago
|
_, err = o.Raw("UPDATE sys_dept SET is_del = ? WHERE id in("+str+")", 1, ids).Exec()
|
||
4 years ago
|
return
|
||
|
}
|