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.

62 lines
2.0 KiB

3 years ago
3 years ago
  1. package system
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. "github.com/pkg/errors"
  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. @menus.close_tab AS close_tab,
  35. @authorities_menus.sys_base_menu_id AS menu_id,
  36. @authorities_menus.sys_authority_authority_id AS authority_id
  37. from (@authorities_menus
  38. join @menus on ((@authorities_menus.sys_base_menu_id = @menus.id)));
  39. `
  40. sql = strings.ReplaceAll(sql, "@table_name", v.TableName())
  41. sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus")
  42. sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName())
  43. if err := global.GVA_DB.Exec(sql).Error; err != nil {
  44. return errors.Wrap(err, v.TableName()+"视图创建失败!")
  45. }
  46. return nil
  47. }
  48. func (v *viewAuthorityMenuMysql) CheckDataExist() bool {
  49. err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error
  50. err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Mysql.Dbname, v.TableName()))
  51. if errors.As(err1, &err2) {
  52. return false
  53. }
  54. return true
  55. }