my_chart.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. if total == 0 {
  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. items, err := models.GetMyChartListById(user.UserId, 0, total)
  189. if err != nil {
  190. br.Msg = "查询收藏失败"
  191. br.ErrMsg = "查询收藏失败,Err:" + err.Error()
  192. return
  193. }
  194. for k, v := range items {
  195. var prevChartInfoId, nextChartInfoId int
  196. switch k {
  197. case 0:
  198. prevChartInfoId = -1
  199. if k < total-1 {
  200. nextChartInfoId = items[k+1].ChartInfoId
  201. } else {
  202. nextChartInfoId = -1
  203. }
  204. case total - 1:
  205. nextChartInfoId = -1
  206. if k > 0 {
  207. prevChartInfoId = items[k-1].ChartInfoId
  208. }
  209. default:
  210. prevChartInfoId = items[k-1].ChartInfoId
  211. nextChartInfoId = items[k+1].ChartInfoId
  212. }
  213. tmpChart := &response.MyChartLocateItem{
  214. MyChartId: v.MyChartId,
  215. ChartInfoId: v.ChartInfoId,
  216. ChartName: v.ChartName,
  217. UniqueCode: v.UniqueCode,
  218. PrevChartInfoId: prevChartInfoId,
  219. NextChartInfoId: nextChartInfoId,
  220. }
  221. charts = append(charts, tmpChart)
  222. }
  223. br.Data = charts
  224. br.Msg = "查询成功"
  225. br.Success = true
  226. br.Ret = 200
  227. }
  228. // @Title Detail
  229. // @Description 图表详情
  230. // @Param ChartInfoId query int true "图表详情id"
  231. // @Success 200 {object} models.BaseResponse
  232. // @Failure 403 {object} models.BaseResponse
  233. // @router /detail [get]
  234. func (this *MyChartController) Detail() {
  235. br := new(models.BaseResponse).Init()
  236. defer func() {
  237. this.Data["json"] = br
  238. this.ServeJSON()
  239. }()
  240. user := this.User
  241. chartInfoId, _ := this.GetInt("ChartInfoId")
  242. if chartInfoId <= 0 {
  243. br.Msg = "图表id错误"
  244. return
  245. }
  246. result, err := services.GetChartDetail(chartInfoId)
  247. if err != nil {
  248. br.Msg = "获取图表详情失败"
  249. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  250. return
  251. }
  252. if result.Ret != 200 {
  253. br.Msg = result.Msg
  254. br.ErrMsg = result.ErrMsg
  255. return
  256. }
  257. count, err := models.GetMyChartCount(user.UserId, result.Data.UniqueCode)
  258. if err != nil {
  259. br.Msg = "获取图表详情失败"
  260. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  261. }
  262. if count > 0 {
  263. result.Data.IsCollect = true
  264. }
  265. br.Data = result.Data
  266. br.Msg = "查询成功"
  267. br.Success = true
  268. br.Ret = 200
  269. }
  270. // @Title IsCollect
  271. // @Description create users
  272. // @Param PageSize query int true "每页数据条数"
  273. // @Param CurrentIndex query int true "当前页页码,从1开始"
  274. // @Success 200 {object} models.BaseResponse
  275. // @Failure 403 {object} models.BaseResponse
  276. // @router /isCollect [post]
  277. func (this *MyChartController) IsCollect() {
  278. br := new(models.BaseResponse).Init()
  279. defer func() {
  280. this.Data["json"] = br
  281. this.ServeJSON()
  282. }()
  283. var req request.MyChartIsCollectReq
  284. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  285. br.Msg = "参数解析失败"
  286. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  287. return
  288. }
  289. user := this.User
  290. if user.Status != 2 {
  291. br.Msg = "用户没有权限收藏"
  292. return
  293. }
  294. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  295. if err != nil {
  296. br.Msg = "查询收藏数量失败"
  297. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  298. return
  299. }
  300. resp := new(response.MyChartIsCollectResp)
  301. if count > 0 {
  302. resp.IsCollect = true
  303. } else {
  304. resp.IsCollect = false
  305. }
  306. br.Data = resp
  307. br.Msg = "查询成功"
  308. br.Success = true
  309. br.Ret = 200
  310. }