bloomberg_data.go 14 KB

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