sys_base_menu.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package system
  2. import (
  3. "errors"
  4. "github.com/sirupsen/logrus"
  5. "gorm.io/gorm"
  6. "lc-base-frame/global"
  7. "lc-base-frame/model/system"
  8. )
  9. type BaseMenuService struct{}
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: DeleteBaseMenu
  12. //@description: 删除基础路由
  13. //@param: id float64
  14. //@return: err error
  15. func (baseMenuService *BaseMenuService) DeleteBaseMenu(id int) (err error) {
  16. err = global.Db.Preload("MenuBtn").Preload("Parameters").Where("parent_id = ?", id).First(&system.SysBaseMenu{}).Error
  17. if err != nil {
  18. var menu system.SysBaseMenu
  19. db := global.Db.Preload("SysAuthoritys").Where("id = ?", id).First(&menu).Delete(&menu)
  20. err = global.Db.Delete(&system.SysBaseMenuParameter{}, "sys_base_menu_id = ?", id).Error
  21. err = global.Db.Delete(&system.SysBaseMenuBtn{}, "sys_base_menu_id = ?", id).Error
  22. err = global.Db.Delete(&system.SysAuthorityBtn{}, "sys_menu_id = ?", id).Error
  23. if err != nil {
  24. return err
  25. }
  26. if len(menu.SysAuthoritys) > 0 {
  27. err = global.Db.Model(&menu).Association("SysAuthoritys").Delete(&menu.SysAuthoritys)
  28. } else {
  29. err = db.Error
  30. if err != nil {
  31. return
  32. }
  33. }
  34. } else {
  35. return errors.New("此菜单存在子菜单不可删除")
  36. }
  37. return err
  38. }
  39. //@author: [piexlmax](https://github.com/piexlmax)
  40. //@function: UpdateBaseMenu
  41. //@description: 更新路由
  42. //@param: menu model.SysBaseMenu
  43. //@return: err error
  44. func (baseMenuService *BaseMenuService) UpdateBaseMenu(menu system.SysBaseMenu) (err error) {
  45. var oldMenu system.SysBaseMenu
  46. upDateMap := make(map[string]interface{})
  47. upDateMap["keep_alive"] = menu.KeepAlive
  48. upDateMap["close_tab"] = menu.CloseTab
  49. upDateMap["default_menu"] = menu.DefaultMenu
  50. upDateMap["parent_id"] = menu.ParentId
  51. upDateMap["path"] = menu.Path
  52. upDateMap["name"] = menu.Name
  53. upDateMap["hidden"] = menu.Hidden
  54. upDateMap["component"] = menu.Component
  55. upDateMap["title"] = menu.Title
  56. upDateMap["active_name"] = menu.ActiveName
  57. upDateMap["icon"] = menu.Icon
  58. upDateMap["sort"] = menu.Sort
  59. err = global.Db.Transaction(func(tx *gorm.DB) error {
  60. db := tx.Where("id = ?", menu.ID).Find(&oldMenu)
  61. if oldMenu.Name != menu.Name {
  62. if !errors.Is(tx.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&system.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
  63. logrus.Debug("存在相同name修改失败")
  64. return errors.New("存在相同name修改失败")
  65. }
  66. }
  67. txErr := tx.Unscoped().Delete(&system.SysBaseMenuParameter{}, "sys_base_menu_id = ?", menu.ID).Error
  68. if txErr != nil {
  69. logrus.Debug(txErr.Error())
  70. return txErr
  71. }
  72. txErr = tx.Unscoped().Delete(&system.SysBaseMenuBtn{}, "sys_base_menu_id = ?", menu.ID).Error
  73. if txErr != nil {
  74. logrus.Debug(txErr.Error())
  75. return txErr
  76. }
  77. if len(menu.Parameters) > 0 {
  78. for k := range menu.Parameters {
  79. menu.Parameters[k].SysBaseMenuID = menu.ID
  80. }
  81. txErr = tx.Create(&menu.Parameters).Error
  82. if txErr != nil {
  83. logrus.Debug(txErr.Error())
  84. return txErr
  85. }
  86. }
  87. if len(menu.MenuBtn) > 0 {
  88. for k := range menu.MenuBtn {
  89. menu.MenuBtn[k].SysBaseMenuID = menu.ID
  90. }
  91. txErr = tx.Create(&menu.MenuBtn).Error
  92. if txErr != nil {
  93. logrus.Debug(txErr.Error())
  94. return txErr
  95. }
  96. }
  97. txErr = db.Updates(upDateMap).Error
  98. if txErr != nil {
  99. logrus.Debug(txErr.Error())
  100. return txErr
  101. }
  102. return nil
  103. })
  104. return err
  105. }
  106. //@author: [piexlmax](https://github.com/piexlmax)
  107. //@function: GetBaseMenuById
  108. //@description: 返回当前选中menu
  109. //@param: id float64
  110. //@return: menu system.SysBaseMenu, err error
  111. func (baseMenuService *BaseMenuService) GetBaseMenuById(id int) (menu system.SysBaseMenu, err error) {
  112. err = global.Db.Preload("MenuBtn").Preload("Parameters").Where("id = ?", id).First(&menu).Error
  113. return
  114. }