my_report.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 != 2 {
  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. if total == 0 {
  52. br.Data = resp
  53. br.Msg = "暂无数据"
  54. br.Ret = 200
  55. br.Success = true
  56. return
  57. }
  58. startSize := utils.StartIndex(currentIndex, pageSize)
  59. reportList, err := models.GetMyReportListByUserId(user.UserId, startSize, pageSize)
  60. if err != nil {
  61. br.Msg = "获取我的报告列表失败"
  62. br.ErrMsg = "获取我的报告列表失败,系统异常,Err:" + err.Error()
  63. return
  64. }
  65. page := paging.GetPaging(currentIndex, pageSize, total)
  66. resp.List = reportList
  67. resp.Page = page
  68. br.Data = resp
  69. br.Success = true
  70. br.Msg = "获取我的报告列表成功"
  71. br.Ret = 200
  72. }
  73. // @Title 收藏研报
  74. // @Description 收藏研报
  75. // @Success 200 {object} models.BaseResponse
  76. // @Failure 403 {object} models.BaseResponse
  77. // @router /collect [post]
  78. func (this *MyReportController) Collect() {
  79. br := new(models.BaseResponse).Init()
  80. defer func() {
  81. this.Data["json"] = br
  82. this.ServeJSON()
  83. }()
  84. var req request.MyReportCollectReq
  85. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  86. br.Msg = "参数解析失败"
  87. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  88. return
  89. }
  90. user := this.User
  91. if user.Status != 2 {
  92. br.Msg = "用户没有权限收藏"
  93. return
  94. }
  95. count, err := models.GetMyReportByUserIdAndReportId(user.UserId, req.ReportId)
  96. if err != nil {
  97. br.Msg = "收藏失败"
  98. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  99. return
  100. }
  101. if count > 0 {
  102. br.Msg = "该研报已收藏,请重新刷新页面"
  103. return
  104. }
  105. reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
  106. if err != nil {
  107. br.Msg = "收藏失败"
  108. br.ErrMsg = "获取研报详情失败,Err:" + err.Error()
  109. }
  110. if reportResp.Ret != 200 {
  111. br.Msg = "收藏失败"
  112. br.ErrMsg = "获取研报详情失败,Err:" + reportResp.ErrMsg
  113. return
  114. }
  115. report := reportResp.Data.Report
  116. publishTime, err := time.Parse(utils.FormatDateTime, report.PublishTime)
  117. if err != nil {
  118. br.Msg = "时间格式不对"
  119. return
  120. }
  121. myChart := &models.MyReport{
  122. UserId: user.UserId,
  123. ReportId: req.ReportId,
  124. Title: report.Title,
  125. Abstract: report.Abstract,
  126. Author: report.Author,
  127. PublishTime: publishTime,
  128. Stage: report.Stage,
  129. CreateTime: time.Now(),
  130. }
  131. err = myChart.Insert()
  132. if err != nil {
  133. br.Msg = "收藏失败"
  134. br.ErrMsg = "收藏失败,Err:" + err.Error()
  135. return
  136. }
  137. br.Msg = "收藏成功"
  138. br.Success = true
  139. br.Ret = 200
  140. }
  141. // @Title 取消收藏
  142. // @Description 取消收藏
  143. // @Param PageSize query int true "每页数据条数"
  144. // @Param CurrentIndex query int true "当前页页码,从1开始"
  145. // @Success 200 {object} models.BaseResponse
  146. // @Failure 403 {object} models.BaseResponse
  147. // @router /collectCancel [post]
  148. func (this *MyReportController) CollectCancel() {
  149. br := new(models.BaseResponse).Init()
  150. defer func() {
  151. this.Data["json"] = br
  152. this.ServeJSON()
  153. }()
  154. var req request.MyReportCollectReq
  155. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  156. br.Msg = "参数解析失败"
  157. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  158. return
  159. }
  160. user := this.User
  161. if user.Status != 2 {
  162. br.Msg = "用户没有权限收藏"
  163. return
  164. }
  165. count, err := models.GetMyReportByUserIdAndReportId(user.UserId, req.ReportId)
  166. if err != nil {
  167. br.Msg = "取消收藏失败"
  168. br.ErrMsg = "获取收藏信息失败,Err:" + err.Error()
  169. return
  170. }
  171. if count == 0 {
  172. br.Msg = "该研报已取消收藏,请重新刷新页面"
  173. return
  174. }
  175. err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportId)
  176. if err != nil {
  177. br.Msg = "取消收藏失败"
  178. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  179. return
  180. }
  181. br.Msg = "取消收藏成功"
  182. br.Success = true
  183. br.Ret = 200
  184. }