package com.yihu.wlyy.statistics.job.check;

import com.yihu.wlyy.statistics.util.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.util.Date;

/**
 * Created by Administrator on 2016.10.11.
 * 判断签约的数据对不对 不对的话 添加任务重新生成签约数据
 */
@Component
@Scope("prototype")
public class CheckSignJob implements Job{

    public static String jobKey="CHECK_SIGN_JOB";
    public static String cron="0 */5 * * * ?";

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.from}")
    private String from;;
    @Value("${spring.mail.to}")
    private String to;;

    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try{
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            String timeKey=redisTemplate.opsForValue().get("quota:timeKey");
            if(!redisTemplate.hasKey("quota:16:4:350200:"+timeKey)){
                //发送邮件给管理员
               // sendEmail();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void sendEmail() throws Exception {

        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from);//发送者.
        message.setSubject("实时统计失败");//邮件主题.
        message.setText("实时统计失败，redis数据生成失败 時間:"+ DateUtil.dateToStrLong(new Date()));//邮件内容.
        String[] tos=to.split(",");
        message.setTo(tos);
        javaMailSender.send(message);
    }


}
