bloomberg_data.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. fmt.Println(orderRule)
  94. // 列表
  95. edbList := make([]*data_manage.BaseFromBloombergIndex, 0)
  96. if params.ListAll {
  97. list, e := bloombergOb.GetItemsByCondition(cond, pars, []string{}, orderRule)
  98. if e != nil {
  99. br.Msg = "获取失败"
  100. br.ErrMsg = "获取Bloomberg原始指标列表失败, Err: " + e.Error()
  101. return
  102. }
  103. edbList = list
  104. } else {
  105. list, e := bloombergOb.GetPageItemsByCondition(cond, pars, []string{}, orderRule, startSize, params.PageSize)
  106. if e != nil {
  107. br.Msg = "获取失败"
  108. br.ErrMsg = "获取Bloomberg原始指标分页列表失败, Err: " + e.Error()
  109. return
  110. }
  111. edbList = list
  112. }
  113. respList := make([]*data_manage.BaseFromBloombergIndexItem, 0)
  114. for _, v := range edbList {
  115. respList = append(respList, data_manage.FormatBaseFromBloombergIndex2Item(v))
  116. }
  117. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  118. dataResp := new(data_manage.BloombergSourceListResp)
  119. dataResp.Paging = page
  120. dataResp.List = respList
  121. br.Data = dataResp
  122. br.Ret = 200
  123. br.Success = true
  124. br.Msg = "获取成功"
  125. }
  126. // BatchAdd
  127. // @Title 批量新增
  128. // @Description 批量新增
  129. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  130. // @Success 200 string "操作成功"
  131. // @router /bloomberg_source/batch_add [post]
  132. func (this *BloombergDataController) BatchAdd() {
  133. br := new(models.BaseResponse).Init()
  134. defer func() {
  135. if br.ErrMsg == "" {
  136. br.IsSendEmail = false
  137. }
  138. this.Data["json"] = br
  139. this.ServeJSON()
  140. }()
  141. sysUser := this.SysUser
  142. if sysUser == nil {
  143. br.Msg = "请登录"
  144. br.ErrMsg = "请登录,SysUser Is Empty"
  145. br.Ret = 408
  146. return
  147. }
  148. deleteCache := true
  149. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_BLOOMBERG_" + strconv.Itoa(sysUser.AdminId)
  150. defer func() {
  151. if deleteCache {
  152. _ = utils.Rc.Delete(cacheKey)
  153. }
  154. }()
  155. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  156. deleteCache = false
  157. br.Msg = "系统处理中,请稍后重试!"
  158. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  159. return
  160. }
  161. var req []*data_manage.AddEdbInfoReq
  162. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  163. br.Msg = "参数解析异常!"
  164. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  165. return
  166. }
  167. if len(req) == 0 {
  168. br.Msg = "请选择指标"
  169. return
  170. }
  171. if len(req) > 30 {
  172. br.Msg = "批量添加指标数量不得超过30个"
  173. return
  174. }
  175. indexNames := make([]string, 0)
  176. for _, v := range req {
  177. v.EdbCode = strings.TrimSpace(v.EdbCode)
  178. if v.EdbCode == "" {
  179. br.Msg = "指标ID不可为空"
  180. return
  181. }
  182. v.EdbName = strings.TrimSpace(v.EdbName)
  183. if v.EdbName == "" {
  184. br.Msg = "请输入指标名称"
  185. return
  186. }
  187. indexNames = append(indexNames, v.EdbName)
  188. v.Frequency = strings.TrimSpace(v.Frequency)
  189. if v.Frequency == "" {
  190. br.Msg = "请选择频度"
  191. return
  192. }
  193. v.Unit = strings.TrimSpace(v.Unit)
  194. if v.Unit == "" {
  195. br.Msg = "请输入单位"
  196. return
  197. }
  198. if v.ClassifyId <= 0 {
  199. br.Msg = "请选择分类"
  200. return
  201. }
  202. }
  203. // 限定同一时间最多批量新增30个指标
  204. for _, v := range req {
  205. var r data.BloombergIndexSource2EdbReq
  206. r.EdbCode = v.EdbCode
  207. r.EdbName = v.EdbName
  208. r.Frequency = v.Frequency
  209. r.Unit = v.Unit
  210. r.ClassifyId = v.ClassifyId
  211. r.AdminId = sysUser.AdminId
  212. r.AdminRealName = sysUser.RealName
  213. edbInfo, e, errMsg, skip := data.BloombergIndexSource2Edb(r)
  214. if e != nil {
  215. br.Msg = "操作失败"
  216. if errMsg != "" {
  217. br.Msg = errMsg
  218. }
  219. br.ErrMsg = e.Error()
  220. return
  221. }
  222. if skip {
  223. continue
  224. }
  225. // 试用平台更新用户累计新增指标数
  226. if utils.BusinessCode == utils.BusinessCodeSandbox {
  227. go func() {
  228. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  229. if e != nil {
  230. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  231. utils.FileLog.Info(tips)
  232. return
  233. }
  234. if adminItem.DepartmentName != "ETA试用客户" {
  235. return
  236. }
  237. var ur etaTrialService.EtaTrialUserReq
  238. ur.Mobile = adminItem.Mobile
  239. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  240. }()
  241. }
  242. // 新增操作日志
  243. {
  244. edbLog := new(data_manage.EdbInfoLog)
  245. edbLog.EdbInfoId = edbInfo.EdbInfoId
  246. edbLog.SourceName = edbInfo.SourceName
  247. edbLog.Source = edbInfo.Source
  248. edbLog.EdbCode = edbInfo.EdbCode
  249. edbLog.EdbName = edbInfo.EdbName
  250. edbLog.ClassifyId = edbInfo.ClassifyId
  251. edbLog.SysUserId = sysUser.AdminId
  252. edbLog.SysUserRealName = sysUser.RealName
  253. edbLog.CreateTime = time.Now()
  254. edbLog.Content = string(this.Ctx.Input.RequestBody)
  255. edbLog.Status = "新增指标"
  256. edbLog.Method = this.Ctx.Input.URI()
  257. go data_manage.AddEdbInfoLog(edbLog)
  258. }
  259. }
  260. br.Msg = "操作成功"
  261. br.Ret = 200
  262. br.Success = true
  263. br.IsAddLog = true
  264. }
  265. // NameCheck
  266. // @Title 重名校验
  267. // @Description 批量新增
  268. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  269. // @Success 200 string "操作成功"
  270. // @router /bloomberg_source/name_check [post]
  271. func (this *BloombergDataController) NameCheck() {
  272. br := new(models.BaseResponse).Init()
  273. defer func() {
  274. if br.ErrMsg == "" {
  275. br.IsSendEmail = false
  276. }
  277. this.Data["json"] = br
  278. this.ServeJSON()
  279. }()
  280. sysUser := this.SysUser
  281. if sysUser == nil {
  282. br.Msg = "请登录"
  283. br.ErrMsg = "请登录,SysUser Is Empty"
  284. br.Ret = 408
  285. return
  286. }
  287. var req []*data_manage.AddEdbInfoReq
  288. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  289. br.Msg = "参数解析异常!"
  290. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  291. return
  292. }
  293. if len(req) == 0 {
  294. br.Msg = "请选择指标"
  295. return
  296. }
  297. type NameCheckResult struct {
  298. EdbCode string
  299. EdbName string
  300. Exist bool
  301. }
  302. indexNames := make([]string, 0)
  303. resp := make([]*NameCheckResult, 0)
  304. for _, v := range req {
  305. v.EdbCode = strings.TrimSpace(v.EdbCode)
  306. if v.EdbCode == "" {
  307. br.Msg = "指标ID不可为空"
  308. return
  309. }
  310. v.EdbName = strings.TrimSpace(v.EdbName)
  311. if v.EdbName == "" {
  312. br.Msg = "请输入指标名称"
  313. return
  314. }
  315. indexNames = append(indexNames, v.EdbName)
  316. resp = append(resp, &NameCheckResult{
  317. EdbCode: v.EdbCode,
  318. EdbName: v.EdbName,
  319. })
  320. }
  321. // 重名校验
  322. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames)
  323. if e != nil {
  324. br.Msg = "操作失败"
  325. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  326. return
  327. }
  328. nameExists := make(map[string]bool)
  329. for _, v := range edbList {
  330. nameExists[v.EdbName] = true
  331. }
  332. if len(nameExists) > 0 {
  333. for _, v := range resp {
  334. v.Exist = nameExists[v.EdbName]
  335. }
  336. }
  337. br.Data = resp
  338. br.Msg = "操作成功"
  339. br.Ret = 200
  340. br.Success = true
  341. }