package com.eactive.eai.rms.ext.djb.inflow;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.apim.portal.template.entity.MessageCode;
import com.eactive.eai.rms.data.entity.onl.djb.inflow.InflowTokenInsufficient;
import com.eactive.eai.rms.ext.djb.ums.UmsManager;


@Service
@Transactional
public class InflowTokenService {

	private static final Logger log = LoggerFactory.getLogger(InflowTokenService.class);

	@Autowired
	private InflowTokenRepository inflowTokenRepository;
	
	@Autowired
    private UmsManager ums; 

	public void checkRecentFails(long rangeMinute) {
		List<InflowTokenInsufficient> rows = inflowTokenRepository.countTokenInsufficient(rangeMinute);

		for (InflowTokenInsufficient info : rows) {
			log.debug("유량제어 토큰 획득 실패: {}-{} 최근 {}분 동안 {}건", info.getEaisvcname(), info.getEaisvcdesc(), rangeMinute, info.getCnt());
			String message = "유량제어 토큰 획득 실패가 발생하였습니다 \n" + info.getEaisvcname();
			HashMap<String,Object> params = new HashMap<String,Object>();
			params.put("message", message);
			ums.send("api-monitor", MessageCode.INFLOW_TOKEN_FAILED, params);
		}
	}
}
