multiple_graph_config.go 7.4 KB

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