edb_source.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // 获取表名
  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.Model(&EdbSource{}).Raw(sql, pars).Scan(&items).Error
  40. return
  41. }
  42. // GetEdbSourceItemByCondition 获取指标来源
  43. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  44. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  45. err = global.DEFAULT_DmSQL.Model(&EdbSource{}).Raw(sql, pars).First(&item).Error
  46. return
  47. }
  48. // InitEdbSource 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  49. func InitEdbSource() {
  50. EdbSourceIdMap = make(map[int]*EdbSource)
  51. EdbSourceExtendIdMap = make(map[string]int)
  52. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  53. if e != nil {
  54. utils.FileLog.Info("init source table err: %s", e.Error())
  55. return
  56. }
  57. for _, v := range sources {
  58. EdbSourceIdMap[v.EdbSourceId] = v
  59. if v.SourceExtend != "" {
  60. arr := strings.Split(v.SourceExtend, ",")
  61. if len(arr) == 0 {
  62. continue
  63. }
  64. for _, s := range arr {
  65. EdbSourceExtendIdMap[s] = v.EdbSourceId
  66. }
  67. }
  68. }
  69. }