multiple_graph_config.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/models/data_manage/line_equation/request"
  6. "eta_gn/eta_api/services/alarm_msg"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // MultipleGraphConfig 多图配置表
  14. type MultipleGraphConfig struct {
  15. MultipleGraphConfigId int `orm:"column(multiple_graph_config_id);pk" gorm:"primaryKey" `
  16. EdbInfoIdA int `description:"指标A"`
  17. EdbInfoIdB int `description:"指标B"`
  18. Curve string `description:"曲线图配置"`
  19. Correlation string `description:"相关性配置"`
  20. RollingCorrelation string `description:"滚动相关性配置"`
  21. SysUserId int `description:"操作人id"`
  22. SysUserRealName string `description:"操作人真实姓名"`
  23. ModifyTime time.Time `description:"最近一次修改时间"`
  24. CreateTime time.Time `description:"添加时间"`
  25. }
  26. // AddMultipleGraphConfig 新增多图配置
  27. func AddMultipleGraphConfig(item *MultipleGraphConfig) (err error) {
  28. err = global.DmSQL["data"].Create(item).Error
  29. return
  30. }
  31. // Update 更新 基础信息
  32. func (item *MultipleGraphConfig) Update(cols []string) (err error) {
  33. err = global.DmSQL["data"].Select(cols).Updates(item).Error
  34. return
  35. }
  36. // GetMultipleGraphConfigById 根据配置id获取配置
  37. func GetMultipleGraphConfigById(id int) (item *MultipleGraphConfig, err error) {
  38. sql := `SELECT * FROM multiple_graph_config WHERE multiple_graph_config_id = ? `
  39. // 表格信息入库
  40. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  41. return
  42. }
  43. // CurveConfig 曲线图配置
  44. type CurveConfig struct {
  45. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  46. StartDate string `description:"自定义开始日期"`
  47. EndDate string `description:"自定义结束日期"`
  48. LeftMin float64 `description:"图表左侧最小值"`
  49. LeftMax float64 `description:"图表左侧最大值"`
  50. RightMin float64 `description:"图表右侧最小值"`
  51. RightMax float64 `description:"图表右侧最大值"`
  52. Right2Min float64 `description:"图表右侧最小值"`
  53. Right2Max float64 `description:"图表右侧最大值"`
  54. IsOrder bool `description:"true:正序,false:逆序"`
  55. EdbInfoType bool `description:"true:标准指标,false:领先指标"`
  56. LeadValue int `description:"领先值"`
  57. LeadUnit string `description:"领先单位"`
  58. StartYear int `description:"最近N年"`
  59. }
  60. // CorrelationConfig 相关性配置
  61. type CorrelationConfig struct {
  62. LeadValue int `description:"领先期数"`
  63. LeadUnit string `description:"频度"`
  64. CalculateValue int `description:"计算窗口"`
  65. CalculateUnit string `description:"计算频度"`
  66. }
  67. // RollingCorrelationConfig 滚动相关性配置
  68. type RollingCorrelationConfig struct {
  69. LeadValue int `description:"领先期数"`
  70. LeadUnit string `description:"频度"`
  71. CalculateValue int `description:"计算窗口"`
  72. CalculateUnit string `description:"计算频度"`
  73. }
  74. // ReplaceEdbInfoInLineEquationMultipleGraphConfig 获取拟合方程配置
  75. func ReplaceEdbInfoInLineEquationMultipleGraphConfig(oldEdbInfo, newEdbInfo *EdbInfo) (replaceConfigTotal int, err error) {
  76. var errmsg string
  77. logMsg := `` // 记录替换的日志
  78. to := global.DmSQL["data"].Begin()
  79. defer func() {
  80. if err != nil {
  81. _ = to.Rollback()
  82. } else {
  83. _ = to.Commit()
  84. if logMsg != `` {
  85. utils.FileLog.Info(fmt.Sprintf("替换拟合方程中的指标记录 替换总数%d,旧的指标id:%d,新的指标id:%d;%s", replaceConfigTotal, oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  86. }
  87. }
  88. if errmsg != "" {
  89. fmt.Println("errmsg:" + errmsg)
  90. }
  91. if err != nil && errmsg != "" {
  92. go alarm_msg.SendAlarmMsg("替换拟合方程中的指标记录失败提醒,errmsg:"+errmsg, 3)
  93. }
  94. }()
  95. //替换multiple_graph_config中的指标
  96. {
  97. multipleGraphConfigList := make([]*MultipleGraphConfig, 0)
  98. csql := `SELECT * FROM multiple_graph_config WHERE edb_info_id_a=0 AND edb_info_id_b=0 AND curve !=''`
  99. err = to.Raw(csql).Find(&multipleGraphConfigList).Error
  100. if err != nil {
  101. errmsg = "获取指标关联图表配置信息失败:Err:" + err.Error()
  102. return
  103. }
  104. if len(multipleGraphConfigList) == 0 {
  105. return
  106. }
  107. updateList := make([]*MultipleGraphConfig, 0)
  108. configIds := make([]int, 0)
  109. configIdStr := make([]string, 0)
  110. for _, mv := range multipleGraphConfigList {
  111. if !strings.Contains(mv.Curve, strconv.Itoa(oldEdbInfo.EdbInfoId)) {
  112. continue
  113. }
  114. //解析curve内容
  115. var lineChartInfoConfig request.LineChartInfoReq
  116. if err = json.Unmarshal([]byte(mv.Curve), &lineChartInfoConfig); err != nil {
  117. errmsg = "获取跨品种分析配置信息失败:Err:" + err.Error()
  118. return
  119. }
  120. updateFlag := false
  121. // 遍历跨品种配置里的指标列表
  122. for k, edbInfoId := range lineChartInfoConfig.XEdbInfoIdList {
  123. if edbInfoId == oldEdbInfo.EdbInfoId {
  124. updateFlag = true
  125. lineChartInfoConfig.XEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  126. }
  127. }
  128. for k, edbInfoId := range lineChartInfoConfig.YEdbInfoIdList {
  129. if edbInfoId == oldEdbInfo.EdbInfoId {
  130. updateFlag = true
  131. lineChartInfoConfig.YEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  132. }
  133. }
  134. if !updateFlag {
  135. continue
  136. }
  137. newCurve, _ := json.Marshal(lineChartInfoConfig)
  138. mv.Curve = string(newCurve)
  139. //判断如果达到1000个数,则执行更新语句
  140. updateList = append(updateList, mv)
  141. configIds = append(configIds, mv.MultipleGraphConfigId)
  142. configIdStr = append(configIdStr, strconv.Itoa(mv.MultipleGraphConfigId))
  143. if len(configIds) >= 10 {
  144. numStr := utils.GetOrmInReplace(len(configIds))
  145. updateSQL := `UPDATE multiple_graph_config SET
  146. curve = CASE multiple_graph_config_id `
  147. for _, updateItem := range updateList {
  148. updateSQL += `WHEN ` + strconv.Itoa(updateItem.MultipleGraphConfigId) + ` THEN '` + updateItem.Curve + `' `
  149. }
  150. updateSQL += `END, modify_time = ? WHERE multiple_graph_config_id IN (` + numStr + `)`
  151. err = to.Exec(updateSQL, time.Now(), configIds).Error
  152. if err != nil {
  153. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  154. return
  155. }
  156. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  157. replaceConfigTotal += len(configIds)
  158. configIds = make([]int, 0)
  159. updateList = make([]*MultipleGraphConfig, 0)
  160. configIdStr = make([]string, 0)
  161. }
  162. }
  163. if len(configIds) > 0 {
  164. numStr := utils.GetOrmInReplace(len(configIds))
  165. updateSQL := `UPDATE multiple_graph_config SET
  166. curve = CASE multiple_graph_config_id `
  167. for _, updateItem := range updateList {
  168. updateSQL += `WHEN ` + strconv.Itoa(updateItem.MultipleGraphConfigId) + ` THEN '` + updateItem.Curve + `' `
  169. }
  170. updateSQL += `END, modify_time = ? WHERE multiple_graph_config_id IN (` + numStr + `)`
  171. err = to.Exec(updateSQL, time.Now(), configIds).Error
  172. if err != nil {
  173. errmsg = "更新指标A关联图表配置信息失败:Err:" + err.Error()
  174. return
  175. }
  176. logMsg += `涉及到的配置id:` + strings.Join(configIdStr, ",") + ";"
  177. replaceConfigTotal += len(configIds)
  178. configIds = make([]int, 0)
  179. updateList = make([]*MultipleGraphConfig, 0)
  180. configIdStr = make([]string, 0)
  181. }
  182. }
  183. return
  184. }