predict_edb_conf.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/utils"
  6. "time"
  7. )
  8. type PredictEdbConf struct {
  9. ConfigId int `orm:"column(config_id);pk" gorm:"primaryKey" 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" 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 string `description:"截止日期"`
  32. ModifyTime string `description:"修改时间"`
  33. CreateTime string `description:"添加时间"`
  34. CalculateList []*PredictEdbConfCalculateMappingDetail `description:"配置与指标的关联信息"`
  35. }
  36. // PredictEdbConfAndData 预测规则和其对应的动态数据
  37. type PredictEdbConfAndData struct {
  38. ConfigId int `orm:"column(config_id);pk" gorm:"primaryKey" 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. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  50. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  51. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  52. err = global.DmSQL["data"].Raw(sql, sourceEdbInfoId).Scan(&count).Error
  53. return
  54. }
  55. // AddPredictEdb 添加预测指标
  56. func AddPredictEdb(item *EdbInfo, calculateMappingItem *EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf) (err error) {
  57. to := global.DmSQL["data"].Begin()
  58. defer func() {
  59. if err != nil {
  60. _ = to.Rollback()
  61. } else {
  62. _ = to.Commit()
  63. }
  64. }()
  65. // 新增预测指标
  66. err = to.Create(item).Error
  67. if err != nil {
  68. return
  69. }
  70. // 新增预测指标
  71. calculateMappingItem.EdbInfoId = item.EdbInfoId
  72. err = to.Create(calculateMappingItem).Error
  73. if err != nil {
  74. return
  75. }
  76. // 新增预测指标配置
  77. lenPredictEdbConf := len(predictEdbConfList)
  78. if lenPredictEdbConf > 0 {
  79. for _, v := range predictEdbConfList {
  80. v.PredictEdbInfoId = item.EdbInfoId
  81. }
  82. err = to.CreateInBatches(predictEdbConfList, utils.MultiAddNum).Error
  83. if err != nil {
  84. return
  85. }
  86. }
  87. return
  88. }
  89. // EditPredictEdb 修改预测指标
  90. func EditPredictEdb(edbInfo *EdbInfo, predictEdbConfList []*PredictEdbConf, updateEdbInfoCol []string) (err error) {
  91. to := global.DmSQL["data"].Begin()
  92. defer func() {
  93. if err != nil {
  94. _ = to.Rollback()
  95. } else {
  96. _ = to.Commit()
  97. }
  98. }()
  99. // 修改预测指标
  100. err = to.Select(updateEdbInfoCol).Updates(edbInfo).Error
  101. if err != nil {
  102. return
  103. }
  104. // 先删除原有的配置
  105. sql := ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id = ?`
  106. err = to.Exec(sql, edbInfo.EdbInfoId).Error
  107. if err != nil {
  108. return
  109. }
  110. // 新增新有的预测指标配置
  111. lenPredictEdbConf := len(predictEdbConfList)
  112. if lenPredictEdbConf > 0 {
  113. err = to.CreateInBatches(predictEdbConfList, utils.MultiAddNum).Error
  114. if err != nil {
  115. return
  116. }
  117. }
  118. return
  119. }
  120. // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
  121. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  122. num := len(edbInfoIdList)
  123. if num <= 0 {
  124. return
  125. }
  126. sql := ` SELECT DISTINCT b.* FROM predict_edb_conf AS a
  127. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  128. WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  129. ORDER BY b.edb_info_id ASC `
  130. err = global.DmSQL["data"].Raw(sql, edbInfoIdList).Find(&list).Error
  131. return
  132. }
  133. // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
  134. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  135. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  136. err = global.DmSQL["data"].Raw(sql, edbInfoId).Find(&items).Error
  137. return
  138. }
  139. // GetGroupPredictEdbBySourceEdbInfoId 根据来源指标id获取配置
  140. func GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId int) (items []*EdbInfo, err error) {
  141. sql := ` SELECT a.* FROM edb_info AS a
  142. JOIN predict_edb_conf AS b on a.edb_info_id = b.predict_edb_info_id
  143. WHERE b.source_edb_info_id=? group by a.edb_info_id`
  144. err = global.DmSQL["data"].Raw(sql, sourceEdbInfoId).Find(&items).Error
  145. return
  146. }
  147. // GetGroupPredictEdbInfoIdListBySourceEdbInfoId 根据来源指标id获取配置
  148. func GetGroupPredictEdbInfoIdListBySourceEdbInfoId(sourceEdbInfoId int) (items []int, err error) {
  149. sql := ` SELECT a.edb_info_id FROM edb_info AS a
  150. JOIN predict_edb_conf AS b on a.edb_info_id = b.predict_edb_info_id
  151. WHERE b.source_edb_info_id=? group by a.edb_info_id`
  152. err = global.DmSQL["data"].Raw(sql, sourceEdbInfoId).Scan(&items).Error
  153. return
  154. }
  155. // GetCountRelationPredictEdbInfoListMappingByCondition
  156. // @Description: 根据条件获取关联预测指标数量
  157. // @author: Roc
  158. // @datetime 2024-12-26 10:13:47
  159. // @param condition string
  160. // @param pars []interface{}
  161. // @return total int
  162. // @return err error
  163. func GetCountRelationPredictEdbInfoListMappingByCondition(condition string, pars []interface{}) (total int, err error) {
  164. o := global.DmSQL["data"]
  165. sql := ` SELECT COUNT(1) total FROM predict_edb_conf AS a
  166. JOIN edb_info AS b on a.predict_edb_info_id = b.edb_info_id WHERE 1=1 `
  167. if condition != "" {
  168. sql += condition
  169. }
  170. var totalNull sql2.NullInt64
  171. err = o.Raw(sql, pars...).Scan(&totalNull).Error
  172. if err != nil {
  173. return
  174. }
  175. total = int(totalNull.Int64)
  176. return
  177. }
  178. // GetUserIdListRelationPredictEdbInfoListMappingByCondition
  179. // @Description: 根据条件获取关联预测指标的用户id列表
  180. // @author: Roc
  181. // @datetime 2024-12-26 10:13:47
  182. // @param condition string
  183. // @param pars []interface{}
  184. // @return total int
  185. // @return err error
  186. func GetUserIdListRelationPredictEdbInfoListMappingByCondition(condition string, pars []interface{}) (userIdList []int, err error) {
  187. o := global.DmSQL["data"]
  188. sql := ` SELECT b.sys_user_id FROM predict_edb_conf AS a
  189. JOIN edb_info AS b on a.predict_edb_info_id = b.edb_info_id WHERE 1=1 `
  190. if condition != "" {
  191. sql += condition
  192. }
  193. sql += ` GROUP BY b.sys_user_id `
  194. err = o.Raw(sql, pars...).Scan(&userIdList).Error
  195. return
  196. }