predict_edb_conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package data_manage
  2. import (
  3. "eta_gn/eta_chart_lib/global"
  4. "eta_gn/eta_chart_lib/services/alarm_msg"
  5. "eta_gn/eta_chart_lib/utils"
  6. "time"
  7. )
  8. type PredictEdbConf struct {
  9. ConfigId int `gorm:"column:config_id;primaryKey" description:"规则id" orm:"column(config_id);pk"`
  10. PredictEdbInfoId int `gorm:"column:predict_edb_info_id" description:"预测指标id"`
  11. SourceEdbInfoId int `gorm:"column:source_edb_info_id" description:"来源指标id"`
  12. RuleType int `gorm:"column:rule_type" description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值"`
  13. FixedValue float64 `gorm:"column:fixed_value" description:"固定值"`
  14. Value string `gorm:"column:value" description:"配置的值"`
  15. EndDate time.Time `gorm:"column:end_date" description:"截止日期"`
  16. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  17. CreateTime time.Time `gorm:"column:create_time" description:"添加时间"`
  18. }
  19. func (p *PredictEdbConf) TableName() string {
  20. return "predict_edb_conf"
  21. }
  22. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  23. o := global.DmSQL["data"]
  24. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  25. err = o.Raw(sql, edbInfoId).First(&item).Error
  26. return
  27. }
  28. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  29. o := global.DmSQL["data"]
  30. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  31. err = o.Raw(sql, sourceEdbInfoId).First(&item).Error
  32. return
  33. }
  34. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  35. o := global.DmSQL["data"]
  36. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  37. err = o.Raw(sql, sourceEdbInfoId).Scan(&count).Error
  38. return
  39. }
  40. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  41. o := global.DmSQL["data"]
  42. err = o.Create(item).Error
  43. return
  44. }
  45. func AddPredictEdb(item *EdbInfo, predictEdbConf *PredictEdbConf) (err error) {
  46. o := global.DmSQL["data"]
  47. tx := o.Begin()
  48. defer func() {
  49. if err != nil {
  50. tmpErr := tx.Rollback().Error
  51. if tmpErr != nil {
  52. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  53. }
  54. } else {
  55. _ = tx.Commit()
  56. }
  57. }()
  58. err = o.Create(item).Error
  59. if err != nil {
  60. return
  61. }
  62. predictEdbConf.PredictEdbInfoId = item.EdbInfoId
  63. err = o.Create(predictEdbConf).Error
  64. if err != nil {
  65. return
  66. }
  67. return
  68. }
  69. func EditPredictEdb(edbInfo *EdbInfo, predictEdbConf *PredictEdbConf, updateEdbInfoCol, updatePredictEdbConfCol []string) (err error) {
  70. o := global.DmSQL["data"]
  71. tx := o.Begin()
  72. defer func() {
  73. if err != nil {
  74. tmpErr := tx.Rollback().Error
  75. if tmpErr != nil {
  76. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  77. }
  78. } else {
  79. _ = tx.Commit()
  80. }
  81. }()
  82. err = o.Model(edbInfo).Select(updateEdbInfoCol).Updates(edbInfo).Error
  83. if err != nil {
  84. return
  85. }
  86. err = o.Model(predictEdbConf).Select(updatePredictEdbConfCol).Updates(predictEdbConf).Error
  87. if err != nil {
  88. return
  89. }
  90. return
  91. }
  92. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  93. num := len(edbInfoIdList)
  94. if num <= 0 {
  95. return
  96. }
  97. o := global.DmSQL["data"]
  98. sql := ` SELECT b.* FROM predict_edb_conf AS a
  99. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  100. WHERE a.predict_edb_info_id in (` + utils.GetGormInReplace(num) + `)
  101. GROUP BY a.source_edb_info_id
  102. ORDER BY a.source_edb_info_id ASC `
  103. err = o.Raw(sql, edbInfoIdList).Scan(&list).Error
  104. return
  105. }
  106. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  107. o := global.DmSQL["data"]
  108. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  109. err = o.Raw(sql, edbInfoId).Scan(&items).Error
  110. return
  111. }