package jp.mosp.workflow.dao;

import java.sql.Date;
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.dao.CtWorkflowDao;

import jp.mosp.workflow.dto.CtOvertimeRequestDto;

public class CtOvertimeRequestDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE = "CT_OVERTIME_REQUEST";

	public static final String KEY_1 = "VOUCHER";

	// 列名
	public static final String COL_VOUCHER 		= "VOUCHER";
	public static final String COL_WORKFLOW 		= "WORKFLOW";
	public static final String COL_KCODE 			= "KCODE";
	public static final String COL_REQUESTDATE 	= "REQUESTDATE";
	public static final String COL_WORKDATE 		= "WORKDATE";
	public static final String COL_OVERTIME 		= "OVERTIME";
	public static final String COL_REASON 			= "REASON";
	public static final String COL_DELETEFLAG 		= "DELETEFLAG";

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return CtOvertimeRequestDto CT_OVERTIME_REQUESTレコード
	 * @throws SQLException
	 */
	private CtOvertimeRequestDto mapping() throws SQLException {
		CtOvertimeRequestDto dto = new CtOvertimeRequestDto();
		dto.setVoucher(		rs.getInt(COL_VOUCHER)		);
		dto.setWorkflow(	rs.getLong(COL_WORKFLOW)	);
		dto.setKCode(		rs.getString(COL_KCODE)		);
		dto.setRequestDate(	rs.getDate(COL_REQUESTDATE)	);
		dto.setWorkDate(	rs.getDate(COL_WORKDATE)	);
		dto.setOverTime(	rs.getInt(COL_OVERTIME)		);
		dto.setReason(		rs.getString(COL_REASON)	);
		dto.setDeleteFlag(	rs.getInt(COL_DELETEFLAG)	);
		mappingCommonInfo(dto);
		return dto;
	}

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

	/**
	 * キーによるデータ取得
	 * @param voucher
	 * @return CtOvertimeRequestDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtOvertimeRequestDto findForKey (
			int voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++,   voucher);
			executeQuery();
			CtOvertimeRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param voucher
	 * @return CtOvertimeRequestDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtOvertimeRequestDto findForUpdate (
			int voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++,   voucher);
			executeQuery();
			CtOvertimeRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param voucher
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtOvertimeRequestDto> findForCondition(
			int voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_VOUCHER);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   voucher);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param voucher
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtOvertimeRequestDto> findForCondition(
			String kCode,
			String startDate,
			String endDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKDATE);
		sb.append(" >= ? ");
		sb.append(" AND ");
		sb.append(COL_WORKDATE);
		sb.append(" <= ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kCode);
			setParam(index++,   Date.valueOf(startDate));
			setParam(index++,   Date.valueOf(endDate));
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param voucher
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtOvertimeRequestDto> findForCondition(
			String kCode,
			String startDate,
			String endDate,
			int layer,
			String status
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_WORKDATE);
		sb.append(" >= ? ");
		sb.append(" AND ");
		sb.append(COL_WORKDATE);
		sb.append(" <= ? ");
		if (status.length() != 0) {
			sb.append(" AND ");
			sb.append(COL_WORKFLOW);
			sb.append(" IN (");
			sb.append(CtWorkflowDao.getSubQueryWorkflowStatus());
			sb.append(") ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kCode);
			setParam(index++,   Date.valueOf(startDate));
			setParam(index++,   Date.valueOf(endDate));
			if (status.length() != 0) {
				setParam(index++,   layer);
				setParam(index++,   status);
			}
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param voucher
	 * @return CtOvertimeRequestDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtOvertimeRequestDto findForWorkDate(
			String kcode,
			String workdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(" WHERE ");
			sb.append(COL_DELETEFLAG);
			sb.append(" = " + MospConst.DELETE_FLAG_OFF);
			sb.append(" AND ");
			sb.append(COL_KCODE);
			sb.append(" = ? ");
			sb.append(" AND ");
			sb.append(COL_WORKDATE);
			sb.append(" = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++,   kcode);
			setParam(index++,   Date.valueOf(workdate));
			executeQuery();
			CtOvertimeRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} 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 maxVaoucher = "";
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT MAX(");
			sb.append(COL_VOUCHER);
			sb.append(") FROM ");
			sb.append(TABLE);
			prepareStatement(sb.toString());
			executeQuery();
			if (rs.next()) {
				maxVaoucher = String.valueOf(rs.getInt(1));
			}
			return maxVaoucher;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(
			CtOvertimeRequestDto 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(
			CtOvertimeRequestDto dto
	) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getVoucher());
			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(
			CtOvertimeRequestDto dto
	) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getVoucher());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(CtOvertimeRequestDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getVoucher());
		setParam(index++, dto.getWorkflow());
		setParam(index++, dto.getKCode());
		setParam(index++, dto.getRequestDate());
		setParam(index++, dto.getWorkDate());
		setParam(index++, dto.getOverTime());
		setParam(index++, dto.getReason());
		setParam(index++, dto.getDeleteFlag());
		setCommonParams(isInsert);
	}

}