bookmark_controller.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package user
  2. import (
  3. "errors"
  4. "eta/eta_mini_ht_api/common/component/cache"
  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/page"
  8. "eta/eta_mini_ht_api/controllers"
  9. permissionService "eta/eta_mini_ht_api/domian/config"
  10. reportDomian "eta/eta_mini_ht_api/domian/report"
  11. chartService "eta/eta_mini_ht_api/service/media"
  12. "eta/eta_mini_ht_api/service/report"
  13. "eta/eta_mini_ht_api/service/user"
  14. userService "eta/eta_mini_ht_api/service/user"
  15. "gorm.io/gorm"
  16. "sync"
  17. )
  18. // BookMarkController Operations about bookmark
  19. type BookMarkController struct {
  20. controllers.ListController
  21. redis *cache.RedisCache
  22. }
  23. const (
  24. Chart = "chart"
  25. Report = "report"
  26. )
  27. func (bk *BookMarkController) Prepare() {
  28. bk.ListController.Prepare()
  29. bk.redis = cache.GetInstance()
  30. }
  31. type BookMarkReq struct {
  32. SourceType string `json:"sourceType"`
  33. SourceId int `json:"sourceId"`
  34. ChartImage string `json:"chartImage"`
  35. ChartInfoId int `json:"chartInfoId"`
  36. ChartName string `json:"chartName"`
  37. UniqueCode string `json:"uniqueCode"`
  38. }
  39. // BookMark 收藏
  40. // @Summary 收藏
  41. // @Description 收藏
  42. // @Success 200 {object} controllers.BaseResponse
  43. // @router /bookMark [post]
  44. func (bk *BookMarkController) BookMark() {
  45. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  46. result = bk.InitWrapData("收藏失败")
  47. bookMark := new(BookMarkReq)
  48. bk.GetPostParams(bookMark)
  49. var userInfo user.User
  50. userInfo = bk.Data["user"].(user.User)
  51. if bookMark.SourceType == "" {
  52. bk.FailedResult("收藏失败", result)
  53. err = exception.New(exception.IllegalSourceType)
  54. return
  55. }
  56. if bookMark.SourceId == 0 {
  57. bk.FailedResult("收藏失败", result)
  58. err = exception.New(exception.IllegalSourceId)
  59. return
  60. }
  61. err = userService.BookMark(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
  62. if err != nil {
  63. err = exception.NewWithException(exception.FeedBackError, err.Error())
  64. bk.FailedResult("收藏失败", result)
  65. return
  66. }
  67. //将图表加入es
  68. if bookMark.SourceType == Chart {
  69. chartService.AddChartToEs(chartService.ChartInfo{
  70. ChartImage: bookMark.ChartImage,
  71. ChartInfoId: bookMark.ChartInfoId,
  72. ChartName: bookMark.ChartName,
  73. UniqueCode: bookMark.UniqueCode,
  74. })
  75. }
  76. bk.SuccessResult("收藏成功", nil, result)
  77. return
  78. })
  79. }
  80. // UnBookMark 取消收藏
  81. // @Summary 取消收藏
  82. // @Description 取消收藏
  83. // @Success 200 {object} controllers.BaseResponse
  84. // @router /unBookMark [post]
  85. func (bk *BookMarkController) UnBookMark() {
  86. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  87. result = bk.InitWrapData("取消收藏失败")
  88. bookMark := new(BookMarkReq)
  89. bk.GetPostParams(bookMark)
  90. var userInfo user.User
  91. userInfo = bk.Data["user"].(user.User)
  92. if bookMark.SourceType == "" {
  93. bk.FailedResult("取消收藏失败", result)
  94. err = exception.New(exception.IllegalSourceType)
  95. return
  96. }
  97. if bookMark.SourceId == 0 {
  98. bk.FailedResult("取消收藏失败", result)
  99. err = exception.New(exception.IllegalSourceId)
  100. return
  101. }
  102. err = userService.UnBookMark(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
  103. if err != nil {
  104. err = exception.NewWithException(exception.FeedBackError, err.Error())
  105. bk.FailedResult("取消收藏失败", result)
  106. return
  107. }
  108. bk.SuccessResult("收藏成功", nil, result)
  109. return
  110. })
  111. }
  112. type CheckBookMarkResp struct {
  113. IsBookMarked bool `json:"isBookMarked"`
  114. }
  115. // CheckBookMark 取消收藏
  116. // @Summary 取消收藏
  117. // @Description 取消收藏
  118. // @Success 200 {object} controllers.BaseResponse
  119. // @router /checkBookMark [post]
  120. func (bk *BookMarkController) CheckBookMark() {
  121. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  122. result = bk.InitWrapData("取消收藏失败")
  123. bookMark := new(BookMarkReq)
  124. bk.GetPostParams(bookMark)
  125. var userInfo user.User
  126. userInfo = bk.Data["user"].(user.User)
  127. if bookMark.SourceType == "" {
  128. bk.FailedResult("获取是否收藏失败", result)
  129. err = exception.New(exception.IllegalSourceType)
  130. return
  131. }
  132. if bookMark.SourceId == 0 {
  133. bk.FailedResult("获取是否收藏失败", result)
  134. err = exception.New(exception.IllegalSourceId)
  135. return
  136. }
  137. isBookMarked, err := userService.CheckBookMarkStatus(userInfo.Id, bookMark.SourceId, bookMark.SourceType)
  138. if err != nil {
  139. err = exception.NewWithException(exception.FeedBackError, err.Error())
  140. bk.FailedResult("获取是否收藏失败", result)
  141. return
  142. }
  143. bk.SuccessResult("获取是否收藏成功", &CheckBookMarkResp{
  144. IsBookMarked: isBookMarked,
  145. }, result)
  146. return
  147. })
  148. }
  149. // BookMarkSearch 搜索收藏列表
  150. // @Description 搜索报告列表
  151. // @Success 200 {object}
  152. // @router /bookMarkSearch [get]
  153. func (bk *BookMarkController) BookMarkSearch(key string) {
  154. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  155. result = bk.InitWrapData("分页搜索报告列表失败")
  156. if key == "" {
  157. err = exception.New(exception.SearchKeyEmptyError)
  158. bk.FailedResult("分页搜索报告列表失败", result)
  159. return
  160. }
  161. userInfo := bk.Data["user"].(user.User)
  162. pageRes := page.Page{
  163. Current: bk.PageInfo.Current,
  164. PageSize: bk.PageInfo.PageSize,
  165. }
  166. //获取当前可以被搜索的报告原始ID
  167. //先要限制查询的id范围
  168. var reportIds []int
  169. var mappingRiskLevel string
  170. var userRiskStatus string
  171. pageRes.Total, pageRes.LatestId, reportIds, _, mappingRiskLevel, userRiskStatus, err = report.RangeSearch(key, true, userInfo.Id)
  172. if err != nil {
  173. logger.Error("获取报告原始ID列表失败:%v", err)
  174. bk.FailedResult("分页搜索报告列表失败", result)
  175. return
  176. }
  177. if len(reportIds) == 0 {
  178. reports := new(page.PageResult)
  179. reports.Data = []reportDomian.ReportDTO{}
  180. reports.Page = pageRes
  181. logger.Info("没有可以查询的报告列表")
  182. bk.SuccessResult("分页搜索报告列表成功", reports, result)
  183. return
  184. }
  185. if bk.PageInfo.LatestId == 0 {
  186. //pageRes.Total, pageRes.LatestId = report.SearchMaxReportId(key)
  187. bk.PageInfo.LatestId = pageRes.LatestId
  188. bk.PageInfo.Total = pageRes.Total
  189. } else {
  190. pageRes.LatestId = bk.PageInfo.LatestId
  191. pageRes.Total = bk.PageInfo.Total
  192. }
  193. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  194. list := make([]reportDomian.ReportDTO, 0)
  195. if pageRes.LatestId > 0 {
  196. list, err = report.SearchReportList(key, reportIds, bk.PageInfo, true, userInfo.Id, mappingRiskLevel, userRiskStatus)
  197. if err != nil {
  198. bk.FailedResult("分页搜索报告列表失败", result)
  199. return
  200. }
  201. }
  202. reports := new(page.PageResult)
  203. reports.Data = list
  204. reports.Page = pageRes
  205. bk.SuccessResult("分页搜索报告列表成功", reports, result)
  206. return
  207. })
  208. }
  209. type BookMarkListReq struct {
  210. SourceType string `json:"source_type"`
  211. }
  212. // SearchBookMark 获取收藏列表
  213. // @Description 获取收藏列表
  214. // @Success 200 {object}
  215. // @router /searchBookMark [get]
  216. func (bk *BookMarkController) SearchBookMark(sourceType string, key string) {
  217. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  218. result = bk.InitWrapData("分页搜索收藏列表失败")
  219. pageRes := page.Page{
  220. Current: bk.PageInfo.Current,
  221. PageSize: bk.PageInfo.PageSize,
  222. }
  223. if sourceType == "" || (sourceType != Report && sourceType != Chart) {
  224. err = exception.New(exception.IllegalSourceType)
  225. bk.FailedResult("分页搜索收藏列表失败", result)
  226. return
  227. }
  228. if key == "" {
  229. err = exception.New(exception.IllegalSearchKeyword)
  230. bk.FailedResult("分页搜索收藏列表失败", result)
  231. return
  232. }
  233. userInfo := bk.Data["user"].(user.User)
  234. var sourceIds []int
  235. pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
  236. //隐藏品种信息未设置风险等级的报告
  237. if sourceType == Report {
  238. pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
  239. }
  240. if pageRes.Total == 0 {
  241. bookMarks := new(page.PageResult)
  242. bookMarks.Data = []interface{}{}
  243. bookMarks.Page = pageRes
  244. bk.SuccessResult("分页搜索收藏列表列表成功", bookMarks, result)
  245. return
  246. }
  247. pageRes.Total = bk.PageInfo.Total
  248. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  249. var bookMarkList []userService.BookMarkInterface
  250. switch sourceType {
  251. case Report:
  252. reportList, reportErr := report.SearchReportBookMark(key, sourceIds, bk.PageInfo, true, userInfo.Id)
  253. if reportErr != nil {
  254. logger.Error("搜索研报列表失败%v", err)
  255. err = exception.NewWithException(exception.GetBookMarkListFailed, reportErr.Error())
  256. bk.FailedResult("分页搜索收藏列表失败", result)
  257. return
  258. }
  259. for _, reportInfo := range reportList {
  260. bookMarkList = append(bookMarkList, ConvertToBookMarkReport(reportInfo))
  261. }
  262. case Chart:
  263. chartList, chartErr := chartService.SearchChartList(key, sourceIds, bk.PageInfo)
  264. if chartErr != nil {
  265. logger.Error("搜索研报列表失败%v", err)
  266. err = exception.NewWithException(exception.GetBookMarkListFailed, chartErr.Error())
  267. bk.FailedResult("分页搜索收藏列表失败", result)
  268. return
  269. }
  270. for _, chart := range chartList {
  271. bookMarkList = append(bookMarkList, ConvertToBookMarkChart(chart))
  272. }
  273. default:
  274. err = exception.NewWithException(exception.GetBookMarkListFailed, "不支持的收藏类型")
  275. bk.FailedResult("分页搜索收藏列表失败", result)
  276. return
  277. }
  278. //bookMarkList, err = user.SearchBookMark(key, sourceType, sourceIds, bk.PageInfo, userInfo.Id)
  279. bookMarks := new(page.PageResult)
  280. bookMarks.Data = bookMarkList
  281. bookMarks.Page = pageRes
  282. bk.SuccessResult("分页搜索收藏列表成功", bookMarks, result)
  283. return
  284. })
  285. }
  286. func ConvertToBookMarkChart(chart chartService.ChartInfo) userService.BookMarkChart {
  287. return userService.BookMarkChart{
  288. ChartName: chart.ChartName,
  289. ChartImage: chart.ChartImage,
  290. UniqueCode: chart.UniqueCode,
  291. ChartInfoId: chart.ChartInfoId,
  292. }
  293. }
  294. func ConvertToBookMarkReport(report reportDomian.ReportDTO) userService.BookMarkReport {
  295. return userService.BookMarkReport{
  296. Abstract: report.Abstract,
  297. Author: report.Author,
  298. AuthorInfo: report.AuthorInfo,
  299. ClassifyId: report.ClassifyId,
  300. CoverSrc: report.CoverSrc,
  301. CoverUrl: report.CoverUrl,
  302. Detail: report.Detail,
  303. Highlight: report.Highlight,
  304. IsFree: report.IsFree,
  305. IsPackage: report.IsPackage,
  306. IsSubscribe: report.IsSubscribe,
  307. Login: report.Login,
  308. OrgId: report.OrgId,
  309. PdfUrl: report.PdfUrl,
  310. PermissionNames: report.PermissionNames,
  311. Permissions: report.Permissions,
  312. PlateName: report.PlateName,
  313. Price: report.Price,
  314. ProductId: report.ProductId,
  315. PublishedTime: report.PublishedTime,
  316. ReportID: report.ReportID,
  317. RiskLevel: report.RiskLevel,
  318. RiskLevelStatus: report.RiskLevelStatus,
  319. Score: report.Score,
  320. SecondPermission: report.SecondPermission,
  321. Show: report.Show,
  322. Source: report.Source,
  323. SubscribeStatus: report.SubscribeStatus,
  324. Title: report.Title,
  325. Type: report.Type,
  326. }
  327. }
  328. // BookMarkList 获取收藏列表
  329. // @Description 获取收藏列表
  330. // @Success 200 {object}
  331. // @router /bookMarkList [get]
  332. func (bk *BookMarkController) BookMarkList(sourceType string) {
  333. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  334. result = bk.InitWrapData("分页查询收藏列表失败")
  335. pageRes := page.Page{
  336. Current: bk.PageInfo.Current,
  337. PageSize: bk.PageInfo.PageSize,
  338. }
  339. if sourceType == "" || (sourceType != Report && sourceType != Chart) {
  340. err = exception.New(exception.IllegalSourceType)
  341. bk.FailedResult("分页查询收藏列表失败", result)
  342. return
  343. }
  344. userInfo := bk.Data["user"].(user.User)
  345. var sourceIds []int
  346. pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
  347. //隐藏品种信息未设置风险等级的报告
  348. if sourceType == Report {
  349. pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
  350. }
  351. if pageRes.Total == 0 {
  352. bookMarks := new(page.PageResult)
  353. bookMarks.Data = []interface{}{}
  354. bookMarks.Page = pageRes
  355. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  356. return
  357. }
  358. pageRes.Total = bk.PageInfo.Total
  359. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  360. switch sourceType {
  361. case Report:
  362. var list []userService.BookMarkReport
  363. list, err = getReportList(bk.PageInfo, userInfo.Id, sourceIds)
  364. if err != nil {
  365. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  366. bk.FailedResult("分页查询收藏列表失败", result)
  367. return
  368. }
  369. bookMarks := new(page.PageResult)
  370. bookMarks.Data = list
  371. bookMarks.Page = pageRes
  372. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  373. return
  374. case Chart:
  375. var list []userService.BookMarkChart
  376. list, err = getChartList(bk.PageInfo, userInfo.Id)
  377. if err != nil {
  378. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  379. bk.FailedResult("分页查询收藏列表失败", result)
  380. return
  381. }
  382. bookMarks := new(page.PageResult)
  383. bookMarks.Data = list
  384. bookMarks.Page = pageRes
  385. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  386. return
  387. default:
  388. err = exception.New(exception.IllegalSourceType)
  389. bk.FailedResult("分页查询收藏列表失败", result)
  390. return
  391. }
  392. })
  393. }
  394. func getReportList(info page.PageInfo, templateUserId int, sourceIds []int) (list []userService.BookMarkReport, err error) {
  395. sourceIds, err = userService.GetBookMarkPageRangeBySourceType(templateUserId, info, Report, sourceIds)
  396. if err != nil {
  397. return
  398. }
  399. userProfile, userErr := user.GetUserProfile(templateUserId)
  400. if userErr != nil {
  401. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  402. err = exception.New(exception.TemplateUserNotFound)
  403. } else {
  404. err = exception.New(exception.TemplateUserFoundFailed)
  405. }
  406. logger.Error("获取临时客户信息失败:%v", err)
  407. return
  408. }
  409. var mappingRiskLevel, userRiskStatus string
  410. userRiskStatus = userProfile.RiskLevelStatus
  411. if userProfile.RiskLevel != "" {
  412. var mapping permissionService.CustomerProductRiskMappingDTO
  413. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  414. if err != nil {
  415. logger.Error("查询产品风险等级映射失败:%v", err)
  416. return
  417. }
  418. mappingRiskLevel = mapping.ProductRiskLevel
  419. }
  420. reports, err := report.GetReportListById(templateUserId, sourceIds, mappingRiskLevel, userRiskStatus)
  421. if err != nil {
  422. return nil, err
  423. }
  424. list = make([]userService.BookMarkReport, len(sourceIds))
  425. // 并发获取数据
  426. for index, sourceId := range sourceIds {
  427. for _, reportDTO := range reports {
  428. if reportDTO.ReportID == sourceId {
  429. reportInfo := ConvertToBookMarkReport(reportDTO)
  430. list[index] = reportInfo
  431. }
  432. }
  433. }
  434. return
  435. }
  436. func getChartList(info page.PageInfo, templateUserId int) (list []userService.BookMarkChart, err error) {
  437. sourceIds, err := userService.GetBookMarkPageBySourceType(templateUserId, info, Chart)
  438. if err != nil {
  439. return
  440. }
  441. // 创建一个切片来存储结果,长度与 sourceIds 相同
  442. list = make([]userService.BookMarkChart, len(sourceIds))
  443. // 使用 WaitGroup 来等待所有 goroutine 完成
  444. var wg sync.WaitGroup
  445. wg.Add(len(sourceIds))
  446. // 使用 Mutex 来保护对 list 的写操作
  447. var mu sync.Mutex
  448. // 并发获取数据
  449. for index, sourceId := range sourceIds {
  450. go func(index int, id int) {
  451. defer wg.Done()
  452. var data chartService.ChartInfo
  453. data, err = chartService.GetChartById(id)
  454. chartInfo := ConvertToBookMarkChart(data)
  455. if err != nil {
  456. logger.Error("获取数据失败: %v", err)
  457. }
  458. // 使用 Mutex 保护对 list 的写操作
  459. mu.Lock()
  460. list[index] = chartInfo
  461. mu.Unlock()
  462. }(index, sourceId)
  463. }
  464. // 等待所有 goroutine 完成
  465. wg.Wait()
  466. return
  467. }