predict_edb_conf.go 15 KB

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