package com.izouma.nineth.service.systemlog;

import com.izouma.nineth.domain.SystemInfoLog;
import com.izouma.nineth.repo.SystemLogRepo;
import com.izouma.nineth.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

@AllArgsConstructor
@Service
@Slf4j
public class SystemLogService {
    private final UserService userService;
    private final UserFundAccountService userFundAccountService;
    private final RechargeOrderService rechargeOrderService;
    private final WithdrawApplyService withdrawApplyService;
    private final PaymentInfoService paymentInfoService;

    private final SystemLogRepo systemLogRepo;

    @Scheduled(cron = "0 0 2 * * ?")
    private void logLastDayInfo(){
        log.info("开始统计昨天数据");
        //前一天日期是今天减1
        LocalDate localDate = LocalDate.now();
        LocalDate yesterday = localDate.plusDays(-1);
        Integer registerNum = userService.lastDayRegisterNum();
        Integer lastDayOpenWalletNum = userFundAccountService.lastDayOpenWalletNum();
        Integer openWalletNum = userFundAccountService.openWalletNum();
        BigDecimal lastDayRechargeNum = rechargeOrderService.lastDayRechargeNum();
        BigDecimal lastDayWithdrawNum = withdrawApplyService.lastDayWithdrawNum();
        BigDecimal walletPayNum = paymentInfoService.lastDayWalletPayNum();
        BigDecimal bankPayNum = paymentInfoService.lastDayBankPayNum();
        Integer closingWalletNum = userFundAccountService.lastDayClosingWalletNum();
        SystemInfoLog systemInfoLog = SystemInfoLog.builder().logDate(yesterday).registerNum(registerNum).openWalletNum(lastDayOpenWalletNum)
                .openWalletNum(openWalletNum).dayRechargeNum(lastDayRechargeNum).dayWithdrawNum(lastDayWithdrawNum)
                .walletPayNum(walletPayNum).bankPayNum(bankPayNum).closingWalletNum(closingWalletNum).build();
        systemLogRepo.save(systemInfoLog);
        log.info("统计昨日消费数据完成");
    }
}
