/***********************************************************************************
 * @@ Program Name : User.java Description : 사용자 정보에 대한 클레스 Author : 강원중 Create Date : 2004.10.13 History :
 * @@
 ***********************************************************************************/
package kr.co.kihyun.beans.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import kr.co.kihyun.beans.entity.MUser;
import kr.co.kihyun.beans.entity.MoumiEntity;

import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.lang.MString;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class User extends DBManager {
	private static final Logger LOG = LoggerFactory.getLogger(User.class);

	private ResultSet rs = null;

	/******** 사용자 이름 ********/
	public String getName(String id) {
		StringBuilder sql = new StringBuilder(70);
		String name = "";
		try {
			sql.append("select name from ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" where id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				name = rs.getString(1);
			}
			return name;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}

	public int getSysAuth(String id) {
		StringBuilder sql = new StringBuilder(60);
		int sys_auth = 0;
		try {
			sql.append("select sys_auth from ").append(MoumiEntity.getTableName(MUser.class)).append(" where id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				sys_auth = rs.getInt(1);
			}
			return sys_auth;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}

	public String getDept(String id) {
		StringBuilder sql = new StringBuilder(70);
		String deptID = null;
		try {
			sql.append("select dept_id from ").append(MoumiEntity.getTableName(MUser.class)).append(" where id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				deptID = rs.getString(1);
			}
			return deptID;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}

	public String getPassword(String id) {
		StringBuilder sql = new StringBuilder(60);
		String passwd = "";
		try {
			sql.append("select passwd from ").append(MoumiEntity.getTableName(MUser.class)).append(" where id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				passwd = rs.getString(1);
			}
			return passwd;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}

	/******** 사용자 아이디 ********/
	public boolean isUsing(String id) throws SQLException {
		boolean isUsing = execQuery(
				new StringBuilder(50).append("select id from ")
				.append(MoumiEntity.getTableName(MUser.class)).append(" where id=?"), id
				).next();
		close(rs);
		execClose();
		return isUsing;
	}

	public boolean isUsingName(String deptID, String userName) {
		String userID = checkName(deptID, userName);
		return !MString.isNull(userID);
	}

	// 한사람에 한나의 계정만 사용 가능하게
	public String checkName(String deptID, String name) {
		StringBuilder sql = new StringBuilder(120);
		String id = "";
		try {
			sql.append("select id from ").append(MoumiEntity.getTableName(MUser.class))
					.append(" where sp_cm_dec_fu(name)=? and dept_id=? and (del_type='0' or del_type='N')");

			rs = execQuery(sql, name, deptID);
			if (rs.next()) {
				id = rs.getString(1);
			}
			return id;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}
}
