report_service.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. err = exception.NewWithException(exception.GetReportFailed, err.Error())
  171. }
  172. return
  173. }
  174. func GetTotalPageCountByPermissionIds(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int, disCardIds []int, mappingRiskLevel, userRiskStatus string) {
  175. return getCount(permissionIds, isLogin, userId)
  176. }
  177. // ParseRiskLevel 解析风险等级字符串,并返回数字部分
  178. func SearchReportList(key string, Ids []int, pageInfo page.PageInfo, isLogin bool, userId int, mappingRiskLevel, userRiskStatus string) (list []reportService.ReportDTO, err error) {
  179. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  180. var reports []reportService.ReportDTO
  181. reports, err = reportService.SearchReportList(key, Ids, offset, pageInfo.PageSize, pageInfo.LatestId)
  182. list, err = dealReportInfo(reports, isLogin, userId, mappingRiskLevel, userRiskStatus)
  183. if err != nil {
  184. err = exception.New(exception.SearchReportPageFailed)
  185. }
  186. return
  187. }
  188. func SearchReportProduct(key string, docIds []int) (list []reportService.ReportDTO, err error) {
  189. list, err = reportService.SearchReportProduct(key, 100, 0, docIds)
  190. if err != nil {
  191. err = exception.New(exception.SearchReportPageFailed)
  192. }
  193. return
  194. }
  195. func RangeSearchByAnalyst(analystName string, userId int) (total int64, latestId int64, ids []int, mappingRiskLevel, userRiskStatus string) {
  196. return getCountByAnalyst(nil, true, userId, analystName)
  197. }
  198. func RangeSearch(key string, isLogin bool, userId int) (total int64, latestId int64, reportIds []int, discardIds []int, mappingRiskLevel, userRiskStatus string, err error) {
  199. var orgIds map[string][]int
  200. _, latestId, orgIds, discardIds, mappingRiskLevel, userRiskStatus = getCount(nil, isLogin, userId)
  201. reportIds, err = GetReportByIdListByOrgIds(orgIds)
  202. if err != nil {
  203. logger.Error("获取报告ID列表失败:%v", err)
  204. err = exception.NewWithException(exception.GetReportSearchRangeFailed, err.Error())
  205. return
  206. }
  207. total = reportService.SearchMaxReportIdWithRange(key, reportIds)
  208. return
  209. }
  210. func dealReportInfo(list []reportService.ReportDTO, isLogin bool, userId int, mappingRiskLevel, userRiskStatus string) (resultList []reportService.ReportDTO, err error) {
  211. var wg sync.WaitGroup
  212. wg.Add(len(list))
  213. for i := 0; i < len(list); i++ {
  214. go func(report *reportService.ReportDTO) {
  215. defer wg.Done()
  216. report, err = DealReportInfo(report, isLogin, userId, mappingRiskLevel, userRiskStatus)
  217. if err != nil {
  218. logger.Error("处理报告信息失败:%v", err)
  219. }
  220. }(&list[i])
  221. }
  222. wg.Wait()
  223. resultList = list
  224. return
  225. }
  226. func getETAReportDetail(report *reportService.ReportDTO) (etaReport reportService.ETAReportDTO, err error) {
  227. return reportService.GetETAReport(report.OrgId)
  228. }
  229. func getHTReportDetail(report *reportService.ReportDTO) (url string, err error) {
  230. return reportService.GetHtReport(report.OrgId)
  231. }
  232. func getReportContent(report *reportService.ReportDTO, login bool) (err error) {
  233. var pdfUrl string
  234. switch report.Source {
  235. case SourceETA:
  236. var detail reportService.ETAReportDTO
  237. detail, err = getETAReportDetail(report)
  238. if err != nil {
  239. logger.Error("获取研报详情失败失败:%v", err)
  240. return
  241. }
  242. if !login {
  243. detail.Content = ""
  244. } else {
  245. if report.RiskLevelStatus != RiskLevelMatch {
  246. detail.Content = ""
  247. }
  248. }
  249. var jsonStr []byte
  250. jsonStr, err = json.Marshal(detail)
  251. if err != nil {
  252. logger.Error("生成研报详情失败:%v", err)
  253. return
  254. }
  255. report.Detail = jsonStr
  256. return
  257. case SourceHT:
  258. pdfUrl, err = getHTReportDetail(report)
  259. if err != nil {
  260. logger.Error("获取研报详情失败失败:%v")
  261. err = exception.New(exception.GetReportFailed)
  262. return
  263. }
  264. if !login {
  265. report.PdfUrl = ""
  266. } else {
  267. if report.RiskLevelStatus == RiskLevelMatch {
  268. report.PdfUrl = pdfUrl
  269. }
  270. }
  271. return
  272. default:
  273. logger.Error("不支持的研报来演:%v")
  274. err = exception.New(exception.GetReportFailed)
  275. return
  276. }
  277. }
  278. func DealReportInfo(report *reportService.ReportDTO, isLogin bool, userId int, mappingRiskLevel, userRiskStatus string) (resultReport *reportService.ReportDTO, err error) {
  279. report.Login = isLogin
  280. report.Permissions, report.PermissionNames = GetReportPermissionNames(report.OrgId, report.Source)
  281. var permissions []permissionService.PermissionDTO
  282. permissions, report.SecondPermission = getReportSecondPermissions(report.OrgId, report.Source)
  283. if len(permissions) == 0 {
  284. resultReport = report
  285. return
  286. }
  287. riskNum := config.GetHighestRiskLevel(permissions)
  288. report.RiskLevel = strings.Join([]string{"R", strconv.Itoa(riskNum)}, "")
  289. var src string
  290. src, err = mediaService.GetImageSrc(report.CoverSrc)
  291. if err != nil {
  292. logger.Error("获取图片地址失败:%v", err)
  293. src = ""
  294. } else {
  295. report.CoverUrl = src
  296. }
  297. //套餐信息
  298. var packageList []productService.MerchantProductDTO
  299. //查询产品信息
  300. product, pdErr := productService.GetProductBySourceId(report.ReportID, productDao.Report)
  301. var permissionIds []int
  302. if len(permissions) > 0 {
  303. for _, permission := range permissions {
  304. permissionIds = append(permissionIds, permission.PermissionId)
  305. }
  306. //单品不存在的话查套餐
  307. packageList, err = productService.GetProductListBySourceIds(permissionIds, "package")
  308. if err != nil {
  309. logger.Error("获取套餐列表失败:%v", err)
  310. }
  311. }
  312. if pdErr != nil {
  313. //单品不存在的话查套餐
  314. if len(packageList) == 0 {
  315. logger.Error("获取套餐列表失败:%v", err)
  316. report.Price = defaultProductPrice
  317. report.IsFree = true
  318. report.IsSubscribe = false
  319. report.IsPackage = false
  320. } else {
  321. report.Price = packageList[0].Price
  322. report.IsFree = false
  323. report.IsSubscribe = false
  324. report.IsPackage = true
  325. report.ProductId = packageList[0].Id
  326. }
  327. } else {
  328. report.Price = product.Price
  329. report.IsFree = false
  330. report.ProductId = product.Id
  331. report.IsPackage = false
  332. }
  333. //最热最新的时候使用
  334. productList := append(packageList)
  335. if pdErr == nil {
  336. productList = append(productList, product)
  337. }
  338. if len(productList) == 0 {
  339. report.Show = true
  340. } else {
  341. for _, productItem := range productList {
  342. if productItem.SaleStatus == "on_sale" {
  343. report.Show = true
  344. break
  345. }
  346. }
  347. }
  348. if isLogin {
  349. var productIds []int
  350. if len(packageList) > 0 {
  351. for _, packageItem := range packageList {
  352. productIds = append(productIds, packageItem.Id)
  353. }
  354. }
  355. if product.Id > 0 {
  356. productIds = append(productIds, product.Id)
  357. }
  358. subscribeList, subscribeErr := userService.GetUserSubscribe(productIds, userId)
  359. if subscribeErr != nil {
  360. report.IsSubscribe = false
  361. } else {
  362. for _, subscribe := range subscribeList {
  363. if subscribe.Status == productDao.SubscribeValid {
  364. report.IsSubscribe = true
  365. break
  366. }
  367. }
  368. }
  369. if userRiskStatus != user.RiskValid {
  370. if userRiskStatus == user.RiskUnTest {
  371. report.RiskLevelStatus = RiskLevelUnTest
  372. }
  373. if userRiskStatus == user.RiskExpired {
  374. report.RiskLevelStatus = RiskLevelExpired
  375. }
  376. } else {
  377. report.RiskLevelStatus = RiskLevelUnMatch
  378. if mappingRiskLevel != "" {
  379. mappingRiskNum, parseErr := config.ParseRiskLevel(mappingRiskLevel)
  380. if parseErr != nil {
  381. return
  382. }
  383. var rpRiskNum int
  384. rpRiskNum, parseErr = config.ParseRiskLevel(report.RiskLevel)
  385. if parseErr != nil {
  386. return
  387. }
  388. if rpRiskNum <= mappingRiskNum {
  389. report.RiskLevelStatus = RiskLevelMatch
  390. }
  391. }
  392. }
  393. }
  394. resultReport = report
  395. return
  396. }
  397. // GetReportPage 分页获取报告列表
  398. func GetReportPage(pageInfo page.PageInfo, orgIds map[string][]int, discardIds []int, isLogin bool, userId int, mappingRiskLevel, userRiskStatus string) (reports []reportService.ReportDTO, err error) {
  399. var list []reportService.ReportDTO
  400. list, err = reportService.GetReportPageByOrgIds(pageInfo, orgIds, discardIds)
  401. reports, err = dealReportInfo(list, isLogin, userId, mappingRiskLevel, userRiskStatus)
  402. if err != nil {
  403. err = exception.New(exception.QueryReportPageFailed)
  404. }
  405. return
  406. }
  407. func GetReportListById(userId int, reportIds []int, mappingRiskLevel, userRiskStatus string) (reports []reportService.ReportDTO, err error) {
  408. var list []reportService.ReportDTO
  409. list, err = reportService.GetReportListById(reportIds)
  410. reports, err = dealReportInfo(list, true, userId, mappingRiskLevel, userRiskStatus)
  411. if err != nil {
  412. err = exception.New(exception.QueryReportPageFailed)
  413. }
  414. return
  415. }
  416. func GetReportPageByAnalyst(pageInfo page.PageInfo, analyst string, reportIds []int, templateUserId int, mappingRiskLevel, userRiskStatus string) (list []reportService.ReportDTO, err error) {
  417. list, err = reportService.GetReportPageByAnalyst(pageInfo, analyst, reportIds)
  418. list, err = dealReportInfo(list, true, templateUserId, mappingRiskLevel, userRiskStatus)
  419. if err != nil {
  420. err = exception.New(exception.QueryReportPageFailed)
  421. }
  422. return
  423. }
  424. func CountReport(count RecordCount) (traceId string, err error) {
  425. report, err := reportService.GetReportById(count.ReportId, 0)
  426. if err != nil {
  427. err = exception.New(exception.GetReportFailed)
  428. return
  429. }
  430. dto := convertToRecordCountDTO(count)
  431. dto.SourceTitle = report.Title
  432. return userService.CountReport(dto)
  433. }
  434. func GetRandedReportByWeeklyHot(limit int, isLogin bool, userId int, mappingRiskLevel string, userRiskStatus string) (reports []HotRankedReport, err error) {
  435. end := time.Now()
  436. begin := date.GetBeginOfTheWeek(end, time.Monday)
  437. hotReports := userService.GetHotReports(begin.Format(time.DateOnly), end.Format(time.DateOnly), limit)
  438. if len(hotReports) > 0 {
  439. var dtoList []reportService.ReportDTO
  440. var ids []int
  441. for i := 0; i < len(hotReports); i++ {
  442. ids = append(ids, hotReports[i].ReportId)
  443. }
  444. dtoList, err = reportService.GetListByCondition("id", ids)
  445. if err != nil {
  446. logger.Error("获取本周最热研报列表失败:%v", err)
  447. err = exception.New(exception.GetHotRandListFailed)
  448. return
  449. }
  450. dtoList, err = dealReportInfo(dtoList, isLogin, userId, mappingRiskLevel, userRiskStatus)
  451. if err != nil {
  452. logger.Error("获取本周最热研报列表失败:%v", err)
  453. err = exception.New(exception.GetHotRandListFailed)
  454. return
  455. }
  456. var filterList []reportService.ReportDTO
  457. if mappingRiskLevel != "" {
  458. for _, report := range dtoList {
  459. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  460. if paresErr != nil {
  461. logger.Error("解析风险等级失败:%v", err)
  462. continue
  463. }
  464. reRiskNum, paresErr := config.ParseRiskLevel(mappingRiskLevel)
  465. if paresErr != nil {
  466. logger.Error("解析风险等级失败:%v", err)
  467. continue
  468. }
  469. if pdRiskNum <= reRiskNum && report.Show {
  470. filterList = append(filterList, report)
  471. }
  472. }
  473. } else {
  474. for _, report := range dtoList {
  475. if report.Show {
  476. filterList = append(filterList, report)
  477. }
  478. }
  479. }
  480. reports = make([]HotRankedReport, len(ids))
  481. for i := 0; i < len(filterList); i++ {
  482. report := convertToHotRankedReport(filterList[i])
  483. for j := 0; j < len(hotReports); j++ {
  484. if hotReports[j].ReportId == report.Id {
  485. report.Count = hotReports[j].Count
  486. reports[j] = report
  487. break
  488. }
  489. }
  490. }
  491. } else {
  492. reports = []HotRankedReport{}
  493. }
  494. return
  495. }
  496. func GetRandedReportByPublishTimeWeekly(limit int, week bool, isLogin bool, userId int, mappingRiskLevel, userRiskStatus string) (reports []PublishRankedReport, err error) {
  497. dtoList, err := reportService.GetListOrderByConditionWeekly(week, "published_time", limit, reportService.DESC)
  498. if err != nil {
  499. logger.Error("获取最新发布的研报列表失败:%v", err)
  500. err = exception.New(exception.GetPublishedRandListFailed)
  501. return
  502. }
  503. var filterList []reportService.ReportDTO
  504. dtoList, err = dealReportInfo(dtoList, isLogin, userId, mappingRiskLevel, userRiskStatus)
  505. if err != nil {
  506. logger.Error("获取最新发布的研报列表失败:%v", err)
  507. err = exception.New(exception.GetPublishedRandListFailed)
  508. return
  509. }
  510. if mappingRiskLevel != "" {
  511. for _, report := range dtoList {
  512. pdRiskNum, paresErr := config.ParseRiskLevel(report.RiskLevel)
  513. if paresErr != nil {
  514. logger.Error("解析风险等级失败:%v", err)
  515. continue
  516. }
  517. reRiskNum, paresErr := config.ParseRiskLevel(mappingRiskLevel)
  518. if paresErr != nil {
  519. logger.Error("解析风险等级失败:%v", err)
  520. continue
  521. }
  522. if pdRiskNum <= reRiskNum && report.Show {
  523. filterList = append(filterList, report)
  524. }
  525. }
  526. } else {
  527. for _, report := range dtoList {
  528. if report.Show {
  529. filterList = append(filterList, report)
  530. }
  531. }
  532. }
  533. reports = convertToPublishRankedReportList(filterList)
  534. return
  535. }
  536. func GetReportPermissionNames(id int, source string) (permissionMap map[int]string, labels []string) {
  537. permissions := reportService.GetReportPermissionsById(id, source)
  538. permissionMap = make(map[int]string, len(permissions))
  539. if len(permissions) > 0 {
  540. for _, permission := range permissions {
  541. labels = append(labels, permission.PermissionName)
  542. permissionMap[permission.PermissionId] = permission.PermissionName
  543. }
  544. }
  545. return
  546. }
  547. func GetReportSecondPermissionsMap(id int, source string) (permissionMap map[int]string) {
  548. permissionMap = make(map[int]string)
  549. permissions := reportService.GetReportSecondPermissionsById(id, source)
  550. for _, permission := range permissions {
  551. permissionMap[permission.PermissionId] = permission.PermissionName
  552. }
  553. return
  554. }
  555. func getReportSecondPermissions(id int, source string) (permissionList []permissionService.PermissionDTO, secondPermissionMap map[int]string) {
  556. permissionList = reportService.GetReportSecondPermissionsById(id, source)
  557. if len(permissionList) > 0 {
  558. secondPermissionMap = make(map[int]string, len(permissionList))
  559. for _, permission := range permissionList {
  560. secondPermissionMap[permission.PermissionId] = permission.PermissionName
  561. }
  562. }
  563. return
  564. }
  565. func getReportPermissionsMap(id int, source string) (permissionMap map[int]string) {
  566. permissionMap = make(map[int]string)
  567. permissions := reportService.GetReportPermissionsById(id, source)
  568. for _, permission := range permissions {
  569. permissionMap[permission.PermissionId] = permission.PermissionName
  570. }
  571. return
  572. }
  573. func GetPermissionList() (root *permissionService.PermissionNode, err error) {
  574. return permissionService.GetPermissionList()
  575. }
  576. func convertToHotRankedReport(dto reportService.ReportDTO) (report HotRankedReport) {
  577. report = HotRankedReport{
  578. Id: dto.ReportID,
  579. OrgId: dto.OrgId,
  580. Abstract: dto.Abstract,
  581. PublishedTime: dto.PublishedTime,
  582. Title: dto.Title,
  583. SecondPermissions: dto.SecondPermission,
  584. Permissions: dto.Permissions,
  585. PermissionNames: dto.PermissionNames,
  586. CoverUrl: dto.CoverUrl,
  587. IsSubscribe: dto.IsSubscribe,
  588. IsFree: dto.IsFree,
  589. Price: dto.Price,
  590. RiskLevel: dto.RiskLevel,
  591. Login: dto.Login,
  592. ProductId: dto.ProductId,
  593. IsPackage: dto.IsPackage,
  594. }
  595. return
  596. }
  597. func convertToPublishRankedReportList(dtoList []reportService.ReportDTO) (reports []PublishRankedReport) {
  598. reports = []PublishRankedReport{}
  599. for _, dto := range dtoList {
  600. risk, err := config.ParseRiskLevel(dto.RiskLevel)
  601. if err != nil || risk == 0 {
  602. continue
  603. }
  604. src, err := mediaService.GetImageSrc(dto.CoverSrc)
  605. if err != nil {
  606. logger.Error("获取封面图片失败:%v", err)
  607. src = ""
  608. }
  609. report := PublishRankedReport{
  610. Id: dto.ReportID,
  611. OrgId: dto.OrgId,
  612. PublishedTime: dto.PublishedTime,
  613. Abstract: dto.Abstract,
  614. Title: dto.Title,
  615. Permissions: dto.Permissions,
  616. SecondPermissions: dto.SecondPermission,
  617. PermissionNames: dto.PermissionNames,
  618. CoverUrl: src,
  619. IsSubscribe: dto.IsSubscribe,
  620. IsFree: dto.IsFree,
  621. Price: dto.Price,
  622. RiskLevel: dto.RiskLevel,
  623. Login: dto.Login,
  624. ProductId: dto.ProductId,
  625. IsPackage: dto.IsPackage,
  626. }
  627. reports = append(reports, report)
  628. }
  629. return
  630. }
  631. func convertToRecordCountDTO(record RecordCount) (dto userService.RecordCountDTO) {
  632. return userService.RecordCountDTO{
  633. UserId: record.UserId,
  634. TraceId: record.TraceId,
  635. Mobile: record.Mobile,
  636. SourceId: record.ReportId,
  637. IpAddress: record.IpAddress,
  638. Location: record.Location,
  639. Referer: record.Referer,
  640. Additional: record.Additional,
  641. }
  642. }
  643. func GetReportByIdListByOrgIds(orgIds map[string][]int) (ids []int, err error) {
  644. ids, err = reportService.GetReportByIdListByOrgIds(orgIds)
  645. if err != nil {
  646. logger.Error("获取报告ID列表失败:%v", err)
  647. err = exception.New(exception.GetReportSearchRangeFailed)
  648. }
  649. return
  650. }
  651. func RangePermissionIds(isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, userRiskStatus string, err error) {
  652. return user.GetRiskLevelPermissionList(nil, isLogin, userId)
  653. }
  654. func getCount(permissionIds []int, isLogin bool, userId int) (total int64, latestId int64, ids map[string][]int, discardIds []int, mappingRiskLevel, userRiskStatus string) {
  655. filterPermissionIds, mappingRiskLevel, userRiskStatus, err := user.GetRiskLevelPermissionList(permissionIds, isLogin, userId)
  656. if err != nil {
  657. logger.Error("获取过滤品种信息失败:%v", err)
  658. return
  659. }
  660. if len(filterPermissionIds) == 0 {
  661. logger.Warn("不存在设置风险等级的品种信息")
  662. return
  663. }
  664. total, latestId, ids, discardIds = reportService.GetTotalPageCountByPermissionIds(filterPermissionIds)
  665. return
  666. }
  667. func getCountByAnalyst(permissionIds []int, isLogin bool, userId int, analystName string) (total int64, latestId int64, ids []int, mappingRiskLevel, userRiskStatus string) {
  668. filterPermissionIds, mappingRiskLevel, userRiskStatus, err := user.GetRiskLevelPermissionList(permissionIds, isLogin, userId)
  669. if err != nil {
  670. logger.Error("校验用户风险等级失败:%v", err)
  671. return
  672. }
  673. total, latestId, ids = reportService.GetTotalPageCountByAnalyst(analystName, filterPermissionIds)
  674. return
  675. }
  676. func CountPermissionWeight(ids []int) (permissionMap map[int]int) {
  677. list, err := reportService.CountPermissionWeight(ids)
  678. permissionMap = make(map[int]int)
  679. if err != nil {
  680. return
  681. }
  682. for _, item := range list {
  683. permissionMap[item.PermissionId] = item.Weight
  684. }
  685. return
  686. }
  687. func FilterReportIds(reportIds []int) (total int64, filterReportIds []int, err error) {
  688. return reportService.FilterReportIds(reportIds)
  689. }