瀏覽代碼

并发处理

yq 3 年之前
父節點
當前提交
04b6eb34b4

+ 13 - 0
src/main/java/com/usky/dxtop/controller/web/business/TestController.java

@@ -2,7 +2,9 @@ package com.usky.dxtop.controller.web.business;
 
 import com.alibaba.fastjson.JSONObject;
 import com.usky.dxtop.common.utils.http.HttpUtils;
+import com.usky.dxtop.model.Order;
 import com.usky.dxtop.model.Staff;
+import com.usky.dxtop.service.OrderService;
 import com.usky.dxtop.service.StaffService;
 import com.usky.dxtop.service.api.OneCardApi;
 import com.usky.dxtop.service.api.SmApi;
@@ -25,6 +27,9 @@ public class TestController {
     @Autowired
     private StaffService staffService;
 
+    @Autowired
+    private OrderService orderService;
+
     @GetMapping("/cardLoss")
     public String testCardLoss(@RequestParam String cid, @RequestParam String cno, @RequestParam String msg){
         TreeMap<String, String> param = OneCardApi.cardLoss(cid, cno, msg);
@@ -49,4 +54,12 @@ public class TestController {
         staffService.personSendMessage(staff,type);
         return "";
     }
+
+
+    @GetMapping("/orderSuccess")
+    public String testOrderSuccess(@RequestParam Long orderId){
+        Order byId = orderService.getById(orderId);
+        orderService.paySuccess(byId);
+        return "";
+    }
 }

+ 38 - 14
src/main/java/com/usky/dxtop/service/impl/OrderServiceImpl.java

@@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.sun.org.apache.xpath.internal.operations.Bool;
 import com.usky.dxtop.common.core.domain.model.LoginUser;
 import com.usky.dxtop.common.core.page.CommonPage;
 import com.usky.dxtop.common.exception.CustomException;
@@ -41,12 +42,16 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
 import java.util.stream.Collectors;
 
 /**
@@ -95,6 +100,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
     @Autowired
     private ISysConfigService sysConfigService;
 
+    private Map<String, String> map = new ConcurrentHashMap<>();
+
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean add(Order order) {
@@ -201,7 +208,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
             msgLog.setExchange(ChargeConsumeConfig.NAME);
             msgLog.setRoutingKey(ChargeConsumeConfig.NAME);
             msgLog.setMsg(JSON.toJSONString(chargeVo));
-            msgLogService.addOrUpdate(msgLog);
+            msgLog.setId(System.currentTimeMillis());
+            msgLog.setCreateTime(new Date());
+            msgLogService.save(msgLog);
             chargeVo.setSeq(msgLog.getId());
             CorrelationData correlationData = new CorrelationData(msgLog.getId().toString());
             rabbitTemplate.convertAndSend(msgLog.getExchange(), msgLog.getRoutingKey(), chargeVo,correlationData);
@@ -224,12 +233,36 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
      * @return
      */
     public boolean isCheckOrder(String orderNumber){
-        Order order = one(orderNumber);
-        //订单是未支付和支付中需要处理
-        return OrderStatus.NO_PAYMENT.getCode().equals(order.getOrderFlag()) || OrderStatus.AWAIT_PAY.getCode().equals(order.getOrderFlag());
+        if (map.putIfAbsent(orderNumber, orderNumber) != null){
+            return false;
+        }
+        try {
+            Order order = one(orderNumber);
+            //订单是未支付和支付中需要处理
+            boolean isCheck = OrderStatus.NO_PAYMENT.getCode().equals(order.getOrderFlag()) || OrderStatus.AWAIT_PAY.getCode().equals(order.getOrderFlag());
+            if (isCheck){
+                //判断是不是游客充值
+                if (null == order.getUserId() || 0 == order.getUserId()){
+                    order.setOrderFlag(OrderStatus.COMPLETE.getCode());
+                }else {
+                    //调用卡充值api
+                    callCardTopApi(order);
+                }
+                this.updateById(order);
+            }
+            return isCheck;
+        }catch (Exception e){
+            log.error("订单查询异常"+e);
+            throw new CustomException("订单查询异常");
+        }finally {
+            map.remove(orderNumber);
+        }
+
     }
 
 
+
+
     /**
      * 查询交易后处理订单
      * @param order
@@ -270,16 +303,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
     @Transactional(rollbackFor = Exception.class)
     @Override
     public void paySuccess(Order order){
-        if (isCheckOrder(order.getOrderNumber())){
-            //判断是不是游客充值
-            if (null == order.getUserId() || 0 == order.getUserId()){
-                order.setOrderFlag(OrderStatus.COMPLETE.getCode());
-            }else {
-                //调用卡充值api
-                callCardTopApi(order);
-            }
-            this.updateById(order);
-        }
+        isCheckOrder(order.getOrderNumber());
     }