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

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

import java.sql.*;
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.Repoadm;

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

import kr.co.kihyun.db.*;

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

	private ResultSet rs = null;

	private int count = 0;
	private String[] repoadmNameList = null;
	private String[] repoadmIDList = null;
	private Long[] repoadmDeptList = null;
	private String[] repoadmDeptNameList = null;
	private int[] recogNumList = null;
	private int[] recogList = null;
	private String[] userNameList = null;
	private String[] decDateList = null;
	private int[] historyList = null;

	public void repoadmListQuery(Long docID, Long deptID) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("SELECT ra.repoadm_id, ra.recog_num, sp_cm_dec_fu(us.name), ra.dec_date FROM ")
					.append(MoumiEntity.getTableName(Repoadm.class)).append(" ra, ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us ")
					.append("WHERE ra.repoadm_id=us.id and ra.doc_id=? and ra.dept_id=? order by recog_num");
			rs = execQuery(sql.toString(), docID, deptID);
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}

			repoadmIDList = new String[count];
			recogNumList = new int[count];
			userNameList = new String[count];
			decDateList = new String[count];

			for (int i = 0; rs.next(); i++) {
				repoadmIDList[i] = rs.getString(1);
				recogNumList[i] = rs.getInt(2);
				userNameList[i] = rs.getString(3);
				decDateList[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 void repoadmListQuery(Long docID, Long deptID, String repUserID) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("SELECT ra.repoadm_id, ra.recog_num, sp_cm_dec_fu(us.name), ra.dec_date FROM ")
					.append(MoumiEntity.getTableName(Repoadm.class)).append(" ra, ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us ")
					.append("WHERE ra.repoadm_id=us.id and ra.doc_id=? and ra.dept_id=? and ra.repuser_id=? ")
					.append("order by recog_num");

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

			repoadmIDList = new String[count];
			recogNumList = new int[count];
			userNameList = new String[count];
			decDateList = new String[count];

			for (int i = 0; rs.next(); i++) {
				repoadmIDList[i] = rs.getString(1);
				recogNumList[i] = rs.getInt(2);
				userNameList[i] = rs.getString(3);
				decDateList[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 void repoadmListQuery(Long docID, Long deptID, int seqNum) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("SELECT ra.repoadm_id, ra.recog_num, sp_cm_dec_fu(us.name), ra.dec_date FROM ")
					.append(MoumiEntity.getTableName(Repoadm.class)).append(" ra, ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us ")
					.append("WHERE ra.repoadm_id=us.id and ra.doc_id=? and ra.dept_id=? and ra.seq_num=? ")
					.append("order by recog_num");

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

			repoadmIDList = new String[count];
			recogNumList = new int[count];
			userNameList = new String[count];
			decDateList = new String[count];

			for (int i = 0; rs.next(); i++) {
				repoadmIDList[i] = rs.getString(1);
				recogNumList[i] = rs.getInt(2);
				userNameList[i] = rs.getString(3);
				decDateList[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 void repoadmListQuery(Long docID, Long deptID, int seqNum, String repUserID) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("SELECT ra.repoadm_id, ra.recog_num, sp_cm_dec_fu(us.name), ra.dec_date, ra.history FROM ")
					.append(MoumiEntity.getTableName(Repoadm.class)).append(" ra, ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us ")
					.append("WHERE ra.repoadm_id=us.id and ra.doc_id=? and ra.dept_id=? ")
					.append("and ra.seq_num=? and ra.repuser_id=? order by recog_num");

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

			repoadmIDList = new String[count];
			recogNumList = new int[count];
			userNameList = new String[count];
			decDateList = new String[count];
			historyList = new int[count];

			for (int i = 0; rs.next(); i++) {
				repoadmIDList[i] = rs.getString(1);
				recogNumList[i] = rs.getInt(2);
				userNameList[i] = rs.getString(3);
				decDateList[i] = rs.getString(4);
				historyList[i] = rs.getInt(5);
			}
		} 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 repoadmSetList(Long docID, String repUserID, Long deptID) {
		StringBuilder sql = new StringBuilder(300);

		try {
			sql.append("select sp_cm_dec_fu(us.name), dt.name, ")
					.append("ra.repoadm_id, ra.repoadm_dept_id, ra.recog, ra.recog_num from ")
					.append(MoumiEntity.getTableName(Repoadm.class)).append(" ra, ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" dt ")
					.append("where ra.repoadm_id=us.id and ra.repoadm_dept_id=dt.id and ra.doc_id=? ")
					.append("and ra.repuser_id=? and ra.dept_id=? and ra.history=0 order by recog_num");

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

			repoadmNameList = new String[count];
			repoadmDeptNameList = new String[count];
			repoadmIDList = new String[count];
			repoadmDeptList = new Long[count];
			recogList = new int[count];
			recogNumList = new int[count];

			for (int i = 0; rs.next(); i++) {
				repoadmNameList[i] = rs.getString(1);
				repoadmDeptNameList[i] = rs.getString(2);
				repoadmIDList[i] = rs.getString(3);
				repoadmDeptList[i] = rs.getLong(4);
				recogList[i] = rs.getInt(5);
				recogNumList[i] = rs.getInt(6);
			}
		} 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[] getRepoadmNameList() {
            //24.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by KWON,HAN
	    //	return repoadmNameList;
                
            String[] ret = null;
            if( this.repoadmNameList != null) {
                ret = new String[repoadmNameList.length];
                for (int i=0; i<repoadmNameList.length; i++) {
                    ret[i] = this.repoadmNameList[i];
                }
            }
            return ret;
            //====================================================================                   
	}

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

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

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

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

	public int[] getRecogNumList() {
	    //	return recogNumList;
                
            int[] ret = null;
            if( this.recogNumList != null) {
                ret = new int[recogNumList.length];
                for (int i=0; i<recogNumList.length; i++) {
                    ret[i] = this.recogNumList[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 String[] getDecDateList() {
	    //	return decDateList;
                
            String[] ret = null;
            if( this.decDateList != null) {
                ret = new String[decDateList.length];
                for (int i=0; i<decDateList.length; i++) {
                    ret[i] = this.decDateList[i];
                }
            }
            return ret;                 
	}

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