package com.iamberry.wechat.service.wechat.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.wx.KeyWord;
import com.iamberry.wechat.core.entity.wx.PagingDTO;
import com.iamberry.wechat.service.mapper.KeyWordMapper;
import com.iamberry.wechat.service.wechat.dao.KeyWordDAO;

/**
 * desc:后台，管理员DAO接口实现
 * @author Administrator
 *
 */
@Repository
public class KeyWordDAOImpl implements KeyWordDAO {
	
	@Autowired
	private KeyWordMapper keyWordMapper;
	public void setKeyWordMapper(KeyWordMapper keyWordMapper) {
		this.keyWordMapper = keyWordMapper;
	}
	@Override
	public KeyWord getById(Long keyServiceNo) {
		return keyWordMapper.selectOne(keyServiceNo);
	}
	@Override
	public List<KeyWord> selectKeyList(PaperBean bean) {
		return keyWordMapper.selectKeyList(bean);
	}
	
	@Override
	public void insert(KeyWord keyWord) {
		keyWordMapper.insert(keyWord);
	}
	@Override
	public void updateByPrimaryKey(KeyWord keyword) {
		keyWordMapper.updateByPrimaryKey(keyword);
	}
	
	//以下是对关键字回复列表的查询
	/**
	 * 根据公众账号获取当前关键字回复的总数
	 */
	public int getAllKeyWorkNo(String pubNo) {
		return keyWordMapper.selectAllKeyWordNum(pubNo);
	}
	
	/**
	 * 每页多少条，共分多少页，
	 */
	public int getPageNum(String pubNo, int selectNum) {
		return this.getAllKeyWorkNo(pubNo) % selectNum == 0 ? this.getAllKeyWorkNo(pubNo) / selectNum : (this.getAllKeyWorkNo(pubNo) / selectNum) + 1;
	}
	
	/**
	 * 根据当前页的页码查询
	 */
	public List<KeyWord> getBypubNoAndPageNo(PagingDTO pdto) {
		int pageNo = pdto.getPageNo();
		if(pageNo < 1) {
			pageNo = 1;
			pdto.setPageNo(pageNo);
		} else if(pageNo > this.getPageNum(pdto.getWxPublicNo(), pdto.getSelectNum())) {
			pageNo = this.getPageNum(pdto.getWxPublicNo(), pdto.getSelectNum());
		}
		
		return keyWordMapper.selectKeyWordPageList(pdto);
		
	}
	@Override
	public KeyWord getByKeyWord(String keyword) {
		return keyWordMapper.getByKeyWord(keyword);
	}
	@Override
	public Integer selectKeyListCount(PaperBean bean) {
		// TODO Auto-generated method stub
		return keyWordMapper.selectKeyListCount(bean);
	}
	@Override
	public Integer batchDelKeyWordById(List<Integer> delIdList) {
		// TODO Auto-generated method stub
		return keyWordMapper.batchDelKeyWordById(delIdList);
	}
	@Override
	public Integer deleteKeyWordById(Long id) {
		// TODO Auto-generated method stub
		return keyWordMapper.deleteKeyWordById(id);
	}

}
