xiongzhu 3 gadi atpakaļ
vecāks
revīzija
37db0c403c

+ 16 - 0
src/main/java/com/izouma/nineth/service/SysConfigService.java

@@ -218,6 +218,22 @@ public class SysConfigService {
                     .value("1")
                     .build());
         }
+        if (list.stream().noneMatch(i -> i.getName().equals("enable_auto_withdraw"))) {
+            sysConfigRepo.save(SysConfig.builder()
+                    .name("enable_auto_withdraw")
+                    .desc("开启自动提现")
+                    .type(SysConfig.ValueType.BOOLEAN)
+                    .value("0")
+                    .build());
+        }
+        if (list.stream().noneMatch(i -> i.getName().equals("enable_force_withdraw"))) {
+            sysConfigRepo.save(SysConfig.builder()
+                    .name("enable_force_withdraw")
+                    .desc("开启强制提现")
+                    .type(SysConfig.ValueType.BOOLEAN)
+                    .value("0")
+                    .build());
+        }
         SearchMode searchMode = SearchMode.valueOf(sysConfigRepo.findByName("default_search_mode").get().getValue());
         JpaUtils.setDefaultSearchMode(searchMode);
 

+ 67 - 0
src/main/java/com/izouma/nineth/service/WithdrawApplyScheduleService.java

@@ -0,0 +1,67 @@
+package com.izouma.nineth.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.izouma.nineth.annotations.RedisLock;
+import com.izouma.nineth.config.Constants;
+import com.izouma.nineth.domain.WithdrawApply;
+import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.dto.UserBankCard;
+import com.izouma.nineth.enums.BalanceType;
+import com.izouma.nineth.enums.WithdrawStatus;
+import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.repo.BalanceRecordRepo;
+import com.izouma.nineth.repo.UserBalanceRepo;
+import com.izouma.nineth.repo.UserBankCardRepo;
+import com.izouma.nineth.repo.WithdrawApplyRepo;
+import com.izouma.nineth.utils.DateTimeUtils;
+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;
+import org.springframework.core.env.Environment;
+import org.springframework.data.domain.Page;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.Arrays;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+
+@Service
+@AllArgsConstructor
+@Slf4j
+public class WithdrawApplyScheduleService {
+
+    private SysConfigService     sysConfigService;
+    private WithdrawApplyService withdrawApplyService;
+
+
+    @Scheduled(cron = "0 0 8 * * ?")
+    @RedisLock(value = "'scheduleApplyAll'", expire = 1, unit = TimeUnit.HOURS)
+    public void scheduleApplyAll() throws ExecutionException, InterruptedException {
+        if (sysConfigService.getBoolean("enable_force_withdraw")) {
+            withdrawApplyService.applyAll();
+        }
+    }
+
+    @Scheduled(cron = "0 0 15 * * ?")
+    @RedisLock(value = "'scheduleAutoApprove'", expire = 1, unit = TimeUnit.HOURS)
+    public void scheduleAutoApprove() throws ExecutionException, InterruptedException {
+        if (sysConfigService.getBoolean("enable_auto_withdraw")
+                && DateTimeUtils.isWorkDay(LocalDate.now())) {
+            withdrawApplyService.approveAll();
+        }
+    }
+}

+ 20 - 2
src/main/java/com/izouma/nineth/service/WithdrawApplyService.java

@@ -58,7 +58,7 @@ public class WithdrawApplyService {
         return withdrawApplyRepo.findAll(JpaUtils.toSpecification(pageQuery, WithdrawApply.class), JpaUtils.toPageRequest(pageQuery));
     }
 
