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

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

import jp.mosp.human.dao.MKihonDao;
import jp.mosp.human.dto.MKihonDto;

public class MKihonPayrollDao extends MKihonDao {
	
	/**
	 * 初期処理を行う。
	 */
	public MKihonPayrollDao() {
		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;
	}
	
	/**
	 * 条件文によりデータを取得する。
	 * @param tsdate 検索日付
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> find(
			int tsdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append("(");
		sb.append(COL_TSDATE + " = 0 ");
		sb.append(" OR ");
		sb.append(COL_TSDATE + " >= ? ");
		sb.append(") ");
		sb.append(" ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, tsdate);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
}
