/***********************************************************************************
 * @@ Program Name : HttpLogin.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.MDept;
import kr.co.kihyun.beans.entity.MUser;
import kr.co.kihyun.beans.entity.MoumiEntity;

import kr.co.kihyun.db.DBManager;

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

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

	private ResultSet rs = null;

	/**************
	 * mUSER ************** return 0 => 보고자 계정 return 1 => 집계자 계정 return 5 => ID존재 PW일치하지 않음 return 6 => 존재하는 ID없음
	 * return 7 => 시스템 에러
	 ************************************/
	private String partID;
	private int partAuth;
	private String partName;
	private String deptID;
	private String deptName;
	private String usID;
	private String usName;
	private String passwd;
	private int sysAuth;

	public int executeQuery(String id, String password) {
		StringBuilder sql = new StringBuilder(250);
		int succNum = 0;
		try {
			sql.append("select us.passwd, pt.id, pt.name, dt.id, dt.name, us.id, ")
					.append("sp_cm_dec_fu(us.name), us.sys_auth, pt.sys_auth ")
					.append("from ").append(MoumiEntity.getTableName(MUser.class)).append(" us, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" dt, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" pt ")
					.append("where us.dept_id=dt.id and dt.organ=pt.id and us.id=? ")
					.append("and (us.del_type='0' or us.del_type='N')");
			rs = execQuery(sql, id);

			if (rs.next()) {
				passwd = rs.getString(1);
				if (password != null && password.equals(passwd)) {
					partID = rs.getString(2);
					partName = rs.getString(3);
					deptID = rs.getString(4);
					deptName = rs.getString(5);
					usID = rs.getString(6);
					usName = rs.getString(7);
					sysAuth = rs.getInt(8);
					partAuth = rs.getInt(9);
					succNum = sysAuth;
				} else {
					succNum = 10;
				}
			} else {
				succNum = 11;
			}

			return succNum;
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			return 12;
		} catch (Exception ex) {
			return 12;
		} finally {
			close(rs);
			execClose();
		}
	}

	public String getPartID() {
		return partID;
	}

	public int getPartAuth() {
		return partAuth;
	}

	public String getPartName() {
		return partName;
	}

	public String getDeptID() {
		return deptID;
	}

	public String getDeptName() {
		return deptName;
	}

	public String getUserID() {
		return usID;
	}

	public String getUserName() {
		return usName;
	}

	public String getPassword() {
		return passwd;
	}

	public int getSysAuth() {
		return sysAuth;
	}

}
