chart_info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_hub/models"
  5. "eta/eta_forum_hub/services"
  6. "eta/eta_forum_hub/utils"
  7. "strings"
  8. "time"
  9. )
  10. type ChartInfoController struct {
  11. BaseAuthController
  12. }
  13. // Update
  14. // @Title 图表-更新
  15. // @Description 图表-更新
  16. // @Param request body data_manage.AddChartReq true "type json string"
  17. // @Success 200 {object} data_manage.AddChartInfoResp
  18. // @router /update [post]
  19. func (this *ChartInfoController) Update() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. /*deleteCache := true
  26. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  27. defer func() {
  28. if deleteCache {
  29. utils.Rc.Delete(cacheKey)
  30. }
  31. }()
  32. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  33. deleteCache = false
  34. br.Msg = "系统处理中,请稍后重试!"
  35. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  36. return
  37. }*/
  38. var req models.AddChartReq
  39. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  40. if err != nil {
  41. br.Msg = "参数解析异常!"
  42. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  43. return
  44. }
  45. if req.ChartInfo == nil {
  46. br.Msg = "请选择图表"
  47. return
  48. }
  49. if req.ChartInfo.ChartInfoId <= 0 {
  50. br.Msg = "请选择图表"
  51. return
  52. }
  53. //校验图表名称是否重复
  54. req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ")
  55. if req.ChartInfo.ChartName == "" {
  56. br.Msg = "请填写图表名称!"
  57. return
  58. }
  59. if len(req.EdbInfoList) <= 0 {
  60. br.Msg = "指标信息不能为空"
  61. return
  62. }
  63. for _, edbInfo := range req.EdbInfoList {
  64. if edbInfo.EdbName == "" {
  65. br.Msg = "指标名称不能为空"
  66. return
  67. }
  68. }
  69. if len(req.ChartEdbMapping) <= 0 {
  70. br.Msg = "图表指标映射不能为空"
  71. return
  72. }
  73. chartInfo, err, errMsg, _ := services.UpdateChartInfoAndEdbInfo(&req, 0, "")
  74. if err != nil {
  75. br.Msg = errMsg
  76. br.ErrMsg = err.Error()
  77. return
  78. }
  79. br.Ret = 200
  80. br.Success = true
  81. br.Msg = "保存成功"
  82. br.Data = models.AddChartInfoResp{
  83. ChartInfoId: chartInfo.ChartInfoId,
  84. UniqueCode: chartInfo.UniqueCode,
  85. ChartType: chartInfo.ChartType,
  86. }
  87. return
  88. }
  89. // Save
  90. // @Title 图表-保存
  91. // @Description 图表-保存
  92. // @Param request body data_manage.AddChartReq true "type json string"
  93. // @Success 200 {object} data_manage.AddChartInfoResp
  94. // @router /save [post]
  95. func (this *ChartInfoController) Save() {
  96. br := new(models.BaseResponse).Init()
  97. defer func() {
  98. this.Data["json"] = br
  99. this.ServeJSON()
  100. }()
  101. var req models.AddChartReq
  102. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  103. if err != nil {
  104. br.Msg = "参数解析异常!"
  105. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  106. return
  107. }
  108. if req.ChartInfo == nil {
  109. br.Msg = "请选择图表"
  110. return
  111. }
  112. //校验图表名称是否重复
  113. req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ")
  114. if req.ChartInfo.ChartName == "" {
  115. br.Msg = "请填写图表名称!"
  116. return
  117. }
  118. if len(req.EdbInfoList) <= 0 {
  119. br.Msg = "指标信息不能为空"
  120. return
  121. }
  122. for _, edbInfo := range req.EdbInfoList {
  123. if edbInfo.EdbName == "" {
  124. br.Msg = "指标名称不能为空"
  125. return
  126. }
  127. }
  128. if len(req.ChartEdbMapping) <= 0 {
  129. br.Msg = "图表指标映射不能为空"
  130. return
  131. }
  132. deleteCache := true
  133. cacheKey := "eta_forum_hub:CACHE_CHART_INFO_ADD:" + req.ChartInfo.ChartName
  134. defer func() {
  135. if deleteCache {
  136. utils.Rc.Delete(cacheKey)
  137. }
  138. }()
  139. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  140. deleteCache = false
  141. br.Msg = "系统处理中,请稍后重试!"
  142. br.ErrMsg = "系统处理中,请稍后重试!;data:" + string(this.Ctx.Input.RequestBody)
  143. return
  144. }
  145. chartInfo, err, errMsg, _ := services.AddChartInfo(&req, 0, "")
  146. if err != nil {
  147. br.Msg = errMsg
  148. br.ErrMsg = err.Error()
  149. return
  150. }
  151. br.Ret = 200
  152. br.Success = true
  153. br.Msg = "保存成功"
  154. br.Data = models.AddChartInfoResp{
  155. ChartInfoId: chartInfo.ChartInfoId,
  156. UniqueCode: chartInfo.UniqueCode,
  157. ChartType: chartInfo.ChartType,
  158. }
  159. return
  160. }
  161. // Delete
  162. // @Title 图表-删除
  163. // @Description 图表-删除
  164. // @Param request body data_manage.DeleteChartReq true "type json string"
  165. // @Success 200 {object}
  166. // @router /delete [post]
  167. func (this *ChartInfoController) Delete() {
  168. br := new(models.BaseResponse).Init()
  169. defer func() {
  170. this.Data["json"] = br
  171. this.ServeJSON()
  172. }()
  173. var req models.DeleteChartReq
  174. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  175. if err != nil {
  176. br.Msg = "参数解析异常!"
  177. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  178. return
  179. }
  180. if req.ChartInfoId <= 0 {
  181. br.Msg = "请选择图表"
  182. return
  183. }
  184. err, errMsg := services.DeleteChart(req.ChartInfoId)
  185. if err != nil {
  186. br.Msg = errMsg
  187. br.ErrMsg = err.Error()
  188. return
  189. }
  190. br.Ret = 200
  191. br.Success = true
  192. br.Msg = "删除成功"
  193. }