my_report.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)
  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. }
  133. err = myChart.Insert()
  134. if err != nil {
  135. br.Msg = "收藏失败"
  136. br.ErrMsg = "收藏失败,Err:" + err.Error()
  137. return
  138. }
  139. br.Msg = "收藏成功"
  140. br.Success = true
  141. br.Ret = 200
  142. }
  143. // @Title 取消收藏
  144. // @Description 取消收藏
  145. // @Param PageSize query int true "每页数据条数"
  146. // @Param CurrentIndex query int true "当前页页码,从1开始"
  147. // @Success 200 {object} models.BaseResponse
  148. // @Failure 403 {object} models.BaseResponse
  149. // @router /collectCancel [post]
  150. func (this *MyReportController) CollectCancel() {
  151. br := new(models.BaseResponse).Init()
  152. defer func() {
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. var req request.MyReportCollectReq
  157. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  158. br.Msg = "参数解析失败"
  159. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  160. return
  161. }
  162. user := this.User
  163. if user.Status != utils.UserStatusFormal {
  164. br.Msg = "用户没有权限收藏"
  165. return
  166. }
  167. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId)
  168. if err != nil {
  169. br.Msg = "取消收藏失败"
  170. br.ErrMsg = "获取收藏信息失败,Err:" + err.Error()
  171. return
  172. }
  173. if count == 0 {
  174. br.Msg = "该研报已取消收藏,请重新刷新页面"
  175. return
  176. }
  177. err = models.DeleteMyReportByUserIdAndReportId(user.UserId, req.ReportId)
  178. if err != nil {
  179. br.Msg = "取消收藏失败"
  180. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  181. return
  182. }
  183. br.Msg = "取消收藏成功"
  184. br.Success = true
  185. br.Ret = 200
  186. }
  187. // @Title 是否收藏研报
  188. // @Description 是否收藏研报
  189. // @Param request body request.MyReportCollectReq true "type json string"
  190. // @Success 200 {object} models.BaseResponse
  191. // @router /isCollect [post]
  192. func (this *MyReportController) IsCollect() {
  193. br := new(models.BaseResponse).Init()
  194. defer func() {
  195. this.Data["json"] = br
  196. this.ServeJSON()
  197. }()
  198. var req request.MyReportCollectReq
  199. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  200. br.Msg = "参数解析失败"
  201. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  202. return
  203. }
  204. user := this.User
  205. if user.Status != utils.UserStatusFormal {
  206. br.Msg = "用户没有权限收藏"
  207. return
  208. }
  209. count, err := models.GetMyReportCountByUserIdAndReportId(user.UserId, req.ReportId)
  210. if err != nil {
  211. br.Msg = "收藏失败"
  212. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  213. return
  214. }
  215. resp := new(response.MyReportIsCollectResp)
  216. if count > 0 {
  217. resp.IsCollect = true
  218. } else {
  219. resp.IsCollect = false
  220. }
  221. br.Data = resp
  222. br.Msg = "收藏成功"
  223. br.Success = true
  224. br.Ret = 200
  225. }