package com.simuwang.manage.task;

import ch.qos.logback.core.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.simuwang.base.common.enums.EmailCron;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.QuartzUtils;
import com.simuwang.base.pojo.dos.MailboxInfoDO;
import com.simuwang.base.pojo.dto.MailboxInfoDTO;
import com.simuwang.daq.service.EmailParseApiService;
import com.simuwang.manage.service.EmailConfigService;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * FileName: ParseSchedulerTask
 * Author:   chenjianhua
 * Date:     2024/9/17 10:48
 * Description: ${DESCRIPTION}
 */

public class ParseSchedulerTask  extends QuartzJobBean {

    @Autowired
    private EmailParseApiService emailParseApiService;
    @Autowired
    private EmailConfigService emailConfigService;
    private static final Logger log = LoggerFactory.getLogger(ParseSchedulerTask.class);
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("ParseSchedulerTask {}", context.getJobDetail().getKey());
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        log.info("ParseSchedulerTask jobDataMap {}", JSON.toJSONString(jobDataMap));
        String account = jobDataMap.get("account").toString();
        String port = jobDataMap.get("port").toString();
        String protocol = jobDataMap.get("protocol").toString();
        String host = jobDataMap.get("host").toString();
        String password = jobDataMap.get("password").toString();
        MailboxInfoDTO paramDTO = new MailboxInfoDTO();
        paramDTO.setAccount(account);
        paramDTO.setPassword(password);
        paramDTO.setPort(port);
        paramDTO.setHost(host);
        paramDTO.setProtocol(protocol);
        MailboxInfoDO mailboxInfoDO = emailConfigService.selectEmailConfigByEmail(account);
        if(mailboxInfoDO != null){
            Date startDate = null;
            Date endDate = null;
            if(EmailCron.getEmailCronByCron(mailboxInfoDO.getCron()) == EmailCron.EVERY_HOUR){
                endDate = DateUtils.getNowDate();
                startDate = DateUtils.addHours(DateUtils.getNowDate(),-1);;
            }else if (EmailCron.getEmailCronByCron(mailboxInfoDO.getCron()) == EmailCron.ONE_AM){
                endDate = DateUtils.getNowDate();
                startDate = DateUtils.addHours(DateUtils.getNowDate(),-24);;
            }
            log.info(account+"开始执行邮件解析任务，时间："+LocalDateTime.now());
            emailParseApiService.parseEmail(paramDTO,startDate,endDate);
            log.info(account+"結束执行邮件解析任务，时间："+LocalDateTime.now());
        }
    }
}
