chart.go 4.3 KB

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