package com.cloudcross.ssp.service.impl;



import java.util.ArrayList;
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.model.ClientAccount;
import com.cloudcross.ssp.service.IClientAccountService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;

@Service
public class ClientAccountService implements IClientAccountService{

	@Autowired
	protected GenericIBatisDao myBatisDao;
	
	@Override
	public ClientAccount findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("value", searchValue);
		if(paramMap.get("sysType")==null || paramMap.get("sysType")=="" || Long.valueOf(paramMap.get("sysType").toString())==0){
			//查找全部的账号，不分媒体，不分代理商
			paramMap.put("value", searchValue);
			int numOperator = myBatisDao.get("clientAccountSqlMapper.countOperatorByParams", paramMap);
			int numAgent = myBatisDao.get("clientAccountSqlMapper.countAgentByParams", paramMap);
			return numAgent+numOperator;
		}else if ((Long)paramMap.get("sysType")==2) {//表示只查找媒体
			return myBatisDao.get("clientAccountSqlMapper.countOperatorByParams", paramMap);
		}else{//只查找代理商
			return myBatisDao.get("clientAccountSqlMapper.countAgentByParams", paramMap);
		}
	}
//----------------------------
//----------------------------
	@Override
	public List<ClientAccount> findByParams(Map<String, Object> paramMap,
			Pager pager) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("value", searchValue);
		int index1 = pager.getOffset();
		int index2 = pager.getPageSize();
		int page = pager.getPage();
		if (page == 0 ){
			page = 1;
		}
		if(paramMap.get("sysType")==null || paramMap.get("sysType")=="" || Long.valueOf(paramMap.get("sysType").toString())==0){
			//查找全部的账号，不分媒体，不分代理商
			paramMap.put("value", searchValue);
			List<ClientAccount> operatorAccountsList = myBatisDao.getList("clientAccountSqlMapper.findOperatorByParams", paramMap);
			List<ClientAccount> agentAccountsList = myBatisDao.getList("clientAccountSqlMapper.findAgentByParams", paramMap);
			operatorAccountsList.addAll(agentAccountsList);
			int allNum = operatorAccountsList.size();
			System.err.println("out"+allNum);
			if((allNum - (page-1)*pager.getLimit())<pager.getLimit()){//要判断一下要显示的页面上总数有多少，超过list中所有的，就要调用此处！
				List<ClientAccount> clientAccounts = operatorAccountsList.subList(index1, index1 + allNum - (page-1)*pager.getLimit());
				return clientAccounts;
			}
			List<ClientAccount> clientAccountsList = operatorAccountsList.subList(index1, index1+index2);
			return clientAccountsList;
		}else if ((Long)paramMap.get("sysType")==2) {//表示只查找媒体
			List<ClientAccount> operatorAccountsList = myBatisDao.getList("clientAccountSqlMapper.findOperatorByParams", paramMap);
			int allNum = operatorAccountsList.size();
			if((allNum - (page-1)*pager.getLimit())<pager.getLimit()){//要判断一下要显示的页面上总数有多少，超过list中所有的，就要调用此处！
				List<ClientAccount> clientAccounts = operatorAccountsList.subList(index1, index1 + allNum - (page-1)*pager.getLimit());
				return clientAccounts;
			}
			return operatorAccountsList.subList(index1, index1+index2);
		}else{//只查找代理商
			List<ClientAccount> agentAccountsList = myBatisDao.getList("clientAccountSqlMapper.findAgentByParams", paramMap);
			int allNum = agentAccountsList.size();
			if((allNum - (page-1)*pager.getLimit())<pager.getLimit()){
				List<ClientAccount> clientAccounts = agentAccountsList.subList(index1, index1 + allNum - (page-1)*pager.getLimit());
				return clientAccounts;
			}
			return agentAccountsList.subList(index1, index1+index2);
		}
	}

	@Override
	public boolean add(ClientAccount t) {
		// TODO Auto-generated method stub
		
		
		return true;
	}

	@Override
	public boolean edit(ClientAccount t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<ClientAccount> findExcelAccounts(
			HashMap<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("value", searchValue);
		List<ClientAccount> res = null;
		if(paramMap.get("sysType")==null || paramMap.get("sysType")=="" || Long.valueOf(paramMap.get("sysType").toString())==0){
			//查找全部的账号 ，不分媒体，不分代理商
			List<ClientAccount> operatorAccountsList = myBatisDao.getList("clientAccountSqlMapper.findOperatorByParams", paramMap);
			List<ClientAccount> agentAccountsList = myBatisDao.getList("clientAccountSqlMapper.findAgentByParams", paramMap);
			res = new ArrayList<ClientAccount>();
			operatorAccountsList.addAll(agentAccountsList);
			res.addAll(operatorAccountsList);
		}else if ((Long)paramMap.get("sysType")==2) {//表示只查找媒体
			res = myBatisDao.getList("clientAccountSqlMapper.findOperatorByParams", paramMap);
		}else{//只查找代理商
			res = myBatisDao.getList("clientAccountSqlMapper.findAgentByParams", paramMap);
		}
		return res;
	}
	
}
