base_from_bloomberg.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // PCSGRefreshDaily
  144. // @Title 中石油新加坡-刷新日度指标
  145. // @Description 中石油新加坡-刷新日度指标
  146. // @Success 200 {object} models.AddEdbInfoReq
  147. // @router /pcsg/refresh_daily [post]
  148. func (this *BloombergController) PCSGRefreshDaily() {
  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. // 获取数据
  158. indexes, e := services.GetPCSGBloombergDailyFromBridge()
  159. if e != nil {
  160. br.Msg = "刷新失败"
  161. br.ErrMsg = "Bridge-获取PCSG彭博日度指标失败, Err: " + e.Error()
  162. return
  163. }
  164. if len(indexes) == 0 {
  165. br.Ret = 200
  166. br.Success = true
  167. br.Msg = "操作成功"
  168. return
  169. }
  170. // 写入数据
  171. if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
  172. br.Msg = "刷新失败"
  173. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  174. return
  175. }
  176. br.Ret = 200
  177. br.Success = true
  178. br.Msg = "操作成功"
  179. }
  180. // PCSGRefreshWeekly
  181. // @Title 中石油新加坡-刷新周度指标
  182. // @Description 中石油新加坡-刷新周度指标
  183. // @Success 200 {object} models.AddEdbInfoReq
  184. // @router /pcsg/refresh_weekly [post]
  185. func (this *BloombergController) PCSGRefreshWeekly() {
  186. br := new(models.BaseResponse).Init()
  187. defer func() {
  188. if br.ErrMsg == "" {
  189. br.IsSendEmail = false
  190. }
  191. this.Data["json"] = br
  192. this.ServeJSON()
  193. }()
  194. // 获取数据
  195. indexes, e := services.GetPCSGBloombergWeeklyFromBridge()
  196. if e != nil {
  197. br.Msg = "刷新失败"
  198. br.ErrMsg = "Bridge-获取PCSG彭博周度指标失败, Err: " + e.Error()
  199. return
  200. }
  201. if len(indexes) == 0 {
  202. br.Ret = 200
  203. br.Success = true
  204. br.Msg = "操作成功"
  205. return
  206. }
  207. // 写入数据
  208. if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
  209. br.Msg = "刷新失败"
  210. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  211. return
  212. }
  213. br.Ret = 200
  214. br.Success = true
  215. br.Msg = "操作成功"
  216. }
  217. // PCSGRefreshMonthly
  218. // @Title 中石油新加坡-刷新月度指标
  219. // @Description 中石油新加坡-刷新周度指标
  220. // @Success 200 {object} models.AddEdbInfoReq
  221. // @router /pcsg/refresh_monthly [post]
  222. func (this *BloombergController) PCSGRefreshMonthly() {
  223. br := new(models.BaseResponse).Init()
  224. defer func() {
  225. if br.ErrMsg == "" {
  226. br.IsSendEmail = false
  227. }
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. // 获取数据
  232. indexes, e := services.GetPCSGBloombergMonthlyFromBridge()
  233. if e != nil {
  234. br.Msg = "刷新失败"
  235. br.ErrMsg = "Bridge-获取PCSG彭博月度指标失败, Err: " + e.Error()
  236. return
  237. }
  238. if len(indexes) == 0 {
  239. br.Ret = 200
  240. br.Success = true
  241. br.Msg = "操作成功"
  242. return
  243. }
  244. // 写入数据
  245. if e = services.PCSGWrite2BaseBloomberg(indexes); e != nil {
  246. br.Msg = "刷新失败"
  247. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  248. return
  249. }
  250. br.Ret = 200
  251. br.Success = true
  252. br.Msg = "操作成功"
  253. }
  254. // PCSGImportHistoryData
  255. // @Title 中石油新加坡-导入历史数据
  256. // @Description 中石油新加坡-导入历史数据
  257. // @Success 200 {object} models.PCSGImportHistoryDataReq
  258. // @router /pcsg/import_history_data [post]
  259. func (this *BloombergController) PCSGImportHistoryData() {
  260. br := new(models.BaseResponse).Init()
  261. defer func() {
  262. if br.ErrMsg == "" {
  263. br.IsSendEmail = false
  264. }
  265. this.Data["json"] = br
  266. this.ServeJSON()
  267. }()
  268. var req models.PCSGImportHistoryDataReq
  269. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  270. br.Msg = "参数解析异常!"
  271. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  272. return
  273. }
  274. var indexes []models.BaseFromBloombergApiIndexAndData
  275. var index models.BaseFromBloombergApiIndexAndData
  276. var indexData []models.BaseFromBloombergApiIndexData
  277. index.IndexCode = req.IndexCode
  278. for k, v := range req.DataMap {
  279. indexData = append(indexData, models.BaseFromBloombergApiIndexData{
  280. DataTime: k,
  281. Value: v,
  282. })
  283. }
  284. index.Data = indexData
  285. indexes = append(indexes, index)
  286. // 写入数据
  287. if e := services.PCSGWrite2BaseBloomberg(indexes); e != nil {
  288. br.Msg = "刷新失败"
  289. br.ErrMsg = "PCSG-写入Bloomberg数据源失败, Err: " + e.Error()
  290. return
  291. }
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "操作成功"
  295. }