/*********************************************************************************************************
	프로그램명		: UserList.java
	프로그램설명	: 프로젝트와 관련된 정보를 얻을수 있는 class
	작성자				: 강원중
	작성일				: 2003.12.18
	변경일				:	2003.12.18
**********************************************************************************************************/

package kr.co.kihyun.beans.totsys.sysadm.user;


import java.sql.ResultSet;
import java.sql.SQLException;

import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.db.SQLMessage;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * @Deprecated
 * use kr.co.kihyun.beans.entity Package and Moumi.getEntityManager() method
 */
@Deprecated
public class UserList extends DBManager{
	private static final Logger LOG = LoggerFactory.getLogger(UserList.class);
	ResultSet rs = null;	
	
	int count = 0;
	String[] idList = null;
	String[] passwdList = null;
	String[] nameList = null;
	String[] sysAuthList = null;
	String[] userNameList = null;
	String[] deptNameList = null;
	String[] dutyNameList = null;
	
	public void executeQuery(String findOption, String findWord){
		executeQuery("", findOption, findWord);
	}
	
	public void executeQuery(String partID, String findOption, String findWord) {
		ResultSet rs = null;
		String sql = "";
	
		try{
			sql = getSelectClause() + getFromClause(partID) + getWhereClause(partID);
			sql += getSearchCondition(findOption, findWord);
			sql += getOrderClause();
	
			rs = execQuery(sql);
			if(rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}
			setFields(rs);
		}catch(SQLException sqlEx){
			count = -1;
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] {sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql});
		}catch(Exception ex){
			count = -1;
			ex.printStackTrace();
		}finally{
			close(rs);
			execClose();
		}
	}

	private static final String getSelectClause() {
		return "select us.id, us.passwd, us.name, us.sys_auth, dt.name, us.duty_name, us.priority";
	}

	private static final String getFromClause(String partID) {
		String fromString = " from muser us, dept dt";
		
		if(!MString.isNull(partID))
			fromString +=", dept pt";
		
		return fromString;
	}

	private String getWhereClause(String partID) {
		String whereString = " where us.del_type="+Person.NOT_DEL+" and us.dept_id=dt.id";
		if(!MString.isNull(partID))
			whereString += " and dt.organ=pt.id and (pt.id='"+partID+"' or (pt.upper_dept='"+partID+"' and pt.sys_auth=0))";
		
		return whereString;
	}

	private String getSearchCondition(String findOption, String findWord) {
		if("userID".equals(findOption)){
			return " and us.id='"+findWord+"'";
		}else if("userName".equals(findOption)){
			return " and us.name like '%"+findWord+"%'";
		}else if("deptName".equals(findOption)){
			return " and dt.name like '%"+findWord+"%'";
		}else if("deptID".equals(findOption)){
			return " and dt.id = '"+findWord+"'";
		}else if("sys_auth".equals(findOption)){
			return " and us.sys_auth like '%"+findWord+"%'";
		}else if("all".equals(findOption)){
			return " and (us.id like '%"+findWord+"%' or us.name like '%"+findWord+"%' or dt.name like '%"+findWord+"%' or us.sys_auth like '%"+findWord+"%')";
		}else{
			return "";
		}
	}
	
	private String getOrderClause() {
		return " order by us.priority, us.sys_auth desc, dt.id";
	}
	
	private void setFields(ResultSet rs) throws SQLException {
		//DB에거 가저올 레코드 수 만큼 배열을 생성한다.
		idList = new String[count];
		passwdList = new String[count];
		nameList = new String[count];
		sysAuthList = new String[count];
		userNameList = new String[count];
		deptNameList = new String[count];
		dutyNameList = new String[count];
			
		for(int i=0; rs.next(); i++){
			idList[i] = rs.getString(1);
			passwdList[i] = rs.getString(2);
			nameList[i] = rs.getString(3);
			sysAuthList[i] = rs.getString(4);
			deptNameList[i] = MString.checkNull(rs.getString(5));
			dutyNameList[i] = MString.checkNull(rs.getString(6));
		}
	}

	public int getCount(){return count;}
	public String[] getIDList(){return idList;}
	public String[] getPasswdList(){return passwdList;}
	public String[] getNameList(){return nameList;}
	public String[] getSysAuthList(){return sysAuthList;}
	public String[] getDeptNameList(){return deptNameList;}
	public String[] getDutyNameList() {return dutyNameList;}
}
