edb_source.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package data_manage
  2. import (
  3. "eta/eta_task/global"
  4. "eta/eta_task/utils"
  5. "fmt"
  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 `gorm:"primaryKey"`
  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. fields := strings.Join(fieldArr, ",")
  30. if len(fieldArr) == 0 {
  31. fields = `*`
  32. }
  33. order := `ORDER BY edb_source_id ASC`
  34. if orderRule != "" {
  35. order = ` ORDER BY ` + orderRule
  36. }
  37. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  38. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  39. return
  40. }
  41. // GetEdbSourceItemByCondition 获取指标来源
  42. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  43. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  44. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).First(&item).Error
  45. return
  46. }
  47. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  48. func InitEdbSourceVar() {
  49. EdbDataTableNameMap = make(map[int]string)
  50. EdbDataRefreshMethodMap = make(map[int]string)
  51. EdbTableNameSourceMap = make(map[string]*EdbSource)
  52. EdbSourceIdMap = make(map[int]*EdbSource)
  53. EdbSourceExtendIdMap = make(map[string]int)
  54. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  55. if e != nil {
  56. utils.FileLog.Info("init source table err: %s", e.Error())
  57. return
  58. }
  59. for _, v := range sources {
  60. EdbDataTableNameMap[v.EdbSourceId] = v.TableName
  61. EdbDataRefreshMethodMap[v.EdbSourceId] = v.EdbRefreshMethod
  62. EdbTableNameSourceMap[v.TableName] = v
  63. EdbSourceIdMap[v.EdbSourceId] = v
  64. if v.SourceExtend != "" {
  65. arr := strings.Split(v.SourceExtend, ",")
  66. if len(arr) == 0 {
  67. continue
  68. }
  69. for _, s := range arr {
  70. EdbSourceExtendIdMap[s] = v.EdbSourceId
  71. }
  72. }
  73. }
  74. }