report_service.go 25 KB

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