multiple_graph_config_chart_mapping.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/services/alarm_msg"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // MultipleGraphConfigChartMapping 图表与多图配置的关系表
  12. type MultipleGraphConfigChartMapping struct {
  13. Id int `orm:"column(id);pk" gorm:"primaryKey"`
  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 := global.DbMap[utils.DbNameIndex]
  23. // 表格信息入库
  24. err = o.Create(item).Error
  25. return
  26. }
  27. // Update 更新 基础信息
  28. func (item *MultipleGraphConfigChartMapping) Update(cols []string) (err error) {
  29. o := global.DbMap[utils.DbNameIndex]
  30. err = o.Select(cols).Updates(item).Error
  31. return
  32. }
  33. // GetMultipleGraphConfigChartMappingByIdAndSource 根据配置id和来源获取关联关系
  34. func GetMultipleGraphConfigChartMappingByIdAndSource(configId, source int) (item *MultipleGraphConfigChartMapping, err error) {
  35. o := global.DbMap[utils.DbNameIndex]
  36. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE multiple_graph_config_id = ? AND source = ? `
  37. err = o.Raw(sql, configId, source).Find(&item).Error
  38. return
  39. }
  40. // GetMultipleGraphConfigChartMappingByChartId 根据图表id和来源获取关联关系
  41. func GetMultipleGraphConfigChartMappingByChartId(chartId int) (item *MultipleGraphConfigChartMapping, err error) {
  42. o := global.DbMap[utils.DbNameIndex]
  43. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE chart_info_id = ? `
  44. err = o.Raw(sql, chartId).Find(&item).Error
  45. return
  46. }
  47. // GetMultipleGraphConfigChartMappingListById 根据配置id获取所有关联关系
  48. func GetMultipleGraphConfigChartMappingListById(configId int) (items []*MultipleGraphConfigChartMapping, err error) {
  49. o := global.DbMap[utils.DbNameIndex]
  50. sql := `SELECT * FROM multiple_graph_config_chart_mapping WHERE multiple_graph_config_id = ? `
  51. err = o.Raw(sql, configId).Find(&items).Error
  52. return
  53. }
  54. // ReplaceMultipleGraphConfigChartEdb 替换相关性分析配置中的指标
  55. func ReplaceMultipleGraphConfigChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (replaceConfigTotal int, err error) {
  56. var errmsg string
  57. logMsg := `` // 记录替换的日志
  58. o := global.DbMap[utils.DbNameIndex]
  59. to := o.Begin()
  60. defer func() {
  61. if err != nil {
  62. _ = to.Rollback()
  63. } else {
  64. _ = to.Commit()
  65. if logMsg != `` {
  66. utils.FileLog.Info(fmt.Sprintf("替换相关性分析中的指标记录 替换总数%d,旧的指标id:%d,新的指标id:%d;%s", replaceConfigTotal, oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  67. }
  68. }
  69. if errmsg != "" {
  70. fmt.Println("errmsg:" + errmsg)
  71. }
  72. if err != nil && errmsg != "" {
  73. go alarm_msg.SendAlarmMsg("替换统计分析配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
  74. }
  75. }()
  76. //替换multiple_graph_config中的指标
  77. multipleGraphConfigList := make([]*MultipleGraphConfig, 0)
  78. csql := `SELECT * FROM multiple_graph_config WHERE (edb_info_id_a=? or edb_info_id_b=?)`
  79. err = to.Raw(csql, oldEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Find(&multipleGraphConfigList).Error
  80. if err != nil {
  81. errmsg = "获取指标关联图表配置信息失败:Err:" + err.Error()
  82. return
  83. }
  84. if len(multipleGraphConfigList) > 0 {
  85. replaceConfigTotal = len(multipleGraphConfigList)
  86. configIdMap := make(map[int]int)
  87. configIds := make([]int, 0)
  88. configIdStr := make([]string, 0)
  89. for _, mv := range multipleGraphConfigList {
  90. if _, ok := configIdMap[mv.MultipleGraphConfigId]; !ok {
  91. //判断如果达到1000个数,则执行更新语句
  92. configIds = append(configIds, mv.MultipleGraphConfigId)
  93. configIdStr = append(configIdStr, strconv.Itoa(mv.MultipleGraphConfigId))
  94. configIdMap[mv.MultipleGraphConfigId] = mv.MultipleGraphConfigId
  95. if len(configIds) >= 1000 {
  96. numStr := utils.GetOrmInReplace(len(configIds))
  97. //更新配置中的指标A
  98. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, modify_time=? WHERE edb_info_id_a=? and multiple_graph_config_id IN (` + numStr + `)`
  99. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  100. if err != nil {
  101. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  102. return
  103. }
  104. //更新配置中的指标B
  105. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, modify_time=? WHERE edb_info_id_b=? and multiple_graph_config_id IN (` + numStr + `)`
  106. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  107. if err != nil {
  108. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  109. return
  110. }
  111. // 更新
  112. // 更新指标id
  113. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE edb_info_id=? and multiple_graph_config_id IN (` + numStr + `)`
  114. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  115. if err != nil {
  116. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  117. return
  118. }
  119. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  120. configIds = make([]int, 0)
  121. configIdStr = make([]string, 0)
  122. }
  123. }
  124. }
  125. if len(configIds) > 0 {
  126. numStr := utils.GetOrmInReplace(len(configIds))
  127. //更新配置中的指标A
  128. sql := `UPDATE multiple_graph_config SET edb_info_id_a=?, modify_time=? WHERE edb_info_id_a=? and multiple_graph_config_id IN (` + numStr + `)`
  129. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  130. if err != nil {
  131. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  132. return
  133. }
  134. //更新配置中的指标B
  135. sql = `UPDATE multiple_graph_config SET edb_info_id_b=?, modify_time=? WHERE edb_info_id_b=? and multiple_graph_config_id IN (` + numStr + `)`
  136. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  137. if err != nil {
  138. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  139. return
  140. }
  141. // 更新指标id
  142. sql = `UPDATE multiple_graph_config_edb_mapping SET edb_info_id=?, modify_time=? WHERE edb_info_id=? and multiple_graph_config_id IN (` + numStr + `)`
  143. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId, configIds).Error
  144. if err != nil {
  145. errmsg = "更新指标id关联图表配置信息失败:Err:" + err.Error()
  146. return
  147. }
  148. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  149. configIds = make([]int, 0)
  150. configIdStr = make([]string, 0)
  151. }
  152. }
  153. // 更新相关性图表中的
  154. sql := `UPDATE chart_info_correlation SET edb_info_id_first=?, modify_time=? WHERE edb_info_id_first=?`
  155. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Error
  156. if err != nil {
  157. errmsg = "更新相关性图表中的指标id关联失败:Err:" + err.Error()
  158. return
  159. }
  160. sql = `UPDATE chart_info_correlation SET edb_info_id_second=?, modify_time=? WHERE edb_info_id_second=?`
  161. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Error
  162. if err != nil {
  163. errmsg = "更新相关性图表中的指标id关联失败:Err:" + err.Error()
  164. return
  165. }
  166. // 替换拟合方程曲线替换指标
  167. // 替换跨品种标签绑定得到指标ID
  168. sql = `UPDATE chart_tag_variety SET edb_info_id=?, modify_time=? WHERE edb_info_id=?`
  169. err = to.Exec(sql, newEdbInfo.EdbInfoId, time.Now(), oldEdbInfo.EdbInfoId).Error
  170. if err != nil {
  171. errmsg = "更新指标id关联跨品种分析标签失败:Err:" + err.Error()
  172. return
  173. }
  174. return
  175. }
  176. func RemoveMultiConfigChartMappingByChartInfoId(chartInfoId int) (err error) {
  177. o := global.DbMap[utils.DbNameIndex]
  178. sql := `DELETE FROM multiple_graph_config_chart_mapping WHERE chart_info_id = ?`
  179. err = o.Exec(sql, chartInfoId).Error
  180. return
  181. }