multiple_graph_config_chart_mapping.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package data_manage
  2. import (
  3. "eta/eta_api/services/alarm_msg"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // MultipleGraphConfigChartMapping 图表与多图配置的关系表
  12. type MultipleGraphConfigChartMapping struct {
  13. Id int `orm:"column(id);pk"`
  14. MultipleGraphConfigId int `description:"多图配置id"`
  15. ChartInfoId int `description:"图表id"`
  16. Source int `description:"来源,1:曲线图,2:相关性图;3:滚动相关性图1;4:滚动相关性图2;"`
  17. ModifyTime time.Time `description:"最近一次修改时间"`
  18. CreateTime time.Time `description:"添加时间"`
  19. }
  20. // AddMultipleGraphConfigChartMapping 新增多图配置
  21. func AddMultipleGraphConfigChartMapping(item *MultipleGraphConfigChartMapping) (err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. // 表格信息入库
  24. lastId, err := o.Insert(item)
  25. if err != nil {
  26. return
  27. }
  28. item.Id = int(lastId)
  29. return
  30. }
  31. // Update 更新 基础信息
  32. func (item *MultipleGraphConfigChartMapping) Update(cols []string) (err error) {
  33. o := orm.NewOrmUsingDB("data")
  34. _, err = o.Update(item, cols...)
  35. return
  36. }
  37. // GetMultipleGraphConfigChartMappingByIdAndSource 根据配置id和来源获取关联关系
  38. func GetMultipleGraphConfigChartMappingByIdAndSource(configId, source int) (item *MultipleGraphConfigChartMapping, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE multiple_graph_config_id = ? AND source = ? `
  41. err = o.Raw(sql, configId, source).QueryRow(&item)
  42. return
  43. }
  44. // GetMultipleGraphConfigChartMappingByChartId 根据图表id和来源获取关联关系
  45. func GetMultipleGraphConfigChartMappingByChartId(chartId int) (item *MultipleGraphConfigChartMapping, err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE chart_info_id = ? `
  48. err = o.Raw(sql, chartId).QueryRow(&item)
  49. return
  50. }
  51. // GetMultipleGraphConfigChartMappingListById 根据配置id获取所有关联关系
  52. func GetMultipleGraphConfigChartMappingListById(configId int) (items []*MultipleGraphConfigChartMapping, err error) {
  53. o := orm.NewOrmUsingDB("data")
  54. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE multiple_graph_config_id = ? `
  55. _, err = o.Raw(sql, configId).QueryRows(&items)
  56. return
  57. }
  58. // ReplaceMultipleGraphConfigChartEdb 替换配置中的指标
  59. func ReplaceMultipleGraphConfigChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (replaceConfigTotal int, err error) {
  60. var errmsg string
  61. logMsg := `` // 记录替换的日志
  62. o := orm.NewOrmUsingDB("data")
  63. to, err := o.Begin()
  64. if err != nil {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. _ = to.Rollback()
  70. } else {
  71. _ = to.Commit()
  72. if logMsg != `` {
  73. utils.FileLog.Info(fmt.Sprintf("替换替换配置中的指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  74. }
  75. }
  76. if errmsg != "" {
  77. fmt.Println("errmsg:" + errmsg)
  78. }
  79. if err != nil && errmsg != "" {
  80. go alarm_msg.SendAlarmMsg("替换替换配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
  81. }
  82. }()
  83. //替换multiple_graph_config中的指标
  84. {
  85. multipleGraphConfigList := make([]*MultipleGraphConfig, 0)
  86. csql := `SELECT * FROM multiple_graph_config WHERE (edb_info_id_a=? or edb_info_id_b=?)`
  87. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).QueryRows(&multipleGraphConfigList)
  88. if err != nil {
  89. errmsg = "获取指标关联图表配置信息失败:Err:" + err.Error()
  90. return
  91. }
  92. replaceConfigTotal = len(multipleGraphConfigList)
  93. configIdMap := make(map[int]int)
  94. configIds := make([]int, 0)
  95. configIdStr := make([]string, 0)
  96. for _, mv := range multipleGraphConfigList {
  97. if _, ok := configIdMap[mv.MultipleGraphConfigId]; !ok {
  98. //判断如果达到1000个数,则执行更新语句
  99. if len(configIds) >= 1000 {
  100. //更新配置中的指标A
  101. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, update_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id_a=?`
  102. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  103. if err != nil {
  104. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  105. return
  106. }
  107. //更新配置中的指标B
  108. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, update_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id_b=?`
  109. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  110. if err != nil {
  111. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  112. return
  113. }
  114. // 更新指标id
  115. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id=?`
  116. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  117. if err != nil {
  118. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  119. return
  120. }
  121. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  122. configIds = make([]int, 0)
  123. configIdStr = make([]string, 0)
  124. }
  125. configIds = append(configIds, mv.MultipleGraphConfigId)
  126. configIdStr = append(configIdStr, strconv.Itoa(mv.MultipleGraphConfigId))
  127. configIdMap[mv.MultipleGraphConfigId] = mv.MultipleGraphConfigId
  128. }
  129. }
  130. if len(configIds) >= 1000 {
  131. //更新配置中的指标A
  132. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, update_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id_a=?`
  133. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  134. if err != nil {
  135. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  136. return
  137. }
  138. //更新配置中的指标B
  139. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, update_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id_b=?`
  140. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  141. if err != nil {
  142. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  143. return
  144. }
  145. // 更新指标id
  146. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE multiple_graph_config_id IN (?) and edb_info_id=?`
  147. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), configIds, oldEdbInfo.EdbInfoId).Exec()
  148. if err != nil {
  149. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  150. return
  151. }
  152. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  153. configIds = make([]int, 0)
  154. configIdStr = make([]string, 0)
  155. }
  156. }
  157. return
  158. }