report_service.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. userDao "eta/eta_mini_ht_api/models/user"
  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, 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 := 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. if num > matchNum {
  140. riskLevelMatch = RiskLevelUnMatch
  141. return
  142. } else {
  143. riskLevelMatch = RiskLevelMatch
  144. return
  145. }
  146. }
  147. func getHighestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  148. for _, permission := range permissions {
  149. pRiskNum, err := parseRiskLevel(permission.RiskLevel)
  150. if err != nil {
  151. logger.Error("解析风险等级失败:%v", err)
  152. continue
  153. }
  154. if riskLevelNum == 0 {
  155. riskLevelNum = pRiskNum
  156. } else {
  157. if riskLevelNum < pRiskNum {
  158. riskLevelNum = pRiskNum
  159. }
  160. }
  161. }
  162. return
  163. }
  164. func getLowestRiskLevel(permissions []permissionService.PermissionDTO) (riskLevelNum int) {
  165. for _, permission := range permissions {
  166. pRiskNum, err := parseRiskLevel(permission.RiskLevel)
  167. if err != nil {
  168. logger.Error("解析风险等级失败:%v", err)
  169. continue
  170. }
  171. if riskLevelNum == 0 {
  172. riskLevelNum = pRiskNum
  173. } else {
  174. if riskLevelNum > pRiskNum {
  175. riskLevelNum = pRiskNum
  176. }
  177. }
  178. }
  179. return
  180. }
  181. func GetReportById(reportId int, login bool, userId int) (report reportService.ReportDTO, err error) {
  182. report, err = reportService.GetGetReportById(reportId)
  183. if err != nil {
  184. logger.Error("获取研报失败:%v", err)
  185. err = exception.New(exception.GetReportFailed)
  186. return
  187. }
  188. var status string
  189. status, err = matchRiskLevel(userId, report)
  190. if err != nil {
  191. logger.Error("匹配风险等级失败:%v", err)
  192. err = exception.New(exception.ReportRiskLevelUnSet)
  193. return
  194. }
  195. var pdfUrl string
  196. switch report.Source {
  197. case SourceETA:
  198. var detail reportService.ETAReportDTO
  199. detail, err = getETAReportDetail(&report)
  200. if err != nil {
  201. logger.Error("获取研报详情失败失败:%v", err)
  202. err = exception.New(exception.GetReportFailed)
  203. return
  204. }
  205. if !login {
  206. detail.Content = ""
  207. report.RiskLevelStatus = RiskLevelUnMatch
  208. report.Login = false
  209. } else {
  210. if status != RiskLevelMatch {
  211. detail.Content = ""
  212. }
  213. report.RiskLevelStatus = status
  214. report.Login = true
  215. }
  216. var jsonStr []byte
  217. jsonStr, err = json.Marshal(detail)
  218. if err != nil {
  219. logger.Error("生成研报详情失败:%v", err)
  220. err = exception.New(exception.GetReportFailed)
  221. }
  222. report.Detail = jsonStr
  223. return
  224. case SourceHT:
  225. pdfUrl, err = getHTReportDetail(&report)
  226. if err != nil {
  227. logger.Error("获取研报详情失败失败:%v")
  228. err = exception.New(exception.GetReportFailed)
  229. return
  230. }
  231. if !login {
  232. report.PdfUrl = ""
  233. report.RiskLevelStatus = RiskLevelUnMatch
  234. report.Login = false
  235. } else {
  236. if status == RiskLevelMatch {
  237. report.PdfUrl = pdfUrl
  238. }
  239. report.RiskLevelStatus = status
  240. report.Login = true
  241. }
  242. return
  243. default:
  244. logger.Error("不支持的研报来演:%v")
  245. err = exception.New(exception.GetReportFailed)
  246. return
  247. }
  248. }
  249. func getETAReportDetail(report *reportService.ReportDTO) (etaReport reportService.ETAReportDTO, err error) {
  250. return reportService.GetETAReport(report.OrgId)
  251. }
  252. func getHTReportDetail(report *reportService.ReportDTO) (url string, err error) {
  253. return reportService.GetHtReport(report.OrgId)
  254. }
  255. func GetTotalPageCountByPermissionIds(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  256. return getCount(permissionIds, isLogin, userId)
  257. //var err error
  258. ////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  259. //if isLogin {
  260. // userProfile, userErr := user.GetUserProfile(userId)
  261. // if userErr != nil {
  262. // if errors.Is(userErr, gorm.ErrRecordNotFound) {
  263. // err = exception.New(exception.TemplateUserNotFound)
  264. // } else {
  265. // err = exception.New(exception.TemplateUserFoundFailed)
  266. // }
  267. // logger.Error("分页查询报告列表失败:%v", err)
  268. // return
  269. // }
  270. // //获取产品风险等级
  271. // if userProfile.RiskLevel == user.RiskUnTest {
  272. // logger.Error("客户未做风险等级测评,mobile:%d", userProfile.Mobile)
  273. // return
  274. // }
  275. // if userProfile.RiskLevelStatus == user.RiskExpired {
  276. // logger.Error("客户风险等级已过期,mobile:%d", userProfile.Mobile)
  277. // return
  278. // }
  279. // mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  280. // if mappingErr != nil {
  281. // logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  282. // return
  283. // }
  284. // var permissionList []permissionService.PermissionDTO
  285. // if len(permissionIds) == 0 {
  286. // //获取所有设置风险等级的品种
  287. // permissionList, err = permissionService.GetPermissionListWithRisk()
  288. // } else {
  289. // //更具id过滤设置了风险等级的品种
  290. // permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  291. // }
  292. // permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  293. // if len(permissionList) == 0 {
  294. // return
  295. // }
  296. // var filterPermissionIds []int
  297. // for _, permission := range permissionList {
  298. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  299. // }
  300. // return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, mapping.ProductRiskLevel)
  301. //} else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  302. // var permissionList []permissionService.PermissionDTO
  303. // if len(permissionIds) == 0 {
  304. // //获取所有设置风险等级的品种
  305. // permissionList, err = permissionService.GetPermissionListWithRisk()
  306. // } else {
  307. // //更具id过滤设置了风险等级的品种
  308. // permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  309. // }
  310. // if err != nil {
  311. // logger.Error("根据ID查询品种列表失败:%v", err)
  312. // }
  313. // var filterPermissionIds []int
  314. // for _, permission := range permissionList {
  315. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  316. // }
  317. // //查询品种
  318. // return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, "")
  319. //}
  320. }
  321. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  322. if riskLevel != "" {
  323. riskLevelNum, err := parseRiskLevel(riskLevel)
  324. if err != nil {
  325. logger.Error("风险等级解析失败:%v", err)
  326. return
  327. }
  328. for _, permission := range permissionList {
  329. pRiskNum, riskErr := parseRiskLevel(permission.RiskLevel)
  330. if riskErr != nil {
  331. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  332. continue
  333. }
  334. if pRiskNum <= riskLevelNum {
  335. resultList = append(resultList, permission)
  336. }
  337. }
  338. } else {
  339. resultList = permissionList
  340. }
  341. return
  342. }
  343. // ParseRiskLevel 解析风险等级字符串,并返回数字部分
  344. func parseRiskLevel(level string) (int, error) {
  345. parts := strings.Split(level, "R")
  346. if len(parts) < 2 {
  347. return 0, fmt.Errorf("无效的风险等级: %s", level)
  348. }
  349. numberStr := parts[1]
  350. number, err := strconv.Atoi(numberStr)
  351. if err != nil {
  352. return 0, fmt.Errorf("无法将风险等级转换为数字: %s", err)
  353. }
  354. return number, nil
  355. }
  356. func SearchReportList(key string, Ids []int, pageInfo page.PageInfo, isLogin bool, userId int) (list []reportService.ReportDTO, err error) {
  357. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  358. var reports []reportService.ReportDTO
  359. reports, err = reportService.SearchReportList(key, Ids, offset, pageInfo.PageSize, pageInfo.LatestId)
  360. list, err = dealReportInfo(reports, isLogin, userId)
  361. //var wg sync.WaitGroup
  362. //wg.Add(len(reports))
  363. //for i := 0; i < len(reports); i++ {
  364. // go func(report *reportService.ReportDTO) {
  365. // defer wg.Done()
  366. // report.Login = isLogin
  367. // report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  368. // permissions := getReportSecondPermissions(report.OrgId, report.Source)
  369. // if len(permissions) == 0 {
  370. // return
  371. // }
  372. // riskNum := getHighestRiskLevel(permissions)
  373. // report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  374. // var src string
  375. // src, err = mediaService.GetImageSrc(report.CoverSrc)
  376. // if err != nil {
  377. // logger.Error("获取图片地址失败:%v", err)
  378. // src = ""
  379. // } else {
  380. // report.CoverUrl = src
  381. // }
  382. // //下查询产品信息
  383. // product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  384. // if pdErr != nil {
  385. // if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  386. // report.Price = defaultProductPrice
  387. // report.IsFree = true
  388. // report.IsSubscribe = false
  389. // } else {
  390. // report.Price = defaultProductPrice
  391. // report.IsFree = false
  392. // report.IsSubscribe = false
  393. // }
  394. // } else {
  395. // report.Price = product.Price.String()
  396. // report.IsFree = false
  397. // if isLogin {
  398. // subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  399. // if subscribeErr != nil {
  400. // report.IsSubscribe = false
  401. // } else {
  402. // report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  403. // }
  404. // }
  405. // pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
  406. // if parseErr != nil {
  407. // return
  408. // }
  409. // rpRiskNum, parseErr := parseRiskLevel(report.RiskLevel)
  410. // if parseErr != nil {
  411. // return
  412. // }
  413. // if rpRiskNum <= pdRiskNum {
  414. // report.RiskLevel = product.RiskLevel
  415. // }
  416. // }
  417. // list = append(list, *report)
  418. // }(&reports[i])
  419. //}
  420. //wg.Wait()
  421. if err != nil {
  422. err = exception.New(exception.SearchReportPageFailed)
  423. }
  424. return
  425. }
  426. func RangeSearchByAnalyst(analystName string, userId int) (total int64, latestId int64, ids []int) {
  427. return getCountByAnalyst(nil, true, userId, analystName)
  428. ////登录了需要校验风险等级,如果风险等级没做或者过期直接返回空,做了就筛选风险等级
  429. //userProfile, userErr := user.GetUserProfile(userId)
  430. //if userErr != nil {
  431. // if errors.Is(userErr, gorm.ErrRecordNotFound) {
  432. // err = exception.New(exception.TemplateUserNotFound)
  433. // } else {
  434. // err = exception.New(exception.TemplateUserFoundFailed)
  435. // }
  436. // logger.Error("分页查询报告列表失败:%v", err)
  437. // return
  438. //}
  439. ////获取产品风险等级
  440. //if userProfile.RiskLevel == user.RiskUnTest {
  441. // logger.Error("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  442. // return
  443. //}
  444. //if userProfile.RiskLevelStatus == user.RiskExpired {
  445. // logger.Error("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  446. // return
  447. //}
  448. //mapping, mappingErr := permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  449. //if mappingErr != nil {
  450. // logger.Error("查询产品风险等级映射失败:%v", mappingErr)
  451. // return
  452. //}
  453. //var permissionList []permissionService.PermissionDTO
  454. ////获取所有设置风险等级的品种
  455. //permissionList, err = permissionService.GetPermissionListWithRisk()
  456. //permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  457. //if len(permissionList) == 0 {
  458. // return
  459. //}
  460. //var filterPermissionIds []int
  461. //for _, permission := range permissionList {
  462. // filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  463. //}
  464. //return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds)
  465. }
  466. func RangeSearch(isLogin bool, userId int) (total int64, latestId int64, orgIds map[string][]int) {
  467. return getCount(nil, isLogin, userId)
  468. }
  469. func dealReportInfo(list []reportService.ReportDTO, isLogin bool, userId int) (resultList []reportService.ReportDTO, err error) {
  470. var wg sync.WaitGroup
  471. wg.Add(len(list))
  472. for i := 0; i < len(list); i++ {
  473. go func(report *reportService.ReportDTO) {
  474. defer wg.Done()
  475. report.Login = isLogin
  476. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  477. permissions := getReportSecondPermissions(report.OrgId, report.Source)
  478. if len(permissions) == 0 {
  479. return
  480. }
  481. riskNum := getHighestRiskLevel(permissions)
  482. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  483. var src string
  484. src, err = mediaService.GetImageSrc(report.CoverSrc)
  485. if err != nil {
  486. logger.Error("获取图片地址失败:%v", err)
  487. src = ""
  488. } else {
  489. report.CoverUrl = src
  490. }
  491. //下查询产品信息
  492. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  493. if pdErr != nil {
  494. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  495. report.Price = defaultProductPrice
  496. report.IsFree = true
  497. report.IsSubscribe = false
  498. } else {
  499. report.Price = defaultProductPrice
  500. report.IsFree = false
  501. report.IsSubscribe = false
  502. }
  503. } else {
  504. report.Price = product.Price.String()
  505. report.IsFree = false
  506. if isLogin {
  507. subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  508. if subscribeErr != nil {
  509. report.IsSubscribe = false
  510. } else {
  511. report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  512. }
  513. }
  514. pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
  515. if parseErr != nil {
  516. return
  517. }
  518. rpRiskNum, parseErr := parseRiskLevel(report.RiskLevel)
  519. if parseErr != nil {
  520. return
  521. }
  522. if rpRiskNum <= pdRiskNum {
  523. report.RiskLevel = product.RiskLevel
  524. }
  525. }
  526. }(&list[i])
  527. }
  528. wg.Wait()
  529. resultList = list
  530. return
  531. }
  532. // GetReportPage 分页获取报告列表
  533. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, searchAll bool, isLogin bool, userId int) (reports []reportService.ReportDTO, err error) {
  534. var list []reportService.ReportDTO
  535. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, searchAll)
  536. reports, err = dealReportInfo(list, isLogin, userId)
  537. ////并发获取研报的标签
  538. //var wg sync.WaitGroup
  539. //wg.Add(len(list))
  540. //for i := 0; i < len(list); i++ {
  541. // go func(report *reportService.ReportDTO) {
  542. // defer wg.Done()
  543. // report.Login = isLogin
  544. // report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  545. // permissions := getReportSecondPermissions(report.OrgId, report.Source)
  546. // if len(permissions) == 0 {
  547. // return
  548. // }
  549. // riskNum := getHighestRiskLevel(permissions)
  550. // report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  551. // var src string
  552. // src, err = mediaService.GetImageSrc(report.CoverSrc)
  553. // if err != nil {
  554. // logger.Error("获取图片地址失败:%v", err)
  555. // src = ""
  556. // } else {
  557. // report.CoverUrl = src
  558. // }
  559. // //下查询产品信息
  560. // product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  561. // if pdErr != nil {
  562. // if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  563. // report.Price = defaultProductPrice
  564. // report.IsFree = true
  565. // report.IsSubscribe = false
  566. // } else {
  567. // report.Price = defaultProductPrice
  568. // report.IsFree = false
  569. // report.IsSubscribe = false
  570. // }
  571. // } else {
  572. // report.Price = product.Price.String()
  573. // report.IsFree = false
  574. // if isLogin {
  575. // subscribe, subscribeErr := userService.GetUserSubscribe(product.Id, userId)
  576. // if subscribeErr != nil {
  577. // report.IsSubscribe = false
  578. // } else {
  579. // report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  580. // }
  581. // }
  582. // pdRiskNum, parseErr := parseRiskLevel(product.RiskLevel)
  583. // if parseErr != nil {
  584. // return
  585. // }
  586. // rpRiskNum, parseErr := parseRiskLevel(report.RiskLevel)
  587. // if parseErr != nil {
  588. // return
  589. // }
  590. // if rpRiskNum <= pdRiskNum {
  591. // report.RiskLevel = product.RiskLevel
  592. // }
  593. // }
  594. // reports = append(reports, *report)
  595. // }(&list[i])
  596. //}
  597. //wg.Wait()
  598. if err != nil {
  599. err = exception.New(exception.QueryReportPageFailed)
  600. }
  601. return
  602. }
  603. // func GetTotalPageCountByAnalyst(analyst string) (total int64, latestId int64) {
  604. // return reportService.GetTotalPageCountByAnalyst(analyst)
  605. // }
  606. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int) (list []reportService.ReportDTO, err error) {
  607. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
  608. //并发获取研报的标签
  609. var wg sync.WaitGroup
  610. wg.Add(len(list))
  611. for i := 0; i < len(list); i++ {
  612. go func(report *reportService.ReportDTO) {
  613. defer wg.Done()
  614. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  615. }(&list[i])
  616. }
  617. wg.Wait()
  618. if err != nil {
  619. err = exception.New(exception.QueryReportPageFailed)
  620. }
  621. return
  622. }
  623. func CountReport(count RecordCount) (traceId string, err error) {
  624. dto := convertToRecordCountDTO(count)
  625. return userService.CountReport(dto)
  626. }
  627. func GetRandedReportByWeeklyHot(limit int, isLogin bool, userId int, pdRiskLevel string) (reports []HotRankedReport, err error) {
  628. end := time.Now()
  629. begin := date.GetBeginOfTheWeek(end, time.Monday)
  630. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  631. if len(hotReports) > 0 {
  632. var dtoList []reportService.ReportDTO
  633. var ids []int
  634. for i := 0; i < len(hotReports); i++ {
  635. ids = append(ids, hotReports[i].ReportId)
  636. }
  637. dtoList, err = reportService.GetListByCondition("id", ids)
  638. if err != nil {
  639. logger.Error("获取本周最热研报列表失败:%v", err)
  640. err = exception.New(exception.GetHotRandListFailed)
  641. return
  642. }
  643. var filterList []reportService.ReportDTO
  644. if pdRiskLevel != "" {
  645. for _, report := range dtoList {
  646. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  647. if pdErr != nil {
  648. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  649. report.Price = defaultProductPrice
  650. report.IsFree = true
  651. report.IsSubscribe = false
  652. } else {
  653. logger.Error("查询产品失败:%v", pdErr)
  654. report.Price = defaultProductPrice
  655. report.IsFree = false
  656. report.IsSubscribe = false
  657. }
  658. filterList = append(filterList, report)
  659. } else {
  660. pdRiskNum, paresErr := parseRiskLevel(product.RiskLevel)
  661. if paresErr != nil {
  662. logger.Error("解析风险等级失败:%v", err)
  663. continue
  664. }
  665. reRiskNum, paresErr := parseRiskLevel(pdRiskLevel)
  666. if paresErr != nil {
  667. logger.Error("解析风险等级失败:%v", err)
  668. continue
  669. }
  670. report.RiskLevel = product.RiskLevel
  671. if isLogin {
  672. subscribe, subErr := userService.GetUserSubscribe(product.Id, userId)
  673. if subErr != nil {
  674. logger.Error("查询用户订阅信息失败:%v,productId:%v,userId:%v", err, product.Id, userId)
  675. report.Price = product.Price.String()
  676. report.IsFree = false
  677. report.IsSubscribe = false
  678. } else {
  679. report.Price = product.Price.String()
  680. report.IsFree = false
  681. report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  682. }
  683. }
  684. if pdRiskNum <= reRiskNum {
  685. filterList = append(filterList, report)
  686. }
  687. }
  688. }
  689. } else {
  690. filterList = dtoList
  691. for _, report := range filterList {
  692. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  693. if pdErr != nil {
  694. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  695. report.Price = defaultProductPrice
  696. report.IsFree = true
  697. report.IsSubscribe = false
  698. } else {
  699. logger.Error("查询产品失败:%v", pdErr)
  700. report.Price = defaultProductPrice
  701. report.IsFree = false
  702. report.IsSubscribe = false
  703. }
  704. } else {
  705. report.Price = product.Price.String()
  706. report.IsFree = false
  707. report.IsSubscribe = false
  708. report.RiskLevel = product.RiskLevel
  709. }
  710. }
  711. }
  712. var wg sync.WaitGroup
  713. wg.Add(len(filterList))
  714. for i := 0; i < len(filterList); i++ {
  715. go func(report *reportService.ReportDTO) {
  716. defer wg.Done()
  717. report.Login = isLogin
  718. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  719. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  720. permissions := getReportSecondPermissions(report.OrgId, report.Source)
  721. var riskNum int
  722. if len(permissions) == 0 {
  723. riskNum = 0
  724. } else {
  725. riskNum = getHighestRiskLevel(permissions)
  726. }
  727. if report.RiskLevel == "" {
  728. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  729. } else {
  730. reRiskNum, paresErr := parseRiskLevel(report.RiskLevel)
  731. if paresErr != nil {
  732. logger.Error("解析风险等级失败:%v", err)
  733. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  734. } else {
  735. if reRiskNum <= riskNum {
  736. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  737. }
  738. }
  739. }
  740. var label []string
  741. for _, permission := range report.Permissions {
  742. label = append(label, permission)
  743. }
  744. report.PermissionNames = label
  745. }(&filterList[i])
  746. }
  747. wg.Wait()
  748. reports = make([]HotRankedReport, len(ids))
  749. for i := 0; i < len(filterList); i++ {
  750. risk, parseErr := parseRiskLevel(filterList[i].RiskLevel)
  751. if parseErr != nil || risk == 0 {
  752. continue
  753. }
  754. report := convertToHotRankedReport(filterList[i])
  755. for j := 0; j < len(hotReports); j++ {
  756. if hotReports[j].ReportId == report.Id {
  757. report.Count = hotReports[j].Count
  758. reports[j] = report
  759. break
  760. }
  761. }
  762. }
  763. } else {
  764. reports = []HotRankedReport{}
  765. }
  766. return
  767. }
  768. func GetRandedReportByPublishTimeWeekly(limit int, week bool, isLogin bool, userId int, pdRiskLevel string) (reports []PublishRankedReport, err error) {
  769. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  770. if err != nil {
  771. logger.Error("获取最新发布的研报列表失败:%v", err)
  772. err = exception.New(exception.GetPublishedRandListFailed)
  773. return
  774. }
  775. var filterList []reportService.ReportDTO
  776. if pdRiskLevel != "" {
  777. for _, report := range dtoList {
  778. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  779. if pdErr != nil {
  780. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  781. report.Price = defaultProductPrice
  782. report.IsFree = true
  783. report.IsSubscribe = false
  784. } else {
  785. logger.Error("查询产品失败:%v", pdErr)
  786. report.Price = defaultProductPrice
  787. report.IsFree = false
  788. report.IsSubscribe = false
  789. }
  790. filterList = append(filterList, report)
  791. } else {
  792. pdRiskNum, paresErr := parseRiskLevel(product.RiskLevel)
  793. if paresErr != nil {
  794. logger.Error("解析风险等级失败:%v", err)
  795. continue
  796. }
  797. reRiskNum, paresErr := parseRiskLevel(pdRiskLevel)
  798. if paresErr != nil {
  799. logger.Error("解析风险等级失败:%v", err)
  800. continue
  801. }
  802. report.RiskLevel = product.RiskLevel
  803. if isLogin {
  804. subscribe, subErr := userService.GetUserSubscribe(product.Id, userId)
  805. if subErr != nil {
  806. logger.Error("查询用户订阅信息失败:%v,productId:%v,userId:%v", err, product.Id, userId)
  807. report.Price = product.Price.String()
  808. report.IsFree = false
  809. report.IsSubscribe = false
  810. } else {
  811. report.Price = product.Price.String()
  812. report.IsFree = false
  813. report.IsSubscribe = subscribe.Status == userDao.SubscribeValid
  814. }
  815. }
  816. if pdRiskNum <= reRiskNum {
  817. filterList = append(filterList, report)
  818. }
  819. }
  820. }
  821. } else {
  822. filterList = dtoList
  823. for _, report := range filterList {
  824. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  825. if pdErr != nil {
  826. if errors.Is(pdErr, gorm.ErrRecordNotFound) {
  827. report.Price = defaultProductPrice
  828. report.IsFree = true
  829. report.IsSubscribe = false
  830. } else {
  831. logger.Error("查询产品失败:%v", pdErr)
  832. report.Price = defaultProductPrice
  833. report.IsFree = false
  834. report.IsSubscribe = false
  835. }
  836. } else {
  837. report.Price = product.Price.String()
  838. report.IsFree = false
  839. report.IsSubscribe = false
  840. report.RiskLevel = product.RiskLevel
  841. }
  842. }
  843. }
  844. //并发获取研报的标签
  845. var wg sync.WaitGroup
  846. wg.Add(len(filterList))
  847. for i := 0; i < len(filterList); i++ {
  848. go func(report *reportService.ReportDTO) {
  849. defer wg.Done()
  850. report.Login = isLogin
  851. report.Permissions = getReportPermissionsMap(report.OrgId, report.Source)
  852. report.SecondPermission = getReportSecondPermissionsMap(report.OrgId, report.Source)
  853. report.PermissionNames = getReportPermissionNames(report.OrgId, report.Source)
  854. var riskNum int
  855. permissions := getReportSecondPermissions(report.OrgId, report.Source)
  856. if len(permissions) == 0 {
  857. riskNum = 0
  858. } else {
  859. riskNum = getHighestRiskLevel(permissions)
  860. }
  861. if report.RiskLevel == "" {
  862. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  863. } else {
  864. reRiskNum, paresErr := parseRiskLevel(report.RiskLevel)
  865. if paresErr != nil {
  866. logger.Error("解析风险等级失败:%v", err)
  867. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  868. } else {
  869. if reRiskNum <= riskNum {
  870. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  871. }
  872. }
  873. }
  874. }(&filterList[i])
  875. }
  876. wg.Wait()
  877. reports = convertToPublishRankedReportList(filterList)
  878. return
  879. }
  880. func getReportPermissionNames(id int, source string) (labels []string) {
  881. permissions := reportService.GetReportPermissionsById(id, source)
  882. for _, permission := range permissions {
  883. labels = append(labels, permission.PermissionName)
  884. }
  885. return
  886. }
  887. func getReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  888. permissionMap = make(map[int]string)
  889. permissions := reportService.GetReportSecondPermissionsById(id, source)
  890. for _, permission := range permissions {
  891. permissionMap[permission.PermissionId] = permission.PermissionName
  892. }
  893. return
  894. }
  895. func getReportSecondPermissions(id int, source string) (permissionList []permissionService.PermissionDTO) {
  896. return reportService.GetReportSecondPermissionsById(id, source)
  897. }
  898. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  899. permissionMap = make(map[int]string)
  900. permissions := reportService.GetReportPermissionsById(id, source)
  901. for _, permission := range permissions {
  902. permissionMap[permission.PermissionId] = permission.PermissionName
  903. }
  904. return
  905. }
  906. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  907. return permissionService.GetPermissionList()
  908. }
  909. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  910. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  911. if err != nil {
  912. logger.Error("获取封面图片失败:%v", err)
  913. src = ""
  914. }
  915. report = HotRankedReport{
  916. Id: dto.ReportID,
  917. OrgId: dto.OrgId,
  918. Abstract: dto.Abstract,
  919. PublishedTime: dto.PublishedTime,
  920. Title: dto.Title,
  921. SecondPermissions: dto.SecondPermission,
  922. Permissions: dto.Permissions,
  923. PermissionNames: dto.PermissionNames,
  924. CoverUrl: src,
  925. IsSubscribe: dto.IsSubscribe,
  926. IsFree: dto.IsFree,
  927. Price: dto.Price,
  928. RiskLevel: dto.RiskLevel,
  929. Login: dto.Login,
  930. }
  931. return
  932. }
  933. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  934. reports = []PublishRankedReport{}
  935. for _, dto := range dtoList {
  936. risk, err := parseRiskLevel(dto.RiskLevel)
  937. if err != nil || risk == 0 {
  938. continue
  939. }
  940. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  941. if err != nil {
  942. logger.Error("获取封面图片失败:%v", err)
  943. src = ""
  944. }
  945. report := PublishRankedReport{
  946. Id: dto.ReportID,
  947. OrgId: dto.OrgId,
  948. PublishedTime: dto.PublishedTime,
  949. Abstract: dto.Abstract,
  950. Title: dto.Title,
  951. Permissions: dto.Permissions,
  952. SecondPermissions: dto.SecondPermission,
  953. PermissionNames: dto.PermissionNames,
  954. CoverUrl: src,
  955. IsSubscribe: dto.IsSubscribe,
  956. IsFree: dto.IsFree,
  957. Price: dto.Price,
  958. RiskLevel: dto.RiskLevel,
  959. Login: dto.Login,
  960. }
  961. reports = append(reports, report)
  962. }
  963. return
  964. }
  965. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  966. return userService.RecordCountDTO{
  967. UserId: record.UserId,
  968. TraceId: record.TraceId,
  969. Mobile: record.Mobile,
  970. SourceId: record.ReportId,
  971. IpAddress: record.IpAddress,
  972. Location: record.Location,
  973. Referer: record.Referer,
  974. Additional: record.Additional,
  975. }
  976. }
  977. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  978. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  979. if err != nil {
  980. logger.Error("获取报告ID列表失败:%v", err)
  981. err = exception.New(exception.GetReportSearchRangeFailed)
  982. }
  983. return
  984. }
  985. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  986. return checkUserRisk(nil, isLogin, userId)
  987. }
  988. func checkUserRisk(permissionIds []int, isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, err error) {
  989. if isLogin {
  990. userProfile, userErr := user.GetUserProfile(userId)
  991. if userErr != nil {
  992. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  993. err = exception.New(exception.TemplateUserNotFound)
  994. } else {
  995. err = exception.New(exception.TemplateUserFoundFailed)
  996. }
  997. logger.Error("分页查询报告列表失败:%v", err)
  998. return
  999. }
  1000. //获取产品风险等级
  1001. if userProfile.RiskLevel == user.RiskUnTest {
  1002. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  1003. }
  1004. if userProfile.RiskLevelStatus == user.RiskExpired {
  1005. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  1006. }
  1007. var mapping permissionService.CustomerProductRiskMappingDTO
  1008. if userProfile.RiskLevel != "" {
  1009. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  1010. if err != nil {
  1011. logger.Error("查询产品风险等级映射失败:%v", err)
  1012. return
  1013. }
  1014. }
  1015. var permissionList []permissionService.PermissionDTO
  1016. if len(permissionIds) == 0 {
  1017. //获取所有设置风险等级的品种
  1018. permissionList, err = permissionService.GetPermissionListWithRisk()
  1019. } else {
  1020. //更具id过滤设置了风险等级的品种
  1021. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  1022. }
  1023. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  1024. riskLevel = mapping.ProductRiskLevel
  1025. if len(permissionList) == 0 {
  1026. return
  1027. }
  1028. for _, permission := range permissionList {
  1029. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  1030. }
  1031. return
  1032. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  1033. var permissionList []permissionService.PermissionDTO
  1034. if len(permissionIds) == 0 {
  1035. //获取所有设置风险等级的品种
  1036. permissionList, err = permissionService.GetPermissionListWithRisk()
  1037. } else {
  1038. //更具id过滤设置了风险等级的品种
  1039. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  1040. }
  1041. if err != nil {
  1042. logger.Error("根据ID查询品种列表失败:%v", err)
  1043. }
  1044. for _, permission := range permissionList {
  1045. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  1046. }
  1047. //查询品种
  1048. return
  1049. }
  1050. }
  1051. func getCount(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int) {
  1052. filterPermissionIds, riskLevel, err := checkUserRisk(permissionIds, isLogin, userId)
  1053. if err != nil {
  1054. logger.Error("校验用户风险等级失败:%v", err)
  1055. return
  1056. }
  1057. return reportService.GetTotalPageCountByPermissionIds(filterPermissionIds, riskLevel)
  1058. }
  1059. func getCountByAnalyst(permissionIds []int, isLogin bool, userId int, analystName string) (total int64, latestId int64, ids []int) {
  1060. filterPermissionIds, riskLevel, err := checkUserRisk(permissionIds, isLogin, userId)
  1061. if err != nil {
  1062. logger.Error("校验用户风险等级失败:%v", err)
  1063. return
  1064. }
  1065. return reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds, riskLevel)
  1066. }