edb_source.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strings"
  8. )
  9. var (
  10. EdbSourceIdMap map[int]*EdbSource // 指标来源
  11. )
  12. // EdbSource 指标来源表
  13. type EdbSource struct {
  14. EdbSourceId int `orm:"column(edb_source_id);pk"`
  15. SourceName string `description:"指标来源名称"`
  16. SourceNameEn string `description:"指标来源名称英文"`
  17. TableName string `description:"数据表名"`
  18. EdbAddMethod string `description:"指标新增接口"`
  19. EdbRefreshMethod string `description:"指标刷新接口"`
  20. IsBase int `description:"是否为基础指标: 0-否; 1-是"`
  21. FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
  22. BridgeFlag string `description:"桥接服务对象标识"`
  23. SourceExtend string `description:"扩展字段做查询用"`
  24. EdbCodeRequired int `description:"指标编码是否必填: 0-否; 1-是"`
  25. IndexTableName string `description:"数据源指标表名"`
  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"`
  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. o := orm.NewOrmUsingDB("data")
  53. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  54. err = o.Raw(sql, pars).QueryRow(&item)
  55. return
  56. }
  57. // InitEdbSourceVar 初始化时加载指标来源对应信息, 避免循环中查库, 注意edb_source表修改table_name的话需要重启服务
  58. func InitEdbSourceVar() {
  59. EdbSourceIdMap = make(map[int]*EdbSource)
  60. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  61. if e != nil {
  62. utils.FileLog.Info("init source table err: %s", e.Error())
  63. return
  64. }
  65. for _, v := range sources {
  66. EdbSourceIdMap[v.EdbSourceId] = v
  67. }
  68. }