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.Operator;
import com.cloudcross.ssp.service.IOperatorService;
import com.cloudcross.ssp.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;

@Service
public class OperatorService implements IOperatorService,ISelectorProvider{

	@Autowired
	protected GenericIBatisDao myBatisDao;
	@Override
	public Operator findById(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("operatorSqlMapper.findById",id);
	}

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

	@Override
	public List<Operator> findByParams(Map<String, Object> paramMap, Pager pager) {
		// TODO Auto-generated method stub
		paramMap.put("paramMap", paramMap);
		return myBatisDao.getList("operatorSqlMapper.findByParams", paramMap);
	}
	
		@Override
	public List<Operator> findByParamsBack(Map<String, Object> paramMap, Pager pager) {
		// TODO Auto-generated method stub
		paramMap.put("pager", pager);
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("value", searchValue);
		return myBatisDao.getList("operatorSqlMapper.findByParams", paramMap);
	}
	

	@Override
	public boolean add(Operator t) {
		// TODO Auto-generated method stub
		myBatisDao.save("operatorSqlMapper.add", t);
		return true;
	}
	
	

	@Override
	public boolean edit(Operator t) {
		// TODO Auto-generated method stub
		myBatisDao.save("operatorSqlMapper.editOperator", t);
		return true;
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean updateStatusBack(List<Long> idList, int status) {
		// TODO Auto-generated method stub
		myBatisDao.save("operatorSqlMapper.updateOperatorStatus",
				MapBuilder.create(HashMap.class)
					.add("operatorIdList", idList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}
	
	public List<Operator> findAllOperator(){
		return myBatisDao.getList("operatorSqlMapper.findAllOperator");
	}
	
	@Override
	public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
		// TODO Auto-generated method stub
//		Assert.isTrue(ArrayUtils.isNotEmpty(parentId));
		String operaterId = parentId[0];
		List<Operator> operatorList = findAllOperator();
		return MapBuilder.create(HashMap.class).add("operatorList", operatorList)
				.map();
	}

	@Override
	public Operator getNew(Operator o) {
		// TODO Auto-generated method stub
		return myBatisDao.get("operatorSqlMapper.getNew", o);
	}
}
