edb_source.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package data_manage
  2. import (
  3. "eta_gn/eta_chart_lib/global"
  4. "eta_gn/eta_chart_lib/utils"
  5. "fmt"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. )
  11. // EdbSource 指标来源表
  12. //
  13. // type EdbSource struct {
  14. // EdbSourceId int `orm:"column(edb_source_id);pk"`
  15. // SourceName string `description:"指标来源名称"`
  16. // TableName string `description:"数据表名"`
  17. // EdbAddMethod string `description:"指标新增接口"`
  18. // EdbRefreshMethod string `description:"指标刷新接口"`
  19. // IsBase int `description:"是否为基础指标: 0-否; 1-是"`
  20. // FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
  21. // BridgeFlag string `description:"桥接服务对象标识"`
  22. // SourceExtend string `description:"扩展字段做查询用"`
  23. // EdbCodeRequired int `description:"指标编码是否必填: 0-否; 1-是"`
  24. // }
  25. // EdbSource 指标来源表
  26. type EdbSource struct {
  27. EdbSourceId int `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
  28. SourceName string `gorm:"column:source_name" description:"指标来源名称"`
  29. TableName string `gorm:"column:table_name" description:"数据表名"`
  30. EdbAddMethod string `gorm:"column:edb_add_method" description:"指标新增接口"`
  31. EdbRefreshMethod string `gorm:"column:edb_refresh_method" description:"指标刷新接口"`
  32. IsBase int `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
  33. FromBridge int `gorm:"column:from_bridge" description:"是否来源于桥接服务: 0-否; 1-是"`
  34. BridgeFlag string `gorm:"column:bridge_flag" description:"桥接服务对象标识"`
  35. SourceExtend string `gorm:"column:source_extend" description:"扩展字段做查询用"`
  36. EdbCodeRequired int `gorm:"column:edb_code_required" description:"指标编码是否必填: 0-否; 1-是"`
  37. }
  38. // GetEdbSourceItemsByCondition 获取指标来源列表
  39. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  40. o := global.DmSQL["data"]
  41. fields := strings.Join(fieldArr, ",")
  42. if len(fieldArr) == 0 {
  43. fields = `*`
  44. }
  45. order := `ORDER BY edb_source_id ASC`
  46. if orderRule != "" {
  47. order = ` ORDER BY ` + orderRule
  48. }
  49. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  50. err = o.Raw(sql, pars).Scan(&items).Error
  51. return
  52. }
  53. // GetEdbSourceItemsByCondition 获取指标来源列表
  54. // func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  55. // o := orm.NewOrmUsingDB("data")
  56. // fields := strings.Join(fieldArr, ",")
  57. // if len(fieldArr) == 0 {
  58. // fields = `*`
  59. // }
  60. // order := `ORDER BY edb_source_id ASC`
  61. // if orderRule != "" {
  62. // order = ` ORDER BY ` + orderRule
  63. // }
  64. // sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  65. // _, err = o.Raw(sql, pars).QueryRows(&items)
  66. // return
  67. // }
  68. // EdbSourceChild 指标来源表
  69. //
  70. // type EdbSourceChild struct {
  71. // EdbSourceId int `orm:"column(edb_source_id);pk"`
  72. // SourceName string `description:"指标来源名称"`
  73. // TableName string `description:"数据表名"`
  74. // IsBase int `description:"是否为基础指标: 2-否; 1-是"`
  75. // Child []EdbSourceChild
  76. // }
  77. type EdbSourceChild struct {
  78. EdbSourceId int `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
  79. SourceName string `gorm:"column:source_name" description:"指标来源名称"`
  80. TableName string `gorm:"column:table_name" description:"数据表名"`
  81. IsBase int `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
  82. Child []EdbSourceChild `description:"子源"`
  83. }
  84. // GetEdbSourceItemByCondition 获取指标来源
  85. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  86. o := global.DmSQL["data"]
  87. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  88. err = o.Raw(sql, pars).First(&item).Error
  89. return
  90. }
  91. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  92. func InitEdbSourceVar() {
  93. EdbSourceIdMap = make(map[int]*EdbSource)
  94. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  95. if e != nil {
  96. utils.FileLog.Info("init source table err: %s", e.Error())
  97. return
  98. }
  99. for _, v := range sources {
  100. EdbSourceIdMap[v.EdbSourceId] = v
  101. }
  102. }
  103. // GetEdbSourceItemByCondition 获取指标来源
  104. // func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  105. // o := orm.NewOrmUsingDB("data")
  106. // sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  107. // err = o.Raw(sql, pars).QueryRow(&item)
  108. // return
  109. // }
  110. // // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  111. // func InitEdbSourceVar() {
  112. // EdbSourceIdMap = make(map[int]*EdbSource)
  113. // sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  114. // if e != nil {
  115. // utils.FileLog.Info("init source table err: %s", e.Error())
  116. // return
  117. // }
  118. // for _, v := range sources {
  119. // EdbSourceIdMap[v.EdbSourceId] = v
  120. // }
  121. // }