edb_source.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. )
  8. var (
  9. EdbSourceIdMap map[int]*EdbSource // 指标来源
  10. )
  11. type EdbSource struct {
  12. EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
  13. SourceName string `description:"指标来源名称"`
  14. SourceNameEn 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. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  26. o := global.DmSQL["data"]
  27. fields := strings.Join(fieldArr, ",")
  28. if len(fieldArr) == 0 {
  29. fields = `*`
  30. }
  31. order := `ORDER BY edb_source_id ASC`
  32. if orderRule != "" {
  33. order = ` ORDER BY ` + orderRule
  34. }
  35. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  36. err = o.Raw(sql, pars...).Find(&items).Error
  37. return
  38. }
  39. type EdbSourceChild struct {
  40. EdbSourceId int `orm:"column(edb_source_id);pk" gorm:"primaryKey" `
  41. SourceName string `description:"指标来源名称"`
  42. TableName string `description:"数据表名"`
  43. IsBase int `description:"是否为基础指标: 2-否; 1-是"`
  44. Child []EdbSourceChild
  45. }
  46. func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
  47. sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
  48. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  49. return
  50. }
  51. func InitEdbSourceVar() {
  52. EdbSourceIdMap = make(map[int]*EdbSource)
  53. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  54. if e != nil {
  55. utils.FileLog.Info("init source table err: %s", e.Error())
  56. return
  57. }
  58. for _, v := range sources {
  59. EdbSourceIdMap[v.EdbSourceId] = v
  60. }
  61. }
  62. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  63. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  64. err = global.DmSQL["data"].Raw(sql, sourceId).First(&item).Error
  65. return
  66. }
  67. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  68. sourceItem, ok := EdbSourceIdMap[sourceId]
  69. if !ok {
  70. item, err := GetEdbSourceItemsSourceId(sourceId)
  71. if err != nil {
  72. return
  73. }
  74. if item.EdbSourceId > 0 {
  75. sourceItem = item
  76. EdbSourceIdMap[sourceId] = sourceItem
  77. }
  78. }
  79. return
  80. }