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.

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