/***********************************************************************************
 * @@ Program Name : Report.java Description : 보고자료에 대한 전반적인 정보 Author : 강원중 Create Date : 2004-10-27 History :
 * @@
 ***********************************************************************************/

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

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.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.beans.entity.TotReportType;

import kr.co.kihyun.db.DBManager;

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

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

	private ResultSet rs = null;

	/******** 하나의 집계문서의 총 보고기관 수(dept count) ********/
	public int getAllCount(Long docID) {
		return getAllCount(docID, null);
	}

	public int getAllCount(Long docID, String partID) {
		StringBuilder sql = new StringBuilder(200);

		try {
			sql.append("select count(rt.dept_id) from ").append(MoumiEntity.getTableName(TotReport.class));
			if (partID == null) {
				sql.append(" rt where rt.doc_id = ? and (rt.del_type='0' or rt.del_type='N')");
				rs = execQuery(sql.toString(), docID);
			} else {
				sql.append(" rt, ").append(MoumiEntity.getTableName(MDept.class)).append(" dt ")
						.append("where rt.doc_id = ? and rt.dept_id=dt.id and dt.organ = ? ").append("and (rt.del_type='0' or rt.del_type='N')");
				rs = execQuery(sql.toString(), docID, partID);
			}
//            System.out.print("All Count :::: query::::"+sql.toString());
			return rs.next() ? rs.getInt(1) : 0;
		} 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 getSendCount(Long docID) {
		StringBuilder sql = new StringBuilder(150);

		try {
			sql.append("select count(rt.dept_id) from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" rt where rt.doc_id = ? ")
					//.append("and (rt.del_type='0' or rt.del_type='N') and prss=? and data is not null");
                    .append("and (rt.del_type='0' or rt.del_type='N') and prss=?");

			rs = execQuery(sql.toString(), docID, TotReportProcess.COMP.ordinal());
//            System.out.print("Seld Count :::: query::::"+sql.toString());
			return rs.next() ? rs.getInt(1) : 0;
		} 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 getNotCount(Long docID) {
		StringBuilder sql = new StringBuilder(150);

		try {
			sql.append(" select count(rt.dept_id) from ")
               .append(MoumiEntity.getTableName(TotReport.class))
			   .append(" rt where rt.doc_id = ? ")
			   .append(" and (rt.del_type='0' or rt.del_type='N') and prss=? and data is not null ");

			rs = execQuery(sql.toString(), docID,  TotReportProcess.NOT.ordinal());
//            System.out.print("Not Count :::: query::::"+sql.toString());
			return rs.next() ? rs.getInt(1) : 0;

		} 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 getNoteCount(Long docID) {
		StringBuilder sql = new StringBuilder(150);

		try {
			sql.append("select count(rt.dept_id) from ")
               .append(MoumiEntity.getTableName(TotReport.class))
			   .append(" rt where rt.doc_id = ? ")
               .append(" and (rt.del_type='0' or rt.del_type='N') ")
			   .append(" and prss in (14) ");

			rs = execQuery(sql.toString(), docID);
//            System.out.print("Note Count :::: query::::"+sql.toString());
			return rs.next() ? rs.getInt(1) : 0;

		} 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 getRecog(Long docID, String deptID, String userID) {
		StringBuilder sql = new StringBuilder(90);

		try {
			sql.append("select recog from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" where doc_id = ? and dept_id = ? and user_id=?");

			rs = execQuery(sql.toString(), docID, deptID, userID);
			return rs.next() ? rs.getInt(1) : 0;
		} 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[] getReportDeptList(Long docID) {
		StringBuilder sql = new StringBuilder(150);

		int count = 0;
		String[] deptIDList = null;
		try {
			sql.append("select rt.dept_id from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" rt where rt.doc_id = ? and rt.type=? ")
					.append("and (rt.del_type='0' or rt.del_type='N') and rt.doc_type=0");

//            System.out.print("Report.getReportDeptList :::: query::::\n"+sql.toString()+"\n");
//            System.out.print("Report.getReportDeptList :::: docID::::\n"+docID+"\n");
//            System.out.print("Report.getReportDeptList :::: TotReportType.REPORT.ordinal()::::\n"+TotReportType.REPORT.ordinal()+"\n");
            
			rs = execQuery(sql.toString(), docID, TotReportType.REPORT.ordinal());
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return null;
			}

			deptIDList = new String[count];

			for (int i = 0; rs.next(); i++) {
				deptIDList[i] = rs.getString(1);
			}

			return deptIDList;
		} 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[] getReportUserList(Long docID) {
		StringBuilder sql = new StringBuilder(200);

		int count = 0;
		String[] userIDList = null;
		try {
			sql.append("select rt.user_id from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" rt where rt.doc_id = ? and rt.type=? ")
					.append("and (rt.del_type='0' or rt.del_type='N') and rt.doc_type=1 order by rt.user_id");

//            System.out.print("Report.getReportUserList :::: query::::\n"+sql.toString()+"\n");
//            System.out.print("Report.getReportUserList :::: docID::::\n"+docID+"\n");
//            System.out.print("Report.getReportUserList :::: TotReportType.REPORT.ordinal()::::\n"+TotReportType.REPORT.ordinal()+"\n");
            
			rs = execQuery(sql.toString(), docID, TotReportType.REPORT.ordinal());
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return null;
			}

			userIDList = new String[count];

			for (int i = 0; rs.next(); i++) {
				userIDList[i] = rs.getString(1);
			}

			return userIDList;
		} 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[] getReportUserDeptList(Long docID) {
		StringBuilder sql = new StringBuilder(200);

		int count = 0;
		String[] deptIDList = null;

		try {
			sql.append("select rt.dept_id from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" rt where rt.doc_id = ? and rt.type=? ")
					.append("and (rt.del_type='0' or rt.del_type='N') and rt.doc_type=1 order by rt.user_id");

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

			deptIDList = new String[count];

			for (int i = 0; rs.next(); i++) {
				deptIDList[i] = rs.getString(1);
			}

			return deptIDList;
		} 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();
		}
	}

	/******** 2단계 집계 문서 아이디 ********/
	public Long getDownDocID(Long docID, String deptID) {
		StringBuilder sql = new StringBuilder(90);

		Long downDocID = null;

		try {
			sql.append("select down_doc_id from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" where doc_id = ? and dept_id = ?");

			rs = execQuery(sql.toString(), docID, deptID);
			if (rs.next()) {
				downDocID = rs.getLong(1);
			}

			return downDocID;
		} 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();
		}
	}
}
