media_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/config"
  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. MediaName string
  36. TraceId string
  37. MediaType string
  38. IpAddress string
  39. Location string
  40. Referer string
  41. Additional string
  42. }
  43. func convertToMediaCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  44. return userService.RecordCountDTO{
  45. UserId: record.UserId,
  46. TraceId: record.TraceId,
  47. Mobile: record.Mobile,
  48. SourceId: record.MediaId,
  49. }
  50. }
  51. func CountMedia(count RecordCount) (traceId string, err error) {
  52. media, err := mediaService.GetMediaById(count.MediaType, count.MediaId)
  53. if err != nil {
  54. err = exception.New(exception.MediaFoundFailed)
  55. return
  56. }
  57. dto := convertToMediaCountDTO(count)
  58. dto.SourceTitle = media.MediaName
  59. return userService.CountMedia(dto, media.MediaType)
  60. }
  61. func RangeSearchByAnalyst(mediaType string, analystId int, userId int) (total int64, latestId int64, ids []int, mappingRiskLevel string) {
  62. //var err error
  63. ////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  64. //userProfile, userErr := user.GetUserProfile(userId)
  65. //if userErr != nil {
  66. // if errors.Is(userErr, gorm.ErrRecordNotFound) {
  67. // err = exception.New(exception.TemplateUserNotFound)
  68. // } else {
  69. // err = exception.New(exception.TemplateUserFoundFailed)
  70. // }
  71. // logger.Error("分页查询报告列表失败:%v", err)
  72. // return
  73. //}
  74. ////获取产品风险等级
  75. //if userProfile.RiskLevel == user.RiskUnTest {
  76. // logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  77. //}
  78. //if userProfile.RiskLevelStatus == user.RiskExpired {
  79. // logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  80. //}
  81. //mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  82. //if mappingErr != nil {
  83. // logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  84. // return
  85. //}
  86. //mappingRiskLevel = mapping.ProductRiskLevel
  87. //var permissionList []permissionService.PermissionDTO
  88. ////获取所有设置风险等级的品种
  89. //permissionList, err = permissionService.GetPermissionListWithRisk()
  90. //permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  91. //if len(permissionList) == 0 {
  92. // return
  93. //}
  94. //var filterPermissionIds []int
  95. //for _, permission := range permissionList {
  96. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  97. //}
  98. filterPermissionIds, riskLevel, err := report.CheckUserRisk(nil, true, userId)
  99. if err != nil {
  100. logger.Error("校验用户风险等级失败:%v", err)
  101. return
  102. }
  103. return mediaService.GetAnalystMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, analystId, riskLevel)
  104. }
  105. func getCount(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, orgIds []int, mappingPdRisk string) {
  106. filterPermissionIds, riskLevel, err := report.CheckUserRisk(permissionIds, isLogin, userId)
  107. if err != nil {
  108. logger.Error("校验用户风险等级失败:%v", err)
  109. return
  110. }
  111. return mediaService.GetMediaPermissionMappingByPermissionIds(mediaType, filterPermissionIds, riskLevel)
  112. }
  113. func RangeSearch(mediaType string, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string) {
  114. return getCount(mediaType, nil, isLogin, userId)
  115. }
  116. func GetTotalPageCountByPermissionIds(mediaType string, permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids []int, mappingPdRisk string) {
  117. if mediaType == "" {
  118. return
  119. }
  120. return getCount(mediaType, permissionIds, isLogin, userId)
  121. }
  122. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  123. riskLevelNum, err := config.ParseRiskLevel(riskLevel)
  124. if err != nil {
  125. logger.Error("风险等级解析失败:%v", err)
  126. return
  127. }
  128. for _, permission := range permissionList {
  129. pRiskNum, riskErr := config.ParseRiskLevel(permission.RiskLevel)
  130. if riskErr != nil {
  131. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  132. continue
  133. }
  134. if pRiskNum <= riskLevelNum {
  135. resultList = append(resultList, permission)
  136. }
  137. }
  138. return
  139. }
  140. func SearchMaxMediaId(mediaType string, key string) (total int64, latestId int64) {
  141. return mediaService.SearchMaxMediaId(mediaType, key)
  142. }
  143. func GetMediaById(mediaType string, mediaId int, isLogin bool, userId int) (media *mediaService.MediaDTO, err error) {
  144. var mappingRiskLevel string
  145. _, mappingRiskLevel, err = report.CheckUserRisk(nil, isLogin, userId)
  146. *media, err = mediaService.GetMediaById(mediaType, mediaId)
  147. if err != nil {
  148. logger.Error("获取媒体失败:%v", err)
  149. err = exception.New(exception.MediaFoundFailed)
  150. return
  151. }
  152. return DealMediaInfo(media, isLogin, userId, mappingRiskLevel)
  153. //var status string
  154. //
  155. //status, media.RiskLevel, err = matchRiskLevel(userId, media)
  156. //if err != nil {
  157. // logger.Error("匹配风险等级失败:%v", err)
  158. // err = exception.New(exception.ReportRiskLevelUnSet)
  159. // return
  160. //}
  161. //idStr := strings.Split(media.PermissionIDs, ",")
  162. //var ids []int
  163. //ids, err = stringUtils.StringToIntSlice(idStr)
  164. //if err != nil {
  165. // logger.Error("品种名称列表转换失败:%v", err)
  166. // return
  167. //}
  168. //media.PermissionNames = GetMediaPermissionNames(ids)
  169. //if isLogin {
  170. // media.Login = true
  171. // if status != RiskLevelMatch {
  172. // media.Src = ""
  173. // }
  174. // media.RiskLevelStatus = status
  175. //} else {
  176. // logger.Info("当前用户未登录,展示部分详情")
  177. // media.Src = ""
  178. // media.RiskLevelStatus = RiskLevelUnMatch
  179. // media.Login = false
  180. //}
  181. return
  182. }
  183. func matchRiskLevel(userId int, media mediaService.MediaDTO) (riskLevelMatch string, productRiskLevel string, err error) {
  184. userProfile, userErr := user.GetUserProfile(userId)
  185. if userErr != nil {
  186. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  187. logger.Error("用户信息不存在,mobile:%d", userProfile.Mobile)
  188. err = exception.New(exception.TemplateUserNotFound)
  189. return
  190. } else {
  191. logger.Error("获取用户信息失败:%v", userErr)
  192. err = exception.New(exception.TemplateUserFoundFailed)
  193. return
  194. }
  195. }
  196. //比较风险等级
  197. if userProfile.RiskLevelStatus == user.RiskUnTest {
  198. logger.Info("客户风险等级未测试,mobile:%v", userProfile.Mobile)
  199. riskLevelMatch = RiskLevelUnTest
  200. return
  201. }
  202. if userProfile.RiskLevelStatus == user.RiskExpired {
  203. logger.Info("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  204. riskLevelMatch = RiskLevelExpired
  205. return
  206. }
  207. level, err := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  208. if err != nil {
  209. logger.Error("获取媒体风险等级设置失败:%v", err)
  210. return
  211. }
  212. var permissionIds []int
  213. permissionStrList := strings.Split(media.PermissionIDs, ",")
  214. permissionIds, err = stringUtils.StringToIntSlice(permissionStrList)
  215. if err != nil {
  216. logger.Error("解析媒体品种ID列表失败:%v", err)
  217. }
  218. permissionDTOs, err := permissionService.GetPermissionListByIds(permissionIds)
  219. if err != nil {
  220. logger.Error("获取品种风险等级失败:%v", err)
  221. return
  222. }
  223. //能够查看最高等级
  224. matchNum, err := config.ParseRiskLevel(level.ProductRiskLevel)
  225. if err != nil {
  226. logger.Error("解析风险等级失败:%v", err)
  227. return
  228. }
  229. if len(permissionDTOs) == 0 {
  230. logger.Error("当前报告对应品种未设置风险等级")
  231. err = exception.New(exception.ReportRiskLevelUnSet)
  232. return
  233. }
  234. //能够查看需要的最小等级
  235. num := config.GetHighestRiskLevel(permissionDTOs)
  236. productRiskLevel = fmt.Sprintf("R%d", num)
  237. if num > matchNum {
  238. riskLevelMatch = RiskLevelUnMatch
  239. return
  240. } else {
  241. riskLevelMatch = RiskLevelMatch
  242. return
  243. }
  244. }
  245. func getLowestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  246. for _, permission := range permissions {
  247. pRiskNum, err := config.ParseRiskLevel(permission.RiskLevel)
  248. if err != nil {
  249. logger.Error("解析风险等级失败:%v", err)
  250. continue
  251. }
  252. if riskLevelNum == 0 {
  253. riskLevelNum = pRiskNum
  254. } else {
  255. if riskLevelNum > pRiskNum {
  256. riskLevelNum = pRiskNum
  257. }
  258. }
  259. }
  260. return
  261. }
  262. func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int, userId int, mappingRiskLevel string) (list []mediaService.MediaDTO, err error) {
  263. list, err = mediaService.GetMediaPageByAnalystId(mediaType, pageInfo, analystId, mediaIds)
  264. if err != nil {
  265. err = exception.New(exception.GetAnalystMediaListFailed)
  266. return
  267. }
  268. if len(list) == 0 {
  269. logger.Info("研究员媒体列表为空")
  270. return
  271. }
  272. //并发获取媒体的标签
  273. list, err = dealMediaInfo(list, true, userId, mappingRiskLevel)
  274. if err != nil {
  275. err = exception.New(exception.GetAnalystMediaListFailed)
  276. }
  277. return
  278. }
  279. func getMediaSecondPermissions(ids []int) (permissionDTOs []configService.PermissionDTO, err error) {
  280. return mediaService.GetSecondPermissionsByIds(ids)
  281. }
  282. func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int, isLogin bool, userId int, mappingRiskLevel string) (list []mediaService.MediaDTO, err error) {
  283. list, err = mediaService.GetMediaPageByIds(mediaType, pageInfo, mediaIds)
  284. if err != nil {
  285. err = exception.New(exception.GetMediaListFailed)
  286. return
  287. }
  288. list, err = dealMediaInfo(list, isLogin, userId, mappingRiskLevel)
  289. if err != nil {
  290. err = exception.New(exception.QueryReportPageFailed)
  291. }
  292. return
  293. }
  294. func GetMediaPermissionNames(id []int) (labels []string) {
  295. permissions, err := mediaService.GetPermissionsByIds(id)
  296. if err != nil {
  297. logger.Error("查询品种名称列表失败:%v", err)
  298. labels = []string{}
  299. return
  300. }
  301. for _, permission := range permissions {
  302. labels = append(labels, permission.PermissionName)
  303. }
  304. return
  305. }
  306. func DealMediaInfo(media *mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string) (resultMedia *mediaService.MediaDTO, err error) {
  307. idStr := strings.Split(media.PermissionIDs, ",")
  308. var ids []int
  309. ids, err = stringUtils.StringToIntSlice(idStr)
  310. if err != nil {
  311. logger.Error("品种名称列表转换失败:%v", err)
  312. }
  313. media.PermissionNames = GetMediaPermissionNames(ids)
  314. permissions, permissionErr := getMediaSecondPermissions(ids)
  315. if permissionErr != nil {
  316. logger.Error("获取媒体品种信息失败:%v,无法设置媒体风险等级", err)
  317. }
  318. riskNum := config.GetHighestRiskLevel(permissions)
  319. media.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  320. if !isLogin {
  321. media.Src = ""
  322. }
  323. media.Login = isLogin
  324. var productType productDao.MerchantProductType
  325. switch media.MediaType {
  326. case string(mediaDao.Audio):
  327. productType = productDao.Audio
  328. case string(mediaDao.Video):
  329. productType = productDao.Video
  330. default:
  331. logger.Error("未知媒体类型:%s", media.MediaType)
  332. productType = ""
  333. }
  334. var packageList []productService.MerchantProductDTO
  335. var permissionIds []int
  336. if len(permissions) > 0 {
  337. for _, permission := range permissions {
  338. permissionIds = append(permissionIds, permission.PermissionId)
  339. }
  340. //单品不存在的话查套餐
  341. packageList, err = productService.GetProductListBySourceIds(permissionIds, "package")
  342. if err != nil {
  343. logger.Error("获取套餐列表失败:%v", err)
  344. }
  345. }
  346. product, pdErr := productService.GetProductBySourceId(media.MediaId, productType)
  347. if pdErr != nil {
  348. if len(packageList) == 0 {
  349. logger.Error("获取套餐列表失败:%v", err)
  350. media.Price = defaultProductPrice
  351. media.IsFree = true
  352. media.IsSubscribe = false
  353. media.IsPackage = false
  354. } else {
  355. media.Price = defaultProductPrice
  356. media.IsFree = false
  357. media.IsSubscribe = false
  358. media.IsPackage = true
  359. media.ProductId = packageList[0].Id
  360. }
  361. } else {
  362. media.Price = product.Price
  363. media.IsFree = false
  364. media.IsPackage = false
  365. media.IsSubscribe = false
  366. media.ProductId = product.Id
  367. }
  368. if isLogin {
  369. var productIds []int
  370. if len(packageList) > 0 {
  371. for _, packageItem := range packageList {
  372. productIds = append(productIds, packageItem.Id)
  373. }
  374. }
  375. if product.Id > 0 {
  376. productIds = append(productIds, product.Id)
  377. }
  378. subscribeList, subscribeErr := userService.GetUserSubscribe(productIds, templateUserId)
  379. if subscribeErr != nil {
  380. media.IsSubscribe = false
  381. } else {
  382. for _, subscribe := range subscribeList {
  383. if subscribe.Status == productDao.SubscribeValid {
  384. media.IsSubscribe = true
  385. break
  386. }
  387. }
  388. }
  389. }
  390. media.RiskLevelStatus = RiskLevelUnMatch
  391. mappingRiskNum, parseErr := config.ParseRiskLevel(mappingRiskLevel)
  392. if parseErr != nil {
  393. return
  394. }
  395. var rpRiskNum int
  396. rpRiskNum, parseErr = config.ParseRiskLevel(media.RiskLevel)
  397. if parseErr != nil {
  398. return
  399. }
  400. if rpRiskNum <= mappingRiskNum {
  401. media.RiskLevelStatus = RiskLevelMatch
  402. }
  403. resultMedia = media
  404. return
  405. }
  406. // stringToIntSlice 将一个包含数字字符串的切片转换为整数切片
  407. func dealMediaInfo(list []mediaService.MediaDTO, isLogin bool, templateUserId int, mappingRiskLevel string) (dealList []mediaService.MediaDTO, err error) {
  408. //并发获取媒体的标签
  409. var wg sync.WaitGroup
  410. wg.Add(len(list))
  411. for i := 0; i < len(list); i++ {
  412. go func(media *mediaService.MediaDTO) {
  413. defer wg.Done()
  414. media, err = DealMediaInfo(media, isLogin, templateUserId, mappingRiskLevel)
  415. if err != nil {
  416. logger.Error("处理媒体信息失败:%v", err)
  417. }
  418. }(&list[i])
  419. }
  420. wg.Wait()
  421. dealList = list
  422. return
  423. }
  424. func SearchMediaList(mediaType string, key string, mediaIds []int, pageInfo page.PageInfo, isLogin bool, userId int, mappingRiskLevel string) (medias []mediaService.MediaDTO, err error) {
  425. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  426. medias, err = mediaService.SearchMediaList(mediaType, key, mediaIds, offset, pageInfo.PageSize, pageInfo.LatestId)
  427. medias, err = dealMediaInfo(medias, isLogin, userId, mappingRiskLevel)
  428. if err != nil {
  429. err = exception.New(exception.SearchReportPageFailed)
  430. }
  431. return
  432. }
  433. func SearchMediaProduct(key string, docIds []int) (list []mediaService.MediaDTO, err error) {
  434. list, err = mediaService.SearchMediaProduct(key, 100, 0, docIds)
  435. if err != nil {
  436. err = exception.New(exception.SearchReportPageFailed)
  437. }
  438. return
  439. }
  440. func CountPermissionWeight(ids []int) (permissionMap map[int]int) {
  441. list, err := mediaService.CountPermissionWeight(ids)
  442. if err != nil {
  443. return
  444. }
  445. for _, permission := range list {
  446. permissionMap[permission.PermissionId] = permission.Weight
  447. }
  448. return
  449. }