package com.zhiwei;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mongodb.BasicDBObject;
import com.zhiwei.manage.bean.CrawTemplate;

import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.News;
import com.zhiwei.manage.dao.NewsDao;
import com.zhiwei.manage.service.CrawTemplateService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
@Component
public class NewsTest {

	@Resource(name = "newmongoTemplate")
	private MongoTemplate mongoTemplate;
	@Autowired
	private NewsDao nd;
	@Autowired
	private CrawTemplateService cd;

	public void find() throws UnsupportedEncodingException {
		Query query = new Query();
		query.addCriteria(Criteria.where("source").is("凤凰网"));
		query.with(new Sort(new Order(Direction.DESC, "time")));
		query.limit(10);
		List<News> list = mongoTemplate.find(query, News.class, "net_media_201711");
		int sum = 0;
		for (News nes : list) {
			sum += nes.getSavetime() - nes.getTime().getTime();
		}
		System.out.println(sum / 10.0 / 1000 / 60 / 60);

	}



	public void findCount() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -1);
		Criteria criteria = new Criteria();
		criteria.and("source").is("凤凰网");
		criteria.and("time").gt(c.getTime());
		System.out.println(c.getTime());
		// query.addCriteria(Criteria.where("source").is("凤凰网").orOperator(Criteria.where("time").lt(c.getTime())));
		long count = mongoTemplate.count(Query.query(criteria), News.class, "net_media_201711");
		System.out.println(count);
	}

	public void findNews() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -1);
		Criteria criteria = new Criteria();
		criteria.and("source").is("凤凰网");
		Date d = new Date();
		d.setHours(10);
		;
		criteria.and("time").gt(c.getTime()).lt(d);
		// query.addCriteria(Criteria.where("source").is("凤凰网").orOperator(Criteria.where("time").lt(c.getTime())));
		List<News> list = mongoTemplate.find(Query.query(criteria), News.class, "net_media_201711");
		for (News l : list) {
			System.out.println(l);
		}
	}

	public void finddis() {

		List<Object> list = mongoTemplate.getCollection("net_media_201711").distinct("source");
		for (Object obj : list) {
			System.out.println(obj);
		}
	}

	public void findcount() {
		// List<CrawTemplate> list=cd.findAll();
		// Calendar start=Calendar.getInstance();
		// start.add(Calendar.DATE, -1);
		// Calendar end=Calendar.getInstance();
		// int num=0;
		// //每日的数据量入库（对应模板）
		// for(CrawTemplate cr:list){
		// System.out.println(cr.toString());
		// System.out.println(TimeUtil.yearToDay(start.getTime())+","+TimeUtil.yearToDay(end.getTime()));
		// int count=(int) nd.findCount(cr.getId(), start.getTime(),
		// end.getTime());
		// Data data=new Data();
		// data.setCount(count);
		// data.setPt("网媒");
		// data.setTempName(cr.getConfigName());
		// data.setTime(TimeUtil.yearToDay(start.getTime()));
		// data.setSpyderInfoId(cr.getId());
		// data.toString();
		// System.out.println(data);
		// if(num++>10){
		// break;
		// }
		// }
	}

	public void testfindCount() {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		System.out.println("net_media_" + year + month);
		Criteria criteria = new Criteria();
		criteria.and("spyderInfoId").is("UrlListDetail29ae234f-4f47-4b1e-935f-ae8c18621d74");
		Date d = new Date();
		d.setDate(10);
		long l = mongoTemplate.count(Query.query(Criteria.where("spyderInfoId")
				.is("UrlListDetail29ae234f-4f47-4b1e-935f-ae8c18621d74").andOperator(Criteria.where("time").gt(d))),
				News.class, "net_media_" + year + month);
		System.out.println(l);
	}

	public void findConfigName() {
		List<CrawTemplate> list = mongoTemplate.findAll(CrawTemplate.class);
		for (CrawTemplate c : list) {
			System.out.println(c.getConfigName());
		}
	}

}
