serviceNode.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package v1
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator"
  6. "lot_interlligentControl/app/e"
  7. "lot_interlligentControl/app/services"
  8. "lot_interlligentControl/app/services/imp"
  9. "lot_interlligentControl/models"
  10. "lot_interlligentControl/unity"
  11. "lot_interlligentControl/utils"
  12. "strconv"
  13. )
  14. var serviceNode services.ServiceNodes = &imp.ServiceNodes{}
  15. // GetServiceNodesById
  16. // @Tags 节点管理
  17. // @Summary 查询自己的所有节点
  18. // @Success 200 {object} e.R
  19. // @Fail 400 {object} e.R
  20. // @Param req body unity.PageParams true "分页数据"
  21. // @Accept application/json
  22. // @Router /serviceNode [get]
  23. func GetServiceNodesById(c *gin.Context) {
  24. var pages unity.PageParams
  25. uId := utils.GetUId(c)
  26. err := c.ShouldBindJSON(&pages)
  27. if err != nil {
  28. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  29. return
  30. }
  31. result, total, err := unity.PaginateByID(uId, pages, models.ServiceNodes{})
  32. if err != nil {
  33. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  34. return
  35. }
  36. e.ResPonsePage(c, result, total, pages)
  37. }
  38. // DeleteServiceNodesById
  39. // @Tags 节点管理
  40. // @Summary 根据Id删除节点
  41. // @Success 200 {object} e.R
  42. // @Fail 400 {object} e.R
  43. // @Param id query string true "id"
  44. // @Accept application/json
  45. // @Router /serviceNode [delete]
  46. func DeleteServiceNodesById(c *gin.Context) {
  47. id := c.Query("id")
  48. validate := validator.New()
  49. err := validate.Var(id, "required,numeric")
  50. if err != nil {
  51. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  52. return
  53. }
  54. atoi, _ := strconv.Atoi(id)
  55. _, rescode := unity.DeleteById(atoi, models.ServiceNodes{})
  56. if rescode != nil {
  57. e.ResponseWithMsg(c, e.DeleteFail, e.DeleteFail.GetMsg())
  58. return
  59. }
  60. e.ResponseSuccess(c, "删除成功")
  61. }
  62. // AddServiceNode
  63. // @Tags 节点管理
  64. // @Summary 添加节点
  65. // @Success 200 {object} e.R
  66. // @Fail 400 {object} e.R
  67. // @Param req body models.ServiceNodesDto true "节点数据"
  68. // @Accept application/json
  69. // @Router /serviceNode [post]
  70. func AddServiceNode(c *gin.Context) {
  71. var nodes models.ServiceNodesDto
  72. err := c.ShouldBindJSON(&nodes)
  73. if err != nil {
  74. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  75. return
  76. }
  77. uid := utils.GetUId(c)
  78. rescode := serviceNode.AddServiceNodes(uid, nodes)
  79. if errors.Is(rescode, e.SUCCESS) {
  80. e.ResponseSuccess(c, "添加成功")
  81. return
  82. }
  83. e.ResponseError(c, rescode)
  84. }
  85. // UpdateServiceNode
  86. // @Tags 节点管理
  87. // @Summary 修改节点
  88. // @Success 200 {object} e.R
  89. // @Fail 400 {object} e.R
  90. // @Param id query string true "id"
  91. // @Param req body models.ServiceNodesDto true "节点数据"
  92. // @Accept application/json
  93. // @Router /serviceNode [put]
  94. func UpdateServiceNode(c *gin.Context) {
  95. id := c.Query("id")
  96. var nodes models.ServiceNodesDto
  97. validate := validator.New()
  98. atoi, _ := strconv.Atoi(id)
  99. err := c.ShouldBindJSON(&nodes)
  100. err = validate.Var(id, "required,numeric")
  101. if err != nil {
  102. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  103. return
  104. }
  105. if err != nil {
  106. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  107. return
  108. }
  109. uId := utils.GetUId(c)
  110. rescode := serviceNode.UpdateServiceNodes(uId, atoi, nodes)
  111. if errors.Is(rescode, e.SUCCESS) {
  112. e.ResponseSuccess(c, "修改成功")
  113. return
  114. }
  115. e.ResponseError(c, rescode)
  116. }
  117. // GetNode
  118. // @Tags 节点管理
  119. // @Summary 获取自己节点以及公开节点
  120. // @Success 200 {object} []models.ServiceNodes
  121. // @Fail 400 {object} e.R
  122. // @Accept application/json
  123. // @Router /getNode [get]
  124. func GetNode(c *gin.Context) {
  125. uid := utils.GetUId(c)
  126. node, rescode := serviceNode.GetNode(uid)
  127. if errors.Is(rescode, e.SUCCESS) {
  128. e.ResponseSuccess(c, node)
  129. return
  130. }
  131. e.ResponseError(c, rescode)
  132. }