address.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controller
  2. import (
  3. "gas-cylinder-api/app/admin/model"
  4. "gas-cylinder-api/app/admin/service"
  5. "gas-cylinder-api/app/admin/service/dto"
  6. "gas-cylinder-api/common/actions"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gin-gonic/gin/binding"
  9. "gogs.baozhida.cn/zoie/OAuth-core/api"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  11. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. )
  13. type AddressController struct {
  14. api.Api
  15. }
  16. // GetPage 获取收货地址列表
  17. // @Summary 获取收货地址列表
  18. // @Description 获取收货地址列表
  19. // @Tags 收货地址
  20. // @Param pageSize query int false "页条数"
  21. // @Param page query int false "页码"
  22. // @Success 200 {object} response.Response{data=response.Page{list=[]model.Address}} "{"code": 200, "data": [...]}"
  23. // @Router /api/address [get]
  24. // @Security Bearer
  25. func (e AddressController) GetPage(c *gin.Context) {
  26. s := service.Address{}
  27. req := dto.AddressGetPageReq{}
  28. err := e.MakeContext(c).
  29. MakeOrm().
  30. Bind(&req, binding.Query).
  31. MakeService(&s.Service).
  32. Errors
  33. if err != nil {
  34. e.Logger.Error(err)
  35. e.Error(500, err, err.Error())
  36. return
  37. }
  38. //数据权限检查
  39. req.CustomerId = service.GetAppletCustomerId(c)
  40. list := make([]model.Address, 0)
  41. var count int64
  42. err = s.GetPage(&req, &list, &count)
  43. if err != nil {
  44. e.Error(500, err, err.Error())
  45. return
  46. }
  47. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  48. }
  49. // Get 通过id获取收货地址
  50. // @Summary 通过id获取收货地址
  51. // @Description 通过id获取收货地址
  52. // @Tags 收货地址
  53. // @Param id path string true "收货地址id"
  54. // @Success 200 {object} response.Response{data=model.Address} "{"code": 200, "data": [...]}"
  55. // @Router /api/address/{id} [get]
  56. // @Security Bearer
  57. func (e AddressController) Get(c *gin.Context) {
  58. s := service.Address{}
  59. req := dto.AddressGetReq{}
  60. err := e.MakeContext(c).
  61. MakeOrm().
  62. Bind(&req, nil).
  63. MakeService(&s.Service).
  64. Errors
  65. if err != nil {
  66. e.Logger.Error(err)
  67. e.Error(500, err, err.Error())
  68. return
  69. }
  70. var object model.Address
  71. p := actions.GetPermissionFromContext(c)
  72. //数据权限检查
  73. err = s.Get(&req, &object, p)
  74. if err != nil {
  75. e.Error(500, err, err.Error())
  76. return
  77. }
  78. e.OK(object, "查询成功")
  79. }
  80. // Insert 添加收货地址
  81. // @Summary 添加收货地址
  82. // @Description 添加收货地址
  83. // @Tags 收货地址
  84. // @Accept application/json
  85. // @Product application/json
  86. // @Param data body dto.AddressInsertReq true "data"
  87. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  88. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  89. // @Router /api/address [post]
  90. // @Security Bearer
  91. func (e AddressController) Insert(c *gin.Context) {
  92. s := service.Address{}
  93. req := dto.AddressInsertReq{}
  94. err := e.MakeContext(c).
  95. MakeOrm().
  96. Bind(&req, binding.JSON).
  97. MakeService(&s.Service).
  98. Errors
  99. if err != nil {
  100. e.Logger.Error(err)
  101. e.Error(500, err, err.Error())
  102. return
  103. }
  104. // 设置创建人
  105. req.SetCustomerId(service.GetAppletCustomerId(c))
  106. err = s.Insert(&req)
  107. if err != nil {
  108. e.Error(500, err, err.Error())
  109. return
  110. }
  111. e.OK(req.GetId(), "创建成功")
  112. }
  113. // Update 修改收货地址
  114. // @Summary 修改收货地址
  115. // @Description 修改收货地址
  116. // @Tags 收货地址
  117. // @Accept application/json
  118. // @Product application/json
  119. // @Param data body dto.AddressUpdateReq true "body"
  120. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  121. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  122. // @Router /api/address [put]
  123. // @Security Bearer
  124. func (e AddressController) Update(c *gin.Context) {
  125. s := service.Address{}
  126. req := dto.AddressUpdateReq{}
  127. err := e.MakeContext(c).
  128. MakeOrm().
  129. Bind(&req).
  130. MakeService(&s.Service).
  131. Errors
  132. if err != nil {
  133. e.Logger.Error(err)
  134. e.Error(500, err, err.Error())
  135. return
  136. }
  137. p := actions.GetPermissionFromContext(c)
  138. req.SetUpdateBy(user.GetUserId(c))
  139. err = s.Update(&req, p)
  140. if err != nil {
  141. e.Error(500, err, err.Error())
  142. return
  143. }
  144. e.OK(req.GetId(), "更新成功")
  145. }
  146. // Delete 删除收货地址
  147. // @Summary 删除收货地址
  148. // @Description 删除收货地址
  149. // @Tags 收货地址
  150. // @Accept application/json
  151. // @Product application/json
  152. // @Param data body dto.AddressDeleteReq true "body"
  153. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  154. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  155. // @Router /api/address [delete]
  156. // @Security Bearer
  157. func (e AddressController) Delete(c *gin.Context) {
  158. s := service.Address{}
  159. req := dto.AddressDeleteReq{}
  160. userSvc := service.SysUser{}
  161. err := e.MakeContext(c).
  162. MakeOrm().
  163. Bind(&req, binding.JSON, nil).
  164. MakeService(&s.Service).
  165. MakeService(&userSvc.Service).
  166. Errors
  167. if err != nil {
  168. e.Logger.Error(err)
  169. e.Error(500, err, err.Error())
  170. return
  171. }
  172. //数据权限检查
  173. p := actions.GetPermissionFromContext(c)
  174. err = s.Remove(&req, p)
  175. if err != nil {
  176. e.Error(500, err, err.Error())
  177. return
  178. }
  179. e.OK(req.GetId(), "删除成功")
  180. }