package nc.bs.ic.m45.ext.rule;

import java.util.Date;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.core.service.TimeService;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;


/**
 * 
 * @author 叶志安
 *增加业务参数访问方法
 */
public class BaseToolsFm45 {
	private UFDateTime getCurrentTime() {
		return new UFDateTime(new Date(TimeService.getInstance().getTime()));
	}
	
	public static String getDBTime(BaseDAO baseDAO) throws BusinessException{

		String result ="" ;

		StringBuilder sql = new StringBuilder();
		sql.append("select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') value from dual ");

		try {
			List list = (List) baseDAO.executeQuery(sql.toString(),
					new MapListProcessor());

			if (list != null) {
				Map map = null;
				if (list.get(0) != null) {
					map = (Map) list.get(0);
					result = map.get("value") == null ? "" : (String) map
							.get("value");
				}
			}
		} catch (DAOException e) {
			Logger.error(e.getMessage());
			throw new BusinessException(e.getMessage());
		}

		return result;
	}

	public static String getGlobleSysInitValue(String initCode, BaseDAO baseDAO)
			throws BusinessException {

		String result = "";

		StringBuilder sql = new StringBuilder();
		sql.append("select value from pub_sysinit t ");
		sql.append(" where t.pk_org='GLOBLE00000000000000'");
		sql.append(" and t.initcode='" + initCode + "'");

		try {
			List list = (List) baseDAO.executeQuery(sql.toString(),
					new MapListProcessor());

			if (list != null) {
				Map map = null;
				if (list.get(0) != null) {
					map = (Map) list.get(0);
					result = map.get("value") == null ? "" : (String) map
							.get("value");
				}
			}
		} catch (DAOException e) {
			Logger.error(e.getMessage());
			throw new BusinessException(e.getMessage());
		}

		return result;
	}
	
	public static String getGlobleSysInitValue(String initCode)
			throws BusinessException {
		BaseDAO baseDAO= new BaseDAO();
		return getGlobleSysInitValue(initCode,baseDAO);
	}

	public static String makeErrorInfo(String errorInfo) {

		if (errorInfo == null || errorInfo.length() == 0) {
			return "";
		}
//     错误信息截取过长导致回写错误信息报错 add by wuwcong 2016-07-14
		if (errorInfo.length() >= 500) {
			errorInfo = errorInfo.substring(0, 500);
		}
		errorInfo = errorInfo.replace("'", "''");

		return errorInfo;
	}
	
	
}
