package com.cloudcross.ssp.service.impl;

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

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

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.Config;
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.model.Zone;
import com.cloudcross.ssp.service.IZoneService;
import com.cloudcross.ssp.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;

    @Service
    public class ZoneService implements IZoneService,ISelectorProvider{
	
	@Autowired
	private GenericIBatisDao myBatisDao;
	@Autowired
	Config config;
	//文件系统可上传的文件后缀集合
	private static Set<String> noStuffixSet = new HashSet<String>();
		
	//初始化可上传文件后缀
	static {
		noStuffixSet.add("jpg");
		noStuffixSet.add("gif");
	}
	
	@Override
	public boolean add( Zone t) {
		Date date = new Date();
		t.setUpdated(date);
		t.setSupdated(date);
		myBatisDao.save("zoneSqlMapper.addZone", t);		
		return true;
	}

	@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);
		paramMap.put("width", SqlHelper.doLike(paramMap.get("width")));
		paramMap.put("height", SqlHelper.doLike(paramMap.get("height")));
		paramMap.put("device", SqlHelper.doLike(paramMap.get("device")));
		paramMap.put("position", SqlHelper.doLike(paramMap.get("position")));
		paramMap.put("status", SqlHelper.doLike(paramMap.get("status")));
		return myBatisDao.get("zoneSqlMapper.countByParams", paramMap);
	}
	@Override
	public int countByParamsBack(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean edit(Zone zone) {
		Date date = new Date();
		zone.setUpdated(date);
		zone.setSupdated(date);
		myBatisDao.save("zoneSqlMapper.editZone", zone);
		return true;
	}
	@Override
	public Zone findZone(Long zoneId) {
		return myBatisDao.get("zoneSqlMapper.findZone", zoneId);	
	}
	
	@Override
	public Zone findById(Long id) {
		return myBatisDao.get("zoneSqlMapper.selectByIdOperator", id);
	}
	
	@Override
	public Zone findByIdBack(Long id) {
		return myBatisDao.get("zoneSqlMapper.selectByIdBack", id);
	}
	@Override
	public List<Zone> 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);
		paramMap.put("width", SqlHelper.doLike(paramMap.get("width")));
		paramMap.put("height", SqlHelper.doLike(paramMap.get("height")));
		paramMap.put("device", SqlHelper.doLike(paramMap.get("device")));
		paramMap.put("position", SqlHelper.doLike(paramMap.get("position")));
		paramMap.put("status", SqlHelper.doLike(paramMap.get("status")));
		return myBatisDao.getList("zoneSqlMapper.selectByParams", paramMap);
	}
	@Override
	public boolean updateStatus(List<Long> zoneIdList, int status) {
		myBatisDao.save("zoneSqlMapper.updateZoneStatus",
				MapBuilder.create(HashMap.class)
					.add("zoneIdList", zoneIdList)
					.add("updated", new Date())
					.add("status", status).map());
		return true;
	}
	
	//导出excel
	@Override
	public List<Zone> findDeliverEffectAnalysis(Map<String, Object> paramMap) {
		
		return myBatisDao.getList("zoneSqlMapper.findDeliverEffectAnalysis", paramMap);
	}
	
	
	
	@Override
	public long findByName(String name) {
		// TODO Auto-generated method stub
		return myBatisDao.get("zoneSqlMapper.findByName", name);
	}

	@Override
	public String deleteById(long id) {
		// TODO Auto-generated method stub
		return myBatisDao.get("zoneSqlMapper.deleteById", id);
	}

	@Override
	public List<Zone> findByDeviceAndPlaceId(Map<String, Object> paramMap) {
		return myBatisDao.getList("zoneSqlMapper.findByDeviceAndPlaceId", paramMap);
	}
	
	@Override
	public List<String> findAdgroupNameByZoneIdAndSchedule(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("zoneSqlMapper.findAdgroupNameByZoneIdAndSchedule", paramMap);
	}
	
	/**
	 * 首页查找设备 用到，勿删
	 */
	@Override
	public List<Zone> findByDevice(Integer device){
		// TODO Auto-generated method stub
		return myBatisDao.getList("zoneSqlMapper.findByDevice", device);
	}


	@Override
	public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
//		Assert.isTrue(ArrayUtils.isNotEmpty(parentId));
		Integer operatorId = Integer.parseInt(parentId[0]);
		List<Zone> zoneList = findByOperatorId(operatorId);
		return MapBuilder.create(HashMap.class).add("zoneList", zoneList).map();
	}
	//operatorId类型居然搞不一样

	@Override
	public List<Zone> findByOperatorId(Integer operatorId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
	}
	
	@Override
	public List<Zone> findByOperatorIdBack(Integer operatorId) {
		// TODO Auto-generated method stub
		return myBatisDao.getList("zoneSqlMapper.findByOperatorIdBack", operatorId);
	}

	@Override
	public List<Zone> findByOperatorId(Long operatorId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, List<? extends ISelector>> provideDataBack(
			String[] parentId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Zone> queryZoneByCondition(Map<String,Object>condition) {
		return myBatisDao.getList("zoneSqlMapper.queryZoneByCondition", condition);
	}
	

}

