package com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg;

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

import javax.servlet.http.HttpServletRequest;

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

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.transformer.dao.excel.ArraySheet;

@Service("ifUploadForStgCopyService")
public class IfUploadForStgCopyService extends BaseService {
	@Autowired
    private EaiMsgDao dao;
	
	private static final int ROW_APP_CD = 0;			//어플리케이션코드
	private static final int ROW_SYS_IF_CD = 1;			//시스템인터페이스코드
	private static final int ROW_SYS_IF_NAME = 2;		//시스템인터페이스명
	private static final int ROW_EAI_SEVR_CD = 5;		//서버구분코드
	private static final int ROW_SNDR_RECV_TYPE = 3;	//요청구분
	private static final int ROW_IO_TYPE = 15;			//당타발구분
	
	public List<HashMap<String,Object>> uploadFull(String data[][], HttpServletRequest request, String sheetName) throws Throwable {
		int row = data.length;
		int col = 0;

		if (row > 0) {
			col = data[0].length;
		}

		if (row < 4 || col <= 0) {
			String msg = "[INTERFACE LIST upload] Invalid array size : row=" + row + " ,col=" + col;
			logger.error(msg);
			throw new Exception(msg);
		}
		ArraySheet sheet = new ArraySheet(row, col);
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		
		//String result="O";
		for (int r = 0; r < row; r++) {
			for (int c = 0; c < col; c++) {
				sheet.setString(r, c, data[r][c]);
			}
			if(r!=0 ){
				String eaiSvcName = "";
				String sysIfCd = data[r][ROW_SYS_IF_CD];
				String apCd = data[r][ROW_APP_CD];
				String sndrRecv = (data[r][ROW_SNDR_RECV_TYPE].equals("요청"))?"S":"R";
				if(sysIfCd.equals("시스템인터페이스코드") || sysIfCd.equals("")){
					continue;
				}
				//당발 인터페이스코드 조합 > 시스템인터페이스코드 + 어플리케이션코드+요청구분+당타발구분
				//타발 				 시스템인터페이스코드 + 시스템인터페이스코드앞3자리+요청+당타발
				if(data[r][ROW_EAI_SEVR_CD].equals("내부")){	//내부일때는 취급(당발)거래밖에없음.
					eaiSvcName = sysIfCd+apCd+sndrRecv+"01";
				}else{	//대외 공동 DMZ 오픈 
					String ioType = data[r][ROW_IO_TYPE];
					if(ioType.equals("개설(타발)")){
						if(sndrRecv.equals("S")){
							eaiSvcName = sysIfCd+sysIfCd.substring(0,3)+sndrRecv+"01";
						}else if(sndrRecv.equals("R")){
							eaiSvcName = sysIfCd+sysIfCd.substring(0,3)+sndrRecv+"02";
						}
					}else if(ioType.equals("취급(당발)")){
						if(sndrRecv.equals("S")){
							eaiSvcName = sysIfCd+apCd+sndrRecv+"02";
						}else if(sndrRecv.equals("R")){
							eaiSvcName = sysIfCd+apCd+sndrRecv+"01";
						}
					}
				}
				HashMap paramMap = new HashMap();
				
				paramMap.put("searchEaiSvcName", eaiSvcName);
				int cnt = dao.selectListCount(paramMap);
				
				HashMap resMap = new HashMap();
				resMap.put("EAISVCNAME", eaiSvcName);
				resMap.put("APPCODE", apCd);
				resMap.put("SYSIFCD", sysIfCd);
				resMap.put("SYSIFNAME", data[r][ROW_SYS_IF_NAME]);
				resMap.put("EAISEVRDSTCD",data[r][ROW_EAI_SEVR_CD]);
				resMap.put("IOTYPE", data[r][ROW_IO_TYPE]);
				
				if(cnt==1){
					resMap.put("EXIST", "O");
				}else{
					resMap.put("EXIST", "X");
				}
				list.add(resMap);
			}
		}
		return list;
	}
}
