address.go 5.2 KB

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