package com.zhiwei.manage.dao;

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.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.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.News;

@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 > 9 ? "" + 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 > 9 ? "" + 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 > 9 ? "" + 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 > 9 ? "" + 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 > 9 ? "" + 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 > 9 ? "" + 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 > 9 ? "" + month : "0" + month));
	}

	/**
	 * 获取新闻检测
	 */
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<Map<String, Object>> findNews(String host, int port, String dbNamel, String collection, int count,
//			String id, String pt) {
//		Calendar cal = Calendar.getInstance();
//		cal.add(Calendar.DATE, -1);
//		int month = cal.get(Calendar.MONTH) + 1;
//		String end = cal.get(Calendar.YEAR) + "" + (month > 9 ? month : "0" + month);
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		DBCollection con = mongo.getCollection(collection + end);
//		if (id != null && !id.equals("")) {
//			DBCursor cur = con.find(new BasicDBObject("spyderInfoId", id)).sort(new BasicDBObject("rsid", -1))
//					.limit(count);
//			while (cur.hasNext()) {
//				Map<String, Object> map = (Map<String, Object>) cur.next();
//				list.add(map);
//			}
//		} else {
//			DBCursor cur = con.find().sort(new BasicDBObject("rsid", -1))
//					.limit(count);
//			while (cur.hasNext()) {
//				Map<String, Object> map = (Map<String, Object>) cur.next();
//				list.add(map);
//			}
//		}
//		return list;
//	}

}
