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.1 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 ViewAuthorityMenuPostgres = new(viewAuthorityMenuPostgres)
  10. type viewAuthorityMenuPostgres struct{}
  11. func (a *viewAuthorityMenuPostgres) TableName() string {
  12. var entity system.SysMenu
  13. return entity.TableName()
  14. }
  15. func (a *viewAuthorityMenuPostgres) Initialize() error {
  16. var entity AuthorityMenus
  17. sql := `
  18. CREATE VIEW @table_name as
  19. select @menus.id as id,
  20. @menus.path as path,
  21. @menus.name as name,
  22. @menus.icon as icon,
  23. @menus.sort as sort,
  24. @menus.title as title,
  25. @menus.hidden as hidden,
  26. @menus.parent_id as parent_id,
  27. @menus.component as component,
  28. @menus.keep_alive as keep_alive,
  29. @menus.created_at as created_at,
  30. @menus.updated_at as updated_at,
  31. @menus.deleted_at as deleted_at,
  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 join @menus on ((@authorities_menus.sys_base_menu_id = @menus.id)));`
  38. sql = strings.ReplaceAll(sql, "@table_name", a.TableName())
  39. sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus")
  40. sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName())
  41. if err := global.GVA_DB.Exec(sql).Error; err != nil {
  42. return errors.Wrap(err, a.TableName()+"视图创建失败!")
  43. }
  44. return nil
  45. }
  46. func (a *viewAuthorityMenuPostgres) CheckDataExist() bool {
  47. err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error
  48. err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Pgsql.Dbname, a.TableName()))
  49. if errors.As(err1, &err2) {
  50. return false
  51. }
  52. return true
  53. }