edb_source.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  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"`
  14. SourceName string `description:"指标来源名称"`
  15. TableName string `description:"数据表名"`
  16. EdbAddMethod string `description:"指标新增接口"`
  17. EdbRefreshMethod string `description:"指标刷新接口"`
  18. IsBase int `description:"是否为基础指标: 0-否; 1-是"`
  19. FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
  20. BridgeFlag string `description:"桥接服务对象标识"`
  21. SourceExtend string `description:"扩展字段做查询用"`
  22. EdbCodeRequired int `description:"指标编码是否必填: 0-否; 1-是"`
  23. IndexTableName string `description:"数据源指标表名"`
  24. }
  25. // GetEdbSourceItemsByCondition 获取指标来源列表
  26. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. fields := strings.Join(fieldArr, ",")
  29. if len(fieldArr) == 0 {
  30. fields = `*`
  31. }
  32. order := `ORDER BY edb_source_id ASC`
  33. if orderRule != "" {
  34. order = ` ORDER BY ` + orderRule
  35. }
  36. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  37. _, err = o.Raw(sql, pars).QueryRows(&items)
  38. return
  39. }
  40. // EdbSourceChild 指标来源表
  41. type EdbSourceChild struct {
  42. EdbSourceId int `orm:"column(edb_source_id);pk"`
  43. SourceName string `description:"指标来源名称"`
  44. TableName string `description:"数据表名"`
  45. IsBase int `description:"是否为基础指标: 2-否; 1-是"`
  46. Child []EdbSourceChild
  47. }
  48. // GetEdbSourceItemByCondition 获取指标来源
  49. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  52. err = o.Raw(sql, pars).QueryRow(&item)
  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. }