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.

107 lines
3.1 KiB

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