my_chart.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/utils"
  8. "time"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. type MyChartController struct {
  12. BaseAuthController
  13. }
  14. // @Title 收藏图表
  15. // @Description 收藏图表
  16. // @Param PageSize query int true "每页数据条数"
  17. // @Param CurrentIndex query int true "当前页页码,从1开始"
  18. // @Success 200 {object} models.BaseResponse
  19. // @Failure 403 {object} models.BaseResponse
  20. // @router /collect [post]
  21. func (this *MyChartController) Collect() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req request.MyChartCollectReq
  28. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  29. br.Msg = "参数解析失败"
  30. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  31. return
  32. }
  33. user := this.User
  34. if user.Status != 2 {
  35. br.Msg = "用户没有权限收藏"
  36. return
  37. }
  38. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  39. if err != nil {
  40. br.Msg = "收藏失败"
  41. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  42. return
  43. }
  44. if count > 0 {
  45. br.Msg = "该图表已收藏,请重新刷新页面"
  46. return
  47. }
  48. myChart := &models.MyChart{
  49. UserId: user.UserId,
  50. UserRealName: user.RealName,
  51. UniqueCode: req.UniqueCode,
  52. ChartImage: req.ChartImage,
  53. ChartName: req.ChartName,
  54. ChartInfoId: req.ChartInfoId,
  55. CreateTime: time.Now(),
  56. ModifyTime: time.Now(),
  57. }
  58. err = myChart.Insert()
  59. if err != nil {
  60. br.Msg = "收藏失败"
  61. br.ErrMsg = "收藏失败,Err:" + err.Error()
  62. return
  63. }
  64. br.Msg = "收藏成功"
  65. br.Success = true
  66. br.Ret = 200
  67. }
  68. // @Title 取消收藏
  69. // @Description 取消收藏
  70. // @Param PageSize query int true "每页数据条数"
  71. // @Param CurrentIndex query int true "当前页页码,从1开始"
  72. // @Success 200 {object} models.BaseResponse
  73. // @Failure 403 {object} models.BaseResponse
  74. // @router /collectCancel [post]
  75. func (this *MyChartController) CollectCancel() {
  76. br := new(models.BaseResponse).Init()
  77. defer func() {
  78. this.Data["json"] = br
  79. this.ServeJSON()
  80. }()
  81. var req request.MyChartCollectCancelReq
  82. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  83. br.Msg = "参数解析失败"
  84. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  85. return
  86. }
  87. user := this.User
  88. if user.Status != 2 {
  89. br.Msg = "用户没有权限收藏"
  90. return
  91. }
  92. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  93. if err != nil {
  94. br.Msg = "取消收藏失败"
  95. br.ErrMsg = "获取收藏信息失败,Err:" + err.Error()
  96. return
  97. }
  98. if count == 0 {
  99. br.Msg = "该图表已取消收藏,请重新刷新页面"
  100. return
  101. }
  102. err = models.DeleteMyChart(user.UserId, req.UniqueCode)
  103. if err != nil {
  104. br.Msg = "取消收藏失败"
  105. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  106. return
  107. }
  108. br.Msg = "取消收藏成功"
  109. br.Success = true
  110. br.Ret = 200
  111. }
  112. // @Title List
  113. // @Description create users
  114. // @Param PageSize query int true "每页数据条数"
  115. // @Param CurrentIndex query int true "当前页页码,从1开始"
  116. // @Success 200 {object} models.BaseResponse
  117. // @Failure 403 {object} models.BaseResponse
  118. // @router /list [get]
  119. func (this *MyChartController) List() {
  120. br := new(models.BaseResponse).Init()
  121. defer func() {
  122. this.Data["json"] = br
  123. this.ServeJSON()
  124. }()
  125. pageSize, _ := this.GetInt("PageSize")
  126. currentIndex, _ := this.GetInt("CurrentIndex")
  127. if pageSize <= 0 {
  128. pageSize = 30
  129. }
  130. if currentIndex <= 0 {
  131. currentIndex = 1
  132. }
  133. user := this.User
  134. if user.Status != 2 {
  135. br.Msg = "用户没有收藏权限"
  136. return
  137. }
  138. total, err := models.GetMyChartListCountById(user.UserId)
  139. if err != nil {
  140. br.Msg = "查询收藏数量失败"
  141. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  142. return
  143. }
  144. resp := new(response.MyChartListResp)
  145. if total == 0 {
  146. br.Data = resp
  147. br.Msg = "暂无数据"
  148. br.Ret = 200
  149. br.Success = true
  150. return
  151. }
  152. startSize := utils.StartIndex(currentIndex, pageSize)
  153. items, err := models.GetMyChartListById(user.UserId, startSize, pageSize)
  154. if err != nil {
  155. br.Msg = "查询收藏失败"
  156. br.ErrMsg = "查询收藏失败,Err:" + err.Error()
  157. return
  158. }
  159. page := paging.GetPaging(currentIndex, pageSize, total)
  160. resp.List = items
  161. resp.Paging = page
  162. br.Data = resp
  163. br.Msg = "查询成功"
  164. br.Success = true
  165. br.Ret = 200
  166. }
  167. // @Title Locate
  168. // @Description create users
  169. // @Param PageSize query int true "每页数据条数"
  170. // @Param CurrentIndex query int true "当前页页码,从1开始"
  171. // @Success 200 {object} models.BaseResponse
  172. // @Failure 403 {object} models.BaseResponse
  173. // @router /locate [get]
  174. func (this *MyChartController) Locate() {
  175. br := new(models.BaseResponse).Init()
  176. defer func() {
  177. this.Data["json"] = br
  178. this.ServeJSON()
  179. }()
  180. user := this.User
  181. total, err := models.GetMyChartListCountById(user.UserId)
  182. if err != nil {
  183. br.Msg = "查询收藏数量失败"
  184. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  185. return
  186. }
  187. charts := make([]*response.MyChartLocateItem, 0)
  188. if total == 0 {
  189. br.Data = charts
  190. br.Ret = 200
  191. br.Success = true
  192. br.Msg = "获取成功"
  193. return
  194. }
  195. items, err := models.GetMyChartListById(user.UserId, 0, total)
  196. if err != nil {
  197. br.Msg = "查询收藏失败"
  198. br.ErrMsg = "查询收藏失败,Err:" + err.Error()
  199. return
  200. }
  201. for k, v := range items {
  202. var prevChartInfoId, nextChartInfoId int
  203. switch k {
  204. case 0:
  205. prevChartInfoId = -1
  206. if k < total {
  207. nextChartInfoId = items[k+1].ChartInfoId
  208. }
  209. case total - 1:
  210. nextChartInfoId = -1
  211. if k > 0 {
  212. prevChartInfoId = items[k-1].ChartInfoId
  213. }
  214. default:
  215. prevChartInfoId = items[k-1].ChartInfoId
  216. nextChartInfoId = items[k+1].ChartInfoId
  217. }
  218. tmpChart := &response.MyChartLocateItem{
  219. MyChartId: v.MyChartId,
  220. ChartInfoId: v.ChartInfoId,
  221. ChartName: v.ChartName,
  222. UniqueCode: v.UniqueCode,
  223. PrevChartInfoId: prevChartInfoId,
  224. NextChartInfoId: nextChartInfoId,
  225. }
  226. charts = append(charts, tmpChart)
  227. }
  228. br.Data = charts
  229. br.Msg = "查询成功"
  230. br.Success = true
  231. br.Ret = 200
  232. }
  233. // @Title IsCollect
  234. // @Description create users
  235. // @Param PageSize query int true "每页数据条数"
  236. // @Param CurrentIndex query int true "当前页页码,从1开始"
  237. // @Success 200 {object} models.BaseResponse
  238. // @Failure 403 {object} models.BaseResponse
  239. // @router /isCollect [post]
  240. func (this *MyChartController) IsCollect() {
  241. br := new(models.BaseResponse).Init()
  242. defer func() {
  243. this.Data["json"] = br
  244. this.ServeJSON()
  245. }()
  246. var req request.MyChartIsCollectReq
  247. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  248. br.Msg = "参数解析失败"
  249. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  250. return
  251. }
  252. user := this.User
  253. if user.Status != 2 {
  254. br.Msg = "用户没有权限收藏"
  255. return
  256. }
  257. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  258. if err != nil {
  259. br.Msg = "查询收藏数量失败"
  260. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  261. return
  262. }
  263. resp := new(response.MyChartIsCollectResp)
  264. if count > 0 {
  265. resp.IsCollect = true
  266. } else {
  267. resp.IsCollect = false
  268. }
  269. br.Data = resp
  270. br.Msg = "查询成功"
  271. br.Success = true
  272. br.Ret = 200
  273. }