report_service.go 23 KB

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