package com.zhiwei.manage.dao;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.zhiwei.manage.bean.Data;

@SuppressWarnings({ "all" })
@Component
public class DataDaoImpl implements DataDao {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH");
	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;

	@Override
	public List<Data> findByPt(Date startTime, Date endTime, String pt) {
		Criteria criteria = new Criteria();
		criteria.and("pt").is(pt);
		criteria.and("time").gt(startTime).lt(endTime);
		criteria.and("tempName").exists(false);
		return mongo.find(Query.query(criteria), Data.class, "Data");
	}

	@Override
	public List<Data> findByPtDay(Date startTime, Date endTime, String pt) {
		Criteria criteria = new Criteria();
		criteria.and("pt").is(pt);
		criteria.and("time").gt(startTime).lt(endTime);
		return mongo.find(Query.query(criteria).with(new Sort(Direction.ASC, "time")), Data.class, "DayData");
	}

	@Override
	public List<Data> findBySource(Date startTime, Date endTime, String source) {
		Criteria criteria = new Criteria();
		criteria.and("source").regex(source);
		criteria.and("time").gte(startTime).lt(endTime);
		return mongo.find(Query.query(criteria), Data.class, "Data");
	}

	@Override
	public List<Data> findBytempName(Date startTime, Date endTime, String tempName) {
		Criteria criteria = new Criteria();
		criteria.and("tempName").regex(tempName);
		criteria.and("time").gte(startTime).lt(endTime);
		return mongo.find(Query.query(criteria), Data.class, "Data");
	}

