package com.gyee.frame.util.mongodb;

import com.gyee.frame.model.auto.WindPowerStationTestingPoint2;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi2;
import com.gyee.frame.model.custom.DNAStatVal;
import com.gyee.frame.model.custom.DNAVal;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 */

@Component
public class MongoDbUtil {
	private static Logger logger = LoggerFactory.getLogger(MongoDbUtil.class);
	private String po_key = "S";
	private String po_value = "V";

	@Resource
	@Qualifier("ls_redis")
	private JedisPool jedisPool;


	public List<PointData> getHistoryDatasSnap(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate, Long count, Long pried) throws Exception {

//		long startStamp = System.currentTimeMillis();
//		Date startDate = DateUtils.parseLongToDate(startStamp);
//		//System.out.println("开始执行任务,任务开始时间:" + DateUtils.toDate(startDate));

		List<PointData> ls = new ArrayList<PointData>();
		HistorySnapUtil snap=new HistorySnapUtil();
		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(point.getCode()) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {
			snap.findHistoryDatasSnap(point.getCode(), beginDate,  endDate, count, pried, ls,jedisPool);

		}

//		long cost = System.currentTimeMillis() - startStamp;
//		//System.out.println("任务执行结束,耗时：" + cost / 1000 + "秒!........");

		return ls;
	}

	public List<PointData> getHistoryDatasSnap(String pointid, Long beginDate, Long endDate, Long count, Long pried) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		HistorySnapUtil snap=new HistorySnapUtil();
		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(pointid) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {

			snap.findHistoryDatasSnap(pointid, beginDate,  endDate, count, pried, ls,jedisPool);

		}

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public List<PointData> getHistoryDatasSnap(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate, Long count, Long pried) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		HistorySnapUtil snap=new HistorySnapUtil();
		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(point.getId()) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {
		//	findHistoryDatasSnap(point.getId(), beginDate, count, pried, ls);
			snap.findHistoryDatasSnap(point.getId(), beginDate,  endDate, count, pried, ls,jedisPool);
		}

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	




	private DNAStatVal[] findHistoryDatasStat(String point, Long beginDate, Long count, Long pried) throws Exception {

		DNAStatVal[] statResult = null;
		

		return statResult;
	}

	private void setDnaStatValue(DNAStatVal po, int type, String point, Object[] obj, MongoCollection<Document> collection, long begintemp, int i, Document doc) {
		BasicDBObject query;

		if (StringUtils.isNotNull(doc)) {

			setDNAStatVal(point, obj, po, doc, type);

			logger.debug(i + "__时间：" + begintemp + "__数值:" + doc.getDouble(po_value));
		} else if (StringUtils.isNotEmpty(doc)) {
			query = new BasicDBObject();
			query.put(po_key, new BasicDBObject("$lte", begintemp));
			doc = collection.find(query).first();
			if (StringUtils.isNotNull(doc)) {
				setDNAStatVal(point, obj, po, doc, type);
				logger.debug("补充记录" + i + "__时间：" + begintemp + "__数值:" + doc.getDouble(po_value));
			} else {
				switch (type) {
				case 0:
					po.avg.Time = Integer.valueOf(String.valueOf(begintemp));
					po.avg.DValue = 0.0;
					break;
				case 1:
					po.max.Time = Integer.valueOf(String.valueOf(begintemp));
					po.max.DValue = 0.0;
					break;
				case 2:
					po.min.Time = Integer.valueOf(String.valueOf(begintemp));
					po.min.DValue = 0.0;
					break;
				default:
					break;
				}

				logger.debug("缺失" + i + "__时间：" + begintemp + "__数值:" + 0);
			}
		}

	}

	public List<PointData> getHistStat(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(point.getCode()) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {
			HistoryStatUtil stat=new HistoryStatUtil();
			stat.findHistoryDatasStat(point.getId(), beginDate, endDate, pried, ls, jedisPool, type);


		}

//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public DNAStatVal[] getHistStat(String point, Long beginDate, Long endDate, Integer pried) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		DNAStatVal[] statResult = null;

		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(pried)) {

			long total = endDate - beginDate;
			long temp = Long.valueOf(pried);
			long count = 0;
			if (temp != 0) {
				count = total / temp;
			}
			statResult = findHistoryDatasStat(point, beginDate, count, temp);

		}

