media_service.go 19 KB

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