factor_edb_series.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/models/data_manage/request"
  8. "eta_gn/eta_api/services/data"
  9. correlationServ "eta_gn/eta_api/services/data/correlation"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // FactorEdbSeriesController 因子指标系列
  17. type FactorEdbSeriesController struct {
  18. controllers.BaseAuthController
  19. }
  20. // CalculateFuncList
  21. // @Title 计算方式列表
  22. // @Description 计算方式列表
  23. // @Param EdbInfoType query int false "指标计算类型: 0-普通指标; 1-预测指标"
  24. // @Success Ret=200 操作成功
  25. // @router /factor_edb_series/calculate_func/list [get]
  26. func (this *FactorEdbSeriesController) CalculateFuncList() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. if br.ErrMsg == "" {
  30. br.IsSendEmail = false
  31. }
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. sysUser := this.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. br.Ret = 408
  40. return
  41. }
  42. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  43. funcOb := new(data_manage.FactorEdbSeriesCalculateFunc)
  44. cond := fmt.Sprintf(` AND %s = ?`, funcOb.Cols().EdbInfoType)
  45. pars := make([]interface{}, 0)
  46. pars = append(pars, edbInfoType)
  47. list, e := funcOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", funcOb.Cols().PrimaryId))
  48. if e != nil {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = fmt.Sprintf("获取计算方式列表失败, Err: %v", e)
  51. return
  52. }
  53. resp := make([]*data_manage.FactorEdbSeriesCalculateFuncItem, 0)
  54. for _, v := range list {
  55. resp = append(resp, v.Format2Item())
  56. }
  57. br.Data = resp
  58. br.Ret = 200
  59. br.Success = true
  60. br.Msg = "获取成功"
  61. }
  62. // Add
  63. // @Title 新增
  64. // @Description 新增
  65. // @Param request body request.AddFactorEdbSeriesReq true "type json string"
  66. // @Success Ret=200 操作成功
  67. // @router /factor_edb_series/add [post]
  68. func (this *FactorEdbSeriesController) Add() {
  69. br := new(models.BaseResponse).Init()
  70. defer func() {
  71. if br.ErrMsg == "" {
  72. br.IsSendEmail = false
  73. }
  74. this.Data["json"] = br
  75. this.ServeJSON()
  76. }()
  77. sysUser := this.SysUser
  78. if sysUser == nil {
  79. br.Msg = "请登录"
  80. br.ErrMsg = "请登录,SysUser Is Empty"
  81. br.Ret = 408
  82. return
  83. }
  84. var req request.AddFactorEdbSeriesReq
  85. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  86. br.Msg = "参数解析异常"
  87. br.ErrMsg = fmt.Sprintf("参数解析异常, Err: %v", e)
  88. return
  89. }
  90. req.SeriesName = strings.TrimSpace(req.SeriesName)
  91. if req.SeriesName == "" {
  92. br.Msg = "请输入指标系列名称"
  93. return
  94. }
  95. if len(req.EdbInfoIds) <= 0 {
  96. br.Msg = "请选择因子指标系列"
  97. return
  98. }
  99. if len(req.EdbInfoIds) > 100 {
  100. br.Msg = "添加指标总数量不得超过100"
  101. return
  102. }
  103. calculateLen := len(req.Calculates)
  104. if calculateLen > 5 {
  105. br.Msg = "计算公式不可超过5个"
  106. return
  107. }
  108. var calculatesJson string
  109. if calculateLen > 0 {
  110. b, e := json.Marshal(req.Calculates)
  111. if e != nil {
  112. br.Msg = "计算方式格式有误"
  113. br.ErrMsg = "解析计算方式参数失败, Err: " + e.Error()
  114. return
  115. }
  116. calculatesJson = string(b)
  117. for _, v := range req.Calculates {
  118. switch v.Source {
  119. case utils.EdbBaseCalculateNszydpjjs, utils.EdbBaseCalculateHbz, utils.EdbBaseCalculateHcz, utils.EdbBaseCalculateCjjx:
  120. if v.Formula == nil {
  121. br.Msg = "请输入N值"
  122. return
  123. }
  124. formula, ok := v.Formula.(string)
  125. if !ok {
  126. br.Msg = "N值格式有误"
  127. return
  128. }
  129. formulaInt, _ := strconv.Atoi(formula)
  130. if formulaInt <= 0 {
  131. br.Msg = "N值不可小于0, 重新输入"
  132. return
  133. }
  134. case utils.EdbBaseCalculateExponentialSmoothing:
  135. if v.Formula == nil {
  136. br.Msg = "请填写alpha值"
  137. return
  138. }
  139. formula, ok := v.Formula.(string)
  140. if !ok {
  141. br.Msg = "alpha值格式有误"
  142. return
  143. }
  144. alpha, _ := strconv.ParseFloat(formula, 64)
  145. if alpha <= 0 || alpha >= 1 {
  146. br.Msg = "alpha值应在0-1之间, 请重新输入"
  147. return
  148. }
  149. }
  150. }
  151. }
  152. edbArr, e := data_manage.GetEdbInfoByIdList(req.EdbInfoIds)
  153. if e != nil {
  154. br.Msg = "操作失败"
  155. br.ErrMsg = "获取指标列表失败, Err: " + e.Error()
  156. return
  157. }
  158. if len(edbArr) == 0 {
  159. br.Msg = "因子指标系列有误"
  160. br.ErrMsg = "因子指标系列长度为0"
  161. return
  162. }
  163. // 新增指标系列
  164. seriesItem := new(data_manage.FactorEdbSeries)
  165. seriesItem.SeriesName = req.SeriesName
  166. seriesItem.EdbInfoType = req.EdbInfoType
  167. seriesItem.CreateTime = time.Now().Local()
  168. seriesItem.ModifyTime = time.Now().Local()
  169. if calculateLen > 0 {
  170. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  171. seriesItem.CalculateStep = calculatesJson
  172. }
  173. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  174. for _, v := range edbArr {
  175. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  176. EdbInfoId: v.EdbInfoId,
  177. EdbCode: v.EdbCode,
  178. CreateTime: time.Now().Local(),
  179. ModifyTime: time.Now().Local(),
  180. })
  181. }
  182. seriesId, e := seriesItem.CreateSeriesAndMapping(seriesItem, mappings)
  183. if e != nil {
  184. br.Msg = "操作失败"
  185. br.ErrMsg = "新增因子指标系列失败, Err: " + e.Error()
  186. return
  187. }
  188. // 计算指标数据
  189. var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
  190. if calculateLen > 0 {
  191. calculateResp, e = data.FactorEdbStepCalculate(seriesId, edbArr, req.Calculates, this.Lang, false)
  192. if e != nil {
  193. br.Msg = "操作失败"
  194. br.ErrMsg = "计算因子指标失败, Err: " + e.Error()
  195. return
  196. }
  197. // 更新系列计算状态
  198. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  199. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  200. seriesItem.ModifyTime = time.Now().Local()
  201. if e = seriesItem.Update(cols); e != nil {
  202. br.Msg = "操作失败"
  203. br.ErrMsg = "更新因子指标系列计算状态失败, Err: " + e.Error()
  204. return
  205. }
  206. } else {
  207. for _, v := range edbArr {
  208. calculateResp.Success = append(calculateResp.Success, data_manage.FactorEdbSeriesStepCalculateResult{
  209. EdbInfoId: v.EdbInfoId,
  210. EdbCode: v.EdbCode,
  211. Msg: "保存成功",
  212. })
  213. }
  214. }
  215. calculateResp.SeriesId = seriesId
  216. br.Data = calculateResp
  217. br.Ret = 200
  218. br.Success = true
  219. br.Msg = "操作成功"
  220. br.IsAddLog = true
  221. }
  222. // Edit
  223. // @Title 编辑
  224. // @Description 编辑
  225. // @Param request body request.EditFactorEdbSeriesReq true "type json string"
  226. // @Success Ret=200 操作成功
  227. // @router /factor_edb_series/edit [post]
  228. func (this *FactorEdbSeriesController) Edit() {
  229. br := new(models.BaseResponse).Init()
  230. defer func() {
  231. if br.ErrMsg == "" {
  232. br.IsSendEmail = false
  233. }
  234. this.Data["json"] = br
  235. this.ServeJSON()
  236. }()
  237. sysUser := this.SysUser
  238. if sysUser == nil {
  239. br.Msg = "请登录"
  240. br.ErrMsg = "请登录,SysUser Is Empty"
  241. br.Ret = 408
  242. return
  243. }
  244. var req request.EditFactorEdbSeriesReq
  245. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  246. br.Msg = "参数解析异常"
  247. br.ErrMsg = fmt.Sprintf("参数解析异常, Err: %v", e)
  248. return
  249. }
  250. if req.SeriesId <= 0 {
  251. br.Msg = "参数有误"
  252. br.ErrMsg = fmt.Sprintf("参数有误, SeriesId: %d", req.SeriesId)
  253. return
  254. }
  255. req.SeriesName = strings.TrimSpace(req.SeriesName)
  256. if req.SeriesName == "" {
  257. br.Msg = "请输入指标系列名称"
  258. return
  259. }
  260. if len(req.EdbInfoIds) <= 0 {
  261. br.Msg = "请选择因子指标系列"
  262. return
  263. }
  264. if len(req.EdbInfoIds) > 100 {
  265. br.Msg = "添加指标总数量不得超过100"
  266. return
  267. }
  268. calculateLen := len(req.Calculates)
  269. if calculateLen > 5 {
  270. br.Msg = "计算公式不可超过5个"
  271. return
  272. }
  273. var calculatesJson string
  274. if calculateLen > 0 {
  275. b, e := json.Marshal(req.Calculates)
  276. if e != nil {
  277. br.Msg = "计算方式格式有误"
  278. br.ErrMsg = "解析计算方式参数失败, Err: " + e.Error()
  279. return
  280. }
  281. calculatesJson = string(b)
  282. for _, v := range req.Calculates {
  283. switch v.Source {
  284. case utils.EdbBaseCalculateNszydpjjs, utils.EdbBaseCalculateHbz, utils.EdbBaseCalculateHcz, utils.EdbBaseCalculateCjjx:
  285. if v.Formula == nil {
  286. br.Msg = "请输入N值"
  287. return
  288. }
  289. formula, ok := v.Formula.(string)
  290. if !ok {
  291. br.Msg = "N值格式有误"
  292. return
  293. }
  294. formulaInt, _ := strconv.Atoi(formula)
  295. if formulaInt <= 0 {
  296. br.Msg = "N值不可小于0, 重新输入"
  297. return
  298. }
  299. case utils.EdbBaseCalculateExponentialSmoothing:
  300. if v.Formula == nil {
  301. br.Msg = "请填写alpha值"
  302. return
  303. }
  304. formula, ok := v.Formula.(string)
  305. if !ok {
  306. br.Msg = "alpha值格式有误"
  307. return
  308. }
  309. alpha, _ := strconv.ParseFloat(formula, 64)
  310. if alpha <= 0 || alpha >= 1 {
  311. br.Msg = "alpha值应在0-1之间, 请重新输入"
  312. return
  313. }
  314. }
  315. }
  316. }
  317. seriesOb := new(data_manage.FactorEdbSeries)
  318. seriesItem, e := seriesOb.GetItemById(req.SeriesId)
  319. if e != nil {
  320. if utils.IsErrNoRow(e) {
  321. br.Msg = "该因子指标系列不存在"
  322. return
  323. }
  324. br.Msg = "获取失败"
  325. br.ErrMsg = "获取因子指标系列失败, Err: " + e.Error()
  326. return
  327. }
  328. originCalculateState := seriesItem.CalculateState
  329. edbArr, e := data_manage.GetEdbInfoByIdList(req.EdbInfoIds)
  330. if e != nil {
  331. br.Msg = "操作失败"
  332. br.ErrMsg = "获取指标列表失败, Err: " + e.Error()
  333. return
  334. }
  335. if len(edbArr) == 0 {
  336. br.Msg = "因子指标系列有误"
  337. br.ErrMsg = "因子指标系列长度为0"
  338. return
  339. }
  340. var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
  341. calculateResp.SeriesId = seriesItem.FactorEdbSeriesId
  342. // 如果不需要进行重新计算(比如只改了系列名称)那么只更新指标系列
  343. seriesItem.SeriesName = req.SeriesName
  344. seriesItem.EdbInfoType = req.EdbInfoType
  345. seriesItem.ModifyTime = time.Now().Local()
  346. updateCols := []string{seriesOb.Cols().SeriesName, seriesOb.Cols().EdbInfoType, seriesOb.Cols().ModifyTime}
  347. if !req.Recalculate {
  348. if e = seriesItem.Update(updateCols); e != nil {
  349. br.Msg = "操作成功"
  350. br.ErrMsg = "更新因子指标系列信息失败, Err: " + e.Error()
  351. return
  352. }
  353. for _, v := range edbArr {
  354. calculateResp.Success = append(calculateResp.Success, data_manage.FactorEdbSeriesStepCalculateResult{
  355. EdbInfoId: v.EdbInfoId,
  356. EdbCode: v.EdbCode,
  357. Msg: "保存成功",
  358. })
  359. }
  360. br.Data = calculateResp
  361. br.Ret = 200
  362. br.Success = true
  363. br.Msg = "操作成功"
  364. return
  365. }
  366. // 更新系列信息和指标关联
  367. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculateNone
  368. if calculateLen > 0 {
  369. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  370. seriesItem.CalculateStep = calculatesJson
  371. updateCols = append(updateCols, seriesOb.Cols().CalculateState, seriesOb.Cols().CalculateStep)
  372. }
  373. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  374. for _, v := range edbArr {
  375. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  376. EdbInfoId: v.EdbInfoId,
  377. EdbCode: v.EdbCode,
  378. CreateTime: time.Now().Local(),
  379. ModifyTime: time.Now().Local(),
  380. })
  381. }
  382. if e = seriesItem.EditSeriesAndMapping(seriesItem, mappings, updateCols); e != nil {
  383. br.Msg = "操作失败"
  384. br.ErrMsg = "编辑因子指标系列失败, Err: " + e.Error()
  385. return
  386. }
  387. // 重新计算
  388. calculateResp, e = data.FactorEdbStepCalculate(seriesItem.FactorEdbSeriesId, edbArr, req.Calculates, this.Lang, req.Recalculate)
  389. if e != nil {
  390. br.Msg = "操作失败"
  391. br.ErrMsg = "计算因子指标失败, Err: " + e.Error()
  392. return
  393. }
  394. if seriesItem.CalculateState == data_manage.FactorEdbSeriesCalculating {
  395. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  396. }
  397. // 若原状态不一致, 更新状态
  398. if originCalculateState != seriesItem.CalculateState {
  399. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  400. seriesItem.ModifyTime = time.Now().Local()
  401. if e = seriesItem.Update(cols); e != nil {
  402. br.Msg = "操作失败"
  403. br.ErrMsg = "更新因子指标系列计算状态失败, Err: " + e.Error()
  404. return
  405. }
  406. }
  407. br.Data = calculateResp
  408. br.Ret = 200
  409. br.Success = true
  410. br.Msg = "操作成功"
  411. br.IsAddLog = true
  412. }
  413. // Detail
  414. // @Title 详情
  415. // @Description 详情
  416. // @Param SeriesId query int false "多因子指标系列ID"
  417. // @Success 200 {object} data_manage.FactorEdbSeriesDetail
  418. // @router /factor_edb_series/detail [get]
  419. func (this *FactorEdbSeriesController) Detail() {
  420. br := new(models.BaseResponse).Init()
  421. defer func() {
  422. if br.ErrMsg == "" {
  423. br.IsSendEmail = false
  424. }
  425. this.Data["json"] = br
  426. this.ServeJSON()
  427. }()
  428. sysUser := this.SysUser
  429. if sysUser == nil {
  430. br.Msg = "请登录"
  431. br.ErrMsg = "请登录,SysUser Is Empty"
  432. br.Ret = 408
  433. return
  434. }
  435. seriesId, _ := this.GetInt("SeriesId", 0)
  436. if seriesId <= 0 {
  437. br.Msg = "参数有误"
  438. br.ErrMsg = fmt.Sprintf("参数有误, SeriesId: %d", seriesId)
  439. return
  440. }
  441. seriesOb := new(data_manage.FactorEdbSeries)
  442. series, e := seriesOb.GetItemById(seriesId)
  443. if e != nil {
  444. if utils.IsErrNoRow(e) {
  445. br.Msg = "该因子指标系列不存在"
  446. return
  447. }
  448. br.Msg = "获取失败"
  449. br.ErrMsg = "获取因子指标系列失败, Err: " + e.Error()
  450. return
  451. }
  452. mappingOb := new(data_manage.FactorEdbSeriesMapping)
  453. cond := fmt.Sprintf(" AND %s = ?", mappingOb.Cols().FactorEdbSeriesId)
  454. pars := make([]interface{}, 0)
  455. pars = append(pars, seriesId)
  456. mappings, e := mappingOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", mappingOb.Cols().CreateTime))
  457. if e != nil {
  458. br.Msg = "获取失败"
  459. br.ErrMsg = "获取因子指标系列关联失败, Err: " + e.Error()
  460. return
  461. }
  462. resp := new(data_manage.FactorEdbSeriesDetail)
  463. resp.FactorEdbSeriesItem = series.Format2Item()
  464. for _, m := range mappings {
  465. resp.EdbMappings = append(resp.EdbMappings, m.Format2Item())
  466. }
  467. br.Data = resp
  468. br.Ret = 200
  469. br.Success = true
  470. br.Msg = "获取成功"
  471. }
  472. // CorrelationMatrix
  473. // @Title 因子指标系列-相关性矩阵
  474. // @Description 因子指标系列-相关性矩阵
  475. // @Param request body request.FactorEdbSeriesCorrelationMatrixReq true "type json string"
  476. // @Success 200 {object} data_manage.FactorEdbSeriesCorrelationMatrixItem
  477. // @router /factor_edb_series/correlation/matrix [post]
  478. func (this *FactorEdbSeriesController) CorrelationMatrix() {
  479. br := new(models.BaseResponse).Init()
  480. defer func() {
  481. if br.ErrMsg == "" {
  482. br.IsSendEmail = false
  483. }
  484. this.Data["json"] = br
  485. this.ServeJSON()
  486. }()
  487. sysUser := this.SysUser
  488. if sysUser == nil {
  489. br.Msg = "请登录"
  490. br.ErrMsg = "请登录,SysUser Is Empty"
  491. br.Ret = 408
  492. return
  493. }
  494. var req request.FactorEdbSeriesCorrelationMatrixReq
  495. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  496. br.Msg = "参数解析异常"
  497. br.ErrMsg = fmt.Sprintf("参数解析异常, Err: %v", e)
  498. return
  499. }
  500. if req.BaseEdbInfoId <= 0 {
  501. br.Msg = "请选择标的指标"
  502. return
  503. }
  504. if len(req.SeriesIds) == 0 {
  505. br.Msg = "请选择因子指标系列"
  506. return
  507. }
  508. if req.Correlation.LeadValue <= 0 {
  509. br.Msg = "分析周期不允许设置为负数或0"
  510. return
  511. }
  512. if req.Correlation.LeadUnit == "" {
  513. br.Msg = "请选择分析周期频度"
  514. return
  515. }
  516. leadUnitDays, ok := utils.FrequencyDaysMap[req.Correlation.LeadUnit]
  517. if !ok {
  518. br.Msg = "错误的分析周期频度"
  519. br.ErrMsg = fmt.Sprintf("分析周期频度有误: %s", req.Correlation.LeadUnit)
  520. return
  521. }
  522. if req.Correlation.CalculateUnit == "" {
  523. br.Msg = "请选择计算窗口频度"
  524. return
  525. }
  526. calculateUnitDays, ok := utils.FrequencyDaysMap[req.Correlation.CalculateUnit]
  527. if !ok {
  528. br.Msg = "错误的计算窗口频度"
  529. br.ErrMsg = fmt.Sprintf("计算窗口频度有误: %s", req.Correlation.CalculateUnit)
  530. return
  531. }
  532. leadDays := 2 * req.Correlation.LeadValue * leadUnitDays
  533. calculateDays := req.Correlation.CalculateValue * calculateUnitDays
  534. if calculateDays < leadDays {
  535. br.Msg = "计算窗口必须≥2*分析周期"
  536. return
  537. }
  538. // 矩阵计算
  539. var calculatePars data_manage.CalculateCorrelationMatrixPars
  540. calculatePars.BaseEdbInfoId = req.BaseEdbInfoId
  541. calculatePars.SeriesIds = req.SeriesIds
  542. calculatePars.Correlation = req.Correlation
  543. resp, _, e := correlationServ.CalculateCorrelationMatrix(calculatePars)
  544. if e != nil {
  545. br.Msg = "计算失败"
  546. br.ErrMsg = fmt.Sprintf("计算相关性矩阵失败, %v", e)
  547. return
  548. }
  549. // 获取标的指标信息及数据
  550. //baseEdb, e := data_manage.GetEdbInfoById(req.BaseEdbInfoId)
  551. //if e != nil {
  552. // if utils.IsErrNoRow(e) {
  553. // br.Msg = "标的指标不存在"
  554. // return
  555. // }
  556. // br.Msg = "获取失败"
  557. // br.ErrMsg = "获取标的指标信息失败, Err: " + e.Error()
  558. // return
  559. //}
  560. //dataListA := make([]*data_manage.EdbDataList, 0)
  561. //{
  562. // // 标的指标数据日期区间
  563. // startDate := time.Now().AddDate(0, 0, -calculateDays).Format(utils.FormatDate)
  564. // endDate := time.Now().Format(utils.FormatDate)
  565. // startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  566. // startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate) // 不包含第一天
  567. // switch baseEdb.EdbInfoType {
  568. // case 0:
  569. // dataListA, e = data_manage.GetEdbDataList(baseEdb.Source, baseEdb.SubSource, baseEdb.EdbInfoId, startDate, endDate)
  570. // case 1:
  571. // _, dataListA, _, _, e, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdb.EdbInfoId, startDate, endDate, false)
  572. // default:
  573. // br.Msg = "获取失败"
  574. // br.ErrMsg = fmt.Sprintf("标的指标类型异常: %d", baseEdb.EdbInfoType)
  575. // return
  576. // }
  577. // if e != nil {
  578. // br.Msg = "获取失败"
  579. // br.ErrMsg = "获取标的指标数据失败, Err:" + e.Error()
  580. // return
  581. // }
  582. //}
  583. //
  584. //// 获取因子系列
  585. //seriesIdItem := make(map[int]*data_manage.FactorEdbSeries)
  586. //{
  587. // ob := new(data_manage.FactorEdbSeries)
  588. // cond := fmt.Sprintf(" AND %s IN (%s)", ob.Cols().PrimaryId, utils.GetOrmInReplace(len(req.SeriesIds)))
  589. // pars := make([]interface{}, 0)
  590. // pars = append(pars, req.SeriesIds)
  591. // items, e := ob.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", ob.Cols().PrimaryId))
  592. // if e != nil {
  593. // br.Msg = "获取失败"
  594. // br.ErrMsg = "获取因子指标系列失败, Err: " + e.Error()
  595. // return
  596. // }
  597. // if len(items) != len(req.SeriesIds) {
  598. // br.Msg = "获取失败"
  599. // br.ErrMsg = "因子指标系列数量有误"
  600. // return
  601. // }
  602. // for _, v := range items {
  603. // seriesIdItem[v.FactorEdbSeriesId] = v
  604. // }
  605. //}
  606. //
  607. //// 获取因子指标
  608. //edbMappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  609. //edbInfoIds := make([]int, 0)
  610. //{
  611. // ob := new(data_manage.FactorEdbSeriesMapping)
  612. // cond := fmt.Sprintf(" AND %s IN (%s)", ob.Cols().FactorEdbSeriesId, utils.GetOrmInReplace(len(req.SeriesIds)))
  613. // pars := make([]interface{}, 0)
  614. // pars = append(pars, req.SeriesIds)
  615. // order := fmt.Sprintf("%s ASC, %s ASC", ob.Cols().FactorEdbSeriesId, ob.Cols().EdbInfoId)
  616. // items, e := ob.GetItemsByCondition(cond, pars, []string{}, order)
  617. // if e != nil {
  618. // br.Msg = "获取失败"
  619. // br.ErrMsg = "获取因子指标关联失败, Err: " + e.Error()
  620. // return
  621. // }
  622. // for _, v := range items {
  623. // edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  624. // }
  625. // edbMappings = items
  626. //}
  627. //edbIdItem := make(map[int]*data_manage.EdbInfo)
  628. //edbItems, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  629. //if e != nil {
  630. // br.Msg = "获取失败"
  631. // br.ErrMsg = "获取因子指标失败, Err: " + e.Error()
  632. // return
  633. //}
  634. //for _, v := range edbItems {
  635. // edbIdItem[v.EdbInfoId] = v
  636. //}
  637. //
  638. //// 获取因子指标数据, 计算相关性
  639. //resp := new(data_manage.FactorEdbSeriesCorrelationMatrixResp)
  640. //calculateDataOb := new(data_manage.FactorEdbSeriesCalculateData)
  641. //
  642. //calculateWorkers := make(chan struct{}, 10)
  643. //wg := sync.WaitGroup{}
  644. //edbExists := make(map[string]bool)
  645. //chartKeyMap := make(map[string]*data_manage.FactorEdbSeriesChartMapping)
  646. //for _, v := range edbMappings {
  647. // existsKey := fmt.Sprintf("%d-%d", v.FactorEdbSeriesId, v.EdbInfoId)
  648. // if edbExists[existsKey] {
  649. // continue
  650. // }
  651. // edbExists[existsKey] = true
  652. //
  653. // edbItem := edbIdItem[v.EdbInfoId]
  654. // if edbItem == nil {
  655. // continue
  656. // }
  657. // seriesItem := seriesIdItem[v.FactorEdbSeriesId]
  658. // if seriesItem == nil {
  659. // continue
  660. // }
  661. //
  662. // wg.Add(1)
  663. // go func(mapping *data_manage.FactorEdbSeriesMapping, edb *data_manage.EdbInfo, series *data_manage.FactorEdbSeries) {
  664. // defer func() {
  665. // wg.Done()
  666. // <-calculateWorkers
  667. // }()
  668. // calculateWorkers <- struct{}{}
  669. //
  670. // var item data_manage.FactorEdbSeriesCorrelationMatrixItem
  671. // item.SeriesId = series.FactorEdbSeriesId
  672. // item.EdbInfoId = edb.EdbInfoId
  673. // item.EdbCode = edb.EdbCode
  674. // item.EdbName = edb.EdbName
  675. //
  676. // // 指标来源
  677. // edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  678. // edbList = append(edbList, &data_manage.ChartEdbInfoMapping{
  679. // EdbInfoId: edb.EdbInfoId,
  680. // EdbInfoCategoryType: edb.EdbInfoType,
  681. // EdbType: edb.EdbType,
  682. // Source: edb.Source,
  683. // SourceName: edb.SourceName,
  684. // })
  685. // sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  686. // item.SourceName = strings.Join(sourceNameList, ",")
  687. // item.SourceNameEn = strings.Join(sourceNameEnList, ",")
  688. //
  689. // // 获取指标数据
  690. // dataListB := make([]*data_manage.EdbDataList, 0)
  691. // if series.CalculateState == data_manage.FactorEdbSeriesCalculated {
  692. // cond := fmt.Sprintf(" AND %s = ? AND %s = ?", calculateDataOb.Cols().FactorEdbSeriesId, calculateDataOb.Cols().EdbInfoId)
  693. // pars := make([]interface{}, 0)
  694. // pars = append(pars, mapping.FactorEdbSeriesId, mapping.EdbInfoId)
  695. // dataItems, e := calculateDataOb.GetItemsByCondition(cond, pars, []string{calculateDataOb.Cols().DataTime, calculateDataOb.Cols().Value}, fmt.Sprintf("%s ASC", calculateDataOb.Cols().DataTime))
  696. // if e != nil {
  697. // item.Msg = fmt.Sprintf("计算失败")
  698. // item.ErrMsg = fmt.Sprintf("获取计算数据失败, err: %v", e)
  699. // resp.Fail = append(resp.Fail, item)
  700. // return
  701. // }
  702. // dataListB = data_manage.TransEdbSeriesCalculateData2EdbDataList(dataItems)
  703. // } else {
  704. // switch edb.EdbInfoType {
  705. // case 0:
  706. // dataListB, e = data_manage.GetEdbDataList(edb.Source, edb.SubSource, edb.EdbInfoId, "", "")
  707. // case 1:
  708. // _, dataListB, _, _, e, _ = data.GetPredictDataListByPredictEdbInfoId(edb.EdbInfoId, "", "", false)
  709. // default:
  710. // item.Msg = fmt.Sprintf("计算失败")
  711. // item.ErrMsg = fmt.Sprintf("指标类型异常, edbType: %d", edb.EdbInfoType)
  712. // resp.Fail = append(resp.Fail, item)
  713. // return
  714. // }
  715. // }
  716. //
  717. // // 计算相关性
  718. // xEdbIdValue, yDataList, e := correlationServ.CalculateCorrelation(req.Correlation.LeadValue, req.Correlation.LeadUnit, baseEdb.Frequency, edb.Frequency, dataListA, dataListB)
  719. // if e != nil {
  720. // item.Msg = fmt.Sprintf("计算失败")
  721. // item.ErrMsg = fmt.Sprintf("相关性计算失败, err: %v", e)
  722. // resp.Fail = append(resp.Fail, item)
  723. // return
  724. // }
  725. //
  726. // // X及Y轴数据
  727. // yData := yDataList[0].Value
  728. // yLen := len(yData)
  729. // values := make([]data_manage.FactorEdbSeriesCorrelationMatrixValues, len(xEdbIdValue))
  730. // for k, x := range xEdbIdValue {
  731. // var y float64
  732. // if k >= 0 && k < yLen {
  733. // y = yData[k]
  734. // }
  735. // y = utils.SubFloatToFloat(y, 2)
  736. // values[k] = data_manage.FactorEdbSeriesCorrelationMatrixValues{
  737. // XData: x, YData: y,
  738. // }
  739. // }
  740. //
  741. // // 图表关联-此处添加的chart_info_id=0
  742. // newMapping := new(data_manage.FactorEdbSeriesChartMapping)
  743. // newMapping.CalculateType = data_manage.FactorEdbSeriesChartCalculateTypeCorrelation
  744. //
  745. // // 计算参数
  746. // var calculatePars data_manage.FactorEdbSeriesChartCalculateCorrelationReq
  747. // calculatePars.BaseEdbInfoId = req.BaseEdbInfoId
  748. // calculatePars.LeadValue = req.Correlation.LeadValue
  749. // calculatePars.LeadUnit = req.Correlation.LeadUnit
  750. // calculatePars.CalculateValue = req.Correlation.CalculateValue
  751. // calculatePars.CalculateUnit = req.Correlation.CalculateUnit
  752. // bc, e := json.Marshal(calculatePars)
  753. // if e != nil {
  754. // item.Msg = fmt.Sprintf("计算失败")
  755. // item.ErrMsg = fmt.Sprintf("计算参数JSON格式化失败, err: %v", e)
  756. // resp.Fail = append(resp.Fail, item)
  757. // return
  758. // }
  759. // newMapping.CalculatePars = string(bc)
  760. //
  761. // // 计算结果, 注此处保存的是排序前的顺序
  762. // bv, e := json.Marshal(values)
  763. // if e != nil {
  764. // item.Msg = fmt.Sprintf("计算失败")
  765. // item.ErrMsg = fmt.Sprintf("计算结果JSON格式化失败, err: %v", e)
  766. // resp.Fail = append(resp.Fail, item)
  767. // return
  768. // }
  769. // newMapping.CalculateData = string(bv)
  770. // newMapping.FactorEdbSeriesId = mapping.FactorEdbSeriesId
  771. // newMapping.EdbInfoId = mapping.EdbInfoId
  772. // newMapping.CreateTime = time.Now().Local()
  773. // newMapping.ModifyTime = time.Now().Local()
  774. // chartKeyMap[existsKey] = newMapping
  775. //
  776. // // 按照固定规则排期数[0 1 2 3 -1 -2 -3], 仅矩阵展示为此顺序
  777. // sort.Sort(data_manage.FactorEdbSeriesCorrelationMatrixOrder(values))
  778. // item.Msg = "计算成功"
  779. // item.Values = values
  780. // resp.Success = append(resp.Success, item)
  781. // }(v, edbItem, seriesItem)
  782. //}
  783. //wg.Wait()
  784. //
  785. //// 新增图表关联, 此处按照顺序添加
  786. //chartMappings := make([]*data_manage.FactorEdbSeriesChartMapping, 0)
  787. //for _, v := range edbMappings {
  788. // k := fmt.Sprintf("%d-%d", v.FactorEdbSeriesId, v.EdbInfoId)
  789. // item := chartKeyMap[k]
  790. // if item == nil {
  791. // continue
  792. // }
  793. // chartMappings = append(chartMappings, item)
  794. //}
  795. //chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
  796. //if e = chartMappingOb.ClearAndCreateMapping(req.SeriesIds, chartMappings); e != nil {
  797. // br.Msg = "获取失败"
  798. // br.ErrMsg = fmt.Sprintf("新增图表关联失败, Err: %v", e)
  799. // return
  800. //}
  801. br.Data = resp
  802. br.Ret = 200
  803. br.Success = true
  804. br.Msg = "获取成功"
  805. }