report_service.go 27 KB

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