package com.bizzan.bitrade.service;

import java.math.BigDecimal;
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.bizzan.bitrade.dao.SymbolPriceBiasDao;
import com.bizzan.bitrade.entity.SymbolPriceBias;
import com.bizzan.bitrade.util.RedisManager;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SymbolPriceBiasService {

	@Autowired
	private SymbolPriceBiasDao dao;
	@Autowired
	private RedisManager redisManager;
	
	public void save(SymbolPriceBias entity) {
		dao.save(entity);
	}
	
	public SymbolPriceBias get(Long id) {
		return dao.findOne(id);
	}
	
	public Map<String, BigDecimal> getBiases() {
		Map<String, BigDecimal> biasMap = redisManager.getBiasMap();
		if(biasMap!=null)
			return biasMap;
		else {
			Map<String, BigDecimal> map = new HashMap<>();
			List<SymbolPriceBias> list = dao.findAll();
			list.forEach(bias -> {
				map.put(bias.getSymbol(), bias.getBias());
			});
			redisManager.saveBiasMap(map);
			return map;
		}
	}
	
	public void updateBiases() {
		redisManager.removeBiasMap();
	}
}
