/*********************************************************************************************************
 * 프로그램명 : DeptUpdate.java 프로그램설명 : 작성자 : 조용준 작성일 : 2004.07.05 변경일 :
 **********************************************************************************************************/
package kr.co.kihyun.beans.totsys.sysadm.dept;

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

import kr.co.kihyun.db.DBManager;

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

public class DeptUpdate extends DBManager {
	private static final Logger LOG = LoggerFactory.getLogger(DeptUpdate.class);
	private ResultSet rs = null;

	/** return type **/
	/* isDeptID = true => 같은 passcode 존재 */
	/* isDeptID = false => 같은 passcode 없음 */
	/**                                             **/
	public boolean isDeptPasscode(String passcode) {
		boolean isDeptPasscode = true;
		int result = -1;
		StringBuilder sql = new StringBuilder(120);

		try {
			sql.append("select count(passcode) from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where passcode=?");
			rs = execQuery(sql.toString(), passcode);
			if (rs.next()) {
				result = rs.getInt(1);
				if (result == 0) {
					isDeptPasscode = false;
				}
			}
			return isDeptPasscode;
		} 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();
		}
	}

	/** return type **/
	/* isDeptID = true => 같은 passcode 존재 */
	/* isDeptID = false => 같은 passcode 없음 */
	/**                                             **/
	public boolean isDeptPasscode(Long id, String passcode) {
		boolean isDeptPasscode = true;
		int result = -1;
		StringBuilder sql = new StringBuilder(150);

		try {
			sql.append("select count(passcode) from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where id not in(?) and passcode=?");
			rs = execQuery(sql.toString(), id, passcode);
			if (rs.next()) {
				result = rs.getInt(1);
				if (result == 0) {
					isDeptPasscode = false;
				}
			}
			return isDeptPasscode;
		} 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();
		}
	}
}
