order_controller.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package order
  2. import (
  3. "eta/eta_mini_ht_api/common/exception"
  4. "eta/eta_mini_ht_api/common/utils/page"
  5. "eta/eta_mini_ht_api/controllers"
  6. "eta/eta_mini_ht_api/service/order"
  7. "eta/eta_mini_ht_api/service/user"
  8. )
  9. type OrderController struct {
  10. controllers.BaseController
  11. }
  12. type PreviewOrderRequest struct {
  13. ProductId int `json:"productId"`
  14. }
  15. // PreviewProductOrder 预览订单
  16. // @Summary 预览订单
  17. // @Description 预览订单
  18. // @Success 200 {object} controllers.BaseResponse
  19. // @router /previewProductOrder [get]
  20. func (o *OrderController) PreviewProductOrder(productId int) {
  21. controllers.Wrap(&o.BaseController, func() (result *controllers.WrapData, err error) {
  22. result = o.InitWrapData("预览订单创建失败")
  23. var userInfo user.User
  24. userInfo = o.Data["user"].(user.User)
  25. if productId <= 0 {
  26. err = exception.New(exception.IllegalProductId)
  27. o.FailedResult("预览订单失败,产品编号非法", result)
  28. return
  29. }
  30. //创单
  31. //返回订单信息、商品信息
  32. orderInfo, err := order.PreViewProductOrder(userInfo, productId)
  33. if err != nil {
  34. o.FailedResult("创建预览订单失败", result)
  35. return
  36. }
  37. o.SuccessResult("预览订单创建成功", orderInfo, result)
  38. return
  39. })
  40. }
  41. // OrderDetail 获取订单详情
  42. // @Summary 获取订单详情
  43. // @Description 获取订单详情
  44. // @Success 200 {object} controllers.BaseResponse
  45. // @router /orderDetail [get]
  46. func (sc *SubscribeController) GetOrderDetail(orderNo string) {
  47. controllers.Wrap(&sc.BaseController, func() (result *controllers.WrapData, err error) {
  48. result = sc.InitWrapData("获取订单号失败")
  49. userInfo := sc.Data["user"].(user.User)
  50. if orderNo == "" {
  51. err = exception.New(exception.IllegalProductOrderId)
  52. sc.FailedResult("获取订单详情失败,产品编号非法", result)
  53. return
  54. }
  55. orderDetail, err := order.GetOrderDetail(orderNo, userInfo.Id)
  56. if err != nil {
  57. sc.FailedResult("获取订单详情失败", result)
  58. err = exception.NewWithException(exception.GetOrderListFailed, err.Error())
  59. return
  60. }
  61. sc.SuccessResult("获取订单详情成功", orderDetail, result)
  62. return
  63. })
  64. }
  65. // GetOrderList 获取用户订单号列表
  66. // @Summary 获取用户订单号列表
  67. // @Description 获取用户订单号列表
  68. // @Success 200 {object} controllers.BaseResponse
  69. // @router /orderList [get]
  70. func (sc *SubscribeController) GetOrderList(orderStatus string) {
  71. controllers.Wrap(&sc.BaseController, func() (result *controllers.WrapData, err error) {
  72. result = sc.InitWrapData("分页查询订单列表失败")
  73. userInfo := sc.Data["user"].(user.User)
  74. pageRes := page.Page{
  75. Current: sc.PageInfo.Current,
  76. PageSize: sc.PageInfo.PageSize,
  77. }
  78. pageRes.Total, pageRes.LatestId = order.GetTotalPageCountByUserId(userInfo.Id, orderStatus)
  79. if sc.PageInfo.LatestId == 0 {
  80. sc.PageInfo.LatestId = pageRes.LatestId
  81. } else {
  82. pageRes.LatestId = sc.PageInfo.LatestId
  83. pageRes.Total = sc.PageInfo.Total
  84. }
  85. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  86. list, err := order.GetOrderPage(sc.PageInfo, userInfo.Id, orderStatus)
  87. if err != nil {
  88. sc.FailedResult("分页查询订单列表失败", result)
  89. return
  90. }
  91. orders := new(page.PageResult)
  92. orders.Data = list
  93. orders.Page = pageRes
  94. sc.SuccessResult("查询订单列表成功", orders, result)
  95. return
  96. })
  97. }
  98. type OrderRequest struct {
  99. ProductOrderNo string `json:"productOrderNo"`
  100. }
  101. // CloseOrder 获取用户订单号列表
  102. // @Summary 获取用户订单号列表
  103. // @Description 获取用户订单号列表
  104. // @Success 200 {object} controllers.BaseResponse
  105. // @router /closeOrder [post]
  106. func (sc *SubscribeController) CloseOrder() {
  107. controllers.Wrap(&sc.BaseController, func() (result *controllers.WrapData, err error) {
  108. result = sc.InitWrapData("关闭订单失败")
  109. userInfo := sc.Data["user"].(user.User)
  110. orderReq := new(OrderRequest)
  111. sc.GetPostParams(orderReq)
  112. if orderReq.ProductOrderNo == "" {
  113. sc.FailedResult("关闭订单失败,非法的订单编号", result)
  114. err = exception.New(exception.IllegalOrderNo)
  115. return
  116. }
  117. err = order.CloseProductOrder(userInfo.Id, orderReq.ProductOrderNo)
  118. if err != nil {
  119. sc.FailedResult("关闭订单失败", result)
  120. err = exception.NewWithException(exception.CloseOrderFailed, err.Error())
  121. return
  122. }
  123. closeDTO := map[string]string{
  124. "orderNo": orderReq.ProductOrderNo,
  125. }
  126. sc.SuccessResult("关闭订单成功", closeDTO, result)
  127. return
  128. })
  129. }