chart.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/response"
  6. "eta/eta_mini_api/services"
  7. "eta/eta_mini_api/services/alarm_msg"
  8. "eta/eta_mini_api/utils"
  9. "fmt"
  10. )
  11. type ChartController struct {
  12. BaseAuthController
  13. }
  14. // @Title List
  15. // @Description create users
  16. // @Param PageSize query int true "每页数据条数"
  17. // @Param CurrentIndex query int true "当前页页码,从1开始"
  18. // @Success 200 {object} models.BaseResponse
  19. // @router /list [get]
  20. func (this *ChartController) List() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. pageSize, _ := this.GetInt("PageSize")
  27. currentIndex, _ := this.GetInt("CurrentIndex")
  28. result, err := services.GetChartList(currentIndex, pageSize)
  29. if err != nil {
  30. br.Msg = "查询图表失败"
  31. br.ErrMsg = "查询图表失败,系统异常,Err:" + err.Error()
  32. return
  33. }
  34. if result.Ret != 200 {
  35. br.Msg = "查询图表失败"
  36. br.ErrMsg = result.ErrMsg
  37. return
  38. }
  39. resp := new(response.ChartListResp)
  40. resp.List = result.Data.List
  41. resp.Paging = result.Data.Paging
  42. br.Msg = "查询图表成功"
  43. br.Data = result.Data
  44. br.Success = true
  45. br.Ret = 200
  46. }
  47. // @Title Detail
  48. // @Description 图表详情
  49. // @Param ChartInfoId query int true "图表详情id"
  50. // @Success 200 {object} models.BaseResponse
  51. // @router /detail [get]
  52. func (this *ChartController) Detail() {
  53. br := new(models.BaseResponse).Init()
  54. defer func() {
  55. if br.Ret != 200 {
  56. errb, _ := json.Marshal(br)
  57. alarm_msg.SendAlarmMsg(fmt.Sprintf("获得图表详情失败:%s, user:%d", string(errb), this.User.UserId), 1)
  58. }
  59. this.Data["json"] = br
  60. this.ServeJSON()
  61. }()
  62. user := this.User
  63. if user.Status != utils.UserStatusFormal {
  64. br.Msg = "用户没有权限查看图表"
  65. return
  66. }
  67. chartInfoId, _ := this.GetInt("ChartInfoId")
  68. if chartInfoId <= 0 {
  69. br.Msg = "图表id错误"
  70. return
  71. }
  72. result, err := services.GetChartDetail(chartInfoId, "")
  73. if err != nil {
  74. br.Msg = "获取图表详情失败"
  75. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  76. return
  77. }
  78. if result.Ret == 200 && result.Data.UniqueCode == "" {
  79. // 说明后台删除了这个图表,那么尝试将收藏的图表也删除
  80. alarm_msg.SendAlarmMsg(fmt.Sprintf("图表不存在,删除图表,id:%d", chartInfoId), 1)
  81. models.DeleteMyChartByUserIdAndChartInfoId(user.UserId, chartInfoId)
  82. br.Msg = "图表已删除或不存在"
  83. return
  84. }
  85. count, err := models.GetMyChartCount(user.UserId, result.Data.UniqueCode)
  86. if err != nil {
  87. br.Msg = "获取图表详情失败"
  88. br.ErrMsg = "获取图表详情失败,Err:" + err.Error()
  89. }
  90. if count > 0 {
  91. result.Data.IsCollect = true
  92. }
  93. br.Data = result.Data
  94. br.Msg = "查询成功"
  95. br.Success = true
  96. br.Ret = 200
  97. }
  98. // @Title Locate
  99. // @Description create users
  100. // @Param PageSize query int true "每页数据条数"
  101. // @Param CurrentIndex query int true "当前页页码,从1开始"
  102. // @Success 200 {object} models.BaseResponse
  103. // @router /locate [get]
  104. func (this *ChartController) Locate() {
  105. br := new(models.BaseResponse).Init()
  106. defer func() {
  107. this.Data["json"] = br
  108. this.ServeJSON()
  109. }()
  110. pageSize, _ := this.GetInt("PageSize")
  111. currentIndex, _ := this.GetInt("CurrentIndex")
  112. if currentIndex <= 0 {
  113. currentIndex = 1
  114. }
  115. if pageSize <= 0 {
  116. pageSize = 2
  117. }
  118. result, err := services.GetChartList(currentIndex, pageSize)
  119. if err != nil {
  120. br.Msg = "查询图表失败"
  121. br.ErrMsg = "查询图表失败,系统异常,Err:" + err.Error()
  122. return
  123. }
  124. if result.Ret != 200 {
  125. br.Msg = result.Msg
  126. br.ErrMsg = result.ErrMsg
  127. return
  128. }
  129. total := len(result.Data.List)
  130. charts := make([]*response.ChartLocateItem, 0)
  131. items := result.Data.List
  132. for k, v := range items {
  133. var prevChartInfoId, nextChartInfoId int
  134. switch k {
  135. case 0:
  136. prevChartInfoId = -1
  137. if k < total-1 {
  138. nextChartInfoId = items[k+1].ChartInfoId
  139. } else {
  140. nextChartInfoId = -1
  141. }
  142. case total - 1:
  143. nextChartInfoId = -1
  144. if k > 0 {
  145. prevChartInfoId = items[k-1].ChartInfoId
  146. }
  147. default:
  148. prevChartInfoId = items[k-1].ChartInfoId
  149. nextChartInfoId = items[k+1].ChartInfoId
  150. }
  151. tmpLocate := &response.ChartLocateItem{
  152. ChartInfoId: v.ChartInfoId,
  153. ChartName: v.ChartName,
  154. UniqueCode: v.UniqueCode,
  155. PrevChartInfoId: prevChartInfoId,
  156. NextChartInfoId: nextChartInfoId,
  157. }
  158. charts = append(charts, tmpLocate)
  159. }
  160. br.Data = charts
  161. br.Msg = "查询成功"
  162. br.Success = true
  163. br.Ret = 200
  164. }