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.

60 lines
2.0 KiB

  1. package system
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  6. "github.com/pkg/errors"
  7. "strings"
  8. )
  9. var ViewAuthorityMenuMysql = new(viewAuthorityMenuMysql)
  10. type viewAuthorityMenuMysql struct{}
  11. func (v *viewAuthorityMenuMysql) TableName() string {
  12. var entity system.SysMenu
  13. return entity.TableName()
  14. }
  15. func (v *viewAuthorityMenuMysql) Initialize() error {
  16. var entity AuthorityMenus
  17. sql := `
  18. CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW @table_name AS
  19. select @menus.id AS id,
  20. @menus.path AS path,
  21. @menus.icon AS icon,
  22. @menus.name AS name,
  23. @menus.sort AS sort,
  24. @menus.title AS title,
  25. @menus.hidden AS hidden,
  26. @menus.component AS component,
  27. @menus.parent_id AS parent_id,
  28. @menus.created_at AS created_at,
  29. @menus.updated_at AS updated_at,
  30. @menus.deleted_at AS deleted_at,
  31. @menus.keep_alive AS keep_alive,
  32. @menus.menu_level AS menu_level,
  33. @menus.default_menu AS default_menu,
  34. @authorities_menus.sys_base_menu_id AS menu_id,
  35. @authorities_menus.sys_authority_authority_id AS authority_id
  36. from (@authorities_menus
  37. join @menus on ((@authorities_menus.sys_base_menu_id = @menus.id)));
  38. `
  39. sql = strings.ReplaceAll(sql, "@table_name", v.TableName())
  40. sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus")
  41. sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName())
  42. if err := global.GVA_DB.Exec(sql).Error; err != nil {
  43. return errors.Wrap(err, v.TableName()+"视图创建失败!")
  44. }
  45. return nil
  46. }
  47. func (v *viewAuthorityMenuMysql) CheckDataExist() bool {
  48. err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error
  49. err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Mysql.Dbname, v.TableName()))
  50. if errors.As(err1, &err2) {
  51. return false
  52. }
  53. return true
  54. }