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" "eta/eta_mini_ht_api/common/utils/page" configDomain "eta/eta_mini_ht_api/domian/config" reportService "eta/eta_mini_ht_api/domian/report" "eta/eta_mini_ht_api/models" configDao "eta/eta_mini_ht_api/models/config" "eta/eta_mini_ht_api/models/image" mediaDao "eta/eta_mini_ht_api/models/media" merchantDao "eta/eta_mini_ht_api/models/merchant" "sort" "strconv" "time" ) var ( htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig) ) const ( DESC models.Order = "desc" ASC models.Order = "asc" ESColumn = "mediaName" ESRangeColumn = "mediaId" ConditionColumn = "mediaType" ) var ( sortField = []string{"_score:desc"} transProductMap = map[string]merchantDao.MerchantProductType{ "report": merchantDao.Report, "video": merchantDao.Video, "audio": merchantDao.Audio, "package": merchantDao.Package, } ) func elastic() *es.ESClient { return es.GetInstance() } type MediaDTO struct { Type string `json:"type"` MediaId int `json:"mediaId"` AuthorId int `json:"authorId"` AuthorName string `json:"authorName,omitempty"` MediaType string `json:"mediaType"` Src string `json:"src"` MediaName string `json:"mediaName"` MediaTitle string `json:"mediaTitle,omitempty"` CoverSrc string `json:"coverSrc"` CoverUrl string `json:"coverUrl"` SourceType string `json:"sourceType"` MediaPlayMilliseconds int `json:"mediaPlayMilliseconds"` PublishedTime string `json:"publishedTime"` PermissionIDs string `json:"permissionIds"` PermissionNames []string `json:"permissionNames,omitempty"` Highlight []string `json:"highlight,omitempty"` Login bool `json:"login"` RiskLevelStatus string `json:"riskLevelStatus"` IsFree bool `json:"isFree"` Price string `json:"price"` IsSubscribe bool `json:"isSubscribe"` SubscribeStatus string `json:"subscribeStatus"` IsPackage bool `json:"isPackage"` RiskLevel string `json:"riskLevel"` ProductId int `json:"productId"` Score float64 `json:"score"` } 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 } func SearchMaxMediaId(mediaType string, key string) (total int64, latestId int64) { mediaSort := []string{"mediaId:desc"} request := matchAllByCondition(mediaSort, key, ConditionColumn, mediaType) //同步es //re, err := elastic().Search(request) re, err := elastic().Count(request) if err != nil { logger.Error("es搜索失败:%v", err) } count := re.Count total = int64(count) if total > 0 { request = matchByCondition(mediaSort, key, ConditionColumn, mediaType, 0, count) re, err = elastic().Search(request) if err != nil { logger.Error("es搜索异常:%v", err) } hits := elastic().GetSource(re.Hits) data := hits[0].Source media := MediaDTO{} err = json.Unmarshal(data, &media) if err != nil { logger.Error("获取当前最大媒体id失败:%v", err) return } total = int64(len(hits)) latestId = int64(media.MediaId) return } return } func GetMediaPermissionMappingByPermissionIds(mediaType string, permissionIds []int) (total int64, latestId int64, ids []int) { mediaIds, err := mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, permissionIds) if err != nil { logger.Error("获取配置品种的媒体列表信息失败:%v", err) return 0, 0, ids } //获取一下下架的报告产品 var offSaleProducts []merchantDao.MerchantProduct offSaleProducts, err = merchantDao.GetOffSaleProducts([]merchantDao.MerchantProductType{transProductMap[mediaType], merchantDao.Package}) var disCardMediaIds []int var ProductPermissionIds []int var filterIds []int if err != nil { logger.Error("获取下架的报告产品失败:%v", err) } else { for _, product := range offSaleProducts { if product.Type == "package" { ProductPermissionIds = append(ProductPermissionIds, product.SourceId) } if product.Type == transProductMap[mediaType] { disCardMediaIds = append(disCardMediaIds, product.SourceId) } } } if len(ProductPermissionIds) > 0 { var disCardIds []int disCardIds, err = mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, ProductPermissionIds) if len(disCardIds) > 0 { disCardMediaIds = append(disCardMediaIds, disCardIds...) } } //对数据去重 disCardMediaIds = uniqueArray(disCardMediaIds) //获取媒体中还包含上架套餐的id if len(disCardMediaIds) > 0 { mediaIdsSalePackage, _ := merchantDao.GetMediaOnSalePackageIds(disCardMediaIds) mediaIdsSaleProduct, _ := merchantDao.GetOnSaleMediaIds(disCardMediaIds, transProductMap[mediaType]) showReportMap := make(map[int]bool) disCardMap := make(map[int]bool) for _, reportId := range mediaIdsSalePackage { showReportMap[reportId] = true } for _, reportId := range mediaIdsSaleProduct { showReportMap[reportId] = true } var filterDisCardReportIds []int for _, id := range disCardMediaIds { if _, ok := showReportMap[id]; !ok { filterDisCardReportIds = append(filterDisCardReportIds, id) disCardMap[id] = true } } disCardMediaIds = filterDisCardReportIds for _, id := range mediaIds { if _, ok := disCardMap[id]; !ok { filterIds = append(filterIds, id) } } } else { filterIds = mediaIds } if len(filterIds) > 0 { sort.Slice(filterIds, func(i, j int) bool { return filterIds[i] > filterIds[j] }) return int64(len(filterIds)), int64(filterIds[0]), filterIds } return 0, 0, []int{} } func uniqueArray(arr []int) []int { uniqueMap := make(map[int]bool) var result []int for _, value := range arr { if _, exists := uniqueMap[value]; !exists { uniqueMap[value] = true result = append(result, value) } } return result } func GetAnalystMediaPermissionMappingByPermissionIds(mediaType string, permissionIds []int, analystId int) (total int64, latestId int64, ids []int) { meidaIds, err := mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, permissionIds) if err != nil { logger.Error("获取当前最大媒体id失败:%v", err) return 0, 0, ids } ids, err = mediaDao.GetAnalystMediaRangeMediaIds(mediaType, ids, analystId) if err != nil { logger.Error("根据研究员过滤媒体列表id失败:%v", err) return 0, 0, ids } if len(ids) == 0 { logger.Info("根据研究员过滤媒体列表id为空") return 0, 0, ids } var offSaleProducts []merchantDao.MerchantProduct offSaleProducts, err = merchantDao.GetOffSaleProducts([]merchantDao.MerchantProductType{transProductMap[mediaType], merchantDao.Package}) var disCardMediaIds []int var filterPermissionIds []int var ProductPermissionIds []int var filterIds []int if err != nil { logger.Error("获取下架的报告产品失败:%v", err) } else { for _, product := range offSaleProducts { if product.Type == "package" { ProductPermissionIds = append(ProductPermissionIds, product.SourceId) } if product.Type == transProductMap[mediaType] { disCardMediaIds = append(disCardMediaIds, product.SourceId) } } } if len(ProductPermissionIds) > 0 { var disCardIds []int disCardIds, err = mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, filterPermissionIds) if len(disCardMediaIds) > 0 { disCardMediaIds = append(disCardMediaIds, disCardIds...) } } //对数据去重 disCardMediaIds = uniqueArray(disCardMediaIds) //获取媒体中还包含上架套餐的id if len(disCardMediaIds) > 0 { reportIdsSalePackage, _ := merchantDao.GetMediaOnSalePackageIds(disCardMediaIds) reportIdsSaleProduct, _ := merchantDao.GetOnSaleMediaIds(disCardMediaIds, transProductMap[mediaType]) showReportMap := make(map[int]bool) disCardMap := make(map[int]bool) for _, reportId := range reportIdsSalePackage { showReportMap[reportId] = true } for _, reportId := range reportIdsSaleProduct { showReportMap[reportId] = true } var filterDisCardReportIds []int for _, id := range disCardMediaIds { if _, ok := showReportMap[id]; !ok { filterDisCardReportIds = append(filterDisCardReportIds, id) disCardMap[id] = true } } disCardMediaIds = filterDisCardReportIds for _, id := range meidaIds { if _, ok := disCardMap[id]; !ok { filterIds = append(filterIds, id) } } } else { filterIds = meidaIds } if len(filterIds) > 0 { sort.Slice(filterIds, func(i, j int) bool { return filterIds[i] > filterIds[j] }) return int64(len(filterIds)), int64(filterIds[0]), filterIds } return 0, 0, []int{} } func GetTotalPageCount(mediaType string) (count int64, latestId int64) { return mediaDao.GetCountByMediaType(mediaType) } func GetTotalPageCountByAnalystId(mediaType string, analystId int) (count int64, latestId int64) { return mediaDao.GetCountByMediaTypeByAnalystId(mediaType, analystId) } func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int) (list []MediaDTO, err error) { offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize) medias, err := mediaDao.GetMediaPageByIds(pageInfo.LatestId, pageInfo.PageSize, offset, mediaType, mediaIds) if err == nil && medias != nil { for _, media := range medias { dto := convertMediaDTO(media, false) list = append(list, dto) } } return } func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int) (list []MediaDTO, err error) { offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize) medias, err := mediaDao.GetMediaPageByAnalystId(pageInfo.LatestId, pageInfo.PageSize, offset, mediaType, analystId, mediaIds) if err == nil && medias != nil { for _, media := range medias { dto := convertMediaDTO(media, false) list = append(list, dto) } } return } func convertMediaDTO(media mediaDao.Media, fullTime bool) MediaDTO { if fullTime { return MediaDTO{ MediaId: media.Id, AuthorId: media.AuthorID, AuthorName: media.AuthorName, MediaType: string(media.MediaType), Src: media.Src, MediaName: media.MediaName, SourceType: media.SourceType, CoverSrc: media.CoverSrc, CoverUrl: media.CoverSrc, MediaPlayMilliseconds: media.MediaPlayMilliseconds, PermissionIDs: media.PermissionIDs, PublishedTime: media.PublishedTime.Format(time.DateTime), } } else { return MediaDTO{ MediaId: media.Id, AuthorId: media.AuthorID, AuthorName: media.AuthorName, MediaType: string(media.MediaType), Src: media.Src, MediaName: media.MediaName, SourceType: media.SourceType, CoverSrc: media.CoverSrc, CoverUrl: media.CoverSrc, MediaPlayMilliseconds: media.MediaPlayMilliseconds, PermissionIDs: media.PermissionIDs, PublishedTime: media.PublishedTime.Format(time.DateOnly), } } } func GetMediaById(mediaType string, mediaId int) (mediaDTO MediaDTO, err error) { media, err := mediaDao.GetMediaById(mediaType, mediaId) if err != nil { logger.Error("查询媒体信息失败:%v", err) return } mediaDTO = convertMediaDTO(media, true) return } func GetPermissionsByIds(ids []int) (permissionDTOs []configDomain.PermissionDTO, err error) { return reportService.GetFirstPermissionsByIds(ids) } func GetSecondPermissionsByIds(ids []int) (permissionDTOs []configDomain.PermissionDTO, err error) { return reportService.GetPermissionsByPermissionIds(ids) } func matchAllByCondition(sorts []string, key string, column string, value string) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, 1, sorts, es.MatchAllByCondition).ByCondition(column, value) return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, 1, sorts, es.MatchAll) } func matchByCondition(sorts []string, key string, column string, value string, from int, to int) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.MatchAllByCondition).ByCondition(column, value) return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.MatchAll) } func matchRangeByCondition(key string, from int, to int, max int64, sorts []string, column string, value string) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.RangeByCondition).Range(0, max, ESRangeColumn).ByCondition(column, value) return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.Range).Range(0, max, ESRangeColumn) } func matchRangeWithDocIds(key string, from int, to int, max int64, sorts []string, docIds []string) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.RangeByCondition).Range(0, max, ESRangeColumn).ByCondition(column, value) return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.Range).Range(0, max, ESRangeColumn).WithDocs(docIds) } func matchLimitByScore(key string, limit int, score float64, docIds []string) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, limit, sortField, es.LimitByScore).WithScore(score).WithDocs(docIds) } func GetImageSrc(id int) (src string, err error) { return image.GetImageSrc(id) } func SearchMediaProduct(key string, limit int, score float64, docIds []int) (medias []MediaDTO, err error) { var docStrIds []string for _, docId := range docIds { docStrIds = append(docStrIds, strconv.Itoa(docId)) } request := matchLimitByScore(key, limit, score, docStrIds) re, err := elastic().Search(request) if err != nil { logger.Error("es搜索异常:%v", err) } hits := elastic().GetSource(re.Hits) if len(hits) == 0 { medias = []MediaDTO{} return } 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.Score = hit.Score media.Highlight = content[ESColumn] media.PublishedTime = media.PublishedTime[:10] media.MediaTitle = media.Highlight[0] medias = append(medias, media) } return } func CountPermissionWeight(ids []int) (list []configDao.PermissionWeight, err error) { return mediaDao.CountPermissionWeight(ids) }