edb_source.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. )
  11. // EdbSource 指标来源表
  12. type EdbSource struct {
  13. EdbSourceId int `orm:"column(edb_source_id);pk"`
  14. SourceName string `description:"指标来源名称"`
  15. SourceNameEn 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. // EdbSourceChild 指标来源表
  42. type EdbSourceChild struct {
  43. EdbSourceId int `orm:"column(edb_source_id);pk"`
  44. SourceName string `description:"指标来源名称"`
  45. TableName string `description:"数据表名"`
  46. IsBase int `description:"是否为基础指标: 2-否; 1-是"`
  47. Child []EdbSourceChild
  48. }
  49. // GetEdbSourceItemByCondition 获取指标来源
  50. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  51. o := orm.NewOrmUsingDB("data")
  52. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  53. err = o.Raw(sql, pars).QueryRow(&item)
  54. return
  55. }
  56. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  57. func InitEdbSourceVar() {
  58. EdbSourceIdMap = make(map[int]*EdbSource)
  59. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  60. if e != nil {
  61. utils.FileLog.Info("init source table err: %s", e.Error())
  62. return
  63. }
  64. for _, v := range sources {
  65. EdbSourceIdMap[v.EdbSourceId] = v
  66. }
  67. }