-    @RedisLock("'withdrawApple'+#userId")
+    @RedisLock("'withdrawApply'+#userId")
     public WithdrawApply apply(Long userId, BigDecimal amount) {
         if (!sysConfigService.getBoolean("enable_withdraw")) {
             throw new BusinessException("提现功能暂时关闭");
@@ -201,5 +201,23 @@ public class WithdrawApplyService {
 
     }
 
-
+    @Async
+    @RedisLock(value = "'applyAll'", expire = 1, unit = TimeUnit.HOURS)
+    public void applyAll() throws ExecutionException, InterruptedException {
+        new ForkJoinPool(5).submit(() -> {
+            userBalanceRepo.findAll().parallelStream().forEach(userBalance -> {
+                LocalDateTime time = LocalDateTime.now().minusDays(14);
+                if (userBalance.getCreatedAt() != null && userBalance.getCreatedAt().isBefore(time)
+                        && userBalance.getBalance().compareTo(new BigDecimal("100")) >= 0
+                        && withdrawApplyRepo.countByUserIdAndCreatedAtAfter(userBalance.getUserId(), time) <= 0) {
+                    log.info("apply withdraw for user {}", userBalance.getUserId());
+                    try {
+                        apply(userBalance.getUserId(), userBalance.getBalance());
+                    } catch (Exception e) {
+                        log.error("apply withdraw for user {}", userBalance.getUserId(), e);
+                    }
+                }
+            });
+        }).get();
+    }
 }

+ 31 - 2
src/main/java/com/izouma/nineth/utils/DateTimeUtils.java

@@ -1,5 +1,11 @@
 package com.izouma.nineth.utils;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.github.kevinsawicki.http.HttpRequest;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -7,9 +13,9 @@ import java.time.format.DateTimeFormatter;
 import java.time.temporal.TemporalAccessor;
 
 public class DateTimeUtils {
-    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
+    public static final String DATE_TIME_FORMAT   = "yyyy-MM-dd HH:mm";
     public static final String T_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
-    public static final String DATE_FORMAT      = "yyyy-MM-dd";
+    public static final String DATE_FORMAT        = "yyyy-MM-dd";
 
     public static LocalDate toLocalDate(long ts) {
         if (String.valueOf(ts).length() > 10) {
@@ -42,4 +48,27 @@ public class DateTimeUtils {
     public static long toTimestamp(TemporalAccessor temporal) {
         return Instant.from(temporal).toEpochMilli();
     }
+
+    public static boolean isWorkDay(LocalDate date) {
+        try {
+            HttpRequest request = HttpRequest.get("https://jiejiari.market.alicloudapi.com/queryHoliday?day="
+                            + date.format(DateTimeFormatter.ofPattern("yyyyMMdd")))
+                    .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe");
+            int code = request.code();
+            if (code == 200) {
+                String body = request.body();
+                JSONObject res = JSON.parseObject(body);
+                System.out.println(JSON.toJSONString(res, true));
+                if (res.getInteger("showapi_res_code") == 0) {
+                    JSONObject resBody = res.getJSONObject("showapi_res_body");
+                    return StringUtils.equals(resBody.getString("type"), "1");
+                }
+            } else {
+                System.out.println(code);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return true;
+    }
 }

+ 2 - 12
src/main/java/com/izouma/nineth/web/WithdrawApplyController.java

@@ -30,7 +30,6 @@ import java.util.concurrent.ExecutionException;
 public class WithdrawApplyController extends BaseController {
     private WithdrawApplyService withdrawApplyService;
     private WithdrawApplyRepo    withdrawApplyRepo;
-    private UserBalanceRepo      userBalanceRepo;
 
     @PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/all")
@@ -76,17 +75,8 @@ public class WithdrawApplyController extends BaseController {
 
     @PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/applyAll")
-    @Scheduled(cron = "0 0 8 * * ?")
-    public void applyAll() {
-        userBalanceRepo.findAll().parallelStream().forEach(userBalance -> {
-            LocalDateTime time = LocalDateTime.now().minusDays(14);
-            if (userBalance.getCreatedAt() != null && userBalance.getCreatedAt().isBefore(time)
-                    && userBalance.getBalance().compareTo(new BigDecimal("100")) >= 0
-                    && withdrawApplyRepo.countByUserIdAndCreatedAtAfter(userBalance.getUserId(), time) <= 0) {
-                log.info("apply withdraw for user {}", userBalance.getUserId());
-                withdrawApplyService.apply(userBalance.getUserId(), userBalance.getBalance());
-            }
-        });
+    public void applyAll() throws ExecutionException, InterruptedException {
+        withdrawApplyService.applyAll();
     }
 }