report_service.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 RangeSearchByAnalyst(analystName string, userId int) (total int64, latestId int64, ids []int) {
  256. return getCountByAnalyst(nil, true, userId, analystName)
  257. }
  258. func RangeSearch(key string, isLogin bool, userId int) (total int64, latestId int64, reportIds []int, err error) {
  259. var orgIds map[string][]int
  260. _, latestId, orgIds = getCount(nil, isLogin, userId)
  261. reportIds, err = GetReportByIdListByOrgIds(orgIds)
  262. if err != nil {
  263. logger.Error("获取报告ID列表失败:%v", err)
  264. err = exception.NewWithException(exception.GetReportSearchRangeFailed, err.Error())
  265. return
  266. }
  267. total = reportService.SearchMaxReportIdWithRange(key, reportIds)
  268. return
  269. }
  270. func dealReportInfo(list []reportService.ReportDTO, isLogin bool, userId int) (resultList []reportService.ReportDTO, err error) {
  271. var wg sync.WaitGroup
  272. wg.Add(len(list))
  273. for i := 0; i < len(list); i++ {
  274. go func(report *reportService.ReportDTO) {
  275. defer wg.Done()
  276. report.Login = isLogin
  277. report.Permissions, report.PermissionNames = GetReportPermissionNames(report.OrgId, report.Source)
  278. var permissions []permissionService.PermissionDTO
  279. permissions, report.SecondPermission = getReportSecondPermissions(report.OrgId, report.Source)
  280. if len(permissions) == 0 {
  281. return
  282. }
  283. riskNum := config.GetHighestRiskLevel(permissions)
  284. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  285. var src string
  286. src, err = mediaService.GetImageSrc(report.CoverSrc)
  287. if err != nil {
  288. logger.Error("获取图片地址失败:%v", err)
  289. src = ""
  290. } else {
  291. report.CoverUrl = src
  292. }
  293. //套餐信息
  294. //查询产品信息
  295. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  296. if pdErr != nil {
  297. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  298. var permissionIds []int
  299. if len(permissions) > 0 {
  300. for _, permission := range permissions {
  301. permissionIds = append(permissionIds, permission.PermissionId)
  302. }
  303. //单品不存在的话查套餐
  304. productList, prodErr := productService.GetProductListBySourceIds(permissionIds, "package")
  305. if prodErr != nil || len(productList) == 0 {
  306. logger.Error("获取套餐列表失败:%v", pdErr)
  307. report.Price = defaultProductPrice
  308. report.IsFree = true
  309. report.IsSubscribe = false
  310. report.IsPackage = false
  311. } else {
  312. report.Price = defaultProductPrice
  313. report.IsFree = true
  314. report.IsSubscribe = false
  315. report.IsPackage = true
  316. report.ProductId = productList[0].Id
  317. }
  318. } else {
  319. report.Price = defaultProductPrice
  320. report.IsFree = true
  321. report.IsSubscribe = false
  322. report.IsPackage = false
  323. }
  324. } else {
  325. report.Price = defaultProductPrice
  326. report.IsFree = false
  327. report.IsSubscribe = false
  328. report.IsPackage = false
  329. }
  330. } else {
  331. report.Price = product.Price.String()
  332. report.IsFree = false
  333. report.ProductId = product.Id
  334. report.IsPackage = false
  335. if isLogin {
  336. subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  337. if subscribeErr != nil {
  338. report.IsSubscribe = false
  339. } else {
  340. report.IsSubscribe = subscribe.Status == productDao.SubscribeValid
  341. }
  342. }
  343. }
  344. }(&list[i])
  345. }
  346. wg.Wait()
  347. resultList = list
  348. return
  349. }
  350. // GetReportPage 分页获取报告列表
  351. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bool, isLogin bool, userId int) (reports []reportService.ReportDTO, err error) {
  352. var list []reportService.ReportDTO
  353. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
  354. reports, err = dealReportInfo(list, isLogin, userId)
  355. if err != nil {
  356. err = exception.New(exception.QueryReportPageFailed)
  357. }
  358. return
  359. }
  360. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int) (list []reportService.ReportDTO, err error) {
  361. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
  362. //并发获取研报的标签
  363. var wg sync.WaitGroup
  364. wg.Add(len(list))
  365. for i := 0; i < len(list); i++ {
  366. go func(report *reportService.ReportDTO) {
  367. defer wg.Done()
  368. report.Permissions, report.PermissionNames = GetReportPermissionNames(report.OrgId, report.Source)
  369. }(&list[i])
  370. }
  371. wg.Wait()
  372. if err != nil {
  373. err = exception.New(exception.QueryReportPageFailed)
  374. }
  375. return
  376. }
  377. func CountReport(count RecordCount) (traceId string, err error) {
  378. dto := convertToRecordCountDTO(count)
  379. return userService.CountReport(dto)
  380. }
  381. func GetRandedReportByWeeklyHot(limit int, isLogin bool, userId int, pdRiskLevel string) (reports []HotRankedReport, err error) {
  382. end := time.Now()
  383. begin := date.GetBeginOfTheWeek(end, time.Monday)
  384. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  385. if len(hotReports) > 0 {
  386. var dtoList []reportService.ReportDTO
  387. var ids []int
  388. for i := 0; i < len(hotReports); i++ {
  389. ids = append(ids, hotReports[i].ReportId)
  390. }
  391. dtoList, err = reportService.GetListByCondition("id", ids)
  392. if err != nil {
  393. logger.Error("获取本周最热研报列表失败:%v", err)
  394. err = exception.New(exception.GetHotRandListFailed)
  395. return
  396. }
  397. dtoList, err = dealReportInfo(dtoList, isLogin, userId)
  398. if err != nil {
  399. logger.Error("获取本周最热研报列表失败:%v", err)
  400. err = exception.New(exception.GetHotRandListFailed)
  401. return
  402. }
  403. var filterList []reportService.ReportDTO
  404. if pdRiskLevel != "" {
  405. for _, report := range dtoList {
  406. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  407. if paresErr != nil {
  408. logger.Error("解析风险等级失败:%v", err)
  409. continue
  410. }
  411. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  412. if paresErr != nil {
  413. logger.Error("解析风险等级失败:%v", err)
  414. continue
  415. }
  416. if pdRiskNum <= reRiskNum {
  417. filterList = append(filterList, report)
  418. }
  419. }
  420. } else {
  421. filterList = dtoList
  422. }
  423. reports = make([]HotRankedReport, len(ids))
  424. for i := 0; i < len(filterList); i++ {
  425. report := convertToHotRankedReport(filterList[i])
  426. for j := 0; j < len(hotReports); j++ {
  427. if hotReports[j].ReportId == report.Id {
  428. report.Count = hotReports[j].Count
  429. reports[j] = report
  430. break
  431. }
  432. }
  433. }
  434. } else {
  435. reports = []HotRankedReport{}
  436. }
  437. return
  438. }
  439. func GetRandedReportByPublishTimeWeekly(limit int, week bool, isLogin bool, userId int, pdRiskLevel string) (reports []PublishRankedReport, err error) {
  440. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  441. if err != nil {
  442. logger.Error("获取最新发布的研报列表失败:%v", err)
  443. err = exception.New(exception.GetPublishedRandListFailed)
  444. return
  445. }
  446. var filterList []reportService.ReportDTO
  447. dtoList, err = dealReportInfo(dtoList, isLogin, userId)
  448. if err != nil {
  449. logger.Error("获取最新发布的研报列表失败:%v", err)
  450. err = exception.New(exception.GetPublishedRandListFailed)
  451. return
  452. }
  453. if pdRiskLevel != "" {
  454. for _, report := range dtoList {
  455. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  456. if paresErr != nil {
  457. logger.Error("解析风险等级失败:%v", err)
  458. continue
  459. }
  460. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  461. if paresErr != nil {
  462. logger.Error("解析风险等级失败:%v", err)
  463. continue
  464. }
  465. if pdRiskNum <= reRiskNum {
  466. filterList = append(filterList, report)
  467. }
  468. }
  469. } else {
  470. filterList = dtoList
  471. }
  472. reports = convertToPublishRankedReportList(filterList)
  473. return
  474. }
  475. func GetReportPermissionNames(id int, source string) (permissionMap map[int]string, labels []string) {
  476. permissions := reportService.GetReportPermissionsById(id, source)
  477. permissionMap = make(map[int]string, len(permissions))
  478. if len(permissions) > 0 {
  479. for _, permission := range permissions {
  480. labels = append(labels, permission.PermissionName)
  481. permissionMap[permission.PermissionId] = permission.PermissionName
  482. }
  483. }
  484. return
  485. }
  486. func GetReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  487. permissionMap = make(map[int]string)
  488. permissions := reportService.GetReportSecondPermissionsById(id, source)
  489. for _, permission := range permissions {
  490. permissionMap[permission.PermissionId] = permission.PermissionName
  491. }
  492. return
  493. }
  494. func getReportSecondPermissions(id int, source string) (permissionList []permissionService.PermissionDTO, secondPermissionMap map[int]string) {
  495. permissionList = reportService.GetReportSecondPermissionsById(id, source)
  496. if len(permissionList) > 0 {
  497. secondPermissionMap = make(map[int]string, len(permissionList))
  498. for _, permission := range permissionList {
  499. secondPermissionMap[permission.PermissionId] = permission.PermissionName
  500. }
  501. }
  502. return
  503. }
  504. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  505. permissionMap = make(map[int]string)
  506. permissions := reportService.GetReportPermissionsById(id, source)
  507. for _, permission := range permissions {
  508. permissionMap[permission.PermissionId] = permission.PermissionName
  509. }
  510. return
  511. }
  512. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  513. return permissionService.GetPermissionList()
  514. }
  515. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  516. report = HotRankedReport{
  517. Id: dto.ReportID,
  518. OrgId: dto.OrgId,
  519. Abstract: dto.Abstract,
  520. PublishedTime: dto.PublishedTime,
  521. Title: dto.Title,
  522. SecondPermissions: dto.SecondPermission,
  523. Permissions: dto.Permissions,
  524. PermissionNames: dto.PermissionNames,
  525. CoverUrl: dto.CoverUrl,
  526. IsSubscribe: dto.IsSubscribe,
  527. IsFree: dto.IsFree,
  528. Price: dto.Price,
  529. RiskLevel: dto.RiskLevel,
  530. Login: dto.Login,
  531. ProductId: dto.ProductId,
  532. }
  533. return
  534. }
  535. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  536. reports = []PublishRankedReport{}
  537. for _, dto := range dtoList {
  538. risk, err := config.ParseRiskLevel(dto.RiskLevel)
  539. if err != nil || risk == 0 {
  540. continue
  541. }
  542. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  543. if err != nil {
  544. logger.Error("获取封面图片失败:%v", err)
  545. src = ""
  546. }
  547. report := PublishRankedReport{
  548. Id: dto.ReportID,
  549. OrgId: dto.OrgId,
  550. PublishedTime: dto.PublishedTime,
  551. Abstract: dto.Abstract,
  552. Title: dto.Title,
  553. Permissions: dto.Permissions,
  554. SecondPermissions: dto.SecondPermission,
  555. PermissionNames: dto.PermissionNames,
  556. CoverUrl: src,
  557. IsSubscribe: dto.IsSubscribe,
  558. IsFree: dto.IsFree,
  559. Price: dto.Price,
  560. RiskLevel: dto.RiskLevel,
  561. Login: dto.Login,
  562. ProductId: dto.ProductId,
  563. }
  564. reports = append(reports, report)
  565. }
  566. return
  567. }
  568. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  569. return userService.RecordCountDTO{
  570. UserId: record.UserId,
  571. TraceId: record.TraceId,
  572. Mobile: record.Mobile,
  573. SourceId: record.ReportId,
  574. IpAddress: record.IpAddress,
  575. Location: record.Location,
  576. Referer: record.Referer,
  577. Additional: record.Additional,
  578. }
  579. }
  580. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  581. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  582. if err != nil {
  583. logger.Error("获取报告ID列表失败:%v", err)
  584. err = exception.New(exception.GetReportSearchRangeFailed)
  585. }
  586. return
  587. }
  588. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  589. return CheckUserRisk(nil, isLogin, userId)
  590. }
  591. func CheckUserRisk(permissionIds []int, isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  592. if isLogin {
  593. userProfile, userErr := user.GetUserProfile(userId)
  594. if userErr != nil {
  595. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  596. err = exception.New(exception.TemplateUserNotFound)
  597. } else {
  598. err = exception.New(exception.TemplateUserFoundFailed)
  599. }
  600. logger.Error("分页查询列表失败:%v", err)
  601. return
  602. }
  603. //获取产品风险等级
  604. if userProfile.RiskLevelStatus == user.RiskUnTest {
  605. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  606. }
  607. if userProfile.RiskLevelStatus == user.RiskExpired {
  608. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  609. }
  610. var mapping permissionService.CustomerProductRiskMappingDTO
  611. if userProfile.RiskLevel != "" {
  612. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  613. if err != nil {
  614. logger.Error("查询产品风险等级映射失败:%v", err)
  615. return
  616. }
  617. }
  618. var permissionList []permissionService.PermissionDTO
  619. if len(permissionIds) == 0 {
  620. //获取所有设置风险等级的品种
  621. permissionList, err = permissionService.GetPermissionListWithRisk()
  622. } else {
  623. //更具id过滤设置了风险等级的品种
  624. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  625. }
  626. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  627. riskLevel = mapping.ProductRiskLevel
  628. if len(permissionList) == 0 {
  629. return
  630. }
  631. for _, permission := range permissionList {
  632. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  633. }
  634. return
  635. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  636. var permissionList []permissionService.PermissionDTO
  637. if len(permissionIds) == 0 {
  638. //获取所有设置风险等级的品种
  639. permissionList, err = permissionService.GetPermissionListWithRisk()
  640. } else {
  641. //更具id过滤设置了风险等级的品种
  642. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  643. }
  644. if err != nil {
  645. logger.Error("根据ID查询品种列表失败:%v", err)
  646. }
  647. for _, permission := range permissionList {
  648. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  649. }
  650. //查询品种
  651. return
  652. }
  653. }
  654. func getCount(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  655. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  656. if err != nil {
  657. logger.Error("校验用户风险等级失败:%v", err)
  658. return
  659. }
  660. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, riskLevel)
  661. }
  662. func getCountByAnalyst(permissionIds []int, isLogin bool, userId int, analystName string) (total int64, latestId int64, ids []int) {
  663. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  664. if err != nil {
  665. logger.Error("校验用户风险等级失败:%v", err)
  666. return
  667. }
  668. return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds, riskLevel)
  669. }