chart.go 4.4 KB

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