package core.stlmes.server.sale.dbhelp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;

public class DbManager extends CoreIComponent
{
	private final String DaoName = core.stlmes.server.sale.constdefine.ConstDef.daoName;
	private final String SuccessResult = "SUCCESS";
	private final String FailResult = "FAIL";

	private static DbManager _DbManager = null;

	private void setCroErrMsg(CoreReturnObject cro, String errMsg)
	{
		cro.setV_errCode(-1);
		cro.setV_errMsg(errMsg);

	}

	public CoreReturnObject exceuteQuery(String sql) throws Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDao(DaoName).ExcuteQuery(sql);
		return cro;
	}

	public ResultSet executeForResult(String sqlStr) throws SQLException
	{
		ResultSet rs = null;
		rs = this.getDao(this.DaoName).ExceuteQueryForResultSet(sqlStr);
		return rs;
	}

	public CoreReturnObject exceuteQuery(String sql, String daoName)
			throws Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDao(daoName).ExcuteQuery(sql);
		return cro;
	}

	public CoreReturnObject exceuteNoQuery(String sql) throws Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDao(DaoName).ExcuteNonQuery(sql);
		return cro;
	}

	public CoreReturnObject exceuteNoQuery(String sql, String daoName)
			throws Exception
	{
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDao(daoName).ExcuteNonQuery(sql);
		return cro;
	}

	public CoreReturnObject beginTransaction(String daoName)
			throws java.sql.SQLException
	{
		return DbTransAction.getDbTransActionInstance()
				.doGetConnection(daoName);
	}

	public CoreReturnObject beginTransaction() throws java.sql.SQLException
	{
		return DbTransAction.getDbTransActionInstance()
				.doGetConnection(DaoName);
	}

	public CoreReturnObject ExceuteNonQueryTranscation(String sql,
			core.stlmes.server.sale.dbhelp.DbTransAction dbaction)
			throws java.lang.Exception,java.sql.SQLException
	{
		CoreReturnObject obj = new CoreReturnObject();
		if (dbaction.getErrMsg().length() > 0)
		{
			obj.setResult(dbaction.getErrMsg());
			return obj;
		}
		obj = this.getDao(DaoName).ExceuteNonQueryTranscation(sql,
				new java.lang.Object[]
				{}, dbaction.getConnection());
		return obj;

	}

	public CoreReturnObject exceuteNoQueryTranscation(String sql, String uuid)
	{
		CoreReturnObject obj = new CoreReturnObject();

		Connection conn = DbTransAction.getDbTransActionInstance()
				.getConnection(uuid);
		String strSql = sql;
		String errMsg = "";
		if (conn == null)
		{
			this.setCroErrMsg(obj, this.FailResult);
			return obj;
		}
		try
		{

			PreparedStatement ps = conn.prepareStatement(sql);
			// trans.getConn().setAutoCommit(false);
			int result = ps.executeUpdate();
			ps.close();
			obj.setResult(result);

		}
		catch (Exception e)
		{
			this.setCroErrMsg(obj, e.getMessage());
		}
		return obj;
	}

	public CoreReturnObject exceuteNoQueryTranscation(String sql,
			String daoName, String uuid)
	{
		CoreReturnObject obj = new CoreReturnObject();

		Connection conn = DbTransAction.getDbTransActionInstance()
				.getConnection(uuid);
		String strSql = sql;
		String errMsg = "";
		if (conn == null)
		{
			this.setCroErrMsg(obj, this.FailResult);
			return obj;
		}
		try
		{

			PreparedStatement ps = conn.prepareStatement(sql);
			// trans.getConn().setAutoCommit(false);
			int result = ps.executeUpdate();
			ps.close();
			obj.setResult(result);

		}
		catch (Exception e)
		{
			this.setCroErrMsg(obj, e.getMessage());
		}
		return obj;
	}

	public CoreReturnObject commit(String uuid)
	{
		Connection conn = DbTransAction.getDbTransActionInstance()
				.getConnection(uuid);
		CoreReturnObject obj = new CoreReturnObject();
		if (conn == null)
		{
			this.setCroErrMsg(obj, this.FailResult);
			return obj;
		}
		try
		{
			System.out.println(uuid);
			conn.commit();
			conn.close();
			DbTransAction.getDbTransActionInstance().removeConnection(uuid);

		}
		catch (Exception e)
		{
			this.setCroErrMsg(obj, e.getMessage());
		}
		finally
		{
			conn = null;
		}
		return obj;
	}

	public CoreReturnObject rollBack(String uuid)
	{
		Connection conn = null;
		CoreReturnObject obj = new CoreReturnObject();
		try
		{
			conn = DbTransAction.getDbTransActionInstance().getConnection(uuid);
			System.out.println("rollBack" + uuid);
			conn.rollback();
			conn.close();
		}
		catch (Exception e)
		{
			this.setCroErrMsg(obj, e.getMessage());
		}
		finally
		{
			conn = null;
		}
		return obj;
	}
}
