package com.zhiwei;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.DBCursor;
import com.mongodb.DBObject;
import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.ServerBean;
import com.zhiwei.manage.dao.ServerDao;

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

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;
	@Autowired
	private ServerDao sd;
 	public void insert(){
		ServerBean sb=new ServerBean();
		sb.setCollection("Data");
		sb.setDataType("数据量");
		sb.setDbHost("192.168.0.245");
		sb.setDbUsername("");
		sb.setDbPassword("");
		sb.setDbPort(27017);
		sb.setDbType("mongo");
		sb.setEffect("数据量的统计");
		sb.setOther("Message");
		sb.setDbName("dataTest");
		sb.setPt("网媒");
		mongo.insert(sb);
	}

	public void find(){
		Calendar c=Calendar.getInstance();
		c.add(Calendar.DATE, -6);
		ServerBean sben=mongo.findOne(Query.query(Criteria.where("_id").is("5a24e4cd60a2fea23897d326")), ServerBean.class);
		List<Map<String,Object>> list=new ArrayList<>();
		DBObject obj=new BasicDBObject();
		obj.put("time", new BasicDBObject("$gt",c.getTime()));
		DBCursor dc=mongo.getCollection(sben.getCollection()).find(obj);
		while(dc.hasNext()){
			list.add((Map<String,Object>)dc.next());
		}
		for(Map<String,Object> map:list){
			Set<String> keys=map.keySet();
			for(Iterator<String> it=keys.iterator();it.hasNext();){
				String key=it.next();
				System.out.print(key+":"+map.get(key)+",");
			}
			System.out.println();
		}
	}
	
	public void update(){
		ServerBean sb=new ServerBean();
	
	}
	
}