		// ApiEntry entry = ApiEdos.getInstance();
		// // String id = point.substring(point.indexOf(".") + 1).replace(".",
		// // "_");
		// String id = point;
		// DNAStatVal[] statResult = null;
		//
		// try {
		// statResult = entry.getHistStat(id, beginDate.intValue(),
		// endDate.intValue(), pried.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + id);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return statResult;

	}

	public List<PointData> getHistStat(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(point.getCode()) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {
			HistoryStatUtil stat=new HistoryStatUtil();
			stat.findHistoryDatasStat(point.getCode(), beginDate, endDate, pried, ls, jedisPool, type);
		}

		// ApiEntry entry = ApiEdos.getInstance();
		// // String id = point.getCode().substring(point.getCode().indexOf(".")
		// +
		// // 1).replace(".", "_");
		// String id = point.getCode();
		// DNAStatVal[] statResult = null;
		//
		// try {
		// statResult = entry.getHistStat(id, beginDate.intValue(),
		// endDate.intValue(), pried.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + id);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
		// List<PointData> ls = new ArrayList<PointData>();
		// if (statResult.length > 0) {
		// for (int i = 0; i < statResult.length; i++) {
		// PointData po = new PointData();
		// switch (type) {
		// case 0:// 最大值
		// po.setPointTime(Long.valueOf(statResult[i].max.Time));
		// po.setPointValueInDouble(statResult[i].max.DValue);
		// break;
		// case 1:// 最小值
		// po.setPointTime(Long.valueOf(statResult[i].min.Time));
		// po.setPointValueInDouble(statResult[i].min.DValue);
		// break;
		// case 2:// 平均值
		// po.setPointTime(Long.valueOf(statResult[i].avg.Time));
		// po.setPointValueInDouble(statResult[i].avg.DValue);
		// break;
		// default:
		// break;
		// }
		//
		// ls.add(po);
		// }
		//
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public List<PointData> getHistStat(String pointid, Long beginDate, Long endDate, Long count, Long pried, int type) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));

		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(pointid) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate) && StringUtils.isNotNull(count) && StringUtils.isNotNull(pried)) {
			HistoryStatUtil stat=new HistoryStatUtil();
			stat.findHistoryDatasStat(pointid, beginDate, endDate, pried, ls, jedisPool, type);

		}

		// ApiEntry entry = ApiEdos.getInstance();
		//
		// DNAStatVal[] statResult = null;
		//
		// try {
		// statResult = entry.getHistStat(pointid, beginDate.intValue(),
		// endDate.intValue(), pried.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + pointid);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
		// List<PointData> ls = new ArrayList<PointData>();
		// if (statResult.length > 0) {
		// for (int i = 0; i < statResult.length; i++) {
		// PointData po = new PointData();
		// switch (type) {
		// case 0:// 最大值
		// po.setPointTime(Long.valueOf(statResult[i].max.Time));
		// po.setPointValueInDouble(statResult[i].max.DValue);
		// break;
		// case 1:// 最小值
		// po.setPointTime(Long.valueOf(statResult[i].min.Time));
		// po.setPointValueInDouble(statResult[i].min.DValue);
		// break;
		// case 2:// 平均值
		// po.setPointTime(Long.valueOf(statResult[i].avg.Time));
		// po.setPointValueInDouble(statResult[i].avg.DValue);
		// break;
		// default:
		// break;
		// }
		//
		// ls.add(po);
		// }
		//
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public List<PointData> getHistoryDatasRaw(WindTurbineTestingPointAi2 point, Long beginDate, Long endDate) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		List<PointData> ls = new ArrayList<PointData>();

		HistoryDatasRawUtil raw=new HistoryDatasRawUtil();
		raw.findHistoryDatasRaw(point.getId(), beginDate, endDate, ls, jedisPool);

		// ApiEntry entry = ApiEdos.getInstance();
		// // String id = point.getId().substring(point.getId().indexOf(".") +
		// // 1).replace(".", "_");
		// String id = point.getId();
		//
		// DNAVal[] statResult = null;
		// try {
		// statResult = entry.getHistRaw(id, beginDate.intValue(),
		// endDate.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + id);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
		// List<PointData> ls = new ArrayList<PointData>();
		// if (statResult.length > 0) {
		// for (int i = 0; i < statResult.length; i++) {
		// PointData po = new PointData();
		// po.setPointTime(Long.valueOf(statResult[i].Time));
		// po.setPointValueInDouble(statResult[i].DValue);
		// ls.add(po);
		// }
		//
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public List<PointData> getHistoryDatasRaw(WindPowerStationTestingPoint2 point, Long beginDate, Long endDate) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(point) && StringUtils.isNotNull(point.getCode()) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate)) {
			HistoryDatasRawUtil raw=new HistoryDatasRawUtil();
			raw.findHistoryDatasRaw(point.getCode(), beginDate, endDate, ls, jedisPool);

		}

		// ApiEntry entry = ApiEdos.getInstance();
		// // String id = point.getCode().substring(point.getCode().indexOf(".")
		// +
		// // 1).replace(".", "_");
		// String id = point.getCode();
		//
		// DNAVal[] statResult = null;
		// try {
		// statResult = entry.getHistRaw(id, beginDate.intValue(),
		// endDate.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + id);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
		// List<PointData> ls = new ArrayList<PointData>();
		// if (statResult.length > 0) {
		// for (int i = 0; i < statResult.length; i++) {
		// PointData po = new PointData();
		// po.setPointTime(Long.valueOf(statResult[i].Time));
		// po.setPointValueInDouble(statResult[i].DValue);
		// ls.add(po);
		// }
		//
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	public List<PointData> getHistoryDatasRaw(String pointid, Long beginDate, Long endDate) throws Exception {
//		long startStamp = System.currentTimeMillis();
//		logger.info("==================开始时刻: " + DateUtils.parseLongToDate(startStamp));
		List<PointData> ls = new ArrayList<PointData>();

		if (StringUtils.isNotNull(pointid) && StringUtils.isNotNull(beginDate) && StringUtils.isNotNull(endDate)) {
			HistoryDatasRawUtil raw=new HistoryDatasRawUtil();
			raw.findHistoryDatasRaw(pointid, beginDate, endDate, ls, jedisPool);

		}

		// ApiEntry entry = ApiEdos.getInstance();
		//
		// // DNAVal[] statResult = entry.getHistRaw(pointid,
		// beginDate.intValue(),
		// // endDate.intValue());
		// DNAVal[] statResult = null;
		// try {
		// statResult = entry.getHistRaw(pointid, beginDate.intValue(),
		// endDate.intValue());
		// } catch (Exception e) {
		// logger.info("测点编号" + pointid);
		// logger.info(e.getMessage());
		// long endStamp = System.currentTimeMillis();
		// long cost = endStamp - startStamp;
		// logger.info("==================结束时刻: " +
		// DateUtils.parseLongToDate(endStamp));
		// logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		// throw e;
		// }
		// List<PointData> ls = new ArrayList<PointData>();
		// if (statResult.length > 0) {
		// for (int i = 0; i < statResult.length; i++) {
		// PointData po = new PointData();
		// po.setPointTime(Long.valueOf(statResult[i].Time));
		// po.setPointValueInDouble(statResult[i].DValue);
		// ls.add(po);
		// }
		//
		// }
//		long endStamp = System.currentTimeMillis();
//		long cost = endStamp - startStamp;
//		logger.info("==================结束时刻: " + DateUtils.parseLongToDate(endStamp));
//		logger.info("调度程执行结束,耗时：" + cost / 1000 + "秒!........");
		return ls;
	}

	// private static List<DBObject> getMobileQuery(Date beginDate, Date
	// endDate) {
	// List<DBObject> phoneList = Lists.newArrayList();
	// phoneList.add(new BasicDBObject(po_key, new BasicDBObject("$gt",
	// beginDate.getTime() / 1000)));
	// phoneList.add(new BasicDBObject(po_key, new BasicDBObject("$lt",
	// endDate.getTime() / 1000)));
	// return phoneList;
	// }

	private void setPointData(String code, Object[] obj, PointData po, Document doc) {

		po.setEdnaId(code);

		Long time = Long.valueOf(doc.getString(po_key));

		int type = (Integer) obj[2];
		if (type == 0) {
			Boolean value = Boolean.valueOf(doc.getString(po_value));
			if (value) {
				po.setPointValueInDouble(1);
			} else {
				po.setPointValueInDouble(0);
			}

		} else {
			Double value = Double.valueOf(doc.getString(po_value));
			po.setPointValueInDouble(value);
		}

		po.setPointTime(time * 1000);

	}

	private void setDNAStatVal(String code, Object[] obj, DNAStatVal po, Document doc, int dtype) {

		DNAVal val = null;
		switch (dtype) {
		case 0:
			val = po.avg;
			break;
		case 1:
			val = po.max;
			break;
		case 2:
			val = po.min;
			break;
		default:
			break;
		}

		// po..setEdnaId(code);
		Integer time = Integer.valueOf(doc.getString(po_key));
		int type = (Integer) obj[2];
		if (type == 0) {
			Boolean value = Boolean.valueOf(doc.getString(po_value));
			if (value) {
				val.DValue = 1;

			} else {
				val.DValue = 0;
			}

		} else {
			Double value = Double.valueOf(doc.getString(po_value));
			val.DValue = value;
		}

		val.Time = time;

	}

	public void updatePoint(PointData point,JedisPool jedisPool_ss) throws Exception {

		UpdateDataUtil ud=new UpdateDataUtil();
		ud.updateData(point,jedisPool_ss, jedisPool);
		

	}

}
