package com.zhiwei.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.zw.entity.ExcelFile;
import com.zw.entity.ReadWechat;

public class ReadWechatConnect {

	private MongoClient client;

	public ReadWechatConnect() {
		client = new MongoClient("192.168.0.11", 27217);
	}

	public void insertFileWechat(ExcelFile excel) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DB db = client.getDB(Config.getVal("rdbName"));
		DBCollection dbCollection = db.getCollection(Config.getVal("rwFileUrl"));
		DBObject obj = new BasicDBObject();
		obj.put("url", excel.getUrl());
		obj.put("mail", excel.getMail());
		obj.put("date", sdf.format(new Date()));
		dbCollection.insert(obj);
	}

	public List<ExcelFile> findFile() {
		List<ExcelFile> list = new ArrayList<ExcelFile>();
		DB db = client.getDB(Config.getVal("rdbName"));
		DBCollection dbCollection = db.getCollection(Config.getVal("rwFileUrl"));
		DBCursor dbCursor = dbCollection.find().sort(new BasicDBObject("date", -1));
		System.out.println("size" + dbCursor.size());
		while (dbCursor.hasNext()) {
			Map<String, Object> map = (Map<String, Object>) dbCursor.next();
			ExcelFile ef = new ExcelFile();
			ef.setId(String.valueOf(map.get("_id")));
			ef.setUrl(String.valueOf(map.get("url")));
			ef.setMail(String.valueOf(map.get("mail")));
			ef.setDate(String.valueOf(map.get("date")));
			list.add(ef);
		}
		return list;
	}

	public String findByid(String id) {
		DB db = client.getDB(Config.getVal("rdbName"));
		DBCollection dbCollection = db.getCollection(Config.getVal("rwFileUrl"));
		DBObject obj = dbCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
		return String.valueOf(obj.get("url"));
	}

	/**
	 * 点赞阅读的插入
	 */
	public void insertReadWechat(List<ReadWechat> list) {
		List<Document> saveList = new ArrayList<Document>();
		for (int i = 1; i <= list.size(); i++) {
			Document doc = new Document();
			doc.append("_id", "" + i);
			doc.append("url", list.get(i - 1).getUrl());
			saveList.add(doc);
		}
		MongoDatabase db = client.getDatabase(Config.getVal("rdbName"));
		MongoCollection<Document> collection = db.getCollection(Config.getVal("insertCol"));
		collection.insertMany(saveList);
		db.getCollection(Config.getVal("resultCol")).insertMany(saveList);
	}

	/**
	 * 从mango获取数据
	 * 
	 * @return
	 */
	public List<Document> findReadWechat() {
		List<Document> result = new ArrayList<Document>();
		MongoDatabase db = client.getDatabase(Config.getVal("rdbName"));
		MongoCollection<Document> collection = db.getCollection(Config.getVal("resultCol"));
		FindIterable<Document> fit = collection.find();
		MongoCursor<Document> cursor = fit.iterator();
		while (cursor.hasNext()) {
			result.add(cursor.next());
		}
		return result;
	}

	public void remove() {
		MongoDatabase db = client.getDatabase(Config.getVal("rdbName"));
		MongoCollection<Document> collection = db.getCollection(Config.getVal("resultCol"));
		collection.drop();
	}

	public static void main(String[] args) {
		ReadWechatConnect rc = new ReadWechatConnect();
		Map<String, ReadWechat> map = new HashMap<>();
		for (Document doc : rc.findReadWechat()) {
			ReadWechat rw = new ReadWechat();
			rw.setComment_num(String.valueOf(doc.get("comment_num")));
			rw.setUrl(String.valueOf(doc.get("url")));
			rw.setRead_num(String.valueOf(doc.get("read_num")));
			rw.setZan_num(String.valueOf(doc.get("zan_num")));
			System.out.println(rw);
		}
	}
}
