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.

75 lines
2.5 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gorm.io/gorm"
  7. )
  8. // @title DeleteBaseMenu
  9. // @description 删除基础路由
  10. // @auth (2020/04/05 20:22)
  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. // @title UpdateBaseMenu
  30. // @description 更新路由
  31. // @auth (2020/04/05 20:22)
  32. // @param menu model.SysBaseMenu
  33. // @return err errorgetMenu
  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. db := global.GVA_DB.Where("id = ?", menu.ID).Find(&oldMenu)
  48. if oldMenu.Name != menu.Name {
  49. notSame := errors.Is(global.GVA_DB.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound)
  50. if !notSame {
  51. global.GVA_LOG.Debug("存在相同name修改失败")
  52. return errors.New("存在相同name修改失败")
  53. }
  54. }
  55. err = global.GVA_DB.Delete(&model.SysBaseMenuParameter{}, "sys_base_menu_id = ?", menu.ID).Error
  56. err = db.Updates(upDateMap).Error
  57. return err
  58. }
  59. // @title GetBaseMenuById
  60. // @description get current menus, 返回当前选中menu
  61. // @auth (2020/04/05 20:22)
  62. // @param id float64
  63. // @return err error
  64. func GetBaseMenuById(id float64) (err error, menu model.SysBaseMenu) {
  65. err = global.GVA_DB.Preload("Parameters").Where("id = ?", id).First(&menu).Error
  66. return
  67. }