predict_edb_conf.go 8.2 KB

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