	@Override
	public void insert(Data data) {
		Data d = null;
		Date today = null;
		try {
			today = sdf.parse(sdf.format(data.getTime()));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		if (data.getTempName() != null) {
			d = mongo.findOne(Query.query(
					Criteria.where("time").is(today).andOperator(Criteria.where("tempName").is(data.getTempName()))),
					Data.class);
		} else {
			d = mongo.findOne(
					Query.query(Criteria.where("time").is(today).andOperator(Criteria.where("pt").is(data.getPt()))),
					Data.class);
		}
		if (d == null) {
			mongo.insert(data, "Data");
		} else {
			d.setCount(data.getCount());
			Update update = new Update();
			Field[] fields = d.getClass().getDeclaredFields();
			for (int j = 0; j < fields.length; j++) {
				fields[j].setAccessible(true);
				try {
					if (fields[j].getName().equals("dataId")) {
						continue;
					} else if (fields[j].getName().equals("time")) {
						update.set(fields[j].getName(), today);
					}
					if (fields[j].get(d) != null)
						update.set(fields[j].getName(), fields[j].get(d));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			mongo.updateFirst(Query.query(Criteria.where("_id").is(d.getDataId())), update, Data.class);
			Calendar cal = Calendar.getInstance();
			try {
				data.setTime(sdf2.parse(sdf2.format(cal.getTime())));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			mongo.insert(data, "DayData");
		}
	}

	public void insertTest(Data data) {
		mongo.insert(data, "DayData");
	}

	// @Override
	// public long finCountByDayData(Date startTime, Date endTime, String pt) {
	// String end = "";
	// Calendar cal = Calendar.getInstance();
	// cal.setTime(startTime);
	// MongoCollection<Document> col = null;
	// if (pt.equals("知乎")) {
	// if (DbDepot.dataCol != null) {
	// if (DbDepot.dataCol.get(pt) != null && DbDepot.month ==
	// cal.get(Calendar.MONTH)) {
	// col = (MongoCollection<Document>) DbDepot.dataCol.get(pt);
	// }
	// }
	// int resultCount = 0;
	// if (col == null) {
	// MongoClient mongoClient = null;
	// ServerBean ser = mongo.findOne(
	// Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("other").is("data"))),
	// ServerBean.class);
	// mongoClient = new MongoClient(new MongoClientURI(
	// "mongodb://sjzno:sjzno1q2w3e4r@" + ser.getDbHost() + ":" + ser.getDbPort() +
	// "/admin"));
	// // 连接到数据库
	// MongoDatabase mongo = mongoClient.getDatabase(ser.getDbName());
	// for (int i = 1; i < 6; i++) {
	// col = mongo.getCollection(ser.getCollection() + i);
	// Bson query = new BasicDBObject("update_at",
	// new BasicDBObject("$gte", startTime).append("$lte", endTime));
	// resultCount += col.count(query);
	// }
	// }
	// return resultCount;
	// }
	// col = getcol(startTime, endTime, pt);
	// long count = 0;
	// if (pt.equals("微博")) {
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	// Bson query = new BasicDBObject("time",
	// new BasicDBObject("$gte", sdf.format(startTime)).append("$lte",
	// sdf.format(endTime)));
	// count = col.count(query);
	// } else if (pt.equals("微信") || pt.equals("网媒") || pt.equals("论坛") ||
	// pt.equals("贴吧") || pt.equals("平媒")) {
	// Bson query = new BasicDBObject("time", new BasicDBObject("$gte",
	// startTime).append("$lte", endTime))
	// .append("pt", pt);
	// count = col.count(query);
	// } else {
	// Bson query = new BasicDBObject("time", new BasicDBObject("$gte",
	// startTime).append("$lte", endTime));
	// count = col.count(query);
	// }
	// return count;
	// }

	// @Override
	// public long finCountByDayData(Date startTime, Date endTime, Template temp) {
	// String end = "";
	// Calendar cal = Calendar.getInstance();
	// String pt = temp.getPt();
	// cal.setTime(startTime);
	// MongoCollection<Document> col = getcol(startTime, endTime, pt);
	// Bson query = new BasicDBObject("time", new BasicDBObject("$gte",
	// startTime).append("$lte", endTime))
	// .append("spyderInfoId", temp.getSpyderInfoId());
	// return col.count(query);
	// }

	// public MongoCollection<Document> getcol(Date startTime, Date endTime, String
	// pt) {
	// String end = "";
	// Calendar cal = Calendar.getInstance();
	// cal.setTime(startTime);
	// if (DbDepot.dataCol != null) {
	// if (DbDepot.dataCol.get(pt) != null && DbDepot.month ==
	// cal.get(Calendar.MONTH)) {
	// return (MongoCollection<Document>) DbDepot.dataCol.get(pt);
	// } else {
	// ServerBean ser = mongo.findOne(
	// Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("other").is("data"))),
	// ServerBean.class);
	// if (ser.isTable()) {
	// int month = cal.get(Calendar.MONTH) + 1;
	// end = cal.get(Calendar.YEAR) + "" + (month > 9 ? "" + month : "0" + month);
	// System.out.println(end);
	// }
	// MongoClient mongoClient = null;
	// if (ser.getPt().equals("微信全量")) {
	// mongoClient = new MongoClient(new MongoClientURI(
	// "mongodb://sjzzc:sjzzc1q2w3e4r@" + ser.getDbHost() + ":" + ser.getDbPort() +
	// "/admin"));
	// } else {
	// mongoClient = new MongoClient(new MongoClientURI(
	// "mongodb://sjzno:sjzno1q2w3e4r@" + ser.getDbHost() + ":" + ser.getDbPort() +
	// "/admin"));
	// }
	// // if (ser.getDbUsername() != null &&
	// // ser.getDbUsername().length() > 2) {
	// // ServerAddress sa = new ServerAddress(ser.getDbHost(),
	// // ser.getDbPort());
	// // List<MongoCredential> mongoCredentialList = new
	// // ArrayList<MongoCredential>();
	// //
	// mongoCredentialList.add(MongoCredential.createMongoCRCredential(ser.getDbUsername(),
	// // ser.getDbName(), ser.getDbPassword().toCharArray()));
	// // new MongoClient(sa, mongoCredentialList);
	// // mongoClient = new MongoClient(sa, mongoCredentialList);
	// // } else
	// // mongoClient = new MongoClient(ser.getDbHost(),
	// // ser.getDbPort());
	// // 连接到数据库
	// MongoDatabase mongo = mongoClient.getDatabase(ser.getDbName());
	// DbDepot.dataCol.put(pt, mongo.getCollection(ser.getCollection() + end));
	// DbDepot.month = cal.get(Calendar.MONTH);
	// return mongo.getCollection(ser.getCollection() + end);
	// }
	// } else {
	// DbDepot.dataCol = new ConcurrentHashMap<String, Object>();
	// ServerBean ser = mongo.findOne(
	// Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("other").is("data"))),
	// ServerBean.class);
	// if (ser.isTable()) {
	// int month = cal.get(Calendar.MONTH) + 1;
	// end = cal.get(Calendar.YEAR) + "" + (month > 9 ? "" + month : "0" + month);
	// }
	// MongoClient mongoClient = null;
	// if (ser.getDbUsername() != null) {
	// ServerAddress sa = new ServerAddress(ser.getDbHost(), ser.getDbPort());
	// List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();
	// mongoCredentialList.add(MongoCredential.createMongoCRCredential(ser.getDbUsername(),
	// ser.getDbName(),
	// ser.getDbPassword().toCharArray()));
	// mongoClient = new MongoClient(sa, mongoCredentialList);
	// } else
	// mongoClient = new MongoClient(ser.getDbHost(), ser.getDbPort());
	// MongoDatabase mongo = mongoClient.getDatabase(ser.getDbName());
	// DbDepot.dataCol.put(pt, mongo.getCollection(ser.getCollection() + end));
	// return mongo.getCollection(ser.getCollection() + end);
	// }
	// }

	public static void main(String[] args) {
		MongoClient mongoClient = new MongoClient(
				new MongoClientURI("mongodb://sjzno:sjzno1q2w3e4r@115.236.59.91:27017/admin"));
		DB db = mongoClient.getDB("mediaspider");
		DBCollection col = db.getCollection("net_media_201804");
		Date d = new Date();
		d.setHours(2);
		System.out.println(col.count(new BasicDBObject("time", new BasicDBObject("$gte", d))));

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> findTmpNameByLikeParam(String param) {
		try {
			return mongo.getCollection("Data").distinct("tempName",
					new BasicDBObject("tempName", new BasicDBObject("$regex", param)));
		} catch (Exception e) {
			return mongo.getCollection("Data").distinct("tempName",
					new BasicDBObject("tempName", new BasicDBObject("$regex", param)));
		}
	}

	// @Override
	// public List<Data> findByMethod(Date startTime, Date endTime, String method,
	// Object value) {
	// List<Data> list = new ArrayList<Data>();
	// if (DbDepot.dbCons.get("data") != null) {
	// DBCollection coll = (DBCollection) DbDepot.dbCons.get("data");
	// DBObject obj = new BasicDBObject();
	// if (method != null) {
	// obj.put(method, value);
	// obj.put("time", new BasicDBObject("$gte", startTime).append("$lt", endTime));
	// } else {
	// obj.put("time", new BasicDBObject("$gte", startTime).append("$lt", endTime));
	// }
	// DBCursor cursor = coll.find(obj).sort(new BasicDBObject("time", -1));
	// while (cursor.hasNext()) {
	// Map<String, Object> map = (Map<String, Object>) cursor.next();
	// Data d = Change.toBean(Data.class, map);
	// list.add(d);
	// }
	// cursor.close();
	// } else {
	// ServerBean sb =
	// mongo.findOne(Query.query(Criteria.where("other").is("data")),
	// ServerBean.class);
	// DBCollection coll = null;
	// if (sb.getDbUsername() == null || sb.getDbUsername().equals("")) {
	// coll = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
	// .getCollection(sb.getCollection());
	// } else {
	// coll = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(),
	// sb.getDbPassword(),
	// sb.getDbName()).getCollection(sb.getCollection());
	// DbDepot.dbCons.put(sb.getOther(), coll);
	// }
	// DBObject obj = new BasicDBObject();
	// if (method != null) {
	// obj.put(method, value);
	// obj.put("time", new BasicDBObject("$gte", startTime).append("$lt", endTime));
	// } else {
	// obj.put("time", new BasicDBObject("$gte", startTime).append("$lt", endTime));
	// }
	// DBCursor cursor = coll.find(obj).sort(new BasicDBObject("time", -1));
	// while (cursor.hasNext()) {
	// Map<String, Object> map = (Map<String, Object>) cursor.next();
	// Data d = Change.toBean(Data.class, map);
	// list.add(d);
	// }
	// cursor.close();
	// }
	// return list;
	// }
}
