/** 
 * @Title: WeiboBusinessDAOImpl.java 
 * @Package com.zhiweidata.bjfzxWebapp.mongoDao.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午9:32:40 
 * @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 com.zhiweidata.bjfzxWebapp.entity.WeiboBusiness;
import com.zhiweidata.bjfzxWebapp.exception.ZhiweiException;
import com.zhiweidata.bjfzxWebapp.mongoDao.WeiboBusinessDao;

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

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

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

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

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

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

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

	@Override
	public List<WeiboBusiness> 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("createdAt").gte(dateSta).lte(dateEnd));
		query.addCriteria(Criteria.where("keyword").is(keyword));
		
		return find(query);
	}

	@Override
	public List<WeiboBusiness> findLastHour(String keyword) throws ZhiweiException {
		LocalDateTime sta = LocalDateTime.now();
		LocalDateTime end = sta.plusHours(-1);
		
		Query query = new Query();
		query.addCriteria(Criteria.where("createdAt").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("createdAt").gte(dateSta).lte(dateEnd));
		query.addCriteria(Criteria.where("keyword").is(keyword));
		
		return count(query);
	}
	
}
