edb_source.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  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" gorm:"primaryKey" `
  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. EdbInfoTType int `description:"指标类型,0:指标库;1:预测指标"`
  26. }
  27. // GetEdbSourceItemsByCondition 获取指标来源列表
  28. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  29. o := global.DmSQL["data"]
  30. fields := strings.Join(fieldArr, ",")
  31. if len(fieldArr) == 0 {
  32. fields = `*`
  33. }
  34. order := `ORDER BY edb_source_id ASC`
  35. if orderRule != "" {
  36. order = ` ORDER BY ` + orderRule
  37. }
  38. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  39. err = o.Raw(sql, pars...).Find(&items).Error
  40. return
  41. }
  42. // EdbSourceChild 指标来源表
  43. type EdbSourceChild struct {
  44. EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
  45. SourceName string `description:"指标来源名称"`
  46. TableName string `description:"数据表名"`
  47. IsBase int `description:"是否为基础指标: 2-否; 1-是"`
  48. Child []EdbSourceChild
  49. }
  50. // GetEdbSourceItemByCondition 获取指标来源
  51. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  52. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  53. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  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. }
  68. // GetEdbSourceItemsSourceId
  69. // @Description: 根据来源id获取指标来源
  70. // @param sourceId
  71. // @return item
  72. // @return err
  73. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  74. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  75. err = global.DmSQL["data"].Raw(sql, sourceId).First(&item).Error
  76. return
  77. }
  78. // GetEdbSourceBySourceId
  79. // @Description: 根据来源id获取指标来源
  80. // @param sourceId
  81. // @return sourceItem
  82. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  83. sourceItem, ok := EdbSourceIdMap[sourceId]
  84. if !ok {
  85. item, err := GetEdbSourceItemsSourceId(sourceId)
  86. if err != nil {
  87. return
  88. }
  89. if item.EdbSourceId > 0 {
  90. sourceItem = item
  91. // 写入到内存中
  92. EdbSourceIdMap[sourceId] = sourceItem
  93. }
  94. }
  95. return
  96. }