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.ExcelUtil;
import com.zhiwei.util.MongoConnection;
import com.zhiwei.util.ReadExcel;
import com.zw.entity.NewsResult;

import sendmail.SendMailUtil;

//@Component
public class PushFinanceByTimeLess extends Thread {
	private static final Log log = LogFactory.getLog(PushFinanceByTimeLess.class);
	private static final MongoConnection con = new MongoConnection();

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

	public void run() {
		while (true) {
			List<String> sources = new ArrayList<String>();
			String path = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/pushFinanceless.xls").getPath();
			Document doc = con.find("financePush", "excelPath", new Document().append("name", "lessSource"));
			if (doc != null) {
				if (doc.getString("url") != null) {
					path = doc.getString("url");
				}
			}
			log.info("推送检测");
			System.out.println(path);
			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,"media");
					String url = PushFinanceByTimeLess.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,"media");
					String url = PushFinanceByTimeLess.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 = PushFinanceByTimeLess.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);
			}
		}
	}

	private void sendsEmail(Calendar cal, List<NewsResult> list, String url) throws Exception {
		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);
	}

}
