Przeglądaj źródła

更新 content-service mall 模块

reghao 1 rok temu
rodzic
commit
ee90ecb005

+ 2 - 15
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/controller/OrderController.java

@@ -1,12 +1,10 @@
 package cn.reghao.tnb.content.app.mall.controller;
 
-import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.jutil.web.WebResult;
 import cn.reghao.tnb.common.auth.AuthUser;
 import cn.reghao.tnb.content.app.mall.model.dto.OrderDto;
 import cn.reghao.tnb.content.app.mall.model.vo.OrderDetail;
 import cn.reghao.tnb.content.app.mall.service.OrderService;
-import cn.reghao.tnb.content.app.mall.service.PayService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.validation.annotation.Validated;
@@ -23,11 +21,9 @@ import java.util.List;
 @RequestMapping("/api/mall/order")
 public class OrderController {
     private final OrderService orderService;
-    private final PayService payService;
 
-    public OrderController(OrderService orderService, PayService payService) {
+    public OrderController(OrderService orderService) {
         this.orderService = orderService;
-        this.payService = payService;
     }
 
     @AuthUser
@@ -45,20 +41,11 @@ public class OrderController {
         return WebResult.success();
     }
 
-    @ApiOperation(value = "支付订单", notes = "N")
-    @AuthUser
-    @PostMapping("/pay/{orderId}")
-    public String payOrder(@PathVariable("orderId") Long orderId) {
-        Result result = payService.pay(orderId);
-        return WebResult.result(result);
-    }
-
     @ApiOperation(value = "订单退款", notes = "N")
     @AuthUser
     @PostMapping("/refund/{orderId}")
     public String refund(@PathVariable("orderId") Long orderId) {
-        Result result = payService.pay(orderId);
-        return WebResult.result(result);
+        return WebResult.success();
     }
 
     @AuthUser

+ 35 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/controller/PayController.java

@@ -0,0 +1,35 @@
+package cn.reghao.tnb.content.app.mall.controller;
+
+import cn.reghao.jutil.jdk.result.Result;
+import cn.reghao.jutil.web.WebResult;
+import cn.reghao.tnb.common.auth.AuthUser;
+import cn.reghao.tnb.content.app.mall.service.PayService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author reghao
+ * @date 2024-12-05 15:25:25
+ */
+@Api(tags = "支付接口")
+@RestController
+@RequestMapping("/api/mall/pay")
+public class PayController {
+    private final PayService payService;
+
+    public PayController(PayService payService) {
+        this.payService = payService;
+    }
+
+    @ApiOperation(value = "支付订单", notes = "N")
+    @AuthUser
+    @PostMapping("/order/{orderId}")
+    public String payOrder(@PathVariable("orderId") Long orderId) {
+        Result result = payService.payOrder(orderId);
+        return WebResult.result(result);
+    }
+}

+ 2 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/db/mapper/ProductMapper.java

@@ -12,5 +12,7 @@ import org.apache.ibatis.annotations.Param;
 @Mapper
 public interface ProductMapper extends BaseMapper<Product> {
     void updateStockMinus(@Param("itemId") long itemId, @Param("num") int num);
+    void updateStockPlus(@Param("itemId") long itemId, @Param("num") int num);
+
     Product findByItemId(long itemId);
 }

+ 28 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/db/repository/MallRepository.java

@@ -3,6 +3,7 @@ package cn.reghao.tnb.content.app.mall.db.repository;
 import cn.reghao.tnb.content.app.mall.db.mapper.OrderMapper;
 import cn.reghao.tnb.content.app.mall.db.mapper.ProductMapper;
 import cn.reghao.tnb.content.app.mall.db.mapper.ProductSnapshotMapper;
+import cn.reghao.tnb.content.app.mall.model.constant.OrderStatus;
 import cn.reghao.tnb.content.app.mall.model.po.Order;
 import cn.reghao.tnb.content.app.mall.model.po.Product;
 import cn.reghao.tnb.content.app.mall.model.po.ProductSnapshot;
@@ -30,12 +31,34 @@ public class MallRepository {
         this.orderMapper = orderMapper;
     }
 
+    // TODO 需要锁来保证库存变化的准确性
     @Transactional(rollbackFor = Exception.class)
     public void saveOrder(Order order, ProductSnapshot productSnapshot) {
+        long productId = order.getProductId();
+        int amount = order.getAmount();
+        // 减商品库存
+        productMapper.updateStockMinus(productId, amount);
+
         orderMapper.save(order);
         productSnapshotMapper.save(productSnapshot);
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public void updateCancelOrder(Order order) {
+        long productId = order.getProductId();
+        int amount = order.getAmount();
+        // 恢复商品库存
+        productMapper.updateStockMinus(productId, amount);
+
+        long orderId = order.getOrderId();
+        orderMapper.updateOrderStatus(orderId, OrderStatus.cancelled.getCode());
+    }
+
+    public void updatePayOrder(long orderId) {
+        int status = OrderStatus.toConfirm.getCode();
+        orderMapper.updateOrderStatus(orderId, status);
+    }
+
     @Cacheable(cacheNames = "tnb:mall:product", key = "#itemId", unless = "#result == null")
     public Product getProduct(Long itemId) {
         log.info("miss cache");
@@ -48,4 +71,9 @@ public class MallRepository {
         OrderDetail orderDetail = new OrderDetail(order, productSnapshot);
         return orderDetail;
     }
+
+    public Order getOrder(long orderId) {
+        Order order = orderMapper.findByOrderId(orderId);
+        return order;
+    }
 }

+ 1 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/model/constant/OrderStatus.java

@@ -28,7 +28,7 @@ public enum OrderStatus {
         this.desc = desc;
     }
 
-    public Integer getCode() {
+    public int getCode() {
         return code;
     }
 

+ 9 - 9
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/rpc/MallServiceImpl.java

@@ -2,7 +2,7 @@ package cn.reghao.tnb.content.app.mall.rpc;
 
 import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.tnb.content.api.iface.MallService;
-import cn.reghao.tnb.content.app.mall.db.mapper.OrderMapper;
+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 lombok.extern.slf4j.Slf4j;
@@ -17,20 +17,20 @@ import org.springframework.stereotype.Service;
 @DubboService
 @Service
 public class MallServiceImpl implements MallService {
-    private final OrderMapper orderMapper;
+    private final MallRepository mallRepository;
 
-    public MallServiceImpl(OrderMapper orderMapper) {
-        this.orderMapper = orderMapper;
+    public MallServiceImpl(MallRepository mallRepository) {
+        this.mallRepository = mallRepository;
     }
 
     public Result orderTimeout(long orderId) {
-        Order order = orderMapper.findByOrderId(orderId);
-        int currentStatus = order.getStatus();
-        if (currentStatus == OrderStatus.toPay.getCode()) {
-            orderMapper.updateOrderStatus(orderId, OrderStatus.cancelled.getCode());
+        Order order = mallRepository.getOrder(orderId);
+        if (order != null && order.getStatus() == OrderStatus.toPay.getCode()) {
+            mallRepository.updateCancelOrder(order);
             log.info("cancel order");
+            return Result.success();
         }
 
-        return Result.success();
+        return Result.fail(String.format("order %s not exist", orderId));
     }
 }

+ 3 - 1
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/service/OrderService.java

@@ -78,7 +78,9 @@ public class OrderService {
         Order order = new Order(orderId, product.getPrice(), deliveryId, orderItem);
         ProductSnapshot productSnapshot = new ProductSnapshot(orderId, product);
         mallRepository.saveOrder(order, productSnapshot);
-        long jobId = jobService.addOrderTimeoutJob(orderId);
+
+        long timeout = 15*60;
+        long jobId = jobService.addOrderTimeoutJob(orderId, timeout);
         return orderId;
     }
 

+ 12 - 9
content/content-service/src/main/java/cn/reghao/tnb/content/app/mall/service/PayService.java

@@ -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;

+ 5 - 0
content/content-service/src/main/resources/mapper/mall/ProductMapper.xml

@@ -14,6 +14,11 @@
         set stock=stock-#{num}
         where item_id=#{itemId}
     </update>
+    <update id="updateStockPlus">
+        update mall_product
+        set stock=stock+#{num}
+        where item_id=#{itemId}
+    </update>
 
     <select id="findAll" resultType="cn.reghao.tnb.content.app.mall.model.po.Product">
         select *

+ 1 - 1
file/file-api/src/main/java/cn/reghao/file/api/iface/JobService.java

@@ -5,7 +5,7 @@ package cn.reghao.file.api.iface;
  * @date 2024-12-04 11:46:04
  */
 public interface JobService {
-    long addOrderTimeoutJob(long orderId);
+    long addOrderTimeoutJob(long orderId, long delaySecond);
     long addPublishVideoJob(String videoId, long publishAt);
     long addConvertVideoJob(String videoFileId, int channelCode);
 }

+ 1 - 2
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/JobServiceImpl.java

@@ -54,10 +54,9 @@ public class JobServiceImpl implements JobService {
     }
 
     @Override
-    public long addOrderTimeoutJob(long orderId) {
+    public long addOrderTimeoutJob(long orderId, long delaySecond) {
         long jobId = idGenerator.nextId();
         OrderTask orderTask = new OrderTask(mallService, orderId);
-        long delaySecond = 15;
         DelayJob delayJob = new DelayJob(jobId, orderTask, delaySecond);
         jobContext.addJob(delayJob);
 

+ 0 - 1
user/user-service/src/main/java/cn/reghao/tnb/user/app/rpc/UserWalletServiceImpl.java

@@ -105,7 +105,6 @@ public class UserWalletServiceImpl implements UserWalletService {
     public Result pay(long fromUserId, double amount, long toUserId) {
         double balance = getBalance(fromUserId);
         if (balance < amount) {
-            log.info("{} 的余额不足...", fromUserId);
             return Result.fail("余额不足");
         }