my_report.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/request"
  6. "eta/eta_mini_api/models/response"
  7. "eta/eta_mini_api/services"
  8. "eta/eta_mini_api/utils"
  9. "fmt"
  10. "time"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. )
  13. type MyReportController struct {
  14. BaseAuthController
  15. }
  16. // @Title 获取我的报告列表
  17. // @Description 获取我的报告列表
  18. // @Param PageSize query int true "每页数据条数"
  19. // @Param CurrentIndex query int true "当前页页码,从1开始"
  20. // @Success 200 {object} models.MyReportResp
  21. // @router /list [get]
  22. func (this *MyReportController) List() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. if err := recover(); err != nil {
  26. fmt.Println(err)
  27. }
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. user := this.User
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. if pageSize <= 0 {
  35. pageSize = utils.PageSize30
  36. }
  37. if currentIndex <= 0 {
  38. currentIndex = 1
  39. }
  40. if user.Status != utils.UserStatusFormal {
  41. br.Msg = "该用户没有收藏权限"
  42. return
  43. }
  44. total, err := models.GetMyReportListCountByUserId(user.UserId)
  45. if err != nil {
  46. br.Msg = "获取我的报告列表失败"
  47. br.ErrMsg = "获取我的报告列表失败,系统异常,Err:" + err.Error()
  48. return
  49. }
  50. resp := new(response.MyReportListResp)
  51. startSize := utils.StartIndex(currentIndex, pageSize)
  52. reportList, err := models.GetMyReportListByUserId(user.UserId, startSize, pageSize)
  53. if err != nil {
  54. br.Msg = "获取我的报告列表失败"
  55. br.ErrMsg = "获取我的报告列表失败,系统异常,Err:" + err.Error()
  56. return
  57. }
  58. page := paging.GetPaging(currentIndex, pageSize, total)
  59. resp.List = reportList
  60. resp.Paging = page
  61. br.Data = resp
  62. br.Success = true
  63. br.Msg = "获取我的报告列表成功"
  64. br.Ret = 200
  65. }
  66. // @Title 收藏研报
  67. // @Description 收藏研报
  68. // @Success 200 {object} models.BaseResponse
  69. // @Failure 403 {object} models.BaseResponse
  70. // @router /collect [post]
  71. func (this *MyReportController) Collect() {
  72. br := new(models.BaseResponse).Init()
  73. defer func() {
  74. this.Data["json"] = br
  75. this.ServeJSON()
  76. }()
  77. var req request.MyReportCollectReq
  78. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  79. br.Msg = "参数解析失败"
  80. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  81. return
  82. }
  83. user := this.User
  84. if user.Status != utils.UserStatusFormal {
  85. br.Msg = "用户没有权限收藏"
  86. return
  87. }
  88. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta)
  89. if err != nil {
  90. br.Msg = "收藏失败"
  91. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  92. return
  93. }
  94. if count > 0 {
  95. br.Msg = "该研报已收藏,请重新刷新页面"
  96. return
  97. }
  98. reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
  99. if err != nil {
  100. br.Msg = "收藏失败"
  101. br.ErrMsg = "获取研报详情失败,Err:" + err.Error()
  102. return
  103. }
  104. if reportResp.Ret != 200 {
  105. br.Msg = "收藏失败"
  106. br.ErrMsg = "获取研报详情失败,Err:" + reportResp.ErrMsg
  107. return
  108. }
  109. if reportResp.Data.Report == nil {
  110. br.Msg = "该研报已删除或不存在,请刷新页面"
  111. return
  112. }
  113. if reportResp.Data.Status != utils.ReportPermissionStatusHas {
  114. br.Msg = "用户权限不足,不能收藏"
  115. return
  116. }
  117. report := reportResp.Data.Report
  118. publishTime, err := time.Parse(utils.FormatDateTime, report.PublishTime)
  119. if err != nil {
  120. br.Msg = "时间格式不对"
  121. return
  122. }
  123. myChart := &models.MyReport{
  124. UserId: user.UserId,
  125. ReportId: req.ReportId,
  126. Title: report.Title,
  127. Abstract: report.Abstract,
  128. Author: report.Author,
  129. PublishTime: publishTime,
  130. Stage: report.Stage,
  131. CreateTime: time.Now(),
  132. ReportType: utils.ReportTypeEta,
  133. }
  134. err = myChart.Insert()
  135. if err != nil {
  136. br.Msg = "收藏失败"
  137. br.ErrMsg = "收藏失败,Err:" + err.Error()
  138. return
  139. }
  140. br.Msg = "收藏成功"
  141. br.Success = true
  142. br.Ret = 200
  143. }
  144. // @Title 收藏pdf研报
  145. // @Description 收藏pdf研报
  146. // @Success 200 {object} models.BaseResponse
  147. // @Failure 403 {object} models.BaseResponse
  148. // @router /pdf/collect [post]
  149. func (this *MyReportController) PdfCollect() {
  150. br := new(models.BaseResponse).Init()
  151. defer func() {
  152. this.Data["json"] = br
  153. this.ServeJSON()
  154. }()
  155. var req request.MyReportPdfCollectReq
  156. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  157. br.Msg = "参数解析失败"
  158. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  159. return
  160. }
  161. user := this.User
  162. if user.Status != utils.UserStatusFormal {
  163. br.Msg = "用户没有权限收藏"
  164. return
  165. }
  166. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf)
  167. if err != nil {
  168. br.Msg = "收藏失败"
  169. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  170. return
  171. }
  172. if count > 0 {
  173. br.Msg = "该研报已收藏,请重新刷新页面"
  174. return
  175. }
  176. reportPdf, err := models.GetReportPdfById(req.ReportPdfId)
  177. if err != nil {
  178. if err.Error() == utils.ErrNoRow() {
  179. br.Msg = "研报不存在或已删除"
  180. return
  181. }
  182. br.Msg = "收藏失败"
  183. br.ErrMsg = "获取研报详情失败,Err:" + err.Error()
  184. return
  185. }
  186. reportClassifyId := services.GetReportPdfClassify(reportPdf)
  187. chartPermissionResp, err := services.GetChartPermissionListByClassifyId(reportClassifyId, 0)
  188. if err != nil {
  189. br.Msg = "收藏失败"
  190. br.ErrMsg = "获取研报详情失败,Err:" + err.Error()
  191. return
  192. }
  193. if chartPermissionResp.Ret != 200 {
  194. br.Msg = "收藏失败"
  195. br.ErrMsg = "获取研报详情失败,Err:" + chartPermissionResp.ErrMsg
  196. return
  197. }
  198. var IsPublic bool
  199. var permissionMap = make(map[int]struct{})
  200. for _, v := range chartPermissionResp.Data {
  201. if v.IsPublic == 1 {
  202. IsPublic = true
  203. }
  204. permissionMap[v.ChartPermissionId] = struct{}{}
  205. }
  206. // 校验是否有收藏权限
  207. var IsHas bool
  208. if !IsPublic {
  209. chartPermissionIds, err := models.GetUserChartPermissionIdByUserId(user.UserId)
  210. if err != nil {
  211. br.Msg = "收藏失败"
  212. br.ErrMsg = "获取用户权限失败,Err:" + err.Error()
  213. return
  214. }
  215. if len(chartPermissionIds) == 0 {
  216. br.Msg = "用户没有权限收藏"
  217. return
  218. }
  219. for _, v := range chartPermissionIds {
  220. if _, ok := permissionMap[v]; ok {
  221. IsHas = true
  222. break
  223. }
  224. }
  225. if !IsHas {
  226. br.Msg = "用户没有权限收藏"
  227. return
  228. }
  229. }
  230. publishTime, err := time.Parse(utils.FormatDateTime, reportPdf.PublishTime)
  231. if err != nil {
  232. br.Msg = "收藏失败"
  233. br.ErrMsg = "时间格式不对, err:" + err.Error()
  234. return
  235. }
  236. myChart := &models.MyReport{
  237. UserId: user.UserId,
  238. ReportId: req.ReportPdfId,
  239. Title: reportPdf.Title,
  240. Abstract: reportPdf.Abstract,
  241. Author: reportPdf.Author,
  242. PublishTime: publishTime,
  243. Stage: reportPdf.Stage,
  244. CreateTime: time.Now(),
  245. ReportType: utils.ReportTypePdf,
  246. }
  247. err = myChart.Insert()
  248. if err != nil {
  249. br.Msg = "收藏失败"
  250. br.ErrMsg = "收藏失败,Err:" + err.Error()
  251. return
  252. }
  253. br.Msg = "收藏成功"
  254. br.Success = true
  255. br.Ret = 200
  256. }
  257. // @Title 收藏pdf研报
  258. // @Description 收藏pdf研报
  259. // @Success 200 {object} models.BaseResponse
  260. // @Failure 403 {object} models.BaseResponse
  261. // @router /pdf/collectCancel [post]
  262. func (this *MyReportController) PdfCollectCancel() {
  263. br := new(models.BaseResponse).Init()
  264. defer func() {
  265. this.Data["json"] = br
  266. this.ServeJSON()
  267. }()
  268. var req request.MyReportPdfCollectReq
  269. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  270. br.Msg = "参数解析失败"
  271. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  272. return
  273. }
  274. user := this.User
  275. if user.Status != utils.UserStatusFormal {
  276. br.Msg = "用户没有权限收藏"
  277. return
  278. }
  279. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf)
  280. if err != nil {
  281. br.Msg = "取消收藏失败"
  282. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  283. return
  284. }
  285. if count == 0 {
  286. br.Msg = "该研报已取消收藏,请重新刷新页面"
  287. return
  288. }
  289. err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf)
  290. if err != nil {
  291. br.Msg = "取消收藏失败"
  292. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  293. return
  294. }
  295. br.Msg = "取消收藏成功"
  296. br.Success = true
  297. br.Ret = 200
  298. }
  299. // @Title 取消收藏
  300. // @Description 取消收藏
  301. // @Param PageSize query int true "每页数据条数"
  302. // @Param CurrentIndex query int true "当前页页码,从1开始"
  303. // @Success 200 {object} models.BaseResponse
  304. // @Failure 403 {object} models.BaseResponse
  305. // @router /collectCancel [post]
  306. func (this *MyReportController) CollectCancel() {
  307. br := new(models.BaseResponse).Init()
  308. defer func() {
  309. this.Data["json"] = br
  310. this.ServeJSON()
  311. }()
  312. var req request.MyReportCollectReq
  313. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  314. br.Msg = "参数解析失败"
  315. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  316. return
  317. }
  318. user := this.User
  319. if user.Status != utils.UserStatusFormal {
  320. br.Msg = "用户没有权限收藏"
  321. return
  322. }
  323. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta)
  324. if err != nil {
  325. br.Msg = "取消收藏失败"
  326. br.ErrMsg = "获取收藏信息失败,Err:" + err.Error()
  327. return
  328. }
  329. if count == 0 {
  330. br.Msg = "该研报已取消收藏,请重新刷新页面"
  331. return
  332. }
  333. err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta)
  334. if err != nil {
  335. br.Msg = "取消收藏失败"
  336. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  337. return
  338. }
  339. br.Msg = "取消收藏成功"
  340. br.Success = true
  341. br.Ret = 200
  342. }
  343. // @Title 是否收藏pdf研报
  344. // @Description 是否收藏pdf研报
  345. // @Param request body request.MyReportCollectReq true "type json string"
  346. // @Success 200 {object} models.BaseResponse
  347. // @router /pdf/isCollect [post]
  348. func (this *MyReportController) IsPdfCollect() {
  349. br := new(models.BaseResponse).Init()
  350. defer func() {
  351. this.Data["json"] = br
  352. this.ServeJSON()
  353. }()
  354. var req request.MyReportPdfCollectReq
  355. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  356. br.Msg = "参数解析失败"
  357. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  358. return
  359. }
  360. user := this.User
  361. if user.Status != utils.UserStatusFormal {
  362. br.Msg = "用户没有权限收藏"
  363. return
  364. }
  365. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportPdfId, utils.ReportTypePdf)
  366. if err != nil {
  367. br.Msg = "收藏失败"
  368. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  369. return
  370. }
  371. resp := new(response.MyReportIsCollectResp)
  372. if count > 0 {
  373. resp.IsCollect = true
  374. } else {
  375. resp.IsCollect = false
  376. }
  377. br.Data = resp
  378. br.Msg = "获取成功"
  379. br.Success = true
  380. br.Ret = 200
  381. }
  382. // @Title 是否收藏研报
  383. // @Description 是否收藏研报
  384. // @Param request body request.MyReportCollectReq true "type json string"
  385. // @Success 200 {object} models.BaseResponse
  386. // @router /isCollect [post]
  387. func (this *MyReportController) IsCollect() {
  388. br := new(models.BaseResponse).Init()
  389. defer func() {
  390. this.Data["json"] = br
  391. this.ServeJSON()
  392. }()
  393. var req request.MyReportCollectReq
  394. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  395. br.Msg = "参数解析失败"
  396. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  397. return
  398. }
  399. user := this.User
  400. if user.Status != utils.UserStatusFormal {
  401. br.Msg = "用户没有权限收藏"
  402. return
  403. }
  404. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId, utils.ReportTypeEta)
  405. if err != nil {
  406. br.Msg = "收藏失败"
  407. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  408. return
  409. }
  410. resp := new(response.MyReportIsCollectResp)
  411. if count > 0 {
  412. resp.IsCollect = true
  413. } else {
  414. resp.IsCollect = false
  415. }
  416. br.Data = resp
  417. br.Msg = "获取成功"
  418. br.Success = true
  419. br.Ret = 200
  420. }