predict_edb_conf.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_edb_lib/services/alarm_msg"
  5. "hongze/hongze_edb_lib/utils"
  6. "time"
  7. )
  8. // AddPredictEdbInfoReq 添加预测指标请求
  9. type AddPredictEdbInfoReq struct {
  10. EdbInfoId int `description:"指标ID"`
  11. ClassifyId int `description:"分类id"`
  12. AdminId int `description:"添加人id"`
  13. AdminName string `description:"添加人名称"`
  14. SourceEdbInfoId int `description:"来源指标id"`
  15. EdbName string `description:"指标名称"`
  16. RuleList []RuleConfig `description:"配置规则列表"`
  17. }
  18. // RuleConfig 预测规则配置
  19. type RuleConfig struct {
  20. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  21. Value string `description:"值/计算公式"`
  22. EndDate string `description:"截止日期"`
  23. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  24. }
  25. // EditPredictEdbInfoReq 编辑预测指标请求
  26. type EditPredictEdbInfoReq struct {
  27. EdbInfoId int `description:"指标ID"`
  28. ClassifyId int `description:"分类id"`
  29. EdbName string `description:"指标名称"`
  30. RuleList []RuleConfig `description:"配置规则列表"`
  31. }
  32. type PredictEdbConf struct {
  33. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  34. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  35. SourceEdbInfoId int `description:"来源指标id"`
  36. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值"`
  37. FixedValue float64 `description:"固定值"`
  38. Value string `description:"配置的值"`
  39. EndDate time.Time `description:"截止日期"`
  40. ModifyTime time.Time `description:"修改时间"`
  41. CreateTime time.Time `description:"添加时间"`
  42. }
  43. // PredictEdbConfAndData 预测规则和其对应的动态数据
  44. type PredictEdbConfAndData struct {
  45. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  46. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  47. SourceEdbInfoId int `description:"来源指标id"`
  48. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  49. FixedValue float64 `description:"固定值"`
  50. Value string `description:"配置的值"`
  51. EndDate time.Time `description:"截止日期"`
  52. ModifyTime time.Time `description:"修改时间"`
  53. CreateTime time.Time `description:"添加时间"`
  54. DataList []*EdbInfoSearchData `description:"动态数据"`
  55. }
  56. // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
  57. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  58. o := orm.NewOrm()
  59. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  60. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  61. return
  62. }
  63. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  64. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  65. o := orm.NewOrm()
  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. // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
  71. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  72. o := orm.NewOrm()
  73. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  74. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  75. return
  76. }
  77. // GetPredictEdbConfAndDataListById 根据预测指标id获取预测指标配置信息列表
  78. func GetPredictEdbConfAndDataListById(edbInfoId int) (items []*PredictEdbConfAndData, err error) {
  79. o := orm.NewOrm()
  80. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  81. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  82. return
  83. }
  84. // GetPredictEdbConfListByConfigIdList 根据预测指标id列表获取预测指标配置信息列表
  85. func GetPredictEdbConfListByConfigIdList(configIdList []int) (items []*PredictEdbConf, err error) {
  86. num := len(configIdList)
  87. if num <= 0 {
  88. return
  89. }
  90. o := orm.NewOrm()
  91. sql := ` SELECT * FROM predict_edb_conf WHERE config_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY config_id ASC`
  92. _, err = o.Raw(sql, configIdList).QueryRows(&items)
  93. return
  94. }
  95. // ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId 根据来源指标修改预测指标的最新数据信息
  96. func ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(sourceEdbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  97. o := orm.NewOrm()
  98. var list []*PredictEdbConf
  99. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  100. total, err := o.Raw(sql, sourceEdbInfoId).QueryRows(&list)
  101. if err != nil {
  102. return
  103. }
  104. if total > 0 {
  105. idList := make([]int, 0)
  106. for _, v := range list {
  107. idList = append(idList, v.PredictEdbInfoId)
  108. }
  109. sql := ` UPDATE edb_info SET start_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id in (` + utils.GetOrmInReplace(int(total)) + `) `
  110. _, err = o.Raw(sql, item.MinDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, idList).Exec()
  111. }
  112. return
  113. }
  114. // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
  115. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  116. o := orm.NewOrm()
  117. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  118. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
  119. return
  120. }
  121. // AddPredictEdbConf 添加预测指标规则
  122. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  123. o := orm.NewOrm()
  124. lastId, err = o.Insert(item)
  125. return
  126. }
  127. // AddPredictEdb 添加预测指标
  128. // edbInfo, calculateMappingList, predictEdbConfList,calculateRule9List,trendsMappingList
  129. func AddPredictEdb(item *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error) {
  130. o := orm.NewOrm()
  131. tx, err := o.Begin()
  132. if err != nil {
  133. return
  134. }
  135. defer func() {
  136. if err != nil {
  137. tmpErr := tx.Rollback()
  138. if tmpErr != nil {
  139. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  140. }
  141. } else {
  142. err = tx.Commit()
  143. }
  144. }()
  145. // 新增预测指标
  146. edbInfoId, err := tx.Insert(item)
  147. if err != nil {
  148. return
  149. }
  150. item.EdbInfoId = int(edbInfoId)
  151. // 新增预测指标的关联关系
  152. lenCalculateMapping := len(calculateMappingList)
  153. if lenCalculateMapping > 0 {
  154. for _, calculateMappingItem := range calculateMappingList {
  155. calculateMappingItem.EdbInfoId = item.EdbInfoId
  156. }
  157. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  158. if err != nil {
  159. return
  160. }
  161. }
  162. predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
  163. // 新增预测指标配置
  164. for k, v := range predictEdbConfList {
  165. v.PredictEdbInfoId = item.EdbInfoId
  166. configId, tmpErr := tx.Insert(v)
  167. if tmpErr != nil {
  168. err = tmpErr
  169. return
  170. }
  171. v.ConfigId = int(configId)
  172. // 每次规则计算的时候,产生的临时数据
  173. resultDataList := make([]*EdbInfoSearchData, 0)
  174. switch v.RuleType {
  175. case 9: //动态环差规则
  176. calculateRule := calculateRuleMap[k]
  177. calculateRule.ConfigId = v.ConfigId
  178. calculateRule.EdbInfoId = v.PredictEdbInfoId
  179. // 指标与规则的动态数据生成入库
  180. resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
  181. if err != nil {
  182. return
  183. }
  184. // 规则与指标的关系入库
  185. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  186. if lenTrendsCalculateMapping > 0 {
  187. for _, vv := range calculateRule.TrendsCalculateMappingList {
  188. vv.EdbInfoId = item.EdbInfoId
  189. vv.ConfigId = v.ConfigId
  190. }
  191. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  192. if err != nil {
  193. return
  194. }
  195. }
  196. case 14: //14:根据 一元线性拟合 规则获取预测数据
  197. calculateRule := calculateRuleMap[k]
  198. calculateRule.ConfigId = v.ConfigId
  199. calculateRule.EdbInfoId = v.PredictEdbInfoId
  200. // 指标与规则的动态数据(拟合数据)生成入库
  201. err = CalculateByRuleByRuleLineNh(tx, *item, predictEdbConfAndDataList, *v)
  202. if err != nil {
  203. return
  204. }
  205. // 规则与指标的关系入库
  206. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  207. if lenTrendsCalculateMapping > 0 {
  208. for _, vv := range calculateRule.TrendsCalculateMappingList {
  209. vv.EdbInfoId = item.EdbInfoId
  210. vv.ConfigId = v.ConfigId
  211. }
  212. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  213. if err != nil {
  214. return
  215. }
  216. }
  217. }
  218. // 规则配置(含数据)
  219. tmpPredictEdbConfAndData := &PredictEdbConfAndData{
  220. ConfigId: 0,
  221. PredictEdbInfoId: 0,
  222. SourceEdbInfoId: v.SourceEdbInfoId,
  223. RuleType: v.RuleType,
  224. FixedValue: v.FixedValue,
  225. Value: v.Value,
  226. EndDate: v.EndDate,
  227. ModifyTime: v.ModifyTime,
  228. CreateTime: v.CreateTime,
  229. DataList: resultDataList,
  230. }
  231. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  232. }
  233. return
  234. }
  235. // EditPredictEdb 修改预测指标
  236. func EditPredictEdb(edbInfo *EdbInfo, updateEdbInfoCol []string, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error) {
  237. o := orm.NewOrm()
  238. tx, err := o.Begin()
  239. if err != nil {
  240. return
  241. }
  242. defer func() {
  243. if err != nil {
  244. tmpErr := tx.Rollback()
  245. if tmpErr != nil {
  246. go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
  247. }
  248. } else {
  249. err = tx.Commit()
  250. }
  251. }()
  252. // 修改预测指标
  253. _, err = tx.Update(edbInfo, updateEdbInfoCol...)
  254. if err != nil {
  255. return
  256. }
  257. // 先删除原有的预测指标 与 其他指标的 关联关系
  258. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ?`
  259. _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
  260. if err != nil {
  261. return
  262. }
  263. // 先删除原有的配置
  264. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id = ?`
  265. _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
  266. if err != nil {
  267. return
  268. }
  269. // 删除基础预测指标 规则配置 与 其他指标的 关联关系
  270. sql = ` DELETE FROM predict_edb_conf_calculate_mapping WHERE edb_info_id = ?`
  271. _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
  272. if err != nil {
  273. return
  274. }
  275. // 删除基础预测指标 规则配置 生成的动态数据值
  276. sql = ` DELETE FROM predict_edb_rule_data WHERE edb_info_id = ?`
  277. _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
  278. if err != nil {
  279. return
  280. }
  281. // 新增预测指标的关联关系
  282. lenCalculateMapping := len(calculateMappingList)
  283. if lenCalculateMapping > 0 {
  284. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  285. if err != nil {
  286. return
  287. }
  288. }
  289. calculateRuleIndex := 0 // 预测计算规则下标
  290. predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
  291. // 新增预测指标配置
  292. for confIndex, v := range predictEdbConfList {
  293. configId, tmpErr := tx.Insert(v)
  294. if tmpErr != nil {
  295. err = tmpErr
  296. return
  297. }
  298. v.ConfigId = int(configId)
  299. // 每次规则计算的时候,产生的临时数据
  300. resultDataList := make([]*EdbInfoSearchData, 0)
  301. switch v.RuleType {
  302. case 9: //动态环差规则
  303. calculateRule := calculateRuleMap[confIndex]
  304. calculateRule.ConfigId = v.ConfigId
  305. calculateRule.EdbInfoId = v.PredictEdbInfoId
  306. // 指标与规则的动态数据生成入库
  307. resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
  308. if err != nil {
  309. return
  310. }
  311. // 规则与指标的关系入库
  312. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  313. if lenTrendsCalculateMapping > 0 {
  314. for _, vv := range calculateRule.TrendsCalculateMappingList {
  315. vv.ConfigId = v.ConfigId
  316. }
  317. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  318. if err != nil {
  319. return
  320. }
  321. }
  322. case 14: //14:根据 一元线性拟合 规则获取预测数据
  323. calculateRule := calculateRuleMap[confIndex]
  324. calculateRule.ConfigId = v.ConfigId
  325. calculateRule.EdbInfoId = v.PredictEdbInfoId
  326. // 指标与规则的动态数据(拟合数据)生成入库
  327. err = CalculateByRuleByRuleLineNh(tx, *edbInfo, predictEdbConfAndDataList, *v)
  328. if err != nil {
  329. return
  330. }
  331. // 规则与指标的关系入库
  332. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  333. if lenTrendsCalculateMapping > 0 {
  334. for _, vv := range calculateRule.TrendsCalculateMappingList {
  335. vv.ConfigId = v.ConfigId
  336. }
  337. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  338. if err != nil {
  339. return
  340. }
  341. }
  342. }
  343. calculateRuleIndex++
  344. // 规则配置(含数据)
  345. tmpPredictEdbConfAndData := &PredictEdbConfAndData{
  346. ConfigId: 0,
  347. PredictEdbInfoId: 0,
  348. SourceEdbInfoId: v.SourceEdbInfoId,
  349. RuleType: v.RuleType,
  350. FixedValue: v.FixedValue,
  351. Value: v.Value,
  352. EndDate: v.EndDate,
  353. ModifyTime: v.ModifyTime,
  354. CreateTime: v.CreateTime,
  355. DataList: resultDataList,
  356. }
  357. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  358. }
  359. return
  360. }
  361. // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
  362. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  363. num := len(edbInfoIdList)
  364. if num <= 0 {
  365. return
  366. }
  367. o := orm.NewOrm()
  368. sql := ` SELECT b.* FROM predict_edb_conf AS a
  369. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  370. WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  371. GROUP BY a.source_edb_info_id
  372. ORDER BY a.source_edb_info_id ASC `
  373. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  374. return
  375. }