package com.sonic.lion.job;

import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.lion.domain.entity.AccountBuffAwaiting;
import com.sonic.lion.service.AccountBuffAwaitingService;
import com.sonic.lion.service.AccountBuffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: code
 * @date: 2025/07/20
 * @Description:
 * @version: 1.0.0
 */
@Slf4j
@Component
public class AccountBuffJob {

    @Autowired
    private AccountBuffService accountBuffService;

    @Autowired
    private AccountBuffAwaitingService accountBuffAwaitingService;

    @Autowired
    private JobmanClient jobmanClient;

    /**
     * 每1分钟执行一次
     * 待入帐收入-->可提现收入
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void awaitingIncomeToWithdrawAbleIncomeSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("awaitingIncomeToWithdrawAbleIncomeJob", TimeUnit.MINUTES.toSeconds(2), this::awaitingIncomeToWithdrawAbleIncomeJob);
    }

    protected JobmanClient.JobResult awaitingIncomeToWithdrawAbleIncomeJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> awaitingIncomeToWithdrawAbleIncomeJob start !!!");
            List<AccountBuffAwaiting> list = accountBuffAwaitingService.listWillToIncome(200);
            for (AccountBuffAwaiting accountBuffAwaiting : list) {
                log.info("可提现buff入账, {}", accountBuffAwaiting);
                try {
                    accountBuffService.awaitingIncomeToWithdrawableIncome(accountBuffAwaiting);
                } catch (Exception e) {
                    log.error("可提现buff入账, id: " + accountBuffAwaiting.getId(), e);
                }
            }
            log.info("===> awaitingIncomeToWithdrawAbleIncomeJob count : {} end !!!", 0);
            return JobmanClient.JobResult.success(list.size());
        } finally {
            LogUtils.removeTraceId();
        }
    }


    /**
     * 每1分钟执行一次
     * 三天之前待入帐的可提现时间改成当前时间 处理 awaitingIncomeToWithdrawAbleIncomeJob 扫描漏数据的问题
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void awaitingIncomeUpdateOldDataSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("awaitingIncomeUpdateOldDataJob", TimeUnit.MINUTES.toSeconds(2), this::awaitingIncomeUpdateOldDataJob);
    }

    protected JobmanClient.JobResult awaitingIncomeUpdateOldDataJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> awaitingIncomeUpdateOldDataJob start !!!");
            int count = accountBuffAwaitingService.updateOldData();
            log.info("===> awaitingIncomeUpdateOldDataJob count : {} end !!!", 0);
            return JobmanClient.JobResult.success(count);
        } finally {
            LogUtils.removeTraceId();
        }
    }
}
