package com.eactive.eai.rms.service.tas;

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

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

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;

@Service("tasInfoService")
public class TasInfoService extends BaseService {

	@Autowired
	@Qualifier("tasInfoDao")
	private TasInfoDao dao;

	public HashMap selectBizAdapterInfoList(HashMap paramMap) throws Exception {
		List list = dao.selectBizAdapterInfoList(paramMap);
		HashMap map = new HashMap();
		map.put("info", list);		
		return map;
	}
	
	public HashMap selectAdapterMessageKeyInfoList(HashMap paramMap) throws Exception {
		List list = dao.selectAdapterMessageKeyInfoList(paramMap);
		HashMap map = new HashMap();
		map.put("info", list);		
		return map;
	}
	
	public HashMap selectLayoutInfo(HashMap paramMap) throws Exception {
		List list = dao.selectLayoutInfo(paramMap);
		HashMap map = new HashMap();
		map.put("info", list);		
		return map;
	}	
	
		
	
	public HashMap selectLayoutList(HashMap paramMap) throws Exception {
		List list = dao.selectLayoutList(paramMap);
		HashMap map = new HashMap();
		map.put("info", list);		
		return map;
	}
	
	public HashMap selectCallType(HashMap paramMap) throws Exception {
		List list = dao.selectCallType(paramMap);
		HashMap map = new HashMap();
		map.put("info", list);		
		return map;
	}
	
	public HashMap selectTasInfoWithBizCode(String bzwkDstcd)  throws Exception {
		HashMap<String,Object> map = new HashMap<String,Object>();
		
		HashMap resultMap = new HashMap();
		
		String error = null;
		
		do {
			if(bzwkDstcd == null || bzwkDstcd.length() < 3) {
				error = "업무코드오류";
				break;
			}
			bzwkDstcd = bzwkDstcd.trim();
			
			map.put("bzwkDstcd", bzwkDstcd);
			
			List<DataSourceType> dataTypes = DataSourceTypeManager.getOnlineDataSourceTypes();
			DataSourceContextHolder.setDataSourceType(dataTypes.get(0));
			
			ArrayList adapterList = (ArrayList)dao.selectBizAdapterInfoList(map);
			
			if(adapterList.size() == 0) {				
				error = "어댑터 미등록";
				break;
			}
			HashMap adapterInfo = (HashMap)adapterList.get(0);
			
			resultMap.put("adapterGroupName", adapterInfo.get("ADPTRBZWKGROUPNAME"));
			
			resultMap.put("apCodeName", adapterInfo.get("BZWKDSTICNAME"));
			resultMap.put("apCode", adapterInfo.get("EAIBZWKDSTCD"));
						
			resultMap.put("orgName", adapterInfo.get("ADPTRBZWKGROUPDESC"));
			resultMap.put("orgCode", adapterInfo.get("OSIDINSTINO"));
			
			resultMap.put("adapterName", adapterInfo.get("BZWKDSTNAME"));
			resultMap.put("adapter", adapterInfo.get("BZWKDSTCD"));
			
			map.put("adapterBzwkGroupName", adapterInfo.get("ADPTRBZWKGROUPNAME"));
			
			ArrayList messageKeys = (ArrayList)dao.selectAdapterMessageKeyInfoList(map);
			
			if(messageKeys.size() == 0) {				
				error = "전문종별 미등록";
				break;
			}
			else {
				HashMap messageKey = null;
				if(messageKeys.size() > 0) {
					messageKey = (HashMap)messageKeys.get(0);
					resultMap.put("telgmFldOfst", messageKey.get("MSGFLDSTARTSITUVAL"));
					resultMap.put("telgmFldLen", messageKey.get("MSGFLDLEN"));
				}
				if(messageKeys.size() > 1) {
					messageKey = (HashMap)messageKeys.get(1);
					resultMap.put("bzwkFldOfst", messageKey.get("MSGFLDSTARTSITUVAL"));
					resultMap.put("bzwkFldLen", messageKey.get("MSGFLDLEN"));				
				}
			}
			
			int offset = 0;
			int len = 0;
			HashMap layoutItem = null;
			// 취급
			map.put("eaiSvcName", bzwkDstcd+"%02");
			ArrayList layoutInfos1 = (ArrayList)dao.selectLayoutInfo(map);
			if(layoutInfos1.size() == 0) {				
				error = "취급 서비스 미등록";
				break;
			}
			HashMap layoutInfo1 = (HashMap)layoutInfos1.get(0);
			
			String bzwkFldName1 = (String)layoutInfo1.get("BZWKFLDNAME");
			
//			resultMap.put("C_LAYOUT",  layoutInfo1.get("LOUTNAME"));
//			resultMap.put("C_BZWKFLDNAME", bzwkFldName1);
			
			 
			String[] colNames = bzwkFldName1.split("\\.");
			String colName = colNames[colNames.length-1];
			
			map.put("loutName", layoutInfo1.get("LOUTNAME"));
			
			ArrayList layoutList1 = (ArrayList)dao.selectLayoutList(map);
			
			offset = 0;
			for(int i=0; i< layoutList1.size(); i++) {
				layoutItem = (HashMap)layoutList1.get(i);
				if(colName.equals(layoutItem.get("LOUTITEMNAME"))) {
					len = (int)layoutItem.get("LOUTITEMLENCNT");
					break;
				}
				offset = offset + (int)layoutItem.get("LOUTITEMLENCNT");
			}
			resultMap.put("resSerNoFldOfst",  offset);
			resultMap.put("resSerNoFldLen", len);

			// 개설		
			map.put("eaiSvcName", bzwkDstcd+"%01");
			ArrayList layoutInfos2 = (ArrayList)dao.selectLayoutInfo(map);
			
			if(layoutInfos2.size() == 0) {				
				error = "개설 서비스 미등록";
				break;
			}
			HashMap layoutInfo2 = (HashMap)layoutInfos2.get(0);
			
			String bzwkFldName2 = (String)layoutInfo2.get("BZWKFLDNAME");
			
//			resultMap.put("G_LAYOUT",  layoutInfo2.get("LOUTNAME"));
//			resultMap.put("G_BZWKFLDNAME",  bzwkFldName2);
			
			String[] colNames2 = bzwkFldName2.split("\\.");
			String colName2 = colNames2[colNames2.length-1];
			
			map.put("loutName", layoutInfo2.get("LOUTNAME"));
			
			ArrayList layoutList2 = (ArrayList)dao.selectLayoutList(map);
			
			offset = 0;
			
			for(int i=0; i< layoutList2.size(); i++) {
				layoutItem = (HashMap)layoutList2.get(i);
				if(colName2.equals(layoutItem.get("LOUTITEMNAME"))) {
					len = (int)layoutItem.get("LOUTITEMLENCNT");
					break;
				}
				offset = offset + (int)layoutItem.get("LOUTITEMLENCNT");
			}
			resultMap.put("reqSerNoFldOfst",  offset);
			resultMap.put("reqSerNoFldLen", len);
			
			
			// syncFlag
			ArrayList callList = (ArrayList)dao.selectCallType(map);
			
			String callType = "1";
			HashMap callTypeMap = (HashMap)callList.get(0);
			if("ASYN".equals(callTypeMap.get("RESPONSETYPE"))) {
				callType = "2";
			}
			resultMap.put("syncFlag", callType);
			
		} while(false);
		
		if(error != null) resultMap.put("ERROR", error);
		
		return resultMap;
	}
}

