package core.stlmes.server.sale.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;
import core.stlmes.server.sale.dbhelp.DbTrans;

public class BaseService extends CoreIComponent
{
	public String daoName = core.stlmes.server.sale.constdefine.ConstDef.daoName;

	public void setDaoName(String daoName)
	{
		this.daoName = daoName;
	}

	private core.stlmes.server.sale.dbhelp.DbTrans trans = null;

	public core.stlmes.server.sale.dbhelp.DbTrans getTrans()
	{
		return trans;
	}

	public void setTrans(core.stlmes.server.sale.dbhelp.DbTrans trans)
	{
		this.trans = trans;
	}

	public ResultSet executeForResult(String sqlStr) throws SQLException
	{
		ResultSet rs = null;
		if (this.trans == null) rs = this.getDao(daoName)
				.ExceuteQueryForResultSet(sqlStr);
		else
			rs = executeForResult(sqlStr, this.trans);

		return rs;
	}

	public ResultSet executeForResult(String sqlStr,
			core.stlmes.server.sale.dbhelp.DbTrans trans) throws SQLException
	{
		ResultSet rs = null;
		java.sql.Statement st = trans.getConn().createStatement();
		rs = st.executeQuery(sqlStr);
		return rs;
	}

	public CoreReturnObject ExcuteNonQuery(String sqlStr)
			throws java.sql.SQLException
	{
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDao(daoName).ExcuteNonQuery(sqlStr);
		return cro;
	}

	public CoreReturnObject ExcuteNonQueryIgnoreTrans(String sqlStr)
			throws java.sql.SQLException,java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (this.trans == null) cro = this.getDao(daoName).ExcuteNonQuery(
				sqlStr);
		else
			cro = ExcuteNonQuery(sqlStr, this.trans);

		return cro;
	}

	public CoreReturnObject ExcuteNonQuery(String sqlStr, DbTrans trans)
			throws java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (trans.getErrMsg().length() > 0)
		{
			cro.setV_errCode(-1);
			cro.setV_errMsg(trans.getErrMsg());
			return cro;
		}

		PreparedStatement ps = trans.getConn().prepareStatement(sqlStr);
		// trans.getConn().setAutoCommit(false);
		int result = ps.executeUpdate();
		ps.close();
		cro.setResult(result);
		// cro = this.getDao(daoName).ExceuteNonQueryTranscation(sqlStr,
		// new Object[]
		// {}, trans.getConn());
		return cro;
	}

	public CoreReturnObject ExcuteNonQueryBatch(java.util.ArrayList sqlStr,
			DbTrans trans) throws java.lang.Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		if (trans.getErrMsg().length() > 0)
		{
			cro.setV_errCode(-1);
			cro.setV_errMsg(trans.getErrMsg());
			return cro;
		}
		java.sql.Statement ps = trans.getConn().createStatement();
		for (Object s : sqlStr)
		{
			ps.addBatch(s.toString());
		}

		int[] result = ps.executeBatch();

		cro.setResult(result);

		return cro;
	}

	public String GetSqlConditionByStringValue(String columnName, String value)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("  and ").append(columnName).append(" = ").append(" '")
				.append(value).append("'");
		return sb.toString();
	}

	public String GetSqlConditionByNumber(String columnName, Double value)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("  and ").append(columnName).append("  =  ").append(value);
		return sb.toString();
	}

	public String getSqlConditionByGreaterOrEqualsThan(String columnName,
			String value)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("  and ").append(columnName).append("  >=  ").append(value);
		return sb.toString();

	}

	public String getSqlConditionByLessOrEqualsThan(String columnName,
			String value)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("  and ").append(columnName).append("  <=  ").append(value);
		return sb.toString();

	}

	public String getSqlConditionByArrayListStringValue(String columnName,
			java.util.List<String> value)
	{
		StringBuilder sb = new StringBuilder();
		StringBuilder sbReturn = new StringBuilder();
		if (value == null) return " and 1 = 2";
		if (value.size() == 0) return " and 1 = 2 ";
		for (String s : value)
		{
			sb.append(" ").append(columnName).append(" = ").append(" '")
					.append(s).append("' ").append("  or  ");
		}
		sb.append(" 1 = 2");
		sbReturn.append(" and  (").append(sb.toString()).append("  )");
		return sbReturn.toString();
	}

	public String GetSqlDateFormatByDateValue(Date value)
	{
		StringBuilder sb = new StringBuilder();
		if (value == null)
		{
			sb.append(" to_date('").append("").append(
					"','yyyy/mm/dd HH24:MI:SS')");
		}
		else
		{
			SimpleDateFormat tempDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String time = tempDate.format(value);
			sb.append(" to_date('").append(time).append(
					"','yyyy/mm/dd HH24:MI:SS')");
		}
		return sb.toString();
	}

	public double getMoneyByOrderUnit(String price, String orderUnit,
			double weight, double qty, double sendNum)
	{

		if ("吨".equals(orderUnit))
		{
			return weight
					* (core.stlmes.server.sale.util.Convert.getDouble(price) );
		}
		else if ("支".equals(orderUnit))
		{
			return qty
					* (core.stlmes.server.sale.util.Convert.getDouble(price) );
		}
		else
		{
			if (sendNum == 0)
			{
				return weight
						* (core.stlmes.server.sale.util.Convert
								.getDouble(price) );
			}
			else

				return sendNum
						* (core.stlmes.server.sale.util.Convert
								.getDouble(price) );
		}

	}

	@SuppressWarnings("deprecation")
	public String getPreparedSQL(String sql, Object[] params)
	{
		return core.stlmes.server.sale.util.Convert.getPreparedSQL(sql, params);
	}

	public String getQuerySql()
	{
		return "";
	}

}
