123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package media
- import (
- "encoding/json"
- "eta/eta_mini_ht_api/common/component/config"
- "eta/eta_mini_ht_api/common/component/es"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/contants"
- chartService "eta/eta_mini_ht_api/domian/media"
- "strconv"
- )
- type ChartInfo struct {
- ChartInfoId int
- ChartName string
- ChartImage string
- UniqueCode string
- }
- const (
- ESColumn = "chartName"
- )
- var (
- sortField = []string{"_score:desc"}
- htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
- )
- func elastic() *es.ESClient {
- return es.GetInstance()
- }
- 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)
- }
- }
- func SearchChartList(key string, ids []int, from int, size int) (charts []ChartInfo, err error) {
- //同步es
- var docIds []string
- for _, id := range ids {
- docIds = append(docIds, strconv.Itoa(id))
- }
- sorts := append(sortField)
- request := matchRangeWithDocIds(key, from, size, sorts, docIds)
- re, err := elastic().Search(request)
- if err != nil {
- logger.Error("es搜索异常:%v", err)
- }
- hits := elastic().GetSource(re.Hits)
- if len(hits) == 0 {
- return
- }
- for _, hit := range hits {
- var content map[string][]string
- err = json.Unmarshal(hit.Highlight, &content)
- chart := ChartInfo{}
- err = json.Unmarshal(hit.Source, &chart)
- if err != nil {
- logger.Error("解析研报数据失败:%v", err)
- continue
- }
- charts = append(charts, chart)
- }
- return
- }
- func matchRangeWithDocIds(key string, from int, to int, sorts []string, docIds []string) (request *es.ESQueryRequest) {
- req := new(es.ESQueryRequest)
- return req.CreateESQueryRequest(htConfig.GetReportIndex(), ESColumn, key, from, to, sorts, es.RangeByConditionWithDocIds).ByCondition("status", "PUBLISH").WithDocs(docIds)
- }
- func UpdateChartImage(image string, id int) bool {
- return chartService.UpdateChartImage(image, id)
- }
- func GetChartById(chartInfoId int) (info ChartInfo, err error) {
- chart, err := chartService.GetChartById(chartInfoId)
- if err != nil {
- return
- }
- info = convertChartInfo(chart)
- return
- }
- func convertChartInfo(chart chartService.EsChartInfo) ChartInfo {
- return ChartInfo{
- ChartInfoId: chart.ChartInfoId,
- ChartName: chart.ChartName,
- ChartImage: chart.ChartImage,
- UniqueCode: chart.UniqueCode,
- }
- }
|