predict_edb_conf.go 6.0 KB

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