package com.iamberry.wechat.service.wechat;

import java.util.ArrayList;
import java.util.List;

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

import com.iamberry.wechat.core.entity.AdminUtils;
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.face.wechat.KeyWordService;
import com.iamberry.wechat.service.wechat.dao.KeyWordDAO;

/**
 * desc:后台，管理员service接口实现
 * @author Administrator
 *
 */
@Service
public class KeyWordServiceImpl implements KeyWordService {
	@Autowired
	private KeyWordDAO keyWordDAO;
	public void setKeyWordDAO(KeyWordDAO keyWordDAO) {
		this.keyWordDAO = keyWordDAO;
	}
	
	@Autowired
	private AdminUtils adminUtils;
	public void setAdminUtils(AdminUtils adminUtils) {
		this.adminUtils = adminUtils;
	}
	
	@Override
	public KeyWord getById(Long keyServiceNo) {
		return keyWordDAO.getById(keyServiceNo);
	}
	@Override
	public List<KeyWord> selectKeyList(String pubNo, String word, Integer pageNO, Integer pageSize) throws Exception {
		PaperBean bean = new PaperBean();
		bean.setMinNum((pageNO-1)*pageSize);
		bean.setMaxNum(pageSize);
		bean.setText(pubNo);
		bean.setStr(word);
		List<KeyWord> lists = keyWordDAO.selectKeyList(bean);
		//加密ID
		List<KeyWord> mdLists = new ArrayList<KeyWord>();
		for(KeyWord keyWord : lists) {
			String keyWordIdMd5 = adminUtils.encryptObject(keyWord.getKeyServiceNo(), "keyWordNo");
			keyWord.setKeyServiceNoMd5(keyWordIdMd5);
			mdLists.add(keyWord);
		}
		return mdLists;
	}
	
	public Integer selectKeyListCount(String pubNo, String word, Integer pageSize) {
		PaperBean bean = new PaperBean();
		bean.setText(pubNo);
		bean.setStr(word);
		int count = keyWordDAO.selectKeyListCount(bean);
		count = count % pageSize == 0? count/pageSize:(count/pageSize)+1;
		return count;
	}
	
	@Override
	public void insert(KeyWord keyWord) {
		keyWordDAO.insert(keyWord);
	}
	@Override
	public void updateByPrimaryKey(KeyWord keyword) {
		keyWordDAO.updateByPrimaryKey(keyword);
	}
	@Override
	public List<KeyWord> getBypubNoAndPageNo(PagingDTO pdto) {
		return keyWordDAO.getBypubNoAndPageNo(pdto);
	}
	@Override
	public int getPageNum(String pubNo, int selectNum) {
		return keyWordDAO.getPageNum(pubNo, selectNum);
	}
	@Override
	public KeyWord getByKeyWord(String keyword) {
		return keyWordDAO.getByKeyWord(keyword);
	}

	@Override
	public Integer batchDelKeyWordById(List<Integer> delIdList) {
		// TODO Auto-generated method stub
		return keyWordDAO.batchDelKeyWordById(delIdList);
	}

	@Override
	public Integer deleteKeyWordById(Long id) {
		// TODO Auto-generated method stub
		return keyWordDAO.deleteKeyWordById(id);
	}

	
}
