edb_source.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  26. // GetEdbSourceItemsByCondition 获取指标来源列表
  27. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  28. o := global.DmSQL["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...).Find(&items).Error
  39. return
  40. }
  41. // EdbSourceChild 指标来源表
  42. type EdbSourceChild struct {
  43. EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
  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. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  52. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  53. return
  54. }
  55. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  56. func InitEdbSourceVar() {
  57. EdbSourceIdMap = make(map[int]*EdbSource)
  58. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  59. if e != nil {
  60. utils.FileLog.Info("init source table err: %s", e.Error())
  61. return
  62. }
  63. for _, v := range sources {
  64. EdbSourceIdMap[v.EdbSourceId] = v
  65. }
  66. }
  67. // GetEdbSourceItemsSourceId
  68. // @Description: 根据来源id获取指标来源
  69. // @param sourceId
  70. // @return item
  71. // @return err
  72. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  73. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  74. err = global.DmSQL["data"].Raw(sql, sourceId).First(&item).Error
  75. return
  76. }
  77. // GetEdbSourceBySourceId
  78. // @Description: 根据来源id获取指标来源
  79. // @param sourceId
  80. // @return sourceItem
  81. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  82. sourceItem, ok := EdbSourceIdMap[sourceId]
  83. if !ok {
  84. item, err := GetEdbSourceItemsSourceId(sourceId)
  85. if err != nil {
  86. return
  87. }
  88. if item.EdbSourceId > 0 {
  89. sourceItem = item
  90. // 写入到内存中
  91. EdbSourceIdMap[sourceId] = sourceItem
  92. }
  93. }
  94. return
  95. }