edb_source.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package data_manage
  2. import (
  3. "eta_gn/eta_chart_lib/global"
  4. "eta_gn/eta_chart_lib/utils"
  5. "fmt"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. )
  11. type EdbSource struct {
  12. EdbSourceId int `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
  13. SourceName string `gorm:"column:source_name" description:"指标来源名称"`
  14. TableName string `gorm:"column:table_name" description:"数据表名"`
  15. EdbAddMethod string `gorm:"column:edb_add_method" description:"指标新增接口"`
  16. EdbRefreshMethod string `gorm:"column:edb_refresh_method" description:"指标刷新接口"`
  17. IsBase int `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
  18. FromBridge int `gorm:"column:from_bridge" description:"是否来源于桥接服务: 0-否; 1-是"`
  19. BridgeFlag string `gorm:"column:bridge_flag" description:"桥接服务对象标识"`
  20. SourceExtend string `gorm:"column:source_extend" description:"扩展字段做查询用"`
  21. EdbCodeRequired int `gorm:"column:edb_code_required" description:"指标编码是否必填: 0-否; 1-是"`
  22. }
  23. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  24. o := global.DmSQL["data"]
  25. fields := strings.Join(fieldArr, ",")
  26. if len(fieldArr) == 0 {
  27. fields = `*`
  28. }
  29. order := `ORDER BY edb_source_id ASC`
  30. if orderRule != "" {
  31. order = ` ORDER BY ` + orderRule
  32. }
  33. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  34. err = o.Raw(sql, pars...).Scan(&items).Error
  35. return
  36. }
  37. type EdbSourceChild struct {
  38. EdbSourceId int `gorm:"column:edb_source_id;primaryKey" description:"指标来源ID" orm:"column(edb_source_id);pk"`
  39. SourceName string `gorm:"column:source_name" description:"指标来源名称"`
  40. TableName string `gorm:"column:table_name" description:"数据表名"`
  41. IsBase int `gorm:"column:is_base" description:"是否为基础指标: 0-否; 1-是"`
  42. Child []EdbSourceChild `description:"子源"`
  43. }
  44. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  45. o := global.DmSQL["data"]
  46. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  47. err = o.Raw(sql, pars...).First(&item).Error
  48. return
  49. }
  50. func InitEdbSourceVar() {
  51. EdbSourceIdMap = make(map[int]*EdbSource)
  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. }
  60. }
  61. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  62. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  63. err = global.DmSQL["data"].Raw(sql, sourceId).First(&item).Error
  64. return
  65. }
  66. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  67. sourceItem, ok := EdbSourceIdMap[sourceId]
  68. if !ok {
  69. item, err := GetEdbSourceItemsSourceId(sourceId)
  70. if err != nil {
  71. return
  72. }
  73. if item.EdbSourceId > 0 {
  74. sourceItem = item
  75. EdbSourceIdMap[sourceId] = sourceItem
  76. }
  77. }
  78. return
  79. }