chart_service.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package media
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. "eta/eta_mini_ht_api/common/component/es"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "eta/eta_mini_ht_api/common/contants"
  8. chartService "eta/eta_mini_ht_api/domian/media"
  9. "strconv"
  10. )
  11. type ChartInfo struct {
  12. ChartInfoId int
  13. ChartName string
  14. ChartImage string
  15. UniqueCode string
  16. }
  17. const (
  18. ESColumn = "chartName"
  19. )
  20. var (
  21. sortField = []string{"_score:desc"}
  22. htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
  23. )
  24. func elastic() *es.ESClient {
  25. return es.GetInstance()
  26. }
  27. func AddChartToEs(chartInfo ChartInfo) {
  28. success := chartService.AddChartToEs(chartService.EsChartInfo{
  29. ChartInfoId: chartInfo.ChartInfoId,
  30. ChartName: chartInfo.ChartName,
  31. ChartImage: chartInfo.ChartImage,
  32. UniqueCode: chartInfo.UniqueCode,
  33. })
  34. if !success {
  35. logger.Error("新增图表到ES失败, chartInfoId:%d,uniqueCode:%s", chartInfo.ChartInfoId, chartInfo.UniqueCode)
  36. }
  37. }
  38. func SearchChartList(key string, ids []int, from int, size int) (charts []ChartInfo, err error) {
  39. //同步es
  40. var docIds []string
  41. for _, id := range ids {
  42. docIds = append(docIds, strconv.Itoa(id))
  43. }
  44. sorts := append(sortField)
  45. request := matchRangeWithDocIds(key, from, size, sorts, docIds)
  46. re, err := elastic().Search(request)
  47. if err != nil {
  48. logger.Error("es搜索异常:%v", err)
  49. }
  50. hits := elastic().GetSource(re.Hits)
  51. if len(hits) == 0 {
  52. return
  53. }
  54. for _, hit := range hits {
  55. var content map[string][]string
  56. err = json.Unmarshal(hit.Highlight, &content)
  57. chart := ChartInfo{}
  58. err = json.Unmarshal(hit.Source, &chart)
  59. if err != nil {
  60. logger.Error("解析研报数据失败:%v", err)
  61. continue
  62. }
  63. charts = append(charts, chart)
  64. }
  65. return
  66. }
  67. func matchRangeWithDocIds(key string, from int, to int, sorts []string, docIds []string) (request *es.ESQueryRequest) {
  68. req := new(es.ESQueryRequest)
  69. return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, from, to, sorts, es.RangeByConditionWithDocIds).ByCondition("status", "PUBLISH").WithDocs(docIds)
  70. }
  71. func UpdateChartImage(image string, id int) bool {
  72. return chartService.UpdateChartImage(image, id)
  73. }
  74. func GetChartById(chartInfoId int) (info ChartInfo, err error) {
  75. chart, err := chartService.GetChartById(chartInfoId)
  76. if err != nil {
  77. return
  78. }
  79. info = convertChartInfo(chart)
  80. return
  81. }
  82. func convertChartInfo(chart chartService.EsChartInfo) ChartInfo {
  83. return ChartInfo{
  84. ChartInfoId: chart.ChartInfoId,
  85. ChartName: chart.ChartName,
  86. ChartImage: chart.ChartImage,
  87. UniqueCode: chart.UniqueCode,
  88. }
  89. }