package com.zhiwei;

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.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.DBObject;
import com.zhiwei.manage.bean.Template;
import com.zhiwei.manage.dao.TemplateDao;

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

	@Autowired
	private TemplateDao ts;
	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;

	
	public void insertTemp() {
		String[] arr = { "新", "客", "端", "万", "mua", "ge", "千", "财", "能", "于", "土", "报", "日", "晚", "微", "公" };
		for (int i = 0; i < 6; i++) {
			Template t = new Template();
			t.setCreateDate(new Date());
			t.setCreator("栋宝宝");
			t.setFieldIntegrityRate(Math.random());
			t.setFieldInterval((int) (Math.random() * 50));
			t.setMail("949131080@qq.com");
			t.setNewsDelayed(4);
			t.setNewsNumber(40);
			t.setNeedPing(true);
			t.setPingInterval(4);
			t.setTemplateLv(1);
			t.setTempName(arr[(int) (Math.random() * 16)] + arr[(int) (Math.random() * 16)] + "网");
			t.setPt("网媒");
			ts.insert(t);
		}
	}

	public void findAll() {
		List<Template> list = mongo.find(Query.query(Criteria.where("templateName").regex("u")), Template.class);
		for (Template tp : list) {
			System.out.println(tp);
		}
	}

	public void findByid() {
		Template t = ts.findById("5a0d327a60a2fefec491dd4a");
		System.out.println(t.getTempName());
	}
	@org.junit.Test
	public void delete() {
		ts.delete("5a1faea360a2fe58e4edff16");
	}


	public void findBypage() {
		long count = mongo.count(Query.query(Criteria.where("creator").is("栋宝宝")), Template.class);
		System.out.println(count);
		for (int i = 0; i < count / 10; i++) {
			List<Template> list = mongo.find(Query.query(Criteria.where("creator").is("栋宝宝")).skip(i).limit(10),
					Template.class);
			for (Template tp : list) {
				System.out.println(tp);
			}
			System.out.println(
					"--------------------------------------------------------------------------------------------");
		}
	}
}
