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.model.CodeConvert;
import com.cloudcross.ssp.service.ICodeConvertService;
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;

@Service
public class CodeConvertService implements ICodeConvertService{
	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public boolean add(CodeConvert codeConvert) {
	// 添加一个用户。
		codeConvert.setUpdated(new Date());
		myBatisDao.save("codeConvertSqlMapper.addCodeConvert", codeConvert);
		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("convertName", searchValue);
		}else{
			paramMap.put("convertName", null);
		}
		/*String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);*/
		return myBatisDao.get("codeConvertSqlMapper.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("codeConvertSqlMapper.countByParamsOperator", paramMap);
	}

	@Override
	public boolean edit(CodeConvert codeConvert) {
		codeConvert.setUpdated(new Date());
		myBatisDao.save("codeConvertSqlMapper.editCodeConvert", codeConvert);
		return true;
	}
	
	@Override
	public CodeConvert findById(Long id) {
		return myBatisDao.get("codeConvertSqlMapper.selectById", id);
	}
	
	@Override
	public List<CodeConvert> 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("convertName", searchValue);
		}else{
			paramMap.put("convertName", null);
		}
		/*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("codeConvertSqlMapper.selectByParamsadvertiser", paramMap);
	}
	public List<CodeConvert> 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("codeConvertSqlMapper.selectByParamsoperator", paramMap);
	}



	@Override
	public CodeConvert findByName(String convertName) {
		return myBatisDao.get("codeConvertSqlMapper.selectByName", convertName);
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
	
		
		/*myBatisDao.save("advertiser.codeConvertSqlMapper.updateCodeConvertStatus",
				MapBuilder.create(HashMap.class)
					.add("idList", idList)	
					.add("status", status).map());*/
		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("codeConvertSqlMapper.getScriptById", id);
		}else{
			return myBatisDao.get("codeConvertSqlMapper.getPicyureById", id);
		}
	}

}
