address.go 5.3 KB

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