report_service.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. package report
  2. import (
  3. "encoding/json"
  4. "errors"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/exception"
  7. "eta/eta_mini_ht_api/common/utils/date"
  8. "eta/eta_mini_ht_api/common/utils/page"
  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. reportService "eta/eta_mini_ht_api/domian/report"
  13. userService "eta/eta_mini_ht_api/domian/user"
  14. productDao "eta/eta_mini_ht_api/models/merchant"
  15. "eta/eta_mini_ht_api/service/config"
  16. user "eta/eta_mini_ht_api/service/user"
  17. "fmt"
  18. "gorm.io/gorm"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. )
  24. const (
  25. SourceETA = "ETA"
  26. SourceHT = "HT"
  27. RiskLevelUnMatch = "unMatch"
  28. RiskLevelUnTest = "unTest"
  29. RiskLevelExpired = "expired"
  30. RiskLevelMatch = "match"
  31. defaultProductPrice = "0"
  32. )
  33. type PublishRankedReport struct {
  34. Id int `json:"reportId"`
  35. OrgId int `json:"orgId"`
  36. Title string `json:"title"`
  37. Abstract string `json:"abstract"`
  38. SecondPermissions map[int]string `json:"-"`
  39. Permissions map[int]string `json:"-"`
  40. PermissionNames interface{} `json:"permissionNames,omitempty"`
  41. PublishedTime string `json:"publishedTime"`
  42. CoverUrl string `json:"coverUrl"`
  43. RiskLevel string `json:"riskLevel"`
  44. IsFree bool `json:"isFree"`
  45. Price string `json:"price"`
  46. IsSubscribe bool `json:"isSubscribe"`
  47. Login bool `json:"login"`
  48. ProductId int `json:"productId"`
  49. }
  50. type HotRankedReport struct {
  51. Id int `json:"reportId"`
  52. OrgId int `json:"orgId"`
  53. Abstract string `json:"abstract"`
  54. Count int `json:"count"`
  55. Title string `json:"title"`
  56. PublishedTime string `json:"publishedTime"`
  57. SecondPermissions map[int]string `json:"-"`
  58. Permissions map[int]string `json:"-"`
  59. PermissionNames interface{} `json:"permissionNames,omitempty"`
  60. CoverUrl string `json:"coverUrl"`
  61. RiskLevel string `json:"riskLevel"`
  62. IsFree bool `json:"isFree"`
  63. Price string `json:"price"`
  64. ProductId int `json:"productId"`
  65. IsSubscribe bool `json:"isSubscribe"`
  66. Login bool `json:"login"`
  67. }
  68. type RecordCount struct {
  69. UserId int
  70. TraceId string
  71. Mobile string
  72. ReportId int
  73. IpAddress string
  74. Location string
  75. Referer string
  76. Additional string
  77. }
  78. func matchRiskLevel(userId int, report reportService.ReportDTO) (riskLevelMatch string, riskLevel string, err error) {
  79. userProfile, userErr := user.GetUserProfile(userId)
  80. if userErr != nil {
  81. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  82. logger.Error("用户信息不存在,mobile:%d", userProfile.Mobile)
  83. err = exception.New(exception.TemplateUserNotFound)
  84. return
  85. } else {
  86. logger.Error("获取用户信息失败:%v", userErr)
  87. err = exception.New(exception.TemplateUserFoundFailed)
  88. return
  89. }
  90. }
  91. //比较风险等级
  92. if userProfile.RiskLevelStatus == user.RiskUnTest {
  93. logger.Info("客户风险等级未测试,mobile:%d", userProfile.Mobile)
  94. riskLevelMatch = RiskLevelUnTest
  95. return
  96. }
  97. if userProfile.RiskLevelStatus == user.RiskExpired {
  98. logger.Info("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  99. riskLevelMatch = RiskLevelExpired
  100. return
  101. }
  102. level, err := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  103. if err != nil {
  104. logger.Error("获取eta报告风险等级失败:%v", err)
  105. return
  106. }
  107. permissions := reportService.GetReportSecondPermissionsById(report.OrgId, report.Source)
  108. if len(permissions) == 0 {
  109. logger.Error("获取eta报告分类失败:%v", err)
  110. riskLevelMatch = RiskLevelUnMatch
  111. return
  112. }
  113. var permissionIds []int
  114. for _, permission := range permissions {
  115. permissionIds = append(permissionIds, permission.PermissionId)
  116. }
  117. permissionDTOs, err := permissionService.GetPermissionListByIds(permissionIds)
  118. if err != nil {
  119. logger.Error("获取品种风险等级失败:%v", err)
  120. return
  121. }
  122. //能够查看最高等级
  123. matchNum, err := config.ParseRiskLevel(level.ProductRiskLevel)
  124. if err != nil {
  125. logger.Error("解析风险等级失败:%v", err)
  126. return
  127. }
  128. if len(permissionDTOs) == 0 {
  129. logger.Error("当前报告对应品种未设置风险等级")
  130. err = exception.New(exception.ReportRiskLevelUnSet)
  131. return
  132. }
  133. //能够查看需要的最小等级
  134. //num := getLowestRiskLevel(permissionDTOs)
  135. num := config.GetHighestRiskLevel(permissionDTOs)
  136. riskLevel = fmt.Sprintf("R%d", num)
  137. if num > matchNum {
  138. riskLevelMatch = RiskLevelUnMatch
  139. return
  140. } else {
  141. riskLevelMatch = RiskLevelMatch
  142. return
  143. }
  144. }
  145. func GetReportById(reportId int, login bool, userId int) (report reportService.ReportDTO, err error) {
  146. report, err = reportService.GetReportById(reportId)
  147. if err != nil {
  148. logger.Error("获取研报失败:%v", err)
  149. err = exception.New(exception.GetReportFailed)
  150. return
  151. }
  152. var status string
  153. status, report.RiskLevel, err = matchRiskLevel(userId, report)
  154. if err != nil {
  155. logger.Error("匹配风险等级失败:%v", err)
  156. err = exception.New(exception.ReportRiskLevelUnSet)
  157. return
  158. }
  159. var pdfUrl string
  160. switch report.Source {
  161. case SourceETA:
  162. var detail reportService.ETAReportDTO
  163. detail, err = getETAReportDetail(&report)
  164. if err != nil {
  165. logger.Error("获取研报详情失败失败:%v", err)
  166. err = exception.New(exception.GetReportFailed)
  167. return
  168. }
  169. if !login {
  170. detail.Content = ""
  171. report.RiskLevelStatus = RiskLevelUnMatch
  172. report.Login = false
  173. } else {
  174. if status != RiskLevelMatch {
  175. detail.Content = ""
  176. }
  177. report.RiskLevelStatus = status
  178. report.Login = true
  179. }
  180. var jsonStr []byte
  181. jsonStr, err = json.Marshal(detail)
  182. if err != nil {
  183. logger.Error("生成研报详情失败:%v", err)
  184. err = exception.New(exception.GetReportFailed)
  185. }
  186. report.Detail = jsonStr
  187. return
  188. case SourceHT:
  189. pdfUrl, err = getHTReportDetail(&report)
  190. if err != nil {
  191. logger.Error("获取研报详情失败失败:%v")
  192. err = exception.New(exception.GetReportFailed)
  193. return
  194. }
  195. if !login {
  196. report.PdfUrl = ""
  197. report.RiskLevelStatus = RiskLevelUnMatch
  198. report.Login = false
  199. } else {
  200. if status == RiskLevelMatch {
  201. report.PdfUrl = pdfUrl
  202. }
  203. report.RiskLevelStatus = status
  204. report.Login = true
  205. }
  206. return
  207. default:
  208. logger.Error("不支持的研报来演:%v")
  209. err = exception.New(exception.GetReportFailed)
  210. return
  211. }
  212. }
  213. func getETAReportDetail(report *reportService.ReportDTO) (etaReport reportService.ETAReportDTO, err error) {
  214. return reportService.GetETAReport(report.OrgId)
  215. }
  216. func getHTReportDetail(report *reportService.ReportDTO) (url string, err error) {
  217. return reportService.GetHtReport(report.OrgId)
  218. }
  219. func GetTotalPageCountByPermissionIds(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  220. return getCount(permissionIds, isLogin, userId)
  221. }
  222. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  223. if riskLevel != "" {
  224. riskLevelNum, err := config.ParseRiskLevel(riskLevel)
  225. if err != nil {
  226. logger.Error("风险等级解析失败:%v", err)
  227. return
  228. }
  229. for _, permission := range permissionList {
  230. pRiskNum, riskErr := config.ParseRiskLevel(permission.RiskLevel)
  231. if riskErr != nil {
  232. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  233. continue
  234. }
  235. if pRiskNum <= riskLevelNum {
  236. resultList = append(resultList, permission)
  237. }
  238. }
  239. } else {
  240. resultList = permissionList
  241. }
  242. return
  243. }
  244. // ParseRiskLevel 解析风险等级字符串,并返回数字部分
  245. func SearchReportList(key string, Ids []int, pageInfo page.PageInfo, isLogin bool, userId int) (list []reportService.ReportDTO, err error) {
  246. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  247. var reports []reportService.ReportDTO
  248. reports, err = reportService.SearchReportList(key, Ids, offset, pageInfo.PageSize, pageInfo.LatestId)
  249. list, err = dealReportInfo(reports, isLogin, userId)
  250. if err != nil {
  251. err = exception.New(exception.SearchReportPageFailed)
  252. }
  253. return
  254. }
  255. func SearchReportProduct(key string, docIds []int) (list []reportService.ReportDTO, err error) {
  256. list, err = reportService.SearchReportProduct(key, 100, 0, docIds)
  257. if err != nil {
  258. err = exception.New(exception.SearchReportPageFailed)
  259. }
  260. return
  261. }
  262. func RangeSearchByAnalyst(analystName string, userId int) (total int64, latestId int64, ids []int) {
  263. return getCountByAnalyst(nil, true, userId, analystName)
  264. }
  265. func RangeSearch(key string, isLogin bool, userId int) (total int64, latestId int64, reportIds []int, err error) {
  266. var orgIds map[string][]int
  267. _, latestId, orgIds = getCount(nil, isLogin, userId)
  268. reportIds, err = GetReportByIdListByOrgIds(orgIds)
  269. if err != nil {
  270. logger.Error("获取报告ID列表失败:%v", err)
  271. err = exception.NewWithException(exception.GetReportSearchRangeFailed, err.Error())
  272. return
  273. }
  274. total = reportService.SearchMaxReportIdWithRange(key, reportIds)
  275. return
  276. }
  277. func dealReportInfo(list []reportService.ReportDTO, isLogin bool, userId int) (resultList []reportService.ReportDTO, err error) {
  278. var wg sync.WaitGroup
  279. wg.Add(len(list))
  280. for i := 0; i < len(list); i++ {
  281. go func(report *reportService.ReportDTO) {
  282. defer wg.Done()
  283. report, err = DealReportInfo(report, isLogin, userId)
  284. if err != nil {
  285. logger.Error("处理报告信息失败:%v", err)
  286. }
  287. }(&list[i])
  288. }
  289. wg.Wait()
  290. resultList = list
  291. return
  292. }
  293. func DealReportInfo(report *reportService.ReportDTO, isLogin bool, userId int) (resultReport *reportService.ReportDTO, err error) {
  294. report.Login = isLogin
  295. report.Permissions, report.PermissionNames = GetReportPermissionNames(report.OrgId, report.Source)
  296. var permissions []permissionService.PermissionDTO
  297. permissions, report.SecondPermission = getReportSecondPermissions(report.OrgId, report.Source)
  298. if len(permissions) == 0 {
  299. resultReport = report
  300. return
  301. }
  302. riskNum := config.GetHighestRiskLevel(permissions)
  303. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  304. var src string
  305. src, err = mediaService.GetImageSrc(report.CoverSrc)
  306. if err != nil {
  307. logger.Error("获取图片地址失败:%v", err)
  308. src = ""
  309. } else {
  310. report.CoverUrl = src
  311. }
  312. //套餐信息
  313. var packageList []productService.MerchantProductDTO
  314. //查询产品信息
  315. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  316. if pdErr != nil {
  317. var permissionIds []int
  318. if len(permissions) > 0 {
  319. for _, permission := range permissions {
  320. permissionIds = append(permissionIds, permission.PermissionId)
  321. }
  322. //单品不存在的话查套餐
  323. packageList, err = productService.GetProductListBySourceIds(permissionIds, "package")
  324. if err != nil || len(packageList) == 0 {
  325. logger.Error("获取套餐列表失败:%v", err)
  326. report.Price = defaultProductPrice
  327. report.IsFree = true
  328. report.IsSubscribe = false
  329. report.IsPackage = false
  330. } else {
  331. report.Price = defaultProductPrice
  332. report.IsFree = false
  333. report.IsSubscribe = false
  334. report.IsPackage = true
  335. report.ProductId = packageList[0].Id
  336. }
  337. } else {
  338. report.Price = defaultProductPrice
  339. report.IsFree = true
  340. report.IsSubscribe = false
  341. report.IsPackage = false
  342. }
  343. } else {
  344. report.Price = product.Price.String()
  345. report.IsFree = false
  346. report.ProductId = product.Id
  347. report.IsPackage = false
  348. }
  349. if isLogin {
  350. var productIds []int
  351. if len(packageList) > 0 {
  352. for _, packageItem := range packageList {
  353. productIds = append(productIds, packageItem.Id)
  354. }
  355. }
  356. if product.Id > 0 {
  357. productIds = append(productIds, product.Id)
  358. }
  359. subscribeList, subscribeErr := userService.GetUserSubscribe(productIds, userId)
  360. if subscribeErr != nil {
  361. report.IsSubscribe = false
  362. } else {
  363. for _, subscribe := range subscribeList {
  364. if subscribe.Status == productDao.SubscribeValid {
  365. report.IsSubscribe = true
  366. break
  367. }
  368. }
  369. }
  370. }
  371. resultReport = report
  372. return
  373. }
  374. // GetReportPage 分页获取报告列表
  375. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bool, isLogin bool, userId int) (reports []reportService.ReportDTO, err error) {
  376. var list []reportService.ReportDTO
  377. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
  378. reports, err = dealReportInfo(list, isLogin, userId)
  379. if err != nil {
  380. err = exception.New(exception.QueryReportPageFailed)
  381. }
  382. return
  383. }
  384. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int) (list []reportService.ReportDTO, err error) {
  385. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
  386. //并发获取研报的标签
  387. var wg sync.WaitGroup
  388. wg.Add(len(list))
  389. for i := 0; i < len(list); i++ {
  390. go func(report *reportService.ReportDTO) {
  391. defer wg.Done()
  392. report.Permissions, report.PermissionNames = GetReportPermissionNames(report.OrgId, report.Source)
  393. }(&list[i])
  394. }
  395. wg.Wait()
  396. if err != nil {
  397. err = exception.New(exception.QueryReportPageFailed)
  398. }
  399. return
  400. }
  401. func CountReport(count RecordCount) (traceId string, err error) {
  402. dto := convertToRecordCountDTO(count)
  403. return userService.CountReport(dto)
  404. }
  405. func GetRandedReportByWeeklyHot(limit int, isLogin bool, userId int, pdRiskLevel string) (reports []HotRankedReport, err error) {
  406. end := time.Now()
  407. begin := date.GetBeginOfTheWeek(end, time.Monday)
  408. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  409. if len(hotReports) > 0 {
  410. var dtoList []reportService.ReportDTO
  411. var ids []int
  412. for i := 0; i < len(hotReports); i++ {
  413. ids = append(ids, hotReports[i].ReportId)
  414. }
  415. dtoList, err = reportService.GetListByCondition("id", ids)
  416. if err != nil {
  417. logger.Error("获取本周最热研报列表失败:%v", err)
  418. err = exception.New(exception.GetHotRandListFailed)
  419. return
  420. }
  421. dtoList, err = dealReportInfo(dtoList, isLogin, userId)
  422. if err != nil {
  423. logger.Error("获取本周最热研报列表失败:%v", err)
  424. err = exception.New(exception.GetHotRandListFailed)
  425. return
  426. }
  427. var filterList []reportService.ReportDTO
  428. if pdRiskLevel != "" {
  429. for _, report := range dtoList {
  430. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  431. if paresErr != nil {
  432. logger.Error("解析风险等级失败:%v", err)
  433. continue
  434. }
  435. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  436. if paresErr != nil {
  437. logger.Error("解析风险等级失败:%v", err)
  438. continue
  439. }
  440. if pdRiskNum <= reRiskNum {
  441. filterList = append(filterList, report)
  442. }
  443. }
  444. } else {
  445. filterList = dtoList
  446. }
  447. reports = make([]HotRankedReport, len(ids))
  448. for i := 0; i < len(filterList); i++ {
  449. report := convertToHotRankedReport(filterList[i])
  450. for j := 0; j < len(hotReports); j++ {
  451. if hotReports[j].ReportId == report.Id {
  452. report.Count = hotReports[j].Count
  453. reports[j] = report
  454. break
  455. }
  456. }
  457. }
  458. } else {
  459. reports = []HotRankedReport{}
  460. }
  461. return
  462. }
  463. func GetRandedReportByPublishTimeWeekly(limit int, week bool, isLogin bool, userId int, pdRiskLevel string) (reports []PublishRankedReport, err error) {
  464. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  465. if err != nil {
  466. logger.Error("获取最新发布的研报列表失败:%v", err)
  467. err = exception.New(exception.GetPublishedRandListFailed)
  468. return
  469. }
  470. var filterList []reportService.ReportDTO
  471. dtoList, err = dealReportInfo(dtoList, isLogin, userId)
  472. if err != nil {
  473. logger.Error("获取最新发布的研报列表失败:%v", err)
  474. err = exception.New(exception.GetPublishedRandListFailed)
  475. return
  476. }
  477. if pdRiskLevel != "" {
  478. for _, report := range dtoList {
  479. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  480. if paresErr != nil {
  481. logger.Error("解析风险等级失败:%v", err)
  482. continue
  483. }
  484. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  485. if paresErr != nil {
  486. logger.Error("解析风险等级失败:%v", err)
  487. continue
  488. }
  489. if pdRiskNum <= reRiskNum {
  490. filterList = append(filterList, report)
  491. }
  492. }
  493. } else {
  494. filterList = dtoList
  495. }
  496. reports = convertToPublishRankedReportList(filterList)
  497. return
  498. }
  499. func GetReportPermissionNames(id int, source string) (permissionMap map[int]string, labels []string) {
  500. permissions := reportService.GetReportPermissionsById(id, source)
  501. permissionMap = make(map[int]string, len(permissions))
  502. if len(permissions) > 0 {
  503. for _, permission := range permissions {
  504. labels = append(labels, permission.PermissionName)
  505. permissionMap[permission.PermissionId] = permission.PermissionName
  506. }
  507. }
  508. return
  509. }
  510. func GetReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  511. permissionMap = make(map[int]string)
  512. permissions := reportService.GetReportSecondPermissionsById(id, source)
  513. for _, permission := range permissions {
  514. permissionMap[permission.PermissionId] = permission.PermissionName
  515. }
  516. return
  517. }
  518. func getReportSecondPermissions(id int, source string) (permissionList []permissionService.PermissionDTO, secondPermissionMap map[int]string) {
  519. permissionList = reportService.GetReportSecondPermissionsById(id, source)
  520. if len(permissionList) > 0 {
  521. secondPermissionMap = make(map[int]string, len(permissionList))
  522. for _, permission := range permissionList {
  523. secondPermissionMap[permission.PermissionId] = permission.PermissionName
  524. }
  525. }
  526. return
  527. }
  528. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  529. permissionMap = make(map[int]string)
  530. permissions := reportService.GetReportPermissionsById(id, source)
  531. for _, permission := range permissions {
  532. permissionMap[permission.PermissionId] = permission.PermissionName
  533. }
  534. return
  535. }
  536. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  537. return permissionService.GetPermissionList()
  538. }
  539. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  540. report = HotRankedReport{
  541. Id: dto.ReportID,
  542. OrgId: dto.OrgId,
  543. Abstract: dto.Abstract,
  544. PublishedTime: dto.PublishedTime,
  545. Title: dto.Title,
  546. SecondPermissions: dto.SecondPermission,
  547. Permissions: dto.Permissions,
  548. PermissionNames: dto.PermissionNames,
  549. CoverUrl: dto.CoverUrl,
  550. IsSubscribe: dto.IsSubscribe,
  551. IsFree: dto.IsFree,
  552. Price: dto.Price,
  553. RiskLevel: dto.RiskLevel,
  554. Login: dto.Login,
  555. ProductId: dto.ProductId,
  556. }
  557. return
  558. }
  559. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  560. reports = []PublishRankedReport{}
  561. for _, dto := range dtoList {
  562. risk, err := config.ParseRiskLevel(dto.RiskLevel)
  563. if err != nil || risk == 0 {
  564. continue
  565. }
  566. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  567. if err != nil {
  568. logger.Error("获取封面图片失败:%v", err)
  569. src = ""
  570. }
  571. report := PublishRankedReport{
  572. Id: dto.ReportID,
  573. OrgId: dto.OrgId,
  574. PublishedTime: dto.PublishedTime,
  575. Abstract: dto.Abstract,
  576. Title: dto.Title,
  577. Permissions: dto.Permissions,
  578. SecondPermissions: dto.SecondPermission,
  579. PermissionNames: dto.PermissionNames,
  580. CoverUrl: src,
  581. IsSubscribe: dto.IsSubscribe,
  582. IsFree: dto.IsFree,
  583. Price: dto.Price,
  584. RiskLevel: dto.RiskLevel,
  585. Login: dto.Login,
  586. ProductId: dto.ProductId,
  587. }
  588. reports = append(reports, report)
  589. }
  590. return
  591. }
  592. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  593. return userService.RecordCountDTO{
  594. UserId: record.UserId,
  595. TraceId: record.TraceId,
  596. Mobile: record.Mobile,
  597. SourceId: record.ReportId,
  598. IpAddress: record.IpAddress,
  599. Location: record.Location,
  600. Referer: record.Referer,
  601. Additional: record.Additional,
  602. }
  603. }
  604. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  605. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  606. if err != nil {
  607. logger.Error("获取报告ID列表失败:%v", err)
  608. err = exception.New(exception.GetReportSearchRangeFailed)
  609. }
  610. return
  611. }
  612. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  613. return CheckUserRisk(nil, isLogin, userId)
  614. }
  615. func CheckUserRisk(permissionIds []int, isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  616. if isLogin {
  617. userProfile, userErr := user.GetUserProfile(userId)
  618. if userErr != nil {
  619. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  620. err = exception.New(exception.TemplateUserNotFound)
  621. } else {
  622. err = exception.New(exception.TemplateUserFoundFailed)
  623. }
  624. logger.Error("分页查询列表失败:%v", err)
  625. return
  626. }
  627. //获取产品风险等级
  628. if userProfile.RiskLevelStatus == user.RiskUnTest {
  629. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  630. }
  631. if userProfile.RiskLevelStatus == user.RiskExpired {
  632. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  633. }
  634. var mapping permissionService.CustomerProductRiskMappingDTO
  635. if userProfile.RiskLevel != "" {
  636. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  637. if err != nil {
  638. logger.Error("查询产品风险等级映射失败:%v", err)
  639. return
  640. }
  641. }
  642. var permissionList []permissionService.PermissionDTO
  643. if len(permissionIds) == 0 {
  644. //获取所有设置风险等级的品种
  645. permissionList, err = permissionService.GetPermissionListWithRisk()
  646. } else {
  647. //更具id过滤设置了风险等级的品种
  648. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  649. }
  650. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  651. riskLevel = mapping.ProductRiskLevel
  652. if len(permissionList) == 0 {
  653. return
  654. }
  655. for _, permission := range permissionList {
  656. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  657. }
  658. return
  659. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  660. var permissionList []permissionService.PermissionDTO
  661. if len(permissionIds) == 0 {
  662. //获取所有设置风险等级的品种
  663. permissionList, err = permissionService.GetPermissionListWithRisk()
  664. } else {
  665. //更具id过滤设置了风险等级的品种
  666. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  667. }
  668. if err != nil {
  669. logger.Error("根据ID查询品种列表失败:%v", err)
  670. }
  671. for _, permission := range permissionList {
  672. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  673. }
  674. //查询品种
  675. return
  676. }
  677. }
  678. func getCount(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  679. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  680. if err != nil {
  681. logger.Error("校验用户风险等级失败:%v", err)
  682. return
  683. }
  684. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, riskLevel)
  685. }
  686. func getCountByAnalyst(permissionIds []int, isLogin bool, userId int, analystName string) (total int64, latestId int64, ids []int) {
  687. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  688. if err != nil {
  689. logger.Error("校验用户风险等级失败:%v", err)
  690. return
  691. }
  692. return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds, riskLevel)
  693. }
  694. func CountPermissionWeight(ids []int) (permissionMap map[int]int) {
  695. list, err := reportService.CountPermissionWeight(ids)
  696. permissionMap = make(map[int]int)
  697. if err != nil {
  698. return
  699. }
  700. for _, item := range list {
  701. permissionMap[item.PermissionId] = item.Weight
  702. }
  703. return
  704. }