Browse Source

收藏图表

kobe6258 4 months ago
parent
commit
eb9d80ae37

+ 5 - 0
common/component/config/ht_biz_config.go

@@ -6,6 +6,7 @@ import "eta/eta_mini_ht_api/common/contants"
 type HTOpts struct {
 	ReportIndex       string
 	MediaIndex        string
+	ChartIndex        string
 	Encode            string
 	DesCode           string
 	Task              string
@@ -32,6 +33,9 @@ func (e *HTBizConfig) GetReportIndex() string {
 func (e *HTBizConfig) GetMediaIndex() string {
 	return e.opts.MediaIndex
 }
+func (e *HTBizConfig) GetChartIndex() string {
+	return e.opts.ChartIndex
+}
 
 func (e *HTBizConfig) GetAccountInfoUrl() string {
 	return e.opts.AccountInfoUrl
@@ -84,6 +88,7 @@ func (e *HTBizConfig) InitConfig() {
 	opts := HTOpts{
 		ReportIndex:       e.GetString("es_report_index"),
 		MediaIndex:        e.GetString("es_media_index"),
+		ChartIndex:        e.GetString("es_chart_index"),
 		Encode:            e.GetString("response.encode"),
 		DesCode:           e.GetString("response.des_code"),
 		Task:              e.GetString("task"),

+ 7 - 0
common/component/es/es.go

@@ -640,6 +640,13 @@ func (es *ESClient) Update(indexName string, id int, doc interface{}) bool {
 	fmt.Printf("%s\n", string(body))
 	return true
 }
+func (es *ESClient) InsertOrUpdate(indexName string, id int, doc interface{}) (success bool) {
+	if exist, existErr := es.Exist(indexName, id); existErr == nil && exist {
+		return es.Update(indexName, id, doc)
+	} else {
+		return es.CreateDocument(indexName, id, doc)
+	}
+}
 
 // Delete *
 // 删除

+ 16 - 0
controllers/user/bookmark_controller.go

@@ -4,6 +4,7 @@ import (
 	"eta/eta_mini_ht_api/common/component/cache"
 	"eta/eta_mini_ht_api/common/exception"
 	"eta/eta_mini_ht_api/controllers"
+	chartService "eta/eta_mini_ht_api/service/media"
 	"eta/eta_mini_ht_api/service/user"
 	userService "eta/eta_mini_ht_api/service/user"
 )
@@ -14,6 +15,11 @@ type BookMarkController struct {
 	redis *cache.RedisCache
 }
 
+const (
+	Chart  = "chart"
+	Report = "report"
+)
+
 func (u *BookMarkController) Prepare() {
 	u.redis = cache.GetInstance()
 }
@@ -24,6 +30,7 @@ type BookMarkReq struct {
 	ChartImage  string `json:"chartImage"`
 	ChartInfoId int    `json:"chartInfoId"`
 	ChartName   string `json:"chartName"`
+	UniqueCode  string `json:"uniqueCode"`
 }
 
 // BookMark  收藏
@@ -54,6 +61,15 @@ func (u *BookMarkController) BookMark() {
 			u.FailedResult("收藏失败", result)
 			return
 		}
+		//将图表加入es
+		if bookMark.SourceType == Chart {
+			chartService.AddChartToEs(chartService.ChartInfo{
+				ChartImage:  bookMark.ChartImage,
+				ChartInfoId: bookMark.ChartInfoId,
+				ChartName:   bookMark.ChartName,
+				UniqueCode:  bookMark.UniqueCode,
+			})
+		}
 		u.SuccessResult("收藏成功", nil, result)
 		return
 	})

+ 47 - 0
domian/media/chart_service.go

@@ -0,0 +1,47 @@
+package media
+
+const (
+	ChartESColumn      = "chartName"
+	ChartESRangeColumn = "chartInfoId"
+)
+
+//	func SearchMediaList(_ string, key string, mediaIds []int, from int, size int, max int64) (medias []MediaDTO, err error) {
+//		//同步es
+//		var docIds []string
+//		for _, id := range mediaIds {
+//			docIds = append(docIds, strconv.Itoa(id))
+//		}
+//		sorts := append(sortField, "publishedTime:desc")
+//		request := matchRangeWithDocIds(key, from, size, max, sorts, docIds)
+//		re, err := elastic().Search(request)
+//		if err != nil {
+//			logger.Error("es搜索失败:%v", err)
+//		}
+//		hits := elastic().GetSource(re.Hits)
+//		for _, hit := range hits {
+//			var content map[string][]string
+//			err = json.Unmarshal(hit.Highlight, &content)
+//			media := MediaDTO{}
+//			err = json.Unmarshal(hit.Source, &media)
+//			if err != nil {
+//				logger.Error("解析媒体数据失败:%v", err)
+//				continue
+//			}
+//			media.Highlight = content[ESColumn]
+//			media.PublishedTime = media.PublishedTime[:10]
+//			media.MediaTitle = media.Highlight[0]
+//			medias = append(medias, media)
+//		}
+//		return
+//	}
+type EsChartInfo struct {
+	ChartInfoId int
+	ChartName   string
+	ChartImage  string
+	UniqueCode  string
+}
+
+// 同步es
+func AddChartToEs(esChart EsChartInfo) bool {
+	return elastic().InsertOrUpdate(htConfig.GetChartIndex(), esChart.ChartInfoId, esChart)
+}

+ 25 - 0
service/media/chart_service.go

@@ -0,0 +1,25 @@
+package media
+
+import (
+	logger "eta/eta_mini_ht_api/common/component/log"
+	chartService "eta/eta_mini_ht_api/domian/media"
+)
+
+type ChartInfo struct {
+	ChartInfoId int
+	ChartName   string
+	ChartImage  string
+	UniqueCode  string
+}
+
+func AddChartToEs(chartInfo ChartInfo) {
+	success := chartService.AddChartToEs(chartService.EsChartInfo{
+		ChartInfoId: chartInfo.ChartInfoId,
+		ChartName:   chartInfo.ChartName,
+		ChartImage:  chartInfo.ChartImage,
+		UniqueCode:  chartInfo.UniqueCode,
+	})
+	if !success {
+		logger.Error("新增图表到ES失败, chartInfoId:%d,uniqueCode:%s", chartInfo.ChartInfoId, chartInfo.UniqueCode)
+	}
+}