/**
 *
 * @author Kts
 */

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 DeptAdd extends DBManager{

    private static final Logger LOG = LoggerFactory.getLogger(Dept.class);

    //public static String[] gradeName = { "유치원", "초등학교", "중학교", "고등학교", "기타기관" };

    private ResultSet rs = null;

    /******** 부서grade 가져오기 ********/
    public int getGrade(String id , String deptId) {
        StringBuilder sql = new StringBuilder(50);
        int grade = 0;
        try {
                sql.append("select dt.grade from ").append(MoumiEntity.getTableName(MDept.class)).append(" dt, ").
                        append(MoumiEntity.getTableName(MUser.class)).append(" du").
                        append(" where du.id=? and dt.id=?");
                rs = execQuery(sql.toString(), id ,deptId);

                if (rs.next()) {
                    return (grade = rs.getInt(1));
                }

        } 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 {
                if(null != rs)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                execClose();
                return grade;
        }
    }
    /******** 부서foundation 가져오기 ********/
    public int getFoundation(String id ,String deptId) {
        StringBuilder sql = new StringBuilder(50);

        int foundation = 0;
        try {
                sql.append("select dt.foundation from ").append(MoumiEntity.getTableName(MDept.class)).append(" dt, ").
                        append(MoumiEntity.getTableName(MUser.class)).append(" du").
                        append(" where du.id=? and dt.id=?");
                rs = execQuery(sql.toString(), id ,deptId);

                if (rs.next()) {
                        foundation = rs.getInt(1);
                }
                return foundation;
        } 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();
        }
    }
    /******** 부서rootDept 가져오기 ********/
    public String getRootDept(String id) {
        StringBuilder sql = new StringBuilder(50);

        String rootDept = "";
        try {
                sql.append("select distinct dt.root_Dept from ").append(MoumiEntity.getTableName(MDept.class)).append(" dt, ").
                        append(MoumiEntity.getTableName(MUser.class)).append(" du ").append(" where du.id=? and dt.root_Dept like 'K%'");

                rs = execQuery(sql.toString(), id);

                if (rs.next()) {
                        rootDept = rs.getString(1);
                }
                return rootDept;
        } 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();
        }
    }

}
