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.

39 lines
1.8 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 := "CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `@table_name` AS select `@menus`.id AS id, `@menus`.path AS path, `@menus`.icon AS icon, `@menus`.name AS name, `@menus`.sort AS sort, `@menus`.title AS title, `@menus`.hidden AS hidden, `@menus`.component AS component, `@menus`.parent_id AS parent_id, `@menus`.created_at AS created_at, `@menus`.updated_at AS updated_at, `@menus`.deleted_at AS deleted_at, `@menus`.keep_alive AS keep_alive, `@menus`.menu_level AS menu_level, `@menus`.default_menu AS default_menu, `@authorities_menus`.menu_id AS menu_id, `@authorities_menus`.authority_id AS authority_id from (`@authorities_menus` join `@menus` on ((`@authorities_menus`.menu_id = `@menus`.id)));"
  18. sql = strings.ReplaceAll(sql, "@table_name", v.TableName())
  19. sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus")
  20. sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName())
  21. if err := global.GVA_DB.Exec(sql).Error; err != nil {
  22. return errors.Wrap(err, v.TableName()+"视图创建失败!")
  23. }
  24. return nil
  25. }
  26. func (v *viewAuthorityMenuMysql) CheckDataExist() bool {
  27. err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error
  28. err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Mysql.Dbname, v.TableName()))
  29. if errors.As(err1, &err2) {
  30. return false
  31. }
  32. return true
  33. }