package com.steering.comm.tools;

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

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

/**
 * 事务处理工具
 * 
 * @author Administrator
 * 
 */
public class TransctionTool extends Object {
	private Connection con;

	public Connection getCon() {
		return con;
	}

	private CoreReturnObject returnObject;

	public CoreReturnObject getReturnObject() {
		return returnObject;
	}

	private CoreIComponent component;

	public TransctionTool() {
		component = new CoreIComponent();
		con = component.getDao(CoreConstantData.sysbeanstr).getConnection();
		returnObject = component.getDao(CoreConstantData.sysbeanstr).ExcuteQuery("SELECT 1 FROM DUAL");
		returnObject.setResult(0);
		try {
			con.setAutoCommit(false);
		} catch (SQLException ex) {
			returnObject.setV_errCode(-1);
			returnObject.setV_errMsg(ex.getMessage());
			try {
				con.rollback();
			} catch (SQLException e) {
				returnObject.setV_errCode(-1);
				returnObject.setV_errMsg(e.getMessage());
			}
		}
	}

	public CoreReturnObject commit() {
		try {
			con.commit();
		} catch (SQLException ex) {
			returnObject.setV_errCode(-1);
			returnObject.setV_errMsg(ex.getMessage());
			try {
				con.rollback();
			} catch (SQLException e) {
				returnObject.setV_errCode(-1);
				returnObject.setV_errMsg(e.getMessage());
			} finally {
				if (con != null) {
					try {
						if (!con.isClosed()) {
							con.close();
						}
					} catch (SQLException e) {
						returnObject.setV_errCode(-1);
						returnObject.setV_errMsg(e.getMessage());
					}
				}
			}
		}
		
		return returnObject;
	}

	public CoreReturnObject rollBack() {
		try {
			con.rollback();
		} catch (SQLException ex) {
			returnObject.setV_errCode(-1);
			returnObject.setV_errMsg(ex.getMessage());
		} finally {
			if (con != null) {
				try {
					if (!con.isClosed()) {
						con.close();
					}
				} catch (SQLException e) {
					returnObject.setV_errCode(-1);
					returnObject.setV_errMsg(e.getMessage());
				}
			}
		}
		
		returnObject.setV_errCode(-1);
		returnObject.setV_errMsg("服务端处理失败！");
		return returnObject;
	}

	// public CoreReturnObject ExcuteTransction(ArrayList<String> sqlList){
	// CoreReturnObject cro = new CoreReturnObject();
	// int count = 0;
	// try{
	// for(String sql : sqlList){
	// PreparedStatement statement = con.prepareStatement(sql);
	// int tmpCount = statement.executeUpdate();
	// count += tmpCount;
	// statement.close();
	// }
	// }
	// catch(Exception ex){
	// try{
	// con.rollback();
	// }
	// catch(Exception ex2){}
	// cro.setV_errCode(-1);
	// return cro;
	// }
	// finally{
	// if(con != null){
	// try{
	// con.close();
	// }
	// catch(Exception ex){}
	// }
	// }
	// cro.setResult(count);
	// return cro;
	// }

	public boolean ExcuteTransction(String sql) {
		try {
			Statement statement = con.createStatement();
			int count = statement.executeUpdate(sql);
			statement.close();
			int totalCount = count + Integer.parseInt(returnObject.getResult().toString());
			returnObject.setResult(totalCount);
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				returnObject.setV_errCode(-1);
				returnObject.setV_errMsg(ex.getMessage());
				CoreIComponent.logAdapter.filelogger
						.logError(" ExcuteTransction(String sql)"
								+ ex.getMessage());
				con.rollback();
			} catch (Exception ex2) {
				returnObject.setV_errCode(-1);
				returnObject.setV_errMsg(ex2.getMessage());
			}
			returnObject.setV_errCode(-1);
			return false;
		}
		return true;
	}

	protected void finalize() {
		try {
			if (con != null) {
				con.close();
			}
			try {
				super.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		} catch (Exception ex) {
		}
	}
}
