report_service.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. permissions = reportService.GetReportSecondPermissionsById(report.OrgId, report.Source)
  301. var permissionIds []int
  302. if len(permissions) > 0 {
  303. for _, permission := range permissions {
  304. permissionIds = append(permissionIds, permission.PermissionId)
  305. }
  306. //单品不存在的话查套餐
  307. productList, prodErr := productService.GetProductListBySourceIds(permissionIds, "package")
  308. if prodErr != nil || len(productList) == 0 {
  309. logger.Error("获取套餐列表失败:%v", pdErr)
  310. report.Price = defaultProductPrice
  311. report.IsFree = true
  312. report.IsSubscribe = false
  313. report.IsPackage = false
  314. } else {
  315. report.Price = defaultProductPrice
  316. report.IsFree = true
  317. report.IsSubscribe = false
  318. report.IsPackage = true
  319. report.ProductId = productList[0].Id
  320. }
  321. } else {
  322. report.Price = defaultProductPrice
  323. report.IsFree = true
  324. report.IsSubscribe = false
  325. report.IsPackage = false
  326. }
  327. } else {
  328. report.Price = defaultProductPrice
  329. report.IsFree = false
  330. report.IsSubscribe = false
  331. report.IsPackage = false
  332. }
  333. } else {
  334. report.Price = product.Price.String()
  335. report.IsFree = false
  336. report.ProductId = product.Id
  337. report.IsPackage = false
  338. if isLogin {
  339. subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  340. if subscribeErr != nil {
  341. report.IsSubscribe = false
  342. } else {
  343. report.IsSubscribe = subscribe.Status == productDao.SubscribeValid
  344. }
  345. }
  346. }
  347. }(&list[i])
  348. }
  349. wg.Wait()
  350. resultList = list
  351. return
  352. }
  353. // GetReportPage 分页获取报告列表
  354. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bool, isLogin bool, userId int) (reports []reportService.ReportDTO, err error) {
  355. var list []reportService.ReportDTO
  356. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
  357. reports, err = dealReportInfo(list, isLogin, userId)
  358. if err != nil {
  359. err = exception.New(exception.QueryReportPageFailed)
  360. }
  361. return
  362. }
  363. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int) (list []reportService.ReportDTO, err error) {
  364. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
  365. //并发获取研报的标签
  366. var wg sync.WaitGroup
  367. wg.Add(len(list))
  368. for i := 0; i < len(list); i++ {
  369. go func(report *reportService.ReportDTO) {
  370. defer wg.Done()
  371. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  372. }(&list[i])
  373. }
  374. wg.Wait()
  375. if err != nil {
  376. err = exception.New(exception.QueryReportPageFailed)
  377. }
  378. return
  379. }
  380. func CountReport(count RecordCount) (traceId string, err error) {
  381. dto := convertToRecordCountDTO(count)
  382. return userService.CountReport(dto)
  383. }
  384. func GetRandedReportByWeeklyHot(limit int, isLogin bool, userId int, pdRiskLevel string) (reports []HotRankedReport, err error) {
  385. end := time.Now()
  386. begin := date.GetBeginOfTheWeek(end, time.Monday)
  387. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  388. if len(hotReports) > 0 {
  389. var dtoList []reportService.ReportDTO
  390. var ids []int
  391. for i := 0; i < len(hotReports); i++ {
  392. ids = append(ids, hotReports[i].ReportId)
  393. }
  394. dtoList, err = reportService.GetListByCondition("id", ids)
  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. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  404. if pdErr != nil {
  405. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  406. report.Price = defaultProductPrice
  407. report.IsFree = true
  408. report.IsSubscribe = false
  409. } else {
  410. logger.Error("查询产品失败:%v", pdErr)
  411. report.Price = defaultProductPrice
  412. report.IsFree = false
  413. report.IsSubscribe = false
  414. }
  415. filterList = append(filterList, report)
  416. } else {
  417. pdRiskNum, paresErr := config.ParseRiskLevel(product.RiskLevel)
  418. if paresErr != nil {
  419. logger.Error("解析风险等级失败:%v", err)
  420. continue
  421. }
  422. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  423. if paresErr != nil {
  424. logger.Error("解析风险等级失败:%v", err)
  425. continue
  426. }
  427. report.RiskLevel = product.RiskLevel
  428. if isLogin {
  429. subscribe, subErr := userService.GetUserSubscribe(product.Id, userId)
  430. if subErr != nil {
  431. logger.Error("查询用户订阅信息失败:%v,productId:%v,userId:%v", err, product.Id, userId)
  432. report.Price = product.Price.String()
  433. report.IsFree = false
  434. report.IsSubscribe = false
  435. } else {
  436. report.Price = product.Price.String()
  437. report.IsFree = false
  438. report.IsSubscribe = subscribe.Status == productDao.SubscribeValid
  439. }
  440. }
  441. if pdRiskNum <= reRiskNum {
  442. filterList = append(filterList, report)
  443. }
  444. }
  445. }
  446. } else {
  447. filterList = dtoList
  448. for _, report := range filterList {
  449. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  450. if pdErr != nil {
  451. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  452. report.Price = defaultProductPrice
  453. report.IsFree = true
  454. report.IsSubscribe = false
  455. } else {
  456. logger.Error("查询产品失败:%v", pdErr)
  457. report.Price = defaultProductPrice
  458. report.IsFree = false
  459. report.IsSubscribe = false
  460. }
  461. } else {
  462. report.Price = product.Price.String()
  463. report.IsFree = false
  464. report.IsSubscribe = false
  465. report.RiskLevel = product.RiskLevel
  466. }
  467. }
  468. }
  469. var wg sync.WaitGroup
  470. wg.Add(len(filterList))
  471. for i := 0; i < len(filterList); i++ {
  472. go func(report *reportService.ReportDTO) {
  473. defer wg.Done()
  474. report.Login = isLogin
  475. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  476. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  477. permissions := getReportSecondPermissions(report.OrgId, report.Source)
  478. var riskNum int
  479. if len(permissions) == 0 {
  480. riskNum = 0
  481. } else {
  482. riskNum = config.GetHighestRiskLevel(permissions)
  483. }
  484. if report.RiskLevel == "" {
  485. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  486. } else {
  487. reRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  488. if paresErr != nil {
  489. logger.Error("解析风险等级失败:%v", err)
  490. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  491. } else {
  492. if reRiskNum <= riskNum {
  493. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  494. }
  495. }
  496. }
  497. var label []string
  498. for _, permission := range report.Permissions {
  499. label = append(label, permission)
  500. }
  501. report.PermissionNames = label
  502. }(&filterList[i])
  503. }
  504. wg.Wait()
  505. reports = make([]HotRankedReport, len(ids))
  506. for i := 0; i < len(filterList); i++ {
  507. risk, parseErr := config.ParseRiskLevel(filterList[i].RiskLevel)
  508. if parseErr != nil || risk == 0 {
  509. continue
  510. }
  511. report := convertToHotRankedReport(filterList[i])
  512. for j := 0; j < len(hotReports); j++ {
  513. if hotReports[j].ReportId == report.Id {
  514. report.Count = hotReports[j].Count
  515. reports[j] = report
  516. break
  517. }
  518. }
  519. }
  520. } else {
  521. reports = []HotRankedReport{}
  522. }
  523. return
  524. }
  525. func GetRandedReportByPublishTimeWeekly(limit int, week bool, isLogin bool, userId int, pdRiskLevel string) (reports []PublishRankedReport, err error) {
  526. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  527. if err != nil {
  528. logger.Error("获取最新发布的研报列表失败:%v", err)
  529. err = exception.New(exception.GetPublishedRandListFailed)
  530. return
  531. }
  532. var filterList []reportService.ReportDTO
  533. if pdRiskLevel != "" {
  534. for _, report := range dtoList {
  535. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  536. if pdErr != nil {
  537. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  538. report.Price = defaultProductPrice
  539. report.IsFree = true
  540. report.IsSubscribe = false
  541. } else {
  542. logger.Error("查询产品失败:%v", pdErr)
  543. report.Price = defaultProductPrice
  544. report.IsFree = false
  545. report.IsSubscribe = false
  546. }
  547. filterList = append(filterList, report)
  548. } else {
  549. pdRiskNum, paresErr := config.ParseRiskLevel(product.RiskLevel)
  550. if paresErr != nil {
  551. logger.Error("解析风险等级失败:%v", err)
  552. continue
  553. }
  554. reRiskNum, paresErr := config.ParseRiskLevel(pdRiskLevel)
  555. if paresErr != nil {
  556. logger.Error("解析风险等级失败:%v", err)
  557. continue
  558. }
  559. report.RiskLevel = product.RiskLevel
  560. if isLogin {
  561. subscribe, subErr := userService.GetUserSubscribe(product.Id, userId)
  562. if subErr != nil {
  563. logger.Error("查询用户订阅信息失败:%v,productId:%v,userId:%v", err, product.Id, userId)
  564. report.Price = product.Price.String()
  565. report.IsFree = false
  566. report.IsSubscribe = false
  567. } else {
  568. report.Price = product.Price.String()
  569. report.IsFree = false
  570. report.IsSubscribe = subscribe.Status == productDao.SubscribeValid
  571. }
  572. }
  573. if pdRiskNum <= reRiskNum {
  574. filterList = append(filterList, report)
  575. }
  576. }
  577. }
  578. } else {
  579. filterList = dtoList
  580. for _, report := range filterList {
  581. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  582. if pdErr != nil {
  583. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  584. report.Price = defaultProductPrice
  585. report.IsFree = true
  586. report.IsSubscribe = false
  587. } else {
  588. logger.Error("查询产品失败:%v", pdErr)
  589. report.Price = defaultProductPrice
  590. report.IsFree = false
  591. report.IsSubscribe = false
  592. }
  593. } else {
  594. report.Price = product.Price.String()
  595. report.IsFree = false
  596. report.IsSubscribe = false
  597. report.RiskLevel = product.RiskLevel
  598. }
  599. }
  600. }
  601. //并发获取研报的标签
  602. var wg sync.WaitGroup
  603. wg.Add(len(filterList))
  604. for i := 0; i < len(filterList); i++ {
  605. go func(report *reportService.ReportDTO) {
  606. defer wg.Done()
  607. report.Login = isLogin
  608. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  609. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  610. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  611. var riskNum int
  612. permissions := getReportSecondPermissions(report.OrgId, report.Source)
  613. if len(permissions) == 0 {
  614. riskNum = 0
  615. } else {
  616. riskNum = config.GetHighestRiskLevel(permissions)
  617. }
  618. if report.RiskLevel == "" {
  619. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  620. } else {
  621. reRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  622. if paresErr != nil {
  623. logger.Error("解析风险等级失败:%v", err)
  624. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  625. } else {
  626. if reRiskNum <= riskNum {
  627. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  628. }
  629. }
  630. }
  631. }(&filterList[i])
  632. }
  633. wg.Wait()
  634. reports = convertToPublishRankedReportList(filterList)
  635. return
  636. }
  637. func getReportPermissionNames(id int, source string) (labels []string) {
  638. permissions := reportService.GetReportPermissionsById(id, source)
  639. for _, permission := range permissions {
  640. labels = append(labels, permission.PermissionName)
  641. }
  642. return
  643. }
  644. func getReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  645. permissionMap = make(map[int]string)
  646. permissions := reportService.GetReportSecondPermissionsById(id, source)
  647. for _, permission := range permissions {
  648. permissionMap[permission.PermissionId] = permission.PermissionName
  649. }
  650. return
  651. }
  652. func getReportSecondPermissions(id int, source string) (permissionList []permissionService.PermissionDTO) {
  653. return reportService.GetReportSecondPermissionsById(id, source)
  654. }
  655. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  656. permissionMap = make(map[int]string)
  657. permissions := reportService.GetReportPermissionsById(id, source)
  658. for _, permission := range permissions {
  659. permissionMap[permission.PermissionId] = permission.PermissionName
  660. }
  661. return
  662. }
  663. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  664. return permissionService.GetPermissionList()
  665. }
  666. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  667. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  668. if err != nil {
  669. logger.Error("获取封面图片失败:%v", err)
  670. src = ""
  671. }
  672. report = HotRankedReport{
  673. Id: dto.ReportID,
  674. OrgId: dto.OrgId,
  675. Abstract: dto.Abstract,
  676. PublishedTime: dto.PublishedTime,
  677. Title: dto.Title,
  678. SecondPermissions: dto.SecondPermission,
  679. Permissions: dto.Permissions,
  680. PermissionNames: dto.PermissionNames,
  681. CoverUrl: src,
  682. IsSubscribe: dto.IsSubscribe,
  683. IsFree: dto.IsFree,
  684. Price: dto.Price,
  685. RiskLevel: dto.RiskLevel,
  686. Login: dto.Login,
  687. }
  688. return
  689. }
  690. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  691. reports = []PublishRankedReport{}
  692. for _, dto := range dtoList {
  693. risk, err := config.ParseRiskLevel(dto.RiskLevel)
  694. if err != nil || risk == 0 {
  695. continue
  696. }
  697. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  698. if err != nil {
  699. logger.Error("获取封面图片失败:%v", err)
  700. src = ""
  701. }
  702. report := PublishRankedReport{
  703. Id: dto.ReportID,
  704. OrgId: dto.OrgId,
  705. PublishedTime: dto.PublishedTime,
  706. Abstract: dto.Abstract,
  707. Title: dto.Title,
  708. Permissions: dto.Permissions,
  709. SecondPermissions: dto.SecondPermission,
  710. PermissionNames: dto.PermissionNames,
  711. CoverUrl: src,
  712. IsSubscribe: dto.IsSubscribe,
  713. IsFree: dto.IsFree,
  714. Price: dto.Price,
  715. RiskLevel: dto.RiskLevel,
  716. Login: dto.Login,
  717. }
  718. reports = append(reports, report)
  719. }
  720. return
  721. }
  722. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  723. return userService.RecordCountDTO{
  724. UserId: record.UserId,
  725. TraceId: record.TraceId,
  726. Mobile: record.Mobile,
  727. SourceId: record.ReportId,
  728. IpAddress: record.IpAddress,
  729. Location: record.Location,
  730. Referer: record.Referer,
  731. Additional: record.Additional,
  732. }
  733. }
  734. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  735. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  736. if err != nil {
  737. logger.Error("获取报告ID列表失败:%v", err)
  738. err = exception.New(exception.GetReportSearchRangeFailed)
  739. }
  740. return
  741. }
  742. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  743. return CheckUserRisk(nil, isLogin, userId)
  744. }
  745. func CheckUserRisk(permissionIds []int, isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  746. if isLogin {
  747. userProfile, userErr := user.GetUserProfile(userId)
  748. if userErr != nil {
  749. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  750. err = exception.New(exception.TemplateUserNotFound)
  751. } else {
  752. err = exception.New(exception.TemplateUserFoundFailed)
  753. }
  754. logger.Error("分页查询报告列表失败:%v", err)
  755. return
  756. }
  757. //获取产品风险等级
  758. if userProfile.RiskLevelStatus == user.RiskUnTest {
  759. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  760. }
  761. if userProfile.RiskLevelStatus == user.RiskExpired {
  762. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  763. }
  764. var mapping permissionService.CustomerProductRiskMappingDTO
  765. if userProfile.RiskLevel != "" {
  766. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  767. if err != nil {
  768. logger.Error("查询产品风险等级映射失败:%v", err)
  769. return
  770. }
  771. }
  772. var permissionList []permissionService.PermissionDTO
  773. if len(permissionIds) == 0 {
  774. //获取所有设置风险等级的品种
  775. permissionList, err = permissionService.GetPermissionListWithRisk()
  776. } else {
  777. //更具id过滤设置了风险等级的品种
  778. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  779. }
  780. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  781. riskLevel = mapping.ProductRiskLevel
  782. if len(permissionList) == 0 {
  783. return
  784. }
  785. for _, permission := range permissionList {
  786. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  787. }
  788. return
  789. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  790. var permissionList []permissionService.PermissionDTO
  791. if len(permissionIds) == 0 {
  792. //获取所有设置风险等级的品种
  793. permissionList, err = permissionService.GetPermissionListWithRisk()
  794. } else {
  795. //更具id过滤设置了风险等级的品种
  796. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  797. }
  798. if err != nil {
  799. logger.Error("根据ID查询品种列表失败:%v", err)
  800. }
  801. for _, permission := range permissionList {
  802. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  803. }
  804. //查询品种
  805. return
  806. }
  807. }
  808. func getCount(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  809. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  810. if err != nil {
  811. logger.Error("校验用户风险等级失败:%v", err)
  812. return
  813. }
  814. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, riskLevel)
  815. }
  816. func getCountByAnalyst(permissionIds []int, isLogin bool, userId int, analystName string) (total int64, latestId int64, ids []int) {
  817. filterPermissionIds, riskLevel, err := CheckUserRisk(permissionIds, isLogin, userId)
  818. if err != nil {
  819. logger.Error("校验用户风险等级失败:%v", err)
  820. return
  821. }
  822. return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds, riskLevel)
  823. }