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.CodeClass;
import com.cloudcross.ssp.service.ICodeClassService;

@Service
public class CodeClassService implements ICodeClassService{
	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public boolean add(CodeClass codeClass) {
		// 添加一个用户。
		codeClass.setUpdated(new Date());
		myBatisDao.save("codeClassSqlMapper.addCodeClass", codeClass);
		return true;
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		String searchValue = "";
		if(paramMap.get("searchValue")!=null && !"".equals(paramMap.get("searchValue")) 
				&& !"null".equals(paramMap.get("searchValue"))){
			searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("codeName", searchValue);
		}else{
			paramMap.put("codeName", null);
		}
		//String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		//paramMap.put("key", paramMap.get("searchKey"));
		//paramMap.put("value", searchValue);
		return myBatisDao.get("codeClassSqlMapper.countByParamsadvertiser", paramMap);
	}
	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("codeClassSqlMapper.countByParamsoperator", paramMap);
	}
	@Override
	public boolean edit(CodeClass codeClass) {
		        
		codeClass.setUpdated(new Date());
		
		myBatisDao.save("codeClassSqlMapper.editCodeClass", codeClass);
		return true;
	}

	
	
	// 根据id从数据库中查询代码分类对象。
	@Override
	public CodeClass findById(Long id) {
		return myBatisDao.get("advertiser.codeClassSqlMapper.selectById", id);
	}
	

	@Override
	public List<CodeClass> findByParams(Map<String, Object> paramMap, Pager pager) {
		String searchValue = "";
		if(paramMap.get("searchValue")!=null && !"".equals(paramMap.get("searchValue")) 
				&& !"null".equals(paramMap.get("searchValue"))){
			searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("codeName", searchValue);
		}else{
			paramMap.put("codeName", null);
		}
		
		//paramMap.put("key", paramMap.get("searchKey"));
		//paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("codeClassSqlMapper.selectByParamsadvertiser", paramMap);
	}
	public List<CodeClass> findByParamsOperator(Map<String, Object> paramMap, Pager pager) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		System.out.println("key="+paramMap.get("searchKey")+",value="+searchValue);
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("codeClassSqlMapper.selectByParamsoperator", paramMap);
	}



	@Override
	public CodeClass findByName(String codeName) {
		return myBatisDao.get("codeClassSqlMapper.selectByName", codeName);
	}

	
	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		/*myBatisDao.save("advertiser.codeClassSqlMapper.updateCodeClassStatus",
				MapBuilder.create(HashMap.class)
					.add("idList", idList)	
					.add("status", status).map());*/
		System.out.println("没有sql");
		return true;
	
	
	}

	/**
	 * 根据id获取分类代码的代码值或图片
	 * @param id
	 * @param type
	 * @return
	 */
	@Override
	public String getCode(Long id, int type) {
		// TODO Auto-generated method stub
		if(type == 0){
			return myBatisDao.get("advertiser.codeClassSqlMapper.getScriptById", id);
		}else{
			return myBatisDao.get("advertiser.codeClassSqlMapper.getPicyureById", id);
		}
	}
	
}
