123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package dao
- // Menu 角色
- type Menu struct {
- ID int `gorm:"type:int;primary_key" json:"id"` //编号
- ParentId int `gorm:"type:int;default 0" json:"parentId"` //父级菜单
- Code string `gorm:"type:varchar(255)" json:"code"` //菜单编号
- Name string `gorm:"type:varchar(255)" json:"name"` //菜单名称
- Alias string `gorm:"type:varchar(255)" json:"alias"` //菜单别名
- Path string `gorm:"type:varchar(255)" json:"path"` //请求地址
- Source string `gorm:"type:varchar(255)" json:"source"` //菜单资源
- Sort int `gorm:"type:int" json:"sort"` //排序
- Category int `gorm:"type:int" json:"category"` //菜单类型
- Action int `gorm:"type:int;default 0" json:"action"` //操作按钮类型
- IsOpen int `gorm:"type:int;default 1" json:"isOpen"` //是否打开新页面
- Remark string `gorm:"type:varchar(255)" json:"remark"` //备注
- IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除
- }
- func (Menu) TableName() string {
- return "menu"
- }
- func (c *Menu) Get() error {
- return Db.Model(&c).Where("id = ?", c.ID).Find(&c).Error
- }
- func (c *Menu) Save() error {
- return Db.Model(&c).Save(&c).Error
- }
- func (c *Menu) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *Menu) GetAll() ([]Menu, error) {
- var menus []Menu
- err := Db.Model(&c).Where(" is_deleted = 0 and category = 1").Order("sort").Find(&menus).Error
- return menus, err
- }
- func (c *Menu) GetByParentId() ([]Menu, error) {
- var menus []Menu
- db := Db.Model(&c).Where(" is_deleted = 0")
- if c.ParentId >= 0 {
- db = db.Where("parent_id = ?", c.ParentId)
- }
- if c.Name != "" {
- db = db.Where("name like ?", "%"+c.Name+"%")
- }
- if c.Code != "" {
- db = db.Where("code like ?", "%"+c.Code+"%")
- }
- if c.Alias != "" {
- db = db.Where("alias like ?", "%"+c.Alias+"%")
- }
- err := db.Order("sort").Find(&menus).Error
- return menus, err
- }
- func (c *Menu) HasChildren(parentIds []int) (map[int]bool, error) {
- type hasChildren struct {
- ParentId int `gorm:"type:int"`
- Count int `gorm:"type:int"`
- }
- var result []hasChildren
- err := Db.Table("menu").Select("parent_id, count(*) count").Where(" is_deleted = 0 and parent_id in (?)", parentIds).Group("parent_id").Find(&result).Error
- m := make(map[int]bool)
- for _, item := range result {
- m[item.ParentId] = item.Count > 0
- }
- return m, err
- }
- // todo 查询菜单树,只查id,parent_id和name
- func (c *Menu) GetMenuTree() ([]Menu, error) {
- var menus []Menu
- err := Db.Model(&c).Select("id,name,parent_id").Where(" is_deleted = 0 and category = 1").Order("sort").Find(&menus).Error
- return menus, err
- }
|