Browse Source

下订单限制

qzyReal 3 năm trước cách đây
mục cha
commit
a503006ac7

+ 17 - 8
sp-server/src/main/java/com/pj/api/jh/service/JhService.java

@@ -79,9 +79,18 @@ public class JhService {
         String businessType = request.getParameter("businessType");
         Attach atchMap = new Attach();
         atchMap.setC(c).setB(businessId).setA(a);
+        String attchStr = JSONUtil.toJsonStr(atchMap);
+        TbOrder dbOrder = tbOrderService.find30Seconds(attchStr, LocalDateTime.now().minusSeconds(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+        if (dbOrder!=null&&!StrUtil.equals(dbOrder.getOpenid(),openid)){
+            throw new Exception("其他人正在付款....");
+        }
+        TbOrder successOrder=tbOrderService.findSuccessOrder(attchStr);
+        if (successOrder!=null){
+            throw new Exception("业务订单已支付....");
+        }
         String out_trade_no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + RandomUtil.randomNumbers(6);
         Map<String, Object> params = new HashMap<>();
-       // params.put("attach", JSONUtil.toJsonStr(atchMap));
+        // params.put("attach", JSONUtil.toJsonStr(atchMap));
         params.put("productType", jhConfig.getProductType());
         params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo());
         params.put("openId", openid);
@@ -115,7 +124,7 @@ public class JhService {
 
             object.set("outTradeNo", out_trade_no);
             TbOrder tbOrder = new TbOrder();
-            tbOrder.setAttach(JSONUtil.toJsonStr(atchMap))
+            tbOrder.setAttach(attchStr)
                     .setOpenid(openid)
                     .setOrderTime(new Date())
                     .setOutTradeNo(out_trade_no).setPrice(money);
@@ -124,15 +133,15 @@ public class JhService {
             return BeanUtil.beanToMap(object);
         }
         throw new Exception("生成订单失败");
-      //  return new HashMap<>();
+        //  return new HashMap<>();
     }
 
-    private    String handlerDesc(String desc){
-        if (StrUtil.isEmpty(desc)){
+    private String handlerDesc(String desc) {
+        if (StrUtil.isEmpty(desc)) {
             return "";
         }
-        if (desc.getBytes(Charset.forName("utf-8")).length>128){
-            desc=StrUtil.sub(desc,0,desc.lastIndexOf("-"));
+        if (desc.getBytes(Charset.forName("utf-8")).length > 128) {
+            desc = StrUtil.sub(desc, 0, desc.lastIndexOf("-"));
             handlerDesc(desc);
         }
         return desc;
@@ -160,7 +169,7 @@ public class JhService {
     public void notifyResult(JhNotifyBO bo) {
         String orderStatus = bo.getTradeStatus();
         if (!"SUCCESS".equals(orderStatus) && !"FINISH".equals(orderStatus)) {
-            log.error("支付订单回调失败:{}",JSONUtil.toJsonStr(bo));
+            log.error("支付订单回调失败:{}", JSONUtil.toJsonStr(bo));
             return;
         }
         NotifyBO notifyBO = new NotifyBO();

+ 1 - 0
sp-server/src/main/java/com/pj/api/open/service/OpenService.java

@@ -191,6 +191,7 @@ public class OpenService {
             if (dif > 0) {
                 RedisUtil.setByMINUTES(channel, carNo, 1);
                 log.error("请求返回:请缴停车费:{},{}元", carNo, dif);
+                //todo 异步扣费
                 return ResultJson.error("请缴停车费" + dif + "元");
             } else {
                 freeOut(tbBusinessCar, now, channel, image);

+ 23 - 0
sp-server/src/main/java/com/pj/project/tb_order/TbOrderService.java

@@ -94,4 +94,27 @@ public class TbOrderService extends ServiceImpl<TbOrderMapper, TbOrder> implemen
         ew.eq("order_status", "SUCCESS");
         return getOne(ew);
     }
+
+    /**
+     * 30秒内最新的一条
+     * @param attchStr
+     * @param orderTime
+     * @return
+     */
+    public TbOrder find30Seconds(String attchStr, String orderTime) {
+        QueryWrapper<TbOrder> ew = new QueryWrapper<>();
+        ew.lambda().eq(TbOrder::getAttach,attchStr).orderByDesc(TbOrder::getOrderTime);
+        ew.ge("date_format(order_time,'%Y-%m-%d %H:%i:%s')",orderTime);
+        List<TbOrder>list=list(ew);
+        return list.isEmpty()?null:list.get(0);
+    }
+
+    public TbOrder findSuccessOrder(String attchStr) {
+        QueryWrapper<TbOrder> ew = new QueryWrapper<>();
+        ew.lambda().eq(TbOrder::getAttach,attchStr)
+                .eq(TbOrder::getOrderStatus,"SUCCESS")
+                .orderByDesc(TbOrder::getOrderTime);
+        List<TbOrder>list=list(ew);
+        return list.isEmpty()?null:list.get(0);
+    }
 }

+ 2 - 2
sp-server/src/main/resources/application-dev.yml

@@ -6,9 +6,9 @@ spring:
     # 数据源配置
     datasource:
         type: com.alibaba.druid.pool.DruidDataSource
-        url: jdbc:mysql://127.0.0.1:3307/pco0815?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
+        url: jdbc:mysql://47.101.143.145:3306/pco0815?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
         username: root
-        password: 1234
+        password: 1qaz@WSX
         # 是否打开sql监控台  (生产环境请务必关闭此选项)
         druid:
             stat-view-servlet: