wangqifan před 4 roky
rodič
revize
1090c8734d

+ 5 - 0
pom.xml

@@ -398,6 +398,11 @@
             <artifactId>commons-httpclient</artifactId>
             <artifactId>commons-httpclient</artifactId>
             <version>3.1</version>
             <version>3.1</version>
         </dependency>
         </dependency>
+
+        <dependency>
+            <groupId>com.github.ben-manes.caffeine</groupId>
+            <artifactId>caffeine</artifactId>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
 </project>
 </project>

+ 32 - 0
src/main/java/com/izouma/zhumj/config/CacheConfig.java

@@ -0,0 +1,32 @@
+package com.izouma.zhumj.config;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.support.SimpleCacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.TimeUnit;
+
+@EnableCaching
+@Configuration
+public class CacheConfig {
+    @Bean
+    public CacheManager cacheManager() {
+        SimpleCacheManager manager = new SimpleCacheManager();
+        return manager;
+    }
+
+    @Bean
+    public Cache<String, Object> caffeineCache() {
+        return Caffeine.newBuilder()
+                // 设置最后一次写入或访问后经过固定时间过期
+                .expireAfterWrite(4, TimeUnit.HOURS)
+                // 初始的缓存空间大小
+                .initialCapacity(100)
+                // 缓存的最大条数
+                .build();
+    }
+}

+ 17 - 3
src/main/java/com/izouma/zhumj/service/RechargeRecordService.java

@@ -1,6 +1,7 @@
 package com.izouma.zhumj.service;
 package com.izouma.zhumj.service;
 
 
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
+import com.github.benmanes.caffeine.cache.Cache;
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
 import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
 import com.izouma.zhumj.config.Constants;
 import com.izouma.zhumj.config.Constants;
 import com.izouma.zhumj.domain.*;
 import com.izouma.zhumj.domain.*;
@@ -14,6 +15,8 @@ import com.izouma.zhumj.service.ammeter.AmmeterApi;
 import jodd.util.StringUtil;
 import jodd.util.StringUtil;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.event.EventListener;
 import org.springframework.context.event.EventListener;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
@@ -27,6 +30,7 @@ import java.time.LocalDateTime;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Optional;
+import java.util.concurrent.Semaphore;
 
 
 @Service
 @Service
 @AllArgsConstructor
 @AllArgsConstructor
@@ -42,12 +46,17 @@ public class RechargeRecordService {
     private final PersonalFeeTypeRepo    personalFeeTypeRepo;
     private final PersonalFeeTypeRepo    personalFeeTypeRepo;
     private final PersonalFeeRepo        personalFeeRepo;
     private final PersonalFeeRepo        personalFeeRepo;
     private final BedInfoRepo            bedInfoRepo;
     private final BedInfoRepo            bedInfoRepo;
+    private final Cache<String, Object>  caffeineCache;
 
 
     @Transactional
     @Transactional
     public void handleRechargeNotify(WxPayOrderNotifyResult notifyResult) {
     public void handleRechargeNotify(WxPayOrderNotifyResult notifyResult) {
         if (rechargeRecordRepo.findByTransactionId(notifyResult.getTransactionId()) != null) {
         if (rechargeRecordRepo.findByTransactionId(notifyResult.getTransactionId()) != null) {
             return;
             return;
         }
         }
+        Long transactionId = (Long) caffeineCache.getIfPresent(notifyResult.getTransactionId());
+        if (transactionId != null) {
+            throw new BusinessException("正在处理中");
+        }
         JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
         JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
         RechargeType type = RechargeType.valueOf(attach.getString(Constants.ATTACH_RECHARGE_TYPE));
         RechargeType type = RechargeType.valueOf(attach.getString(Constants.ATTACH_RECHARGE_TYPE));
         Long userId = attach.getLong(Constants.ATTACH_USER_ID);
         Long userId = attach.getLong(Constants.ATTACH_USER_ID);
@@ -103,9 +112,13 @@ public class RechargeRecordService {
             if (roomInfo.getAmmeterType() == null || StringUtil.isEmpty(roomInfo.getAmmeterId())) {
             if (roomInfo.getAmmeterType() == null || StringUtil.isEmpty(roomInfo.getAmmeterId())) {
                 throw new BusinessException("此房间没有绑定电表");
                 throw new BusinessException("此房间没有绑定电表");
             }
             }
-            AmmeterApi ammeterApi = (AmmeterApi) context.getBean(roomInfo.getAmmeterType().name());
-            ammeterApi.recharge(roomInfo.getAmmeterId(), amount);
-
+            try {
+                AmmeterApi ammeterApi = (AmmeterApi) context.getBean(roomInfo.getAmmeterType().name());
+                caffeineCache.put(notifyResult.getTransactionId(), notifyResult.getTransactionId());
+                ammeterApi.recharge(roomInfo.getAmmeterId(), amount);
+            } catch (Exception e) {
+                caffeineCache.invalidate(notifyResult.getTransactionId());
+            }
             PersonalFeeType feeType = personalFeeTypeRepo.findFirstByStoreIdAndName(checkinInfo.getStoreId(), "电费");
             PersonalFeeType feeType = personalFeeTypeRepo.findFirstByStoreIdAndName(checkinInfo.getStoreId(), "电费");
             if (feeType != null) {
             if (feeType != null) {
                 personalFeeRepo.save(PersonalFee.builder()
                 personalFeeRepo.save(PersonalFee.builder()
@@ -126,6 +139,7 @@ public class RechargeRecordService {
                         .enabled(true)
                         .enabled(true)
                         .build());
                         .build());
             }
             }
+            caffeineCache.invalidate(notifyResult.getTransactionId());
         }
         }
     }
     }