report_service.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. reportService "eta/eta_mini_ht_api/domian/report"
  12. userService "eta/eta_mini_ht_api/domian/user"
  13. user "eta/eta_mini_ht_api/service/user"
  14. "fmt"
  15. "gorm.io/gorm"
  16. "strconv"
  17. "strings"
  18. "sync"
  19. "time"
  20. )
  21. const (
  22. SourceETA = "ETA"
  23. SourceHT = "HT"
  24. RiskLevelUnMatch = "unMatch"
  25. RiskLevelUnTest = "unTest"
  26. RiskLevelExpired = "expired"
  27. RiskLevelMatch = "match"
  28. )
  29. type PublishRankedReport struct {
  30. Id int `json:"reportId"`
  31. OrgId int `json:"orgId"`
  32. Title string `json:"title"`
  33. Abstract string `json:"abstract"`
  34. SecondPermissions map[int]string `json:"-"`
  35. Permissions map[int]string `json:"-"`
  36. PermissionNames interface{} `json:"permissionNames,omitempty"`
  37. PublishedTime string `json:"publishedTime"`
  38. CoverUrl string `json:"coverUrl"`
  39. }
  40. type HotRankedReport struct {
  41. Id int `json:"reportId"`
  42. OrgId int `json:"orgId"`
  43. Abstract string `json:"abstract"`
  44. Count int `json:"count"`
  45. Title string `json:"title"`
  46. PublishedTime string `json:"publishedTime"`
  47. SecondPermissions map[int]string `json:"-"`
  48. Permissions map[int]string `json:"-"`
  49. PermissionNames interface{} `json:"permissionNames,omitempty"`
  50. CoverUrl string `json:"coverUrl"`
  51. }
  52. //type PermissionNode struct {
  53. // ID int `json:"id"`
  54. // Name string `json:"name"`
  55. // ParentID int `json:"parentId"`
  56. // Children []*PermissionNode `json:"children,omitempty"`
  57. //}
  58. type RecordCount struct {
  59. UserId int
  60. Mobile string
  61. ReportId int
  62. IpAddress string
  63. Location string
  64. Referer string
  65. Additional string
  66. }
  67. func matchRiskLevel(userId int, report reportService.ReportDTO) (riskLevelMatch string, err error) {
  68. userProfile, userErr := user.GetUserProfile(userId)
  69. if userErr != nil {
  70. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  71. logger.Error("用户信息不存在,mobile:%d", userProfile.Mobile)
  72. err = exception.New(exception.TemplateUserNotFound)
  73. return
  74. } else {
  75. logger.Error("获取用户信息失败:%v", userErr)
  76. err = exception.New(exception.TemplateUserFoundFailed)
  77. return
  78. }
  79. }
  80. //比较风险等级
  81. if userProfile.RiskLevelStatus == user.RiskUnTest {
  82. logger.Info("客户风险等级未测试,mobile:%d", userProfile.Mobile)
  83. riskLevelMatch = RiskLevelUnTest
  84. return
  85. }
  86. if userProfile.RiskLevelStatus == user.RiskExpired {
  87. logger.Info("客户风险等级已过期,mobile:%d", userProfile.Mobile)
  88. riskLevelMatch = RiskLevelExpired
  89. return
  90. }
  91. level, err := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  92. if err != nil {
  93. logger.Error("获取eta报告风险等级失败:%v", err)
  94. return
  95. }
  96. permissions := reportService.GetReportSecondPermissionsById(report.OrgId, report.Source)
  97. if len(permissions) == 0 {
  98. logger.Error("获取eta报告分类失败:%v", err)
  99. riskLevelMatch = RiskLevelUnMatch
  100. return
  101. }
  102. var permissionIds []int
  103. for _, permission := range permissions {
  104. permissionIds = append(permissionIds, permission.ID)
  105. }
  106. permissionDTOs, err := permissionService.GetPermissionListByIds(permissionIds)
  107. if err != nil {
  108. logger.Error("获取品种风险等级失败:%v", err)
  109. return
  110. }
  111. //能够查看最高等级
  112. matchNum, err := parseRiskLevel(level.ProductRiskLevel)
  113. if err != nil {
  114. logger.Error("解析风险等级失败:%v", err)
  115. return
  116. }
  117. if len(permissionDTOs) == 0 {
  118. logger.Error("当前报告对应品种未设置风险等级")
  119. err = exception.New(exception.ReportRiskLevelUnSet)
  120. return
  121. }
  122. //能够查看需要的最小等级
  123. num := getLowestRiskLevel(permissionDTOs)
  124. if num > matchNum {
  125. riskLevelMatch = RiskLevelUnMatch
  126. return
  127. } else {
  128. riskLevelMatch = RiskLevelMatch
  129. return
  130. }
  131. }
  132. func getLowestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  133. for _, permission := range permissions {
  134. pRiskNum, err := parseRiskLevel(permission.RiskLevel)
  135. if err != nil {
  136. logger.Error("解析风险等级失败:%v", err)
  137. continue
  138. }
  139. if riskLevelNum == 0 {
  140. riskLevelNum = pRiskNum
  141. } else {
  142. if riskLevelNum > pRiskNum {
  143. riskLevelNum = pRiskNum
  144. }
  145. }
  146. }
  147. return
  148. }
  149. func GetReportById(reportId int, login bool, userId int) (report reportService.ReportDTO, err error) {
  150. report, err = reportService.GetGetReportById(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, 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 GetTotalPageCount() (total int64, latestId int64, err error) {
  224. return reportService.GetTotalPageCount()
  225. }
  226. func GetTotalPageCountByPermissionIds(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  227. var err error
  228. //登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  229. if isLogin {
  230. userProfile, userErr := user.GetUserProfile(userId)
  231. if userErr != nil {
  232. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  233. err = exception.New(exception.TemplateUserNotFound)
  234. } else {
  235. err = exception.New(exception.TemplateUserFoundFailed)
  236. }
  237. logger.Error("分页查询报告列表失败:%v", err)
  238. return
  239. }
  240. //获取产品风险等级
  241. if userProfile.RiskLevel == user.RiskUnTest {
  242. logger.Error("客户未做风险等级测评,mobile:%d", userProfile.Mobile)
  243. return
  244. }
  245. if userProfile.RiskLevelStatus == user.RiskExpired {
  246. logger.Error("客户风险等级已过期,mobile:%d", userProfile.Mobile)
  247. return
  248. }
  249. mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  250. if mappingErr != nil {
  251. logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  252. return
  253. }
  254. var permissionList []permissionService.PermissionDTO
  255. if len(permissionIds) == 0 {
  256. //获取所有设置风险等级的品种
  257. permissionList, err = permissionService.GetPermissionListWithRisk()
  258. } else {
  259. //更具id过滤设置了风险等级的品种
  260. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  261. }
  262. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  263. if len(permissionList) == 0 {
  264. return
  265. }
  266. var filterPermissionIds []int
  267. for _, permission := range permissionList {
  268. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  269. }
  270. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds)
  271. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  272. var permissionList []permissionService.PermissionDTO
  273. if len(permissionIds) == 0 {
  274. //获取所有设置风险等级的品种
  275. permissionList, err = permissionService.GetPermissionListWithRisk()
  276. } else {
  277. //更具id过滤设置了风险等级的品种
  278. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  279. }
  280. if err != nil {
  281. logger.Error("根据ID查询品种列表失败:%v", err)
  282. }
  283. var filterPermissionIds []int
  284. for _, permission := range permissionList {
  285. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  286. }
  287. //查询品种
  288. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds)
  289. }
  290. }
  291. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  292. riskLevelNum, err := parseRiskLevel(riskLevel)
  293. if err != nil {
  294. logger.Error("风险等级解析失败:%v", err)
  295. return
  296. }
  297. for _, permission := range permissionList {
  298. pRiskNum, riskErr := parseRiskLevel(permission.RiskLevel)
  299. if riskErr != nil {
  300. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  301. continue
  302. }
  303. if pRiskNum <= riskLevelNum {
  304. resultList = append(resultList, permission)
  305. }
  306. }
  307. return
  308. }
  309. // ParseRiskLevel 解析风险等级字符串,并返回数字部分
  310. func parseRiskLevel(level string) (int, error) {
  311. parts := strings.Split(level, "R")
  312. if len(parts) < 2 {
  313. return 0, fmt.Errorf("无效的风险等级: %s", level)
  314. }
  315. numberStr := parts[1]
  316. number, err := strconv.Atoi(numberStr)
  317. if err != nil {
  318. return 0, fmt.Errorf("无法将风险等级转换为数字: %s", err)
  319. }
  320. return number, nil
  321. }
  322. func SearchReportList(key string, Ids []int, pageInfo page.PageInfo) (reports []reportService.ReportDTO, err error) {
  323. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  324. reports, err = reportService.SearchReportList(key, Ids, offset, pageInfo.PageSize, pageInfo.LatestId)
  325. var wg sync.WaitGroup
  326. wg.Add(len(reports))
  327. for i := 0; i < len(reports); i++ {
  328. go func(report *reportService.ReportDTO) {
  329. defer wg.Done()
  330. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  331. var src string
  332. src, err = mediaService.GetImageSrc(report.CoverSrc)
  333. if err != nil {
  334. logger.Error("获取图片地址失败:%v", err)
  335. src = ""
  336. } else {
  337. report.CoverUrl = src
  338. }
  339. }(&reports[i])
  340. }
  341. wg.Wait()
  342. if err != nil {
  343. err = exception.New(exception.SearchReportPageFailed)
  344. }
  345. return
  346. }
  347. func SearchMaxReportId(key string) (total int64, id int64) {
  348. return reportService.SearchMaxReportId(key)
  349. }
  350. func RangeSearch(isLogin bool, userId int) (total int64, latestId int64, orgIds map[string][]int) {
  351. var err error
  352. //登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  353. if isLogin {
  354. userProfile, userErr := user.GetUserProfile(userId)
  355. if userErr != nil {
  356. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  357. err = exception.New(exception.TemplateUserNotFound)
  358. } else {
  359. err = exception.New(exception.TemplateUserFoundFailed)
  360. }
  361. logger.Error("分页查询报告列表失败:%v", err)
  362. return
  363. }
  364. //获取产品风险等级
  365. if userProfile.RiskLevel == user.RiskUnTest {
  366. logger.Error("客户未做风险等级测评,mobile:%d", userProfile.Mobile)
  367. return
  368. }
  369. if userProfile.RiskLevelStatus == user.RiskExpired {
  370. logger.Error("客户风险等级已过期,mobile:%d", userProfile.Mobile)
  371. return
  372. }
  373. mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  374. if mappingErr != nil {
  375. logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  376. return
  377. }
  378. var permissionList []permissionService.PermissionDTO
  379. //获取所有设置风险等级的品种
  380. permissionList, err = permissionService.GetPermissionListWithRisk()
  381. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  382. if len(permissionList) == 0 {
  383. return
  384. }
  385. var filterPermissionIds []int
  386. for _, permission := range permissionList {
  387. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  388. }
  389. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds)
  390. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  391. var permissionList []permissionService.PermissionDTO
  392. //获取所有设置风险等级的品种
  393. permissionList, err = permissionService.GetPermissionListWithRisk()
  394. if err != nil {
  395. logger.Error("根据ID查询品种列表失败:%v", err)
  396. }
  397. var filterPermissionIds []int
  398. for _, permission := range permissionList {
  399. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  400. }
  401. //查询品种
  402. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds)
  403. }
  404. }
  405. // GetReportPage 分页获取报告列表
  406. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bool, isLogin bool) (list []reportService.ReportDTO, err error) {
  407. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
  408. //并发获取研报的标签
  409. var wg sync.WaitGroup
  410. wg.Add(len(list))
  411. for i := 0; i < len(list); i++ {
  412. go func(report *reportService.ReportDTO) {
  413. defer wg.Done()
  414. report.Login = isLogin
  415. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  416. var src string
  417. src, err = mediaService.GetImageSrc(report.CoverSrc)
  418. if err != nil {
  419. logger.Error("获取图片地址失败:%v", err)
  420. src = ""
  421. } else {
  422. report.CoverUrl = src
  423. }
  424. }(&list[i])
  425. }
  426. wg.Wait()
  427. if err != nil {
  428. err = exception.New(exception.QueryReportPageFailed)
  429. }
  430. return
  431. }
  432. func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
  433. return reportService.GetTotalPageCountByAnalyst(analyst)
  434. }
  435. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string) (list []reportService.ReportDTO, err error) {
  436. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst)
  437. //并发获取研报的标签
  438. var wg sync.WaitGroup
  439. wg.Add(len(list))
  440. for i := 0; i < len(list); i++ {
  441. go func(report *reportService.ReportDTO) {
  442. defer wg.Done()
  443. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  444. }(&list[i])
  445. }
  446. wg.Wait()
  447. if err != nil {
  448. err = exception.New(exception.QueryReportPageFailed)
  449. }
  450. return
  451. }
  452. func CountReport(count RecordCount) error {
  453. dto := convertToRecordCountDTO(count)
  454. return userService.CountReport(dto)
  455. }
  456. func GetRandedReportByWeeklyHot(limit int) (reports []HotRankedReport, err error) {
  457. end := time.Now()
  458. begin := date.GetBeginOfTheWeek(end, time.Monday)
  459. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  460. if len(hotReports) > 0 {
  461. var dtoList []reportService.ReportDTO
  462. var ids []int
  463. for i := 0; i < len(hotReports); i++ {
  464. ids = append(ids, hotReports[i].ReportId)
  465. }
  466. dtoList, err = reportService.GetListByCondition("id", ids)
  467. if err != nil {
  468. logger.Error("获取本周最热研报列表失败:%v", err)
  469. err = exception.New(exception.GetHotRandListFailed)
  470. return
  471. }
  472. var wg sync.WaitGroup
  473. wg.Add(len(dtoList))
  474. for i := 0; i < len(dtoList); i++ {
  475. go func(report *reportService.ReportDTO) {
  476. defer wg.Done()
  477. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  478. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  479. var label []string
  480. for _, permission := range report.Permissions {
  481. label = append(label, permission)
  482. }
  483. report.PermissionNames = label
  484. }(&dtoList[i])
  485. }
  486. wg.Wait()
  487. reports = make([]HotRankedReport, len(ids))
  488. for i := 0; i < len(dtoList); i++ {
  489. report := convertToHotRankedReport(dtoList[i])
  490. for j := 0; j < len(hotReports); j++ {
  491. if hotReports[j].ReportId == report.Id {
  492. report.Count = hotReports[j].Count
  493. reports[j] = report
  494. break
  495. }
  496. }
  497. }
  498. } else {
  499. reports = []HotRankedReport{}
  500. }
  501. return
  502. }
  503. func GetRandedReportByPublishTimeWeekly(limit int, week bool) (reports []PublishRankedReport, err error) {
  504. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  505. if err != nil {
  506. logger.Error("获取最新发布的研报列表失败:%v", err)
  507. err = exception.New(exception.GetPublishedRandListFailed)
  508. return
  509. }
  510. //并发获取研报的标签
  511. var wg sync.WaitGroup
  512. wg.Add(len(dtoList))
  513. for i := 0; i < len(dtoList); i++ {
  514. go func(report *reportService.ReportDTO) {
  515. defer wg.Done()
  516. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  517. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  518. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  519. }(&dtoList[i])
  520. }
  521. wg.Wait()
  522. reports = convertToPublishRankedReportList(dtoList)
  523. return
  524. }
  525. func getReportPermissionNames(id int, source string) (labels []string) {
  526. permissions := reportService.GetReportPermissionsById(id, source)
  527. for _, permission := range permissions {
  528. labels = append(labels, permission.Name)
  529. }
  530. return
  531. }
  532. func getReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  533. permissionMap = make(map[int]string)
  534. permissions := reportService.GetReportSecondPermissionsById(id, source)
  535. for _, permission := range permissions {
  536. permissionMap[permission.ID] = permission.Name
  537. }
  538. return
  539. }
  540. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  541. permissionMap = make(map[int]string)
  542. permissions := reportService.GetReportPermissionsById(id, source)
  543. for _, permission := range permissions {
  544. permissionMap[permission.ID] = permission.Name
  545. }
  546. return
  547. }
  548. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  549. return permissionService.GetPermissionList()
  550. //if err != nil {
  551. // logger.Error("获取品种列表失败:%v", err)
  552. // err = exception.New(exception.GetPermissionListFailed)
  553. // return
  554. //}
  555. ////root = &PermissionNode{
  556. //// ID: 0,
  557. //// ParentID: 0,
  558. ////}
  559. ////assemblePermissionNode(list, root, 0, 2)
  560. //return
  561. }
  562. // func assemblePermissionNode(list []reportService.PermissionDTO, node *PermissionNode, current int, level int) {
  563. // if node != nil && current < level {
  564. // for _, permission := range list {
  565. // if permission.ParentID == node.ID {
  566. // childNode := &PermissionNode{
  567. // ID: permission.ID,
  568. // Name: permission.Name,
  569. // ParentID: permission.ParentID,
  570. // }
  571. // node.Children = append(node.Children, childNode)
  572. // assemblePermissionNode(list, childNode, current+1, level)
  573. // }
  574. // }
  575. // }
  576. // }
  577. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  578. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  579. if err != nil {
  580. logger.Error("获取封面图片失败:%v", err)
  581. src = ""
  582. }
  583. report = HotRankedReport{
  584. Id: dto.ReportID,
  585. OrgId: dto.OrgId,
  586. Abstract: dto.Abstract,
  587. PublishedTime: dto.PublishedTime,
  588. Title: dto.Title,
  589. SecondPermissions: dto.SecondPermission,
  590. Permissions: dto.Permissions,
  591. PermissionNames: dto.PermissionNames,
  592. CoverUrl: src,
  593. }
  594. return
  595. }
  596. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  597. reports = []PublishRankedReport{}
  598. for _, dto := range dtoList {
  599. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  600. if err != nil {
  601. logger.Error("获取封面图片失败:%v", err)
  602. src = ""
  603. }
  604. report := PublishRankedReport{
  605. Id: dto.ReportID,
  606. OrgId: dto.OrgId,
  607. PublishedTime: dto.PublishedTime,
  608. Abstract: dto.Abstract,
  609. Title: dto.Title,
  610. Permissions: dto.Permissions,
  611. SecondPermissions: dto.SecondPermission,
  612. PermissionNames: dto.PermissionNames,
  613. CoverUrl: src,
  614. }
  615. reports = append(reports, report)
  616. }
  617. return
  618. }
  619. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  620. return userService.RecordCountDTO{
  621. UserId: record.UserId,
  622. Mobile: record.Mobile,
  623. SourceId: record.ReportId,
  624. IpAddress: record.IpAddress,
  625. Location: record.Location,
  626. Referer: record.Referer,
  627. Additional: record.Additional,
  628. }
  629. }
  630. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  631. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  632. if err != nil {
  633. logger.Error("获取报告ID列表失败:%v", err)
  634. err = exception.New(exception.GetReportSearchRangeFailed)
  635. }
  636. return
  637. }
  638. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, err error) {
  639. if isLogin {
  640. userProfile, userErr := user.GetUserProfile(userId)
  641. if userErr != nil {
  642. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  643. err = exception.New(exception.TemplateUserNotFound)
  644. } else {
  645. err = exception.New(exception.TemplateUserFoundFailed)
  646. }
  647. logger.Error("获取有权限的品种列表失败:%v", err)
  648. return
  649. }
  650. //获取产品风险等级
  651. if userProfile.RiskLevel == user.RiskUnTest {
  652. logger.Error("客户未做风险等级测评,mobile:%d", userProfile.Mobile)
  653. return
  654. }
  655. if userProfile.RiskLevelStatus == user.RiskExpired {
  656. logger.Error("客户风险等级已过期,mobile:%d", userProfile.Mobile)
  657. return
  658. }
  659. mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  660. if mappingErr != nil {
  661. logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  662. return
  663. }
  664. var permissionList []permissionService.PermissionDTO
  665. //获取所有设置风险等级的品种
  666. permissionList, err = permissionService.GetPermissionListWithRisk()
  667. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  668. if len(permissionList) == 0 {
  669. return
  670. }
  671. for _, permission := range permissionList {
  672. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  673. }
  674. return
  675. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  676. var permissionList []permissionService.PermissionDTO
  677. //获取所有设置风险等级的品种
  678. permissionList, err = permissionService.GetPermissionListWithRisk()
  679. if err != nil {
  680. logger.Error("根据ID查询品种列表失败:%v", err)
  681. }
  682. for _, permission := range permissionList {
  683. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  684. }
  685. //查询品种
  686. return
  687. }
  688. }