predict_edb_conf.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package data_manage
  2. import (
  3. "eta/eta_chart_lib/services/alarm_msg"
  4. "eta/eta_chart_lib/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type PredictEdbConf struct {
  9. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  10. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  11. SourceEdbInfoId int `description:"来源指标id"`
  12. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值"`
  13. FixedValue float64 `description:"固定值"`
  14. Value string `description:"配置的值"`
  15. EndDate time.Time `description:"截止日期"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. CreateTime time.Time `description:"添加时间"`
  18. }
  19. // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
  20. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  21. o := orm.NewOrmUsingDB("data")
  22. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  23. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  24. return
  25. }
  26. // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
  27. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  30. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
  31. return
  32. }
  33. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  34. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  37. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&count)
  38. return
  39. }
  40. // AddPredictEdbConf 添加预测指标规则
  41. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. lastId, err = o.Insert(item)
  44. return
  45. }
  46. // AddPredictEdb 添加预测指标
  47. func AddPredictEdb(item *EdbInfo, predictEdbConf *PredictEdbConf) (err error) {
  48. o := orm.NewOrmUsingDB("data")
  49. tx, err := o.Begin()
  50. if err != nil {
  51. return
  52. }
  53. defer func() {
  54. if err != nil {
  55. tmpErr := tx.Rollback()
  56. if tmpErr != nil {
  57. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  58. }
  59. } else {
  60. err = tx.Commit()
  61. }
  62. }()
  63. // 新增预测指标
  64. edbInfoId, err := o.Insert(item)
  65. if err != nil {
  66. return
  67. }
  68. item.EdbInfoId = int(edbInfoId)
  69. // 新增预测指标配置
  70. predictEdbConf.PredictEdbInfoId = item.EdbInfoId
  71. _, err = o.Insert(predictEdbConf)
  72. if err != nil {
  73. return
  74. }
  75. return
  76. }
  77. // EditPredictEdb 修改预测指标
  78. func EditPredictEdb(edbInfo *EdbInfo, predictEdbConf *PredictEdbConf, updateEdbInfoCol, updatePredictEdbConfCol []string) (err error) {
  79. o := orm.NewOrmUsingDB("data")
  80. tx, err := o.Begin()
  81. if err != nil {
  82. return
  83. }
  84. defer func() {
  85. if err != nil {
  86. tmpErr := tx.Rollback()
  87. if tmpErr != nil {
  88. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  89. }
  90. } else {
  91. err = tx.Commit()
  92. }
  93. }()
  94. // 修改预测指标
  95. _, err = o.Update(edbInfo, updateEdbInfoCol...)
  96. if err != nil {
  97. return
  98. }
  99. // 修改预测指标配置
  100. _, err = o.Update(predictEdbConf, updatePredictEdbConfCol...)
  101. if err != nil {
  102. return
  103. }
  104. return
  105. }
  106. // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
  107. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  108. num := len(edbInfoIdList)
  109. if num <= 0 {
  110. return
  111. }
  112. o := orm.NewOrmUsingDB("data")
  113. sql := ` SELECT b.* FROM predict_edb_conf AS a
  114. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  115. WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  116. GROUP BY a.source_edb_info_id
  117. ORDER BY a.source_edb_info_id ASC `
  118. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  119. return
  120. }
  121. // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
  122. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  125. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  126. return
  127. }