package com.eactive.eai.rms.bat.common.service;

import java.util.HashMap;

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

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

@Repository("batLogTableDao")
public class BatLogTableDao  extends SqlMapClientTemplateDao {

    final Logger logger = Logger.getLogger(BatLogTableDao.class);
    
    @SuppressWarnings("deprecation")
	public long [] clearLogTable(HashMap<String, String> paramMap, int keyLevel) {

    	long [] cnt = new long[4];
    	
    	cnt[0] = 0;
    	cnt[1] = 0;
    	cnt[2] = 0;
    	cnt[3] = 0;
    	
    	for (int inx = 0; inx < power( 16, keyLevel); inx++) {
    		paramMap.put("uuid", toHex(inx,keyLevel));		
    		cnt[0] += this.template.update("BatLogDelete.clearInterface", paramMap);
    		cnt[1] += this.template.update("BatLogDelete.clearFileLog", paramMap);
    		cnt[2] += this.template.update("BatLogDelete.clearDBLog", paramMap);
    		cnt[3] += this.template.update("BatLogDelete.clearErrorLog", paramMap);
    	}
    	return cnt;
    }
    private int power( int x, int y ){
    	if ( y == 1 )
    		return x;
    	return x * power( x, y -1);
    }

    private String toHex( int val, int len){
    	StringBuffer sb = new StringBuffer();
    	String sval = Integer.toHexString(val);
    	for ( int inx = 0; inx < len - sval.length(); inx++){
    		sb.append('0');
    	}
    	sb.append(sval);
    	return sb.toString();
    }
    
    @SuppressWarnings("deprecation")
	public int delete(HashMap<String, Object> paramMap) {
		
		int deletedCount = 0;
		
		paramMap.put("tableName", "TSEAIES03");
		deletedCount += template.delete("BatLogDelete.deleteDetail", paramMap);
		paramMap.put("tableName", "TSEAIES04");
		deletedCount += template.delete("BatLogDelete.deleteDetail", paramMap);
		paramMap.put("tableName", "TSEAIES05");
		deletedCount += template.delete("BatLogDelete.deleteDetail", paramMap);
		
		deletedCount += template.delete("BatLogDelete.delete", paramMap);
		
		return deletedCount;
	}

}
