package com.reptileclient.jlifeapp.schedule;

import com.reptileclient.jlifeapp.entity.jz.JzEventHistory;
import com.reptileclient.jlifeapp.service.JzEventHistoryService;
import com.reptileclient.jlifeapp.service.emailnotice.EmailNoticeService;
import com.reptileclient.jlifeapp.util.DateUtils;

import com.reptileclient.jlifeapp.util.email.MailSend;
import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  邮箱超时处理通知
 *
 * @author lu
 *
 */
@Slf4j
@Component
public class EmailNoticeTask {

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

	@Autowired
	EmailNoticeService emailNoticeService;

	@Autowired
	private JzEventHistoryService eventHistoryService;


	private static Map<String, String> exit = new HashMap<String, String>();

	@Scheduled(cron = "0 */10 8-11 * * ?") // 每天8到11点每10分钟执行一次
	public void startJob() {
		String currentDay = DateUtils.getCurrentDay();
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);

		if (hour == 9) {
			String key = currentDay + "ganwei-sql";//只提醒一次
			if (!exit.containsKey(key)) {
				log.info("每天8点判断 甘微-sql1和sql2");

				String title = "全部成功";

				//report库下的脚本执行情况
				StringBuilder content = new StringBuilder("<html><head></head><body>");

				//邮件主体内容---report下的脚本执行情况
				boolean isSuccess = emailNoticeService.checkETL("运行z_ganwei_sql脚本成功");
				content.append("jlife_reports.z_ganwei_sql执行结果：" + (isSuccess ? "成功" : "失败"));
				content.append("<br/>");
				if (!isSuccess) title = "失败";

				isSuccess = emailNoticeService.checkETL("运行z_ganwei_sql2脚本成功");
				content.append("jlife_reports.z_ganwei_sql2执行结果：" + (isSuccess ? "成功" : "失败"));
				content.append("<br/>");
				if (!isSuccess) title = "失败";

				//薪资执行情况
				isSuccess=emailNoticeService.checkETL("运行etl_salary_detail脚本成功");
				content.append("etl_salary_detail执行结果：" + (isSuccess ? "成功" : "失败"));
				content.append("<br/><br/>");
				if (!isSuccess) title = "失败";

				//trade_order表以及同步worker_order_info情况自荐
				/**
				 * 自检分2步
				 * 首先是盒马服务单，根据repliter.hm_book_trade_no表数量和trade_order比较
				 * 第二是商城服务单自检
				 */


				//其他任务执行情况
				content.append("jlife_nurse运行情况：<br/>");
				content.append("<table border=\"1\" style=\"width:700px; height:150px;border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
				content.append("<tr style=\"background-color: #428BCA; color:#ffffff\">" +
						"<td>脚本名称</td>" +
						"<td>开始时间</td>" +
						"<td>结束时间</td>" +
						"<td>运行结果</td>" +
						"<td>错误日志</td>" +
						"</tr>");
				List<JzEventHistory> historyList = eventHistoryService.selectByToday();
				for (JzEventHistory item : historyList) {
					content.append("<tr>" +
							"<td>" + item.getEventname() + "</td>" +
							"<td>" + item.getStarttime() + "</td>" +
							"<td>" + item.getEndtime() + "</td>" +
							"<td>" + (item.getIssuccess() == 1 ? "成功" : "失败") + "</td>" +
							"<td>" + item.getErrormessage() + "</td>" +
							"</tr>");
					if (item.getIssuccess() == 0)
						title = "失败";
				}
				content.append("</tr>");
				content.append("</table>");
				content.append("</body></html>");
				MailSend.sendMS(LocalDate.now() + "脚本执行：" + title, content.toString());
				exit.put(key, "ok");
			}
		}

		//9:30判断薪资
//		if (hour == 9 && minute == 30) {
//			String key = currentDay + "etl_salary_detail";//只提醒一次
//			if (!exit.containsKey(key)) {
//				log.info("每天9.30判断薪资etl_salary_detail");
//				emailNoticeService.checkETL("jlife_worker.etl_salary_detail薪资函数提醒", "运行etl_salary_detail脚本成功");
//				exit.put(key, "ok");
//			}
//		}

	}

	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		System.out.println(hour + ":" + minute);//16:22
	}

}
