bookmark_controller.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. userInfo := bk.Data["user"].(user.User)
  229. var sourceIds []int
  230. pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
  231. //隐藏品种信息未设置风险等级的报告
  232. if sourceType == Report {
  233. pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
  234. }
  235. if pageRes.Total == 0 {
  236. bookMarks := new(page.PageResult)
  237. bookMarks.Data = []interface{}{}
  238. bookMarks.Page = pageRes
  239. bk.SuccessResult("分页搜索收藏列表列表成功", bookMarks, result)
  240. return
  241. }
  242. pageRes.Total = bk.PageInfo.Total
  243. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  244. var bookMarkList []userService.BookMarkInterface
  245. switch sourceType {
  246. case Report:
  247. reportList, reportErr := report.SearchReportBookMark(key, sourceIds, bk.PageInfo, true, userInfo.Id)
  248. if reportErr != nil {
  249. logger.Error("搜索研报列表失败%v", err)
  250. err = exception.NewWithException(exception.GetBookMarkListFailed, reportErr.Error())
  251. bk.FailedResult("分页搜索收藏列表失败", result)
  252. return
  253. }
  254. for _, reportInfo := range reportList {
  255. bookMarkList = append(bookMarkList, ConvertToBookMarkReport(reportInfo))
  256. }
  257. case Chart:
  258. chartList, chartErr := chartService.SearchChartList(key, sourceIds, bk.PageInfo)
  259. if chartErr != nil {
  260. logger.Error("搜索研报列表失败%v", err)
  261. err = exception.NewWithException(exception.GetBookMarkListFailed, chartErr.Error())
  262. bk.FailedResult("分页搜索收藏列表失败", result)
  263. return
  264. }
  265. for _, chart := range chartList {
  266. bookMarkList = append(bookMarkList, ConvertToBookMarkChart(chart))
  267. }
  268. default:
  269. err = exception.NewWithException(exception.GetBookMarkListFailed, "不支持的收藏类型")
  270. bk.FailedResult("分页搜索收藏列表失败", result)
  271. return
  272. }
  273. //bookMarkList, err = user.SearchBookMark(key, sourceType, sourceIds, bk.PageInfo, userInfo.Id)
  274. bookMarks := new(page.PageResult)
  275. bookMarks.Data = bookMarkList
  276. bookMarks.Page = pageRes
  277. bk.SuccessResult("分页搜索收藏列表成功", bookMarks, result)
  278. return
  279. })
  280. }
  281. func ConvertToBookMarkChart(chart chartService.ChartInfo) userService.BookMarkChart {
  282. return userService.BookMarkChart{
  283. ChartName: chart.ChartName,
  284. ChartImage: chart.ChartImage,
  285. UniqueCode: chart.UniqueCode,
  286. ChartInfoId: chart.ChartInfoId,
  287. }
  288. }
  289. func ConvertToBookMarkReport(report reportDomian.ReportDTO) userService.BookMarkReport {
  290. return userService.BookMarkReport{
  291. Abstract: report.Abstract,
  292. Author: report.Author,
  293. AuthorInfo: report.AuthorInfo,
  294. ClassifyId: report.ClassifyId,
  295. CoverSrc: report.CoverSrc,
  296. CoverUrl: report.CoverUrl,
  297. Detail: report.Detail,
  298. Highlight: report.Highlight,
  299. IsFree: report.IsFree,
  300. IsPackage: report.IsPackage,
  301. IsSubscribe: report.IsSubscribe,
  302. Login: report.Login,
  303. OrgId: report.OrgId,
  304. PdfUrl: report.PdfUrl,
  305. PermissionNames: report.PermissionNames,
  306. Permissions: report.Permissions,
  307. PlateName: report.PlateName,
  308. Price: report.Price,
  309. ProductId: report.ProductId,
  310. PublishedTime: report.PublishedTime,
  311. ReportID: report.ReportID,
  312. RiskLevel: report.RiskLevel,
  313. RiskLevelStatus: report.RiskLevelStatus,
  314. Score: report.Score,
  315. SecondPermission: report.SecondPermission,
  316. Show: report.Show,
  317. Source: report.Source,
  318. SubscribeStatus: report.SubscribeStatus,
  319. Title: report.Title,
  320. Type: report.Type,
  321. }
  322. }
  323. // BookMarkList 获取收藏列表
  324. // @Description 获取收藏列表
  325. // @Success 200 {object}
  326. // @router /bookMarkList [get]
  327. func (bk *BookMarkController) BookMarkList(sourceType string) {
  328. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  329. result = bk.InitWrapData("分页查询收藏列表失败")
  330. pageRes := page.Page{
  331. Current: bk.PageInfo.Current,
  332. PageSize: bk.PageInfo.PageSize,
  333. }
  334. if sourceType == "" || (sourceType != Report && sourceType != Chart) {
  335. err = exception.New(exception.IllegalSourceType)
  336. bk.FailedResult("分页查询收藏列表失败", result)
  337. return
  338. }
  339. userInfo := bk.Data["user"].(user.User)
  340. var sourceIds []int
  341. pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
  342. //隐藏品种信息未设置风险等级的报告
  343. if sourceType == Report {
  344. pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
  345. }
  346. if pageRes.Total == 0 {
  347. bookMarks := new(page.PageResult)
  348. bookMarks.Data = []interface{}{}
  349. bookMarks.Page = pageRes
  350. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  351. return
  352. }
  353. pageRes.Total = bk.PageInfo.Total
  354. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  355. switch sourceType {
  356. case Report:
  357. var list []userService.BookMarkReport
  358. list, err = getReportList(bk.PageInfo, userInfo.Id, sourceIds)
  359. if err != nil {
  360. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  361. bk.FailedResult("分页查询收藏列表失败", result)
  362. return
  363. }
  364. bookMarks := new(page.PageResult)
  365. bookMarks.Data = list
  366. bookMarks.Page = pageRes
  367. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  368. return
  369. case Chart:
  370. var list []userService.BookMarkChart
  371. list, err = getChartList(bk.PageInfo, userInfo.Id)
  372. if err != nil {
  373. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  374. bk.FailedResult("分页查询收藏列表失败", result)
  375. return
  376. }
  377. bookMarks := new(page.PageResult)
  378. bookMarks.Data = list
  379. bookMarks.Page = pageRes
  380. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  381. return
  382. default:
  383. err = exception.New(exception.IllegalSourceType)
  384. bk.FailedResult("分页查询收藏列表失败", result)
  385. return
  386. }
  387. })
  388. }
  389. func getReportList(info page.PageInfo, templateUserId int, sourceIds []int) (list []userService.BookMarkReport, err error) {
  390. sourceIds, err = userService.GetBookMarkPageRangeBySourceType(templateUserId, info, Report, sourceIds)
  391. if err != nil {
  392. return
  393. }
  394. userProfile, userErr := user.GetUserProfile(templateUserId)
  395. if userErr != nil {
  396. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  397. err = exception.New(exception.TemplateUserNotFound)
  398. } else {
  399. err = exception.New(exception.TemplateUserFoundFailed)
  400. }
  401. logger.Error("获取临时客户信息失败:%v", err)
  402. return
  403. }
  404. var mappingRiskLevel, userRiskStatus string
  405. userRiskStatus = userProfile.RiskLevelStatus
  406. if userProfile.RiskLevel != "" {
  407. var mapping permissionService.CustomerProductRiskMappingDTO
  408. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  409. if err != nil {
  410. logger.Error("查询产品风险等级映射失败:%v", err)
  411. return
  412. }
  413. mappingRiskLevel = mapping.ProductRiskLevel
  414. }
  415. reports, err := report.GetReportListById(templateUserId, sourceIds, mappingRiskLevel, userRiskStatus)
  416. if err != nil {
  417. return nil, err
  418. }
  419. list = make([]userService.BookMarkReport, len(sourceIds))
  420. // 并发获取数据
  421. for index, sourceId := range sourceIds {
  422. for _, reportDTO := range reports {
  423. if reportDTO.ReportID == sourceId {
  424. reportInfo := ConvertToBookMarkReport(reportDTO)
  425. list[index] = reportInfo
  426. }
  427. }
  428. }
  429. return
  430. }
  431. func getChartList(info page.PageInfo, templateUserId int) (list []userService.BookMarkChart, err error) {
  432. sourceIds, err := userService.GetBookMarkPageBySourceType(templateUserId, info, Chart)
  433. if err != nil {
  434. return
  435. }
  436. // 创建一个切片来存储结果,长度与 sourceIds 相同
  437. list = make([]userService.BookMarkChart, len(sourceIds))
  438. // 使用 WaitGroup 来等待所有 goroutine 完成
  439. var wg sync.WaitGroup
  440. wg.Add(len(sourceIds))
  441. // 使用 Mutex 来保护对 list 的写操作
  442. var mu sync.Mutex
  443. // 并发获取数据
  444. for index, sourceId := range sourceIds {
  445. go func(index int, id int) {
  446. defer wg.Done()
  447. var data chartService.ChartInfo
  448. data, err = chartService.GetChartById(id)
  449. chartInfo := ConvertToBookMarkChart(data)
  450. if err != nil {
  451. logger.Error("获取数据失败: %v", err)
  452. }
  453. // 使用 Mutex 保护对 list 的写操作
  454. mu.Lock()
  455. list[index] = chartInfo
  456. mu.Unlock()
  457. }(index, sourceId)
  458. }
  459. // 等待所有 goroutine 完成
  460. wg.Wait()
  461. return
  462. }