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.

97 lines
2.8 KiB

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