package com.eactive.eai.rms.onl.manage.rule.upload;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
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.OnlBaseService;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;

@Service("ioMapLoaderForMonitoringService")
public class IoMapLoaderForMonitoringService  extends OnlBaseService {

    private static final Logger logger = Logger.getLogger("DYNAMIC_LOGGER");
    
    @Autowired
    @Qualifier("ioMapLoaderForMonitoringDao") 
    private IoMapLoaderForMonitoringDao dao; 
    
    /**
     * CM 호출시 적재할 화일이 있는지 체크함
     * 
     * 대상 파일이 1개라도 있는면 정상으로 하고 리턴한다.
     * @param downloadPath
     * @param apCode
     * @param pkgNumber
     * @return
     */
    public boolean checkFileList(String downloadPath, String apCode, String pkgNumber){
    	String filePath = "";
    	//서버구분코드별 검색
    	for(DataSourceType dt : DataSourceTypeManager.getOnlineDataSourceTypes()){
    		filePath = downloadPath + apCode + File.separator + pkgNumber + File.separator + dt.getName() + File.separator;
    		logger.debug(" filePath : " + filePath);
    		try {
    			File file = new File(filePath);
    			if (!file.exists()){
    				continue;
    			}
    			
    			file = new File(filePath + IoMapLoaderKey.LAYOUT);
    			File[] fileList = file.listFiles();
    			if(fileList != null && fileList.length > 0 ){
    				return true;
    			}
    			file = new File(filePath + IoMapLoaderKey.TRANSFORM);
    			fileList = file.listFiles();
    			if(fileList != null && fileList.length > 0) {
    				return true;
    			}
    		} catch (Exception e) {
    			return false;
    		}
    	}
    	return false;
    }
    public void loadTSEAIRM29(String eaiBzwkDstcd, String modfiMgtPckagNo, String modfiMgtModfiYMS, String dteCnvsnYn 
    		, String eaiAplyYn, String chmnErrcd, String chmnKndDstcd ) throws Exception{
    	HashMap<String,String> param = new HashMap<String, String>();
    	param.put("eaiBzwkDstcd", eaiBzwkDstcd);//업무구분코드
    	param.put("modfiMgtPckagNo", modfiMgtPckagNo);//패키지 번호
    	param.put("hostName", " ");//호스트명
    	param.put("modfiMgtModfiYMS", modfiMgtModfiYMS);//실행일자
    	param.put("dteCnvsnYn", dteCnvsnYn);//일자전환여부
    	param.put("eaiAplyYn", eaiAplyYn);//EAI적용여부
    	param.put("chmnErrcd", chmnErrcd);//에러코드
    	param.put("chmnKndDstcd", chmnKndDstcd);//cm종류
    	dao.delete(param);
    	dao.insert(param);
    	
    }
    public List<HashMap> selectTSEAIRM29(String dteCnvsnYn , String eaiAplyYn ) throws Exception{
    	HashMap<String,String> param = new HashMap<String, String>();
    	param.put("dteCnvsnYn", dteCnvsnYn);//일자전환여부
    	param.put("eaiAplyYn", eaiAplyYn);//EAI적용여부
    	return dao.selectForCmType(param);
    	
    }
    public void updateTSEAIRM29(String modfiMgtPckagNo, String dteCnvsnYn , String eaiAplyYn, String chmnErrcd ) throws Exception{
    	HashMap<String,String> param = new HashMap<String, String>();
    	param.put("modfiMgtPckagNo", modfiMgtPckagNo);//패키지 번호
    	param.put("dteCnvsnYn", dteCnvsnYn);//일자전환여부
    	param.put("eaiAplyYn", eaiAplyYn);//EAI적용여부
    	param.put("chmnErrcd", chmnErrcd);//에러코드
    	dao.update(param);
    	
    }
	public List selectIocmRst(String searchApCode, String searchPkgNumber, String searchStartDate, String searchEndDate) throws Exception {
		Map params = new HashMap();
		params.put("searchApCode", searchApCode.toUpperCase());
		params.put("searchPkgNumber", searchPkgNumber.toUpperCase());
		params.put("searchStartDate", searchStartDate.replaceAll("-", ""));
		params.put("searchEndDate", searchEndDate.replaceAll("-", ""));
		return dao.selectIocmRst(params);
	}

}
