package com.simuwang.manage.init;

import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.mapper.system.SysConfigMapper;
import com.simuwang.manage.task.FundDeletionTask;
import com.simuwang.manage.task.SendCompanyEmailTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.time.LocalDateTime;


/**
 * TODO
 * @version 1.0
 * @author chenjianhua
 * @date 2021/12/29 13:35
 */
@Configuration
@EnableScheduling
public class CompleteScheduleConfig implements SchedulingConfigurer {

    private static Logger logger = LoggerFactory.getLogger(CompleteScheduleConfig.class);

    @Autowired
    private FundDeletionTask fundDeletionTask;

    @Autowired
    private SendCompanyEmailTask sendCompanyEmailTask;

    @Autowired
    private SysConfigMapper sysConfigMapper;
    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler=new ThreadPoolTaskScheduler();
        // 设定最大可用的线程数目
        taskScheduler.setPoolSize(10);
        taskScheduler.initialize();
        taskRegistrar.setScheduler(taskScheduler);
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> {
                    try {
                        logger.info("缺失统计任务开始"+ DateUtils.getTime());
                        fundDeletionTask.computeDeletion();//定时拉取数据
                        logger.info("缺失统计任务结束"+ DateUtils.getTime());
                    } catch (Exception e) {
                        logger.error("缺失统计任务异常========="+e.getMessage()+"----" +DateUtils.getTime(),e);
                    }
                },
                //2.设置执行周期(Trigger)
                loadFtpFileTriggerContext -> {
                    String cron = sysConfigMapper.selectConfigByKey("deletion_cron");
                    logger.info("缺失统计定时任务执行时间："+cron);
                    //2.2 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(loadFtpFileTriggerContext).toInstant();
                }
        );
        //增量补数
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> {
                    try {
                        logger.info("给管理人发送基金缺失预警邮件定时任务开始"+DateUtils.getTime());
                        sendCompanyEmailTask.sendEmail();//定时拉取数据
                        logger.info("给管理人发送基金缺失预警邮件定时任务结束========="+ DateUtils.getTime());
                    } catch (Exception e) {
                        logger.error("给管理人发送基金缺失预警邮件定时任务异常========="+e.getMessage()+ DateUtils.getTime(),e);
                    }
                },
                //2.设置执行周期(Trigger)
                loadFtpFileUpdateTriggerContext -> {
                    String cron = sysConfigMapper.selectConfigByKey("send_company_email");
                    logger.info("给管理人发送基金缺失预警邮件定时任务轮训时间："+cron);
                    //2.2 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(loadFtpFileUpdateTriggerContext).toInstant();
                }
        );
    }

}
