package media import ( logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/exception" "eta/eta_mini_ht_api/common/utils/page" stringUtils "eta/eta_mini_ht_api/common/utils/string" configService "eta/eta_mini_ht_api/domian/config" mediaService "eta/eta_mini_ht_api/domian/media" productService "eta/eta_mini_ht_api/domian/merchant" userService "eta/eta_mini_ht_api/domian/user" mediaDao "eta/eta_mini_ht_api/models/media" productDao "eta/eta_mini_ht_api/models/merchant" "eta/eta_mini_ht_api/service/config" "eta/eta_mini_ht_api/service/user" "strconv" "strings" "sync" ) const ( RiskLevelUnMatch = "unMatch" RiskLevelUnTest = "unTest" RiskLevelExpired = "expired" RiskLevelMatch = "match" defaultProductPrice = "0" ) type RecordCount struct { UserId int Mobile string MediaId int MediaName string TraceId string MediaType string IpAddress string Location string Referer string Additional string } func convertToMediaCountDTO(record RecordCount) (dto userService.RecordCountDTO) { return userService.RecordCountDTO{ UserId: record.UserId, TraceId: record.TraceId, Mobile: record.Mobile, SourceId: record.MediaId, } } func CountMedia(count RecordCount) (traceId string, err error) { media, err := mediaService.GetMediaById(count.MediaType, count.MediaId) if err != nil { err = exception.New(exception.MediaFoundFailed) return } dto := convertToMediaCountDTO(count) dto.SourceTitle = media.MediaName return userService.CountMedia(dto, media.MediaType) } func RangeSearchByAnalyst(mediaType string, analystId int, userId int) (total int64, latestId int64, ids []int, mappingRiskLevel, userRiskStatus string) { filterPermissionIds, mappingRiskLevel, userRiskStatus, err := user.GetRiskLevelPermissionList(nil, true, userId) if err != nil { logger.Error("校验用户风险等级失败:%v", err) return } total, latestId, ids = mediaService.GetAnalystMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, analystId) return } func getCount(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, mediaIds []int, mappingPdRisk string, userRiskStatus string) { filterPermissionIds, mappingPdRisk, userRiskStatus, err := user.GetRiskLevelPermissionList(permissionIds, isLogin, userId) if err != nil { logger.Error("校验用户风险等级失败:%v", err) return } total, latestId, mediaIds = mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds) return } func RangeSearch(mediaType string, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string, userRiskStatus string) { return getCount(mediaType, nil, isLogin, userId) } func GetTotalPageCountByPermissionIds(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string, userRiskStatus string) { if mediaType == "" { return } return getCount(mediaType, permissionIds, isLogin, userId) } func GetMediaById(mediaType string, mediaId int, isLogin bool, userId int) (media *mediaService.MediaDTO, err error) { mappingRiskLevel, userRiskStatus, err := user.CheckUserRiskMatchStatus(userId) if err != nil { logger.Error("校验用户风险等级失败:%v", err) } var mediaInfo mediaService.MediaDTO mediaInfo, err = mediaService.GetMediaById(mediaType, mediaId) if err != nil { logger.Error("获取媒体失败:%v", err) err = exception.New(exception.MediaFoundFailed) return } return DealMediaInfo(&mediaInfo, isLogin, userId, mappingRiskLevel, userRiskStatus) } func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int, userId int, mappingRiskLevel string, userRiskStatus string) (list []mediaService.MediaDTO, err error) { list, err = mediaService.GetMediaPageByAnalystId(mediaType, pageInfo, analystId, mediaIds) if err != nil { err = exception.New(exception.GetAnalystMediaListFailed) return } if len(list) == 0 { logger.Info("研究员媒体列表为空") return } //并发获取媒体的标签 list, err = dealMediaInfo(list, true, userId, mappingRiskLevel, userRiskStatus) if err != nil { err = exception.New(exception.GetAnalystMediaListFailed) } return } func getMediaSecondPermissions(ids []int) (permissionDTOs []configService.PermissionDTO, err error) { return mediaService.GetSecondPermissionsByIds(ids) } func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int, isLogin bool, userId int, mappingRiskLevel string, userRiskStatus string) (list []mediaService.MediaDTO, err error) { list, err = mediaService.GetMediaPageByIds(mediaType, pageInfo, mediaIds) if err != nil { err = exception.New(exception.GetMediaListFailed) return } list, err = dealMediaInfo(list, isLogin, userId, mappingRiskLevel, userRiskStatus) if err != nil { err = exception.New(exception.QueryReportPageFailed) } return } func GetMediaPermissionNames(id []int) (labels []string) { permissions, err := mediaService.GetPermissionsByIds(id) if err != nil { logger.Error("查询品种名称列表失败:%v", err) labels = []string{} return } for _, permission := range permissions { labels = append(labels, permission.PermissionName) } return } func DealMediaInfo(media *mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string, userRiskStatus string) (resultMedia *mediaService.MediaDTO, err error) { idStr := strings.Split(media.PermissionIDs, ",") var ids []int ids, err = stringUtils.StringToIntSlice(idStr) if err != nil { logger.Error("品种名称列表转换失败:%v", err) } media.PermissionNames = GetMediaPermissionNames(ids) permissions, permissionErr := getMediaSecondPermissions(ids) if permissionErr != nil { logger.Error("获取媒体品种信息失败:%v,无法设置媒体风险等级", err) } riskNum := config.GetHighestRiskLevel(permissions) media.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "") if !isLogin { media.Src = "" } media.Login = isLogin var productType productDao.MerchantProductType switch media.MediaType { case string(mediaDao.Audio): productType = productDao.Audio case string(mediaDao.Video): productType = productDao.Video default: logger.Error("未知媒体类型:%s", media.MediaType) productType = "" } var packageList []productService.MerchantProductDTO var permissionIds []int if len(permissions) > 0 { for _, permission := range permissions { permissionIds = append(permissionIds, permission.PermissionId) } packageList, err = productService.GetProductListBySourceIds(permissionIds, "package") if err != nil { logger.Error("获取套餐列表失败:%v", err) } } //判断是否有单品,没有的话查看套餐 product, pdErr := productService.GetProductBySourceId(media.MediaId, productType) if pdErr != nil { if len(packageList) == 0 { logger.Error("获取套餐列表失败:%v", err) media.Price = defaultProductPrice media.IsFree = true media.IsSubscribe = false media.IsPackage = false } else { media.Price = packageList[0].Price media.IsFree = false media.IsSubscribe = false media.IsPackage = true media.ProductId = packageList[0].Id } } else { media.Price = product.Price media.IsFree = false media.IsPackage = false media.IsSubscribe = false media.ProductId = product.Id } if isLogin { var productIds []int if len(packageList) > 0 { for _, packageItem := range packageList { productIds = append(productIds, packageItem.Id) } } if product.Id > 0 { productIds = append(productIds, product.Id) } subscribeList, subscribeErr := userService.GetUserSubscribe(productIds, templateUserId) if subscribeErr != nil { media.IsSubscribe = false } else { for _, subscribe := range subscribeList { if subscribe.Status == productDao.SubscribeValid { media.IsSubscribe = true break } } } if userRiskStatus != user.RiskValid { if userRiskStatus == user.RiskUnTest { media.RiskLevelStatus = RiskLevelUnTest } if userRiskStatus == user.RiskExpired { media.RiskLevelStatus = RiskLevelExpired } } else { media.RiskLevelStatus = RiskLevelUnMatch if mappingRiskLevel != "" { mappingRiskNum, parseErr := config.ParseRiskLevel(mappingRiskLevel) if parseErr != nil { return } var rpRiskNum int rpRiskNum, parseErr = config.ParseRiskLevel(media.RiskLevel) if parseErr != nil { return } if rpRiskNum <= mappingRiskNum { media.RiskLevelStatus = RiskLevelMatch } } } } resultMedia = media return } // stringToIntSlice 将一个包含数字字符串的切片转换为整数切片 func dealMediaInfo(list []mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string, userRiskStatus string) (dealList []mediaService.MediaDTO, err error) { //并发获取媒体的标签 var wg sync.WaitGroup wg.Add(len(list)) for i := 0; i < len(list); i++ { go func(media *mediaService.MediaDTO) { defer wg.Done() media, err = DealMediaInfo(media, isLogin, templateUserId, mappingRiskLevel, userRiskStatus) if err != nil { logger.Error("处理媒体信息失败:%v", err) } }(&list[i]) } wg.Wait() dealList = list return } func SearchMediaList(mediaType string, key string, mediaIds []int, pageInfo page.PageInfo, isLogin bool, userId int, mappingRiskLevel string, userRiskStatus string) (medias []mediaService.MediaDTO, err error) { offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize) medias, err = mediaService.SearchMediaList(mediaType, key, mediaIds, offset, pageInfo.PageSize, pageInfo.LatestId) medias, err = dealMediaInfo(medias, isLogin, userId, mappingRiskLevel, userRiskStatus) if err != nil { err = exception.New(exception.SearchReportPageFailed) } return } func SearchMediaProduct(key string, docIds []int) (list []mediaService.MediaDTO, err error) { list, err = mediaService.SearchMediaProduct(key, 100, 0, docIds) if err != nil { err = exception.New(exception.SearchReportPageFailed) } return } func CountPermissionWeight(ids []int) (permissionMap map[int]int) { list, err := mediaService.CountPermissionWeight(ids) permissionMap = make(map[int]int) if err != nil { return } for _, permission := range list { permissionMap[permission.PermissionId] = permission.Weight } return }