package com.cloudcross.ssp.back.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.back.model.Account;
import com.cloudcross.ssp.back.service.IAccountService;
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.pulgin.mybatis.plugin.PageView;
@Service
public class AccountService implements IAccountService{

	@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("back.accountSqlMapper.query", map);
		pageView.setRecords(list);
		return pageView;
	}

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

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

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

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

	public void add(Account account) throws Exception {
		// TODO Auto-generated method stub
		myBatisDao.save("back.accountSqlMapper.add", account);
	}
	public boolean addAccount(Account account) {
		myBatisDao.save("back.accountSqlMapper.addAccount", account);
		return true;
	}
	@Override
	public Account querySingleAccount(String accountName) {
		// TODO Auto-generated method stub
		return myBatisDao.get("back.accountSqlMapper.querySingleAccount",accountName);
	}

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

	@Override
	public Account countAccount(Account account) {
		// TODO Auto-generated method stub
		return myBatisDao.get("back.accountSqlMapper.countAccount", account);
	}

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

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("back.accountSqlMapper.countByParams", paramMap);
	}

	@Override
	public List<Account> findByParams(Map<String, Object> paramMap, Pager pager) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("back.accountSqlMapper.selectByParams", paramMap);
	}
	
	
	//批量开启、批量停用、批量（伪）删除、操作
	@Override
	public boolean updateState(List<Long> accountIdList, int state) {
		// TODO Auto-generated method stub
		myBatisDao.save("back.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) {
		// TODO Auto-generated method stub
		System.err.println("wwwwwwwww"+t);
		myBatisDao.save("back.accountSqlMapper.editAccount", t);
		return true;
	}

	@Override
	public List<Account> findExcelAccounts(HashMap<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.accountSqlMapper.findExcelAccount",paramMap);
	}
}
