package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
import com.cloudcross.ssp.service.IAccountService;

@Service
public class AccountService implements IAccountService{

	public AccountService() {
		System.out.println("初始化了。。。");
	}
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Override
	public PageView query(PageView pageView, Account t) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("t", t);
		List<Account> list = myBatisDao.getList("accountSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

	@Override
	public List<Account> queryAll(Account t) {
		// TODO Auto-generated method stub
	
		return myBatisDao.getList("accountSqlMapper.queryAll", t);
	}

	@Override
	public void delete(String id) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("accountSqlMapper.delete", id);
	}

	@Override
	public void update(Account t) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("accountSqlMapper.update", t);
	}

	@Override
	public Account getById(String id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("accountSqlMapper.getById", id);
	}

	public void add(Account account) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("accountSqlMapper.add", account);
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("accountSqlMapper.countByParams", paramMap);
	}

	@Override
	public List<Account> findByParams(Map<String, Object> paramMap, Pager pager) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("accountSqlMapper.selectByParams", paramMap);
	}

	@Override
	public Account querySingleAccount(String accountName) {
		return myBatisDao.get("accountSqlMapper.querySingleAccount",accountName);
	}

	@Override
	public Account isExist(String accountName) {
		return myBatisDao.get("accountSqlMapper.isExist", accountName);
	}

	@Override
	public boolean addAccount(Account account) {
		myBatisDao.save("accountSqlMapper.addAccount", account);
		return true;
	}

	@Override
	public Account countAccount(Account account) {
		return myBatisDao.get("accountSqlMapper.countAccount", account);
	}

	@Override
	public PageView queryNoMatch(Account account, PageView pageView) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("t", account);
		List<Account> list = myBatisDao.getList("accountSqlMapper.queryNoMatch", account);
		pageView.setRecords(list);
		return pageView;
	}

	@Override
	public boolean updateState(List<Long> accountIdList, int state) {
		myBatisDao.save("accountSqlMapper.updateAccountStatus",
				MapBuilder.create(HashMap.class)
					.add("accountIdList", accountIdList)
					.add("modifyDate", new Date())
					.add("state", state).map());
		return true;
	}

	@Override
	public boolean editAccount(Account t) {
		myBatisDao.save("accountSqlMapper.editAccount", t);
		return true;
	}

	@Override
	public List<Account> findExcelAccounts(HashMap<String, Object> paramMap) {
		return myBatisDao.getList("accountSqlMapper.findExcelAccount",paramMap);
	}

	@Override
	public boolean addAccountOperator(Account account) {
		myBatisDao.save("accountSqlMapper.addAccountOperator", account);
		return true;
	}

	@Override
	public boolean addAccountAdvertiser(Account account) {
		myBatisDao.save("accountSqlMapper.addAccountAdvertiser", account);
		return true;
	}

	@Override
	public int countByParamsOperator(Map<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("accountSqlMapper.countByParamsOperator", paramMap);
	}

	@Override
	public int countByParamsAdvertiser(Map<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("accountSqlMapper.countByParamsAdvertiser", paramMap);
	}

	@Override
	public List<Account> findByParamsOperator(Map<String, Object> paramMap,
			Pager pager) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("accountSqlMapper.selectByParamsOperator", paramMap);
	}

	@Override
	public List<Account> findByParamsAdvertiser(Map<String, Object> paramMap,
			Pager pager) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("accountSqlMapper.selectByParamsAdvertiser", paramMap);
	}

	@Override
	public boolean editAccountOperator(Account t) {
		myBatisDao.save("accountSqlMapper.editAccountOperator", t);
		return true;
	}

	@Override
	public boolean editAccountAdvertiser(Account t) {
		myBatisDao.save("accountSqlMapper.editAccountAdvertiser", t);
		return true;
	}

	@Override
	public List<Account> findExcelAccountsAdvertiser(
			HashMap<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("value"));
		paramMap.put("value", searchValue);
		return myBatisDao.getList("accountSqlMapper.findExcelAccountsAdvertiser",paramMap);
	}

	@Override
	public List<Account> findExcelAccountsOperator(
			HashMap<String, Object> paramMap) {
		return myBatisDao.getList("accountSqlMapper.findExcelAccountsOperator",paramMap);
	}

	
}
