/***********************************************************************************
 * @@ Program Name : AssignList.java Description : 보고자 배정 Author : Create Date : History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.totsys.repoadm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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.beans.entity.TotReport;

import kr.co.kihyun.db.DBManager;

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

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

	private ResultSet rs = null;
	private int process = 0;

	private int count = 0;
	private String[] userIDList = null;
	private String[] userNameList = null;
	private String[] deptNameList = null;
	private Long[] deptIDList = null;

	public void assignListQuery(String userID, Long deptID, int sysAuth) {
		StringBuilder sql = new StringBuilder(250);

		count = 0;

		try {
			sql.append("SELECT us.id, sp_cm_dec_fu(us.name), us.dept_id, dp.name FROM ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" dp ")
					.append("WHERE us.dept_id=dp.id AND us.dept_id=? AND us.sys_auth < 3 ")
					.append("AND us.sys_auth >= 0 and (us.del_type='0' or us.del_type='N')");

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

			userIDList = new String[count];
			userNameList = new String[count];
			deptNameList = new String[count];
			deptIDList = new Long[count];

			for (int i = 0; rs.next(); i++) {
				userIDList[i] = rs.getString(1);
				userNameList[i] = rs.getString(2);
				deptIDList[i] = rs.getLong(3);
				deptNameList[i] = rs.getString(4);
			}
		} 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 int assignAct(Long docID, Long deptID) {
		StringBuilder sql = new StringBuilder(90);

		process = 0;
		try {
			sql.append("SELECT prss FROM ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" WHERE doc_id=? AND dept_id=?");

			rs = execQuery(sql.toString(), docID, deptID);
			if (rs.next()) {
				process = rs.getInt(1);
			}
			return process;
		} 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 void searchListQuery(String userID, Long deptID, int sysAuth, String findOption, String findWord) {
		StringBuilder sql = new StringBuilder(300);
		ArrayList<Object> paramList = new ArrayList<Object>();
		count = 0;

		try {
			sql.append("SELECT us.id, sp_cm_dec_fu(us.name), us.dept_id, dp.name FROM ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" dp ")
					.append("WHERE (us.del_type='0' or us.del_type='N') and us.dept_id=dp.id ")
					.append("AND us.dept_id=? AND us.sys_auth < 3 AND us.sys_auth >= 0 AND ");
			paramList.add(deptID);
			if ("dept".equals(findOption)) {
				sql.append("dp.name like ?");
				paramList.add("%" + findWord + "%");
			} else if ("name".equals(findOption)) {
				sql.append("sp_cm_dec_fu(us.name) like ?");
				paramList.add("%" + findWord + "%");
			} else if ("id".equals(findOption)) {
				sql.append("us.id like ?");
				paramList.add("%" + findWord + "%");
			} else {
				sql.append("(dp.name like ? OR sp_cm_dec_fu(us.name) like ? OR us.id like ?)");
				paramList.add("%" + findWord + "%");
				paramList.add("%" + findWord + "%");
				paramList.add("%" + findWord + "%");
			}

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

			userIDList = new String[count];
			userNameList = new String[count];
			deptNameList = new String[count];
			deptIDList = new Long[count];

			for (int i = 0; rs.next(); i++) {
				userIDList[i] = rs.getString(1);
				userNameList[i] = rs.getString(2);
				deptIDList[i] = rs.getLong(3);
				deptNameList[i] = rs.getString(4);
			}
		} 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 int getCount() {
		return count;
	}

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

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

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

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