refactor: 订单确认获取商品明细入参优化和注释完善

This commit is contained in:
郝先瑞 2022-04-16 11:07:14 +08:00
parent 9cb21396c7
commit 6d09e0b859
4 changed files with 45 additions and 43 deletions

View File

@ -1,10 +1,10 @@
package com.youlai.mall.oms.controller.app;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.common.base.IBaseEnum;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.mall.oms.enums.PayTypeEnum;
import com.youlai.mall.oms.pojo.dto.OrderConfirmDTO;
import com.youlai.mall.oms.pojo.entity.OmsOrder;
import com.youlai.mall.oms.pojo.form.OrderSubmitForm;
import com.youlai.mall.oms.pojo.query.OrderPageQuery;
@ -40,10 +40,22 @@ public class OrderController {
return PageResult.success(result);
}
/**
* 订单确认 进入创建订单页面
* <p>
* 获取购买商品明细用户默认收货地址防重提交唯一token
* 进入订单创建页面有两个入口1立即购买2购物车结算
*
* @param skuId 直接购买必填购物车结算不填
* @return
*/
@ApiOperation("订单确认")
@PostMapping("/_confirm")
public Result<OrderConfirmVO> confirm(@RequestBody OrderConfirmDTO orderConfirm) {
OrderConfirmVO result = orderService.confirm(orderConfirm);
public Result<OrderConfirmVO> confirm(
@RequestParam(required = false) Long skuId
) {
OrderConfirmVO result = orderService.confirm(skuId);
return Result.success(result);
}
@ -63,7 +75,7 @@ public class OrderController {
})
public <T> Result<T> pay(@PathVariable Long orderId, Integer payType, String appId) {
PayTypeEnum payTypeEnum = PayTypeEnum.getByCode(payType);
PayTypeEnum payTypeEnum = IBaseEnum.getEnumByValue(payType, PayTypeEnum.class);
if (payTypeEnum == null) {
return Result.failed("系统暂不支持该支付方式~");
}

View File

@ -1,21 +0,0 @@
package com.youlai.mall.oms.pojo.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
/**
* @author huawei
* @desc 订单提交实体类
* @email huawei_code@163.com
* @date 2021/1/16
*/
@Data
public class OrderConfirmDTO {
private Long skuId;
private Integer count;
}

View File

@ -7,7 +7,6 @@ import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.youlai.mall.oms.enums.PayTypeEnum;
import com.youlai.mall.oms.pojo.entity.OmsOrder;
import com.youlai.mall.oms.pojo.dto.OrderConfirmDTO;
import com.youlai.mall.oms.pojo.query.OrderPageQuery;
import com.youlai.mall.oms.pojo.vo.OrderConfirmVO;
import com.youlai.mall.oms.pojo.vo.OrderSubmitVO;
@ -23,9 +22,15 @@ import com.youlai.mall.oms.pojo.form.OrderSubmitForm;
public interface IOrderService extends IService<OmsOrder> {
/**
* 订单确认
* 订单确认 进入创建订单页面
* <p>
* 获取购买商品明细用户默认收货地址防重提交唯一token
* 进入订单创建页面有两个入口1立即购买2购物车结算
*
* @param skuId 直接购买必填购物车结算不填
* @return
*/
OrderConfirmVO confirm(OrderConfirmDTO orderConfirmDTO);
OrderConfirmVO confirm(Long skuId);
/**
* 订单提交

View File

@ -31,7 +31,6 @@ import com.youlai.mall.oms.enums.OrderTypeEnum;
import com.youlai.mall.oms.enums.PayTypeEnum;
import com.youlai.mall.oms.mapper.OrderMapper;
import com.youlai.mall.oms.pojo.dto.CartItemDTO;
import com.youlai.mall.oms.pojo.dto.OrderConfirmDTO;
import com.youlai.mall.oms.pojo.dto.OrderItemDTO;
import com.youlai.mall.oms.pojo.entity.OmsOrder;
import com.youlai.mall.oms.pojo.entity.OmsOrderItem;
@ -110,15 +109,20 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
}
/**
* 订单确认
* 订单确认 进入创建订单页面
* <p>
* 获取购买商品明细用户默认收货地址防重提交唯一token
* 进入订单创建页面有两个入口1立即购买2购物车结算
*
* @param skuId 直接购买必填购物车结算不填
* @return
*/
@Override
public OrderConfirmVO confirm(OrderConfirmDTO orderConfirmDTO) {
log.info("订单确认:{}", orderConfirmDTO);
public OrderConfirmVO confirm(Long skuId) {
OrderConfirmVO orderConfirmVO = new OrderConfirmVO();
// 获取订单的商品信息
// 获取订单的商品明细信息
CompletableFuture<Void> orderItemsCompletableFuture = CompletableFuture.runAsync(() -> {
List<OrderItemDTO> orderItems = this.getOrderItems(orderConfirmDTO.getSkuId());
List<OrderItemDTO> orderItems = this.getOrderItems(skuId);
orderConfirmVO.setOrderItems(orderItems);
}, threadPoolExecutor);
@ -128,7 +132,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
orderConfirmVO.setAddresses(addresses);
}, threadPoolExecutor);
// 生成唯一 token防止订单重复提交
// 生成唯一token防止订单重复提交
CompletableFuture<Void> orderTokenCompletableFuture = CompletableFuture.runAsync(() -> {
String orderToken = businessNoGenerator.generate(BusinessTypeEnum.ORDER);
orderConfirmVO.setOrderToken(orderToken);
@ -233,7 +237,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
}
T result;
switch (payTypeEnum) {
case WEIXIN_JSAPI:
case WX_JSAPI:
result = (T) wxJsapiPay(appId, order);
break;
default:
@ -271,7 +275,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
// 更新订单状态
order.setStatus(OrderStatusEnum.PAYED.getCode());
order.setPayType(PayTypeEnum.BALANCE.getCode());
order.setPayType(PayTypeEnum.BALANCE.getValue());
order.setPayTime(new Date());
this.updateById(order);
// 支付成功删除购物车已勾选的商品
@ -285,7 +289,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
Long memberId = MemberUtils.getMemberId();
Long payAmount = order.getPayAmount();
// 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WEIXIN_JSAPI.getCode().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
try {
wxPayService.closeOrderV3(order.getOutTradeNo());
} catch (WxPayException e) {
@ -301,7 +305,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
String outTradeNo = "wxo_" + System.currentTimeMillis() + RandomUtil.randomNumbers(3) + fiveDigitsUserId;
log.info("商户订单号拼接完成:{}", outTradeNo);
// 更新订单状态
order.setPayType(PayTypeEnum.WEIXIN_JSAPI.getCode());
order.setPayType(PayTypeEnum.WX_JSAPI.getValue());
order.setOutTradeNo(outTradeNo);
this.updateById(order);
@ -333,7 +337,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
return false;
}
// 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WEIXIN_JSAPI.getCode().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
try {
wxPayService.closeOrderV3(order.getOutTradeNo());
order.setOutTradeNo(null);
@ -358,7 +362,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
throw new BizException("取消失败,订单状态不支持取消"); // 通过自定义异常将异常信息抛出由异常处理器捕获显示给前端页面
}
// 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WEIXIN_JSAPI.getCode().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
try {
wxPayService.closeOrderV3(order.getOutTradeNo());
order.setOutTradeNo(null);
@ -465,9 +469,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
}
/**
* 获取订单的商品明细
* 获取订单的商品明细信息
* <p>
* 创建订单两种方式1直接购买2购物车结算
*
* @param skuId 直接购买会有值
* @param skuId 直接购买必有值购物车结算必没
* @return
*/
private List<OrderItemDTO> getOrderItems(Long skuId) {