package ${basePackage}.config;

import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.schedule.RunTask;
import ${basePackage}.frame.schedule.Scheduler;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.module.system.ent.TaskSql;
import ${basePackage}.module.system.mgr.TaskSqlManager;
import ${basePackage}.module.system.req.TaskSqlFindRequest;
import ${basePackage}.module.system.rsp.TaskSqlFindResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AssignableTypeFilter;

import javax.annotation.PostConstruct;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class TaskConfig {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private TaskSqlManager taskSqlManager;

    @Bean
    public Scheduler registerScheduler() {
        return new Scheduler();
    }

    @PostConstruct
    public void registryTask() {
        {// 扫描类任务
            String aPackage = this.getClass().getPackage().getName();
            Pattern compile = Pattern.compile("(.*)\\.config");
            Matcher matcher = compile.matcher(aPackage);
            if (matcher.find()) {
                DefaultListableBeanFactory simpleBeanDefinitionRegistry = new DefaultListableBeanFactory();
                ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(simpleBeanDefinitionRegistry);
                classPathBeanDefinitionScanner.resetFilters(false);
                classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(RunTask.class));
                classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
                    @Override
                    public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
                        String beanClassName = beanDefinition.getBeanClassName();
                        try {
                            Class<?> aClass = Class.forName(beanClassName);
                            Object instance = aClass.newInstance();
                            RunTask task = (RunTask) instance;
                            scheduler.createOrRepeat(task);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtil.i("registry task " + beanClassName);
                        return beanClassName;
                    }
                });
                classPathBeanDefinitionScanner.scan(matcher.group(1));
            }
        }

        {// 实例化SQL任务
            TaskSqlFindRequest taskSqlFindRequest = new TaskSqlFindRequest();
            taskSqlFindRequest.setValid(true);
            taskSqlFindRequest.setPageSize(0);
            TaskSqlFindResponse taskSqlFindResponse = taskSqlManager.find(taskSqlFindRequest, LocalData.getSysToken());
            for (TaskSql taskSql : taskSqlFindResponse.getResult()) {
                taskSqlManager.createOrRepeat(taskSql);
            }
        }
    }

}