package core.stlmes.server.sale.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.lang.*;

public class Convert
{
	public static String getFormDate(java.util.Date date)
	{
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
		if (date == null) return "";
		return tempDate.format(date);

	}

	public static String getFormDateToyyyyMMdd(java.util.Date date)
	{
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
		if (date == null) return "";
		return tempDate.format(date);

	}

	public static double getDouble(String obj)
	{
		try
		{

			return Double.valueOf(obj);

		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
			return 0.0;
		}
	}

	public static Object convertNullTOBlankStr(Object obj)
	{
		if (obj == null) return "";
		return obj;

	}

	public static int findCharCnt(String str, char findChar)
	{
		int num = 0;
		try
		{
			for (int i = 0; i < str.length(); i++)
			{
				if (str.charAt(i) == findChar)
				{
					num++;

				}

			}

		}
		catch (java.lang.Exception ex)
		{

		}

		return num;

	}

	public static String getPreparedSQL(String sql, Object[] params)
	{
		int paramNum = 0;

		if (params != null) paramNum = params.length;
		if (1 > paramNum)
		{
			return sql;
		}

		int sqlParanum = findCharCnt(sql, '?');
		if (sqlParanum != paramNum)
		{

			throw new RuntimeException("参数个数错误");
			// return " sql's parameter is not match with inputParameter";

		}

		StringBuffer returnSQL = new StringBuffer();
		String[] subSQL = sql.split("\\?|:[\\d]+");

		for (int i = 0; i < paramNum; i++)
		{
			try
			{
				if (params[i] == null) returnSQL.append(subSQL[i]).append(" '")
						.append("'");
				else if ( (params[i] instanceof java.util.Date ))
				{
					Date dt = new Date( (( Date)params[i] ).getDate());
					SimpleDateFormat tempDate = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					String time = tempDate.format(( Date)params[i]);
					returnSQL.append(subSQL[i]).append(" to_date('").append(
							time).append("','yyyy/mm/dd HH24:MI:SS')");
				}
				else
				{
					returnSQL.append(subSQL[i]).append(" '").append(params[i])
							.append("' ");
				}
			}
			catch (Exception ex)
			{
			}
		}

		if (subSQL.length > params.length)
		{

			returnSQL.append(subSQL[ (subSQL.length - 1 )]);

		}
		return returnSQL.toString();
	}

}
