package com.bizzan.bitrade.service;

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

import com.bizzan.bitrade.dao.SysConfigDao;
import com.bizzan.bitrade.entity.SysConfig;
import com.bizzan.bitrade.service.Base.BaseService;
import com.bizzan.bitrade.util.RedisManager;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SysConfigService extends BaseService {

	@Autowired
	private SysConfigDao dao;
	
	@Autowired
	private RedisManager redisManager;
	
	public SysConfig get() {
		SysConfig redisConfig = redisManager.getSysConfig();
		SysConfig dbConfig = dao.findOne(1L);

		if(redisConfig == null || redisConfig.getFollowSpecialAmount() != dbConfig.getFollowSpecialAmount()) {
			redisManager.saveSysConfig(dbConfig);
			return dbConfig;
		}

		return redisConfig;
	}

	public SysConfig save(SysConfig sysConfig) {
		sysConfig.setId(1L);
		SysConfig saved = dao.save(sysConfig);
		redisManager.saveSysConfig(saved);
		return saved;
	}
}
