address.go 4.9 KB

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