multiple_graph_config.go 6.7 KB

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