chart_collect.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_forum_hub/models/chart_collect"
  6. "eta/eta_forum_hub/services/alarm_msg"
  7. "eta/eta_forum_hub/utils"
  8. "fmt"
  9. )
  10. // SearchChartCollectData 查询es中的收藏图表数据
  11. func SearchChartCollectData(indexName, keywordStr string, userId int, permissionChartClassifyIdList []int, collectClassifyIds []string, adminIdsSlice []int, from, size int) (list []*chart_collect.ChartCollectEsItem, total int64, err error) {
  12. list = make([]*chart_collect.ChartCollectEsItem, 0)
  13. defer func() {
  14. if err != nil {
  15. utils.FileLog.Info("SearchChartCollectData Err:" + err.Error())
  16. go alarm_msg.SendAlarmMsg("SearchChartCollectData Err:"+err.Error(), 3)
  17. }
  18. }()
  19. client := utils.EsClient
  20. mustMap := make([]interface{}, 0)
  21. shouldItemMap := make([]interface{}, 0)
  22. //指标来源
  23. if userId > 0 {
  24. mustMap = append(mustMap, map[string]interface{}{
  25. "term": map[string]interface{}{
  26. "UserId": userId,
  27. },
  28. })
  29. }
  30. // permissionChartClassifyIdList 有权限的分类
  31. if len(permissionChartClassifyIdList) > 0 {
  32. mustMap = append(mustMap, map[string]interface{}{
  33. "terms": map[string]interface{}{
  34. "ChartClassifyId": permissionChartClassifyIdList,
  35. },
  36. })
  37. }
  38. if keywordStr != "" {
  39. mustMap = append(mustMap, map[string]interface{}{
  40. "match_phrase": map[string]interface{}{
  41. "ChartName": keywordStr,
  42. },
  43. })
  44. }
  45. if len(adminIdsSlice) > 0 {
  46. mustMap = append(mustMap, map[string]interface{}{
  47. "terms": map[string]interface{}{
  48. "SysUserId": adminIdsSlice,
  49. },
  50. })
  51. }
  52. if len(collectClassifyIds) > 0 {
  53. for _, cid := range collectClassifyIds {
  54. shouldItemMap = append(shouldItemMap, map[string]interface{}{
  55. "match_phrase": map[string]interface{}{
  56. "CollectClassifyIds": cid,
  57. },
  58. })
  59. }
  60. }
  61. boolMap := map[string]interface{}{
  62. "must": mustMap,
  63. }
  64. if len(shouldItemMap) > 0 {
  65. boolMap["should"] = shouldItemMap
  66. boolMap["minimum_should_match"] = 1
  67. }
  68. queryMap := map[string]interface{}{
  69. "query": map[string]interface{}{
  70. "bool": boolMap,
  71. },
  72. }
  73. jsonBytes, _ := json.Marshal(queryMap)
  74. fmt.Println(string(jsonBytes))
  75. //根据条件数量统计
  76. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  77. total, err = requestTotalHits.Do(context.Background())
  78. if err != nil {
  79. return
  80. }
  81. // 分页查询
  82. queryMap["from"] = from
  83. queryMap["size"] = size
  84. queryMap["sort"] = []map[string]interface{}{
  85. map[string]interface{}{
  86. "CollectTime": map[string]interface{}{
  87. "order": "desc",
  88. },
  89. },
  90. }
  91. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  92. searchMap := make(map[string]string)
  93. searchResp, err := request.Do(context.Background())
  94. if err != nil {
  95. return
  96. }
  97. fmt.Println(searchResp)
  98. fmt.Println(searchResp.Status)
  99. if searchResp.Status != 0 {
  100. return
  101. }
  102. if searchResp.Hits != nil {
  103. for _, v := range searchResp.Hits.Hits {
  104. if _, ok := searchMap[v.Id]; !ok {
  105. itemJson, tmpErr := v.Source.MarshalJSON()
  106. if tmpErr != nil {
  107. err = tmpErr
  108. fmt.Println("movieJson err:", err)
  109. return
  110. }
  111. chartInfoItem := new(chart_collect.ChartCollectEsItem)
  112. tmpErr = json.Unmarshal(itemJson, &chartInfoItem)
  113. if err != nil {
  114. fmt.Println("json.Unmarshal chartInfoJson err:", err)
  115. err = tmpErr
  116. return
  117. }
  118. if len(v.Highlight["ChartName"]) > 0 {
  119. chartInfoItem.ChartName = v.Highlight["ChartName"][0]
  120. }
  121. list = append(list, chartInfoItem)
  122. searchMap[v.Id] = v.Id
  123. }
  124. }
  125. }
  126. //for _, v := range result {
  127. // fmt.Println(v)
  128. //}
  129. return
  130. }