package com.cloudcross.ssp.service.impl;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.BannerTemplate;
import com.cloudcross.ssp.service.IBannerTemplateService;
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 BannerTemplateService implements IBannerTemplateService {

	private static final Logger LOG = Logger.getLogger(BannerTemplateService.class);
	
	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public BannerTemplate findById(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("bannerTemplateSqlMapper.findById",id);
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
		//这里的搜索只是对名称进行搜索
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("bannerTemplateSqlMapper.countByParams", paramMap);
	}

	
	public List<BannerTemplate> findByParams(Map<String, Object> paramMap, Pager pager) {
		//这里的搜索只是对名称进行搜索
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("bannerTemplateSqlMapper.findByParams", paramMap);
	}
	@Override
	public boolean add(BannerTemplate t) {
		t.setUpdated(new Date());
		myBatisDao.save("bannerTemplateSqlMapper.addBannerTemplate", t);
		return true;
	}
	
	
	@Override
	public boolean save(BannerTemplate bannerTemplate,
			Map<String, Object> targetParamsMap) {
		// TODO Auto-generated method stub
		return false;
	}

	//素材替换上传更新
	@Override
	public boolean edit(BannerTemplate t) {
		Date date = new Date();
		t.setUpdated(date);
		myBatisDao.save("bannerTemplateSqlMapper.editadvertiser", t);
		//更新素材对应创意审核状态
		Long id = t.getId();
		myBatisDao.save("bannerTemplateSqlMapper.updataBannerReplace", MapBuilder.create(HashMap.class)	
						.add("id",id)
						.add("supdated",date)
						.map());
		//更新素材对应运营商审核状态
		myBatisDao.save("bannerTemplateSqlMapper.updataBannerOperatorReplace", MapBuilder.create(HashMap.class)	
				.add("id",id)
				.add("supdated",date)
				.map());
		return true;
	}
	public boolean editOperator(BannerTemplate t) {
		t.setUpdated(new Date());
		myBatisDao.save("bannerTemplateSqlMapper.editoperator", t);
		return true;
	}
	@Override
	public boolean updataIds(BannerTemplate t) {
		t.setUpdated(new Date());
		myBatisDao.save("advertiser.bannerTemplateSqlMapper.updataIds", t);
		return true;
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		Date date = new Date();
		myBatisDao.save("bannerTemplateSqlMapper.updateBannerTemplateStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", date)
				.map());
		//更形创意时间
		myBatisDao.save("bannerTemplateSqlMapper.updateBannerTime", MapBuilder.create(HashMap.class)
				.add("idList", idList)
				.add("updataTime", date)
				.map());
		//更形素材运营商素材审核时间
		myBatisDao.save("bannerTemplateSqlMapper.updateBannerOperatorTime", MapBuilder.create(HashMap.class)
						.add("idList", idList)
						.add("updataTime", date)
						.map());
		return true;

	}
	public boolean updateStatusOperator(List<Long> idList, int status) {
		myBatisDao.save("bannerTemplateSqlMapper.updateBannerTemplateStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", new Date())
				.map());
		return true;
	}
	
	@Override
	public boolean deleteUpdata(List<Long> idList, int status){
		Date date = new Date();
		//删除创意素材
		myBatisDao.save("bannerTemplateSqlMapper.updataDeleteBannerTemplateStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", date)
				.map());
		//删除创意
		myBatisDao.save("bannerTemplateSqlMapper.updataDeleteBannerStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", date)
				.map());
		//更新运营商素材状态
		myBatisDao.save("bannerTemplateSqlMapper.updataDeleteBannerTemplateOperatorStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", date)
				.map());
		
		return true;
	}

	@Override
	public boolean alterName(Map<String, Object> paramMap) {
		myBatisDao.save("bannerTemplateSqlMapper.alterName", paramMap);
		return true;
	}
	
	//@wanfu
		@Override
		public int countByParams2(Map<String, Object> paramMap) {
			//这里的搜索只是对名称进行搜索
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("key", paramMap.get("searchKey"));
			paramMap.put("value", searchValue);
			if(paramMap.get("width") == ""){ paramMap.put("width", null); }
			if(paramMap.get("height") == ""){ paramMap.put("height", null); }
			if(paramMap.get("type") == ""){ paramMap.put("type", null); }
			if(paramMap.get("status") == ""){ paramMap.put("status", null); }
			return myBatisDao.get("bannerTemplateSqlMapper.countByParams2advertiser", paramMap);
		}
		public int countByParams2Operator(Map<String, Object> paramMap) {
			//这里的搜索只是对名称进行搜索
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("key", paramMap.get("searchKey"));
			paramMap.put("value", searchValue);
			return myBatisDao.get("bannerTemplateSqlMapper.countByParams2operator", paramMap);
		}
		//@wanfu
		@Override
		public List<BannerTemplate> findByParams2(Map<String, Object> paramMap, Pager pager) {
			//这里的搜索只是对名称进行搜索
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("key", paramMap.get("searchKey"));
			paramMap.put("value", searchValue);
			paramMap.put("pager", pager);
			if(paramMap.get("width") == ""){ paramMap.put("width", null); }
			if(paramMap.get("height") == ""){ paramMap.put("height", null); }
			if(paramMap.get("type") == ""){ paramMap.put("type", null); }
			if(paramMap.get("status") == ""){ paramMap.put("status", null); }
			if(paramMap.get("advertiserId") == ""){ paramMap.put("advertiserId", null); }
//			if(paramMap.get("orderId") == ""){ paramMap.put("orderId", null); }
//			if(paramMap.get("campaignId") == ""){ paramMap.put("campaignId", null); }
//			if(paramMap.get("adgroupId") == ""){ paramMap.put("adgroupId", null); }
			return myBatisDao.getList("bannerTemplateSqlMapper.findByParams2advertiser", paramMap);
		}

		public List<BannerTemplate> findByParams2Operator(Map<String, Object> paramMap, Pager pager) {
			//这里的搜索只是对名称进行搜索
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("key", paramMap.get("searchKey"));
			paramMap.put("value", searchValue);
			paramMap.put("pager", pager);
			System.out.println("bbbb3:");	
			return myBatisDao.getList("bannerTemplateSqlMapper.findByParams2Operator", paramMap);
		}
		
		@Override
		public int countByParams3(Map<String, Object> paramMap) {
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("key", paramMap.get("searchKey"));
			paramMap.put("value", searchValue);
			if(paramMap.get("height")==""||paramMap.get("width")==""){
				paramMap.put("width", null);
				paramMap.put("height",null);
				
			}else{
				paramMap.put("width", paramMap.get("width"));
				paramMap.put("height", paramMap.get("height"));
				
			}
			if(paramMap.get("type")==""){
					paramMap.put("type",null);
			}else{
				paramMap.put("type",paramMap.get("type"));
			}
			return myBatisDao.get("bannerTemplateSqlMapper.countByParams3advertiser", paramMap);
		}		
		
		public int countByParams3Operator(Map<String, Object> paramMap) {
			return myBatisDao.get("operator.bannerTemplateSqlMapper.countByParams3operator", paramMap);
		}
	@Override
	public List<BannerTemplate> findByParams4(Map<String, Object> paramMap, Pager pager) {		paramMap.put("pager", pager);
	String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
	paramMap.put("key", paramMap.get("searchKey"));
	paramMap.put("value", searchValue);
	paramMap.put("pager", pager);
	if(paramMap.get("height")==""||paramMap.get("width")==""){
		paramMap.put("width", null);
		paramMap.put("height",null);
		
	}else{
		paramMap.put("width", paramMap.get("width"));
		paramMap.put("height", paramMap.get("height"));
		
	}
	if(paramMap.get("type")==""){
			paramMap.put("type",null);
	}else{
		paramMap.put("type",paramMap.get("type"));
	}
		return myBatisDao.getList("bannerTemplateSqlMapper.findByParams4advertiser", paramMap);
	}
	public List<BannerTemplate> findByParams4Operator(Map<String, Object> paramMap, Pager pager) {
//		//这里的搜索只是对名称进行搜索
//		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
//		paramMap.put("key", paramMap.get("searchKey"));
//		paramMap.put("value", searchValue);
		paramMap.put("pager", pager);
		return myBatisDao.getList("operator.bannerTemplateSqlMapper.findByParams4operator", paramMap);
	}

	
	



}
