base_from_bloomberg.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "time"
  11. )
  12. // BloombergController Bloomberg
  13. type BloombergController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增彭博指标接口
  18. // @Description 新增彭博指标接口
  19. // @Success 200 {object} models.AddEdbInfoReq
  20. // @router /add [post]
  21. func (this *BloombergController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. var cacheKey string
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. _ = utils.Rc.Delete(cacheKey)
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. source := utils.DATA_SOURCE_BLOOMBERG
  33. var req models.AddEdbInfoReq
  34. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  35. if err != nil {
  36. br.Msg = "参数解析异常!"
  37. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  38. return
  39. }
  40. if req.EdbCode == "" {
  41. br.Msg = "请输入指标编码!"
  42. br.ErrMsg = "请输入指标编码,指标编码为空"
  43. return
  44. }
  45. if req.Source != source {
  46. br.Msg = "数据源有误"
  47. br.ErrMsg = fmt.Sprintf("数据源ID不匹配, Source: %d", req.Source)
  48. return
  49. }
  50. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  51. if !utils.Rc.IsExist(cacheKey) {
  52. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  53. err = models.AddEdbDataFromBloomberg(req.EdbCode)
  54. if err != nil {
  55. br.Msg = "获取指标信息失败!"
  56. br.ErrMsg = "获取指标信息失败 AddEdbDataFromBloomberg,Err:" + err.Error()
  57. return
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. } else {
  63. br.Ret = 501
  64. br.Success = true
  65. br.Msg = "系统处理中,请稍后重试"
  66. }
  67. }
  68. // Refresh
  69. // @Title 刷新彭博指标接口
  70. // @Description 刷新彭博指标接口
  71. // @Success 200 {object} models.RefreshEdbInfoReq
  72. // @router /refresh [post]
  73. func (this *BloombergController) Refresh() {
  74. br := new(models.BaseResponse).Init()
  75. var cacheKey string
  76. defer func() {
  77. if br.ErrMsg == "" {
  78. br.IsSendEmail = false
  79. }
  80. _ = utils.Rc.Delete(cacheKey)
  81. this.Data["json"] = br
  82. this.ServeJSON()
  83. }()
  84. source := utils.DATA_SOURCE_BLOOMBERG
  85. var req models.RefreshEdbInfoReq
  86. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  87. if err != nil {
  88. br.Msg = "参数解析异常!"
  89. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  90. return
  91. }
  92. if req.EdbCode == "" {
  93. br.Msg = "请输入指标编码!"
  94. br.ErrMsg = "请输入指标编码,指标编码为空"
  95. return
  96. }
  97. if req.EdbInfoId <= 0 {
  98. br.Msg = "请输入指标ID!"
  99. br.ErrMsg = "请输入指标ID"
  100. return
  101. }
  102. // 获取指标详情
  103. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  104. if err != nil {
  105. br.Msg = "指标不存在!"
  106. br.ErrMsg = "指标不存在"
  107. return
  108. }
  109. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  110. if utils.Rc.IsExist(cacheKey) {
  111. br.Ret = 501
  112. br.Success = true
  113. br.Msg = "系统处理中,请稍后重试"
  114. return
  115. }
  116. dataUpdateTime := time.Now().Format(utils.FormatDateTime)
  117. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  118. err = models.RefreshEdbDataFromBloomberg(req.EdbInfoId, req.EdbCode, req.StartDate)
  119. if err != nil && err.Error() != utils.ErrNoRow() {
  120. br.Msg = "刷新指标信息失败!"
  121. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromBloomberg,Err:" + err.Error()
  122. return
  123. }
  124. // 更新指标最大最小值
  125. erDataUpdateDate, err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate(edbInfo, dataUpdateTime)
  126. if err != nil {
  127. br.Msg = errMsg
  128. br.ErrMsg = err.Error()
  129. return
  130. }
  131. // 添加指标刷新成功日志
  132. if erDataUpdateDate != "" {
  133. _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 1, "", 0, 0)
  134. } else {
  135. _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 2, "未刷新到数据", 0, 0)
  136. }
  137. // 更新ES
  138. go logic.UpdateEs(edbInfo.EdbInfoId)
  139. br.Ret = 200
  140. br.Success = true
  141. br.Msg = "获取成功"
  142. }
  143. // PCSGImportHistoryData
  144. // @Title 中石油新加坡-导入历史数据
  145. // @Description 中石油新加坡-导入历史数据
  146. // @Success 200 {object} models.PCSGImportHistoryDataReq
  147. // @router /pcsg/import_history_data [post]
  148. func (this *BloombergController) PCSGImportHistoryData() {
  149. br := new(models.BaseResponse).Init()
  150. defer func() {
  151. if br.ErrMsg == "" {
  152. br.IsSendEmail = false
  153. }
  154. this.Data["json"] = br
  155. this.ServeJSON()
  156. }()
  157. var req models.PCSGImportHistoryDataReq
  158. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  159. br.Msg = "参数解析异常!"
  160. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  161. return
  162. }
  163. var indexes []models.BaseFromBloombergApiIndexAndData
  164. var index models.BaseFromBloombergApiIndexAndData
  165. var indexData []models.BaseFromBloombergApiIndexData
  166. index.IndexCode = req.IndexCode
  167. for k, v := range req.DataMap {
  168. indexData = append(indexData, models.BaseFromBloombergApiIndexData{
  169. DataTime: k,
  170. Value: v,
  171. })
  172. }
  173. index.Data = indexData
  174. indexes = append(indexes, index)
  175. // 写入数据
  176. if e := services.PCSGWrite2BaseBloomberg(indexes, req.IsVCode, req.ExtraLetter, "", ""); e != nil {
  177. br.Msg = "刷新失败"
  178. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  179. return
  180. }
  181. br.Ret = 200
  182. br.Success = true
  183. br.Msg = "操作成功"
  184. }
  185. // PCSGRefreshTask
  186. // @Title 中石油新加坡-刷新任务
  187. // @Description 中石油新加坡-刷新任务
  188. // @Success 200 {object} models.AddEdbInfoReq
  189. // @router /pcsg/refresh_task [post]
  190. func (this *BloombergController) PCSGRefreshTask() {
  191. br := new(models.BaseResponse).Init()
  192. defer func() {
  193. if br.ErrMsg == "" {
  194. br.IsSendEmail = false
  195. }
  196. this.Data["json"] = br
  197. this.ServeJSON()
  198. }()
  199. // 非必传, 只是手动请求的时候用
  200. var req struct {
  201. TaskKey string
  202. }
  203. _ = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  204. tasks, e := services.LoadPCSGBloombergTask()
  205. if e != nil {
  206. br.Msg = "加载配置失败"
  207. br.ErrMsg = fmt.Sprintf("加载配置失败, Err: %v", e)
  208. return
  209. }
  210. for _, v := range tasks {
  211. if req.TaskKey != "" && v.TaskKey != req.TaskKey {
  212. continue
  213. }
  214. fmt.Println(v)
  215. time.Sleep(5 * time.Second)
  216. // 获取数据
  217. var r services.PCSGBloombergApiReq
  218. r.TaskKey = v.TaskKey
  219. r.Frequency = v.Frequency
  220. indexes, e := services.GetPCSGBloombergGeneralIndexFromBridge(r)
  221. if e != nil {
  222. br.Msg = "刷新失败"
  223. br.ErrMsg = "Bridge-GetPCSGBloombergGeneralIndexFromBridge, Err: " + e.Error()
  224. return
  225. }
  226. if len(indexes) == 0 {
  227. br.Ret = 200
  228. br.Success = true
  229. br.Msg = "操作成功"
  230. return
  231. }
  232. // 此处重新整理一下, 将同一指标的数据合并, 否则会使刷新时间变得很长
  233. var newIndexes []models.BaseFromBloombergApiIndexAndData
  234. codeMap := make(map[string]models.BaseFromBloombergApiIndexAndData)
  235. indexCodeData := make(map[string][]models.BaseFromBloombergApiIndexData)
  236. for _, iv := range indexes {
  237. if codeMap[iv.IndexCode].IndexCode == "" {
  238. var indexItem models.BaseFromBloombergApiIndexAndData
  239. indexItem.IndexCode = iv.IndexCode
  240. indexItem.IndexName = iv.IndexName
  241. indexItem.Unit = iv.Unit
  242. indexItem.Source = iv.Source
  243. indexItem.Frequency = iv.Frequency
  244. indexItem.CreateTime = iv.CreateTime
  245. indexItem.ModifyTime = iv.ModifyTime
  246. codeMap[iv.IndexCode] = indexItem
  247. }
  248. indexCodeData[iv.IndexCode] = append(indexCodeData[iv.IndexCode], iv.Data...)
  249. }
  250. for _, mv := range codeMap {
  251. mv.Data = indexCodeData[mv.IndexCode]
  252. newIndexes = append(newIndexes, mv)
  253. }
  254. // 写入数据
  255. if e = services.PCSGWrite2BaseBloomberg(newIndexes, v.VCode, v.ExtraLetter, v.IndexNamePrefix, v.IndexCodeSuffix); e != nil {
  256. br.Msg = "刷新失败"
  257. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  258. return
  259. }
  260. }
  261. br.Ret = 200
  262. br.Success = true
  263. br.Msg = "操作成功"
  264. }