Bladeren bron

Merge branch 'dev'

xiongzhu 3 jaren geleden
bovenliggende
commit
40c26ae1fd

+ 3 - 0
src/main/java/com/izouma/nineth/repo/WithdrawApplyRepo.java

@@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 
 import javax.transaction.Transactional;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.List;
 
@@ -22,4 +23,6 @@ public interface WithdrawApplyRepo extends JpaRepository<WithdrawApply, Long>, J
     List<WithdrawApply> findByCreatedAtBetweenAndStatus(LocalDateTime start, LocalDateTime end, WithdrawStatus status);
 
     List<WithdrawApply> findByCreatedAtBeforeAndStatus(LocalDateTime start, WithdrawStatus status);
+
+    List<WithdrawApply> findByStatusAndAmountLessThanEqual(WithdrawStatus status, BigDecimal amount);
 }

+ 22 - 1
src/main/java/com/izouma/nineth/service/WithdrawApplyService.java

@@ -16,6 +16,7 @@ import com.izouma.nineth.repo.WithdrawApplyRepo;
 import com.izouma.nineth.utils.JpaUtils;
 import com.izouma.nineth.utils.SnowflakeIdWorker;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.redisson.api.RLock;
 import org.redisson.api.RedissonClient;
@@ -37,6 +38,7 @@ import java.util.regex.Pattern;
 
 @Service
 @AllArgsConstructor
+@Slf4j
 public class WithdrawApplyService {
 
     private WithdrawApplyRepo  withdrawApplyRepo;
@@ -96,7 +98,7 @@ public class WithdrawApplyService {
                 String withdrawCharge = sysConfigService.getString("withdraw_charge");
                 if (StringUtils.isNotEmpty(withdrawCharge) && Pattern.matches("^(\\d+|\\d+.\\d+),\\d+$", withdrawCharge)) {
                     String[] arr = withdrawCharge.split(",");
-                    chargeAmount = new BigDecimal(arr[0]);
+                    chargeAmount = apply.getAmount().multiply(new BigDecimal(arr[0]));
                     BigDecimal minChargeAmount = new BigDecimal(arr[1]);
                     if (chargeAmount.compareTo(minChargeAmount) < 0) {
                         chargeAmount = minChargeAmount;
@@ -176,4 +178,23 @@ public class WithdrawApplyService {
                     });
         }).get();
     }
+
+    @Async
+    public void fixCharge() throws ExecutionException, InterruptedException {
+        new ForkJoinPool(5).submit(() -> {
+            withdrawApplyRepo.findByStatusAndAmountLessThanEqual(WithdrawStatus.SUCCESS, new BigDecimal("1000"))
+                    .parallelStream().forEach(apply -> {
+                        try {
+                            UserBankCard bankCard = userBankCardRepo.findByUserId(apply.getUserId())
+                                    .stream().findFirst()
+                                    .orElseThrow(new BusinessException("未绑卡"));
+                            JSONObject res = sandPayService.transfer(snowflakeIdWorker.nextId() + "", bankCard.getRealName(),
+                                    bankCard.getBankNo(), new BigDecimal("6"));
+                        } catch (Exception e) {
+                            log.error("fixCharge", e);
+                        }
+                    });
+        }).get();
+
+    }
 }

+ 6 - 0
src/main/java/com/izouma/nineth/web/WithdrawApplyController.java

@@ -61,5 +61,11 @@ public class WithdrawApplyController extends BaseController {
     public void approveAll() throws ExecutionException, InterruptedException {
         withdrawApplyService.approveAll();
     }
+
+    @PreAuthorize("hasRole('ADMIN')")
+    @PostMapping("/fixCharge")
+    public void fixCharge() throws ExecutionException, InterruptedException {
+        withdrawApplyService.fixCharge();
+    }
 }
 

+ 34 - 0
src/test/java/com/izouma/nineth/service/WithdrawApplyServiceTest.java

@@ -0,0 +1,34 @@
+package com.izouma.nineth.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.izouma.nineth.ApplicationTests;
+import com.izouma.nineth.dto.UserBankCard;
+import com.izouma.nineth.enums.WithdrawStatus;
+import com.izouma.nineth.repo.UserBankCardRepo;
+import com.izouma.nineth.repo.WithdrawApplyRepo;
+import com.izouma.nineth.utils.SnowflakeIdWorker;
+import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.math.BigDecimal;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@AllArgsConstructor
+class WithdrawApplyServiceTest extends ApplicationTests {
+    private WithdrawApplyRepo withdrawApplyRepo;
+    private UserBankCardRepo  userBankCardRepo;
+    private SandPayService    sandPayService;
+    private SnowflakeIdWorker snowflakeIdWorker;
+
+    public void fixCharge() {
+        withdrawApplyRepo.findByStatusAndAmountLessThanEqual(WithdrawStatus.SUCCESS, new BigDecimal("1000"))
+                .parallelStream().forEach(apply -> {
+                    UserBankCard bankCard = userBankCardRepo.findByUserId(apply.getUserId())
+                            .stream().findFirst()
+                            .orElse(null);
+                    JSONObject res = sandPayService.transfer(snowflakeIdWorker.nextId() + "", bankCard.getRealName(),
+                            bankCard.getBankNo(), new BigDecimal("6"));
+                });
+    }
+}