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.util.Config;
import com.zhiwei.util.ExcelUtil;
import com.zhiwei.util.MongoConnection;
import com.zhiwei.util.ReadExcel;
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();
	}

	@Override
	public void run() {
		while (true) {
			List<String> sources = 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);
			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 = 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 = 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 = 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);
			for (String mail : Config.getVal("pushMail").split(",")) {
				SendMailUtil.sendMessage(mail, url, "金融");
			}
			ExcelUtil.delete(url);
		} catch (Exception e) {
			log.error("一号邮箱发送失败，尝试二号邮箱{}", e);
			try {
				new ExcelUtil().pushDataWithFinanceLong(list, cal.getTime(), url);
				for (String mail : Config.getVal("pushMail").split(",")) {
					SendMailUtil.sendMessage2(mail, url, "金融");
				}
				ExcelUtil.delete(url);
			} catch (Exception e2) {
				log.error("二号邮箱发送错误{}", e);
			}
		}
	}

}
