package com.srs.quartz.config;

import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.SrsStuYear;
import com.srs.comprehensive.mapper.SrsStuYearMapper;
import com.srs.comprehensive.service.ICphTestForService;
import com.srs.quartz.mapper.SysJobMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.srs.common.core.domain.AjaxResult.success;

//@Component("testForConfig")//配置类
@Component
@EnableScheduling
public class testForConfig {

    @Autowired
    private ICphTestForService cphTestForService;

    @Autowired
    public SrsStuYearMapper srsStuYearMapper;

    @Autowired
    public SysJobMapper selectJobAll;


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


    //@Scheduled(cron = "0 0 0 * * ?") // 每天0点执行
    public void getbers() throws ParseException {
//        SysJob sysJobs = selectJobAll.selectJobById(103L);
//        if (sysJobs!=null&& Objects.equals(sysJobs.getStatus(), "1")){
//            System.out.println("停用");
//            return;
//        }
        System.out.println("定时任务，启动！");
        //查询当前年份属于学年
        List<SrsStuYear> srsStuYears = srsStuYearMapper.selectSrsStuYearList(null);
        //当前时间
        Date date= DateUtils.getNowDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);//时间格式化后的date
        Date parse = sdf.parse(format);
        for (SrsStuYear stuYear:srsStuYears){
            String dateStuYear=sdf.format(stuYear.getStartTime());
            Date startDate=sdf.parse(dateStuYear);//开始时间
            String endDateTos=sdf.format(stuYear.getEndTime());
            Date endDate=sdf.parse(endDateTos);//结束时间
            int comparison = parse.compareTo(startDate);
            int end = parse.compareTo(endDate);
            if (comparison>=0&&end<=0){
                cphTestForService.testMethod(Math.toIntExact(stuYear.getId()));
                return;
            }
        }

    }

}
