xiongzhu 4 роки тому
батько
коміт
241fbaa850

+ 5 - 0
src/main/java/com/izouma/nineth/domain/ExportWithdraw.java

@@ -6,6 +6,7 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import javax.persistence.Entity;
+import java.math.BigDecimal;
 
 @Data
 @Entity
@@ -18,5 +19,9 @@ public class ExportWithdraw extends BaseEntity {
 
     String remark;
 
+    Integer total;
 
+    BigDecimal sum;
+
+    String status;
 }

+ 5 - 0
src/main/java/com/izouma/nineth/dto/UserBankCard.java

@@ -7,9 +7,14 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import javax.persistence.Entity;
+import javax.persistence.Index;
+import javax.persistence.Table;
 
 @Data
 @Entity
+@Table(indexes = {
+        @Index(columnList = "userId")
+})
 @AllArgsConstructor
 @NoArgsConstructor
 @Builder

+ 35 - 23
src/main/java/com/izouma/nineth/service/UserBalanceService.java

@@ -27,6 +27,7 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.usermodel.WorkbookFactory;
 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;
@@ -50,14 +51,15 @@ import java.util.zip.ZipOutputStream;
 @Slf4j
 @AllArgsConstructor
 public class UserBalanceService {
-    private final UserBalanceRepo    userBalanceRepo;
-    private final BalanceRecordRepo  balanceRecordRepo;
-    private final OrderRepo          orderRepo;
-    private final AssetRepo          assetRepo;
-    private final UserBankCardRepo   userBankCardRepo;
-    private final SettleRecordRepo   settleRecordRepo;
-    private final StorageService     storageService;
-    private final ExportWithdrawRepo exportWithdrawRepo;
+    private final UserBalanceRepo               userBalanceRepo;
+    private final BalanceRecordRepo             balanceRecordRepo;
+    private final OrderRepo                     orderRepo;
+    private final AssetRepo                     assetRepo;
+    private final UserBankCardRepo              userBankCardRepo;
+    private final SettleRecordRepo              settleRecordRepo;
+    private final StorageService                storageService;
+    private final ExportWithdrawRepo            exportWithdrawRepo;
+    private final RedisTemplate<String, Object> redisTemplate;
 
     public void settle(LocalDate start, LocalDate end) {
         for (long i = 0; i <= ChronoUnit.DAYS.between(start, end); i++) {
@@ -144,13 +146,24 @@ public class UserBalanceService {
     @Transactional
     public ExportWithdraw exportWithdraw(String remark) throws IOException, InvalidFormatException {
         List<UserBalance> balanceList = userBalanceRepo.findByBalanceGreaterThan(BigDecimal.ZERO);
+        List<UserBankCard> userBankCardList = balanceList.isEmpty() ? new ArrayList<>() :
+                userBankCardRepo.findByUserIdIn(balanceList.stream().map(UserBalance::getUserId)
+                        .collect(Collectors.toSet()));
         List<UserWithdraw> withdrawList = new ArrayList<>();
-        balanceList.forEach(userBalance -> {
-            log.info("提现查询银行卡userId={}", userBalance.getUserId());
-            userBankCardRepo.findByUserId(userBalance.getUserId()).stream().findFirst()
-                    .ifPresent(userBankCard -> withdrawList.add(new UserWithdraw(userBalance.getUserId(), userBankCard.getRealName(),
-                            userBankCard.getBankNo(), userBalance.getBalance())));
-        });
+        Iterator<UserBalance> it = balanceList.iterator();
+        UserBalance ub;
+        while (it.hasNext()) {
+            ub = it.next();
+            Long userId = ub.getUserId();
+            log.info("查询提现银行卡userId={}", ub.getUserId());
+            UserBankCard ubc = userBankCardList.stream().filter(u -> u.getUserId().equals(userId))
+                    .findFirst().orElse(null);
+            if (ubc != null) {
+                withdrawList.add(new UserWithdraw(userId, ubc.getRealName(), ubc.getBankNo(), ub.getBalance()));
+            } else {
+                it.remove();
+            }
+        }
 
         File tmpDir = TempFile.createTempDirectory("export_" + RandomStringUtils.randomAlphabetic(8));
         File file1 = new File(tmpDir, DateTimeUtils.format(LocalDate.now(), "yyyyMMdd") + "结算.xlsx");
@@ -191,12 +204,11 @@ public class UserBalanceService {
         String url = storageService.uploadFromInputStream(new FileInputStream(zipFile),
                 "upload/" + DateTimeUtils.format(LocalDateTime.now(), "yyyyMMddHHmm") + "_" + RandomStringUtils.randomNumeric(8) + ".zip");
 
-        ExportWithdraw exportWithdraw = exportWithdrawRepo.save(new ExportWithdraw(url, remark));
-        for (UserWithdraw withdraw : withdrawList) {
-            log.info("提现userId={}", withdraw.getUserId());
-            UserBalance userBalance = balanceList.stream()
-                    .filter(i -> i.getUserId().equals(withdraw.getUserId()))
-                    .findFirst().get();
+        ExportWithdraw exportWithdraw = exportWithdrawRepo.save(new ExportWithdraw(url, remark, withdrawList.size(),
+                withdrawList.stream().map(UserWithdraw::getAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO), "处理中"));
+
+        balanceList.parallelStream().forEach(userBalance -> {
+            log.info("提现userId={}", userBalance.getUserId());
             BigDecimal amount = userBalance.getBalance();
             userBalance.setLastBalance(userBalance.getBalance());
             userBalance.setBalance(BigDecimal.ZERO);
@@ -210,7 +222,7 @@ public class UserBalanceService {
                     .lastBalance(userBalance.getLastBalance())
                     .type(BalanceType.WITHDRAW)
                     .build());
-        }
+        });
 
         tmpDir.delete();
         zipFile.delete();
@@ -248,7 +260,7 @@ public class UserBalanceService {
                 failWithdraw.add(list.get(0));
             }
         }
-        for (UserWithdraw withdraw : failWithdraw) {
+        failWithdraw.parallelStream().forEach(withdraw -> {
             UserBalance userBalance = userBalanceRepo.findById(withdraw.getUserId())
                     .orElse(new UserBalance(withdraw.getUserId(), BigDecimal.ZERO, BigDecimal.ZERO));
             userBalance.setLastBalance(userBalance.getBalance());
@@ -263,6 +275,6 @@ public class UserBalanceService {
                     .lastBalance(userBalance.getLastBalance())
                     .type(BalanceType.RETURN)
                     .build());
-        }
+        });
     }
 }