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.text.SimpleDateFormat;
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");
            long produceTime=  redisTemplate.getExpire("quota:16:4:350200:"+timeKey);
            long now =new Date().getTime();
            long nh = 1000*60*60;//一小时的毫秒数
            long nm = 60;//一分钟的毫秒数
            //如果当前时间扣掉生成时间大于2小时
            if((now-Long.valueOf(timeKey))/nh>2){
                sendEmail("实时统计失败，redis数据生成失败，redis数据生成时间時間:"+ DateUtil.dateToStrLong(new Date(Long.valueOf(timeKey)))+"，当前系统时间:"+ DateUtil.dateToStrLong(new Date())+"，key剩下多少时间:"+ (produceTime/3600)+"小时"+(produceTime%3600/60)+"分钟"+(produceTime%3600%60)+"秒");
            }

            //判断当前时间是不是
            if(!redisTemplate.hasKey("quota:16:4:350200:"+timeKey)){
                //发送邮件给管理员
               sendEmail("实时统计失败，redis数据生成失败，redis数据为空時間:"+ DateUtil.dateToStrLong(new Date()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void sendEmail(String text) throws Exception {

        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from);//发送者.
        message.setSubject("实时统计失败");//邮件主题.
        message.setText(text);//邮件内容.
        String[] tos=to.split(",");
        message.setTo(tos);
        javaMailSender.send(message);
    }

    public void dateDiff(String startTime, String endTime, String format) throws Exception{
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数long diff;try {
        //获得两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff/nd;//计算差多少天
        long hour = diff%nd/nh;//计算差多少小时
        long min = diff%nd%nh/nm;//计算差多少分钟
        long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
        System.out.println("时间相差："+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
    }

    public static void main(String[] args) {
        Long produceTime=47265L;
        System.out.println( );
    }
}
