edb_source.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  6. "strings"
  7. )
  8. var (
  9. EdbDataTableNameMap map[int]string // 指标来源对应数据表名
  10. EdbDataRefreshMethodMap map[int]string // 指标来源对应的刷新指标方法
  11. EdbTableNameSourceMap map[string]*EdbSource // 数据表名对应的指标来源
  12. EdbSourceIdMap map[int]*EdbSource // 指标来源
  13. EdbSourceExtendIdMap map[string]int // 指标来源字符串对应来源ID
  14. EdbNameSourceMap map[string]*EdbSource // 数据来源名对应的指标来源
  15. )
  16. type EdbSource struct {
  17. EdbSourceId int `gorm:"column:edb_source_id;primaryKey"` // `orm:"column(edb_source_id);pk"`
  18. SourceName string `description:"指标来源名称"`
  19. TableName string `description:"数据表名"`
  20. EdbAddMethod string `description:"指标新增接口"`
  21. EdbRefreshMethod string `description:"指标刷新接口"`
  22. IsBase int `description:"是否为基础指标: 0-否; 1-是"`
  23. FromBridge int `description:"是否来源于桥接服务: 0-否; 1-是"`
  24. BridgeFlag string `description:"桥接服务对象标识"`
  25. SourceExtend string `description:"扩展字段做查询用"`
  26. EdbCodeRequired int32 `gorm:"column:edb_code_required;type:tinyint(4);comment:指标编码是否必填: 0-否; 1-是;not null;default:0;"` // 指标编码是否必填: 0-否; 1-是
  27. IndexTableName string `gorm:"column:index_table_name;type:varchar(128);comment:源指标表名;not null;"` // 源指标表名
  28. SourceNameEn string `gorm:"column:source_name_en;type:varchar(128);comment:指标来源名称-英文;not null;"` // 指标来源名称-英文
  29. }
  30. func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
  31. fields := strings.Join(fieldArr, ",")
  32. if len(fieldArr) == 0 {
  33. fields = `*`
  34. }
  35. order := `ORDER BY edb_source_id ASC`
  36. if orderRule != "" {
  37. order = ` ORDER BY ` + orderRule
  38. }
  39. sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
  40. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  41. return
  42. }
  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.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  46. return
  47. }
  48. func InitEdbSourceVar() {
  49. EdbDataTableNameMap = make(map[int]string)
  50. EdbDataRefreshMethodMap = make(map[int]string)
  51. EdbTableNameSourceMap = make(map[string]*EdbSource)
  52. EdbNameSourceMap = make(map[string]*EdbSource)
  53. EdbSourceIdMap = make(map[int]*EdbSource)
  54. EdbSourceExtendIdMap = make(map[string]int)
  55. sources, e := GetEdbSourceItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  56. if e != nil {
  57. utils.FileLog.Info("init source table err: %s", e.Error())
  58. return
  59. }
  60. for _, v := range sources {
  61. EdbDataTableNameMap[v.EdbSourceId] = v.TableName
  62. EdbDataRefreshMethodMap[v.EdbSourceId] = v.EdbRefreshMethod
  63. EdbTableNameSourceMap[v.TableName] = v
  64. EdbNameSourceMap[v.SourceName] = v
  65. EdbSourceIdMap[v.EdbSourceId] = v
  66. if v.SourceExtend != "" {
  67. arr := strings.Split(v.SourceExtend, ",")
  68. if len(arr) == 0 {
  69. continue
  70. }
  71. for _, s := range arr {
  72. EdbSourceExtendIdMap[s] = v.EdbSourceId
  73. }
  74. }
  75. }
  76. }
  77. func GetEdbSourceItemsSourceId(sourceId int) (item *EdbSource, err error) {
  78. sql := `SELECT * FROM edb_source WHERE 1=1 AND edb_source_id = ? `
  79. err = global.DmSQL["data"].Raw(sql, sourceId).First(&item).Error
  80. return
  81. }
  82. func GetEdbSourceBySourceId(sourceId int) (sourceItem *EdbSource) {
  83. sourceItem, ok := EdbSourceIdMap[sourceId]
  84. if !ok {
  85. item, err := GetEdbSourceItemsSourceId(sourceId)
  86. if err != nil {
  87. return
  88. }
  89. if item.EdbSourceId > 0 {
  90. sourceItem = item
  91. EdbSourceIdMap[sourceId] = sourceItem
  92. EdbDataTableNameMap[sourceId] = sourceItem.TableName
  93. EdbDataRefreshMethodMap[sourceItem.EdbSourceId] = sourceItem.EdbRefreshMethod
  94. EdbTableNameSourceMap[sourceItem.TableName] = sourceItem
  95. EdbNameSourceMap[sourceItem.SourceName] = sourceItem
  96. }
  97. }
  98. return
  99. }
  100. func GetEdbSourceItemsSourceName(sourceName string) (item *EdbSource, err error) {
  101. sql := `SELECT * FROM edb_source WHERE 1=1 AND source_name = ? `
  102. err = global.DmSQL["data"].Raw(sql, sourceName).First(&item).Error
  103. return
  104. }
  105. func GetEdbSourceBySourceName(sourceName string) (sourceItem *EdbSource) {
  106. sourceItem, ok := EdbNameSourceMap[sourceName]
  107. if !ok {
  108. item, err := GetEdbSourceItemsSourceName(sourceName)
  109. if err != nil {
  110. return
  111. }
  112. if item.EdbSourceId > 0 {
  113. sourceItem = item
  114. EdbSourceIdMap[sourceItem.EdbSourceId] = sourceItem
  115. EdbDataTableNameMap[sourceItem.EdbSourceId] = sourceItem.TableName
  116. EdbDataRefreshMethodMap[sourceItem.EdbSourceId] = sourceItem.EdbRefreshMethod
  117. EdbTableNameSourceMap[sourceItem.TableName] = sourceItem
  118. EdbNameSourceMap[sourceItem.SourceName] = sourceItem
  119. }
  120. }
  121. return
  122. }
  123. func GetEdbSourceTableNameBySourceId(sourceId int) (tableName string) {
  124. sourceItem := GetEdbSourceBySourceId(sourceId)
  125. if sourceItem != nil {
  126. tableName = sourceItem.TableName
  127. }
  128. return
  129. }
  130. func GetEdbSourceRefreshMethodBySourceId(sourceId int) (refreshMethod string) {
  131. sourceItem := GetEdbSourceBySourceId(sourceId)
  132. if sourceItem != nil {
  133. refreshMethod = sourceItem.EdbRefreshMethod
  134. }
  135. return
  136. }
  137. func AddEdbSource(item *EdbSource, indexNamePrefix string) (err error) {
  138. o := global.DmSQL["data"].Begin()
  139. if err != nil {
  140. return
  141. }
  142. defer func() {
  143. if err != nil {
  144. _ = o.Rollback()
  145. return
  146. }
  147. _ = o.Commit()
  148. }()
  149. indexName1 := fmt.Sprintf(`INDEX_%s_EDB_CODE`, indexNamePrefix)
  150. indexName2 := fmt.Sprintf(`INDEX_%s_EDB_INFO_ID`, indexNamePrefix)
  151. sqlStatements := []string{
  152. fmt.Sprintf(`CREATE TABLE "%s"
  153. (
  154. "edb_data_id" INT IDENTITY(1, 1) NOT NULL,
  155. "edb_info_id" INT,
  156. "edb_code" VARCHAR(50),
  157. "data_time" DATE,
  158. "value" DOUBLE,
  159. "create_time" TIMESTAMP(0),
  160. "modify_time" TIMESTAMP(0),
  161. "data_timestamp" BIGINT DEFAULT 0,
  162. NOT CLUSTER PRIMARY KEY("edb_data_id"),
  163. UNIQUE("edb_code", "data_time")) STORAGE(ON "MAIN", CLUSTERBTR) ;
  164. `, item.TableName),
  165. fmt.Sprintf(`COMMENT ON COLUMN "%s"."create_time" IS '创建时间';`, item.TableName),
  166. fmt.Sprintf(`COMMENT ON COLUMN "%s"."data_time" IS '数据日期';`, item.TableName),
  167. fmt.Sprintf(`COMMENT ON COLUMN "%s"."data_timestamp" IS '数据日期时间戳';`, item.TableName),
  168. fmt.Sprintf(`COMMENT ON COLUMN "%s"."edb_code" IS '指标编码';`, item.TableName),
  169. fmt.Sprintf(`COMMENT ON COLUMN "%s"."edb_info_id" IS '指标id';`, item.TableName),
  170. fmt.Sprintf(`COMMENT ON COLUMN "%s"."modify_time" IS '修改时间';`, item.TableName),
  171. fmt.Sprintf(`COMMENT ON COLUMN "%s"."value" IS '数据值';`, item.TableName),
  172. fmt.Sprintf(`CREATE OR REPLACE INDEX "%s" ON "%s"("edb_code" ASC) STORAGE(ON "MAIN", CLUSTERBTR) ;`, indexName1, item.TableName),
  173. fmt.Sprintf(`CREATE OR REPLACE INDEX "%s" ON "%s"("edb_info_id" ASC) STORAGE(ON "MAIN", CLUSTERBTR) ;`, indexName2, item.TableName),
  174. }
  175. for _, sql := range sqlStatements {
  176. err = o.Exec(sql).Error
  177. if err != nil {
  178. return
  179. }
  180. }
  181. err = o.Create(item).Error
  182. if err != nil {
  183. return
  184. }
  185. return
  186. }