producct.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package admin
  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 product services.Product = &imp.Product{}
  15. // AddProduct
  16. // @Tags 管理员
  17. // @Summary 添加产品类型
  18. // @Success 200 {object} e.R
  19. // @Fail 400 {object} e.R
  20. // @Param req body models.ProductDto true "请求参数"
  21. // @Accept application/json
  22. // @Router /admin/product [post]
  23. func AddProduct(c *gin.Context) {
  24. var produ models.ProductDto
  25. err := c.ShouldBindJSON(&produ)
  26. if err != nil {
  27. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  28. return
  29. }
  30. uid := utils.GetUId(c)
  31. rescode := product.AddProduct(uid, produ)
  32. if errors.Is(rescode, e.SUCCESS) {
  33. e.ResponseSuccess(c, "添加成功")
  34. return
  35. }
  36. e.ResponseError(c, rescode)
  37. }
  38. // UpdateProduct
  39. // @Tags 管理员
  40. // @Summary 修改产品类型
  41. // @Success 200 {object} e.R
  42. // @Fail 400 {object} e.R
  43. // @Param req body models.ProductDto true "请求参数"
  44. // @Param id query string true "id"
  45. // @Accept application/json
  46. // @Router /admin/product [put]
  47. func UpdateProduct(c *gin.Context) {
  48. id := c.Query("id")
  49. var produ models.ProductDto
  50. validate := validator.New()
  51. atoi, _ := strconv.Atoi(id)
  52. err := c.ShouldBindJSON(&produ)
  53. err = validate.Var(id, "required,numeric")
  54. if err != nil {
  55. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  56. return
  57. }
  58. if err != nil {
  59. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  60. return
  61. }
  62. uId := utils.GetUId(c)
  63. rescode := product.UpdateProduct(uId, atoi, produ)
  64. if errors.Is(rescode, e.SUCCESS) {
  65. e.ResponseSuccess(c, "修改成功")
  66. return
  67. }
  68. e.ResponseError(c, rescode)
  69. }
  70. // GetAllProduct
  71. // @Tags 管理员
  72. // @Summary 查询所有类型
  73. // @Success 200 {object} e.R
  74. // @Fail 400 {object} e.R
  75. // @Param req body unity.PageParams true "分页数据"
  76. // @Accept application/json
  77. // @Router /admin/productall [post]
  78. //func GetAllProduct(c *gin.Context) {
  79. // var pages unity.PageParams
  80. // err := c.ShouldBindJSON(&pages)
  81. // if err != nil {
  82. // e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  83. // return
  84. // }
  85. // result, total, err := unity.Paginate(pages, models.Product{})
  86. // if err != nil {
  87. // e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  88. // return
  89. // }
  90. // e.ResPonsePage(c, result, total, pages)
  91. //}
  92. // DeleteProductById
  93. // @Tags 管理员
  94. // @Summary 根据ID删除产品类型
  95. // @Success 200 {object} e.R
  96. // @Fail 400 {object} e.R
  97. // @Param id query string true "id"
  98. // @Accept application/json
  99. // @Router /admin/product [delete]
  100. func DeleteProductById(c *gin.Context) {
  101. id := c.Query("id")
  102. validate := validator.New()
  103. err := validate.Var(id, "required,numeric")
  104. if err != nil {
  105. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  106. return
  107. }
  108. atoi, _ := strconv.Atoi(id)
  109. _, rescode := unity.DeleteById(atoi, models.Product{})
  110. if rescode != nil {
  111. e.ResponseWithMsg(c, e.DeleteFail, e.DeleteFail.GetMsg())
  112. return
  113. }
  114. e.ResponseSuccess(c, "删除成功")
  115. }