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.Advertiser;
import com.cloudcross.ssp.back.service.IAdvertiserService;
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 AdvertiserService implements IAdvertiserService, ISelectorProvider{

	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public Advertiser findById(Long id) {
		return myBatisDao.get("back.advertiserSqlMapper.selectById", id);
	}
	@Override
	public long findByName(String name) {	
		return myBatisDao.get("back.advertiserSqlMapper.selectByName", name);
	}
	@Override
	public int countByParams(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("back.advertiserSqlMapper.countByParams", paramMap);
	}

	@Override
	public List<Advertiser> findByParams(Map<String, Object> paramMap,
			Pager pager) {
		// TODO Auto-generated method stub
		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.advertiserSqlMapper.selectByParams", paramMap);
	}
	
	public List<Advertiser> findByOperator(Long operatorId){
		//通过  操作人的 Id 来查找其对应的广告主
		return myBatisDao.getList("back.advertiserSqlMapper.selectByOperator", operatorId);
	}
	
	@Override
	public boolean add(Advertiser t) {
		// TODO Auto-generated method stub
		//————————————————————————————这个地方要注意一点，operator_id并没有插进去！后面需要做完善的！——————————————————————————————————————————
		t.setUpdated(new Date());
//		t.setIndustryId(new Long(1));//由于下拉框的功能还没有完善，测试用。
		myBatisDao.save("back.advertiserSqlMapper.addAdvertiser", t);
		
		return true;
	}

	@Override
	public boolean edit(Advertiser t) {
		// TODO Auto-generated method stub
		t.setUpdated(new Date());
		myBatisDao.save("back.advertiserSqlMapper.editAdvertiser", t);
		return true;
	}

	@Override
	public boolean updateStatus(List<Long> advertiserIdList, int status) {
		// TODO Auto-generated method stub
		System.out.println("yuyuyuyuyuyuyuyu----------");
		myBatisDao.save("back.advertiserSqlMapper.updateAdvertiserStatus",
				MapBuilder.create(HashMap.class)
					.add("advertiserIdList", advertiserIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}
	@Override
	public boolean updateSupdated(List<Long> advertiserIdList, int status) {
		myBatisDao.save("back.advertiserSqlMapper.updateSupdated",
				MapBuilder.create(HashMap.class)
					.add("advertiserIdList", advertiserIdList)
					.add("modifyDate", new Date())
					.add("status", status).map());
		return true;
	}

	@Override
	public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
		// TODO Auto-generated method stub
		System.err.println("yuan"+parentId);
		Assert.isTrue(ArrayUtils.isNotEmpty(parentId));
		String operaterId = parentId[0];
		List<Advertiser> advertiserList = findByAgentId(Long.parseLong(operaterId));
		return MapBuilder.create(HashMap.class).add("advertiserList", advertiserList)
				.map();
	}

	@Override
	public int countByStatus(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.get("back.advertiserSqlMapper.countByStatus", paramMap);
	}

	@Override
	public List<Advertiser> findByStatus(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.getList("back.advertiserSqlMapper.selectByStatus", paramMap);
	}

	@Override
	public double sumBalanceById(Map<String, Object> paramMap) {
		return myBatisDao.get("back.advertiserSqlMapper.sumBalanceById",paramMap);
	}
	@Override
	public List<Advertiser> findByAgentId(Long id) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("back.advertiserSqlMapper.selectByAgentId", id);
	}
	@Override
	public List<Advertiser> findDeliverEffectAnalysis(
			Map<String, Object> paramMap) {
		return myBatisDao.getList("back.advertiserSqlMapper.findDeliverEffectAnalysis", paramMap);
	}


}
