predict_edb_conf.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. // AddPredictEdbInfoReq 添加预测指标请求
  8. type AddPredictEdbInfoReq struct {
  9. EdbInfoId int `description:"指标ID"`
  10. ClassifyId int `description:"分类id"`
  11. AdminId int `description:"添加人id"`
  12. AdminName string `description:"添加人名称"`
  13. SourceEdbInfoId int `description:"来源指标id"`
  14. EdbName string `description:"指标名称"`
  15. RuleList []RuleConfig `description:"配置规则列表"`
  16. MaxValue float64 `description:"最大值"`
  17. MinValue float64 `description:"最小值"`
  18. DataDateType string `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. tx.Rollback()
  143. } else {
  144. err = tx.Commit()
  145. }
  146. }()
  147. // 新增预测指标
  148. edbInfoId, err := tx.Insert(item)
  149. if err != nil {
  150. return
  151. }
  152. item.EdbInfoId = int(edbInfoId)
  153. // 新增预测指标的关联关系
  154. lenCalculateMapping := len(calculateMappingList)
  155. if lenCalculateMapping > 0 {
  156. for _, calculateMappingItem := range calculateMappingList {
  157. calculateMappingItem.EdbInfoId = item.EdbInfoId
  158. calculateMappingItem.EdbCode = item.EdbCode
  159. }
  160. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  161. if err != nil {
  162. return
  163. }
  164. }
  165. predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
  166. // 新增预测指标配置
  167. for k, v := range predictEdbConfList {
  168. v.PredictEdbInfoId = item.EdbInfoId
  169. configId, tmpErr := tx.Insert(v)
  170. if tmpErr != nil {
  171. err = tmpErr
  172. return
  173. }
  174. v.ConfigId = int(configId)
  175. // 每次规则计算的时候,产生的临时数据
  176. resultDataList := make([]*EdbInfoSearchData, 0)
  177. switch v.RuleType {
  178. case 9: //动态环差规则
  179. calculateRule := calculateRuleMap[k]
  180. calculateRule.ConfigId = v.ConfigId
  181. calculateRule.EdbInfoId = v.PredictEdbInfoId
  182. // 指标与规则的动态数据生成入库
  183. resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
  184. if err != nil {
  185. return
  186. }
  187. // 规则与指标的关系入库
  188. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  189. if lenTrendsCalculateMapping > 0 {
  190. for _, vv := range calculateRule.TrendsCalculateMappingList {
  191. vv.EdbInfoId = item.EdbInfoId
  192. vv.ConfigId = v.ConfigId
  193. }
  194. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  195. if err != nil {
  196. return
  197. }
  198. }
  199. case 14: //14:根据 一元线性拟合 规则获取预测数据
  200. calculateRule := calculateRuleMap[k]
  201. calculateRule.ConfigId = v.ConfigId
  202. calculateRule.EdbInfoId = v.PredictEdbInfoId
  203. // 指标与规则的动态数据(拟合数据)生成入库
  204. err, errMsg = CalculateByRuleByRuleLineNh(tx, *item, predictEdbConfAndDataList, *v)
  205. if err != nil {
  206. return
  207. }
  208. // 规则与指标的关系入库
  209. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  210. if lenTrendsCalculateMapping > 0 {
  211. for _, vv := range calculateRule.TrendsCalculateMappingList {
  212. vv.EdbInfoId = item.EdbInfoId
  213. vv.ConfigId = v.ConfigId
  214. }
  215. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  216. if err != nil {
  217. return
  218. }
  219. }
  220. }
  221. // 规则配置(含数据)
  222. tmpPredictEdbConfAndData := &PredictEdbConfAndData{
  223. ConfigId: 0,
  224. PredictEdbInfoId: 0,
  225. SourceEdbInfoId: v.SourceEdbInfoId,
  226. RuleType: v.RuleType,
  227. FixedValue: v.FixedValue,
  228. Value: v.Value,
  229. EndDate: v.EndDate,
  230. ModifyTime: v.ModifyTime,
  231. CreateTime: v.CreateTime,
  232. DataList: resultDataList,
  233. }
  234. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  235. }
  236. return
  237. }
  238. // EditPredictEdb 修改预测指标
  239. func EditPredictEdb(edbInfo *EdbInfo, updateEdbInfoCol []string, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error, errMsg string) {
  240. o := orm.NewOrm()
  241. tx, err := o.Begin()
  242. if err != nil {
  243. return
  244. }
  245. defer func() {
  246. if err != nil {
  247. tx.Rollback()
  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. for _, calculateMappingItem := range calculateMappingList {
  285. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  286. calculateMappingItem.EdbCode = edbInfo.EdbCode
  287. }
  288. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  289. if err != nil {
  290. return
  291. }
  292. }
  293. calculateRuleIndex := 0 // 预测计算规则下标
  294. predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
  295. // 新增预测指标配置
  296. for confIndex, v := range predictEdbConfList {
  297. configId, tmpErr := tx.Insert(v)
  298. if tmpErr != nil {
  299. err = tmpErr
  300. return
  301. }
  302. v.ConfigId = int(configId)
  303. // 每次规则计算的时候,产生的临时数据
  304. resultDataList := make([]*EdbInfoSearchData, 0)
  305. switch v.RuleType {
  306. case 9: //动态环差规则
  307. calculateRule := calculateRuleMap[confIndex]
  308. calculateRule.ConfigId = v.ConfigId
  309. calculateRule.EdbInfoId = v.PredictEdbInfoId
  310. // 指标与规则的动态数据生成入库
  311. resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
  312. if err != nil {
  313. return
  314. }
  315. // 规则与指标的关系入库
  316. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  317. if lenTrendsCalculateMapping > 0 {
  318. for _, vv := range calculateRule.TrendsCalculateMappingList {
  319. vv.EdbInfoId = edbInfo.EdbInfoId
  320. vv.ConfigId = v.ConfigId
  321. }
  322. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  323. if err != nil {
  324. return
  325. }
  326. }
  327. case 14: //14:根据 一元线性拟合 规则获取预测数据
  328. calculateRule := calculateRuleMap[confIndex]
  329. calculateRule.ConfigId = v.ConfigId
  330. calculateRule.EdbInfoId = v.PredictEdbInfoId
  331. // 指标与规则的动态数据(拟合数据)生成入库
  332. err, errMsg = CalculateByRuleByRuleLineNh(tx, *edbInfo, predictEdbConfAndDataList, *v)
  333. if err != nil {
  334. return
  335. }
  336. // 规则与指标的关系入库
  337. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  338. if lenTrendsCalculateMapping > 0 {
  339. for _, vv := range calculateRule.TrendsCalculateMappingList {
  340. vv.EdbInfoId = edbInfo.EdbInfoId
  341. vv.ConfigId = v.ConfigId
  342. }
  343. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  344. if err != nil {
  345. return
  346. }
  347. }
  348. }
  349. calculateRuleIndex++
  350. // 规则配置(含数据)
  351. tmpPredictEdbConfAndData := &PredictEdbConfAndData{
  352. ConfigId: 0,
  353. PredictEdbInfoId: 0,
  354. SourceEdbInfoId: v.SourceEdbInfoId,
  355. RuleType: v.RuleType,
  356. FixedValue: v.FixedValue,
  357. Value: v.Value,
  358. EndDate: v.EndDate,
  359. ModifyTime: v.ModifyTime,
  360. CreateTime: v.CreateTime,
  361. DataList: resultDataList,
  362. }
  363. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  364. }
  365. return
  366. }
  367. // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
  368. func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
  369. num := len(edbInfoIdList)
  370. if num <= 0 {
  371. return
  372. }
  373. o := orm.NewOrm()
  374. sql := ` SELECT b.* FROM predict_edb_conf AS a
  375. INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
  376. WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  377. GROUP BY a.source_edb_info_id
  378. ORDER BY a.source_edb_info_id ASC `
  379. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  380. return
  381. }