report_service.go 23 KB

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