|
|
@@ -4,6 +4,8 @@ import cn.reghao.jutil.jdk.result.Result;
|
|
|
import cn.reghao.jutil.jdk.result.ResultStatus;
|
|
|
import cn.reghao.tnb.common.auth.UserContext;
|
|
|
import cn.reghao.tnb.content.app.mall.db.mapper.ProductMapper;
|
|
|
+import cn.reghao.tnb.content.app.mall.db.repository.MallRepository;
|
|
|
+import cn.reghao.tnb.content.app.mall.model.constant.OrderStatus;
|
|
|
import cn.reghao.tnb.content.app.mall.model.po.Order;
|
|
|
import cn.reghao.tnb.user.api.iface.UserWalletService;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
@@ -21,29 +23,30 @@ public class PayService {
|
|
|
|
|
|
private final OrderService orderService;
|
|
|
private final ProductMapper productMapper;
|
|
|
+ private MallRepository mallRepository;
|
|
|
|
|
|
- public PayService(OrderService orderService, ProductMapper productMapper) {
|
|
|
+ public PayService(OrderService orderService, ProductMapper productMapper, MallRepository mallRepository) {
|
|
|
this.orderService = orderService;
|
|
|
this.productMapper = productMapper;
|
|
|
+ this.mallRepository = mallRepository;
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public Result pay(long orderId) {
|
|
|
+ public Result payOrder(long orderId) {
|
|
|
Order order = orderService.getOrder(orderId);
|
|
|
- if (order != null && order.getStatus() == 1) {
|
|
|
- long productId = order.getProductId();
|
|
|
+ if (order != null && order.getStatus() == OrderStatus.toPay.getCode()) {
|
|
|
+ long sellerId = mallRepository.getProduct(order.getProductId()).getSellerId();
|
|
|
int amount = order.getAmount();
|
|
|
long loginUser = UserContext.getUser();
|
|
|
- long sellerId = productMapper.findByItemId(order.getProductId()).getSellerId();
|
|
|
+ double quantity = amount*order.getPrice();
|
|
|
|
|
|
- // TODO 在一个事务内完成, 需要用到分布式事务
|
|
|
+ // TODO 需要使用分布式事务来保证扣款和修改订单状态在一个事务内完成
|
|
|
// 支付订单
|
|
|
- Result result = userWalletService.pay(loginUser, amount, sellerId);
|
|
|
+ Result result = userWalletService.pay(loginUser, quantity, sellerId);
|
|
|
if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
// 更新订单状态
|
|
|
orderService.payOrder(orderId);
|
|
|
- // 更新库存数量
|
|
|
- productMapper.updateStockMinus(productId, amount);
|
|
|
+ mallRepository.updatePayOrder(orderId);
|
|
|
}
|
|
|
|
|
|
return result;
|