predict_edb_conf.go 8.0 KB

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