package com.goafanti.fragment.service;

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.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goafanti.common.dao.HtmlFragmentMapper;
import com.goafanti.common.model.HtmlFragment;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;

@Service
@Transactional
public class FragmentService extends BaseMybatisDao<HtmlFragmentMapper> {
	@Autowired
	HtmlFragmentMapper htmlFragmentMapper;
	
	private static final Logger	logger	= LoggerFactory.getLogger(FragmentService.class);

	@Cacheable(value = "HtmlFragmentCache", key = "'HtmlFragment:'+#id")
	public String selectById(String id) {
		HtmlFragment hf = htmlFragmentMapper.selectByPrimaryKey(id);
		return hf == null ? "" : hf.getContent();
	}
	
	@CacheEvict(value = "HtmlFragmentCache", key = "'HtmlFragment:'+#id")
	public void clean(String id) {
		LoggerUtils.debug(logger, "清除页面缓存:[%s]", id);
	}

	@CachePut(value = "HtmlFragmentCache", key = "'HtmlFragment:'+#id")
	public String save(String id, String content) {
		HtmlFragment hf = new HtmlFragment();
		hf.setContent(content);
		hf.setId(id);
		htmlFragmentMapper.insert(hf);
		return content;
	}

}
