predict_edb_conf.go 6.7 KB

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