/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.payroll.bonus.dao;

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

import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.BonusConst;
import jp.mosp.payroll.bonus.dto.PhBasisDto;

public class PhBasisDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE					= "UH_BASIS";
	public static final String	KEY_1					= "K_CODE";			// 社員コード
	public static final String	KEY_2					= "EXPECTED_DATE";		// 適用予定日
																				
	// 列名
	public static final String	COL_K_CODE				= "K_CODE";			// 社員コード
	public static final String	COL_EXPECTED_DATE		= "EXPECTED_DATE";		// 適用予定日
	public static final String	COL_APPLIED_DATE		= "APPLIED_DATE";		// 適用日
	public static final String	COL_K_NAME				= "K_NAME";			// 社員氏名
	public static final String	COL_K_KANA				= "K_KANA";			// 社員ﾌﾘｶﾞﾅ
	public static final String	COL_STATION_CODE		= "STATION_CODE";		// 所属コード
	public static final String	COL_POSITION_CODE		= "POSITION_CODE";		// 職位コード
	public static final String	COL_EMPLOYMENT_TYPE		= "EMPLOYMENT_TYPE";	// 雇用区分
	public static final String	COL_ENTRANCE_DATE		= "ENTRANCE_DATE";		// 入社日
	public static final String	COL_APPOINTMENT_DATE	= "APPOINTMENT_DATE";	// 登用日
	public static final String	COL_RETIREMENT_DATE		= "RETIREMENT_DATE";	// 退社日
																				
	
	/**
	 * コンストラクタ
	 */
	public PhBasisDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PhBasisDto> mappingAll() throws SQLException {
		List<PhBasisDto> all = new ArrayList<PhBasisDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PhBasisDto UhBasisレコード
	 * @throws SQLException
	 */
	private PhBasisDto mapping() throws SQLException {
		PhBasisDto dto = new PhBasisDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setExpectedDate(rs.getDate(COL_EXPECTED_DATE)); // 適用予定日
		dto.setAppliedDate(rs.getDate(COL_APPLIED_DATE)); // 適用日
		dto.setKName(rs.getString(COL_K_NAME)); // 社員氏名
		dto.setKKana(rs.getString(COL_K_KANA)); // 社員ﾌﾘｶﾞﾅ
		dto.setStationCode(rs.getString(COL_STATION_CODE)); // 所属コード
		dto.setPositionCode(rs.getString(COL_POSITION_CODE)); // 職位コード
		dto.setEmploymentType(rs.getString(COL_EMPLOYMENT_TYPE)); // 雇用区分
		dto.setEntranceDate(rs.getDate(COL_ENTRANCE_DATE)); // 入社日
		dto.setAppointmentDate(rs.getDate(COL_APPOINTMENT_DATE)); // 登用日
		dto.setRetirementDate(rs.getDate(COL_RETIREMENT_DATE)); // 退社日
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PhBasisDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @param expectedDate 適用年月(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForKey(String kCode, Date expectedDate) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードよるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForEmployeeData(String kCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(勤怠入力-社員一覧用)
	 * @param kCode       検索社員コード
	 * @param kName       検索社員名
	 * @param stationCode 検索所属コード
	 * @param selfKCode   操作社員コード
	 * @param datalevel   操作社員データレベル
	 * @param startDate   操作社員所属集計月開始日
	 * @param endDate     操作社員所属集計月終了日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForCondition(String kCode, String kName, String stationCode, String selfKCode,
			int dataLevel, Date startDate, Date endDate) throws SQLException, IllegalAccessException {
		index = 1;
		try {
			prepareStatement(getQueryForEmployeeSelect());
			// UH_BASIS検索用日付
			setParam(index++, endDate);
			// UH_BASIS部
			setParam(index++, kCode + "%");
			setParam(index++, "%" + kName + "%");
			setParam(index++, selfKCode);
			setParam(index++, stationCode);
			setParam(index++, stationCode);
			setParam(index++, dataLevel);
			setParam(index++, stationCode);
			setParam(index++, selfKCode);
			setParam(index++, stationCode);
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, dataLevel);
			setParam(index++, startDate);
			// 実行
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 年月によるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @param expectedDate 適用年月(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForDate(String kCode, Date expectedDate) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " = (");
			sb.append("SELECT MAX(" + COL_EXPECTED_DATE + ") ");
			sb.append("FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " <= ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			sb.append(")");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 年月によるデータ取得(離職証明書用)
	 * @param kCode 社員コード(検索キー)
	 * @param expectedDate 適用年月(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForLeveJob(String kCode, Date expectedDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " = (");
			sb.append("SELECT MAX(" + COL_EXPECTED_DATE + ") ");
			sb.append("FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " <= ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			sb.append(")");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForEmployeeAll(String kCode) throws SQLException, NoSuchFieldException,
			IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForEmployee(String kCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " = (");
			sb.append("SELECT MAX(" + COL_EXPECTED_DATE + ") ");
			sb.append("FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			sb.append(")");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, kCode);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードによる未適用データ取得
	 * @param kCode       社員コード(検索キー)
	 * @param isForUpdate UPDATE用フラグ
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForEmployeeNotApplied(String kCode, boolean isForUpdate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NULL ");
			if (isForUpdate) {
				sb.append(getForUpdate());
			}
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードによる適用データ取得(適用予定日より先)
	 * @param kCode        社員コード(検索キー)
	 * @param expectedDate 適用予定日(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForEmployeeApplied(String kCode, Date expectedDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_EXPECTED_DATE + " >= ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode        社員コード(検索キー)
	 * @param expectedDate 適用予定日(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PhBasisDto findForUpdate(String kCode, Date expectedDate) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(所属コード)(集計用)
	 * @param stationCode 検索所属コード
	 * @param startDate   対象集計月開始日
	 * @param endDate     対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForTotal(String stationCode, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PmStationDao.getStationQuery());
			sb.append(") ");
			sb.append("AND ");
		}
		sb.append(getSearchEmployeeStatement());
		try {
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, endDate);
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			setParam(index++, startDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(データ一覧検索用)
	 * @param stationCode 検索所属コード
	 * @param startDate   対象集計月開始日
	 * @param endDate     対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws IllegalAccessException
	 */
	public String findForDataLineUp(String stationCode) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(" * ");
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PmStationDao.getStationQuery());
			sb.append(") ");
			sb.append("AND ");
		}
		sb.append(getSearchEmployeeStatement());
		
		return sb.toString();
		
	}
	
	/**
	 * 条件文によるデータ取得(年末調整計算用)
	 * @param startDate   対象年開始日
	 * @param endDate     対象年最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws SQLException 
	 */
	public List<PhBasisDto> findForAdjustData(Date startDate, Date lastDate) throws IllegalAccessException,
			SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(" * ");
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		sb.append(COL_ENTRANCE_DATE + " <= ?");
		sb.append(" AND ");
		sb.append(COL_RETIREMENT_DATE + " >= ?");
		sb.append(" OR ");
		sb.append(COL_RETIREMENT_DATE + " IS NULL");
		try {
			prepareStatement(sb.toString());
			setParam(index++, lastDate);
			setParam(index++, lastDate);
			setParam(index++, startDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(年末調整計算用(個人))
	 * @param kCode       社員コード
	 * @param startDate   対象年開始日
	 * @param endDate     対象年最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws SQLException 
	 */
	public PhBasisDto findForAdjustEmpData(String kCode, Date startDate, Date lastDate) throws IllegalAccessException,
			SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(" * ");
		sb.append("FROM ");
		sb.append(getHistoryEmpTable());
		sb.append("WHERE ");
		sb.append(COL_ENTRANCE_DATE + " <= ?");
		sb.append(" AND ");
		sb.append(COL_RETIREMENT_DATE + " >= ?");
		sb.append(" OR ");
		sb.append(COL_RETIREMENT_DATE + " IS NULL");
		try {
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			setParam(index++, lastDate);
			setParam(index++, lastDate);
			setParam(index++, startDate);
			executeQuery();
			PhBasisDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(所属コード)(月次勤務データ用)
	 * @param stationCode 検索所属コード
	 * @param startDate   対象集計月開始日
	 * @param endDate     対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForReport(String stationCode, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		sb.append("(");
		sb.append(COL_STATION_CODE + " IN ");
		sb.append("(");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") ");
		sb.append("OR ");
		sb.append(COL_K_CODE + " IN ");
		sb.append("(");
		sb.append(PtSupportDao.getTermSupportQuery());
		sb.append(") ");
		sb.append(") ");
		sb.append("AND ");
		sb.append(getSearchEmployeeStatement());
		try {
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, endDate);
			setParam(index++, stationCode);
			setParam(index++, stationCode);
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, startDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(所属コード)(当月入社者)
	 * @param stationCode 検索所属コード
	 * @param startDate   対象集計月開始日
	 * @param endDate     対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForFlesh(String stationCode, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PmStationDao.getStationQuery());
			sb.append(") ");
			sb.append("AND ");
		}
		sb.append(getSearchEmployeeStatement());
		sb.append("AND ");
		sb.append(getSearchFleshStatement());
		try {
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, endDate);
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			setParam(index++, startDate);
			setParam(index++, startDate);
			setParam(index++, endDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(所属コード)(当月退職者)
	 * @param stationCode 検索所属コード
	 * @param startDate   対象集計月開始日
	 * @param endDate     対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForRetire(String stationCode, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PmStationDao.getStationQuery());
			sb.append(") ");
			sb.append("AND ");
		}
		sb.append(getSearchRetireStatement());
		try {
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, endDate);
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			setParam(index++, startDate);
			setParam(index++, endDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(所属コード)(同乗者、同乗先選択用)
	 * @param kCode          除去社員コード(本人除去用)
	 * @param stationCode    検索所属コード
	 * @param attendanceDate 対象日付(退職者除去用)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForFellow(String kCode, String stationCode, Date attendanceDate) throws SQLException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE (");
		sb.append(COL_STATION_CODE + " IN (");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") OR ");
		sb.append(COL_K_CODE + " IN (");
		sb.append(PtSupportDao.getSupportQuery());
		sb.append(")) AND ");
		sb.append(getSearchEmployeeStatement());
		sb.append("AND ");
		sb.append(COL_K_CODE + " <> ? ");
		try {
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, attendanceDate);
			setParam(index++, stationCode);
			setParam(index++, stationCode);
			setParam(index++, attendanceDate);
			setParam(index++, attendanceDate);
			setParam(index++, attendanceDate);
			setParam(index++, kCode);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(不整合検索用)
	 * @param stationCode 検索所属コード
	 * @param targetDate  対象日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForIncompatibility(String stationCode, Date targetDate) throws SQLException,
			IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("FROM ");
			sb.append(getHistoryTable());
			sb.append("WHERE (");
			sb.append(COL_STATION_CODE + " IN (");
			sb.append(PmStationDao.getStationQuery());
			sb.append(") OR ");
			sb.append(COL_K_CODE + " IN (");
			sb.append(PtSupportDao.getSupportQuery());
			sb.append(")) AND ");
			sb.append(getSearchEmployeeStatement());
			prepareStatement(getSelectStatement(getClass()) + sb.toString());
			setParam(index++, targetDate);
			setParam(index++, stationCode);
			setParam(index++, stationCode);
			setParam(index++, targetDate);
			setParam(index++, targetDate);
			setParam(index++, targetDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事基本情報適用画面用)
	 * @param expectedDate   適用予定日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForHistoryList(Date expectedDate) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_EXPECTED_DATE + " <= ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NULL ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, expectedDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param endDate        集計月終了日
	 * @param retirementFlag 退職フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public List<PhBasisDto> findForHumanData(Date endDate, String retirementFlag) throws SQLException,
			IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("FROM ");
			sb.append(getHistoryTable());
			if (retirementFlag != null && retirementFlag.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_ON))) {
				sb.append("WHERE ");
				sb.append(COL_RETIREMENT_DATE + " <= ? ");
			} else if (retirementFlag != null && retirementFlag.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_OFF))) {
				sb.append("WHERE ");
				sb.append(COL_RETIREMENT_DATE + " IS NULL ");
				sb.append("OR ");
				sb.append(COL_RETIREMENT_DATE + " > ? ");
			}
			prepareStatement(getSelectStatement(getClass()) + sb.toString());
			setParam(index++, endDate);
			if (retirementFlag != null
					&& (retirementFlag.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_ON)) || retirementFlag
						.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_OFF)))) {
				setParam(index++, endDate);
			}
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param endDate        集計月終了日
	 * @param retirementFlag 退職フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForRetireHumanData(Date targetDate, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("FROM ");
			sb.append(getHistoryTable());
			sb.append(" WHERE ");
			sb.append("(");
			sb.append(COL_RETIREMENT_DATE + "  >= ? ");
			sb.append(" AND ");
			sb.append(COL_RETIREMENT_DATE + " <= ? ");
			sb.append(") OR (");
			sb.append(COL_ENTRANCE_DATE + "  <= ? ");
			sb.append(" AND ");
			sb.append(COL_RETIREMENT_DATE + "  IS NULL ");
			sb.append(")");
			prepareStatement(getSelectStatement(getClass()) + sb.toString());
			setParam(index++, targetDate);
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, endDate);
			executeQuery();
			List<PhBasisDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(給与計算-勤怠確認用)
	 * @param kCode          検索社員コード
	 * @param kName          検索社員名
	 * @param kKana          検索ﾌﾘｶﾞﾅ
	 * @param stationCode    検索所属コード
	 * @param positionCode   検索職位コード
	 * @param retirementFlag 検索退職フラグ
	 * @param startDate      検索対象集計月初日
	 * @param endDate        検索対象集計月最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public List<PhBasisDto> findForHumanList(String kCode, String kName, String kKana, String stationCode,
			String positionCode, String retirementFlag, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("FROM ");
			sb.append(getHistoryTable());
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " LIKE ? ");
			sb.append("AND ");
			sb.append(COL_K_NAME + " LIKE ? ");
			sb.append("AND ");
			sb.append(COL_K_KANA + " LIKE ? ");
			if (stationCode != null && !stationCode.equals("")) {
				sb.append("AND ");
				sb.append(COL_STATION_CODE + " =  ? ");
			}
			if (positionCode != null && !positionCode.equals("")) {
				sb.append("AND ");
				sb.append(COL_POSITION_CODE + " = ? ");
			}
			if (retirementFlag != null && retirementFlag.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_OFF))) {
				sb.append("AND (");
				sb.append(COL_RETIREMENT_DATE + " IS NULL ");
				sb.append("OR ");
				sb.append(COL_RETIREMENT_DATE + " >= ? ");
				sb.append(") ");
			} else if (retirementFlag != null && retirementFlag.equals(String.valueOf(BonusConst.RETIREMENT_FLAG_ON))) {
				sb.append("AND (");
				sb.append(COL_RETIREMENT_DATE + " IS NOT NULL ");
				sb.append("AND ");
				sb.append(COL_RETIREMENT_DATE + " < ? ");
				sb.append(") ");
			}
			prepareStatement(getSelectStatement(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, endDate);
			setParam(index++, kCode + "%");
			setParam(index++, "%" + kName + "%");
			setParam(index++, "%" + kKana + "%");
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			if (positionCode != null && !positionCode.equals("")) {
				setParam(index++, positionCode);
			}
			if (retirementFlag != null && !retirementFlag.equals("")) {
				setParam(index++, startDate);
			}
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員コードによる未適用データ削除
	 * @param expectedDate 適用予定日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 */
	public int deleteNotApplied(String kCode) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_APPLIED_DATE + " IS NULL ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeUpdate();
			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 insert(PhBasisDto 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(PhBasisDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getExpectedDate());
			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(PhBasisDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getExpectedDate());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ削除
	 * @param kCode 削除対象社員コード
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 */
	public int deleteForEmployee(String kCode) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PhBasisDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()); // 社員コード
		setParam(index++, dto.getExpectedDate()); // 適用予定日
		setParam(index++, dto.getAppliedDate()); // 適用日
		setParam(index++, dto.getKName()); // 社員氏名
		setParam(index++, dto.getKKana()); // 社員ﾌﾘｶﾞﾅ
		setParam(index++, dto.getStationCode()); // 所属コード
		setParam(index++, dto.getPositionCode()); // 職位コード
		setParam(index++, dto.getEmploymentType()); // 雇用区分
		setParam(index++, dto.getEntranceDate()); // 入社日
		setParam(index++, dto.getAppointmentDate()); // 入社日
		setParam(index++, dto.getRetirementDate()); // 退社日
		setCommonParams(isInsert);
	}
	
	/**
	 * 勤怠入力-社員一覧用対象社員コード抽出SQL取得
	 * @return 勤怠入力-社員一覧用対象社員コード抽出SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getQueryForEmployeeSelect() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		//sb.append("SELECT " + COL_K_CODE + " ");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM " + getHistoryTable() + " ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " LIKE ? ");
		sb.append("AND ");
		sb.append(COL_K_NAME + " LIKE ? ");
		sb.append("AND ");
		sb.append("(");
		sb.append("("); // 自分
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_STATION_CODE + " IN (" + PmStationDao.getStationQuery() + ") ");
		sb.append(") ");
		sb.append("OR ");
		sb.append("("); // 所属+低データレベル
		sb.append(COL_STATION_CODE + " IN (" + PmStationDao.getStationQuery() + ") ");
		sb.append("AND ");
		sb.append(COL_POSITION_CODE + " IN (" + PmPositionDao.getQueryForLowerLevel() + ") ");
		sb.append(") ");
		sb.append("OR ");
		sb.append("("); // 所属+部下(Superior)
		sb.append(COL_STATION_CODE + " IN (" + PmStationDao.getStationQuery() + ") ");
		sb.append("AND ");
		sb.append(COL_K_CODE + " IN (" + PtSuperiorDao.getSuperiorQuery() + ") ");
		sb.append(") ");
		sb.append("OR ");
		sb.append("("); // 支援+低データレベル
		sb.append(COL_K_CODE + " IN (" + PtSupportDao.getTermSupportQuery() + ") ");
		sb.append("AND ");
		sb.append(COL_POSITION_CODE + " IN (" + PmPositionDao.getQueryForLowerLevel() + ") ");
		sb.append(")");
		sb.append(") ");
		sb.append("AND ");
		sb.append(getSearchEmployeeStatement());
		return sb.toString();
	}
	
	/**
	 * 対象社員コード取得SQL文字列(勤怠不整合確認用)
	 * @return 対象社員コード取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getQueryForAttendance() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT " + COL_K_CODE + " ");
		sb.append("FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE (");
		sb.append(COL_STATION_CODE + " IN (");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") OR ");
		sb.append(COL_K_CODE + " IN (");
		sb.append(PtSupportDao.getSupportQuery());
		sb.append(")) AND ");
		sb.append(getSearchEmployeeStatement());
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryTable() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append("SELECT ");
		sb.append(COL_K_CODE + " AS H_K_CODE, ");
		sb.append("MAX(" + COL_EXPECTED_DATE + ") AS H_EXPECTED_DATE ");
		sb.append("FROM " + TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_EXPECTED_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
		sb.append("GROUP BY " + COL_K_CODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE");
		sb.append(") " + TABLE + " ");
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得(社員)
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryEmpTable() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append("SELECT ");
		sb.append(COL_K_CODE + " AS H_K_CODE, ");
		sb.append("MAX(" + COL_EXPECTED_DATE + ") AS H_EXPECTED_DATE ");
		sb.append("FROM " + TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_EXPECTED_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
		sb.append("GROUP BY " + COL_K_CODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE");
		sb.append(") " + TABLE + " ");
		return sb.toString();
	}
	
	/**
	 * 在職者取得用検索SQL取得
	 * @return 在職者取得用検索SQL文字列
	 */
	private String getSearchEmployeeStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(COL_RETIREMENT_DATE + " IS NULL ");
		sb.append("OR ");
		sb.append(COL_RETIREMENT_DATE + " >= ?");
		sb.append(") ");
		return sb.toString();
	}
	
	/**
	 * 在職者取得用検索SQL取得
	 * @return 在職者取得用検索SQL文字列
	 */
	private String getSearchFleshStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(COL_ENTRANCE_DATE + " >= ? ");
		sb.append("AND ");
		sb.append(COL_ENTRANCE_DATE + " <= ?");
		sb.append(") ");
		return sb.toString();
	}
	
	/**
	 * 在職者取得用検索SQL取得
	 * @return 在職者取得用検索SQL文字列
	 */
	private String getSearchRetireStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(COL_RETIREMENT_DATE + " >= ? ");
		sb.append("AND ");
		sb.append(COL_RETIREMENT_DATE + " <= ?");
		sb.append(") ");
		return sb.toString();
	}
	
	/**
	 * 職位による並べ替えSQL取得
	 * @return 職位による並べ替えSQL文字列
	 */
	private String getOrderForPosition() {
		StringBuffer sb = new StringBuffer();
		sb.append("ORDER BY ");
		sb.append(COL_POSITION_CODE + ", ");
		sb.append(COL_EMPLOYMENT_TYPE + ", ");
		sb.append(COL_STATION_CODE + ", ");
		sb.append(COL_K_CODE + " ");
		return sb.toString();
	}
	
	/**
	 * 年末調整対象外データ検索用
	 * @return SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getDeleteAdjustData() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_K_CODE);
		sb.append(" FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		sb.append("(");
		sb.append(COL_ENTRANCE_DATE);
		sb.append(" > ");
		sb.append("?");
		sb.append(") ");
		sb.append(" OR ");
		sb.append(COL_ENTRANCE_DATE);
		sb.append(" <= ");
		sb.append("?");
		sb.append(" AND ");
		sb.append(COL_RETIREMENT_DATE);
		sb.append(" <= ");
		sb.append("?");
		return sb.toString();
		
	}
	
	/**
	 * 条件文によるデータ存在チェック
	 * @param  targetMode 存在確認処理モード
	 * @param  targetCode チェックコード(検索キー)
	 * @return PhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 */
	public int findForExist(String targetMode, String targetCode) throws SQLException {
		int count;
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT COUNT(*) FROM " + TABLE + " ");
		sb.append("WHERE ");
		if (targetMode.equals("station")) {
			sb.append(COL_STATION_CODE + " = ? ");
			
		} else if (targetMode.equals("position")) {
			sb.append(COL_POSITION_CODE + " = ? ");
			
		} else if (targetMode.equals("employment")) {
			sb.append(COL_EMPLOYMENT_TYPE + " = ? ");
		}
		sb.append("AND " + COL_APPLIED_DATE + " IS NOT NULL ");
		try {
			index = 1;
			prepareStatement(sb.toString());
			setParam(index++, targetCode);
			executeQuery();
			rs.next();
			count = rs.getInt(1);
			return count;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事追加情報出力用)
	 * @param kCode          検索社員コード
	 * @param kName          検索社員名
	 * @param kKana          検索ﾌﾘｶﾞﾅ
	 * @param stationCode    検索所属コード
	 * @param positionCode   検索職位コード
	 * @param retirementFlag 検索退職フラグ
	 * @param startDate      検索開始日
	 * @param endDate        検索終了日
	 * @return UhLicenseTypeDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PhBasisDto> findForConditionList(String kCode, String kName, String kKana, String stationCode,
			String positionCode, String retirementFlag, Date startDate, Date endDate) throws SQLException,
			IllegalAccessException {
		try {
			PmBasisDao umBasisDao = new PmBasisDao();
			StringBuffer sb = new StringBuffer();
			index = 1;
			sb.append(getSelectStatement(getClass()) + " ");
			sb.append("FROM ");
			sb.append(TABLE);
			sb.append(" WHERE ");
			sb.append(COL_K_CODE + " IN  ");
			sb.append(" ( ");
			sb.append(umBasisDao.getForAddHumanListSubQuery(stationCode, positionCode, retirementFlag));
			sb.append(" ) ");
			sb.append(" AND ");
			sb.append(COL_EXPECTED_DATE + " >= ? ");
			sb.append(" AND ");
			sb.append(COL_EXPECTED_DATE + " <= ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode + "%");
			setParam(index++, "%" + kName + "%");
			setParam(index++, "%" + kKana + "%");
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			if (positionCode != null && !positionCode.equals("")) {
				setParam(index++, positionCode);
			}
			setParam(index++, startDate);
			setParam(index++, endDate);
			executeQuery();
			List<PhBasisDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
}
