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.

96 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["default_menu"] = menu.DefaultMenu
  39. upDateMap["parent_id"] = menu.ParentId
  40. upDateMap["path"] = menu.Path
  41. upDateMap["name"] = menu.Name
  42. upDateMap["hidden"] = menu.Hidden
  43. upDateMap["component"] = menu.Component
  44. upDateMap["title"] = menu.Title
  45. upDateMap["icon"] = menu.Icon
  46. upDateMap["sort"] = menu.Sort
  47. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  48. db := tx.Where("id = ?", menu.ID).Find(&oldMenu)
  49. if oldMenu.Name != menu.Name {
  50. if !errors.Is(tx.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
  51. global.GVA_LOG.Debug("存在相同name修改失败")
  52. return errors.New("存在相同name修改失败")
  53. }
  54. }
  55. txErr := tx.Unscoped().Delete(&model.SysBaseMenuParameter{}, "sys_base_menu_id = ?", menu.ID).Error
  56. if txErr != nil {
  57. global.GVA_LOG.Debug(txErr.Error())
  58. return txErr
  59. }
  60. if len(menu.Parameters) > 0 {
  61. for k, _ := range menu.Parameters {
  62. menu.Parameters[k].SysBaseMenuID = menu.ID
  63. }
  64. txErr = tx.Create(&menu.Parameters).Error
  65. if txErr != nil {
  66. global.GVA_LOG.Debug(txErr.Error())
  67. return txErr
  68. }
  69. }
  70. txErr = db.Updates(upDateMap).Error
  71. if txErr != nil {
  72. global.GVA_LOG.Debug(txErr.Error())
  73. return txErr
  74. }
  75. return nil
  76. })
  77. return err
  78. }
  79. //@author: [piexlmax](https://github.com/piexlmax)
  80. //@function: GetBaseMenuById
  81. //@description: 返回当前选中menu
  82. //@param: id float64
  83. //@return: err error, menu model.SysBaseMenu
  84. func GetBaseMenuById(id float64) (err error, menu model.SysBaseMenu) {
  85. err = global.GVA_DB.Preload("Parameters").Where("id = ?", id).First(&menu).Error
  86. return
  87. }