package com.zhiwei.finance;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.springframework.stereotype.Component;

import com.zhiwei.orm.BeanFactory;
import com.zhiwei.service.Services;
import com.zhiwei.util.ExcelUtil;
import com.zhiwei.util.MongoConnection;
import com.zhiwei.util.ReadExcel;
import com.zhiwei.util.StringUtil;
import com.zw.entity.NewsResult;

import sendmail.SendMailUtil;

@Component
public class PushFinanceByTime extends Thread {

	private static final Log log = LogFactory.getLog(PushFinanceByTime.class);
	private static final MongoConnection con = new MongoConnection();

	public PushFinanceByTime() {
		this.start();
	}

	public void run() {
		while (true) {
			List<String> sources = new ArrayList<String>();
			List<String> wechat = new ArrayList<String>();
			List<String> weibo = new ArrayList<String>();
			log.info("推送检测");
			String path = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/pushSource.xls").getPath();
			Document doc = con.find("financePush", "excelPath", new Document().append("name", "longSource"));
			if (doc != null) {
				if (doc.getString("url") != null) {
					path = doc.getString("url");
				}
			}
			sources = new ReadExcel().getExcel(path);
			System.out.println(sources);
			String path_wechat = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/wechat.xls").getPath();
			Document doc_wechat = con.find("financePush", "excelPath", new Document().append("name", "wechat"));
			if (doc_wechat != null) {
				if (doc_wechat.getString("url") != null) {
					path_wechat = doc_wechat.getString("url");
				}
			}
			wechat = new ReadExcel().getExcel(path_wechat);
			System.out.println(wechat);
			String path_weibo = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/weibo.xls").getPath();
			Document doc_weibo = con.find("financePush", "excelPath", new Document().append("name", "weibo"));
			if (doc_weibo != null) {
				if (doc_weibo.getString("url") != null) {
					path_weibo = doc_weibo.getString("url");
				}
			}
			weibo = new ReadExcel().getExcel(path_weibo);
			System.out.println(weibo);
			Calendar cal = Calendar.getInstance();
			try {
				if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) >= 30) {
					cal.add(Calendar.HOUR_OF_DAY, -16);
					cal.set(Calendar.MINUTE, 0);
					List<NewsResult> list = new ESUtil().pushByKey(cal.getTime(), new Date(), sources, "media");
					String url = PushFinanceByTime.class.getClassLoader().getResource("/").getPath() + "/"
							+ (int) (Math.random() * 5000) + "sends.xls";
					cal.add(Calendar.HOUR_OF_DAY, 16);
					sendsEmail(cal, list, url);
					sleep(1000 * 60 * 60);
				} else if (cal.get(Calendar.HOUR_OF_DAY) == 11) {// 11
					cal.add(Calendar.HOUR_OF_DAY, -3);
					List<NewsResult> list = new ESUtil().pushByKey(cal.getTime(), new Date(), sources, "media");
					String url = PushFinanceByTime.class.getClassLoader().getResource("/").getPath() + "/"
							+ (int) (Math.random() * 5000) + "sends.xls";
					cal.add(Calendar.HOUR_OF_DAY, 3);
					sendsEmail(cal, list, url);
					sleep(1000 * 60 * 60);
				}
				if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
					if (cal.get(Calendar.HOUR_OF_DAY) == 16 && cal.get(Calendar.MINUTE) >= 30) {
						cal.add(Calendar.HOUR_OF_DAY, -5);
						cal.set(Calendar.MINUTE, 0);
						List<NewsResult> list = new ESUtil().pushByKey(cal.getTime(), new Date(), sources, "media");
						String url = PushFinanceByTime.class.getClassLoader().getResource("/").getPath() + "/"
								+ (int) (Math.random() * 5000) + "sends.xls";
						cal.add(Calendar.HOUR_OF_DAY, 5);
						sendsEmail(cal, list,  url);
						sleep(1000 * 60 * 60);
					}
				}
				sleep(1000 * 60 * 5);
			} catch (Exception e) {
				log.error("推送错误信息", e);
			}
			try {
				sleep(1000 * 10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void sendsEmail(Calendar cal, List<NewsResult> list,
			String url) {
		try {
			new ExcelUtil().pushDataWithFinanceLong(list,cal.getTime(), url);
			SendMailUtil.sendMessage("949131080@qq.com", url, "金融");
			SendMailUtil.sendMessage("yuhualong@zhiweidata.com", url, "金融");
			SendMailUtil.sendMessage("ligaodie@zhiweidata.com", url, "金融");
			ExcelUtil.delete(url);
		} catch (Exception e) {
			log.error("一号邮箱发送失败，尝试二号邮箱{}", e);
			try {
				new ExcelUtil().pushDataWithFinanceLong(list, cal.getTime(), url);
				SendMailUtil.sendMessage2("949131080@qq.com", url, "金融");
				SendMailUtil.sendMessage2("yuhualong@zhiweidata.com", url, "金融");
				SendMailUtil.sendMessage2("ligaodie@zhiweidata.com", url, "金融");
				ExcelUtil.delete(url);
			} catch (Exception e2) {
				log.error("二号邮箱发送错误{}", e);
			}
		}
	}

}
