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.

99 lines
3.0 KiB

  1. package sysModel
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. "github.com/pkg/errors"
  6. "main/controller/servers"
  7. "main/init/qmsql"
  8. "main/model/modelInterface"
  9. )
  10. type SysBaseMenu struct {
  11. gorm.Model
  12. MenuLevel uint `json:"-"`
  13. ParentId string `json:"parentId"`
  14. Path string `json:"path"`
  15. Name string `json:"name"`
  16. Hidden bool `json:"hidden"`
  17. Component string `json:"component"`
  18. Sort string `json:"sort"`
  19. Meta `json:"meta"`
  20. NickName string `json:"nickName"`
  21. Children []SysBaseMenu `json:"children"`
  22. }
  23. func (b *SysBaseMenu) AddBaseMenu() (err error) {
  24. findOne := qmsql.DEFAULTDB.Where("name = ?", b.Name).Find(&SysBaseMenu{}).Error
  25. if findOne != nil {
  26. b.NickName = b.Title
  27. err = qmsql.DEFAULTDB.Create(b).Error
  28. } else {
  29. err = errors.New("存在重复name,请修改name")
  30. }
  31. return err
  32. }
  33. func (b *SysBaseMenu) DeleteBaseMenu(id float64) (err error) {
  34. err = qmsql.DEFAULTDB.Where("parent_id = ?", id).First(&SysBaseMenu{}).Error
  35. if err != nil {
  36. err = qmsql.DEFAULTDB.Where("id = ?", id).Delete(&b).Error
  37. err = qmsql.DEFAULTDB.Where("menu_id = ?", id).Unscoped().Delete(&SysMenu{}).Error
  38. } else {
  39. return errors.New("此菜单存在子菜单不可删除")
  40. }
  41. return err
  42. }
  43. func (b *SysBaseMenu) UpdataBaseMenu() (err error) {
  44. upDataMap := make(map[string]interface{})
  45. upDataMap["parent_id"] = b.ParentId
  46. upDataMap["path"] = b.Path
  47. upDataMap["name"] = b.Name
  48. upDataMap["hidden"] = b.Hidden
  49. upDataMap["component"] = b.Component
  50. upDataMap["title"] = b.Title
  51. upDataMap["icon"] = b.Icon
  52. upDataMap["sort"] = b.Sort
  53. err = qmsql.DEFAULTDB.Where("id = ?", b.ID).Find(&SysBaseMenu{}).Updates(upDataMap).Error
  54. err1 := qmsql.DEFAULTDB.Where("menu_id = ?", b.ID).Find(&[]SysMenu{}).Updates(upDataMap).Error
  55. fmt.Printf("菜单修改时候,关联菜单err1:%v,err:%v", err1, err)
  56. return err
  57. }
  58. func (b *SysBaseMenu) GetBaseMenuById(id float64) (err error, menu SysBaseMenu) {
  59. err = qmsql.DEFAULTDB.Where("id = ?", id).First(&menu).Error
  60. return
  61. }
  62. func (b *SysBaseMenu) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  63. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  64. err, db, total := servers.PagingServer(b, info)
  65. if err != nil {
  66. return
  67. } else {
  68. var menuList []SysBaseMenu
  69. err = db.Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  70. for i := 0; i < len(menuList); i++ {
  71. err = getBaseChildrenList(&menuList[i])
  72. }
  73. return err, menuList, total
  74. }
  75. }
  76. //获取基础路由树
  77. func (m *SysBaseMenu) GetBaseMenuTree() (err error, menus []SysBaseMenu) {
  78. err = qmsql.DEFAULTDB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  79. for i := 0; i < len(menus); i++ {
  80. err = getBaseChildrenList(&menus[i])
  81. }
  82. return err, menus
  83. }
  84. func getBaseChildrenList(menu *SysBaseMenu) (err error) {
  85. err = qmsql.DEFAULTDB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  86. for i := 0; i < len(menu.Children); i++ {
  87. err = getBaseChildrenList(&menu.Children[i])
  88. }
  89. return err
  90. }