/*************************************************************************************************
 * 프로그램명 : DeptList.java 프로그램설명 : 부서 정보 목록 작성자 : 조용준 작성일 : 2004.07.01 변경일 :
 **************************************************************************************************/
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 kr.co.kihyun.lang.MString;

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

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

	private String[] idList = null;
	private String[] nameList = null;
	private String[] addrList = null;
	private String[] phoneList = null;
	private String[] passcodeList = null;
	private String[] recoImgList = null;
	private String[] upperDeptIDList = null;
	private String[] partIDList = null;

	public void executeQuery(String partID, String findOption, String findWord) {
		StringBuilder sql = new StringBuilder(500);

		try {
			sql.append(getSelectClause(partID)).append(getFromClause(partID)).append(getWhereClause(partID))
					.append(getSearchCondition(partID, findOption, findWord))
					.append(getOrderClause());

			rs = execQuery(sql.toString());
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}
			setFields(rs);
		} 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();
		}
	}

	private static final String getSelectClause(String partID) {
		if (MString.isNull(partID)) {
			return "select id, name, addr, phone, passcode, reco_img, upper_dept, organ";
		} else {
			return "select dt.id, dt.name, dt.addr, dt.phone, dt.passcode, dt.reco_img, dt.upper_dept, dt.organ";
		}
	}

	private static final String getFromClause(String partID) {
		StringBuilder sql = new StringBuilder(50);

		if (MString.isNull(partID)) {
			return sql.append(" from ").append(MoumiEntity.getTableName(MDept.class)).append("").toString();
		} else {
			return sql.append(" from ").append(MoumiEntity.getTableName(MDept.class)).append(" dt, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" pt").toString();
		}
	}

	private String getWhereClause(String partID) {
		StringBuilder sql = new StringBuilder(120);

		if (MString.isNull(partID)) {
			return "";
		} else {
			return sql.append(" where dt.organ=pt.id and (pt.id='").append(partID).append("' or (pt.upper_dept='")
					.append(partID).append("' and pt.sys_auth=0))").toString();
		}
	}

	private String getSearchCondition(String partID, String findOption, String findWord) {
		StringBuilder sql = new StringBuilder(150);

		if (MString.isNull(partID)) {
			if ("id".equals(findOption)) {
				return sql.append(" where id='").append(findWord).append("'").toString();
			} else if ("deptName".equals(findOption)) {
				return sql.append(" where name like '%").append(findWord).append("%'").toString();
			} else if ("deptCode".equals(findOption)) {
				return sql.append(" where passcode='").append(findWord).append("'").toString();
			} else if ("all".equals(findOption)) {
				return sql.append(" where (id like '%").append(findWord).append("%') ")
						.append("or (name like '%").append(findWord).append("%') ")
						.append("or (passcode like '%").append(findWord).append("%')").toString();
			} else {
				return "";
			}
		} else {
			if ("id".equals(findOption)) {
				return sql.append(" and dt.id='").append(findWord).append("'").toString();
			} else if ("deptName".equals(findOption)) {
				return sql.append(" and dt.name like '%").append(findWord).append("%'").toString();
			} else if ("deptCode".equals(findOption)) {
				return sql.append(" and dt.passcode='").append(findWord).append("'").toString();
			} else if ("all".equals(findOption)) {
				return sql.append(" and (dt.id like '%").append(findWord).append("%') ")
						.append("or (dt.name like '%").append(findWord).append("%') ")
						.append("or (dt.passcode like '%").append(findWord).append("%')").toString();
			} else {
				return "";
			}
		}
	}

	private String getOrderClause() {
		return " order by id";
	}

	private void setFields(ResultSet rs) throws SQLException {

		idList = new String[count];
		nameList = new String[count];
		addrList = new String[count];
		phoneList = new String[count];
		passcodeList = new String[count];
		recoImgList = new String[count];
		upperDeptIDList = new String[count];
		partIDList = new String[count];

		for (int i = 0; rs.next(); i++) {
			idList[i] = rs.getString(1);
			nameList[i] = rs.getString(2);
			addrList[i] = rs.getString(3);
			phoneList[i] = rs.getString(4);
			passcodeList[i] = rs.getString(5);
			recoImgList[i] = rs.getString(6);
			upperDeptIDList[i] = rs.getString(7);
			partIDList[i] = rs.getString(8);
		}
	}

	public int getCount() {
		return count;
	}

	public String[] getIDList() {
            
            //v2 27.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by  YOUNGJUN,CHO
            //return idList;
            String [] ret = null;
            if (this.idList != null) {
                ret = new String[this.idList.length];   
                for (int i = 0 ; i < this.idList.length ; i++) {
                    ret[i] = this.idList[i];
                }
            }
            
            return ret;
            //====================================================================
	}

	public String[] getNameList() {
            //24.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by KWON,HAN
	    //	return nameList;
                
            String[] ret = null;
            if( this.nameList != null) {
                ret = new String[nameList.length];
                for (int i=0; i<nameList.length; i++) {
                    ret[i] = this.nameList[i];
                }
            }
            return ret;
            //====================================================================                  
	}

	public String[] getAddrList() {
	    //	return addrList;
                
            String[] ret = null;
            if( this.addrList != null) {
                ret = new String[addrList.length];
                for (int i=0; i<addrList.length; i++) {
                    ret[i] = this.addrList[i];
                }
            }
            return ret;                
	}

	public String[] getPhoneList() {
	    //	return phoneList;
                
            String[] ret = null;
            if( this.phoneList != null) {
                ret = new String[phoneList.length];
                for (int i=0; i<phoneList.length; i++) {
                    ret[i] = this.phoneList[i];
                }
            }
            return ret;                  
	}

	public String[] getPasscodeList() {
	    //	return passcodeList;
                
            String[] ret = null;
            if( this.passcodeList != null) {
                ret = new String[passcodeList.length];
                for (int i=0; i<passcodeList.length; i++) {
                    ret[i] = this.passcodeList[i];
                }
            }
            return ret;                
	}

	public String[] getRecoImgList() {
	    //	return recoImgList;
                
            String[] ret = null;
            if( this.recoImgList != null) {
                ret = new String[recoImgList.length];
                for (int i=0; i<recoImgList.length; i++) {
                    ret[i] = this.recoImgList[i];
                }
            }
            return ret;                 
	}

	public String[] getUpperDeptIDList() {
	    //	return upperDeptIDList;
                
            String[] ret = null;
            if( this.upperDeptIDList != null) {
                ret = new String[upperDeptIDList.length];
                for (int i=0; i<upperDeptIDList.length; i++) {
                    ret[i] = this.upperDeptIDList[i];
                }
            }
            return ret;                 
	}

	public String[] getPartIDList() {
	    //	return partIDList;
                
            String[] ret = null;
            if( this.partIDList != null) {
                ret = new String[partIDList.length];
                for (int i=0; i<partIDList.length; i++) {
                    ret[i] = this.partIDList[i];
                }
            }
            return ret;                 
	}

}
