multiple_graph_config.go 9.8 KB

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