my_chart.go 8.4 KB

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