/*************************************************************************************************
 * 프로그램명 : MemoList.java 프로그램설명 : 재보고/ 재배정 내용 작성자 : 작성일 : 변경일 :
 **************************************************************************************************/

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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import kr.co.kihyun.beans.entity.Memo;
import kr.co.kihyun.beans.entity.MoumiEntity;
import kr.co.kihyun.beans.entity.TotReport;

import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.lang.MString;

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

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

	private String contents = "";
	private Date writeTime = null;
	private int count = 0;

	public void executeQuery(String groupID, Long docID, String deptID, int prss, String userID) {
		StringBuilder sql = new StringBuilder(300);
		ArrayList<Object> paramList = new ArrayList<Object>();
		try {
			sql.append("SELECT contents FROM ").append(MoumiEntity.getTableName(Memo.class)).append(" ");
			sql.append("WHERE doc_id=? AND group_id=? and response_user_id=? ");
			paramList.add(docID);
			paramList.add(groupID);
			paramList.add(userID);
			sql.append("AND (request_dept_id=? OR response_dept_id=?) ");
			paramList.add(deptID);
			paramList.add(deptID);
			sql.append("ORDER BY id DESC");
			LOG.debug("sql - {}", sql);
			rs = execQuery(sql.toString(), paramList.toArray());
			if (rs.next()) {
				contents = MString.checkNull(rs.getString(1));
			}
		} 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 recogQuery(String groupID, Long docID, String deptID, int prss, String userID) {
		StringBuilder sql = new StringBuilder(150);

		try {
			sql.append("select contents from ").append(MoumiEntity.getTableName(Memo.class))
					.append(" where doc_id=? and group_id=? and response_dept_id=? and response_user_id=? order by id desc");
			rs = execQuery(sql.toString(), docID, groupID, deptID, userID);
			if (rs.next()) {
				contents = MString.checkNull(rs.getString(1));
			}
		} 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 contentsNull(String groupID, Long docID, String deptID, int prss) {
		StringBuilder sql = new StringBuilder(250);

		count = 0;
		try {
			// DB에서 select해온 data 얻어오기.
			sql.append("SELECT count(*) FROM ")
					.append(MoumiEntity.getTableName(TotReport.class))
					.append(" trep, ").append(MoumiEntity.getTableName(Memo.class))
					.append(" mm WHERE mm.group_id=? AND mm.doc_id=? AND mm.doc_id=trep.doc_id")
					.append(" AND mm.response_dept_id=trep.dept_id")
					.append(" AND (mm.response_dept_id=? OR mm.request_dept_id=?) AND trep.prss=?");

			rs = execQuery(sql.toString(), groupID, docID, deptID, deptID, prss);
			if (rs.next()) {
				count = rs.getInt(1);
			}
			return count;
		} 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 contentsNull(String groupID, Long docID, String deptID, int prss, String userID) {
		StringBuilder sql = new StringBuilder(120);

		count = 0;
		try {
			// DB에서 select해온 data 얻어오기.
			sql.append("select count(*) from ").append(MoumiEntity.getTableName(TotReport.class))
					.append(" where doc_id=? and dept_id=? and prss=? and user_id=?");

			rs = execQuery(sql.toString(), docID, deptID, prss, userID);
			if (rs.next()) {
				count = rs.getInt(1);
			}
			return count;
		} 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 getContents() {
		return contents;
	}
}
