You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

245 lines
11 KiB

  1. package com.simple.controller;
  2. import com.simple.common.ErrorCode;
  3. import com.simple.domain.po.WxCUser;
  4. import com.simple.domain.po.WxCouponChannel;
  5. import com.simple.domain.vo.WxOrderCVo;
  6. import com.simple.enums.EnumCouponChannelStatus;
  7. import com.simple.enums.EnumCouponStatus;
  8. import com.simple.enums.EnumOrderStatus;
  9. import com.simple.exception.MallinkException;
  10. import com.simple.service.WxCouponChannelService;
  11. import io.swagger.annotations.Api;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.log4j.Logger;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.web.bind.annotation.*;
  16. import com.github.pagehelper.PageInfo;
  17. import com.simple.common.Result;
  18. import com.simple.common.ResultData;
  19. import com.simple.domain.po.WxOrder;
  20. import com.simple.service.WxOrderService;
  21. import io.swagger.annotations.ApiImplicitParam;
  22. import io.swagger.annotations.ApiImplicitParams;
  23. import io.swagger.annotations.ApiOperation;
  24. import java.util.Map;
  25. @RestController
  26. @RequestMapping("/api/order/")
  27. @Api(description = "订单相关接口")
  28. public class WxOrderController extends BaseController {
  29. private Logger logger = Logger.getLogger(WxOrderController.class);
  30. @Autowired
  31. private WxCouponChannelService wxCouponChannelService;
  32. @Autowired
  33. private WxOrderService wxOrderService;
  34. @ApiOperation(value = "免费领取", notes = "{\"couponChannelId\":\"String\",\"couponId\":\"String\"}")
  35. @PostMapping("freeCoupon")
  36. public ResultData freeCoupon(@RequestBody Map<String, String> paramMap) {
  37. //Assert.notNull(wxOrders.getName(), "角色名不能为空");
  38. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  39. String couponChannelIdStr = paramMap.get("couponChannelId");
  40. String couponIdStr = paramMap.get("couponId");
  41. if (StringUtils.isBlank(couponChannelIdStr)) {
  42. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId不能为空");
  43. }
  44. Long couponChannelId = 0L, couponId = 0L;
  45. try {
  46. couponChannelId = Long.valueOf(couponChannelIdStr);
  47. } catch (NumberFormatException e) {
  48. logger.error("couponChannelId convert error, " + couponChannelIdStr + ", e:" + e.getMessage());
  49. return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "couponChannelId: " + couponChannelIdStr + ", e:" + e.getMessage());
  50. }
  51. WxCouponChannel wxCouponChannel = wxCouponChannelService.getById(couponChannelId);
  52. if (wxCouponChannel == null) {
  53. logger.error("couponChannelId convert error, " + couponChannelIdStr);
  54. return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "找不到发布的频道");
  55. }
  56. if (wxCouponChannel.getStatus() == EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()) {
  57. logger.error("此券已下架:" + couponChannelIdStr);
  58. return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF.getCode(), "此券已下架");
  59. }
  60. if (StringUtils.isBlank(couponIdStr)) {
  61. couponId = wxCouponChannel.getCouponId();
  62. }
  63. WxCUser user = getUser();
  64. WxOrder order = null;
  65. try {
  66. order = wxOrderService.sendUserFreeCoupon(user.getId(), couponId);
  67. return new ResultData(order);
  68. } catch (MallinkException e) {
  69. logger.error(e.getMessage());
  70. return new ResultData(e.getErrorCode(), e.getMessage());
  71. } catch (Exception e) {
  72. logger.error(e.getMessage());
  73. return new ResultData(ErrorCode.ORDER_IS_FAIL, e.getMessage());
  74. }
  75. }
  76. @ApiOperation(value = "下订单", notes = "{\"couponChannelId\":\"String\",\"couponId\":\"String\"}")
  77. @PostMapping("save")
  78. public ResultData saveOrder(@RequestBody Map<String, String> paramMap) {
  79. //Assert.notNull(wxOrders.getName(), "角色名不能为空");
  80. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  81. String couponChannelIdStr = paramMap.get("couponChannelId");
  82. String couponIdStr = paramMap.get("couponId");
  83. if (StringUtils.isBlank(couponChannelIdStr)) {
  84. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "couponChannelId不能为空");
  85. }
  86. Long couponChannelId = 0L, couponId = 0L;
  87. try {
  88. couponChannelId = Long.valueOf(couponChannelIdStr);
  89. } catch (NumberFormatException e) {
  90. logger.error("couponChannelId convert error, " + couponChannelIdStr + ", e:" + e.getMessage());
  91. return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "couponChannelId: " + couponChannelIdStr + ", e:" + e.getMessage());
  92. }
  93. WxCouponChannel wxCouponChannel = wxCouponChannelService.getById(couponChannelId);
  94. if (wxCouponChannel == null) {
  95. logger.error("couponChannelId convert error, " + couponChannelIdStr);
  96. return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "找不到发布的频道");
  97. }
  98. if (wxCouponChannel.getStatus() == EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()) {
  99. logger.error("此券已下架:" + couponChannelIdStr);
  100. return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF.getCode(), "此券已下架");
  101. }
  102. if (StringUtils.isBlank(couponIdStr)) {
  103. couponId = wxCouponChannel.getCouponId();
  104. }
  105. WxCUser user = getUser();
  106. try {
  107. WxOrder order = wxOrderService.saveOrder(user, couponId);
  108. return new ResultData(order);
  109. } catch (MallinkException e) {
  110. logger.error(e.getMessage());
  111. return new ResultData(e.getErrorCode(), e.getMessage());
  112. } catch (Exception e) {
  113. logger.error(e.getMessage());
  114. return new ResultData(ErrorCode.ORDER_IS_FAIL, e.getMessage());
  115. }
  116. }
  117. @ApiOperation(value = "取消订单", notes = "{\"orderId\":\"string\"}")
  118. @PostMapping("cancel")
  119. public ResultData cancelOrder(@RequestBody Map<String, String> paramMap) {
  120. //Assert.notNull(wxOrders.getName(), "角色名不能为空");
  121. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  122. String orderIdStr = paramMap.get("orderId");
  123. if (StringUtils.isBlank(orderIdStr)) {
  124. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
  125. }
  126. Long orderId = 0L;
  127. WxCUser user = getUser();
  128. try {
  129. orderId = Long.valueOf(orderIdStr);
  130. } catch (NumberFormatException e) {
  131. logger.error(e.getMessage());
  132. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL, "orderId: " + orderIdStr + ", e: " + e.getMessage());
  133. }
  134. try {
  135. WxOrder order = wxOrderService.getById(orderId);
  136. if (order != null) {
  137. wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL);
  138. }
  139. } catch (Exception e) {
  140. logger.error("取消订单失败: " + e.getMessage());
  141. return new ResultData(ErrorCode.ORDER_IS_FAIL.getCode(), "取消订单失败: " + e.getMessage());
  142. }
  143. return new ResultData();
  144. }
  145. @ApiOperation(value = "订单已过期", notes = "{\"orderId\":\"string\"}")
  146. @PostMapping("expired")
  147. public ResultData expiredOrder(@RequestBody Map<String, String> paramMap) {
  148. //Assert.notNull(wxOrders.getName(), "角色名不能为空");
  149. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  150. String orderIdStr = paramMap.get("orderId");
  151. if (StringUtils.isBlank(orderIdStr)) {
  152. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "退款订单ID不能为空");
  153. }
  154. Long orderId = 0L;
  155. WxCUser user = getUser();
  156. try {
  157. orderId = Long.valueOf(orderIdStr);
  158. } catch (NumberFormatException e) {
  159. logger.error(e.getMessage());
  160. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "退款订单ID转换失败");
  161. }
  162. try {
  163. WxOrder order = wxOrderService.getById(orderId);
  164. if (order != null) {
  165. wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS);
  166. }
  167. } catch (Exception e) {
  168. logger.error("更新退款订单状态失败:" + e.getMessage());
  169. return new ResultData(ErrorCode.REFUND_ORDER_ERROR.getCode(), "更新退款订单状态失败:" + e.getMessage());
  170. }
  171. return new ResultData();
  172. }
  173. @ApiOperation("分页订单列表接口")
  174. @GetMapping("list")
  175. @ApiImplicitParams({
  176. @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
  177. @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true),
  178. })
  179. public ResultData list(@ModelAttribute WxOrder wxOrder, Integer pageNum, Integer pageSize) {
  180. // c端用户应该只能看到自己的订单
  181. if (wxOrder == null) wxOrder = new WxOrder();
  182. wxOrder.setCUserId(getUser().getId());
  183. final PageInfo<WxOrderCVo> page = wxOrderService.listCUserVoAsPage(wxOrder, pageNum, pageSize);
  184. return new ResultData(page);
  185. }
  186. @ApiOperation("订单详情接口")
  187. @GetMapping("detail")
  188. public ResultData list(@ModelAttribute WxOrder wxOrder) {
  189. // c端用户应该只能看到自己的订单细节
  190. if (wxOrder == null) wxOrder = new WxOrder();
  191. wxOrder.setCUserId(getUser().getId());
  192. WxOrderCVo wxOrderCVo = wxOrderService.detailCUserVo(wxOrder);
  193. if (wxOrderCVo == null)
  194. return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
  195. return new ResultData(wxOrderCVo);
  196. }
  197. @ApiOperation(value = "根据orderId查询接口", notes = "{\"orderId\":\"string\"}")
  198. @GetMapping("/findById")
  199. public ResultData findById(@RequestBody Map<String, String> paramMap) {
  200. String orderIdStr = paramMap.get("orderId");
  201. if (StringUtils.isBlank(orderIdStr)) {
  202. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
  203. }
  204. Long orderId = 0L;
  205. try {
  206. orderId = Long.valueOf(orderIdStr);
  207. } catch (NumberFormatException e) {
  208. logger.error("parse orderId failed");
  209. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "订单ID转换异常");
  210. }
  211. WxOrder order = null;
  212. try {
  213. order = wxOrderService.getById(orderId);
  214. if (order != null) {
  215. return new ResultData(Result.SUCCESS, "查询成功", order);
  216. } else {
  217. return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
  218. }
  219. } catch (Exception e) {
  220. logger.error("parse orderId failed");
  221. return new ResultData(ErrorCode.DB_FAIL.getCode(), "订单查询未成功,e:" + e.getMessage());
  222. }
  223. }
  224. }