package jp.mosp.common.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.CtWorkflowCommentDto;

public class CtWorkflowCommentDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE  = "CT_WORKFLOW_COMMENT";
	public static final String KEY_1 	= "COMMENTID";	//コメントID

	// 列名
	public static final String COL_COMMENT_ID  		= "COMMENTID"		;	//コメントID
	public static final String COL_WORKFLOW  			= "WORKFLOW"		;	//ワークフロー番号
	public static final String COL_KCODE  				= "KCODE"			;	//社員コード
	public static final String COL_TARGET  			= "TARGET"			;	//対象者
	public static final String COL_AUTHOR_COMMENT  	= "AUTHORCOMMENT"	;	//コメント
	public static final String COL_DELETE_FLAG  		= "DELETEFLAG"		;	//削除フラグ

	/**
	 * コンストラクタ
	 */
	public CtWorkflowCommentDao() {
		super();
	}

	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CtWorkflowCommentDto> mappingAll() throws SQLException {
		List<CtWorkflowCommentDto> all = new ArrayList<CtWorkflowCommentDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}

	/**
	 * 検索結果取得(DTO)
	 * @return CtWorkFlowCommentDto ct_workflow_commentレコード
	 * @throws SQLException
	 */
	private CtWorkflowCommentDto mapping() throws SQLException {
		CtWorkflowCommentDto dto = new CtWorkflowCommentDto();
		dto.setCommentId(		rs.getLong (COL_COMMENT_ID)			);	//コメントID
		dto.setWorkflow(		rs.getLong (COL_WORKFLOW)			);	//ワークフロー番号
		dto.setKCode(			rs.getString (COL_KCODE)			);	//社員コード
		dto.setTarget(			rs.getString (COL_TARGET)			);	//ユーザ対象コメント
		dto.setAuthorComment(	rs.getString (COL_AUTHOR_COMMENT)	);	//承認者対象コメント
		dto.setDeleteFlag(		rs.getInt(COL_DELETE_FLAG)			);	//削除フラグ
		return dto;
	}

	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtWorkflowCommentDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CtWorkflowCommentDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param coMMENTID コメントID(検索キー)
	 * @param kcODE 社員コード(検索キー)
	 * @return CtWorkFlowCommentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtWorkflowCommentDto findForKey(
			long  commentid
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, commentid);
			executeQuery();
			CtWorkflowCommentDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param coMMENTID コメントID(検索キー)
	 * @param kcODE 社員コード(検索キー)
	 * @return CtWorkFlowCommentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtWorkflowCommentDto findForUpdate(
			long commentid
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, commentid);
			executeQuery();
			CtWorkflowCommentDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param coMMENTID 検索コメントID
	 * @param kcODE 検索社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtWorkflowCommentDto> findForCondition(
			long commentid,
			String kcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_COMMENT_ID);
		sb.append(" = ? AND ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   commentid  );
			setParam(index++,   kcode   + "%");
			executeQuery();
			List<CtWorkflowCommentDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * @param workflow
	 * @return
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtWorkflowCommentDto> findForWorkFlow(
			long workflow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_WORKFLOW);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   workflow  );
			executeQuery();
			List<CtWorkflowCommentDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * @param target
	 * @param workflow
	 * @return
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtWorkflowCommentDto> findForWorkFlow(
			String target,
			long workflow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_TARGET);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKFLOW);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   target  );
			setParam(index++,   workflow  );
			executeQuery();
			List<CtWorkflowCommentDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * @param target
	 * @param workflow
	 * @return
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtWorkflowCommentDto> findForTarget(
			String target,
			long workflow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_TARGET);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKFLOW);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()) + addDESC());
			setParam(index++,   target  );
			setParam(index++,   workflow  );
			executeQuery();
			List<CtWorkflowCommentDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param target 対象社員コード
	 * @param workflow ワークフロー番号
	 * @return CtWorkFlowCommentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtWorkflowCommentDto findForMaxTarget(
			String target,
			long workflow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_TARGET);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKFLOW);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()) + addDESCLimit1());
			setParam(index++,   target  );
			setParam(index++,   workflow  );
			executeQuery();
			CtWorkflowCommentDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	

	/**
	 * 条件文によるデータ取得
	 * @param target 対象社員コード
	 * @param workflow ワークフロー番号
	 * @return CtWorkFlowCommentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtWorkflowCommentDto findForMaxApprover(
			String kcode,
			long workflow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKFLOW);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()) + addDESCLimit1());
			setParam(index++,   kcode  		);
			setParam(index++,   workflow  	);
			executeQuery();
			CtWorkflowCommentDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(CtWorkflowCommentDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate();
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ更新
	 * @param dto 更新対象DTO
	 * @return int 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(CtWorkflowCommentDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getCommentId());
			setParam(index++, dto.getKCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(CtWorkflowCommentDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getCommentId());
			setParam(index++, dto.getKCode());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * max値取得
	 * @return String　最大連番
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public String findForMax() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			String maxEno = "";
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT MAX(");
			sb.append(COL_COMMENT_ID);
			sb.append(") FROM ");
			sb.append(TABLE);
			prepareStatement(sb.toString());
			executeQuery();
			if (rs.next()) {
				maxEno = String.valueOf(rs.getLong(1));
			}
			return maxEno;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(CtWorkflowCommentDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getCommentId());		//コメントID
		setParam(index++, dto.getWorkflow());		//ワークフロー番号
		setParam(index++, dto.getKCode());			//社員コード
		setParam(index++, dto.getTarget());			//対象者
		setParam(index++, dto.getAuthorComment());	//コメント
		setParam(index++, dto.getDeleteFlag());		//削除フラグ
		setCommonParams(isInsert);
	}

	private String addDESC() {
		return "DESC";
	}

	private String addDESCLimit1() {
		return "DESC LIMIT 1";
	}

}
