123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- 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)
- }
|