/** 
 * @Title: MediaAndWechatEventDAOImpl.java 
 * @Package com.zhiweidata.bjfzxWebapp.mongoDao.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午10:12:23 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.mongoDao.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import javax.annotation.Resource;

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.stereotype.Repository;

import com.zhiweidata.bjfzxWebapp.entity.MediaAndWechatEvent;
import com.zhiweidata.bjfzxWebapp.exception.ZhiweiException;
import com.zhiweidata.bjfzxWebapp.mongoDao.MediaAndWechatEventDAO;

/** 
 * @ClassName: MediaAndWechatEventDAOImpl 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午10:12:23  
 */
@Component
public class MediaAndWechatEventDAOImpl implements MediaAndWechatEventDAO{
	@Resource(name = "bjfzxDataTemplate")
	MongoTemplate mongoTemplate;
	
	@Override
	public List<MediaAndWechatEvent> findAll() throws ZhiweiException{
		try 
		{
			return mongoTemplate.findAll(MediaAndWechatEvent.class);
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return null;
	}

	@Override
	public List<MediaAndWechatEvent> find(Query query) throws ZhiweiException{
		try 
		{
			return mongoTemplate.find(query, MediaAndWechatEvent.class);
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		
		return null;
	}

	
	@Override
	public int add(MediaAndWechatEvent MediaAndWechatEvent) throws ZhiweiException{
		try 
		{
			mongoTemplate.insert(MediaAndWechatEvent);
			return 1;
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return 0;
	}

	@Override
	public long count(Query query) throws ZhiweiException{
		try 
		{
			return mongoTemplate.count(query, MediaAndWechatEvent.class);
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return 0;
	}

	@Override
	public MediaAndWechatEvent findOne(Query query) throws ZhiweiException{
		try 
		{
			return mongoTemplate.findOne(query, MediaAndWechatEvent.class);
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return null;
	}

	
	@Override
	public int update(Query query, Update update) {
		try 
		{
			mongoTemplate.upsert(query, update, MediaAndWechatEvent.class);
			return 1;
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return 0;
	}

	@Override
	public int add(List<MediaAndWechatEvent> list) throws ZhiweiException {
		try 
		{
			for (MediaAndWechatEvent mediaAndWechatEvent : list) {
				mongoTemplate.insert(mediaAndWechatEvent);
			}
			return 1;
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return 0;
	}

	@Override
	public List<MediaAndWechatEvent> findLast(String keyword, String sta, String end) throws ZhiweiException {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime dateSta = LocalDateTime.parse(sta, dtf);
		LocalDateTime dateEnd = LocalDateTime.parse(end, dtf);
		
		Query query = new Query();
		query.addCriteria(Criteria.where("time").gte(dateSta).lte(dateEnd));
		query.addCriteria(Criteria.where("keyword").is(keyword));
		
		return find(query);
	}

	@Override
	public List<MediaAndWechatEvent> findLastHour(String keyword) throws ZhiweiException {
		LocalDateTime sta = LocalDateTime.now();
		LocalDateTime end = sta.plusHours(-1);
		
		Query query = new Query();
		query.addCriteria(Criteria.where("time").gte(sta).lte(end));
		query.addCriteria(Criteria.where("keyword").is(keyword));
		return find(query);
	}

	@Override
	public long countLast(String keyword, String sta, String end) throws ZhiweiException {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime dateSta = LocalDateTime.parse(sta, dtf);
		LocalDateTime dateEnd = LocalDateTime.parse(end, dtf);
		
		Query query = new Query();
		query.addCriteria(Criteria.where("time").gte(dateSta).lte(dateEnd));
		query.addCriteria(Criteria.where("keyword").is(keyword));
		return count(query);
	}
}
