123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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 = "<span style=\"color:#E3B377\">"+temp.ClassifyNameSecond+"</span>"
- // }
- // if temp.ClassifyNameFirst != "" {
- // temp.ClassifyNameFirst = "<span style=\"color:#E3B377\">"+temp.ClassifyNameFirst+"</span>"
- // }
- //}
- 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 = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">" + content + "</div>"
- return
- }
|