address.go 5.0 KB

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