edb_source.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID
  11. )
  12. type EdbSource struct {
  13. EdbSourceId int `gorm:"primaryKey;autoIncrement;column:edb_source_id" description:"指标来源ID"`
  14. SourceName string `gorm:"column:source_name;not null;default:'';type:varchar(128)" description:"指标来源名称"`
  15. TableName1 string `gorm:"column:table_name;not null;default:'';type:varchar(128)" description:"数据表名"`
  16. EdbAddMethod string `gorm:"column:edb_add_method;not null;default:'';type:varchar(255)" description:"指标新增接口"`
  17. EdbRefreshMethod string `gorm:"column:edb_refresh_method;not null;default:'';type:varchar(255)" description:"指标刷新接口"`
  18. IsBase int `gorm:"column:is_base;not null;default:0" description:"是否为基础指标: 0-否; 1-是"`
  19. FromBridge int `gorm:"column:from_bridge;not null;default:0" description:"是否来源于桥接服务: 0-否; 1-是"`
  20. BridgeFlag string `gorm:"column:bridge_flag;not null;default:'';type:varchar(128)" description:"桥接服务对象标识"`
  21. SourceExtend string `gorm:"column:source_extend;not null;default:'';type:varchar(255)" description:"扩展字段做查询用"`
  22. EdbCodeRequired int `gorm:"column:edb_code_required;not null;default:0" description:"指标编码是否必填: 0-否; 1-是"`
  23. }
  24. // TableName 获取表名
  25. func (e *EdbSource) TableName() string {
  26. return "edb_source"
  27. }
  28. // GetEdbSourceItemsByCondition 获取指标来源列表
  29. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  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 = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&items).Error
  40. return
  41. }
  42. // InitEdbSource 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  43. func InitEdbSource() {
  44. EdbSourceIdMap = make(map[int]*EdbSource)
  45. EdbSourceExtendIdMap = make(map[string]int)
  46. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  47. if e != nil {
  48. utils.FileLog.Info("init source table err: %s", e.Error())
  49. return
  50. }
  51. for _, v := range sources {
  52. EdbSourceIdMap[v.EdbSourceId] = v
  53. if v.SourceExtend != "" {
  54. arr := strings.Split(v.SourceExtend, ",")
  55. if len(arr) == 0 {
  56. continue
  57. }
  58. for _, s := range arr {
  59. EdbSourceExtendIdMap[s] = v.EdbSourceId
  60. }
  61. }
  62. }
  63. }
  64. // GetEdbSourceItemsSourceId
  65. // @Description: 根据来源id获取指标来源
  66. // @param sourceId
  67. // @return item
  68. // @return err
  69. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  70. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  71. err = global.DEFAULT_DmSQL.Raw(sql, sourceId).First(&item).Error
  72. return
  73. }
  74. // GetEdbSourceBySourceId
  75. // @Description: 根据来源id获取指标来源
  76. // @param sourceId
  77. // @return sourceItem
  78. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  79. sourceItem, ok := EdbSourceIdMap[sourceId]
  80. if !ok {
  81. item, err := GetEdbSourceItemsSourceId(sourceId)
  82. if err != nil {
  83. return
  84. }
  85. if item.EdbSourceId > 0 {
  86. sourceItem = item
  87. // 写入到内存中
  88. EdbSourceIdMap[sourceId] = sourceItem
  89. }
  90. }
  91. return
  92. }