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

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;

/**
 * The Class TransformDao.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 */
@Repository("transformDao")
@SuppressWarnings("unchecked")
public class TransformDao extends SqlMapClientTemplateDao {

    private static final Logger logger = Logger.getLogger(TransformDao.class);

    public int selectListCount(HashMap map) {
        return (Integer) this.template.queryForObject("Transform.selectListCount", map);
    }
    public int selectDupCount(HashMap map) {
        return (Integer) this.template.queryForObject("Transform.selectDupCount", map);
    }
    public List selectList(HashMap map) {
        return (List) this.template.queryForList("Transform.selectList", map);
    }
    public HashMap selectDetail(HashMap map) {
    	return (HashMap) this.template.queryForObject("Transform.selectDetail", map);
    }
    
    private String toString(Object obj) {
    	String str = null;
    	
    	if(obj instanceof Long) {
    		str = ((Long)obj).toString();
		}
		else if(obj instanceof Integer) {
			str = ((Integer)obj).toString();
		}
		else if(obj instanceof BigDecimal) {
			str = ((BigDecimal)obj).toString();
		}
		else if(obj instanceof String) {
			str = (String)obj;
		}
    	return str;
    }
    
    private int toInt(Object obj) {
    	int ret = 0;
    	if(obj instanceof Long) {
    		ret = ((Long)obj).intValue();
		}
		else if(obj instanceof Integer) {
			ret = ((Integer)obj).intValue();
		}
		else if(obj instanceof BigDecimal) {
			ret = ((BigDecimal)obj).intValue();
		}
		else if(obj instanceof String) {
			ret = Integer.parseInt((String)obj);
		}
    	
    	return ret;
    }
    
    public List selectLayoutItems(HashMap map) {
    	List<HashMap> loutList = (List) this.template.queryForList("Transform.selectLayoutItems", map);
    	
    	HashMap<String, Integer> levelMap = new HashMap<String, Integer>();
    	HashMap<String, String> pathMap = new HashMap<String, String>();
    	
    	for(HashMap<String,Object> row: loutList) {
			try {
				// level
				int level = toInt(row.get("level"));
				String id = toString(row.get("id"));
				String parent = toString(row.get("parent"));
				
				if(levelMap.get(parent) != null) {
					level = levelMap.get(parent) + 1;
					row.put("level", level +1);				
				}				
				levelMap.put(id, level);
				
				// LOUTITEMPATH
				String itemPath = (String)row.get("LOUTITEMPATH");
				if(pathMap.get(parent) != null) {
					itemPath = pathMap.get(parent) + "." + itemPath;
					row.put("LOUTITEMPATH", itemPath);
				}
				pathMap.put(id, itemPath);
				
			}
			catch(Exception ex) {
				logger.warn("Level Check Error  ", ex);
			}
    	}
    	return loutList;
        
    }

    public List selectTransformItems(HashMap map) {
        return (List) this.template.queryForList("Transform.selectTransformItems", map);
    }

    public List selectLayoutCombo(HashMap map) {
        return (List) this.template.queryForList("Transform.selectLayoutCombo",map);
    }
    public int selectLayoutListCount(HashMap map) {
        return (Integer) this.template.queryForObject("Transform.selectLayoutListCount", map);
    }

    public List selectLayoutList(HashMap map) {
        return (List) this.template.queryForList("Transform.selectLayoutList", map);
    }   
    public List selectFunctionCombo(HashMap map) {
        return (List) this.template.queryForList("Transform.selectFunctionCombo",map);
    }
    
    public List selectTransfomForEaiSvcNameCombo(HashMap map) {
        return (List) this.template.queryForList("Transform.selectTransformForEaiSvcNameCombo",map);
    }
    
    public List selectTransformSourceResult(HashMap map) {
        return (List) this.template.queryForList("Transform.selectTransformSourceResult",map);
    }
    public void deleteTransform(HashMap map) {
        this.template.delete("Transform.deleteTransform",map);
    }

    public void deleteTransformSourceResult(HashMap map) {
        this.template.delete("Transform.deleteTransformSourceResult",map);
    }
    public void insertTransformSourceResult(HashMap map) {
        this.template.insert("Transform.insertTransformSourceResult",map);
    }
    
    public void deleteTransformItem(HashMap map) {
        this.template.delete("Transform.deleteTransformItem",map);
    }
    public void insertTransformItem(HashMap map) {
        this.template.insert("Transform.insertTransformItem",map);
    }    
    public void updateTransform(HashMap map) {
        this.template.update("Transform.updateTransform",map);
    }
    public void updateTransfromEaiSvcName(HashMap map) {
        this.template.update("Transform.updateTransfromEaiSvcName",map);
    }
    public void insert(HashMap map) {
        this.template.insert("Transform.insert",map);
    }    
    public List<HashMap> selectTransform(HashMap params) {
    	return template.queryForList("Transform.selectTransform", params);
    }
    public List<HashMap> selectFR03ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectFR03ForMigration", params);
    }
    public List<HashMap> selectFR06ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectFR06ForMigration", params);
    }
    public List<HashMap> selectHE03ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectHE03ForMigration", params);
    }
    public List<HashMap> selectHE04ForMigration(HashMap<String, String> paramMap) {
		return template.queryForList("Transform.selectHE04ForMigration", paramMap);
	}
    public List<HashMap> selectHS01ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectHS01ForMigration", params);
    }
    public List<HashMap> selectHS04ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectHS04ForMigration", params);
    }
    public List<HashMap> selectHS05ForMigration(HashMap params) {
    	return template.queryForList("Transform.selectHS05ForMigration", params);
    }
    public String selectEAISvrCode(HashMap params) {
    	return (String) template.queryForObject("Transform.selectEAISvrCode", params);
    }
    public List selectListToExcel(HashMap param) throws Exception {
    	return template.queryForList("Transform.selectListToExcel", param);
    }
	public List<String> selectTransformListByLayoutList(HashMap<String, Object> paramMap) {
		return template.queryForList("Transform.selectTransformListByLayoutList", paramMap);
	}
}
