package com.gree.mall.manager.schedule;



import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.logic.policy.AgreementLogic;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Date;

/**
 * 定时购买保险
 */
@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class InsureEmailSchedule {


    @Autowired
    AgreementLogic agreementLogic;



    //每小时执行1次
    @Scheduled(fixedDelay = 600* 1000)
    public void task() throws IOException {

        Date date = new Date();

        Date startTime = DateUtil.parseDateTime(DateUtils.formatDate(date,"yyyy-MM-dd") +" "+"08:00:00");
        Date endTime = DateUtil.parseDateTime(DateUtils.formatDate(date,"yyyy-MM-dd") +" "+"20:00:00");

        if (date.before(startTime))
            return;
        if (date.after(endTime))
            return;

        agreementLogic.sendEmailSche();
        agreementLogic.sendEmailScheEM();
        agreementLogic.sendEmailScheIN();
    }


    //监测今天是否有回执邮件，如果有则表示今天买保险的回执成功
    @Scheduled(fixedDelay = 600* 1000)
    public void task2() throws IOException, MessagingException {
        agreementLogic.receEmail();
    }


    //保险过期
    @Scheduled(fixedDelay = 600* 1000)
    public void task3(){
        agreementLogic.offInsure();
    }


    //检查保险是否全部买齐转为保险待审批
    @Scheduled(fixedDelay = 30* 1000)
    public void task4(){
        agreementLogic.offInsure();
        agreementLogic.task4();
    }


    //扫表身份证，高空证过期备注
    @Scheduled(fixedDelay = 600* 1000)
    public void task5(){
        agreementLogic.task5();
    }



    //工伤提前三天续保
    @Scheduled(fixedDelay = 600* 1000)
    public void task6(){
        agreementLogic.task6();
    }

    //雇主提前设置天数续保
    @Scheduled(fixedDelay = 600* 1000)
    public void task7(){
        agreementLogic.task7();
    }


    //将联络涵设置对应状态
    @Scheduled(fixedDelay = 600* 1000)
    public void task8(){
        agreementLogic.task8();
    }
}
