my_chart.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // @Success 200 {object} models.BaseResponse
  18. // @router /collect [post]
  19. func (this *MyChartController) Collect() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. var req request.MyChartCollectReq
  26. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  27. br.Msg = "参数解析失败"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. user := this.User
  32. if user.Status != utils.UserStatusFormal {
  33. br.Msg = "用户没有权限收藏"
  34. return
  35. }
  36. if req.ChartInfoId == 0 {
  37. chartResp, err := services.GetChartDetail(req.ChartInfoId, req.UniqueCode)
  38. if err != nil {
  39. br.Msg = "收藏失败"
  40. br.Msg = "获取图表信息失败,系统错误,Err:" + err.Error()
  41. return
  42. }
  43. if chartResp.Ret != 200 {
  44. br.Msg = chartResp.Msg
  45. br.ErrMsg = chartResp.ErrMsg
  46. return
  47. }
  48. req.ChartInfoId = chartResp.Data.ChartInfoId
  49. }
  50. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  51. if err != nil {
  52. br.Msg = "收藏失败"
  53. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  54. return
  55. }
  56. if count > 0 {
  57. br.Msg = "该图表已收藏,请重新刷新页面"
  58. return
  59. }
  60. myChart := &models.MyChart{
  61. UserId: user.UserId,
  62. UserRealName: user.RealName,
  63. UniqueCode: req.UniqueCode,
  64. ChartImage: req.ChartImage,
  65. ChartName: req.ChartName,
  66. ChartInfoId: req.ChartInfoId,
  67. CreateTime: time.Now(),
  68. ModifyTime: time.Now(),
  69. }
  70. err = myChart.Insert()
  71. if err != nil {
  72. br.Msg = "收藏失败"
  73. br.ErrMsg = "收藏失败,Err:" + err.Error()
  74. return
  75. }
  76. br.Msg = "收藏成功"
  77. br.Success = true
  78. br.Ret = 200
  79. }
  80. // @Title 取消收藏
  81. // @Description 取消收藏
  82. // @Success 200 {object} models.BaseResponse
  83. // @router /collectCancel [post]
  84. func (this *MyChartController) CollectCancel() {
  85. br := new(models.BaseResponse).Init()
  86. defer func() {
  87. this.Data["json"] = br
  88. this.ServeJSON()
  89. }()
  90. var req request.MyChartCollectCancelReq
  91. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  92. br.Msg = "参数解析失败"
  93. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  94. return
  95. }
  96. user := this.User
  97. if user.Status != utils.UserStatusFormal {
  98. br.Msg = "用户没有权限收藏"
  99. return
  100. }
  101. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  102. if err != nil {
  103. br.Msg = "取消收藏失败"
  104. br.ErrMsg = "获取收藏信息失败,Err:" + err.Error()
  105. return
  106. }
  107. if count == 0 {
  108. br.Msg = "该图表已取消收藏,请重新刷新页面"
  109. return
  110. }
  111. err = models.DeleteMyChart(user.UserId, req.UniqueCode)
  112. if err != nil {
  113. br.Msg = "取消收藏失败"
  114. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  115. return
  116. }
  117. br.Msg = "取消收藏成功"
  118. br.Success = true
  119. br.Ret = 200
  120. }
  121. // @Title List
  122. // @Description create users
  123. // @Param PageSize query int true "每页数据条数"
  124. // @Param CurrentIndex query int true "当前页页码,从1开始"
  125. // @Success 200 {object} models.BaseResponse
  126. // @router /list [get]
  127. func (this *MyChartController) List() {
  128. br := new(models.BaseResponse).Init()
  129. defer func() {
  130. this.Data["json"] = br
  131. this.ServeJSON()
  132. }()
  133. pageSize, _ := this.GetInt("PageSize")
  134. currentIndex, _ := this.GetInt("CurrentIndex")
  135. if pageSize <= 0 {
  136. pageSize = 30
  137. }
  138. if currentIndex <= 0 {
  139. currentIndex = 1
  140. }
  141. user := this.User
  142. if user.Status != utils.UserStatusFormal {
  143. br.Msg = "用户没有收藏权限"
  144. return
  145. }
  146. total, err := models.GetMyChartListCountById(user.UserId)
  147. if err != nil {
  148. br.Msg = "查询收藏数量失败"
  149. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  150. return
  151. }
  152. resp := new(response.MyChartListResp)
  153. startSize := utils.StartIndex(currentIndex, pageSize)
  154. items, err := models.GetMyChartListById(user.UserId, startSize, pageSize)
  155. if err != nil {
  156. br.Msg = "查询收藏失败"
  157. br.ErrMsg = "查询收藏失败,Err:" + err.Error()
  158. return
  159. }
  160. page := paging.GetPaging(currentIndex, pageSize, total)
  161. resp.List = items
  162. resp.Paging = page
  163. br.Data = resp
  164. br.Msg = "查询成功"
  165. br.Success = true
  166. br.Ret = 200
  167. }
  168. // @Title Locate
  169. // @Description create users
  170. // @Param PageSize query int true "每页数据条数"
  171. // @Param CurrentIndex query int true "当前页页码,从1开始"
  172. // @Success 200 {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. // @router /detail [get]
  233. func (this *MyChartController) Detail() {
  234. br := new(models.BaseResponse).Init()
  235. defer func() {
  236. this.Data["json"] = br
  237. this.ServeJSON()
  238. }()
  239. user := this.User
  240. chartInfoId, _ := this.GetInt("ChartInfoId")
  241. if chartInfoId <= 0 {
  242. br.Msg = "图表id错误"
  243. return
  244. }
  245. result, err := services.GetChartDetail(chartInfoId, "")
  246. if err != nil {
  247. br.Msg = "获取图表详情失败"
  248. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  249. return
  250. }
  251. if result.Ret != 200 {
  252. br.Msg = result.Msg
  253. br.ErrMsg = result.ErrMsg
  254. return
  255. }
  256. if result.Ret == 200 && result.Data.UniqueCode == "" {
  257. // 说明后台删除了这个图表,那么尝试将收藏的图表也删除
  258. models.DeleteMyChartByUserIdAndChartInfoId(user.UserId, chartInfoId)
  259. br.Msg = "图表已删除或不存在,请刷新页面"
  260. return
  261. }
  262. count, err := models.GetMyChartCount(user.UserId, result.Data.UniqueCode)
  263. if err != nil {
  264. br.Msg = "图表已删除或不存在"
  265. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  266. }
  267. if count > 0 {
  268. result.Data.IsCollect = true
  269. }
  270. br.Data = result.Data
  271. br.Msg = "查询成功"
  272. br.Success = true
  273. br.Ret = 200
  274. }
  275. // @Title IsCollect
  276. // @Description create users
  277. // @Success 200 {object} models.BaseResponse
  278. // @router /isCollect [post]
  279. func (this *MyChartController) IsCollect() {
  280. br := new(models.BaseResponse).Init()
  281. defer func() {
  282. this.Data["json"] = br
  283. this.ServeJSON()
  284. }()
  285. var req request.MyChartIsCollectReq
  286. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  287. br.Msg = "参数解析失败"
  288. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  289. return
  290. }
  291. user := this.User
  292. count, err := models.GetMyChartCount(user.UserId, req.UniqueCode)
  293. if err != nil {
  294. br.Msg = "查询收藏数量失败"
  295. br.ErrMsg = "查询收藏数量失败,Err:" + err.Error()
  296. return
  297. }
  298. resp := new(response.MyChartIsCollectResp)
  299. if count > 0 {
  300. resp.IsCollect = true
  301. } else {
  302. resp.IsCollect = false
  303. }
  304. br.Data = resp
  305. br.Msg = "查询成功"
  306. br.Success = true
  307. br.Ret = 200
  308. }