chart_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package media
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/es"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "strconv"
  7. )
  8. const (
  9. ChartESColumn = "chartName"
  10. ChartESRangeColumn = "chartInfoId"
  11. ChartEsColumn = "chartName"
  12. )
  13. // func SearchMediaList(_ string, key string, mediaIds []int, from int, size int, max int64) (medias []MediaDTO, err error) {
  14. // //同步es
  15. // var docIds []string
  16. // for _, id := range mediaIds {
  17. // docIds = append(docIds, strconv.Itoa(id))
  18. // }
  19. // sorts := append(sortField, "publishedTime:desc")
  20. // request := matchRangeWithDocIds(key, from, size, max, sorts, docIds)
  21. // re, err := elastic().Search(request)
  22. // if err != nil {
  23. // logger.Error("es搜索失败:%v", err)
  24. // }
  25. // hits := elastic().GetSource(re.Hits)
  26. // for _, hit := range hits {
  27. // var content map[string][]string
  28. // err = json.Unmarshal(hit.Highlight, &content)
  29. // media := MediaDTO{}
  30. // err = json.Unmarshal(hit.Source, &media)
  31. // if err != nil {
  32. // logger.Error("解析媒体数据失败:%v", err)
  33. // continue
  34. // }
  35. // media.Highlight = content[ESColumn]
  36. // media.PublishedTime = media.PublishedTime[:10]
  37. // media.MediaTitle = media.Highlight[0]
  38. // medias = append(medias, media)
  39. // }
  40. // return
  41. // }
  42. type EsChartInfo struct {
  43. ChartInfoId int
  44. ChartName string
  45. ChartImage string
  46. UniqueCode string
  47. }
  48. // 同步es
  49. func AddChartToEs(esChart EsChartInfo) bool {
  50. return elastic().InsertOrUpdate(htConfig.GetChartIndex(), esChart.ChartInfoId, esChart)
  51. }
  52. func matchRangeWithDocIdsNoLimit(key string, from int, to int, sorts []string, docIds []string) (request *es.ESQueryRequest) {
  53. req := new(es.ESQueryRequest)
  54. return req.CreateESQueryRequest(htConfig.GetChartIndex(), ChartEsColumn, key, from, to, sorts, es.RangeByConditionWithDocIdsNoLimit).WithDocs(docIds)
  55. }
  56. func SearchChartList(key string, ids []int, from int, size int) (charts []EsChartInfo, err error) {
  57. //同步es
  58. var docIds []string
  59. for _, id := range ids {
  60. docIds = append(docIds, strconv.Itoa(id))
  61. }
  62. sorts := append(sortField)
  63. request := matchRangeWithDocIdsNoLimit(key, from, size, sorts, docIds)
  64. re, err := elastic().Search(request)
  65. if err != nil {
  66. logger.Error("es搜索异常:%v", err)
  67. }
  68. hits := elastic().GetSource(re.Hits)
  69. if len(hits) == 0 {
  70. return
  71. }
  72. for _, hit := range hits {
  73. var content map[string][]string
  74. err = json.Unmarshal(hit.Highlight, &content)
  75. chart := EsChartInfo{}
  76. err = json.Unmarshal(hit.Source, &chart)
  77. if err != nil {
  78. logger.Error("解析研报数据失败:%v", err)
  79. continue
  80. }
  81. charts = append(charts, chart)
  82. }
  83. return
  84. }
  85. func UpdateChartImage(image string, id int) bool {
  86. doc, err := elastic().Get(htConfig.GetChartIndex(), id)
  87. if err != nil {
  88. return false
  89. }
  90. var chartInfo EsChartInfo
  91. err = json.Unmarshal(doc.Source, &chartInfo)
  92. if err != nil {
  93. logger.Error("es获取图表数据失败:%v", err)
  94. return false
  95. }
  96. if chartInfo.ChartInfoId == 0 {
  97. logger.Error("es获取图表数据失败,chartInfoId:%d", id)
  98. return false
  99. }
  100. chartInfo.ChartImage = image
  101. return elastic().Update(htConfig.GetChartIndex(), chartInfo.ChartInfoId, chartInfo)
  102. }
  103. func GetChartById(id int) (chartInfo EsChartInfo, err error) {
  104. doc, err := elastic().Get(htConfig.GetChartIndex(), id)
  105. if err != nil {
  106. logger.Error("es获取图表数据失败:%v", err)
  107. return
  108. }
  109. err = json.Unmarshal(doc.Source, &chartInfo)
  110. if err != nil {
  111. logger.Error("序列化json数据失败,获取图表数据失败:%v", err)
  112. return
  113. }
  114. return
  115. }