package com.cloudcross.ssp.service.impl;



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

import javax.servlet.http.HttpServletRequest;

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

import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.AuditBanner;
import com.cloudcross.ssp.model.Banner;
import com.cloudcross.ssp.model.BannerTemplate;
import com.cloudcross.ssp.model.Log;
import com.cloudcross.ssp.model.Zone;
import com.cloudcross.ssp.service.IBannerService;
import com.cloudcross.ssp.service.ILogService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Common;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;


@Service
public class BannerService implements IBannerService {
	
	private static final Logger LOG = Logger.getLogger(BannerService.class);
	
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Autowired
	ILogService logService;
	
	@Override
	public Banner findById(Long id) {
		return myBatisDao.get("bannerSqlMapper.findByIdAdvertiser", 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("bannerSqlMapper.countByParams", paramMap);
	}

	@Override
	public List<Banner> 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);
		LOG.debug("aaaaaaaaaaaa:");
		return myBatisDao.getList("bannerSqlMapper.findByParams", paramMap);
	}
//新增创意，只新增一条创意记录  djp
	@Override
	public boolean add(Banner t) {
		Date date = new Date();
		t.setUpdated(date);
		t.setSupdated(date);
		myBatisDao.save("bannerSqlMapper.addBanner", t);
		return true;
	}
	
