edb_source.go 2.7 KB

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