package com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CommonConstants;
import com.eactive.eai.rms.onl.tracking.vo.MemStaticsVo;

@XmlRootElement(name = "interface")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItsmRow {
	
	private String serviceCode; // EAI, FEP, ...
	private String serviceDesc; // EAI, 대외MCI ...
	private String serviceType; // Online, Batch
	private String serverType; // T, D, P

	private String transSuccess; // 거래처리 정상 건수
	private String systemError; // 처리 오류 건수
	private String connectError; // 통신 오류 건수
	private String timeoutError; // 타임아웃 건수

	private String sendSuccess; // 배치성 정상 송신
	private String receiveSuccess; // 배치성 정상 송수신 건수
	private String sendFail; // 배치성 거래 에러 건수
	private String receiveFail; // 배치성 거래 에러 건수
	private String sendWait; // 배치성 거래 대기 건수
	private String receiveWait;

	@XmlElement(name = "errorEvent")
	private ErrorEvent errorEvent;

	@XmlElement(name = "adapterStatus")
	private AdapterStatus adapterStatus;

	private String parseErrorMessage;

	public ItsmRow() { }
	
	public ItsmRow(Exception e) {
		this.parseErrorMessage = "Data Create Error : " + e.getMessage();
	}
	
	public static ItsmRow newOnlineInstance(String datasourceName, MemStaticsVo memTradeVoByService) {
		
		ItsmRow row = new ItsmRow();

		row.setServiceCode(datasourceName);
		switch(datasourceName) {
			case DataSourceTypeManager.EAI :
				row.setServiceDesc("EAI");
				break;
			case DataSourceTypeManager.FEP :
				row.setServiceDesc("FEP");
				break;
			case DataSourceTypeManager.MCI :
				row.setServiceDesc("MCI");
				break;
			case DataSourceTypeManager.MCU :
				row.setServiceDesc("대내MCI(비대면)");
				break;
		}
		
		String serverType = CommonConstants.SERVER_TYPE.equals(CommonConstants.DB_MANAGE_TYPE_S)? CommonConstants.DB_MANAGE_TYPE_T : CommonConstants.SERVER_TYPE;
		row.setServerType(serverType);
		
		if (memTradeVoByService != null) {
			
			int procTotal = memTradeVoByService.getProcTotal();
			int systError = memTradeVoByService.getErrorTotal();
			int connError = memTradeVoByService.getSysErrorTotal();
			int timeError = memTradeVoByService.getTimeoutTotal();
			
			int success = procTotal -systError -connError -timeError;
			
			row.setTransSuccess(String.valueOf(success));
			row.setSystemError(String.valueOf(systError));  //내부시스템에러  = bizError*
			row.setConnectError(String.valueOf(connError)); //통신에러*
			row.setTimeoutError(String.valueOf(timeError));
			
		} else {
			row.setParseErrorMessage("ERROR : 메모리에 거래통계 정보가 없습니다.");
		}
		
		return row;
	}
	
	public static ItsmRow newBatchInstance(String datasourceName, String itsmBatchInfo) {
		
		ItsmRow row = new ItsmRow();
		
		switch(datasourceName) {
			case DataSourceTypeManager.BAT :
				row.setServiceCode("EBT");
				row.setServiceDesc("EAI배치");
				break;
			case DataSourceTypeManager.BAP :
				row.setServiceCode("FBT");
				row.setServiceDesc("대외일괄전송");
				break;
		}
		
		String serverType = CommonConstants.SERVER_TYPE.equals(CommonConstants.DB_MANAGE_TYPE_S)? CommonConstants.DB_MANAGE_TYPE_T : CommonConstants.SERVER_TYPE;
		row.setServerType(serverType);
		
		if (itsmBatchInfo != null && !itsmBatchInfo.isEmpty()) {
			String[] split = itsmBatchInfo.split(",");
			row.setSendSuccess(split[0]);
			row.setReceiveSuccess(split[1]);
			row.setSendFail(split[2]);
			row.setReceiveFail(split[3]);
			row.setSendWait(split[4]);
			row.setReceiveWait(split[5]);
		} else {
			row.setParseErrorMessage("ERROR : NO DATA ON MEMORY. EMS 스케줄러를 확인해주세요.");
		}
		
		return row;
	}
	
	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	public String getServiceDesc() {
		return serviceDesc;
	}

	public void setServiceDesc(String serviceDesc) {
		this.serviceDesc = serviceDesc;
	}

	public String getServiceType() {
		return serviceType;
	}

	public void setServiceType(String serviceType) {
		this.serviceType = serviceType;
	}

	public String getTransSuccess() {
		return transSuccess;
	}

	public void setTransSuccess(String transSuccess) {
		this.transSuccess = transSuccess;
	}

	public String getSystemError() {
		return systemError;
	}

	public void setSystemError(String systemError) {
		this.systemError = systemError;
	}

	public String getConnectError() {
		return connectError;
	}

	public void setConnectError(String connectError) {
		this.connectError = connectError;
	}

	public String getTimeoutError() {
		return timeoutError;
	}

	public void setTimeoutError(String timeoutError) {
		this.timeoutError = timeoutError;
	}

	public String getSendSuccess() {
		return sendSuccess;
	}

	public void setSendSuccess(String sendSuccess) {
		this.sendSuccess = sendSuccess;
	}

	public String getReceiveSuccess() {
		return receiveSuccess;
	}

	public void setReceiveSuccess(String receiveSuccess) {
		this.receiveSuccess = receiveSuccess;
	}

	public String getSendFail() {
		return sendFail;
	}

	public void setSendFail(String sendFail) {
		this.sendFail = sendFail;
	}

	public String getReceiveFail() {
		return receiveFail;
	}

	public void setReceiveFail(String receiveFail) {
		this.receiveFail = receiveFail;
	}

	public String getSendWait() {
		return sendWait;
	}

	public void setSendWait(String sendWait) {
		this.sendWait = sendWait;
	}

	public String getReceiveWait() {
		return receiveWait;
	}

	public void setReceiveWait(String receiveWait) {
		this.receiveWait = receiveWait;
	}

	public ErrorEvent getErrorEvent() {
		return errorEvent;
	}

	public void setErrorEvent(ErrorEvent errorEvent) {
		this.errorEvent = errorEvent;
	}

	public AdapterStatus getAdapterStatus() {
		return adapterStatus;
	}

	public void setAdapterStatus(AdapterStatus adapterStatus) {
		this.adapterStatus = adapterStatus;
	}

	public String getParseErrorMessage() {
		return parseErrorMessage;
	}

	public void setParseErrorMessage(String parseErrorMessage) {
		this.parseErrorMessage = parseErrorMessage;
	}

	public String getServerType() {
		return serverType;
	}

	public void setServerType(String serverType) {
		this.serverType = serverType;
	}

	@Override
	public String toString() {
		return "ItsmRow [serviceCode=" + serviceCode + ", serviceDesc=" + serviceDesc + ", serviceType=" + serviceType
				+ ", serverType=" + serverType + ", transSuccess=" + transSuccess + ", systemError=" + systemError
				+ ", connectError=" + connectError + ", timeoutError=" + timeoutError + ", sendSuccess=" + sendSuccess
				+ ", receiveSuccess=" + receiveSuccess + ", sendFail=" + sendFail + ", receiveFail=" + receiveFail
				+ ", sendWait=" + sendWait + ", receiveWait=" + receiveWait + ", errorEvent=" + errorEvent
				+ ", adapterStatus=" + adapterStatus + ", parseErrorMessage=" + parseErrorMessage + "]";
	}

}
