edb_source.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID
  11. )
  12. // EdbSource 指标来源表
  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. IndexTableName string `description:"数据源指标表名"`
  25. }
  26. // GetEdbSourceItemsByCondition 获取指标来源
  27. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  28. o := orm.NewOrmUsingDB("data")
  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 = o.Raw(sql, pars).QueryRows(&items)
  39. return
  40. }
  41. // InitEdbSource 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  42. func InitEdbSource() {
  43. EdbSourceIdMap = make(map[int]*EdbSource)
  44. EdbSourceExtendIdMap = make(map[string]int)
  45. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  46. if e != nil {
  47. utils.FileLog.Info("init source table err: %s", e.Error())
  48. return
  49. }
  50. for _, v := range sources {
  51. EdbSourceIdMap[v.EdbSourceId] = v
  52. if v.SourceExtend != "" {
  53. arr := strings.Split(v.SourceExtend, ",")
  54. if len(arr) == 0 {
  55. continue
  56. }
  57. for _, s := range arr {
  58. EdbSourceExtendIdMap[s] = v.EdbSourceId
  59. }
  60. }
  61. }
  62. }