/*
 * 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.payroll.dao;

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

import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.dto.MKeisanDto;

public class MKeisanDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "M_KEISAN";
	public static final String KEY_1	= "KTNEN";		//給与計算年

	// 列名
	public static final String COL_KTNEN 		= "KTNEN";		//給与計算年
	public static final String COL_KTTUKI  	= "KTTUKI";		//給与計算月
	public static final String COL_KSNEN  	= "KSNEN";		//給与支払年
	public static final String COL_KSTUKI  	= "KSTUKI";		//給与支払月
	public static final String COL_KSHI  		= "KSHI";		//給与支払日
	public static final String COL_KKANRYOU  	= "KKANRYOU";	//給与完了
	public static final String COL_STNEN  	= "STNEN";		//賞与計算年
	public static final String COL_STTUKI  	= "STTUKI";		//賞与計算月
	public static final String COL_SSNEN  	= "SSNEN";		//賞与支払年
	public static final String COL_SSTUKI  	= "SSTUKI";		//賞与支払月
	public static final String COL_SSHI  		= "SSHI";		//賞与支払日
	public static final String COL_SKANRYOU  	= "SKANRYOU";	//賞与完了
	public static final String COL_NTNEN  	= "NTNEN";		//年調計算年
	public static final String COL_NKANRYOU  	= "NKANRYOU";	//年調完了
	public static final String COL_TTNEN  	= "TTNEN";		//算定計算年
	public static final String COL_TTTUKI  	= "TTTUKI";		//算定計算月
	public static final String COL_TKANRYOU  	= "TKANRYOU";	//算定完了
	public static final String COL_GTNEN  	= "GTNEN";		//月変計算年
	public static final String COL_GTTUKI  	= "GTTUKI";		//月変計算月
	public static final String COL_GKANRYOU  	= "GKANRYOU";	//月変完了

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return m_keisanDto m_keisanレコード
	 * @throws SQLException
	 */
	private MKeisanDto mapping() throws SQLException {
		MKeisanDto dto = new MKeisanDto();
		dto.setKTNen 	(rs.getInt    (COL_KTTUKI  	));	//給与計算年
		dto.setKTTuki 	(rs.getInt    (COL_KSNEN  	));	//給与計算月
		dto.setKSNen 	(rs.getInt    (COL_KSTUKI  	));	//給与支払年
		dto.setKSTuki 	(rs.getInt    (COL_KSHI  	));	//給与支払月
		dto.setKShi 	(rs.getInt    (COL_KKANRYOU ));	//給与支払日
		dto.setKKanryou (rs.getString (COL_KKANRYOU ));	//給与完了
		dto.setSTNen 	(rs.getInt    (COL_STNEN  	));	//賞与計算年
		dto.setSTTuki 	(rs.getInt    (COL_STTUKI  	));	//賞与計算月
		dto.setSSNen 	(rs.getInt    (COL_SSNEN  	));	//賞与支払年
		dto.setSSTuki 	(rs.getInt    (COL_SSTUKI  	));	//賞与支払月
		dto.setSSHi 	(rs.getInt    (COL_SSHI  	));	//賞与支払日
		dto.setSKanryou (rs.getString (COL_SKANRYOU ));	//賞与完了
		dto.setNTNen 	(rs.getInt    (COL_NTNEN  	));	//年調計算年
		dto.setNKanryou (rs.getString (COL_NKANRYOU ));	//年調完了
		dto.setTTNen 	(rs.getInt    (COL_TTNEN  	));	//算定計算年
		dto.setTTTuki 	(rs.getInt    (COL_TTTUKI  	));	//算定計算月
		dto.setTKanryou (rs.getString (COL_TKANRYOU ));	//算定完了
		dto.setGTNen 	(rs.getInt    (COL_GTNEN  	));	//月変計算年
		dto.setGTTuki 	(rs.getInt    (COL_GTTUKI  	));	//月変計算月
		dto.setGKanryou (rs.getString (COL_GKANRYOU ));	//月変完了
		return dto;
	}

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

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

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

	/**
	 * 全データ取得
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKeisanDto findForCalc(
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()));
			executeQuery();
			MKeisanDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

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

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MKeisanDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getKTNen());		//給与計算年
		setParam(index++, dto.getKTTuki());		//給与計算月
		setParam(index++, dto.getKSNen());		//給与支払年
		setParam(index++, dto.getKSTuki());		//給与支払月
		setParam(index++, dto.getKShi());		//給与支払日
		setParam(index++, dto.getKKanryou());	//給与完了
		setParam(index++, dto.getSTNen());		//賞与計算年
		setParam(index++, dto.getSTNen());		//賞与計算月
		setParam(index++, dto.getSSNen());		//賞与支払年
		setParam(index++, dto.getSSTuki());		//賞与支払月
		setParam(index++, dto.getSSHi());		//賞与支払日
		setParam(index++, dto.getSKanryou());	//賞与完了
		setParam(index++, dto.getNTNen());		//年調計算年
		setParam(index++, dto.getNKanryou());	//年調完了
		setParam(index++, dto.getTTNen());		//算定計算年
		setParam(index++, dto.getTTTuki());		//算定計算月
		setParam(index++, dto.getTKanryou());	//算定完了
		setParam(index++, dto.getGTNen());		//月変計算年
		setParam(index++, dto.getGTTuki());		//月変計算月
		setParam(index++, dto.getGKanryou());	//月変完了
		setCommonParams(isInsert);
	}

}
