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.

94 lines
2.7 KiB

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