package services import ( "encoding/json" "errors" "eta/eta_mini_bridge/models" "eta/eta_mini_bridge/models/response" "eta/eta_mini_bridge/services/elastic" "eta/eta_mini_bridge/utils" "html" "strconv" "strings" "time" "github.com/rdlucklib/rdluck_tools/paging" ) // addAliasToKeyword 品种别名 func addAliasToKeyword(keyword string) string { if keyword == "" { return "" } keywordArr := make([]string, 0) keywordArr = append(keywordArr, keyword) if strings.Contains(keyword, "沥青") || strings.Contains(keyword, "BU") { keywordArr = append(keywordArr, "沥青", "BU") } if strings.Contains(keyword, "MEG") || strings.Contains(keyword, "EG") || strings.Contains(keyword, "乙二醇") { keywordArr = append(keywordArr, "MEG", "EG", "乙二醇") } if strings.Contains(keyword, "聚酯") || strings.Contains(keyword, "长丝") || strings.Contains(keyword, "短纤") || strings.Contains(keyword, "瓶片") { keywordArr = append(keywordArr, "聚酯", "长丝", "短纤", "瓶片") } if strings.Contains(keyword, "纯苯") || strings.Contains(keyword, "苯乙烯") || strings.Contains(keyword, "EB") { keywordArr = append(keywordArr, "纯苯", "苯乙烯", "EB") } if strings.Contains(keyword, "甲醇") || strings.Contains(keyword, "MA") { keywordArr = append(keywordArr, "甲醇", "MA") } if strings.Contains(keyword, "橡胶") || strings.Contains(keyword, "RU") { keywordArr = append(keywordArr, "橡胶", "RU") } if strings.Contains(keyword, "聚乙烯") || strings.Contains(keyword, "PP") || strings.Contains(keyword, "PE") { keywordArr = append(keywordArr, "聚乙烯", "PP", "PE") } if strings.Contains(keyword, "玻璃") || strings.Contains(keyword, "纯碱") || strings.Contains(keyword, "FG") || strings.Contains(keyword, "SA") { keywordArr = append(keywordArr, "玻璃", "纯碱", "FG", "SA") } keyword = strings.Join(keywordArr, ",") return keyword } func SearchReport(keyWord string, pageIndex, pageSize int) (ret *response.ReportSearchResp, err error, errMsg string) { //查询正常状态的分类 keyWord = addAliasToKeyword(keyWord) searchResp, total, err := elastic.SearchReportV2(keyWord, []int{}, []int{}, pageIndex, pageSize) if err != nil { errMsg = err.Error() err = errors.New("查询失败") return } var reportList []*models.ReportCollectListItem if searchResp.Hits != nil { for _, v := range searchResp.Hits.Hits { temp := new(models.ReportCollectListItem) itemJson, tmpErr := v.Source.MarshalJSON() if tmpErr != nil { errMsg = tmpErr.Error() err = errors.New("解析出错") return } reportItem := new(response.EsReportItem) tmpErr = json.Unmarshal(itemJson, &reportItem) if tmpErr != nil { errMsg = tmpErr.Error() err = errors.New("解析json出错") return } if reportItem.ReportPdfId != 0 { temp.ReportId = reportItem.ReportPdfId temp.ReportType = utils.ReportTypePdf temp.Stage = strconv.Itoa(reportItem.Stage) } else { temp.ReportId = reportItem.ReportId temp.ReportType = utils.ReportTypeEta temp.Stage = reportItem.StageStr temp.ContentSub = reportItem.BodyContent } temp.PdfUrl = reportItem.PdfUrl temp.ReportChapterId = reportItem.ReportChapterId temp.ClassifyIdFirst = reportItem.ClassifyIdFirst temp.ClassifyNameFirst = reportItem.ClassifyNameFirst temp.ClassifyIdSecond = reportItem.ClassifyIdSecond temp.ClassifyNameSecond = reportItem.ClassifyNameSecond temp.PublishTime, err = time.ParseInLocation(utils.FormatDateTime, reportItem.PublishTime, time.Local) temp.Title = reportItem.Title temp.Abstract = reportItem.Abstract temp.Author = reportItem.Author if len(v.Highlight["Title"]) > 0 { temp.Title = v.Highlight["Title"][0] } if len(v.Highlight["BodyContent"]) > 0 { temp.ContentSub = v.Highlight["BodyContent"][0] } //if len(v.Highlight["Categories"]) > 0 { // if temp.ClassifyNameSecond != "" { // temp.ClassifyNameSecond = ""+temp.ClassifyNameSecond+"" // } // if temp.ClassifyNameFirst != "" { // temp.ClassifyNameFirst = ""+temp.ClassifyNameFirst+"" // } //} temp.ContentSub = GetReportContentSub(temp.ContentSub, false) reportList = append(reportList, temp) } } ret = new(response.ReportSearchResp) ret.List = reportList ret.Paging = paging.GetPaging(pageIndex, pageSize, int(total)) return } // GetReportContentSub 特殊处理contentSub func GetReportContentSub(content string, scapeFlag bool) (contentSub string) { if scapeFlag { content = html.UnescapeString(content) content = utils.TrimHtml(content) //只展示纯文本 } contentSub = "
" + content + "
" return }