home_service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package home
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. "eta/eta_mini_ht_api/common/component/es"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "eta/eta_mini_ht_api/common/contants"
  8. "eta/eta_mini_ht_api/common/utils/page"
  9. mediaDomian "eta/eta_mini_ht_api/domian/media"
  10. reportDomain "eta/eta_mini_ht_api/domian/report"
  11. mediaService "eta/eta_mini_ht_api/service/media"
  12. reportService "eta/eta_mini_ht_api/service/report"
  13. userService "eta/eta_mini_ht_api/service/user"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. var (
  19. sortField = []string{"_score:desc"}
  20. htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
  21. )
  22. const (
  23. ReportColumn = "title"
  24. MediaColumn = "mediaName"
  25. )
  26. type ResultType string
  27. const (
  28. MediaResultType ResultType = "media"
  29. ReportResultType ResultType = "report"
  30. )
  31. type HomeSearch struct {
  32. ResultType ResultType
  33. Media *mediaDomian.MediaDTO
  34. Report *reportDomain.ReportDTO
  35. }
  36. func elastic() *es.ESClient {
  37. return es.GetInstance()
  38. }
  39. func Search(key string, pageInfo page.PageInfo, login bool, templateUserId int) (result []HomeSearch, total int64, err error) {
  40. //同步es
  41. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  42. sorts := append(sortField, "publishedTime:desc")
  43. deadLine := time.Unix(pageInfo.TimeStamp, 0).Format(time.DateTime)
  44. request := homeSearch(key, offset, pageInfo.PageSize, sorts, deadLine)
  45. re, err := elastic().Search(request)
  46. if err != nil {
  47. logger.Error("es搜索异常:%v", err)
  48. }
  49. hits := elastic().GetSource(re.Hits)
  50. if len(hits) == 0 {
  51. return
  52. }
  53. for _, hit := range hits {
  54. var content map[string][]string
  55. err = json.Unmarshal(hit.Highlight, &content)
  56. searchResult := HomeSearch{}
  57. err = json.Unmarshal(hit.Source, &searchResult)
  58. if err != nil {
  59. logger.Error("解析研报数据失败:%v", err)
  60. continue
  61. }
  62. switch hit.Index {
  63. case htConfig.GetMediaIndex():
  64. searchResult = HomeSearch{
  65. ResultType: MediaResultType,
  66. Media: transMedia(hit, content),
  67. }
  68. case htConfig.GetReportIndex():
  69. searchResult = HomeSearch{
  70. ResultType: ReportResultType,
  71. Report: transReport(hit, content),
  72. }
  73. }
  74. result = append(result, searchResult)
  75. }
  76. _, mappingRiskLevel, userRiskStatus, err := userService.GetRiskLevelPermissionList(nil, login, templateUserId)
  77. if err != nil {
  78. logger.Error("获取用户信息失败:%v", err)
  79. }
  80. var wg sync.WaitGroup
  81. wg.Add(len(result))
  82. for i := 0; i < len(result); i++ {
  83. go func(searchResult *HomeSearch) {
  84. defer wg.Done()
  85. switch searchResult.ResultType {
  86. case ReportResultType:
  87. searchResult.Report, _ = reportService.DealReportInfo(searchResult.Report, login, templateUserId, mappingRiskLevel, userRiskStatus)
  88. case MediaResultType:
  89. searchResult.Media, _ = mediaService.DealMediaInfo(searchResult.Media, login, templateUserId, mappingRiskLevel, userRiskStatus)
  90. }
  91. }(&result[i])
  92. }
  93. wg.Wait()
  94. return
  95. }
  96. func transMedia(hit es.Hit, content map[string][]string) (media *mediaDomian.MediaDTO) {
  97. err := json.Unmarshal(hit.Source, &media)
  98. if err != nil {
  99. logger.Error("解析媒体数据失败:%v", err)
  100. return
  101. }
  102. media.Highlight = content[MediaColumn]
  103. media.PublishedTime = media.PublishedTime[:10]
  104. media.MediaTitle = media.Highlight[0]
  105. media.Type = media.MediaType
  106. return
  107. }
  108. func transReport(hit es.Hit, content map[string][]string) (report *reportDomain.ReportDTO) {
  109. err := json.Unmarshal(hit.Source, &report)
  110. if err != nil {
  111. logger.Error("解析研报数据失败:%v", err)
  112. return
  113. }
  114. report.Highlight = content[ReportColumn]
  115. report.Title = report.Highlight[0]
  116. report.PublishedTime = report.PublishedTime[:10]
  117. report.Type = "report"
  118. return
  119. }
  120. func homeSearch(key string, from int, to int, sorts []string, publishedTime string) (request *es.ESQueryRequest) {
  121. req := new(es.ESQueryRequest)
  122. return req.CreateESQueryRequest(strings.Join([]string{htConfig.GetReportIndex(), htConfig.GetMediaIndex()}, ","), "", key, from, to, sorts, es.HomeSearch).ByCondition("status", "PUBLISH").Before(publishedTime, "publishedTime")
  123. }