edb_source.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package data_manage
  2. import (
  3. "eta/eta_task/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. )
  8. var (
  9. EdbDataTableNameMap map[int]string // 指标来源对应数据表名
  10. EdbDataRefreshMethodMap map[int]string // 指标来源对应的刷新指标方法
  11. EdbTableNameSourceMap map[string]*EdbSource // 数据表名对应的指标来源
  12. EdbSourceIdMap map[int]*EdbSource // 指标来源
  13. EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID
  14. )
  15. // EdbSource 指标来源表
  16. type EdbSource struct {
  17. EdbSourceId int `orm:"column(edb_source_id);pk"`
  18. SourceName string `description:"指标来源名称"`
  19. TableName string `description:"数据表名"`
  20. EdbAddMethod string `description:"指标新增接口"`
  21. EdbRefreshMethod string `description:"指标刷新接口"`
  22. IsBase int `description:"是否为基础指标: 0-否; 1-是"`
  23. FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
  24. BridgeFlag string `description:"桥接服务对象标识"`
  25. SourceExtend string `description:"扩展字段做查询用"`
  26. }
  27. // GetEdbSourceItemsByCondition 获取指标来源列表
  28. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  29. o := orm.NewOrmUsingDB("data")
  30. fields := strings.Join(fieldArr, ",")
  31. if len(fieldArr) == 0 {
  32. fields = `*`
  33. }
  34. order := `ORDER BY edb_source_id ASC`
  35. if orderRule != "" {
  36. order = ` ORDER BY ` + orderRule
  37. }
  38. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  39. _, err = o.Raw(sql, pars).QueryRows(&items)
  40. return
  41. }
  42. // GetEdbSourceItemByCondition 获取指标来源
  43. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  44. o := orm.NewOrmUsingDB("data")
  45. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  46. err = o.Raw(sql, pars).QueryRow(&item)
  47. return
  48. }
  49. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  50. func InitEdbSourceVar() {
  51. EdbDataTableNameMap = make(map[int]string)
  52. EdbDataRefreshMethodMap = make(map[int]string)
  53. EdbTableNameSourceMap = make(map[string]*EdbSource)
  54. EdbSourceIdMap = make(map[int]*EdbSource)
  55. EdbSourceExtendIdMap = make(map[string]int)
  56. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  57. if e != nil {
  58. utils.FileLog.Info("init source table err: %s", e.Error())
  59. return
  60. }
  61. for _, v := range sources {
  62. EdbDataTableNameMap[v.EdbSourceId] = v.TableName
  63. EdbDataRefreshMethodMap[v.EdbSourceId] = v.EdbRefreshMethod
  64. EdbTableNameSourceMap[v.TableName] = v
  65. EdbSourceIdMap[v.EdbSourceId] = v
  66. if v.SourceExtend != "" {
  67. arr := strings.Split(v.SourceExtend, ",")
  68. if len(arr) == 0 {
  69. continue
  70. }
  71. for _, s := range arr {
  72. EdbSourceExtendIdMap[s] = v.EdbSourceId
  73. }
  74. }
  75. }
  76. }