base_from_bloomberg.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if req.TaskKey == "" {
  164. br.Msg = "TaskKey不可为空"
  165. return
  166. }
  167. tasks, e := services.LoadPCSGBloombergTask()
  168. if e != nil {
  169. br.Msg = "加载配置失败"
  170. br.ErrMsg = fmt.Sprintf("加载配置失败, Err: %v", e)
  171. return
  172. }
  173. conf := new(services.PCSGBloombergTask)
  174. for _, v := range tasks {
  175. if v.TaskKey == req.TaskKey {
  176. conf = v
  177. break
  178. }
  179. }
  180. if conf.TaskKey == "" {
  181. br.Msg = "TaskKey配置不存在"
  182. return
  183. }
  184. var indexes []models.BaseFromBloombergApiIndexAndData
  185. var index models.BaseFromBloombergApiIndexAndData
  186. var indexData []models.BaseFromBloombergApiIndexData
  187. index.IndexCode = req.IndexCode
  188. for k, v := range req.DataMap {
  189. indexData = append(indexData, models.BaseFromBloombergApiIndexData{
  190. DataTime: k,
  191. Value: v,
  192. })
  193. }
  194. index.Data = indexData
  195. indexes = append(indexes, index)
  196. // 写入数据
  197. if e := services.PCSGWrite2BaseBloomberg(indexes, conf.VCode, conf.ExtraLetter, conf.IndexNamePrefix, conf.IndexCodeSuffix); e != nil {
  198. br.Msg = "刷新失败"
  199. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  200. return
  201. }
  202. br.Ret = 200
  203. br.Success = true
  204. br.Msg = "操作成功"
  205. }
  206. // PCSGRefreshTask
  207. // @Title 中石油新加坡-刷新任务
  208. // @Description 中石油新加坡-刷新任务
  209. // @Success 200 {object} models.AddEdbInfoReq
  210. // @router /pcsg/refresh_task [post]
  211. func (this *BloombergController) PCSGRefreshTask() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. if br.ErrMsg == "" {
  215. br.IsSendEmail = false
  216. }
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. // 非必传, 只是手动请求的时候用
  221. var req struct {
  222. TaskKey string
  223. }
  224. _ = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  225. tasks, e := services.LoadPCSGBloombergTask()
  226. if e != nil {
  227. br.Msg = "加载配置失败"
  228. br.ErrMsg = fmt.Sprintf("加载配置失败, Err: %v", e)
  229. return
  230. }
  231. for _, v := range tasks {
  232. if req.TaskKey != "" && v.TaskKey != req.TaskKey {
  233. continue
  234. }
  235. fmt.Println(v)
  236. time.Sleep(5 * time.Second)
  237. // 获取数据
  238. var r services.PCSGBloombergApiReq
  239. r.TaskKey = v.TaskKey
  240. r.Frequency = v.Frequency
  241. indexes, e := services.GetPCSGBloombergGeneralIndexFromBridge(r)
  242. if e != nil {
  243. br.Msg = "刷新失败"
  244. br.ErrMsg = "Bridge-GetPCSGBloombergGeneralIndexFromBridge, Err: " + e.Error()
  245. return
  246. }
  247. if len(indexes) == 0 {
  248. br.Ret = 200
  249. br.Success = true
  250. br.Msg = "操作成功"
  251. return
  252. }
  253. // 此处重新整理一下, 将同一指标的数据合并, 否则会使刷新时间变得很长
  254. var newIndexes []models.BaseFromBloombergApiIndexAndData
  255. codeMap := make(map[string]models.BaseFromBloombergApiIndexAndData)
  256. indexCodeData := make(map[string][]models.BaseFromBloombergApiIndexData)
  257. for _, iv := range indexes {
  258. if codeMap[iv.IndexCode].IndexCode == "" {
  259. var indexItem models.BaseFromBloombergApiIndexAndData
  260. indexItem.IndexCode = iv.IndexCode
  261. indexItem.IndexName = iv.IndexName
  262. indexItem.Unit = iv.Unit
  263. indexItem.Source = iv.Source
  264. indexItem.Frequency = iv.Frequency
  265. indexItem.CreateTime = iv.CreateTime
  266. indexItem.ModifyTime = iv.ModifyTime
  267. codeMap[iv.IndexCode] = indexItem
  268. }
  269. indexCodeData[iv.IndexCode] = append(indexCodeData[iv.IndexCode], iv.Data...)
  270. }
  271. for _, mv := range codeMap {
  272. mv.Data = indexCodeData[mv.IndexCode]
  273. newIndexes = append(newIndexes, mv)
  274. }
  275. // 写入数据
  276. if e = services.PCSGWrite2BaseBloomberg(newIndexes, v.VCode, v.ExtraLetter, v.IndexNamePrefix, v.IndexCodeSuffix); e != nil {
  277. br.Msg = "刷新失败"
  278. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  279. return
  280. }
  281. }
  282. br.Ret = 200
  283. br.Success = true
  284. br.Msg = "操作成功"
  285. }