|
|
@@ -1,6 +1,7 @@
|
|
|
package com.izouma.zhumj.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.github.benmanes.caffeine.cache.Cache;
|
|
|
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
|
|
import com.izouma.zhumj.config.Constants;
|
|
|
import com.izouma.zhumj.domain.*;
|
|
|
@@ -14,6 +15,8 @@ import com.izouma.zhumj.service.ammeter.AmmeterApi;
|
|
|
import jodd.util.StringUtil;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
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.event.EventListener;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
@@ -27,6 +30,7 @@ import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.Semaphore;
|
|
|
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
@@ -42,12 +46,17 @@ public class RechargeRecordService {
|
|
|
private final PersonalFeeTypeRepo personalFeeTypeRepo;
|
|
|
private final PersonalFeeRepo personalFeeRepo;
|
|
|
private final BedInfoRepo bedInfoRepo;
|
|
|
+ private final Cache<String, Object> caffeineCache;
|
|
|
|
|
|
@Transactional
|
|
|
public void handleRechargeNotify(WxPayOrderNotifyResult notifyResult) {
|
|
|
if (rechargeRecordRepo.findByTransactionId(notifyResult.getTransactionId()) != null) {
|
|
|
return;
|
|
|
}
|
|
|
+ Long transactionId = (Long) caffeineCache.getIfPresent(notifyResult.getTransactionId());
|
|
|
+ if (transactionId != null) {
|
|
|
+ throw new BusinessException("正在处理中");
|
|
|
+ }
|
|
|
JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
|
|
|
RechargeType type = RechargeType.valueOf(attach.getString(Constants.ATTACH_RECHARGE_TYPE));
|
|
|
Long userId = attach.getLong(Constants.ATTACH_USER_ID);
|
|
|
@@ -103,9 +112,13 @@ public class RechargeRecordService {
|
|
|
if (roomInfo.getAmmeterType() == null || StringUtil.isEmpty(roomInfo.getAmmeterId())) {
|
|
|
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(), "电费");
|
|
|
if (feeType != null) {
|
|
|
personalFeeRepo.save(PersonalFee.builder()
|
|
|
@@ -126,6 +139,7 @@ public class RechargeRecordService {
|
|
|
.enabled(true)
|
|
|
.build());
|
|
|
}
|
|
|
+ caffeineCache.invalidate(notifyResult.getTransactionId());
|
|
|
}
|
|
|
}
|
|
|
|