multiple_graph_config_chart_mapping.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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("替换相关性分析中的指标记录 替换总数%d,旧的指标id:%d,新的指标id:%d;%s", replaceConfigTotal, 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. multipleGraphConfigList := make([]*MultipleGraphConfig, 0)
  85. csql := `SELECT * FROM multiple_graph_config WHERE (edb_info_id_a=? or edb_info_id_b=?)`
  86. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).QueryRows(&multipleGraphConfigList)
  87. if err != nil {
  88. errmsg = "获取指标关联图表配置信息失败:Err:" + err.Error()
  89. return
  90. }
  91. if len(multipleGraphConfigList) > 0 {
  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. configIds = append(configIds, mv.MultipleGraphConfigId)
  100. configIdStr = append(configIdStr, strconv.Itoa(mv.MultipleGraphConfigId))
  101. configIdMap[mv.MultipleGraphConfigId] = mv.MultipleGraphConfigId
  102. if len(configIds) >= 1000 {
  103. numStr := utils.GetOrmInReplace(len(configIds))
  104. //更新配置中的指标A
  105. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, modify_time=? WHERE edb_info_id_a=? and multiple_graph_config_id IN (` + numStr + `)`
  106. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  107. if err != nil {
  108. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  109. return
  110. }
  111. //更新配置中的指标B
  112. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, modify_time=? WHERE edb_info_id_b=? and multiple_graph_config_id IN (` + numStr + `)`
  113. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  114. if err != nil {
  115. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  116. return
  117. }
  118. // 更新
  119. // 更新指标id
  120. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE edb_info_id=? and multiple_graph_config_id IN (` + numStr + `)`
  121. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  122. if err != nil {
  123. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  124. return
  125. }
  126. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  127. configIds = make([]int, 0)
  128. configIdStr = make([]string, 0)
  129. }
  130. }
  131. }
  132. if len(configIds) > 0 {
  133. numStr := utils.GetOrmInReplace(len(configIds))
  134. //更新配置中的指标A
  135. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, modify_time=? WHERE edb_info_id_a=? and multiple_graph_config_id IN (` + numStr + `)`
  136. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  137. if err != nil {
  138. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  139. return
  140. }
  141. //更新配置中的指标B
  142. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, modify_time=? WHERE edb_info_id_b=? and multiple_graph_config_id IN (` + numStr + `)`
  143. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  144. if err != nil {
  145. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  146. return
  147. }
  148. // 更新指标id
  149. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE edb_info_id=? and multiple_graph_config_id IN (` + numStr + `)`
  150. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Exec()
  151. if err != nil {
  152. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  153. return
  154. }
  155. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  156. configIds = make([]int, 0)
  157. configIdStr = make([]string, 0)
  158. }
  159. }
  160. // 更新相关性图表中的
  161. sql := `UPDATE chart_info_correlation SET edb_info_id_first=?, modify_time=? WHERE edb_info_id_first=?`
  162. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Exec()
  163. if err != nil {
  164. errmsg = "更新相关性图表中的指标id关联失败:Err:" + err.Error()
  165. return
  166. }
  167. sql = `UPDATE chart_info_correlation SET edb_info_id_second=?, modify_time=? WHERE edb_info_id_second=?`
  168. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Exec()
  169. if err != nil {
  170. errmsg = "更新相关性图表中的指标id关联失败:Err:" + err.Error()
  171. return
  172. }
  173. // 替换拟合方程曲线替换指标
  174. // 替换跨品种标签绑定得到指标ID
  175. sql = `UPDATE chart_tag_variety SET edb_info_id=?, modify_time=? WHERE edb_info_id=?`
  176. _, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Exec()
  177. if err != nil {
  178. errmsg = "更新指标id关联跨品种分析标签失败:Err:" + err.Error()
  179. return
  180. }
  181. return
  182. }
  183. func RemoveMultiConfigChartMappingByChartInfoId(chartInfoId int) (err error) {
  184. o := orm.NewOrmUsingDB("data")
  185. sql := `DELETE FROM multiple_graph_config_chart_mapping WHERE chart_info_id = ?`
  186. _, err = o.Raw(sql, chartInfoId).Exec()
  187. return
  188. }