predict_edb_conf.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package data_manage
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_ETA_mobile_api/services/alarm_msg"
  5. "hongze/hongze_ETA_mobile_api/utils"
  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期段线性外推值,9:动态环差"`
  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. // PredictEdbConfDetail 预测规则 和 规则相关联的指标
  20. type PredictEdbConfDetail struct {
  21. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  22. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  23. SourceEdbInfoId int `description:"来源指标id"`
  24. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  25. FixedValue float64 `description:"固定值"`
  26. Value string `description:"配置的值"`
  27. EndDate time.Time `description:"截止日期"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. CreateTime time.Time `description:"添加时间"`
  30. CalculateList []*PredictEdbConfCalculateMappingDetail `description:"配置与指标的关联信息"`
  31. }
  32. // PredictEdbConfAndData 预测规则和其对应的动态数据
  33. type PredictEdbConfAndData struct {
  34. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  35. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  36. SourceEdbInfoId int `description:"来源指标id"`
  37. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  38. FixedValue float64 `description:"固定值"`
  39. Value string `description:"配置的值"`
  40. EndDate time.Time `description:"截止日期"`
  41. ModifyTime time.Time `description:"修改时间"`
  42. CreateTime time.Time `description:"添加时间"`
  43. DataList []*EdbDataList `description:"动态数据"`
  44. }
  45. // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
  46. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  47. o := orm.NewOrmUsingDB("data")
  48. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  49. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  50. return
  51. }
  52. // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
  53. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  54. o := orm.NewOrmUsingDB("data")
  55. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  56. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
  57. return
  58. }
  59. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  60. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  63. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&count)
  64. return
  65. }
  66. // AddPredictEdbConf 添加预测指标规则
  67. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. lastId, err = o.Insert(item)
  70. return
  71. }
  72. // AddPredictEdb 添加预测指标
  73. func AddPredictEdb(item *EdbInfo, calculateMappingItem *EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf) (err error) {
  74. o := orm.NewOrmUsingDB("data")
  75. tx, err := o.Begin()
  76. if err != nil {
  77. return
  78. }
  79. defer func() {
  80. if err != nil {
  81. tmpErr := tx.Rollback()
  82. if tmpErr != nil {
  83. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  84. }
  85. } else {
  86. err = tx.Commit()
  87. }
  88. }()
  89. // 新增预测指标
  90. edbInfoId, err := o.Insert(item)
  91. if err != nil {
  92. return
  93. }
  94. item.EdbInfoId = int(edbInfoId)
  95. // 新增预测指标
  96. calculateMappingItem.EdbInfoId = item.EdbInfoId
  97. _, err = o.Insert(calculateMappingItem)
  98. if err != nil {
  99. return
  100. }
  101. // 新增预测指标配置
  102. lenPredictEdbConf := len(predictEdbConfList)
  103. if lenPredictEdbConf > 0 {
  104. for _, v := range predictEdbConfList {
  105. v.PredictEdbInfoId = item.EdbInfoId
  106. }
  107. _, err = o.InsertMulti(lenPredictEdbConf, predictEdbConfList)
  108. if err != nil {
  109. return
  110. }
  111. }
  112. return
  113. }
  114. // EditPredictEdb 修改预测指标
  115. func EditPredictEdb(edbInfo *EdbInfo, predictEdbConfList []*PredictEdbConf, updateEdbInfoCol []string) (err error) {
  116. o := orm.NewOrmUsingDB("data")
  117. tx, err := o.Begin()
  118. if err != nil {
  119. return
  120. }
  121. defer func() {
  122. if err != nil {
  123. tmpErr := tx.Rollback()
  124. if tmpErr != nil {
  125. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  126. }
  127. } else {
  128. err = tx.Commit()
  129. }
  130. }()
  131. // 修改预测指标
  132. _, err = o.Update(edbInfo, updateEdbInfoCol...)
  133. if err != nil {
  134. return
  135. }
  136. // 先删除原有的配置
  137. sql := ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id = ?`
  138. _, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
  139. if err != nil {
  140. return
  141. }
  142. // 新增新有的预测指标配置
  143. lenPredictEdbConf := len(predictEdbConfList)
  144. if lenPredictEdbConf > 0 {
  145. _, err = o.InsertMulti(lenPredictEdbConf, predictEdbConfList)
  146. if err != nil {
  147. return
  148. }
  149. }
  150. return
  151. }
  152. // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
  153. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  154. num := len(edbInfoIdList)
  155. if num <= 0 {
  156. return
  157. }
  158. o := orm.NewOrmUsingDB("data")
  159. sql := ` SELECT b.* FROM predict_edb_conf AS a
  160. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  161. WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  162. GROUP BY a.source_edb_info_id
  163. ORDER BY a.source_edb_info_id ASC `
  164. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  165. return
  166. }
  167. // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
  168. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  169. o := orm.NewOrmUsingDB("data")
  170. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  171. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  172. return
  173. }