package com.uas.account.service;

import java.util.List;

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.uas.account.core.Const;
import com.uas.account.dao.SettingDao;
import com.uas.account.entity.Setting;
import com.uas.account.exception.VisibleError;

@Service
public class SettingServiceImpl implements SettingService {

	@Autowired
	private SettingDao settingDao;

	@Override
	@CacheEvict(value = "setting", key = "#setting.key")
	public Setting save(Setting setting) {
		return settingDao.save(setting);
	}

	@Override
	@CacheEvict(value = "setting", allEntries = true)
	public void save(List<Setting> settings) {
		settingDao.save(settings);
	}

	@Override
	@Cacheable(value = "setting")
	public Setting findOne(String key) {
		return settingDao.findOne(key);
	}

	@Override
	@CacheEvict(value = "setting")
	public void delete(String key) {
		Setting setting = findOne(key);
		if (setting != null) {
			if (Const.NO == setting.getDeleteable()) {
				throw new VisibleError("该设置不允许删除");
			}
			settingDao.delete(key);
		}
	}

	@Override
	public List<Setting> findAll() {
		return settingDao.findAll();
	}

}
