package com.eactive.eai.rms.kakao.rolling;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.kakao.rolling.CurrentCommitIdService;
import com.eactive.kakao.rolling.TableSuffixResolver;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EMSTableSuffixResolverImpl implements TableSuffixResolver {

	@Autowired
	private CurrentCommitIdService currentCommitIdService;

	@Override
	public String getCurrentSuffix() {
		if (DataSourceContextHolder.getDataSourceType() == null || StringUtils
				.equalsAny(DataSourceContextHolder.getDataSourceType().getName(), DataSourceTypeManager.MONITORING,
						DataSourceTypeManager.META)) {
			return "";
		}

		try {
			String suffix = currentCommitIdService.getCurrentSuffix();
			log
					.debug("current datasource : {}, suffix : {}",
							DataSourceContextHolder.getDataSourceType().getName(), suffix);
			return suffix;
		} catch (Exception e) {
			log.error("current commit id datasource : {}", DataSourceContextHolder.getDataSourceType());
			return "";
		}
	}

}
