multiple_graph_config.go 7.5 KB

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