shop.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 shop services.Shop = &imp.Shop{}
  15. // AddShop
  16. // @Tags 店铺管理
  17. // @Summary 添加节点
  18. // @Success 200 {object} e.R
  19. // @Fail 400 {object} e.R
  20. // @Param req body models.ShopDto true "店铺数据"
  21. // @Accept application/json
  22. // @Router /shop [post]
  23. func AddShop(c *gin.Context) {
  24. var shops models.ShopDto
  25. err := c.ShouldBindJSON(&shops)
  26. if err != nil {
  27. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  28. return
  29. }
  30. uid := utils.GetUId(c)
  31. rescode := shop.AddShop(uid, shops)
  32. if errors.Is(rescode, e.SUCCESS) {
  33. e.ResponseSuccess(c, "添加成功")
  34. return
  35. }
  36. e.ResponseError(c, rescode)
  37. }
  38. // DeleteShopById
  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 /shop [delete]
  46. func DeleteShopById(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.Shop{})
  56. if rescode != nil {
  57. e.ResponseWithMsg(c, e.DeleteFail, e.DeleteFail.GetMsg())
  58. return
  59. }
  60. e.ResponseSuccess(c, "删除成功")
  61. }
  62. // UpdateShop
  63. // @Tags 店铺管理
  64. // @Summary 修改节点
  65. // @Success 200 {object} e.R
  66. // @Fail 400 {object} e.R
  67. // @Param id query string true "id"
  68. // @Param req body models.ShopDto true "店铺数据"
  69. // @Accept application/json
  70. // @Router /shop [put]
  71. func UpdateShop(c *gin.Context) {
  72. id := c.Query("id")
  73. var shops models.ShopDto
  74. validate := validator.New()
  75. atoi, _ := strconv.Atoi(id)
  76. err := c.ShouldBindJSON(&shops)
  77. err = validate.Var(id, "required,numeric")
  78. if err != nil {
  79. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  80. return
  81. }
  82. if err != nil {
  83. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  84. return
  85. }
  86. uId := utils.GetUId(c)
  87. rescode := shop.UpdateShop(uId, atoi, shops)
  88. if errors.Is(rescode, e.SUCCESS) {
  89. e.ResponseSuccess(c, "修改成功")
  90. return
  91. }
  92. e.ResponseError(c, rescode)
  93. }
  94. // GetShopById
  95. // @Tags 店铺管理
  96. // @Summary 获取自己的商品信息
  97. // @Success 200 {object} e.R
  98. // @Fail 400 {object} e.R
  99. // @Param req body unity.PageParams true "分页数据"
  100. // @Accept application/json
  101. // @Router /shop [get]
  102. func GetShopById(c *gin.Context) {
  103. var pages unity.PageParams
  104. uId := utils.GetUId(c)
  105. err := c.ShouldBindJSON(&pages)
  106. if err != nil {
  107. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  108. return
  109. }
  110. result, total, err := unity.PaginateByID(uId, pages, models.Shop{})
  111. if err != nil {
  112. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  113. return
  114. }
  115. e.ResPonsePage(c, result, total, pages)
  116. }
  117. // GetShopByName
  118. // @Tags 店铺管理
  119. // @Summary 根据商品名称模糊查询
  120. // @Success 200 {object} e.R
  121. // @Fail 400 {object} e.R
  122. // @Param req body unity.PageParams true "分页数据"
  123. // @Param product_name query string true "product_name"
  124. // @Accept application/json
  125. // @Router /shopByName [get]
  126. func GetShopByName(c *gin.Context) {
  127. value := c.Query("product_name")
  128. var pages unity.PageParams
  129. validate := validator.New()
  130. err := validate.Var(value, "required")
  131. if err != nil {
  132. e.ResponseWithMsg(c, e.TheParameterCannotBeEmpty, e.TheParameterCannotBeEmpty.GetMsg())
  133. return
  134. }
  135. err = c.ShouldBindJSON(&pages)
  136. if err != nil {
  137. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  138. return
  139. }
  140. uId := utils.GetUId(c)
  141. shops, total, err := shop.GetShopByName(uId, value, pages)
  142. if err != nil {
  143. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  144. return
  145. }
  146. e.ResPonsePage(c, shops, total, pages)
  147. }
  148. // GetAllProduct
  149. // @Tags 店铺管理
  150. // @Summary 查询所有类型
  151. // @Success 200 {object} e.R
  152. // @Fail 400 {object} e.R
  153. // @Param req body unity.PageParams true "分页数据"
  154. // @Accept application/json
  155. // @Router /productall [post]
  156. func GetAllProduct(c *gin.Context) {
  157. var pages unity.PageParams
  158. err := c.ShouldBindJSON(&pages)
  159. if err != nil {
  160. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  161. return
  162. }
  163. result, total, err := unity.Paginate(pages, models.Product{})
  164. if err != nil {
  165. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  166. return
  167. }
  168. e.ResPonsePage(c, result, total, pages)
  169. }