package com.zhiwei.manage.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.bson.BSON;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
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.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.News;
import com.zhiwei.manage.handle.DbDepot;
import com.zhiwei.manage.util.MongoConnect;

@Component
public class NewsDaoImpl implements NewsDao {

	@Resource(name = "newmongoTemplate")
	private MongoTemplate mongo;

	@Override
	public List<News> findByNew(int delayedCount, String type, String source) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		if (source != null) {
			Query query = new Query();
			query.addCriteria(Criteria.where("source").is(source));
			query.with(new Sort(new Order(Direction.DESC, "time")));
			query.limit(delayedCount);
			return mongo.find(query, News.class, "net_media_" + year + (month>10?""+month:"0"+month));
		} else {
			Query query = new Query();
			query.addCriteria(Criteria.where("type").is(type));
			query.with(new Sort(new Order(Direction.DESC, "time")));
			query.limit(delayedCount);
			return mongo.find(query, News.class, "net_media_" + year + (month>10?""+month:"0"+month));
		}
	}

	@Override
	public List<News> findByFiled(int filedCount, String type, String source) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		if (source != null) {
			Query query = new Query();
			query.addCriteria(Criteria.where("source").is(source));
			query.with(new Sort(new Order(Direction.DESC, "time")));
			query.limit(filedCount);
			return mongo.find(query, News.class, "net_media_" + year + (month>10?""+month:"0"+month));
		} else {
			Query query = new Query();
			query.addCriteria(Criteria.where("type").is(type));
			query.with(new Sort(new Order(Direction.DESC, "time")));
			query.limit(filedCount);
			return mongo.find(query, News.class, "net_media_" + year + (month>10?""+month:"0"+month));
		}
	}

	@Override
	public long findCount(String spyId, Date start, Date end) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		Criteria criteria = new Criteria();
		criteria.and("spyderInfoId").is(spyId);
		criteria.and("time").gte(start).lt(end);
		return mongo.count(Query.query(criteria), News.class, "net_media_" + year + (month>10?""+month:"0"+month));
	}

	@Override
	public List<Object> findSource() {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		return mongo.getCollection("net_media_" + year + month).distinct("source", new BasicDBObject("pt", "网媒"));
	}

	@Override
	public int findBySource(String source, Date start, Date end) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		Criteria criteria = new Criteria();
		criteria.and("source").is(source);
		criteria.and("time").gte(start).lt(end);
		return (int) mongo.count(Query.query(criteria), News.class, "net_media_" + year + (month>10?""+month:"0"+month));
	}

	@Override
	public List<EasyNews> finAllNews(Date start, Date end) {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		return mongo.find(Query.query(Criteria.where("time").gte(start).lt(end)), EasyNews.class,
				"net_media_" + year + (month>10?""+month:"0"+month));
	}

	/**
	 * 获取新闻检测
	 */
	@SuppressWarnings({ "resource", "unchecked" })
	@Override
	public List<Map<String, Object>> findNews(String host, int port, String dbNamel, String collection, int count,
			String id, String pt) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		MongoCollection<Document> col = null;
		if (DbDepot.newsCol != null) {
			if (DbDepot.newsCol.get(pt) != null) {
				System.out.println("已经有，不重新创建");
				col = (MongoCollection<Document>) DbDepot.newsCol.get(pt);
			} else {
				MongoClient mongoClient = new MongoClient(host, port);
				// 连接到数据库
				MongoDatabase mongo = mongoClient.getDatabase(dbNamel);
				col = mongo.getCollection(collection);
				DbDepot.newsCol.put(pt, col);
			}
		} else {
			DbDepot.newsCol = new ConcurrentHashMap<String, Object>();
			MongoClient mongoClient = new MongoClient(host, port);
			MongoDatabase mongo = mongoClient.getDatabase(dbNamel);
			col = mongo.getCollection(collection);
			DbDepot.newsCol.put(pt, col);
		}
		if (id != null & id.length() > 5) {
			Bson sort = new BasicDBObject("rsid", -1);
			FindIterable<Document> doc = col.find(new BasicDBObject("spyderInfoId", id)).sort(sort).limit(count);
			MongoCursor<Document> mongoCursor = doc.iterator();
			while (mongoCursor.hasNext()) {
				Map<String, Object> map = mongoCursor.next();
				list.add(map);
			}
		}else{
			Bson sort = new BasicDBObject("rsid", -1);
			FindIterable<Document> doc = col.find().sort(sort).limit(count);
			MongoCursor<Document> mongoCursor = doc.iterator();
			while (mongoCursor.hasNext()) {
				Map<String, Object> map = mongoCursor.next();
				list.add(map);
			}
		}
		return list;
	}

}
