|
|
@@ -284,102 +284,102 @@ public class UserBalanceService {
|
|
|
}
|
|
|
|
|
|
@Async
|
|
|
- public void autoWithdraw(LocalDate date) {
|
|
|
+ public void autoWithdraw(LocalDate date) throws ExecutionException, InterruptedException {
|
|
|
+ ForkJoinPool customThreadPool = new ForkJoinPool(2);
|
|
|
+ customThreadPool.submit(() -> {
|
|
|
+ autoWithdrawRecordRepo.findByDate(date).ifPresent(a -> {
|
|
|
+ throw new BusinessException("今日已经提现过");
|
|
|
+ });
|
|
|
|
|
|
- autoWithdrawRecordRepo.findByDate(date).ifPresent(a -> {
|
|
|
- throw new BusinessException("今日已经提现过");
|
|
|
- });
|
|
|
+ List<UserBalance> list = userBalanceRepo.findByLockedFalseAndBalanceGreaterThanOrderByUserId(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ AutoWithdrawRecord record = AutoWithdrawRecord.builder()
|
|
|
+ .date(LocalDate.now())
|
|
|
+ .status("pending")
|
|
|
+ .progress(0)
|
|
|
+ .total(list.size())
|
|
|
+ .build();
|
|
|
+ autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
+
|
|
|
+ list.parallelStream().forEach(userBalance -> {
|
|
|
+ UserBankCard userBankCard = userBankCardRepo.findByUserId(userBalance.getUserId())
|
|
|
+ .stream().findFirst().orElse(null);
|
|
|
+ if (userBankCard == null) {
|
|
|
+ log.info("自动提现userId={}, amount={}, 未绑卡", userBalance.getBalance(), userBalance.getUserId());
|
|
|
+ record.setProgress(record.getProgress() + 1);
|
|
|
+ record.setCurrentUserId(userBalance.getUserId());
|
|
|
+ autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
+ } else {
|
|
|
+ log.info("自动提现userId={}, amount={}, name={}, bank={}",
|
|
|
+ userBalance.getUserId(), userBalance.getBalance(),
|
|
|
+ userBankCard.getRealName(), userBankCard.getBankNo());
|
|
|
|
|
|
- List<UserBalance> list = userBalanceRepo.findByLockedFalseAndBalanceGreaterThanOrderByUserId(BigDecimal.ZERO);
|
|
|
-
|
|
|
- AutoWithdrawRecord record = AutoWithdrawRecord.builder()
|
|
|
- .date(LocalDate.now())
|
|
|
- .status("pending")
|
|
|
- .progress(0)
|
|
|
- .total(list.size())
|
|
|
- .build();
|
|
|
- autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
-
|
|
|
- for (UserBalance userBalance : list) {
|
|
|
- UserBankCard userBankCard = userBankCardRepo.findByUserId(userBalance.getUserId())
|
|
|
- .stream().findFirst().orElse(null);
|
|
|
- if (userBankCard == null) {
|
|
|
- log.info("自动提现userId={}, amount={}, 未绑卡", userBalance.getBalance(), userBalance.getUserId());
|
|
|
- record.setProgress(record.getProgress() + 1);
|
|
|
- record.setCurrentUserId(userBalance.getUserId());
|
|
|
- autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
- } else {
|
|
|
- log.info("自动提现userId={}, amount={}, name={}, bank={}",
|
|
|
- userBalance.getUserId(), userBalance.getBalance(),
|
|
|
- userBankCard.getRealName(), userBankCard.getBankNo());
|
|
|
-
|
|
|
- String withdrawId = snowflakeIdWorker.nextId() + "";
|
|
|
-
|
|
|
- BigDecimal amount = userBalance.getBalance();
|
|
|
- userBalance.setLastBalance(userBalance.getBalance());
|
|
|
- userBalance.setBalance(BigDecimal.ZERO);
|
|
|
- userBalanceRepo.saveAndFlush(userBalance);
|
|
|
-
|
|
|
- balanceRecordRepo.save(BalanceRecord.builder()
|
|
|
- .time(LocalDateTime.now())
|
|
|
- .userId(userBalance.getUserId())
|
|
|
- .amount(amount.negate())
|
|
|
- .balance(BigDecimal.ZERO)
|
|
|
- .lastBalance(userBalance.getLastBalance())
|
|
|
- .type(BalanceType.WITHDRAW)
|
|
|
- .withdrawId(withdrawId)
|
|
|
- .build());
|
|
|
-
|
|
|
- boolean success = false;
|
|
|
- String msg = null;
|
|
|
- try {
|
|
|
- JSONObject res = sandPayService.transfer(withdrawId, userBankCard.getRealName(), userBankCard.getBankNo(), amount);
|
|
|
- if ("0000".equals(res.getString("respCode"))) {
|
|
|
- success = true;
|
|
|
- } else {
|
|
|
- msg = res.getString("respDesc");
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- msg = e.getMessage();
|
|
|
- }
|
|
|
+ String withdrawId = snowflakeIdWorker.nextId() + "";
|
|
|
|
|
|
- if (!success) {
|
|
|
+ BigDecimal amount = userBalance.getBalance();
|
|
|
userBalance.setLastBalance(userBalance.getBalance());
|
|
|
- userBalance.setBalance(userBalance.getBalance().add(amount));
|
|
|
+ userBalance.setBalance(BigDecimal.ZERO);
|
|
|
userBalanceRepo.saveAndFlush(userBalance);
|
|
|
|
|
|
balanceRecordRepo.save(BalanceRecord.builder()
|
|
|
.time(LocalDateTime.now())
|
|
|
.userId(userBalance.getUserId())
|
|
|
- .amount(amount)
|
|
|
- .balance(userBalance.getBalance())
|
|
|
+ .amount(amount.negate())
|
|
|
+ .balance(BigDecimal.ZERO)
|
|
|
.lastBalance(userBalance.getLastBalance())
|
|
|
- .type(BalanceType.RETURN)
|
|
|
+ .type(BalanceType.WITHDRAW)
|
|
|
.withdrawId(withdrawId)
|
|
|
- .remark(msg)
|
|
|
.build());
|
|
|
- }
|
|
|
|
|
|
- record.setProgress(record.getProgress() + 1);
|
|
|
- record.setCurrentUserId(userBalance.getUserId());
|
|
|
- autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
+ boolean success = false;
|
|
|
+ String msg = null;
|
|
|
+ try {
|
|
|
+ JSONObject res = sandPayService.transfer(withdrawId, userBankCard.getRealName(), userBankCard.getBankNo(), amount);
|
|
|
+ if ("0000".equals(res.getString("respCode"))) {
|
|
|
+ success = true;
|
|
|
+ } else {
|
|
|
+ msg = res.getString("respDesc");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ msg = e.getMessage();
|
|
|
+ }
|
|
|
|
|
|
+ if (!success) {
|
|
|
+ userBalance.setLastBalance(userBalance.getBalance());
|
|
|
+ userBalance.setBalance(userBalance.getBalance().add(amount));
|
|
|
+ userBalanceRepo.saveAndFlush(userBalance);
|
|
|
+
|
|
|
+ balanceRecordRepo.save(BalanceRecord.builder()
|
|
|
+ .time(LocalDateTime.now())
|
|
|
+ .userId(userBalance.getUserId())
|
|
|
+ .amount(amount)
|
|
|
+ .balance(userBalance.getBalance())
|
|
|
+ .lastBalance(userBalance.getLastBalance())
|
|
|
+ .type(BalanceType.RETURN)
|
|
|
+ .withdrawId(withdrawId)
|
|
|
+ .remark(msg)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ record.setProgress(record.getProgress() + 1);
|
|
|
+ record.setCurrentUserId(userBalance.getUserId());
|
|
|
+ autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
|
|
|
- if (!success) {
|
|
|
- msg = msg == null ? "" : msg;
|
|
|
- if (msg.contains("风控") || Pattern.matches(".*\\[.*\\]", msg)) {
|
|
|
- userBalance.setLocked(true);
|
|
|
- userBalance.setLockReason(msg);
|
|
|
- userBalance.setLockTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ if (!success) {
|
|
|
+ msg = msg == null ? "" : msg;
|
|
|
+ if (msg.contains("风控") || Pattern.matches(".*\\[.*\\]", msg)) {
|
|
|
+ userBalance.setLocked(true);
|
|
|
+ userBalance.setLockReason(msg);
|
|
|
+ userBalance.setLockTime(LocalDateTime.now());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- record.setStatus("finish");
|
|
|
- autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
+ });
|
|
|
|
|
|
- redisTemplate.delete("autoWithdraw::" + DateTimeUtils.format(date, "yyyyMMdd"));
|
|
|
+ record.setStatus("finish");
|
|
|
+ autoWithdrawRecordRepo.saveAndFlush(record);
|
|
|
+ }).get();
|
|
|
}
|
|
|
|
|
|
public void revert() throws ExecutionException, InterruptedException {
|