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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mongodb.WriteResult;
import com.zhiwei.manage.bean.Person;
import com.zhiwei.manage.bean.Template;
import com.zhiwei.manage.dao.PersonDao;
import com.zhiwei.manage.dao.TemplateDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
@Component
public class Test {
	@Resource(name="mongoTemplate")
	private MongoTemplate mongo;

	@Autowired
	private PersonDao dao;
	
	@Autowired
	private TemplateDao ts;
	@org.junit.Test
	public void insert() {
		Person p = new Person();
		p.setLv(1);
		p.setPassword("123456");
		p.setUsername("user");
		p.setCreateDate(new Date());
		p.setLastLogin(new Date());
		p.setCreator("夏卡");
		mongo.insert(p);
	}
	
	
	public void find(){
		Person p=mongo.findById(0, Person.class);
		System.out.println(p.getUsername());
	}
	
	public void update(){
		Query query=Query.query(Criteria.where("_id").is(0));
		Update update=new Update();
		update.set("username","us");
		update.set("password", "pd");
		update.set("lv","1");
		WriteResult wr=mongo.updateFirst(query,update, Person.class);
		int result=wr.getN();
		System.out.println(result);
	}
	
	public void delete(){
		WriteResult wr=mongo.remove(Query.query(Criteria.where("_id").is(0)),Person.class);
		int result=wr.getN();
		System.out.println(result);
	}
	
	public void findAll(){
		List<Person> list=mongo.findAll(Person.class);
		for(Person p:list){
			System.out.println(p.getUsername());
		}
	}
	
	public void findUserName(){
		Person p=dao.findByUsername("users");
		if(p!=null)
		System.out.println(p.getUsername());
	}
	

	public void insertTemp(){
		Template t=new Template();
		t.setCreateDate(new Date());
		t.setCreator("sjz");
		t.setFieldIntegrityRate(0.66);
		t.setFieldInterval(4);
		t.setMail("949131080@qq.com");
		t.setNewsDelayed(4);
		t.setNewsNumber(40);
		t.setPingInterval(4);
		t.setTemplateLv(1);
		t.setTempName("mi");
		ts.insert(t);
	}
	
}
