predict_edb_conf.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package data_manage
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_chart_lib/services/alarm_msg"
  5. "time"
  6. )
  7. type PredictEdbConf struct {
  8. PredictEdbInfoId int `orm:"column(predict_edb_info_id);pk" description:"预测指标id"`
  9. SourceEdbInfoId int `description:"来源指标id"`
  10. RuleType int `description:"预测规则,1:最新,2:固定值"`
  11. FixedValue float64 `description:"固定值"`
  12. ModifyTime time.Time `description:"修改时间"`
  13. CreateTime time.Time `description:"添加时间"`
  14. }
  15. // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
  16. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  17. o := orm.NewOrmUsingDB("data")
  18. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  19. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  20. return
  21. }
  22. // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
  23. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  26. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
  27. return
  28. }
  29. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  30. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  31. o := orm.NewOrmUsingDB("data")
  32. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  33. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&count)
  34. return
  35. }
  36. // AddPredictEdbConf 添加预测指标规则
  37. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  38. o := orm.NewOrmUsingDB("data")
  39. lastId, err = o.Insert(item)
  40. return
  41. }
  42. // AddPredictEdb 添加预测指标
  43. func AddPredictEdb(item *EdbInfo, predictEdbConf *PredictEdbConf) (err error) {
  44. o := orm.NewOrmUsingDB("data")
  45. tx, err := o.Begin()
  46. if err != nil {
  47. return
  48. }
  49. defer func() {
  50. if err != nil {
  51. tmpErr := tx.Rollback()
  52. if tmpErr != nil {
  53. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  54. }
  55. } else {
  56. err = tx.Commit()
  57. }
  58. }()
  59. // 新增预测指标
  60. edbInfoId, err := o.Insert(item)
  61. if err != nil {
  62. return
  63. }
  64. item.EdbInfoId = int(edbInfoId)
  65. // 新增预测指标配置
  66. predictEdbConf.PredictEdbInfoId = item.EdbInfoId
  67. _, err = o.Insert(predictEdbConf)
  68. if err != nil {
  69. return
  70. }
  71. return
  72. }
  73. // EditPredictEdb 修改预测指标
  74. func EditPredictEdb(edbInfo *EdbInfo, predictEdbConf *PredictEdbConf, updateEdbInfoCol, updatePredictEdbConfCol []string) (err error) {
  75. o := orm.NewOrmUsingDB("data")
  76. tx, err := o.Begin()
  77. if err != nil {
  78. return
  79. }
  80. defer func() {
  81. if err != nil {
  82. tmpErr := tx.Rollback()
  83. if tmpErr != nil {
  84. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  85. }
  86. } else {
  87. err = tx.Commit()
  88. }
  89. }()
  90. // 修改预测指标
  91. _, err = o.Update(edbInfo, updateEdbInfoCol...)
  92. if err != nil {
  93. return
  94. }
  95. // 修改预测指标配置
  96. _, err = o.Update(predictEdbConf, updatePredictEdbConfCol...)
  97. if err != nil {
  98. return
  99. }
  100. return
  101. }