
package com.eactive.eai.rms.bat.loader.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Repository;

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

/**
 * The Class LayoutDao.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 */
@Repository("batLoaderDao")
@SuppressWarnings("unchecked")
public class BatLoaderDaoImpl extends SqlMapClientTemplateDao {
	
    public int load(DataSourceType dt,final String table, ArrayList list) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	int count = 0;
    	HashMap hm = (HashMap)list.get(0);
		hm.put("schemaId", dt.getSchema());
    	this.template.update("BatLoader.delete"+table, hm);
    	
    	for (int i=0;i<list.size();i++){
    		hm = (HashMap)list.get(i);
    		hm.put("schemaId", dt.getSchema());
    		count = count + this.template.update("BatLoader.insert"+table, hm);
    	}
        return count;
    }
    
     @SuppressWarnings("deprecation")
	public int delete(DataSourceType dt,final String table, HashMap hm) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	//int count = 0;
		hm.put("schemaId", dt.getSchema());
		
    	return this.template.update("BatLoader.deleteTable", hm);
    }
    
    @SuppressWarnings("deprecation")
 	public int delete2Keys(DataSourceType dt,final String table, HashMap hm) {
// 		DataSourceContextHolder.setDataSourceType(dt);
     	//int count = 0;
 		hm.put("schemaId", dt.getSchema());
     	return this.template.update("BatLoader.deleteTable2Keys", hm);
     }
    @SuppressWarnings("deprecation")
 	public int delete3Keys(DataSourceType dt,final String table, HashMap hm) {
// 		DataSourceContextHolder.setDataSourceType(dt);
     	//int count = 0;
 		hm.put("schemaId", dt.getSchema());
     	return this.template.update("BatLoader.deleteTable3Keys", hm);
     }
    @SuppressWarnings("deprecation")
 	public int selectTSEAI02Count(DataSourceType dt, HashMap hm) {
// 		DataSourceContextHolder.setDataSourceType(dt);
     	//int count = 0;
 		hm.put("schemaId", dt.getSchema());
     	return (int)this.template.queryForObject("BatLoader.selectTSEAI02Count", hm);
     }
    
    public int updateNode(DataSourceType dt,String instName) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("instName", instName);
    	return this.template.update("BatLoader.updateNode", hm);
    }
    
    public int updateInst(DataSourceType dt,String type, String value) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("type", type);
		hm.put("value", value);
    	return this.template.update("BatLoader.updateInst", hm);
    }
    public List<LinkedHashMap> selectTable(DataSourceType dt,String tableName ,String key, String value) {
    	//int count = 0;
//		DataSourceContextHolder.setDataSourceType(dt);
		
    	HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("tableName", tableName);
		hm.put("key", key);
		hm.put("value", value);
    	return this.template.queryForList("BatLoader.selectTable", hm);
    } 
    public int insertTable(DataSourceType dt,final String tableName, ArrayList list) {
//		DataSourceContextHolder.setDataSourceType(dt);
    	int count = 0;
    	HashMap hm = new HashMap();


		LinkedHashMap column = (LinkedHashMap)((LinkedHashMap)list.get(0)).clone();
		ArrayList columnList = new ArrayList();
    	for (Object o:column.keySet()){
    		String key = (String)o;
    		columnList.add(key);
    	}
    	
    	LinkedHashMap<String,Object> value = new LinkedHashMap<String,Object>();
    	for (int i=0;i<list.size();i++){
    		value = (LinkedHashMap)list.get(i);
    		
        	ArrayList valueList = new ArrayList();
        	for (Object o:value.keySet()){
        		String key = (String)o;
        		valueList.add(value.get(key));
        	}
    		hm.put("schemaId", dt.getSchema());
    		hm.put("tableName", tableName);
    		hm.put("columnList", columnList);
    		hm.put("valueList", valueList);
    		count = count + this.template.update("BatLoader.insertTable", hm);
    	}
        return count;
    } 
    public void commitTransaction() {
    	try {
			this.template.getSqlMapClient().commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public List<LinkedHashMap> selectTableData( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);    
        
        return template.queryForList("BatLoader.selectTableData", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectTableData", param);
//            }
//        }.execute();
    }
    
    public List<LinkedHashMap> selectDbAdapterTableData( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);    
        
        return template.queryForList("BatLoader.adapterDbDetail", param);
    }
    
    public List<LinkedHashMap> selectTableDataLike( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        
        param.put("tableName", tableName);
        param.put("key1", key1);
        param.put("param1", param1);            
        
        return template.queryForList("BatLoader.selectTableDataLike", param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectTableDataLike", param);
//            }
//        }.execute();
    } 
    public HashMap selectSystemInfo(DataSourceType dt, String sysCode) {
    	HashMap param = new HashMap();
    	param.put("schemaId", dt.getSchema());
    	param.put("sysCode", sysCode);
    	 return (HashMap) template.queryForObject("BatLoader.selectSystemInfo", param);
    }

    public List<LinkedHashMap> selectDataForTableName( DataSourceType dataSourceType, String tableName, String key1, String param1 ) {
        final HashMap param = new HashMap();
        param.put("key1", key1);
        param.put("param1", param1);  
        
        return template.queryForList("BatLoader.selectDataFor"+tableName, param);
        
//        return new SpecifiedDataSourceExecutor<List<LinkedHashMap>>(
//                 dataSourceType) {
//            protected List<LinkedHashMap> doExecute() throws Exception {
//                return template.queryForList("Loader.selectDataFor"+tableName, param);
//            }
//        }.execute();
    }

	@SuppressWarnings("rawtypes")
	public List<LinkedHashMap> selectTableData3Key(DataSourceType dt, String tableName, String key1, String param1, String key2, String param2, String key3, String param3) {
		final HashMap param = new HashMap();
	    param.put("tableName", tableName);
	    param.put("key1", key1);
	    param.put("param1", param1);    
	    param.put("key2", key2);
	    param.put("param2", param2);    
	    param.put("key3", key2);
	    param.put("param3", param2);    
	    return template.queryForList("BatLoader.selectTableData", param);
    
	}

	public ArrayList getInterfaceListByInterfaceIds(DataSourceType dt, String[] interfaceIds) {
		DataSourceContextHolder.setDataSourceType(dt);
		HashMap hm = new HashMap();
		hm.put("schemaId", dt.getSchema());
		hm.put("interfaceIds", interfaceIds);
		return (ArrayList) this.template.queryForList("BatLoader.getInterfaceListByInterfaceIds", hm);
	}
}
