media_service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package media
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. stringUtils "eta/eta_mini_ht_api/common/utils/string"
  7. configService "eta/eta_mini_ht_api/domian/config"
  8. mediaService "eta/eta_mini_ht_api/domian/media"
  9. productService "eta/eta_mini_ht_api/domian/merchant"
  10. userService "eta/eta_mini_ht_api/domian/user"
  11. mediaDao "eta/eta_mini_ht_api/models/media"
  12. productDao "eta/eta_mini_ht_api/models/merchant"
  13. "eta/eta_mini_ht_api/service/config"
  14. "eta/eta_mini_ht_api/service/user"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. )
  19. const (
  20. RiskLevelUnMatch = "unMatch"
  21. RiskLevelUnTest = "unTest"
  22. RiskLevelExpired = "expired"
  23. RiskLevelMatch = "match"
  24. defaultProductPrice = "0"
  25. )
  26. type RecordCount struct {
  27. UserId int
  28. Mobile string
  29. MediaId int
  30. MediaName string
  31. TraceId string
  32. MediaType string
  33. IpAddress string
  34. Location string
  35. Referer string
  36. Additional string
  37. }
  38. func convertToMediaCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  39. return userService.RecordCountDTO{
  40. UserId: record.UserId,
  41. TraceId: record.TraceId,
  42. Mobile: record.Mobile,
  43. SourceId: record.MediaId,
  44. }
  45. }
  46. func CountMedia(count RecordCount) (traceId string, err error) {
  47. media, err := mediaService.GetMediaById(count.MediaType, count.MediaId)
  48. if err != nil {
  49. err = exception.New(exception.MediaFoundFailed)
  50. return
  51. }
  52. dto := convertToMediaCountDTO(count)
  53. dto.SourceTitle = media.MediaName
  54. return userService.CountMedia(dto, media.MediaType)
  55. }
  56. func RangeSearchByAnalyst(mediaType string, analystId int, userId int) (total int64, latestId int64, ids []int, mappingRiskLevel, userRiskStatus string) {
  57. filterPermissionIds, mappingRiskLevel, userRiskStatus, err := user.GetRiskLevelPermissionList(nil, true, userId)
  58. if err != nil {
  59. logger.Error("校验用户风险等级失败:%v", err)
  60. return
  61. }
  62. total, latestId, ids = mediaService.GetAnalystMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, analystId)
  63. return
  64. }
  65. func getCount(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, mediaIds []int, mappingPdRisk string, userRiskStatus string) {
  66. filterPermissionIds, mappingPdRisk, userRiskStatus, err := user.GetRiskLevelPermissionList(permissionIds, isLogin, userId)
  67. if err != nil {
  68. logger.Error("校验用户风险等级失败:%v", err)
  69. return
  70. }
  71. total, latestId, mediaIds = mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds)
  72. return
  73. }
  74. func RangeSearch(mediaType string, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string, userRiskStatus string) {
  75. return getCount(mediaType, nil, isLogin, userId)
  76. }
  77. func GetTotalPageCountByPermissionIds(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string, userRiskStatus string) {
  78. if mediaType == "" {
  79. return
  80. }
  81. return getCount(mediaType, permissionIds, isLogin, userId)
  82. }
  83. func GetMediaById(mediaType string, mediaId int, isLogin bool, userId int) (media *mediaService.MediaDTO, err error) {
  84. mappingRiskLevel, userRiskStatus, err := user.CheckUserRiskMatchStatus(userId)
  85. if err != nil {
  86. logger.Error("校验用户风险等级失败:%v", err)
  87. }
  88. var mediaInfo mediaService.MediaDTO
  89. mediaInfo, err = mediaService.GetMediaById(mediaType, mediaId)
  90. if err != nil {
  91. logger.Error("获取媒体失败:%v", err)
  92. err = exception.New(exception.MediaFoundFailed)
  93. return
  94. }
  95. return DealMediaInfo(&mediaInfo, isLogin, userId, mappingRiskLevel, userRiskStatus)
  96. }
  97. func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int, userId int, mappingRiskLevel string, userRiskStatus string) (list []mediaService.MediaDTO, err error) {
  98. list, err = mediaService.GetMediaPageByAnalystId(mediaType, pageInfo, analystId, mediaIds)
  99. if err != nil {
  100. err = exception.New(exception.GetAnalystMediaListFailed)
  101. return
  102. }
  103. if len(list) == 0 {
  104. logger.Info("研究员媒体列表为空")
  105. return
  106. }
  107. //并发获取媒体的标签
  108. list, err = dealMediaInfo(list, true, userId, mappingRiskLevel, userRiskStatus)
  109. if err != nil {
  110. err = exception.New(exception.GetAnalystMediaListFailed)
  111. }
  112. return
  113. }
  114. func getMediaSecondPermissions(ids []int) (permissionDTOs []configService.PermissionDTO, err error) {
  115. return mediaService.GetSecondPermissionsByIds(ids)
  116. }
  117. func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int, isLogin bool, userId int, mappingRiskLevel string, userRiskStatus string) (list []mediaService.MediaDTO, err error) {
  118. list, err = mediaService.GetMediaPageByIds(mediaType, pageInfo, mediaIds)
  119. if err != nil {
  120. err = exception.New(exception.GetMediaListFailed)
  121. return
  122. }
  123. list, err = dealMediaInfo(list, isLogin, userId, mappingRiskLevel, userRiskStatus)
  124. if err != nil {
  125. err = exception.New(exception.QueryReportPageFailed)
  126. }
  127. return
  128. }
  129. func GetMediaPermissionNames(id []int) (labels []string) {
  130. permissions, err := mediaService.GetPermissionsByIds(id)
  131. if err != nil {
  132. logger.Error("查询品种名称列表失败:%v", err)
  133. labels = []string{}
  134. return
  135. }
  136. for _, permission := range permissions {
  137. labels = append(labels, permission.PermissionName)
  138. }
  139. return
  140. }
  141. func DealMediaInfo(media *mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string, userRiskStatus string) (resultMedia *mediaService.MediaDTO, err error) {
  142. idStr := strings.Split(media.PermissionIDs, ",")
  143. var ids []int
  144. ids, err = stringUtils.StringToIntSlice(idStr)
  145. if err != nil {
  146. logger.Error("品种名称列表转换失败:%v", err)
  147. }
  148. media.PermissionNames = GetMediaPermissionNames(ids)
  149. permissions, permissionErr := getMediaSecondPermissions(ids)
  150. if permissionErr != nil {
  151. logger.Error("获取媒体品种信息失败:%v,无法设置媒体风险等级", err)
  152. }
  153. riskNum := config.GetHighestRiskLevel(permissions)
  154. media.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  155. if !isLogin {
  156. media.Src = ""
  157. }
  158. media.Login = isLogin
  159. var productType productDao.MerchantProductType
  160. switch media.MediaType {
  161. case string(mediaDao.Audio):
  162. productType = productDao.Audio
  163. case string(mediaDao.Video):
  164. productType = productDao.Video
  165. default:
  166. logger.Error("未知媒体类型:%s", media.MediaType)
  167. productType = ""
  168. }
  169. var packageList []productService.MerchantProductDTO
  170. var permissionIds []int
  171. if len(permissions) > 0 {
  172. for _, permission := range permissions {
  173. permissionIds = append(permissionIds, permission.PermissionId)
  174. }
  175. packageList, err = productService.GetProductListBySourceIds(permissionIds, "package")
  176. if err != nil {
  177. logger.Error("获取套餐列表失败:%v", err)
  178. }
  179. }
  180. //判断是否有单品,没有的话查看套餐
  181. product, pdErr := productService.GetProductBySourceId(media.MediaId, productType)
  182. if pdErr != nil {
  183. if len(packageList) == 0 {
  184. logger.Error("获取套餐列表失败:%v", err)
  185. media.Price = defaultProductPrice
  186. media.IsFree = true
  187. media.IsSubscribe = false
  188. media.IsPackage = false
  189. } else {
  190. media.Price = packageList[0].Price
  191. media.IsFree = false
  192. media.IsSubscribe = false
  193. media.IsPackage = true
  194. media.ProductId = packageList[0].Id
  195. }
  196. } else {
  197. media.Price = product.Price
  198. media.IsFree = false
  199. media.IsPackage = false
  200. media.IsSubscribe = false
  201. media.ProductId = product.Id
  202. }
  203. if isLogin {
  204. var productIds []int
  205. if len(packageList) > 0 {
  206. for _, packageItem := range packageList {
  207. productIds = append(productIds, packageItem.Id)
  208. }
  209. }
  210. if product.Id > 0 {
  211. productIds = append(productIds, product.Id)
  212. }
  213. subscribeList, subscribeErr := userService.GetUserSubscribe(productIds, templateUserId)
  214. if subscribeErr != nil {
  215. media.IsSubscribe = false
  216. } else {
  217. for _, subscribe := range subscribeList {
  218. if subscribe.Status == productDao.SubscribeValid {
  219. media.IsSubscribe = true
  220. break
  221. }
  222. }
  223. }
  224. if userRiskStatus != user.RiskValid {
  225. if userRiskStatus == user.RiskUnTest {
  226. media.RiskLevelStatus = RiskLevelUnTest
  227. }
  228. if userRiskStatus == user.RiskExpired {
  229. media.RiskLevelStatus = RiskLevelExpired
  230. }
  231. } else {
  232. media.RiskLevelStatus = RiskLevelUnMatch
  233. if mappingRiskLevel != "" {
  234. mappingRiskNum, parseErr := config.ParseRiskLevel(mappingRiskLevel)
  235. if parseErr != nil {
  236. return
  237. }
  238. var rpRiskNum int
  239. rpRiskNum, parseErr = config.ParseRiskLevel(media.RiskLevel)
  240. if parseErr != nil {
  241. return
  242. }
  243. if rpRiskNum <= mappingRiskNum {
  244. media.RiskLevelStatus = RiskLevelMatch
  245. }
  246. }
  247. }
  248. }
  249. resultMedia = media
  250. return
  251. }
  252. // stringToIntSlice 将一个包含数字字符串的切片转换为整数切片
  253. func dealMediaInfo(list []mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string, userRiskStatus string) (dealList []mediaService.MediaDTO, err error) {
  254. //并发获取媒体的标签
  255. var wg sync.WaitGroup
  256. wg.Add(len(list))
  257. for i := 0; i < len(list); i++ {
  258. go func(media *mediaService.MediaDTO) {
  259. defer wg.Done()
  260. media, err = DealMediaInfo(media, isLogin, templateUserId, mappingRiskLevel, userRiskStatus)
  261. if err != nil {
  262. logger.Error("处理媒体信息失败:%v", err)
  263. }
  264. }(&list[i])
  265. }
  266. wg.Wait()
  267. dealList = list
  268. return
  269. }
  270. func SearchMediaList(mediaType string, key string, mediaIds []int, pageInfo page.PageInfo, isLogin bool, userId int, mappingRiskLevel string, userRiskStatus string) (medias []mediaService.MediaDTO, err error) {
  271. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  272. medias, err = mediaService.SearchMediaList(mediaType, key, mediaIds, offset, pageInfo.PageSize, pageInfo.LatestId)
  273. medias, err = dealMediaInfo(medias, isLogin, userId, mappingRiskLevel, userRiskStatus)
  274. if err != nil {
  275. err = exception.New(exception.SearchReportPageFailed)
  276. }
  277. return
  278. }
  279. func SearchMediaProduct(key string, docIds []int) (list []mediaService.MediaDTO, err error) {
  280. list, err = mediaService.SearchMediaProduct(key, 100, 0, docIds)
  281. if err != nil {
  282. err = exception.New(exception.SearchReportPageFailed)
  283. }
  284. return
  285. }
  286. func CountPermissionWeight(ids []int) (permissionMap map[int]int) {
  287. list, err := mediaService.CountPermissionWeight(ids)
  288. permissionMap = make(map[int]int)
  289. if err != nil {
  290. return
  291. }
  292. for _, permission := range list {
  293. permissionMap[permission.PermissionId] = permission.Weight
  294. }
  295. return
  296. }