chart.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/services"
  7. "hongze/hongze_cygx/utils"
  8. "time"
  9. )
  10. type ChartController struct {
  11. BaseAuthController
  12. }
  13. type BaseChartController struct {
  14. BaseCommonController
  15. }
  16. // @Title 图表标签分类
  17. // @Description 图表标签分类接口
  18. // @Success 200 {object} models.ChartPtagResp
  19. // @router /patg [get]
  20. func (this *BaseChartController) Patg() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. list, err := services.GetChartPtagByApi()
  27. if err != nil {
  28. br.Msg = "获取图表分类失败!"
  29. br.ErrMsg = "获取图表分类失败"
  30. return
  31. }
  32. br.Ret = 200
  33. br.Success = true
  34. br.Msg = "获取成功"
  35. br.Data = list
  36. }
  37. // @Title 我的收藏列表
  38. // @Description 我的收藏列表接口
  39. // @Param PageSize query int true "每页数据条数"
  40. // @Param CurrentIndex query int true "当前页页码,从1开始"
  41. // @Success 200 {object} models.HomeChartListItem
  42. // @router /my/collection [get]
  43. func (this *ChartController) Collection() {
  44. br := new(models.BaseResponse).Init()
  45. defer func() {
  46. this.Data["json"] = br
  47. this.ServeJSON()
  48. }()
  49. user := this.User
  50. if user == nil {
  51. br.Msg = "请重新登录"
  52. br.Ret = 408
  53. return
  54. }
  55. mobile := user.Mobile
  56. if mobile == "" {
  57. br.Msg = "请绑定手机号!"
  58. return
  59. }
  60. pageSize, _ := this.GetInt("PageSize")
  61. currentIndex, _ := this.GetInt("CurrentIndex")
  62. if pageSize <= 0 {
  63. pageSize = utils.PageSize20
  64. }
  65. if currentIndex <= 0 {
  66. currentIndex = 1
  67. pageSize = 0
  68. }
  69. list, err, total := services.GetChartCollectionByApi(mobile, pageSize, (currentIndex-1)*pageSize)
  70. if err != nil {
  71. br.Msg = "获取图表分类失败!"
  72. br.ErrMsg = "获取图表分类失败"
  73. return
  74. }
  75. page := paging.GetPaging(currentIndex, pageSize, total)
  76. resp := new(models.HomeChartListItem)
  77. resp.List = list
  78. resp.Paging = page
  79. br.Ret = 200
  80. br.Success = true
  81. br.Msg = "获取成功"
  82. br.Data = resp
  83. }
  84. // @Title 图表详情
  85. // @Description 我的收藏接口
  86. // @Param ChartId query int true "图表ID"
  87. // @Success 200 {object} models.CygxChartDetail
  88. // @router /detail [get]
  89. func (this *ChartController) Detail() {
  90. br := new(models.BaseResponse).Init()
  91. defer func() {
  92. this.Data["json"] = br
  93. this.ServeJSON()
  94. }()
  95. user := this.User
  96. if user == nil {
  97. br.Msg = "请重新登录"
  98. br.Ret = 408
  99. return
  100. }
  101. mobile := user.Mobile
  102. if mobile == "" {
  103. br.Msg = "请绑定手机号!"
  104. return
  105. }
  106. chartId, _ := this.GetInt("ChartId")
  107. if chartId <= 0 {
  108. br.Msg = "图表信息不存在!"
  109. return
  110. }
  111. detail, err := models.GetChartDetailById(chartId, user.UserId)
  112. if err != nil {
  113. br.Msg = "获取信息失败"
  114. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  115. return
  116. }
  117. if detail.CollectionNum > 0 {
  118. detail.IsCollection = true
  119. }
  120. br.Ret = 200
  121. br.Success = true
  122. br.Msg = "获取成功"
  123. br.Data = detail
  124. }
  125. // @Title 收藏、取消收藏
  126. // @Description 收藏、取消收藏
  127. // @Param request body models.ChartCollectReq true "type json string"
  128. // @Success 200 {object} models.FontsCollectResp
  129. // @router /collect [post]
  130. func (this *ChartController) ChartCollect() {
  131. br := new(models.BaseResponse).Init()
  132. defer func() {
  133. this.Data["json"] = br
  134. this.ServeJSON()
  135. }()
  136. user := this.User
  137. if user == nil {
  138. br.Msg = "请重新登录"
  139. br.Ret = 408
  140. return
  141. }
  142. uid := user.UserId
  143. var req models.ChartCollectReq
  144. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  145. if err != nil {
  146. br.Msg = "参数解析异常!"
  147. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  148. return
  149. }
  150. count, err := models.GetChartCountByUserId(uid, req.ChartId)
  151. if err != nil {
  152. br.Msg = "获取数据失败!"
  153. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  154. return
  155. }
  156. resp := new(models.ArticleCollectResp)
  157. if count <= 0 {
  158. item := new(models.CygxChartCollect)
  159. item.ChartId = req.ChartId
  160. item.UserId = uid
  161. item.RealName = user.RealName
  162. item.CreateTime = time.Now()
  163. item.Mobile = user.Mobile
  164. item.Email = user.Email
  165. item.CompanyId = user.CompanyId
  166. item.CompanyName = user.CompanyName
  167. _, err = models.AddCygxChartCollect(item)
  168. if err != nil {
  169. br.Msg = "收藏失败"
  170. br.ErrMsg = "收藏失败,Err:" + err.Error()
  171. return
  172. }
  173. br.Msg = "收藏成功"
  174. resp.Status = 1
  175. } else {
  176. err = models.RemoveChartCollect(uid, req.ChartId)
  177. if err != nil {
  178. br.Msg = "取消收藏失败"
  179. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  180. return
  181. }
  182. br.Msg = "已取消收藏"
  183. resp.Status = 2
  184. }
  185. br.Ret = 200
  186. br.Success = true
  187. br.Data = resp
  188. }
  189. // @Title 置顶、取消置顶
  190. // @Description 置顶、取消置顶
  191. // @Param request body models.ChartCollectReq true "type json string"
  192. // @Success 200 {object} models.FontsCollectResp
  193. // @router /top [post]
  194. func (this *ChartController) ChartTop() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. this.Data["json"] = br
  198. this.ServeJSON()
  199. }()
  200. user := this.User
  201. if user == nil {
  202. br.Msg = "请重新登录"
  203. br.Ret = 408
  204. return
  205. }
  206. uid := user.UserId
  207. var req models.ChartCollectReq
  208. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  209. if err != nil {
  210. br.Msg = "参数解析异常!"
  211. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  212. return
  213. }
  214. count, err := models.GetChartTopCountByUserId(uid, req.ChartId)
  215. if err != nil {
  216. br.Msg = "获取数据失败!"
  217. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  218. return
  219. }
  220. resp := new(models.ArticleCollectResp)
  221. if count <= 0 {
  222. item := new(models.CygxChartTop)
  223. item.ChartId = req.ChartId
  224. item.UserId = uid
  225. item.RealName = user.RealName
  226. item.CreateTime = time.Now()
  227. item.Mobile = user.Mobile
  228. item.Email = user.Email
  229. item.CompanyId = user.CompanyId
  230. item.CompanyName = user.CompanyName
  231. _, err = models.AddCygxChartTop(item)
  232. if err != nil {
  233. br.Msg = "置顶失败"
  234. br.ErrMsg = "收置顶失败,Err:" + err.Error()
  235. return
  236. }
  237. br.Msg = "收藏成功"
  238. resp.Status = 1
  239. } else {
  240. err = models.RemoveChartTop(uid, req.ChartId)
  241. if err != nil {
  242. br.Msg = "取消置顶失败"
  243. br.ErrMsg = "取消置顶失败,Err:" + err.Error()
  244. return
  245. }
  246. br.Msg = "已取消置顶"
  247. resp.Status = 2
  248. }
  249. br.Ret = 200
  250. br.Success = true
  251. br.Data = resp
  252. }