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.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.Order;
import com.cloudcross.ssp.service.IAdGroupService;
import com.cloudcross.ssp.service.IBannerService;
import com.cloudcross.ssp.service.ICampaignService;
import com.cloudcross.ssp.service.IOrderService;
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;

@Service
public class OrderService implements IOrderService,ISelectorProvider {
	
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Autowired
	private ICampaignService campaignService;
	@Autowired
	private IAdGroupService adGroupService;
	@Autowired
	private IBannerService bannerService;

	

	@Override
	public Order findById(Long id) {
		return myBatisDao.get("orderSqlMapper.selectById", id);
	}
	
	@Override
	public long findByName(String name) {
		return myBatisDao.get("orderSqlMapper.selectByName", name);
	}
	
	
	
		@Override
	public int countByParamsBack(Map<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("orderSqlMapper.countByParamsBack", paramMap);
	}
		@Override
	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("orderSqlMapper.countByParamsOperator", paramMap);
	}
	
	
	
	
	
	
	
	
		@Override
	public List<Order> findByParamsOperator(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("orderSqlMapper.selectByParamsOperator", paramMap);
	}
		@Override
	public List<Order> findByParamsBack(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("orderSqlMapper.selectByParamsBack", paramMap);
	}
	
	/**
	 * @param setUpdated the setUpdated to set
	 */
	@Override
	public boolean add(Order t) {
		t.setAdvertiserId(t.getAdvertiserId());
		//t.setStatus(Status.enable.value);
		t.setUpdated(new Date());
		myBatisDao.save("orderSqlMapper.addOrder", t);
		return true;
	}
	
		@Override
	public boolean addBack(Order t) {
		t.setAdvertiserId(t.getAdvertiserId());
		//t.setStatus(Status.enable.value);
		t.setUpdated(new Date());
		myBatisDao.save("orderSqlMapper.addOrderBack", t);
		return true;
	}
	

	@Override
	public boolean edit(Order t) {
		t.setUpdated(new Date());
		myBatisDao.save("orderSqlMapper.editOrder", t);
		return true;
	}


	@Override
	public boolean updateStatus(List<Long> orderIdList, int status) {
		myBatisDao.save("orderSqlMapper.updateOrderStatus",
				MapBuilder.create(HashMap.class)
					.add("orderIdList", orderIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}
	
	@Override
	//改变投放的Supdated
	public boolean updateSupdated(List<Long> orderIdList, int status) {
		myBatisDao.save("orderSqlMapper.updateSupdated",
				MapBuilder.create(HashMap.class)
					.add("orderIdList", orderIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}

	
	@Override
	public List<Order> findByAdvertiserType(String advertiserType) {
		return myBatisDao.getList("orderSqlMapper.selectByAdvertiserType",
				advertiserType);
	}
	
	@Override
	public List<Order> findByAdvertiserId(Long advertiserId) {
		return myBatisDao.getList("orderSqlMapper.findByAdvertiserId", advertiserId);
	}
	
	@Override
	public Map<String, List<? extends ISelector>> provideData(String[] advertiserId) {
		Assert.isTrue(ArrayUtils.isNotEmpty(advertiserId));
		String advertiserType = advertiserId[0];
		List<Order> orderList = findByAdvertiserType(advertiserType);
		return MapBuilder.create(HashMap.class).add("orderList", orderList).map();
	}

	@Override
	public List<Order> findByStatus(Map<String, Object> paramMap) {
		
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.getList("orderSqlMapper.selectByStatus", paramMap);
	}

	@Override
	public List<Order> findDeliverEffectAnalysis(Map<String, Object> paramMap) {
		return myBatisDao.getList("orderSqlMapper.findDeliverEffectAnalysis", paramMap);
	}

	@Override
	public String selectOrderNameById(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("orderSqlMapper.selectOrderNameById", id);
	}
	@Override
	public boolean updateSupdatedAll(List<Long> orderIdList, Date date) {
		Boolean adgroupFlag = this.adGroupService.updateSupdatedByOrderId(orderIdList, date);
		Boolean bannerFlag = this.bannerService.updateSupdatedByOrderId(orderIdList, date);
		return adgroupFlag&&bannerFlag;
	}

	@Override
	public boolean updateStatusByAdvertiserId(List<Long> advertiserIdList,int status) {
		myBatisDao.save("orderSqlMapper.updateStatusByAdvertiserId",
				MapBuilder.create(HashMap.class)
					.add("advertiserIdList", advertiserIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}
	
	public boolean updateStatusAll(List<Long> orderIdList, int status) {
		Boolean orderFlag = updateStatus(orderIdList,status);
		Boolean campaignFlag = this.campaignService.updateStatusByOrderId(orderIdList, status);
		Boolean adGroupFlag =this.adGroupService.updateStatusByOrderId(orderIdList, status);
		Boolean bannerFlag = this.bannerService.updateStatusByOrderId(orderIdList, status);
		return orderFlag&&campaignFlag&&adGroupFlag&&bannerFlag;
	}
	

	@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("orderSqlMapper.countByParams", paramMap);
	}
	
	@Override
	public List<Order> 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("orderSqlMapper.selectByParams", paramMap);
	}
}
