report.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mini_bridge/models"
  6. "eta/eta_mini_bridge/models/response"
  7. "eta/eta_mini_bridge/services/elastic"
  8. "eta/eta_mini_bridge/utils"
  9. "html"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/rdlucklib/rdluck_tools/paging"
  14. )
  15. // addAliasToKeyword 品种别名
  16. func addAliasToKeyword(keyword string) string {
  17. if keyword == "" {
  18. return ""
  19. }
  20. keywordArr := make([]string, 0)
  21. keywordArr = append(keywordArr, keyword)
  22. if strings.Contains(keyword, "沥青") || strings.Contains(keyword, "BU") {
  23. keywordArr = append(keywordArr, "沥青", "BU")
  24. }
  25. if strings.Contains(keyword, "MEG") || strings.Contains(keyword, "EG") || strings.Contains(keyword, "乙二醇") {
  26. keywordArr = append(keywordArr, "MEG", "EG", "乙二醇")
  27. }
  28. if strings.Contains(keyword, "聚酯") || strings.Contains(keyword, "长丝") || strings.Contains(keyword, "短纤") || strings.Contains(keyword, "瓶片") {
  29. keywordArr = append(keywordArr, "聚酯", "长丝", "短纤", "瓶片")
  30. }
  31. if strings.Contains(keyword, "纯苯") || strings.Contains(keyword, "苯乙烯") || strings.Contains(keyword, "EB") {
  32. keywordArr = append(keywordArr, "纯苯", "苯乙烯", "EB")
  33. }
  34. if strings.Contains(keyword, "甲醇") || strings.Contains(keyword, "MA") {
  35. keywordArr = append(keywordArr, "甲醇", "MA")
  36. }
  37. if strings.Contains(keyword, "橡胶") || strings.Contains(keyword, "RU") {
  38. keywordArr = append(keywordArr, "橡胶", "RU")
  39. }
  40. if strings.Contains(keyword, "聚乙烯") || strings.Contains(keyword, "PP") || strings.Contains(keyword, "PE") {
  41. keywordArr = append(keywordArr, "聚乙烯", "PP", "PE")
  42. }
  43. if strings.Contains(keyword, "玻璃") || strings.Contains(keyword, "纯碱") || strings.Contains(keyword, "FG") || strings.Contains(keyword, "SA") {
  44. keywordArr = append(keywordArr, "玻璃", "纯碱", "FG", "SA")
  45. }
  46. keyword = strings.Join(keywordArr, ",")
  47. return keyword
  48. }
  49. func SearchReport(keyWord string, pageIndex, pageSize int) (ret *response.ReportSearchResp, err error, errMsg string) {
  50. //查询正常状态的分类
  51. keyWord = addAliasToKeyword(keyWord)
  52. searchResp, total, err := elastic.SearchReportV2(keyWord, []int{}, []int{}, pageIndex, pageSize)
  53. if err != nil {
  54. errMsg = err.Error()
  55. err = errors.New("查询失败")
  56. return
  57. }
  58. var reportList []*models.ReportCollectListItem
  59. if searchResp.Hits != nil {
  60. for _, v := range searchResp.Hits.Hits {
  61. temp := new(models.ReportCollectListItem)
  62. itemJson, tmpErr := v.Source.MarshalJSON()
  63. if tmpErr != nil {
  64. errMsg = tmpErr.Error()
  65. err = errors.New("解析出错")
  66. return
  67. }
  68. reportItem := new(response.EsReportItem)
  69. tmpErr = json.Unmarshal(itemJson, &reportItem)
  70. if tmpErr != nil {
  71. errMsg = tmpErr.Error()
  72. err = errors.New("解析json出错")
  73. return
  74. }
  75. if reportItem.ReportPdfId != 0 {
  76. temp.ReportId = reportItem.ReportPdfId
  77. temp.ReportType = utils.ReportTypePdf
  78. temp.Stage = strconv.Itoa(reportItem.Stage)
  79. } else {
  80. temp.ReportId = reportItem.ReportId
  81. temp.ReportType = utils.ReportTypeEta
  82. temp.Stage = reportItem.StageStr
  83. temp.ContentSub = reportItem.BodyContent
  84. }
  85. temp.PdfUrl = reportItem.PdfUrl
  86. temp.ReportChapterId = reportItem.ReportChapterId
  87. temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
  88. temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
  89. temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
  90. temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
  91. temp.PublishTime, err = time.ParseInLocation(utils.FormatDateTime, reportItem.PublishTime, time.Local)
  92. temp.Title = reportItem.Title
  93. temp.Abstract = reportItem.Abstract
  94. temp.Author = reportItem.Author
  95. if len(v.Highlight["Title"]) > 0 {
  96. temp.Title = v.Highlight["Title"][0]
  97. }
  98. if len(v.Highlight["BodyContent"]) > 0 {
  99. temp.ContentSub = v.Highlight["BodyContent"][0]
  100. }
  101. //if len(v.Highlight["Categories"]) > 0 {
  102. // if temp.ClassifyNameSecond != "" {
  103. // temp.ClassifyNameSecond = "<span style=\"color:#E3B377\">"+temp.ClassifyNameSecond+"</span>"
  104. // }
  105. // if temp.ClassifyNameFirst != "" {
  106. // temp.ClassifyNameFirst = "<span style=\"color:#E3B377\">"+temp.ClassifyNameFirst+"</span>"
  107. // }
  108. //}
  109. temp.ContentSub = GetReportContentSub(temp.ContentSub, false)
  110. reportList = append(reportList, temp)
  111. }
  112. }
  113. ret = new(response.ReportSearchResp)
  114. ret.List = reportList
  115. ret.Paging = paging.GetPaging(pageIndex, pageSize, int(total))
  116. return
  117. }
  118. // GetReportContentSub 特殊处理contentSub
  119. func GetReportContentSub(content string, scapeFlag bool) (contentSub string) {
  120. if scapeFlag {
  121. content = html.UnescapeString(content)
  122. content = utils.TrimHtml(content) //只展示纯文本
  123. }
  124. contentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">" + content + "</div>"
  125. return
  126. }