//编辑创意 djp
	@Override
	public boolean edit(Banner t) {
		Date date = new Date();
		t.setUpdated(date);
		t.setSupdated(date);
		t.getBannerTemplate().setUpdated(date);
		myBatisDao.save("bannerSqlMapper.editBanner", t);
		
		return true;
	}
	
	//更新创意  djp
	@Override
	public boolean updata(Banner t) {
		Date date = new Date();
		t.setUpdated(date);
		t.setSupdated(date);
		t.getBannerTemplate().setUpdated(date);
		myBatisDao.save("bannerSqlMapper.updataBanner", t);
		Long id[]={t.getId()};
		myBatisDao.save("bannerSqlMapper.updataOperatorBanner", MapBuilder.create(HashMap.class)
				.add("idList", id)
				.add("supdated", date)
				.map());
		
		return true;
	}


	@Override
	public boolean edit(Map<String, Object> paramMap) {
		
	//	myBatisDao.save("bannerSqlMapper.editBanner", t);
		
		return true;
	}
	
	//批量改变状态  djp
	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		Date date = new Date();
		//更新创意与创意模板状态
		myBatisDao.save("bannerSqlMapper.updateBannerStatus",
						MapBuilder.create(HashMap.class)
						.add("status", status)
						.add("idList", idList)
						.add("updated", date)
						.add("supdated", date)
						.map());
		/**下面为创意物理删除，暂先保留
		//删除选择创意
		myBatisDao.save("bannerSqlMapper.deleteBanner",
						MapBuilder.create(HashMap.class)
						.add("idList", idList)
						.map());
			*/
		myBatisDao.save("bannerSqlMapper.updataOperatorBanner", MapBuilder.create(HashMap.class)
				.add("idList", idList)
				.add("supdated", date)
				.map());
		return true;
	}
	
	@Override
	public int countByAdGroupId(Long adGroupId) {
		return myBatisDao.get("bannerSqlMapper.countByAdGroupId", adGroupId);
	}
	@Override
	public boolean cloneBanner(Map<String, Object> paramMap) {
		// 查询模板
		Long bannerTemplateId = (Long) paramMap.get("bannerTemplateId");
		BannerTemplate bannerTemplate = myBatisDao.get("bannerTemplateSqlMapper.findById",
				bannerTemplateId);
		bannerTemplate.setAdvertiserId((Long)paramMap.get("advertiserId"));
		bannerTemplate.setAgentId((Long)paramMap.get("agentId"));
		myBatisDao.save("bannerTemplateSqlMapper.updataIds", bannerTemplate);
		Banner banner = new Banner();
		//设置新建创意信息
		banner.setBannerTemplateId((Long)paramMap.get("bannerTemplateId"));
		banner.setAdGroupId((Long) paramMap.get("adGroupId"));
		banner.setCampaignId((Long) paramMap.get("campaignId"));
		banner.setOrderId((Long) paramMap.get("orderId"));
		banner.setVisitAddress("visitAddress");
		banner.setClickAddress("clickAddress");
		banner.setImpressionAddress("impressionAddress");
		banner.setMemo("");
		banner.setChecked(1);
		banner.setStatus(bannerTemplate.getStatus());
		Date date = new Date();
		banner.setUpdated(date);
		banner.setSupdated(date);
		banner.setBannerTemplate(bannerTemplate);
		
		LOG.debug("banner====qqq=="+banner);
	
		myBatisDao.save("bannerSqlMapper.addBanner", banner);
		
		/**
		 * 创意添加成功之后要增加日志记录
		 */
		HttpServletRequest request = (HttpServletRequest)paramMap.get("request");
		Account account = (Account)paramMap.get("account"); 
		Log log = new Log();
		//对应模块，这里应该是投放模块
		log.setModule("Ad");
		//对应的表名，这里是t_adgroup
		log.setTableName("t_banner");
		//对应的投放id
		log.setRecordId(banner.getId());
		//对应的操作类型
		log.setActionType("新增");
		//变更详情
		log.setAction("新增了创意：" + bannerTemplate.getName());
		log.setActionTime("100");
		log.setAccountId(new Long(account.getId()));
		log.setUserIp(Common.toIpAddr(request));
		log.setAccountName(account.getAccountName());
		logService.add(log);
		

		return true;
	}
	
	//根据adGroupId更新banner的supdated时间
	public boolean updateSupdated(List<Long> adGroupIdList, Date date) {
		myBatisDao.save("bannerSqlMapper.updateBannerSupdated",
				MapBuilder.create(HashMap.class)
					.add("adGroupIdList", adGroupIdList)
					.add("modifyDate",date ).map());
		return true;
	}
	
	public boolean updateSupdatedByOrderId(List<Long> orderIdList,Date date) {
		myBatisDao.save("bannerSqlMapper.updateBannerSupdatedByOrderId",
				MapBuilder.create(HashMap.class)
					.add("orderIdList", orderIdList)
					.add("modifyDate", date).map());
		
		return true;
	}

	@Override
	public boolean updateSupdatedByCampaignId(List<Long> campaignIdList, Date date) {
		myBatisDao.save("bannerSqlMapper.updateBannerSupdatedByCampaignId",
				MapBuilder.create(HashMap.class)
					.add("campaignIdList", campaignIdList)
					.add("modifyDate", date).map());
		
		return true;
	}
	
	
	public boolean updateStatusByOrderId(List<Long> orderIdList, int status) {
		myBatisDao.save("bannerSqlMapper.updateStatusByOrderId",
				MapBuilder.create(HashMap.class)
					.add("orderIdList", orderIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		
		return true;
	}

	@Override
	public boolean updateStatusByCampaignId(List<Long> campaignIdList,int status) {
		myBatisDao.save("bannerSqlMapper.updateStatusByCampaignId",
				MapBuilder.create(HashMap.class)
					.add("campaignIdList", campaignIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		
		return true;
	}

	@Override
	public boolean updateStatusByAdGroupId(List<Long> adGroupIdList, int status) {
		myBatisDao.save("bannerSqlMapper.updateStatusByAdGroupId",
				MapBuilder.create(HashMap.class)
					.add("adGroupIdList", adGroupIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		
		return true;
	}

	@Override
	public int countByParams1(Map<String, Object> paramMap) {
				return myBatisDao.get("bannerSqlMapper.countByParams1", paramMap);
	}

	@Override
	public List<Banner> findByParams1(Map<String, Object> paramMap, Pager pager) {
				paramMap.put("pager", pager);
				return myBatisDao.getList("bannerSqlMapper.findByParams1",
						paramMap);
	}

	@Override
	public List<Banner> findByParams1(Map<String, Object> paramMap) {
				return myBatisDao.getList("bannerSqlMapper.findByParams2", paramMap);
	}

	@Override
	public boolean updateStatus1(List<Long> idList, int status) {
		Date date = new Date();
		myBatisDao.save("bannerSqlMapper.updateBannerStatus",MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("updated", date)
				.add("supdated", date)
				.map());
		myBatisDao.save("bannerSqlMapper.updateOperatorBannerTime",MapBuilder.create(HashMap.class)
				.add("idList", idList)
				.add("supdated", new Date())
				.map());
		return true;
	}
	
	
	
	
	/**
	 * operator调用service
	 * */
	
	public AuditBanner findByIdOperator(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("auditBannerSqlMapper.findByIdOperator",id);
	}
	
	public boolean updateStatusOperator(List<Long> bannerIdList, int checked) {
		// TODO Auto-generated method stub
		myBatisDao.save("auditBannerSqlMapper.updateBannerStatusOperator",
				MapBuilder.create(HashMap.class)
					.add("bannerIdList", bannerIdList)
					.add("updated", new Date())
					.add("checked", checked).map());
		return true;
	}
	public int countByOperatorIdandParams(Long operatorId,Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		LOG.info("传入的参数：" + operatorId);
		List<Zone> zoneList = myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
		paramMap.put("zoneList", zoneList);
		if(zoneList.size()==0){
			return 0;
		}
		
		if(paramMap.get("searchValue")!=null){
			paramMap.put("searchValue", "%" + paramMap.get("searchValue") + "%");
		}
		if(paramMap.get("operatorChecked")!=null){
			paramMap.put("operatorChecked", paramMap.get("operatorChecked"));
		}
		System.out.println("bbb:" + paramMap.get("checked"));
		int bannerNumber = myBatisDao.get("auditBannerSqlMapper.countByOperator", paramMap);
		LOG.info("计算素材的个数：" + bannerNumber);
		return bannerNumber;
	}
	
	public List<AuditBanner> findByOperatorIdandParams(Long operatorId,Map<String, Object> paramMap, Pager pager) {
		// TODO Auto-generated method stub
		LOG.info("传入的参数：" + operatorId);
		List<Zone> zoneList = myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
		paramMap.put("zoneList", zoneList);
		paramMap.put("pager", pager);
		if(zoneList.size()==0){
			return null;
		}
		System.out.println("bbb1:" + paramMap.get("checked"));
		List<AuditBanner> bannerList = myBatisDao.getList("auditBannerSqlMapper.findByOpertor", paramMap);
		LOG.info("素材列表：" + bannerList);
		return bannerList;
	}
	public boolean addOperatorBanner(Long bannerId,int status,Long operatorId) {
		myBatisDao.save("auditBannerSqlMapper.addOperatorBanner",
				MapBuilder.create(HashMap.class)
					.add("operatorId", operatorId)
					.add("bannerId",bannerId)
					.add("supdated", new Date())
					.add("operatorChecked", status).map());
		return true;
	}
	public boolean updataOperatorBanner(Long bannerId,int status,Long operatorId) {
		myBatisDao.save("auditBannerSqlMapper.updateOperatorBanner",
				MapBuilder.create(HashMap.class)
					.add("operatorId", operatorId)
					.add("bannerId",bannerId)
					.add("supdated", new Date())
					.add("operatorChecked", status).map());
		return true;
	}
}
