package com.zhiwei;

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

import javax.annotation.Resource;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
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.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.zhiwei.manage.bean.Data;
import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.Weibo;
import com.zhiwei.manage.dao.DataDao;
import com.zhiwei.manage.service.DataService;
import com.zhiwei.manage.util.TimeUtil;

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

	@Resource(name = "newmongoTemplate")
	private MongoTemplate mongoTemplate;
	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;
	@Resource(name = "weibomongoTemplate")
	private MongoTemplate weibo;
	@Resource
	private DataDao dataDao;
	@Autowired
	private DataService ds;

	
	public void insert() {
		Date d = new Date();
		for (int i = 0; i < 70; i++) {
			d.setDate(d.getDate()-1);
			Data data = new Data();
			data.setCount((int) (Math.random() * 190));
			data.setPt("网媒");
			data.setSource("uc");
			data.setTime(d);
			// data.setSpyderInfoId(cr.getId());
			dataDao.insert(data);
		}
	}

	public void getDatas(){
		long count =weibo.count(Query.query(Criteria.where("time").gt("2017-11-25 00:00")), Weibo.class,"status_201711");
		System.out.println(count);
	}
	
	
	public void getcount(){
		Date d=new Date();
		d.setDate(25);
		Date d2=new Date();
		d2.setDate(26);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		List<EasyNews> list= ds.findBySourceFromLibrary(d, d2, "东方财富网");//mongoTemplate.find(Query.query(Criteria.where("time").gte(d).lt(d2).and("source").is("东方财富网")),
//				EasyNews.class, "net_media_" + year + month);
		System.out.println(list.size());
	}

	public void getLike(){
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		List<String> list=dataDao.findTmpNameByLikeParam("东方");
		for(String l:list){
			System.out.println(l);
		}
	}
	@org.junit.Test
	public void find(){
		Calendar c=Calendar.getInstance();
		c.add(Calendar.DATE, -5);
		DBObject db=new BasicDBObject();
		db.put("time", new BasicDBObject("$gte",c.getTime()));
		DBCursor dbc=mongo.getCollection("Data").find();
		while(dbc.hasNext()){
			System.out.println(dbc.next().get("tempName"));
		}
		
	}
	

}
