predict_edb_conf.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  25. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  26. EndDate string `description:"截止日期"`
  27. EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
  28. }
  29. // EditPredictEdbInfoReq 编辑预测指标请求
  30. type EditPredictEdbInfoReq struct {
  31. EdbInfoId int `description:"指标ID"`
  32. ClassifyId int `description:"分类id"`
  33. EdbName string `description:"指标名称"`
  34. RuleList []RuleConfig `description:"配置规则列表"`
  35. }
  36. type PredictEdbConf struct {
  37. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  38. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  39. SourceEdbInfoId int `description:"来源指标id"`
  40. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值"`
  41. FixedValue float64 `description:"固定值"`
  42. Value string `description:"配置的值"`
  43. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  44. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  45. EndDate time.Time `description:"截止日期"`
  46. ModifyTime time.Time `description:"修改时间"`
  47. CreateTime time.Time `description:"添加时间"`
  48. }
  49. // PredictEdbConfAndData 预测规则和其对应的动态数据
  50. type PredictEdbConfAndData struct {
  51. ConfigId int `orm:"column(config_id);pk" description:"规则id"`
  52. PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
  53. SourceEdbInfoId int `description:"来源指标id"`
  54. RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
  55. FixedValue float64 `description:"固定值"`
  56. Value string `description:"配置的值"`
  57. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  58. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  59. EndDate time.Time `description:"截止日期"`
  60. ModifyTime time.Time `description:"修改时间"`
  61. CreateTime time.Time `description:"添加时间"`
  62. DataList []*EdbInfoSearchData `description:"动态数据"`
  63. }
  64. // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
  65. func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
  66. o := orm.NewOrm()
  67. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
  68. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  69. return
  70. }
  71. // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
  72. func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
  73. o := orm.NewOrm()
  74. sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
  75. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&count)
  76. return
  77. }
  78. // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
  79. func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
  80. o := orm.NewOrm()
  81. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  82. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  83. return
  84. }
  85. // GetPredictEdbConfAndDataListById 根据预测指标id获取预测指标配置信息列表
  86. func GetPredictEdbConfAndDataListById(edbInfoId int) (items []*PredictEdbConfAndData, err error) {
  87. o := orm.NewOrm()
  88. sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
  89. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  90. return
  91. }
  92. // GetPredictEdbConfListByConfigIdList 根据预测指标id列表获取预测指标配置信息列表
  93. func GetPredictEdbConfListByConfigIdList(configIdList []int) (items []*PredictEdbConf, err error) {
  94. num := len(configIdList)
  95. if num <= 0 {
  96. return
  97. }
  98. o := orm.NewOrm()
  99. sql := ` SELECT * FROM predict_edb_conf WHERE config_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY config_id ASC`
  100. _, err = o.Raw(sql, configIdList).QueryRows(&items)
  101. return
  102. }
  103. // ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId 根据来源指标修改预测指标的最新数据信息
  104. func ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(sourceEdbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  105. //return
  106. o := orm.NewOrm()
  107. var list []*PredictEdbConf
  108. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  109. total, err := o.Raw(sql, sourceEdbInfoId).QueryRows(&list)
  110. if err != nil {
  111. return
  112. }
  113. if total > 0 {
  114. idList := make([]int, 0)
  115. for _, v := range list {
  116. idList = append(idList, v.PredictEdbInfoId)
  117. }
  118. //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)) + `) `
  119. //_, err = o.Raw(sql, item.MinDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, idList).Exec()
  120. sql := ` UPDATE edb_info SET start_date=?,is_update=2,latest_date=?,latest_value=?,end_value=?,modify_time=NOW() WHERE edb_info_id in (` + utils.GetOrmInReplace(int(total)) + `) `
  121. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, item.LatestValue, idList).Exec()
  122. }
  123. return
  124. }
  125. // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
  126. func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
  127. o := orm.NewOrm()
  128. sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
  129. err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
  130. return
  131. }
  132. // AddPredictEdbConf 添加预测指标规则
  133. func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
  134. o := orm.NewOrm()
  135. lastId, err = o.Insert(item)
  136. return
  137. }
  138. // AddPredictEdb 添加预测指标
  139. // edbInfo, calculateMappingList, predictEdbConfList,calculateRule9List,trendsMappingList
  140. func AddPredictEdb(item *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error, errMsg string) {
  141. o := orm.NewOrm()
  142. tx, err := o.Begin()
  143. if err != nil {
  144. return
  145. }
  146. defer func() {
  147. if err != nil {
  148. tx.Rollback()
  149. } else {
  150. err = tx.Commit()
  151. }
  152. }()
  153. // 新增预测指标
  154. edbInfoId, err := tx.Insert(item)
  155. if err != nil {
  156. return
  157. }
  158. item.EdbInfoId = int(edbInfoId)
  159. // 新增预测指标的关联关系
  160. lenCalculateMapping := len(calculateMappingList)
  161. if lenCalculateMapping > 0 {
  162. for _, calculateMappingItem := range calculateMappingList {
  163. calculateMappingItem.EdbInfoId = item.EdbInfoId
  164. calculateMappingItem.EdbCode = item.EdbCode
  165. }
  166. _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
  167. if err != nil {
  168. return
  169. }
  170. }
  171. predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
  172. // 新增预测指标配置
  173. for k, v := range predictEdbConfList {
  174. v.PredictEdbInfoId = item.EdbInfoId
  175. configId, tmpErr := tx.Insert(v)
  176. if tmpErr != nil {
  177. err = tmpErr
  178. return
  179. }
  180. v.ConfigId = int(configId)
  181. // 每次规则计算的时候,产生的临时数据
  182. resultDataList := make([]*EdbInfoSearchData, 0)
  183. switch v.RuleType {
  184. case 9: //动态环差规则
  185. calculateRule := calculateRuleMap[k]
  186. calculateRule.ConfigId = v.ConfigId
  187. calculateRule.EdbInfoId = v.PredictEdbInfoId
  188. // 指标与规则的动态数据生成入库
  189. resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
  190. if err != nil {
  191. return
  192. }
  193. // 规则与指标的关系入库
  194. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  195. if lenTrendsCalculateMapping > 0 {
  196. for _, vv := range calculateRule.TrendsCalculateMappingList {
  197. vv.EdbInfoId = item.EdbInfoId
  198. vv.ConfigId = v.ConfigId
  199. }
  200. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  201. if err != nil {
  202. return
  203. }
  204. }
  205. case 14: //14:根据 一元线性拟合 规则获取预测数据
  206. calculateRule := calculateRuleMap[k]
  207. calculateRule.ConfigId = v.ConfigId
  208. calculateRule.EdbInfoId = v.PredictEdbInfoId
  209. // 指标与规则的动态数据(拟合数据)生成入库
  210. err, errMsg = CalculateByRuleByRuleLineNh(tx, *item, predictEdbConfAndDataList, *v)
  211. if err != nil {
  212. return
  213. }
  214. // 规则与指标的关系入库
  215. lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
  216. if lenTrendsCalculateMapping > 0 {
  217. for _, vv := range calculateRule.TrendsCalculateMappingList {
  218. vv.EdbInfoId = item.EdbInfoId
  219. vv.ConfigId = v.ConfigId
  220. }
  221. _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
  222. if err != nil {
  223. return
  224. }
  225. }
  226. }
  227. // 规则配置(含数据)
  228. tmpPredictEdbConfAndData := &PredictEdbConfAndData{
  229. ConfigId: 0,
  230. PredictEdbInfoId: 0,
  231. SourceEdbInfoId: v.SourceEdbInfoId,
  232. RuleType: v.RuleType,
  233. FixedValue: v.FixedValue,
  234. Value: v.Value,
  235. EndDate: v.EndDate,
  236. ModifyTime: v.ModifyTime,
  237. CreateTime: v.CreateTime,
  238. DataList: resultDataList,
  239. }
  240. predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
  241. }
  242. return
  243. }
  244. // EditPredictEdb 修改预测指标
  245. func EditPredictEdb(edbInfo *EdbInfo, updateEdbInfoCol []string, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error, errMsg string) {
  246. o := orm.NewOrm()
  247. tx, err := o.Begin()
  248. if err != nil {
  249. return
  250. }
  251. defer func() {
  252. if err != nil {
  253. tx.Rollback()
  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. }