bloomberg_data.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/services/data"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type BloombergDataController struct {
  16. controllers.BaseAuthController
  17. }
  18. // @router /bloomberg_source/list [get]
  19. func (this *BloombergDataController) List() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. params := new(data_manage.BloombergSourceListReq)
  36. if e := this.ParseForm(params); e != nil {
  37. br.Msg = "获取失败"
  38. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  39. return
  40. }
  41. cond := ``
  42. pars := make([]interface{}, 0)
  43. {
  44. params.Keywords = strings.TrimSpace(params.Keywords)
  45. if params.Keywords != "" {
  46. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, data_manage.BaseFromBloombergIndexCols.IndexCode, data_manage.BaseFromBloombergIndexCols.IndexName)
  47. kw := fmt.Sprint("%", params.Keywords, "%")
  48. pars = append(pars, kw, kw)
  49. }
  50. if params.Frequency != "" {
  51. cond += fmt.Sprintf(` AND %s = ?`, data_manage.BaseFromBloombergIndexCols.Frequency)
  52. pars = append(pars, params.Frequency)
  53. }
  54. }
  55. bloombergOb := new(data_manage.BaseFromBloombergIndex)
  56. total, e := bloombergOb.GetCountByCondition(cond, pars)
  57. if e != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "获取Bloomberg原始指标列表总数失败, Err: " + e.Error()
  60. return
  61. }
  62. var startSize int
  63. if params.PageSize <= 0 {
  64. params.PageSize = utils.PageSize20
  65. }
  66. if params.CurrentIndex <= 0 {
  67. params.CurrentIndex = 1
  68. }
  69. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  70. orderFields := map[int]string{
  71. 0: data_manage.BaseFromBloombergIndexCols.CreateTime, 1: data_manage.BaseFromBloombergIndexCols.StartDate,
  72. 2: data_manage.BaseFromBloombergIndexCols.EndDate, 3: data_manage.BaseFromBloombergIndexCols.ModifyTime,
  73. }
  74. orderType := map[int]string{0: "DESC", 1: "ASC", 2: "DESC"}
  75. orderRule := fmt.Sprintf("%s %s", orderFields[params.SortField], orderType[params.SortRule])
  76. edbList := make([]*data_manage.BaseFromBloombergIndex, 0)
  77. if params.ListAll {
  78. list, e := bloombergOb.GetItemsByCondition(cond, pars, []string{}, orderRule, 0)
  79. if e != nil {
  80. br.Msg = "获取失败"
  81. br.ErrMsg = "获取Bloomberg原始指标列表失败, Err: " + e.Error()
  82. return
  83. }
  84. edbList = list
  85. } else {
  86. list, e := bloombergOb.GetPageItemsByCondition(cond, pars, []string{}, orderRule, startSize, params.PageSize)
  87. if e != nil {
  88. br.Msg = "获取失败"
  89. br.ErrMsg = "获取Bloomberg原始指标分页列表失败, Err: " + e.Error()
  90. return
  91. }
  92. edbList = list
  93. }
  94. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_BLOOMBERG)
  95. if e != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = "获取Bloomberg已添加的指标失败, Err: " + e.Error()
  98. return
  99. }
  100. existMap := make(map[string]*data_manage.EdbInfo)
  101. for _, v := range existsEdb {
  102. existMap[v.EdbCode] = v
  103. }
  104. respList := make([]*data_manage.BaseFromBloombergIndexItem, 0)
  105. for _, v := range edbList {
  106. t := data_manage.FormatBaseFromBloombergIndex2Item(v)
  107. ed := existMap[v.IndexCode]
  108. if ed != nil {
  109. t.EdbExist = 1
  110. t.EdbInfoId = ed.EdbInfoId
  111. t.EdbUniqueCode = ed.UniqueCode
  112. t.EdbClassifyId = ed.ClassifyId
  113. }
  114. respList = append(respList, t)
  115. }
  116. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  117. dataResp := new(data_manage.BloombergSourceListResp)
  118. dataResp.Paging = page
  119. dataResp.List = respList
  120. br.Data = dataResp
  121. br.Ret = 200
  122. br.Success = true
  123. br.Msg = "获取成功"
  124. }
  125. // @router /bloomberg_source/batch_add [post]
  126. func (this *BloombergDataController) BatchAdd() {
  127. br := new(models.BaseResponse).Init()
  128. defer func() {
  129. if br.ErrMsg == "" {
  130. br.IsSendEmail = false
  131. }
  132. this.Data["json"] = br
  133. this.ServeJSON()
  134. }()
  135. sysUser := this.SysUser
  136. if sysUser == nil {
  137. br.Msg = "请登录"
  138. br.ErrMsg = "请登录,SysUser Is Empty"
  139. br.Ret = 408
  140. return
  141. }
  142. deleteCache := true
  143. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_BLOOMBERG_" + strconv.Itoa(sysUser.AdminId)
  144. defer func() {
  145. if deleteCache {
  146. _ = utils.Rc.Delete(cacheKey)
  147. }
  148. }()
  149. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  150. deleteCache = false
  151. br.Msg = "系统处理中,请稍后重试!"
  152. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  153. return
  154. }
  155. var req []*data_manage.AddEdbInfoReq
  156. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  157. br.Msg = "参数解析异常!"
  158. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  159. return
  160. }
  161. if len(req) == 0 {
  162. br.Msg = "请选择指标"
  163. return
  164. }
  165. if len(req) > 30 {
  166. br.Msg = "批量添加指标数量不得超过30个"
  167. return
  168. }
  169. indexNames := make([]string, 0)
  170. for _, v := range req {
  171. v.EdbCode = strings.TrimSpace(v.EdbCode)
  172. if v.EdbCode == "" {
  173. br.Msg = "指标ID不可为空"
  174. return
  175. }
  176. v.EdbName = strings.TrimSpace(v.EdbName)
  177. if v.EdbName == "" {
  178. br.Msg = "请输入指标名称"
  179. return
  180. }
  181. indexNames = append(indexNames, v.EdbName)
  182. v.Frequency = strings.TrimSpace(v.Frequency)
  183. if v.Frequency == "" {
  184. br.Msg = "请选择频度"
  185. return
  186. }
  187. v.Unit = strings.TrimSpace(v.Unit)
  188. if v.Unit == "" {
  189. br.Msg = "请输入单位"
  190. return
  191. }
  192. if v.ClassifyId <= 0 {
  193. br.Msg = "请选择分类"
  194. return
  195. }
  196. }
  197. for _, v := range req {
  198. var r data.BloombergIndexSource2EdbReq
  199. r.EdbCode = v.EdbCode
  200. r.EdbName = v.EdbName
  201. r.Frequency = v.Frequency
  202. r.Unit = v.Unit
  203. r.ClassifyId = v.ClassifyId
  204. r.AdminId = sysUser.AdminId
  205. r.AdminRealName = sysUser.RealName
  206. edbInfo, e, errMsg, skip := data.BloombergIndexSource2Edb(r, this.Lang)
  207. if e != nil {
  208. br.Msg = "操作失败"
  209. if errMsg != "" {
  210. br.Msg = errMsg
  211. }
  212. br.ErrMsg = e.Error()
  213. return
  214. }
  215. if skip {
  216. continue
  217. }
  218. {
  219. edbLog := new(data_manage.EdbInfoLog)
  220. edbLog.EdbInfoId = edbInfo.EdbInfoId
  221. edbLog.SourceName = edbInfo.SourceName
  222. edbLog.Source = edbInfo.Source
  223. edbLog.EdbCode = edbInfo.EdbCode
  224. edbLog.EdbName = edbInfo.EdbName
  225. edbLog.ClassifyId = edbInfo.ClassifyId
  226. edbLog.SysUserId = sysUser.AdminId
  227. edbLog.SysUserRealName = sysUser.RealName
  228. edbLog.CreateTime = time.Now()
  229. edbLog.Content = string(this.Ctx.Input.RequestBody)
  230. edbLog.Status = "新增指标"
  231. edbLog.Method = this.Ctx.Input.URI()
  232. go data_manage.AddEdbInfoLog(edbLog)
  233. }
  234. }
  235. br.Msg = "操作成功"
  236. br.Ret = 200
  237. br.Success = true
  238. br.IsAddLog = true
  239. }
  240. // @router /bloomberg_source/name_check [post]
  241. func (this *BloombergDataController) NameCheck() {
  242. br := new(models.BaseResponse).Init()
  243. defer func() {
  244. if br.ErrMsg == "" {
  245. br.IsSendEmail = false
  246. }
  247. this.Data["json"] = br
  248. this.ServeJSON()
  249. }()
  250. sysUser := this.SysUser
  251. if sysUser == nil {
  252. br.Msg = "请登录"
  253. br.ErrMsg = "请登录,SysUser Is Empty"
  254. br.Ret = 408
  255. return
  256. }
  257. var req []*data_manage.AddEdbInfoReq
  258. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  259. br.Msg = "参数解析异常!"
  260. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  261. return
  262. }
  263. if len(req) == 0 {
  264. br.Msg = "请选择指标"
  265. return
  266. }
  267. type NameCheckResult struct {
  268. EdbCode string
  269. EdbName string
  270. Exist bool
  271. }
  272. indexNames := make([]string, 0)
  273. resp := make([]*NameCheckResult, 0)
  274. for _, v := range req {
  275. v.EdbCode = strings.TrimSpace(v.EdbCode)
  276. if v.EdbCode == "" {
  277. br.Msg = "指标ID不可为空"
  278. return
  279. }
  280. v.EdbName = strings.TrimSpace(v.EdbName)
  281. if v.EdbName == "" {
  282. br.Msg = "请输入指标名称"
  283. return
  284. }
  285. indexNames = append(indexNames, v.EdbName)
  286. resp = append(resp, &NameCheckResult{
  287. EdbCode: v.EdbCode,
  288. EdbName: v.EdbName,
  289. })
  290. }
  291. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  292. if e != nil {
  293. br.Msg = "操作失败"
  294. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  295. return
  296. }
  297. nameExists := make(map[string]bool)
  298. for _, v := range edbList {
  299. nameExists[v.EdbName] = true
  300. }
  301. if len(nameExists) > 0 {
  302. for _, v := range resp {
  303. v.Exist = nameExists[v.EdbName]
  304. }
  305. }
  306. br.Data = resp
  307. br.Msg = "操作成功"
  308. br.Ret = 200
  309. br.Success = true
  310. }
  311. // @router /bloomberg_source/add_check [post]
  312. func (this *BloombergDataController) AddCheck() {
  313. br := new(models.BaseResponse).Init()
  314. defer func() {
  315. if br.ErrMsg == "" {
  316. br.IsSendEmail = false
  317. }
  318. this.Data["json"] = br
  319. this.ServeJSON()
  320. }()
  321. sysUser := this.SysUser
  322. if sysUser == nil {
  323. br.Msg = "请登录"
  324. br.ErrMsg = "请登录,SysUser Is Empty"
  325. br.Ret = 408
  326. return
  327. }
  328. var req *data_manage.BloombergSourceBatchAddCheckReq
  329. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  330. br.Msg = "参数解析异常!"
  331. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  332. return
  333. }
  334. codeMax := 30
  335. codeLen := len(req.IndexCodes)
  336. orderFields := map[int]string{
  337. 0: data_manage.BaseFromBloombergIndexCols.CreateTime, 1: data_manage.BaseFromBloombergIndexCols.StartDate,
  338. 2: data_manage.BaseFromBloombergIndexCols.EndDate, 3: data_manage.BaseFromBloombergIndexCols.ModifyTime,
  339. }
  340. orderType := map[int]string{0: "DESC", 1: "ASC", 2: "DESC"}
  341. orderRule := fmt.Sprintf("%s %s", orderFields[req.SortField], orderType[req.SortRule])
  342. existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_BLOOMBERG)
  343. if e != nil {
  344. br.Msg = "获取失败"
  345. br.ErrMsg = "获取Bloomberg已添加的指标失败, Err: " + e.Error()
  346. return
  347. }
  348. existMap := make(map[string]*data_manage.EdbInfo)
  349. for _, v := range existsEdb {
  350. existMap[v.EdbCode] = v
  351. }
  352. bloombergOb := new(data_manage.BaseFromBloombergIndex)
  353. if !req.ListAll {
  354. if codeLen == 0 {
  355. br.Msg = "请选择指标"
  356. return
  357. }
  358. if codeLen > codeMax {
  359. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  360. return
  361. }
  362. cond := fmt.Sprintf(` AND %s IN (%s)`, data_manage.BaseFromBloombergIndexCols.IndexCode, utils.GetOrmInReplace(codeLen))
  363. pars := make([]interface{}, 0)
  364. pars = append(pars, req.IndexCodes)
  365. list, e := bloombergOb.GetItemsByCondition(cond, pars, []string{}, orderRule, 0)
  366. if e != nil {
  367. br.Msg = "获取失败"
  368. br.ErrMsg = "获取Bloomberg原始指标列表失败, Err: " + e.Error()
  369. return
  370. }
  371. resp := make([]*data_manage.BaseFromBloombergIndexItem, 0)
  372. for _, v := range list {
  373. t := data_manage.FormatBaseFromBloombergIndex2Item(v)
  374. ed := existMap[v.IndexCode]
  375. if ed != nil {
  376. t.EdbExist = 1
  377. t.EdbInfoId = ed.EdbInfoId
  378. t.EdbUniqueCode = ed.UniqueCode
  379. t.EdbClassifyId = ed.ClassifyId
  380. }
  381. resp = append(resp, t)
  382. }
  383. br.Data = resp
  384. br.Msg = "校验成功"
  385. br.Ret = 200
  386. br.Success = true
  387. return
  388. }
  389. cond := ``
  390. pars := make([]interface{}, 0)
  391. req.Keywords = strings.TrimSpace(req.Keywords)
  392. if req.Keywords != "" {
  393. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, data_manage.BaseFromBloombergIndexCols.IndexCode, data_manage.BaseFromBloombergIndexCols.IndexName)
  394. kw := fmt.Sprint("%", req.Keywords, "%")
  395. pars = append(pars, kw, kw)
  396. }
  397. if req.Frequency != "" {
  398. cond += fmt.Sprintf(` AND %s = ?`, data_manage.BaseFromBloombergIndexCols.Frequency)
  399. pars = append(pars, req.Frequency)
  400. }
  401. if codeLen > 0 {
  402. cond += fmt.Sprintf(` AND %s NOT IN (%s)`, data_manage.BaseFromBloombergIndexCols.IndexCode, utils.GetOrmInReplace(codeLen))
  403. pars = append(pars, req.IndexCodes)
  404. }
  405. list, e := bloombergOb.GetItemsByCondition(cond, pars, []string{}, orderRule, codeMax+1)
  406. if e != nil {
  407. br.Msg = "获取失败"
  408. br.ErrMsg = "获取Bloomberg原始指标列表失败, Err: " + e.Error()
  409. return
  410. }
  411. if len(list) > codeMax {
  412. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  413. return
  414. }
  415. resp := make([]*data_manage.BaseFromBloombergIndexItem, 0)
  416. for _, v := range list {
  417. t := data_manage.FormatBaseFromBloombergIndex2Item(v)
  418. ed := existMap[v.IndexCode]
  419. if ed != nil {
  420. t.EdbExist = 1
  421. t.EdbInfoId = ed.EdbInfoId
  422. t.EdbUniqueCode = ed.UniqueCode
  423. t.EdbClassifyId = ed.ClassifyId
  424. }
  425. resp = append(resp, t)
  426. }
  427. br.Data = resp
  428. br.Msg = "校验成功"
  429. br.Ret = 200
  430. br.Success = true
  431. }