package com.style24.batch.biz.job.monitoring;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbMonitoringService;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.support.env.TscConstants;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;

/**
 * 시간 모니터링
 * 
 * @author gagamel
 * @since 2020. 11. 13
 */
@Component
@Slf4j
public class TsbHourMonitoringJob extends TsbAbstractJob<Collection<GagaMap>, Collection<GagaMap>, Collection<GagaMap>> {

	@Autowired
	private TsbMonitoringService monitoringService;

	@Override
	public Collection<GagaMap> read() throws Exception {
		Collection<GagaMap> dataList = new ArrayList<>();

		GagaMap data = new GagaMap();

		// 1시간내 로그인건수
		data.setString("alarmType", TsbConstants.Monitoring.LOGIN_CNT.value());
		data.setInt("readCnt", monitoringService.getLoginCountWithinOneHour(TscConstants.Site.STYLE24.value()));
		dataList.add(data);

		// 1시간내 유입건수
		data = new GagaMap();
		data.setString("alarmType", TsbConstants.Monitoring.INFLOW_CNT.value());
		data.setInt("readCnt", monitoringService.getInthrowCountWithinOneHour(TscConstants.Site.STYLE24.value()));
		dataList.add(data);

		// 2시간내 결제건수
		data = new GagaMap();
		data.setString("alarmType", TsbConstants.Monitoring.PAY_CNT.value());
		data.setInt("readCnt", monitoringService.getPayCountWithinTwoHour());
		dataList.add(data);

		return dataList;
	}

	@Override
	public Collection<GagaMap> process(Collection<GagaMap> dataList) throws Exception {
		if (dataList == null || dataList.isEmpty()) {
			log.info("처리할 데이터가 없습니다.");
			return null;
		}

		for (GagaMap data : dataList) {
			log.info("Alarm type: {} - readCnt: {}", data.getString("alarmType"), data.getInt("readCnt"));
			if (data.getInt("readCnt") == 0) {
				monitoringService.createSystemAlarmSms(data.getString("alarmType"));
			}
		}

		return null;
	}

	@Override
	public Collection<GagaMap> write(Collection<GagaMap> dataList) throws Exception {
		return dataList;
	}

	@Override
	public void notify(Collection<GagaMap> dataList) throws Exception {
		// Do nothing
	}

}