package com.cloudcross.ssp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.DspBanner;
import com.cloudcross.ssp.model.Zone;
import com.cloudcross.ssp.service.IDspBannerService;

/**
 * 创建人：rongying
 * 创建时间：2015.12.11
 * dsp-广告主-创意服务接口实现
 */

@Service
public class DspBannerService implements IDspBannerService{
	
	@Autowired
	private GenericIBatisDao myBatisDao;

	/**
	 * 添加dsp广告主对应的创意
	 * 
	 * @param list
	 *            实体对象
	 * @return <code>true</code>代表成功，否则代表失败。
	 */
	@Override
	public boolean add(List<DspBanner> list) {
		myBatisDao.save("dspBannerSqlMapper.add", MapBuilder.create(HashMap.class)
				.add("dspBannerList",list).map());
		return true;
	}

	/**
	 * 编辑dsp广告主对应的创意
	 * 
	 * @param list
	 *            实体对象
	 * @return <code>true</code>代表成功，否则代表失败。
	 */
	@Override
	public boolean update(List<DspBanner> list) {
		myBatisDao.save("dspBannerSqlMapper.update", MapBuilder.create(HashMap.class)
				.add("dspBannerList",list).map());
		return true;
	}

	/**
	 * 获取dsp广告主对应的所有创意
	 * 
	 * @param map
	 *            实体对象
	 * @return 符合条件的列表记录
	 */
	@Override
	public List<Map<String,Object>> getAll(Map<String, Object> map) {
		return myBatisDao.getList("dspBannerSqlMapper.getAll", map);
	}

	/**
	 * 根据条件获取对应的创意
	 * 
	 * @param map
	 *            实体对象
	 * @return 符合条件的列表记录
	 */
	@Override
	public List<Map<String,Object>> get(Map<String, Object> map) {
		return myBatisDao.getList("dspBannerSqlMapper.get", map);
	}
	
	/**
	 * 查询最大的id - 插入时获取
	 * 
	 * @param paramMap
	 * 				dspId和advertiserId
	 * @return 下一个id的值
	 */
	@Override
	public Long selectMaxId(Map<String, Long> paramMap) {
		// TODO Auto-generated method stub
		return myBatisDao.get("dspBannerSqlMapper.selectMaxId",paramMap);
	}
	
	/**
	 * 批量查询创意审核的状态
	 * 
	 * @param paramMap
	 * 				bannerIds
	 * @return 符合条件的记录
	 */
	public List<Map<String,Object>> queryState(Map<String,Object> paramMap){
		return myBatisDao.getList("dspBannerSqlMapper.queryState",paramMap);
	}

	
	@Override
	public int countByParams(HashMap<String, Object> paramMap) {
		
		if(!paramMap.containsKey("searchValue")||paramMap.get("searchValue").equals("")){
			paramMap.put("searchValue", null);
				
		}
		if(!paramMap.containsKey("backState")||paramMap.get("backState").equals("")){
			paramMap.put("backState", null);
		}
		
		return myBatisDao.get("dspBannerSqlMapper.countByParams",paramMap);
	}

	@Override
	public List<Map<String, Object>> findByParams(
			HashMap<String, Object> paramMap,Pager pager) {
			paramMap.put("pager", pager);
		return myBatisDao.getList("dspBannerSqlMapper.findByParams", paramMap);
	}

	@Override
	public boolean updatePass(List<Long> auditIdList, int state) {
		Date date = new Date();
		myBatisDao.save("dspBannerSqlMapper.updateBackPass",
				MapBuilder.create(HashMap.class)
					.add("auditIdList", auditIdList)
					.add("updated", date)
					.add("supdated", date.getTime()/1000)
					.add("state", state).map());
		return true;
		
		
	}

	@Override
	public boolean updateNoPass(List<Long> auditIdList, int state, String memo) {
		Date date = new Date();
		myBatisDao.save("dspBannerSqlMapper.updateBackNoPass",
				MapBuilder.create(HashMap.class)
					.add("auditIdList", auditIdList)
					.add("memo",memo)
					.add("updated", date)
					.add("supdated", date.getTime()/1000)
					.add("state", state).map());
		return true;
		
	}

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

	@Override
	public int countByParamsOperator(Long operatorId, HashMap<String, Object> paramMap) {
		
		List<Zone> zoneList = myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
		paramMap.put("zoneList", zoneList);
		if(zoneList.size()==0){
			return 0;
		}
		
		if(!paramMap.containsKey("searchValue")||paramMap.get("searchValue").equals("")){
			paramMap.put("searchValue", null);	
		}
		if(!paramMap.containsKey("operatorChecked")||paramMap.get("operatorChecked").equals("")){
			paramMap.put("operatorChecked", null);	
		}
		return myBatisDao.get("dspBannerSqlMapper.countByParamsOperator", paramMap);
	}

	@Override
	public List<Map<String, Object>> findByParamsOpeator(Long operatorId,HashMap<String, Object> paramMap, Pager pager) {
		paramMap.put("pager", pager);
		
		List<Zone> zoneList = myBatisDao.getList("zoneSqlMapper.findByOperatorIdOperator", operatorId);
		paramMap.put("zoneList", zoneList);
		if(zoneList.size()==0){
			return new ArrayList<Map<String,Object>>();
		}
		
		return myBatisDao.getList("dspBannerSqlMapper.findByParamsOperator", paramMap);
	}

	@Override
	public boolean updateOperatorPass(long id, int checked) {
		Date date = new Date();
		myBatisDao.save("dspBannerSqlMapper.updateOperatorPass",
				MapBuilder.create(HashMap.class)
					.add("bannerId", id)
					.add("updated", date)
					.add("supdated", date.getTime()/1000)
					.add("checked", checked).map());
		return true;
	}

	@Override
	public boolean updateOperatorNoPass(long id, int checked,
			String memo) {
		Date date = new Date();
		myBatisDao.save("dspBannerSqlMapper.updateOperatorNoPass",
				MapBuilder.create(HashMap.class)
					.add("bannerId", id)
					.add("memo",memo)
					.add("updated", date)
					.add("supdated", date.getTime()/1000)
					.add("checked", checked).map());
		return true;
	}

	@Override
	public int getState(long operatorId, long id) {

		return myBatisDao.get("dspBannerSqlMapper.getState", MapBuilder.create(HashMap.class)
								.add("operatorId", operatorId)
								.add("bannerId", id)
								.map());
		
	}

	@Override
	public void addBannerOperator(long operatorId, long id) {
		Date date = new Date();
		long dspId = findById(id).getDspId();
		myBatisDao.save("dspBannerSqlMapper.addBannerOperator", 
				MapBuilder.create(HashMap.class)
				.add("operatorId", operatorId)
				.add("bannerId",id)
				.add("dspId",dspId)
				.add("supdated",date)
				.map());
		
	}

	@Override
	public DspBanner findById(long id) {
		
		return myBatisDao.get("dspBannerSqlMapper.findById", id);
	}
}
