bloomberg_data.go 14 KB

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