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

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.Keyword;
import com.zhiweidata.bjfzxWebapp.exception.ZhiweiException;
import com.zhiweidata.bjfzxWebapp.mongoDao.KeysWordDAO;

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

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

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

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

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

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

	@Override
	public int add(List<Keyword> list) throws ZhiweiException {
		try 
		{
			mongoTemplate.insertAll(list);
			return 1;
		} 
		catch (Exception e) 
		{
			new ZhiweiException(e);
		}
		return 0;
	}

	
	@Override
	public List<Keyword> findNoShieldListByGroup(String groupName) throws ZhiweiException {
		Query query = new Query();
		query.addCriteria(Criteria.where("isShield").is(0));
		query.addCriteria(Criteria.where("groupName").is(groupName));
		
		return find(query);
	}
}
