edb_source.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }
  25. // GetEdbSourceItemsByCondition 获取指标来源
  26. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. fields := strings.Join(fieldArr, ",")
  29. if len(fieldArr) == 0 {
  30. fields = `*`
  31. }
  32. order := `ORDER BY edb_source_id ASC`
  33. if orderRule != "" {
  34. order = ` ORDER BY ` + orderRule
  35. }
  36. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  37. _, err = o.Raw(sql, pars).QueryRows(&items)
  38. return
  39. }
  40. // InitEdbSource 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  41. func InitEdbSource() {
  42. EdbSourceIdMap = make(map[int]*EdbSource)
  43. EdbSourceExtendIdMap = make(map[string]int)
  44. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  45. if e != nil {
  46. utils.FileLog.Info("init source table err: %s", e.Error())
  47. return
  48. }
  49. for _, v := range sources {
  50. EdbSourceIdMap[v.EdbSourceId] = v
  51. if v.SourceExtend != "" {
  52. arr := strings.Split(v.SourceExtend, ",")
  53. if len(arr) == 0 {
  54. continue
  55. }
  56. for _, s := range arr {
  57. EdbSourceExtendIdMap[s] = v.EdbSourceId
  58. }
  59. }
  60. }
  61. }