my_chart.go 8.3 KB

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