bookmark_controller.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. bookMarkList, err = user.SearchBookMark(key, sourceType, sourceIds, bk.PageInfo, userInfo.Id)
  246. if err != nil {
  247. err = exception.New(exception.BookMarkListFailed)
  248. bk.FailedResult("分页搜索收藏列表失败", result)
  249. return
  250. }
  251. bookMarks := new(page.PageResult)
  252. bookMarks.Data = bookMarkList
  253. bookMarks.Page = pageRes
  254. bk.SuccessResult("分页搜索收藏列表成功", bookMarks, result)
  255. return
  256. })
  257. }
  258. // BookMarkList 获取收藏列表
  259. // @Description 获取收藏列表
  260. // @Success 200 {object}
  261. // @router /bookMarkList [get]
  262. func (bk *BookMarkController) BookMarkList(sourceType string) {
  263. controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
  264. result = bk.InitWrapData("分页查询收藏列表失败")
  265. pageRes := page.Page{
  266. Current: bk.PageInfo.Current,
  267. PageSize: bk.PageInfo.PageSize,
  268. }
  269. if sourceType == "" || (sourceType != Report && sourceType != Chart) {
  270. err = exception.New(exception.IllegalSourceType)
  271. bk.FailedResult("分页查询收藏列表失败", result)
  272. return
  273. }
  274. userInfo := bk.Data["user"].(user.User)
  275. var sourceIds []int
  276. pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
  277. //隐藏品种信息未设置风险等级的报告
  278. if sourceType == Report {
  279. pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
  280. }
  281. if pageRes.Total == 0 {
  282. bookMarks := new(page.PageResult)
  283. bookMarks.Data = []interface{}{}
  284. bookMarks.Page = pageRes
  285. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  286. return
  287. }
  288. pageRes.Total = bk.PageInfo.Total
  289. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  290. switch sourceType {
  291. case Report:
  292. var list []userService.BookMarkReport
  293. list, err = getReportList(bk.PageInfo, userInfo.Id, sourceIds)
  294. if err != nil {
  295. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  296. bk.FailedResult("分页查询收藏列表失败", result)
  297. return
  298. }
  299. bookMarks := new(page.PageResult)
  300. bookMarks.Data = list
  301. bookMarks.Page = pageRes
  302. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  303. return
  304. case Chart:
  305. var list []userService.BookMarkChart
  306. list, err = getChartList(bk.PageInfo, userInfo.Id)
  307. if err != nil {
  308. err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
  309. bk.FailedResult("分页查询收藏列表失败", result)
  310. return
  311. }
  312. bookMarks := new(page.PageResult)
  313. bookMarks.Data = list
  314. bookMarks.Page = pageRes
  315. bk.SuccessResult("分页查询收藏列表成功", bookMarks, result)
  316. return
  317. default:
  318. err = exception.New(exception.IllegalSourceType)
  319. bk.FailedResult("分页查询收藏列表失败", result)
  320. return
  321. }
  322. })
  323. }
  324. func getReportList(info page.PageInfo, templateUserId int, sourceIds []int) (list []userService.BookMarkReport, err error) {
  325. sourceIds, err = userService.GetBookMarkPageRangeBySourceType(templateUserId, info, Report, sourceIds)
  326. if err != nil {
  327. return
  328. }
  329. userProfile, userErr := user.GetUserProfile(templateUserId)
  330. if userErr != nil {
  331. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  332. err = exception.New(exception.TemplateUserNotFound)
  333. } else {
  334. err = exception.New(exception.TemplateUserFoundFailed)
  335. }
  336. logger.Error("获取临时客户信息失败:%v", err)
  337. return
  338. }
  339. var mappingRiskLevel, userRiskStatus string
  340. userRiskStatus = userProfile.RiskLevelStatus
  341. if userProfile.RiskLevel != "" {
  342. var mapping permissionService.CustomerProductRiskMappingDTO
  343. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  344. if err != nil {
  345. logger.Error("查询产品风险等级映射失败:%v", err)
  346. return
  347. }
  348. mappingRiskLevel = mapping.ProductRiskLevel
  349. }
  350. reports, err := report.GetReportListById(templateUserId, sourceIds, mappingRiskLevel, userRiskStatus)
  351. if err != nil {
  352. return nil, err
  353. }
  354. list = make([]userService.BookMarkReport, len(sourceIds))
  355. // 并发获取数据
  356. for index, sourceId := range sourceIds {
  357. for _, reportDTO := range reports {
  358. if reportDTO.ReportID == sourceId {
  359. reportInfo := userService.ConvertToBookMarkReport(reportDTO)
  360. list[index] = reportInfo
  361. }
  362. }
  363. }
  364. return
  365. }
  366. func getChartList(info page.PageInfo, templateUserId int) (list []userService.BookMarkChart, err error) {
  367. sourceIds, err := userService.GetBookMarkPageBySourceType(templateUserId, info, Chart)
  368. if err != nil {
  369. return
  370. }
  371. // 创建一个切片来存储结果,长度与 sourceIds 相同
  372. list = make([]userService.BookMarkChart, len(sourceIds))
  373. // 使用 WaitGroup 来等待所有 goroutine 完成
  374. var wg sync.WaitGroup
  375. wg.Add(len(sourceIds))
  376. // 使用 Mutex 来保护对 list 的写操作
  377. var mu sync.Mutex
  378. // 并发获取数据
  379. for index, sourceId := range sourceIds {
  380. go func(index int, id int) {
  381. defer wg.Done()
  382. var data chartService.ChartInfo
  383. data, err = chartService.GetChartById(id)
  384. chartInfo := userService.ConvertToBookMarkChart(data)
  385. if err != nil {
  386. logger.Error("获取数据失败: %v", err)
  387. }
  388. // 使用 Mutex 保护对 list 的写操作
  389. mu.Lock()
  390. list[index] = chartInfo
  391. mu.Unlock()
  392. }(index, sourceId)
  393. }
  394. // 等待所有 goroutine 完成
  395. wg.Wait()
  396. return
  397. }