123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package home
- import (
- "encoding/json"
- "eta/eta_mini_ht_api/common/component/config"
- "eta/eta_mini_ht_api/common/component/es"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/contants"
- "eta/eta_mini_ht_api/common/utils/page"
- mediaDomian "eta/eta_mini_ht_api/domian/media"
- reportDomain "eta/eta_mini_ht_api/domian/report"
- mediaService "eta/eta_mini_ht_api/service/media"
- reportService "eta/eta_mini_ht_api/service/report"
- userService "eta/eta_mini_ht_api/service/user"
- "strings"
- "sync"
- "time"
- )
- var (
- sortField = []string{"_score:desc"}
- htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
- )
- const (
- ReportColumn = "title"
- MediaColumn = "mediaName"
- )
- type ResultType string
- const (
- MediaResultType ResultType = "media"
- ReportResultType ResultType = "report"
- )
- type HomeSearch struct {
- ResultType ResultType
- Media *mediaDomian.MediaDTO
- Report *reportDomain.ReportDTO
- }
- func elastic() *es.ESClient {
- return es.GetInstance()
- }
- func Search(key string, pageInfo page.PageInfo, login bool, templateUserId int) (result []HomeSearch, total int64, err error) {
- //同步es
- offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
- sorts := append(sortField, "publishedTime:desc")
- deadLine := time.Unix(pageInfo.TimeStamp, 0).Format(time.DateTime)
- request := homeSearch(key, offset, pageInfo.PageSize, sorts, deadLine)
- re, err := elastic().Search(request)
- if err != nil {
- logger.Error("es搜索异常:%v", err)
- }
- hits := elastic().GetSource(re.Hits)
- if len(hits) == 0 {
- return
- }
- for _, hit := range hits {
- var content map[string][]string
- err = json.Unmarshal(hit.Highlight, &content)
- searchResult := HomeSearch{}
- err = json.Unmarshal(hit.Source, &searchResult)
- if err != nil {
- logger.Error("解析研报数据失败:%v", err)
- continue
- }
- switch hit.Index {
- case htConfig.GetMediaIndex():
- searchResult = HomeSearch{
- ResultType: MediaResultType,
- Media: transMedia(hit, content),
- }
- case htConfig.GetReportIndex():
- searchResult = HomeSearch{
- ResultType: ReportResultType,
- Report: transReport(hit, content),
- }
- }
- result = append(result, searchResult)
- }
- _, mappingRiskLevel, userRiskStatus, err := userService.GetRiskLevelPermissionList(nil, login, templateUserId)
- if err != nil {
- logger.Error("获取用户信息失败:%v", err)
- }
- var wg sync.WaitGroup
- wg.Add(len(result))
- for i := 0; i < len(result); i++ {
- go func(searchResult *HomeSearch) {
- defer wg.Done()
- switch searchResult.ResultType {
- case ReportResultType:
- searchResult.Report, _ = reportService.DealReportInfo(searchResult.Report, login, templateUserId, mappingRiskLevel, userRiskStatus)
- case MediaResultType:
- searchResult.Media, _ = mediaService.DealMediaInfo(searchResult.Media, login, templateUserId, mappingRiskLevel, userRiskStatus)
- }
- }(&result[i])
- }
- wg.Wait()
- return
- }
- func transMedia(hit es.Hit, content map[string][]string) (media *mediaDomian.MediaDTO) {
- err := json.Unmarshal(hit.Source, &media)
- if err != nil {
- logger.Error("解析媒体数据失败:%v", err)
- return
- }
- media.Highlight = content[MediaColumn]
- media.PublishedTime = media.PublishedTime[:10]
- media.MediaTitle = media.Highlight[0]
- media.Type = media.MediaType
- return
- }
- func transReport(hit es.Hit, content map[string][]string) (report *reportDomain.ReportDTO) {
- err := json.Unmarshal(hit.Source, &report)
- if err != nil {
- logger.Error("解析研报数据失败:%v", err)
- return
- }
- report.Highlight = content[ReportColumn]
- report.Title = report.Highlight[0]
- report.PublishedTime = report.PublishedTime[:10]
- report.Type = "report"
- return
- }
- func homeSearch(key string, from int, to int, sorts []string, publishedTime string) (request *es.ESQueryRequest) {
- req := new(es.ESQueryRequest)
- return req.CreateESQueryRequest(strings.Join([]string{htConfig.GetReportIndex(), htConfig.GetMediaIndex()}, ","), "", key, from, to, sorts, es.HomeSearch).ByCondition("status", "PUBLISH").Before(publishedTime, "publishedTime")
- }
|