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>();
			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");
				}
			}
			log.info("推送检测");
			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 = new ESUtil().pushByKey(cal.getTime(), new Date(), sources);
					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) {
					cal.add(Calendar.HOUR_OF_DAY, -3);
					List<NewsResult> list = new ESUtil().pushByKey(cal.getTime(), new Date(), sources);
					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);
						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().pushDataWithFinance(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().pushDataWithFinance(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);
			}
		}
	}

}
