package kr.co.kihyun.beans.user;

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

import kr.co.kihyun.db.DBManager;

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

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

	private ResultSet rs = null;
	private Long id[] = null;
	private String name[] = null;

	public Long[] getDeptID(Long partID) {
            
            Long[] ids;
            
            StringBuilder sql = new StringBuilder(60);
            int count = 0;

            try {
                sql.append("select id from ").append(MoumiEntity.getTableName(UserDept.class)).append(" where part_id=?");

                rs = execQuery(sql, partID);
                if (rs != null) {
                        rs.last();
                        count = rs.getRow();
                        rs.beforeFirst();
                } else {
                        return null;
                }

                //v2 27.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by  YOUNGJUN,CHO
                /*
                id = new Long[count];

                for (int i = 0; rs.next(); i++) {
                        id[i] = rs.getLong(1);
                }
                return id;
                */
                ids = new Long[count];

                for (int i = 0; rs.next(); i++) {
                    ids[i] = rs.getLong(1);
                }
                return ids;
                //====================================================================

            } 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[] getDeptName(Long partID) {
            
            String[] names;
            StringBuilder sql = new StringBuilder(60);
            int count = 0;

            try {
                sql.append("select name from ").append(MoumiEntity.getTableName(UserDept.class)).append(" where part_id=?");

                rs = execQuery(sql, partID);
                if (rs != null) {
                    rs.last();
                    count = rs.getRow();
                    rs.beforeFirst();
                } else {
                    return null;
                }

                //v2 27.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by  YOUNGJUN,CHO
                /*
                name = new String[count];

                for (int i = 0; rs.next(); i++) {
                        name[i] = rs.getString(1);
                }
                return name;
                */
                names = new String[count];
                
                for (int i = 0; rs.next(); i++) {
                        names[i] = rs.getString(1);
                }
                return names;
                //====================================================================
                        
            } 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 getUserUserDeptUserID(String partID) {
		StringBuilder sql = new StringBuilder(100);
		String deptuserID = "";

		try {
			sql.append("select id, dept_id from ").append(MoumiEntity.getTableName(UserUser.class))
					.append(" where part_id=? and (valid='1' or valid='Y')");
			rs = execQuery(sql, partID);

			for (int i = 0; rs.next(); i++) {
				if (i == 0) {
					deptuserID = rs.getString(1);
					deptuserID = deptuserID + "/" + rs.getString(2);
				} else {
					deptuserID = deptuserID + "," + rs.getString(1);
					deptuserID = deptuserID + "/" + rs.getString(2);
				}
			}
			return deptuserID;
		} 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 getUserDeptDeptID(String partID) {
		StringBuilder sql = new StringBuilder(60);
		String deptID = "";

		try {
			sql.append("select id from ").append(MoumiEntity.getTableName(UserDept.class)).append(" where part_id=?");

			rs = execQuery(sql, partID);

			for (int i = 0; rs.next(); i++) {
				if (i == 0) {
					deptID = rs.getString(1);
				} else {
					deptID = 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();
		}
	}
}
