package com.izouma.nineth.service;

import com.izouma.nineth.annotations.RedisLock;
import com.izouma.nineth.utils.DateTimeUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@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();
        }
    }
}
