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.

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