package com.cloudcross.ssp.back.service.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.back.model.Campaign;
import com.cloudcross.ssp.back.service.ICampaignService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Assert;
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.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;



/**
 * @author Hao
 *
 */
@Service
public class CampaignService implements ICampaignService,ISelectorProvider{
	
	@Autowired
	private GenericIBatisDao ibatisDao;
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Override
	public Campaign findById(Long id) {
		return ibatisDao.get("back.campaignSqlMapper.findById", id);
	}
	@Override
	public long findByName(String name) {
		return ibatisDao.get("back.campaignSqlMapper.findByName", name);
	}

	@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 ibatisDao.get("back.campaignSqlMapper.countByParams", paramMap);
	}	

	@Override
	public List<Campaign> 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 ibatisDao.getList("back.campaignSqlMapper.findByParams", paramMap);
	}
	
	@Override
	public boolean add(Campaign t) {
		Date now = new Date();
		t.setUpDated(now);
//		t.setActiveTime(now);
//		t.setExpireTime(now);
		ibatisDao.save("back.campaignSqlMapper.addCampaign", t);
		return true;
	}

	@Override
	public boolean edit(Campaign t) {
		Date now = new Date();
		t.setUpDated(now);
		ibatisDao.save("back.campaignSqlMapper.editCampaign", t);
		return true;
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
		ibatisDao.save(
				"back.campaignSqlMapper.updateCampaignStatus",
				MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("upDated", new Date())
				.map());
         return true;
				
	}
	@Override
	public boolean updateUpstatus(List<Long> idList, int status) {
		ibatisDao.save(
				"back.campaignSqlMapper.updateCampaignStatus",
				MapBuilder.create(HashMap.class)
				.add("status", status)
				.add("idList", idList)
				.add("upDated", new Date())
				.map());
         return true;
				
	}

	
	@Override
	public List<Campaign> findByOrderId(Long orderId) {
		return ibatisDao.getList("back.campaignSqlMapper.findByOrderId", orderId);
	}

	
	/**
	 * 下拉框中搜索用到，会有两个搜索框，
	 *      分别是广告主搜索和订单搜索
	 * 
	 */		
	@Override
	public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
		Assert.isTrue(ArrayUtils.isNotEmpty(parentId));
		String orderId = parentId[0];
		List<Campaign> campaignList = findByOrderId(Long.parseLong(orderId));
		return MapBuilder.create(HashMap.class).add(orderId, campaignList).map();
	}
	
	/**
	 * 删除只是将status 的值改为 -1批量操作待完善
	 */
	
	@Override
	public boolean deleteByID(long id) {
		ibatisDao.delete("back.campaignSqlMapper.deleteByID", id);
		return true;
	}
	
	/**
	 * 通过状态查找
	 * @param paramMap
	 * @return status
	 */
	@Override
	public List<Campaign> findByStatus(Map<String, Object> paramMap) {		
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return ibatisDao.getList("back.campaignSqlMapper.selectByStatus", paramMap);
	}
	@Override
	public List<Campaign> findDeliverEffectAnalysis(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.campaignSqlMapper.findDeliverEffectAnalysis", paramMap);

	}
	

	
}
