data_source.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package data_source
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/data_manage"
  5. dataSourceModel "eta/eta_api/models/data_source"
  6. "eta/eta_api/services/elastic"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. // SearchByEs
  12. // @Title ES搜索
  13. // @Description ES搜索
  14. // @Param PageSize query int true "每页数据条数"
  15. // @Param CurrentIndex query int true "当前页页码,从1开始"
  16. // @Param Keyword query string true "搜索关键词"
  17. // @Param Source query int true "数据源"
  18. // @Success 200 {object} response.ExcelListResp
  19. // @router /common/search_by_es [get]
  20. func (c *DataSourceController) SearchByEs() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. if br.ErrMsg == "" {
  24. br.IsSendEmail = false
  25. }
  26. c.Data["json"] = br
  27. c.ServeJSON()
  28. }()
  29. sysUser := c.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. pageSize, _ := c.GetInt("PageSize")
  37. currentIndex, _ := c.GetInt("CurrentIndex")
  38. keyword := c.GetString("KeyWord")
  39. if keyword == `` {
  40. keyword = c.GetString("Keyword")
  41. }
  42. source, _ := c.GetInt("Source")
  43. if source <= 0 {
  44. br.Msg = "来源有误"
  45. br.ErrMsg = fmt.Sprintf("数据来源有误, Source: %d", source)
  46. return
  47. }
  48. subSource, _ := c.GetInt("SubSource")
  49. // 以下为兼容各旧接口的额外传参,不为空时修改Resp对应的Key
  50. primaryIdKey := c.GetString("PrimaryIdKey")
  51. indexNameKey := c.GetString("IndexNameKey")
  52. classifyIdKey := c.GetString("ClassifyIdKey")
  53. var total, startSize int
  54. if pageSize <= 0 {
  55. pageSize = utils.PageSize15
  56. }
  57. if currentIndex <= 0 {
  58. currentIndex = 1
  59. }
  60. startSize = paging.StartIndex(currentIndex, pageSize)
  61. // es搜索
  62. t, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, keyword, source, subSource, []int{}, []int{}, []string{}, startSize, pageSize)
  63. if e != nil {
  64. br.Msg = "获取失败"
  65. br.ErrMsg = fmt.Sprintf("ES-搜索数据源列表失败, %v", e)
  66. return
  67. }
  68. total = int(t)
  69. listMap := make([]map[string]interface{}, 0)
  70. for _, v := range list {
  71. // 由于start_date、end_date和latest_value字段不全的历史遗留问题,这里查出来并补充进ES里面去
  72. if v.StartDate == "" || v.EndDate == "" {
  73. minMax, e := dataSourceModel.GetBaseIndexDataMinMax(v.Source, v.SubSource, v.IndexCode)
  74. if e != nil && e.Error() != utils.ErrNoRow() {
  75. br.Msg = "获取失败"
  76. br.ErrMsg = fmt.Sprintf("获取指标开始结束时间失败, %v", e)
  77. return
  78. }
  79. if minMax != nil {
  80. v.StartDate = minMax.MinDate
  81. v.EndDate = minMax.MaxDate
  82. v.LatestValue = minMax.LatestValue
  83. // 写入ES更新队列
  84. if e := utils.Rc.LPush(utils.CACHE_DATA_SOURCE_ES_HANDLE, v); e != nil {
  85. br.Msg = "获取失败"
  86. br.ErrMsg = fmt.Sprintf("写入ES更新队列失败, Source: %d, IndexCode: %s, err: %v", v.Source, v.IndexCode, e)
  87. return
  88. }
  89. }
  90. }
  91. // 转换成map返回
  92. listMap = append(listMap, v.ToMap(primaryIdKey, indexNameKey, classifyIdKey))
  93. }
  94. // 美国农业部(多一个ParentClassifyId字段)
  95. if source == utils.DATA_SOURCE_USDA_FAS {
  96. // 父级分类ID
  97. classifyIds := make([]int, 0)
  98. for _, v := range list {
  99. classifyIds = append(classifyIds, v.ClassifyId)
  100. }
  101. classifyList, e := data_manage.GetBaseFromUsdaFasClassifyByIds(classifyIds)
  102. if e != nil {
  103. br.Msg = "获取失败"
  104. br.ErrMsg = fmt.Sprintf("获取美国农业部分类失败, %v", e)
  105. return
  106. }
  107. classifyMap := make(map[int]int)
  108. for _, v := range classifyList {
  109. classifyMap[v.ClassifyId] = v.ParentId
  110. }
  111. for _, v := range listMap {
  112. id, ok := v["ClassifyId"].(int)
  113. if !ok {
  114. v["ParentClassifyId"] = 0
  115. continue
  116. }
  117. v["ParentClassifyId"] = classifyMap[id]
  118. }
  119. }
  120. // 煤炭江湖(多一个Area字段)
  121. if source == utils.DATA_SOURCE_MTJH {
  122. cond := ``
  123. pars := make([]interface{}, 0)
  124. indexes, e := data_manage.GetMtjhItemsByCondition(cond, pars)
  125. if e != nil {
  126. br.Msg = "获取失败"
  127. br.ErrMsg = fmt.Sprintf("获取煤炭江湖指标失败, %v", e)
  128. return
  129. }
  130. indexArea := make(map[string]string)
  131. for _, v := range indexes {
  132. indexArea[v.IndexCode] = v.Area
  133. }
  134. for _, v := range listMap {
  135. code, ok := v["IndexCode"].(string)
  136. if !ok {
  137. v["Area"] = ""
  138. continue
  139. }
  140. v["Area"] = indexArea[code]
  141. }
  142. }
  143. page := paging.GetPaging(currentIndex, pageSize, total)
  144. resp := dataSourceModel.SearchDataSourceResp{
  145. Paging: page,
  146. List: listMap,
  147. }
  148. br.Data = resp
  149. br.Ret = 200
  150. br.Success = true
  151. br.Msg = "获取成功"
  152. }