|
|
@@ -2,6 +2,7 @@ package com.izouma.nineth.service;
|
|
|
|
|
|
import cn.hutool.core.util.ZipUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.nineth.domain.*;
|
|
|
import com.izouma.nineth.dto.SandPaySettle;
|
|
|
import com.izouma.nineth.dto.UserBankCard;
|
|
|
@@ -13,12 +14,9 @@ import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.*;
|
|
|
import com.izouma.nineth.service.storage.StorageService;
|
|
|
import com.izouma.nineth.utils.DateTimeUtils;
|
|
|
+import com.izouma.nineth.utils.SnowflakeIdWorker;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
-import org.apache.commons.collections.ListUtils;
|
|
|
-import org.apache.commons.collections.Predicate;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
@@ -30,10 +28,8 @@ import org.apache.poi.util.TempFile;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.bind.annotation.RequestPart;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.transaction.Transactional;
|
|
|
import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
@@ -44,8 +40,6 @@ import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
-import java.util.zip.ZipEntry;
|
|
|
-import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@@ -59,6 +53,9 @@ public class UserBalanceService {
|
|
|
private final SettleRecordRepo settleRecordRepo;
|
|
|
private final StorageService storageService;
|
|
|
private final ExportWithdrawRepo exportWithdrawRepo;
|
|
|
+ private final AutoWithdrawRecordRepo autoWithdrawRecordRepo;
|
|
|
+ private final SnowflakeIdWorker snowflakeIdWorker;
|
|
|
+ private final SandPayService sandPayService;
|
|
|
private final RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
public void settle(LocalDate start, LocalDate end) {
|
|
|
@@ -205,7 +202,8 @@ public class UserBalanceService {
|
|
|
"upload/" + DateTimeUtils.format(LocalDateTime.now(), "yyyyMMddHHmm") + "_" + RandomStringUtils.randomNumeric(8) + ".zip");
|
|
|
|
|
|
ExportWithdraw exportWithdraw = exportWithdrawRepo.save(new ExportWithdraw(url, remark, withdrawList.size(),
|
|
|
- withdrawList.stream().map(UserWithdraw::getAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO), "处理中"));
|
|
|
+ withdrawList.stream().map(UserWithdraw::getAmount).reduce(BigDecimal::add)
|
|
|
+ .orElse(BigDecimal.ZERO), "处理中"));
|
|
|
|
|
|
balanceList.parallelStream().forEach(userBalance -> {
|
|
|
log.info("提现userId={}", userBalance.getUserId());
|
|
|
@@ -244,7 +242,8 @@ public class UserBalanceService {
|
|
|
List<UserWithdraw> failWithdraw = new ArrayList<>();
|
|
|
for (SandPaySettle sandPaySettle : failSettleList) {
|
|
|
List<UserWithdraw> list;
|
|
|
- if (StringUtils.isNotBlank(sandPaySettle.getRemark()) && Pattern.matches("^\\d+$", sandPaySettle.getRemark().trim())) {
|
|
|
+ if (StringUtils.isNotBlank(sandPaySettle.getRemark()) && Pattern.matches("^\\d+$", sandPaySettle.getRemark()
|
|
|
+ .trim())) {
|
|
|
Long userId = Long.parseLong(sandPaySettle.getRemark().trim());
|
|
|
list = withdrawList.stream().filter(i -> i.getUserId().equals(userId))
|
|
|
.collect(Collectors.toList());
|
|
|
@@ -277,4 +276,85 @@ public class UserBalanceService {
|
|
|
.build());
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public void autoWithdraw(LocalDate date) {
|
|
|
+
|
|
|
+ autoWithdrawRecordRepo.findByDate(date).ifPresent(a -> {
|
|
|
+ throw new BusinessException("今日已经提现过");
|
|
|
+ });
|
|
|
+
|
|
|
+ List<UserBalance> list = userBalanceRepo.findByBalanceGreaterThanOrderByUserId(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.getBalance(), userBalance.getUserId(),
|
|
|
+ userBankCard.getRealName(), userBankCard.getBank());
|
|
|
+
|
|
|
+ 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)
|
|
|
+ .build());
|
|
|
+
|
|
|
+ boolean success = false;
|
|
|
+ String msg = null;
|
|
|
+ try {
|
|
|
+ JSONObject res = sandPayService.transfer(withdrawId, userBankCard.getRealName(), userBankCard.getBank(), 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|