base_from_bloomberg.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package data
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. )
  7. type BloombergIndexSource2EdbReq struct {
  8. EdbCode string
  9. EdbName string
  10. Frequency string
  11. Unit string
  12. ClassifyId int
  13. AdminId int
  14. AdminRealName string
  15. }
  16. // BloombergIndexSource2Edb 新增彭博数据源到指标库
  17. func BloombergIndexSource2Edb(req BloombergIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  18. if req.EdbCode == "" {
  19. err = fmt.Errorf("指标ID为空")
  20. return
  21. }
  22. defer func() {
  23. if err != nil {
  24. tips := fmt.Sprintf("BloombergIndexSource2Edb新增失败, Err: %s", err.Error())
  25. fmt.Println(tips)
  26. utils.FileLog.Info(tips)
  27. }
  28. }()
  29. source := utils.DATA_SOURCE_BLOOMBERG
  30. // 是否已有指标数据
  31. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  32. if e != nil {
  33. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  34. return
  35. }
  36. // 新增指标数据
  37. if len(dataList) == 0 {
  38. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  39. if e != nil {
  40. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  41. return
  42. }
  43. if res == nil {
  44. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  45. return
  46. }
  47. if res.Ret != 200 {
  48. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  49. return
  50. }
  51. }
  52. // 是否新增过指标
  53. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  54. if e != nil && e.Error() != utils.ErrNoRow() {
  55. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  56. return
  57. }
  58. if exist != nil {
  59. skip = true
  60. return
  61. }
  62. // 开始结束时间
  63. var startDate, endDate string
  64. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  65. if e != nil && e.Error() != utils.ErrNoRow() {
  66. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  67. return
  68. }
  69. if minMax != nil {
  70. startDate = minMax.MinDate
  71. endDate = minMax.MaxDate
  72. }
  73. // 新增指标库
  74. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  75. if e != nil {
  76. errMsg = msg
  77. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  78. return
  79. }
  80. edb = edbInfo
  81. // 标记原始指标为已添加
  82. indexOb := new(data_manage.BaseFromBloombergIndex)
  83. if e = indexOb.UpdateEdbExist(req.EdbCode); e != nil {
  84. err = fmt.Errorf("BaseIndex: 标记已添加指标库失败, err: %s", e.Error())
  85. return
  86. }
  87. // 新增es
  88. go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  89. return
  90. }