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.Order;
import com.cloudcross.ssp.back.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;
	

	@Override
	public Order findById(Long id) {
		return myBatisDao.get("back.orderSqlMapper.selectById", id);
	}
	
	@Override
	public long findByName(String name) {
		return myBatisDao.get("back.orderSqlMapper.selectByName", 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 myBatisDao.get("back.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("back.orderSqlMapper.selectByParams", 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("back.orderSqlMapper.addOrder", t);
		return true;
	}

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


	@Override
	public boolean updateStatus(List<Long> orderIdList, int status) {
		myBatisDao.save("back.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("back.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("back.orderSqlMapper.selectByAdvertiserType",
				advertiserType);
	}
	
	@Override
	public List<Order> findByAdvertiserId(Long advertiserId) {
		return myBatisDao.getList("back.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("back.orderSqlMapper.selectByStatus", paramMap);
	}

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

}
