media_service.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package media
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/exception"
  6. "eta/eta_mini_ht_api/common/utils/page"
  7. stringUtils "eta/eta_mini_ht_api/common/utils/string"
  8. configService "eta/eta_mini_ht_api/domian/config"
  9. permissionService "eta/eta_mini_ht_api/domian/config"
  10. mediaService "eta/eta_mini_ht_api/domian/media"
  11. productService "eta/eta_mini_ht_api/domian/merchant"
  12. userService "eta/eta_mini_ht_api/domian/user"
  13. mediaDao "eta/eta_mini_ht_api/models/media"
  14. productDao "eta/eta_mini_ht_api/models/merchant"
  15. userDao "eta/eta_mini_ht_api/models/user"
  16. "eta/eta_mini_ht_api/service/report"
  17. "eta/eta_mini_ht_api/service/user"
  18. "fmt"
  19. "gorm.io/gorm"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. )
  24. const (
  25. RiskLevelUnMatch = "unMatch"
  26. RiskLevelUnTest = "unTest"
  27. RiskLevelExpired = "expired"
  28. RiskLevelMatch = "match"
  29. defaultProductPrice = "0"
  30. )
  31. type RecordCount struct {
  32. UserId int
  33. Mobile string
  34. MediaId int
  35. TraceId string
  36. MediaType string
  37. IpAddress string
  38. Location string
  39. Referer string
  40. Additional string
  41. }
  42. func convertToMediaCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  43. return userService.RecordCountDTO{
  44. UserId: record.UserId,
  45. TraceId: record.TraceId,
  46. Mobile: record.Mobile,
  47. SourceId: record.MediaId,
  48. }
  49. }
  50. func CountMedia(count RecordCount) (traceId string, err error) {
  51. meida, err := mediaService.GetMediaById(count.MediaType, count.MediaId)
  52. if err != nil {
  53. err = exception.New(exception.MediaFoundFailed)
  54. return
  55. }
  56. dto := convertToMediaCountDTO(count)
  57. return userService.CountMedia(dto, meida.MediaType)
  58. }
  59. func GetTotalPageCount(mediaType string) (count int64, latestId int64) {
  60. if mediaType == "" {
  61. return
  62. }
  63. return mediaService.GetTotalPageCount(mediaType)
  64. }
  65. func GetTotalPageCountByAnalystId(mediaType string, analystId int) (total int64, latestId int64) {
  66. if mediaType == "" {
  67. return
  68. }
  69. return mediaService.GetTotalPageCountByAnalystId(mediaType, analystId)
  70. }
  71. func RangeSearchByAnalyst(mediaType string, analystId int, userId int) (total int64, latestId int64, ids []int) {
  72. var err error
  73. //登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  74. userProfile, userErr := user.GetUserProfile(userId)
  75. if userErr != nil {
  76. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  77. err = exception.New(exception.TemplateUserNotFound)
  78. } else {
  79. err = exception.New(exception.TemplateUserFoundFailed)
  80. }
  81. logger.Error("分页查询报告列表失败:%v", err)
  82. return
  83. }
  84. //获取产品风险等级
  85. if userProfile.RiskLevel == user.RiskUnTest {
  86. logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  87. return
  88. }
  89. if userProfile.RiskLevelStatus == user.RiskExpired {
  90. logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  91. return
  92. }
  93. mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  94. if mappingErr != nil {
  95. logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  96. return
  97. }
  98. var permissionList []permissionService.PermissionDTO
  99. //获取所有设置风险等级的品种
  100. permissionList, err = permissionService.GetPermissionListWithRisk()
  101. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  102. if len(permissionList) == 0 {
  103. return
  104. }
  105. var filterPermissionIds []int
  106. for _, permission := range permissionList {
  107. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  108. }
  109. return mediaService.GetAnalystMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, analystId)
  110. }
  111. func getCount(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, orgIds []int) {
  112. filterPermissionIds, riskLevel, err := report.CheckUserRisk(permissionIds, isLogin, userId)
  113. if err != nil {
  114. logger.Error("校验用户风险等级失败:%v", err)
  115. return
  116. }
  117. return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, riskLevel)
  118. }
  119. func RangeSearch(mediaType string, isLogin bool, userId int) (total int64, latestId int64, ids []int) {
  120. return getCount(mediaType, nil, isLogin, userId)
  121. //var err error
  122. ////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  123. //if isLogin {
  124. // userProfile, userErr := user.GetUserProfile(userId)
  125. // if userErr != nil {
  126. // if errors.Is(userErr, gorm.ErrRecordNotFound) {
  127. // err = exception.New(exception.TemplateUserNotFound)
  128. // } else {
  129. // err = exception.New(exception.TemplateUserFoundFailed)
  130. // }
  131. // logger.Error("分页查询报告列表失败:%v", err)
  132. // return
  133. // }
  134. // //获取产品风险等级
  135. // if userProfile.RiskLevel == user.RiskUnTest {
  136. // logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  137. // return
  138. // }
  139. // if userProfile.RiskLevelStatus == user.RiskExpired {
  140. // logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  141. // return
  142. // }
  143. // mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  144. // if mappingErr != nil {
  145. // logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  146. // return
  147. // }
  148. // var permissionList []permissionService.PermissionDTO
  149. // //获取所有设置风险等级的品种
  150. // permissionList, err = permissionService.GetPermissionListWithRisk()
  151. // permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  152. // if len(permissionList) == 0 {
  153. // return
  154. // }
  155. // var filterPermissionIds []int
  156. // for _, permission := range permissionList {
  157. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  158. // }
  159. // return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, mapping.ProductRiskLevel)
  160. //} else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  161. // var permissionList []permissionService.PermissionDTO
  162. // //获取所有设置风险等级的品种
  163. // permissionList, err = permissionService.GetPermissionListWithRisk()
  164. // if err != nil {
  165. // logger.Error("根据ID查询品种列表失败:%v", err)
  166. // }
  167. // var filterPermissionIds []int
  168. // for _, permission := range permissionList {
  169. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  170. // }
  171. // //查询品种
  172. // return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, "")
  173. //}
  174. }
  175. func GetTotalPageCountByPermissionIds(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids []int) {
  176. if mediaType == "" {
  177. return
  178. }
  179. return getCount(mediaType, permissionIds, isLogin, userId)
  180. //var err error
  181. ////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  182. //if isLogin {
  183. // userProfile, userErr := user.GetUserProfile(userId)
  184. // if userErr != nil {
  185. // if errors.Is(userErr, gorm.ErrRecordNotFound) {
  186. // err = exception.New(exception.TemplateUserNotFound)
  187. // } else {
  188. // err = exception.New(exception.TemplateUserFoundFailed)
  189. // }
  190. // logger.Error("分页查询媒体列表失败:%v", err)
  191. // return
  192. // }
  193. // //获取产品风险等级
  194. // if userProfile.RiskLevel == user.RiskUnTest {
  195. // logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  196. // return
  197. // }
  198. // if userProfile.RiskLevelStatus == user.RiskExpired {
  199. // logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  200. // return
  201. // }
  202. // mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  203. // if mappingErr != nil {
  204. // logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  205. // return
  206. // }
  207. // var permissionList []permissionService.PermissionDTO
  208. // if len(permissionIds) == 0 {
  209. // //获取所有设置风险等级的品种
  210. // permissionList, err = permissionService.GetPermissionListWithRisk()
  211. // } else {
  212. // //更具id过滤设置了风险等级的品种
  213. // permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  214. // }
  215. // permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  216. // if len(permissionList) == 0 {
  217. // return
  218. // }
  219. // var filterPermissionIds []int
  220. // for _, permission := range permissionList {
  221. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  222. // }
  223. // return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, mapping.ProductRiskLevel)
  224. //} else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  225. // var permissionList []permissionService.PermissionDTO
  226. // if len(permissionIds) == 0 {
  227. // //获取所有设置风险等级的品种
  228. // permissionList, err = permissionService.GetPermissionListWithRisk()
  229. // } else {
  230. // //更具id过滤设置了风险等级的品种
  231. // permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  232. // }
  233. // if err != nil {
  234. // logger.Error("根据ID查询品种列表失败:%v", err)
  235. // }
  236. // var filterPermissionIds []int
  237. // for _, permission := range permissionList {
  238. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  239. // }
  240. // //查询品种
  241. // return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, "")
  242. //}
  243. }
  244. func parseRiskLevel(level string) (int, error) {
  245. parts := strings.Split(level, "R")
  246. if len(parts) < 2 {
  247. return 0, fmt.Errorf("无效的风险等级: %s", level)
  248. }
  249. numberStr := parts[1]
  250. number, err := strconv.Atoi(numberStr)
  251. if err != nil {
  252. return 0, fmt.Errorf("无法将风险等级转换为数字: %s", err)
  253. }
  254. return number, nil
  255. }
  256. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  257. riskLevelNum, err := parseRiskLevel(riskLevel)
  258. if err != nil {
  259. logger.Error("风险等级解析失败:%v", err)
  260. return
  261. }
  262. for _, permission := range permissionList {
  263. pRiskNum, riskErr := parseRiskLevel(permission.RiskLevel)
  264. if riskErr != nil {
  265. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  266. continue
  267. }
  268. if pRiskNum <= riskLevelNum {
  269. resultList = append(resultList, permission)
  270. }
  271. }
  272. return
  273. }
  274. func SearchMaxMediaId(mediaType string, key string) (total int64, latestId int64) {
  275. return mediaService.SearchMaxMediaId(mediaType, key)
  276. }
  277. func GetMediaById(mediaType string, mediaId int, isLogin bool, userId int) (media mediaService.MediaDTO, err error) {
  278. media, err = mediaService.GetMediaById(mediaType, mediaId)
  279. if err != nil {
  280. logger.Error("获取媒体失败:%v", err)
  281. err = exception.New(exception.MediaFoundFailed)
  282. return
  283. }
  284. var status string
  285. status, media.RiskLevel, err = matchRiskLevel(userId, media)
  286. if err != nil {
  287. logger.Error("匹配风险等级失败:%v", err)
  288. err = exception.New(exception.ReportRiskLevelUnSet)
  289. return
  290. }
  291. idStr := strings.Split(media.PermissionIDs, ",")
  292. var ids []int
  293. ids, err = stringUtils.StringToIntSlice(idStr)
  294. if err != nil {
  295. logger.Error("品种名称列表转换失败:%v", err)
  296. return
  297. }
  298. media.PermissionNames = getMediaPermissionNames(ids)
  299. if isLogin {
  300. media.Login = true
  301. if status != RiskLevelMatch {
  302. media.Src = ""
  303. }
  304. media.RiskLevelStatus = status
  305. } else {
  306. logger.Info("当前用户未登录,展示部分详情")
  307. media.Src = ""
  308. media.RiskLevelStatus = RiskLevelUnMatch
  309. media.Login = false
  310. }
  311. return
  312. }
  313. func matchRiskLevel(userId int, media mediaService.MediaDTO) (riskLevelMatch string, productRiskLevel string, err error) {
  314. userProfile, userErr := user.GetUserProfile(userId)
  315. if userErr != nil {
  316. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  317. logger.Error("用户信息不存在,mobile:%d", userProfile.Mobile)
  318. err = exception.New(exception.TemplateUserNotFound)
  319. return
  320. } else {
  321. logger.Error("获取用户信息失败:%v", userErr)
  322. err = exception.New(exception.TemplateUserFoundFailed)
  323. return
  324. }
  325. }
  326. //比较风险等级
  327. if userProfile.RiskLevelStatus == user.RiskUnTest {
  328. logger.Info("客户风险等级未测试,mobile:%v", userProfile.Mobile)
  329. riskLevelMatch = RiskLevelUnTest
  330. return
  331. }
  332. if userProfile.RiskLevelStatus == user.RiskExpired {
  333. logger.Info("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  334. riskLevelMatch = RiskLevelExpired
  335. return
  336. }
  337. level, err := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  338. if err != nil {
  339. logger.Error("获取媒体风险等级设置失败:%v", err)
  340. return
  341. }
  342. var permissionIds []int
  343. permissionStrList := strings.Split(media.PermissionIDs, ",")
  344. permissionIds, err = stringUtils.StringToIntSlice(permissionStrList)
  345. if err != nil {
  346. logger.Error("解析媒体品种ID列表失败:%v", err)
  347. }
  348. permissionDTOs, err := permissionService.GetPermissionListByIds(permissionIds)
  349. if err != nil {
  350. logger.Error("获取品种风险等级失败:%v", err)
  351. return
  352. }
  353. //能够查看最高等级
  354. matchNum, err := parseRiskLevel(level.ProductRiskLevel)
  355. if err != nil {
  356. logger.Error("解析风险等级失败:%v", err)
  357. return
  358. }
  359. if len(permissionDTOs) == 0 {
  360. logger.Error("当前报告对应品种未设置风险等级")
  361. err = exception.New(exception.ReportRiskLevelUnSet)
  362. return
  363. }
  364. //能够查看需要的最小等级
  365. num := getHighestRiskLevel(permissionDTOs)
  366. productRiskLevel = fmt.Sprintf("R%d", num)
  367. if num > matchNum {
  368. riskLevelMatch = RiskLevelUnMatch
  369. return
  370. } else {
  371. riskLevelMatch = RiskLevelMatch
  372. return
  373. }
  374. }
  375. func getHighestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  376. for _, permission := range permissions {
  377. pRiskNum, err := parseRiskLevel(permission.RiskLevel)
  378. if err != nil {
  379. logger.Error("解析风险等级失败:%v", err)
  380. continue
  381. }
  382. if riskLevelNum == 0 {
  383. riskLevelNum = pRiskNum
  384. } else {
  385. if riskLevelNum < pRiskNum {
  386. riskLevelNum = pRiskNum
  387. }
  388. }
  389. }
  390. return
  391. }
  392. func getLowestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  393. for _, permission := range permissions {
  394. pRiskNum, err := parseRiskLevel(permission.RiskLevel)
  395. if err != nil {
  396. logger.Error("解析风险等级失败:%v", err)
  397. continue
  398. }
  399. if riskLevelNum == 0 {
  400. riskLevelNum = pRiskNum
  401. } else {
  402. if riskLevelNum > pRiskNum {
  403. riskLevelNum = pRiskNum
  404. }
  405. }
  406. }
  407. return
  408. }
  409. func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int) (list []mediaService.MediaDTO, err error) {
  410. list, err = mediaService.GetMediaPageByAnalystId(mediaType, pageInfo, analystId, mediaIds)
  411. if err != nil {
  412. err = exception.New(exception.GetAnalystMediaListFailed)
  413. return
  414. }
  415. if len(list) == 0 {
  416. logger.Info("研究员媒体列表为空")
  417. return
  418. }
  419. //并发获取媒体的标签
  420. var wg sync.WaitGroup
  421. wg.Add(len(list))
  422. for i := 0; i < len(list); i++ {
  423. go func(media *mediaService.MediaDTO) {
  424. defer wg.Done()
  425. idStr := strings.Split(media.PermissionIDs, ",")
  426. var ids []int
  427. ids, err = stringUtils.StringToIntSlice(idStr)
  428. if err != nil {
  429. logger.Error("品种名称列表转换失败:%v", err)
  430. }
  431. media.PermissionNames = getMediaPermissionNames(ids)
  432. }(&list[i])
  433. }
  434. wg.Wait()
  435. if err != nil {
  436. err = exception.New(exception.GetAnalystMediaListFailed)
  437. }
  438. return
  439. }
  440. func getMediaSecondPermissions(ids []int) (permissionDTOs []configService.PermissionDTO, err error) {
  441. return mediaService.GetSecondPermissionsByIds(ids)
  442. }
  443. func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int, isLogin bool, userId int) (list []mediaService.MediaDTO, err error) {
  444. list, err = mediaService.GetMediaPageByIds(mediaType, pageInfo, mediaIds)
  445. if err != nil {
  446. err = exception.New(exception.GetMediaListFailed)
  447. return
  448. }
  449. //并发获取媒体的标签
  450. var wg sync.WaitGroup
  451. wg.Add(len(list))
  452. for i := 0; i < len(list); i++ {
  453. go func(media *mediaService.MediaDTO) {
  454. defer wg.Done()
  455. idStr := strings.Split(media.PermissionIDs, ",")
  456. var ids []int
  457. ids, err = stringUtils.StringToIntSlice(idStr)
  458. if err != nil {
  459. logger.Error("品种名称列表转换失败:%v", err)
  460. }
  461. media.PermissionNames = getMediaPermissionNames(ids)
  462. permissions, permissionErr := getMediaSecondPermissions(ids)
  463. if permissionErr != nil {
  464. logger.Error("获取媒体品种信息失败:%v,无法设置媒体风险等级", err)
  465. }
  466. riskNum := getHighestRiskLevel(permissions)
  467. media.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  468. if !isLogin {
  469. media.Src = ""
  470. }
  471. media.Login = isLogin
  472. var productType productDao.MerchantProductType
  473. switch media.MediaType {
  474. case string(mediaDao.Audio):
  475. productType = productDao.Audio
  476. case string(mediaDao.Video):
  477. productType = productDao.Video
  478. default:
  479. logger.Error("未知媒体类型:%s", media.MediaType)
  480. productType = ""
  481. }
  482. product, pdErr := productService.GetProductBySourceId(media.MediaId, productType)
  483. if pdErr != nil {
  484. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  485. media.Price = defaultProductPrice
  486. media.IsFree = true
  487. media.IsSubscribe = false
  488. } else {
  489. media.Price = defaultProductPrice
  490. media.IsFree = false
  491. media.IsSubscribe = false
  492. }
  493. return
  494. }
  495. media.Price = product.Price.String()
  496. media.IsFree = false
  497. if isLogin {
  498. subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  499. if subscribeErr != nil {
  500. media.IsSubscribe = false
  501. } else {
  502. media.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  503. }
  504. }
  505. pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
  506. if parseErr != nil {
  507. return
  508. }
  509. rpRiskNum, parseErr := parseRiskLevel(media.RiskLevel)
  510. if parseErr != nil {
  511. return
  512. }
  513. if rpRiskNum <= pdRiskNum {
  514. media.RiskLevel = product.RiskLevel
  515. }
  516. }(&list[i])
  517. }
  518. wg.Wait()
  519. if err != nil {
  520. err = exception.New(exception.QueryReportPageFailed)
  521. }
  522. return
  523. }
  524. func getMediaPermissionNames(id []int) (labels []string) {
  525. permissions, err := mediaService.GetPermissionsByIds(id)
  526. if err != nil {
  527. logger.Error("查询品种名称列表失败:%v", err)
  528. labels = []string{}
  529. return
  530. }
  531. for _, permission := range permissions {
  532. labels = append(labels, permission.PermissionName)
  533. }
  534. return
  535. }
  536. // stringToIntSlice 将一个包含数字字符串的切片转换为整数切片
  537. func SearchMediaList(mediaType string, key string, mediaIds []int, pageInfo page.PageInfo, isLogin bool, userId int) (medias []mediaService.MediaDTO, err error) {
  538. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  539. medias, err = mediaService.SearchMediaList(mediaType, key, mediaIds, offset, pageInfo.PageSize, pageInfo.LatestId)
  540. var wg sync.WaitGroup
  541. wg.Add(len(medias))
  542. for i := 0; i < len(medias); i++ {
  543. go func(media *mediaService.MediaDTO) {
  544. defer wg.Done()
  545. idStr := strings.Split(media.PermissionIDs, ",")
  546. var ids []int
  547. ids, err = stringUtils.StringToIntSlice(idStr)
  548. if err != nil {
  549. logger.Error("获取品种列表失败:%v", err)
  550. }
  551. media.PermissionNames = getMediaPermissionNames(ids)
  552. permissions, permissionErr := getMediaSecondPermissions(ids)
  553. if permissionErr != nil {
  554. logger.Error("获取媒体品种信息失败:%v,无法设置媒体风险等级", err)
  555. }
  556. riskNum := getHighestRiskLevel(permissions)
  557. media.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  558. if !isLogin {
  559. media.Src = ""
  560. }
  561. media.Login = isLogin
  562. var productType productDao.MerchantProductType
  563. switch media.MediaType {
  564. case string(mediaDao.Audio):
  565. productType = productDao.Audio
  566. case string(mediaDao.Video):
  567. productType = productDao.Video
  568. default:
  569. logger.Error("未知媒体类型:%s", media.MediaType)
  570. productType = ""
  571. }
  572. product, pdErr := productService.GetProductBySourceId(media.MediaId, productType)
  573. if pdErr != nil {
  574. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  575. media.Price = defaultProductPrice
  576. media.IsFree = true
  577. media.IsSubscribe = false
  578. } else {
  579. media.Price = defaultProductPrice
  580. media.IsFree = false
  581. media.IsSubscribe = false
  582. }
  583. return
  584. }
  585. media.Price = product.Price.String()
  586. media.IsFree = false
  587. if isLogin {
  588. subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  589. if subscribeErr != nil {
  590. media.IsSubscribe = false
  591. } else {
  592. media.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  593. }
  594. }
  595. pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
  596. if parseErr != nil {
  597. return
  598. }
  599. rpRiskNum, parseErr := parseRiskLevel(media.RiskLevel)
  600. if parseErr != nil {
  601. return
  602. }
  603. if rpRiskNum <= pdRiskNum {
  604. media.RiskLevel = product.RiskLevel
  605. }
  606. }(&medias[i])
  607. }
  608. wg.Wait()
  609. if err != nil {
  610. err = exception.New(exception.SearchReportPageFailed)
  611. }
  612. return
  613. }