predict_edb_conf.go 8.0 KB

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