package com.goafanti.banners.service;

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.goafanti.common.dao.BannersMapper;
import com.goafanti.common.model.Banners;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;

@Service
public class BannersService extends BaseMybatisDao<BannersMapper> {
	@Autowired
	BannersMapper				bannersMapper;

	private static final Logger	logger	= LoggerFactory.getLogger(BannersService.class);

	public int insertBanner(Banners banners) {
		return bannersMapper.insertSelective(banners);
	}

	public int updateBanner(Banners banners) {
		return bannersMapper.updateByPrimaryKeySelective(banners);
	}

	public int deleteBanner(Long id) {
		return bannersMapper.deleteByPrimaryKey(id);
	}

	public List<Banners> findBannersList(String sign, Integer pageNo, Integer pageSize) {
		return portalBanners(sign, pageNo, pageSize);
	}
	
	public int findBannersListCount(String sign) {
		return bannersMapper.findBannerListCount(sign);
	}

	public List<Banners> previewPortalBanners(String key) {
		return portalBanners(key, 0, 6);
	}

	@Cacheable(value = "BannersListCache", key = "'BannersList:Key:'+#key")
	public List<Banners> findPortalBanners(String key) {
		return portalBanners(key, 0, 6);
	}

	private List<Banners> portalBanners(String sign, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(sign)) {
			params.put("sign", sign);
		}
		params.put("pageSize", pageSize);
		params.put("pageNo", pageNo);
		return bannersMapper.findBannerList(params);
	}

	@CacheEvict(value = "BannersListCache", key = "'BannersList:Key:'+#key")
	public void cleanList(String key) {
		LoggerUtils.debug(logger, "清除广告图列表缓存：[%s]", key);
	}

}
