/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2008 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.human.dao;

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

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dao.MosPUserDao;

import jp.mosp.human.dto.MKihonDto;

import jp.mosp.system.dao.MSyozokuDao;

public class MKihonDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "M_KIHON";
	public static final String KEY_1	= "KCODE";	//社員コード

	// 列名
	public static final String COL_KCODE  	= "KCODE";		//社員コード
	public static final String COL_SIMEI  	= "SIMEI";		//氏名
	public static final String COL_FURIGANA  	= "FURIGANA";	//ふりがな
	public static final String COL_SCODE  	= "SCODE";		//所属コード
	public static final String COL_YCODE  	= "YCODE";		//役職コード
	public static final String COL_TOKYU  	= "TOKYU";		//等級
	public static final String COL_JCODE  	= "JCODE";		//事業所コード
	public static final String COL_NYDATE 	= "NYDATE";		//入社日
	public static final String COL_TSDATE  	= "TSDATE";		//退職日
	public static final String COL_KNKUBUN 	= "KNKUBUN";	//休退職区分
	public static final String COL_KYKUBUN  	= "KYKUBUN";	//給与区分
	public static final String COL_TXKUBUN  	= "TXKUBUN";	//税区分
	public static final String COL_TXKUBUNX  	= "TXKUBUNX";	//その他税区分
	public static final String COL_KMKUBUN  	= "KMKUBUN";	//組合区分
	public static final String COL_PTKUBUN  	= "PTKUBUN";	//パート区分
	public static final String COL_SHKUBUN  	= "SHKUBUN";	//支払区分
	public static final String COL_SIWAKECD  	= "SIWAKECD";	//仕訳集計コード
	public static final String COL_HAIGUSYA  	= "HAIGUSYA";	//扶養配偶者
	public static final String COL_FUYOSU  	= "FUYOSU";		//扶養家族数
	public static final String COL_YUKYU  	= "YUKYU";		//有給残
	public static final String COL_NENREI  	= "NENREI";		//年齢
	public static final String COL_KINZOKU  	= "KINZOKU";	//勤続年数
	public static final String COL_KENPONO  	= "KENPONO";	//健保番号
	public static final String COL_KOHONO  	= "KOHONO";		//厚保番号
	public static final String COL_KUSER  	= "KUSER";		//ユーザ
	public static final String COL_PASSW  	= "PASSW";		//パスワード
	public static final String COL_EMAIL  	= "EMAIL";		//email
	public static final String COL_NINSYO  	= "NINSYO";		//認証者
	public static final String COL_DATALVL  	= "DATALVL";	//データ権限レベル
	public static final String COL_PROGLEVEL	= "PROGLEVEL";	//プログラム使用権限

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return m_kihonDto m_kihonレコード
	 * @throws SQLException
	 */
	private MKihonDto mapping() throws SQLException {
		MKihonDto dto = new MKihonDto();
		dto.setKCode		(rs.getString (COL_KCODE));		//社員コード
		dto.setSimei 		(rs.getString (COL_SIMEI));		//氏名
		dto.setFurigana 	(rs.getString (COL_FURIGANA));	//ふりがな
		dto.setSCode 		(rs.getString (COL_SCODE));		//所属コード
		dto.setYCode 		(rs.getString (COL_YCODE));		//役職コード
		dto.setTokyu 		(rs.getString (COL_TOKYU));		//等級
		dto.setJCode 		(rs.getString (COL_JCODE));		//事業所コード
		dto.setNyDate 		(rs.getInt    (COL_NYDATE));	//入社日
		dto.setTsDate 		(rs.getInt    (COL_TSDATE));	//退職日
		dto.setKnKubun 		(rs.getString (COL_KNKUBUN));	//休退職区分
		dto.setKyKubun 		(rs.getString (COL_KYKUBUN));	//給与区分
		dto.setTxKubun 		(rs.getString (COL_TXKUBUN));	//税区分
		dto.setTxKubunx 	(rs.getString (COL_TXKUBUNX));	//その他税区分
		dto.setKmKubun 		(rs.getString (COL_KMKUBUN));	//組合区分
		dto.setPtKubun 		(rs.getString (COL_PTKUBUN));	//パート区分
		dto.setShKubun 		(rs.getString (COL_SHKUBUN));	//支払区分
		dto.setSiwakeCd 	(rs.getString (COL_SIWAKECD));	//仕訳集計コード
		dto.setHaigusya 	(rs.getString (COL_HAIGUSYA));	//扶養配偶者
		dto.setFuyosu 		(rs.getInt    (COL_FUYOSU));	//扶養家族数
		dto.setYukyu 		(rs.getDouble (COL_YUKYU));		//有給残
		dto.setNenrei 		(rs.getInt    (COL_NENREI));	//年齢
		dto.setKinzoku 		(rs.getInt    (COL_KINZOKU));	//勤続年数
		dto.setKenpoNo 		(rs.getString (COL_KENPONO));	//健保番号
		dto.setKohoNo 		(rs.getString (COL_KOHONO));	//厚保番号
		dto.setKUser 		(rs.getString (COL_KUSER));		//ユーザ
		dto.setPassw 		(rs.getString (COL_PASSW));		//パスワード
		dto.setEmail 		(rs.getString (COL_EMAIL));		//email
		dto.setNinsyo 		(rs.getString (COL_NINSYO));	//認証者
		dto.setDataLvl 		(rs.getInt    (COL_DATALVL));	//データ権限レベル
		dto.setProgLevel 	(rs.getInt    (COL_PROGLEVEL));	//プログラム使用権限
		return dto;
	}

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

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

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

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

	/**
	 * 条件文によるデータ取得
	 * @param kcode 検索社員コード
	 * @param simei
	 * @param sCode
	 * @param selfKCode
	 * @param dataLevel
	 * @param startDate
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForCondition(
			String	kcode,
			String	simei,
			String	sCode,
			String	selfJCode,
			String 	selfKCode,
			int		dataLevel,
			int		startDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(" ( ");
		sb.append(		COL_KCODE + " LIKE ? ");
		sb.append(		" AND ");		
		sb.append(		COL_SIMEI + " LIKE ? ");
		sb.append(		" AND ");
		// 事業所
		sb.append(		COL_JCODE + " = ? ");
		// 選択した所属
		if (!sCode.equals("")) {
			sb.append(	" AND ");
			sb.append(	COL_SCODE + " IN (" + MSyozokuDao.getSectionQuery() + ") ");
		}
		sb.append(" AND ");
		// データレベルが自分以下の人
		sb.append(COL_KCODE + " IN (" + MosPUserDao.getSearchDataLevelSubQuery() + ") ");
		sb.append(" AND ");
		// 集計開始日に在職している人		
		sb.append(getSearchEmployeeStatement());
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++		, kcode + "%");
			setParam(index++		, "%" + simei + "%");
			// 事業所
			setParam(index++		, selfJCode);
			// 所属
			if (!sCode.equals("")) {
				setParam(index++	, sCode);
			}
			// データレベル
			setParam(index++		, dataLevel);
			// 集計開始日
			setParam(index++		, startDate);
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param sCode			検索所属コード
	 * @param selfJCode		ユーザ所属事業所コード
	 * @param selfKCode		ユーザ社員コード
	 * @param dataLevel		検索データレベル
	 * @param startDate		開始日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForCondition(
			String	sCode,
			String	selfJCode,
			String 	selfKCode,
			int		dataLevel,
			int		startDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		// 自分
		sb.append(COL_KCODE + " = ? ");
		sb.append(" OR ");
		sb.append(" ( ");
		// 事業所
		sb.append(		COL_JCODE + " = ? ");
		sb.append(		" AND ");
		sb.append(		COL_SCODE + " IN (" + MSyozokuDao.getSectionQuery() + ") ");
		sb.append(		" AND ");
		// データレベルが自分以下の人
		sb.append(		COL_KCODE + " IN (" + MosPUserDao.getSearchDataLevelSubQuery() + ") ");
		sb.append(		" AND ");
		// 集計開始日に在職している人		
		sb.append(		getSearchEmployeeStatement());
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			// 自分
			setParam(index++	, selfKCode);
			// 事業所
			setParam(index++	, selfJCode);
			// 所属
			setParam(index++	, sCode);
			// データレベル
			setParam(index++	, dataLevel);
			// 集計開始日
			setParam(index++	, startDate);
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param tsdate 検索日付
	 * @param kcode 検索社員コード
	 * @param kname 検索社員名
	 * @param scode 検索所属コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForCondition(
			int		tsdate,
			String	kcode,
			String	kname,
			String	scode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(getSearchEmployeeStatement());
		sb.append(" ");
		if (!kcode.equals("")) {
			sb.append("AND ");
			sb.append(COL_KCODE);
			sb.append(" LIKE ? ");
		}
		if (!kname.equals("")) {
			sb.append("AND ");
			sb.append(COL_SIMEI);
			sb.append(" LIKE ? ");
		}
		if (!scode.equals("")) {
			sb.append("AND ");
			sb.append(COL_SCODE);
			sb.append(" IN( ");
			sb.append(MSyozokuDao.getSectionQuery());
			sb.append(") ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, tsdate);
			if (!kcode.equals("")) {
				setParam(index++, kcode + "%");
			}
			if (!kname.equals("")) {
				setParam(index++, "%" + kname + "%");
			}
			if (!scode.equals("")) {
				setParam(index++, scode);
			}
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得(所属コード)
	 * @param stationCode 検索所属コード
	 * @param date 年月日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForSection(
			String stationCode,
			int date
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_SCODE + " IN (");
		sb.append(MSyozokuDao.getSectionQuery());
		sb.append(") ");
		sb.append("AND ");
		sb.append(getSearchEmployeeStatement());
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForPosition());
			setParam(index++, stationCode);
			setParam(index++, date       );
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得(人事マスタ保守-社員一覧用)
	 * @param kCode          検索社員コード
	 * @param simei          検索社員名
	 * @param officeCode    検索事業所コード
	 * @param sectionCode    検索所属コード
	 * @param positionCode   検索職位コード
	 * @param retirementFlag 検索退職フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public List<MKihonDto> findForHumanList(
			String kCode,
			String simei,
			String officeCode,
			String sectionCode,
			String positionCode,
			String retirementFlag
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_KCODE + " LIKE ? ");
		sb.append(" AND ");
		sb.append(COL_SIMEI + " LIKE ? ");
		if (officeCode != null && !officeCode.equals("")) {
			sb.append("AND ");
			sb.append(COL_JCODE + " =  ? ");
		}
		if (sectionCode != null && !sectionCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_SCODE + " =  ? ");
		}
		if (positionCode != null && !positionCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_YCODE + " = ? ");
		}
		if (retirementFlag != null && retirementFlag.equals(String.valueOf(CommonConst.RETIREMENT_FLAG_OFF))) {
			sb.append(" AND ");
			sb.append(COL_KNKUBUN + " = '" + CommonConst.RETIREMENT_FLAG_OFF + "' ");
		} else if (retirementFlag != null && retirementFlag.equals(String.valueOf(CommonConst.RETIREMENT_FLAG_LEAVE))) {
			sb.append(" AND ");
			sb.append(COL_KNKUBUN + " = '" + CommonConst.RETIREMENT_FLAG_LEAVE + "' ");
		} else if (retirementFlag != null && retirementFlag.equals(String.valueOf(CommonConst.RETIREMENT_FLAG_ON))) {
			sb.append(" AND ");
			sb.append(COL_KNKUBUN + " = '" + CommonConst.RETIREMENT_FLAG_ON + "' ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode + "%");
			setParam(index++, "%" + simei + "%");
			if (officeCode != null && !officeCode.equals("")) {
				setParam(index++, officeCode);
			}
			if (sectionCode != null && !sectionCode.equals("")) {
				setParam(index++, sectionCode);
			}
			if (positionCode != null && !positionCode.equals("")) {
				setParam(index++, positionCode);
			}
			// 実行
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得(権限コード)
	 * @param authCode1 検索権限コード1
	 * @param authCode2 検索権限コード2
	 * @param date 年月日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForAuth(
			String authCode1,
			String authCode2,
			int date
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_KCODE + " IN ( " + MosPUserDao.getSearchApprovalSubQuery() + " ) ");
		sb.append(" AND ");
		sb.append(getSearchEmployeeStatement());
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, authCode1	);
			setParam(index++, authCode2	);
			setParam(index++, date		);
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param sCode			検索所属コード
	 * @param selfKCode		ユーザ社員コード
	 * @param dataLevel		検索データレベル
	 * @param startDate		開始日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForApproval(
			String	sCode,
			String	selfJCode,
			String 	selfKCode,
			int		dataLevel,
			int		startDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		// 自分を外す
		sb.append(COL_KCODE + " != ? ");
		sb.append(" AND ");
		sb.append(" ( ");
		// 事業所
		sb.append(		COL_JCODE + " = ? ");
		// 選択した所属
		sb.append(		" AND ");
		sb.append(		COL_SCODE + " IN (" + MSyozokuDao.getSectionQuery() + ") ");
		sb.append(		" AND ");
		// データレベルが自分以下の人
		sb.append(		COL_KCODE + " IN (" + MosPUserDao.getSearchDataLevelSubQuery() + ") ");
		sb.append(		" AND ");
		// 集計開始日に在職している人		
		sb.append(		getSearchEmployeeStatement());
		sb.append(		" AND ");
		sb.append(COL_KNKUBUN + " = '" + CommonConst.RETIREMENT_FLAG_OFF);
		sb.append("') ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			// 自分
			setParam(index++	, selfKCode);
			// 事業所
			setParam(index++	, selfJCode);
			// 所属
			setParam(index++	, sCode);
			// データレベル
			setParam(index++	, dataLevel);
			// 集計開始日
			setParam(index++	, startDate);
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param kCode			検索社員コード
	 * @param sCode			検索所属コード
	 * @param selfJCode		ユーザ所属事業所コード
	 * @param selfKCode		ユーザ社員コード
	 * @param dataLevel		検索データレベル
	 * @param startDate		開始日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKihonDto findForApproval(
			String  kCode,
			String	sCode,
			String	selfJCode,
			String 	selfKCode,
			int		dataLevel,
			int		startDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		// 対象者
		sb.append(COL_KCODE + " = ? ");
		sb.append(" AND ");
		sb.append(" ( ");
		// 事業所
		sb.append(		COL_JCODE + " = ? ");
		// 選択した所属
		sb.append(		" AND ");
		sb.append(		COL_SCODE + " IN (" + MSyozokuDao.getSectionQuery() + ") ");
		sb.append(		" AND ");
		// データレベルが自分以下の人
		sb.append(		COL_KCODE + " IN (" + MosPUserDao.getSearchDataLevelSubQuery() + ") ");
		sb.append(		" AND ");
		// 集計開始日に在職している人		
		sb.append(		getSearchEmployeeStatement());
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			// 対象者
			setParam(index++	, kCode);
			// 事業所
			setParam(index++	, selfJCode);
			// 所属
			setParam(index++	, sCode);
			// データレベル
			setParam(index++	, dataLevel);
			// 集計開始日
			setParam(index++	, startDate);
			executeQuery();
			MKihonDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param kCode 		検索社員コード
	 * @param selfJCode		ユーザ所属事業所コード
	 * @param dataLevel		検索データレベル
	 * @param startDate		開始日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKihonDto findForApprovalKCode(
			String  kCode,
			String	selfJCode,
			int		dataLevel,
			int		startDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		// 対象者
		sb.append(COL_KCODE + " = ? ");
		sb.append(" AND ");
		sb.append(" ( ");
		// 事業所
		sb.append(		COL_JCODE + " = ? ");
		sb.append(		" AND ");
		// データレベルが自分以下の人
		sb.append(		COL_KCODE + " IN (" + MosPUserDao.getSearchDataLevelSubQuery() + ") ");
		sb.append(		" AND ");
		// 集計開始日に在職している人		
		sb.append(		getSearchEmployeeStatement());
		sb.append(" ) ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			// 対象者
			setParam(index++	, kCode);
			// 事業所
			setParam(index++	, selfJCode);
			// データレベル
			setParam(index++	, dataLevel);
			// 集計開始日
			setParam(index++	, startDate);
			executeQuery();
			MKihonDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param tsdate 検索日付
	 * @param kcode 検索社員コード
	 * @param kname 検索社員名
	 * @param scode 検索所属コード
	 * @param limit 検索数
	 * @param offset
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForEmployeeWithLimit(
			int		tsdate,
			String	kcode,
			String	kname,
			String	scode,
			int		limit,
			int		offset
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(getSearchEmployeeStatement());
		sb.append(" ");
		if (!kcode.equals("")) {
			sb.append("AND ");
			sb.append(COL_KCODE);
			sb.append(" LIKE ? ");
		}
		if (!kname.equals("")) {
			sb.append("AND ");
			sb.append(COL_SIMEI);
			sb.append(" LIKE ? ");
		}
		if (!scode.equals("")) {
			sb.append("AND ");
			sb.append(COL_SCODE);
			sb.append(" IN( ");
			sb.append(MSyozokuDao.getSectionQuery());
			sb.append(") ");
		}
		sb.append("ORDER BY ");
		sb.append(KEY_1);
		sb.append(" LIMIT ? OFFSET ?");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, tsdate);
			if (!kcode.equals("")) {
				setParam(index++, kcode + "%");
			}
			if (!kname.equals("")) {
				setParam(index++, "%" + kname + "%");
			}
			if (!scode.equals("")) {
				setParam(index++, scode);
			}
			setParam(index++, limit);
			setParam(index++, offset);
			executeQuery();
			List<MKihonDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * count値取得
	 * @param tsdate 検索日付
	 * @param kcode 検索社員コード
	 * @param kname 検索社員名
	 * @param scode 検索所属コード
	 * @return count
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public int findForCount(
			int		tsdate,
			String	kcode,
			String	kname,
			String	scode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		int count = 0;
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT COUNT(");
		sb.append(COL_KCODE);
		sb.append(") FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(getSearchEmployeeStatement());
		sb.append(" ");
		if (!kcode.equals("")) {
			sb.append("AND ");
			sb.append(COL_KCODE);
			sb.append(" LIKE ? ");
		}
		if (!kname.equals("")) {
			sb.append("AND ");
			sb.append(COL_SIMEI);
			sb.append(" LIKE ? ");
		}
		if (!scode.equals("")) {
			sb.append("AND ");
			sb.append(COL_SCODE);
			sb.append(" IN( ");
			sb.append(MSyozokuDao.getSectionQuery());
			sb.append(") ");
		}
		try {
			prepareStatement(sb.toString());
			setParam(index++, tsdate);
			if (!kcode.equals("")) {
				setParam(index++, kcode + "%");
			}
			if (!kname.equals("")) {
				setParam(index++, "%" + kname + "%");
			}
			if (!scode.equals("")) {
				setParam(index++, scode);
			}
			executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
			return count;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MKihonDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode());		//社員コード
		setParam(index++, dto.getSimei());		//氏名
		setParam(index++, dto.getFurigana());	//ふりがな
		setParam(index++, dto.getSCode());		//所属コード
		setParam(index++, dto.getYCode());		//役職コード
		setParam(index++, dto.getTokyu());		//等級
		setParam(index++, dto.getJCode());		//事業所コード
		setParam(index++, dto.getNyDate());		//入社日
		setParam(index++, dto.getTsDate());		//退職日
		setParam(index++, dto.getKnKubun());	//休退職区分
		setParam(index++, dto.getKyKubun());	//給与区分
		setParam(index++, dto.getTxKubun());	//税区分
		setParam(index++, dto.getTxKubunx());	//その他税区分
		setParam(index++, dto.getKmKubun());	//組合区分
		setParam(index++, dto.getPtKubun());	//パート区分
		setParam(index++, dto.getShKubun());	//支払区分
		setParam(index++, dto.getSiwakeCd());	//仕訳集計コード
		setParam(index++, dto.getHaigusya());	//扶養配偶者
		setParam(index++, dto.getFuyosu());		//扶養家族数
		setParam(index++, dto.getYukyu());		//有給残
		setParam(index++, dto.getNenrei());		//年齢
		setParam(index++, dto.getKinzoku());	//勤続年数
		setParam(index++, dto.getKenpoNo());	//健保番号
		setParam(index++, dto.getKohoNo());		//厚保番号
		setParam(index++, dto.getKUser());		//ユーザ
		setParam(index++, dto.getPassw());		//パスワード
		setParam(index++, dto.getEmail());		//email
		setParam(index++, dto.getNinsyo());		//認証者
		setParam(index++, dto.getDataLvl());	//データ権限レベル
		setParam(index++, dto.getProgLevel());	//プログラム使用権限
		setCommonParams(isInsert);
	}

	/**
	 * 在職者取得用検索SQL取得
	 * @return 在職者取得用検索SQL文字列
	 */
	private String getSearchEmployeeStatement() {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(COL_TSDATE + " = 0 ");
		sb.append(" OR ");
		sb.append(COL_TSDATE + " >= ? ");
		sb.append(") ");
		sb.append(" AND ");
		sb.append(COL_KNKUBUN);		
		sb.append(" = '" + CommonConst.RETIREMENT_FLAG_OFF + "' ");
		return sb.toString();
	}

	/**
	 * 職位による並べ替えSQL取得
	 * @return 職位による並べ替えSQL文字列
	 */
	private String getOrderForPosition() {
		StringBuffer sb = new StringBuffer();
		sb.append("ORDER BY ");
		sb.append(COL_YCODE + ", ");
		sb.append(COL_PTKUBUN + ", ");
		sb.append(COL_SCODE + ", ");
		sb.append(COL_KCODE + " ");
		return sb.toString();
	}

	/**
	 * 社員コード検索用サブクエリー
	 * @return	sb	生成サブクエリ
	 */
	public static String getSearchKCodeSubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_KCODE);
		sb.append(" LIKE ? ");
		return sb.toString();
	}

	/**
	 * 社員コード検索用サブクエリー
	 * @return	sb	生成サブクエリ
	 */
	public static String getSearchKCodeEqualSubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		return sb.toString();
	}

	/**
	 * 社員名検索用サブクエリー
	 * @return	sb	生成サブクエリ
	 */
	public static String getSearchKNameSubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_SIMEI);
		sb.append(" LIKE ? ");
		return sb.toString();
	}

	/**
	 * 権限検索用サブクエリー
	 * @return	sb	生成サブクエリ
	 */
	public static String getSearchAuthoritySubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_PROGLEVEL);
		sb.append(" = ? ");
		return sb.toString();
	}
	
	/**
	 * 所属検索用サブクエリー
	 * @return　sb	生成サブクエリ
	 */
	public static String getSearchSectionSubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_SCODE);
		sb.append(" = ? ");
		return sb.toString();
	}

}
