/*
 * 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.kintai.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.kintai.dto.MYukyuDto;

public class MYukyuDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "M_YUKYU";
	public static final String KEY_1	= "KCODE";		//社員コード
	public static final String KEY_2 	= "KYUYOYMD";	//給与計算年月日

	// 列名
	public static final String COL_KCODE  		= "KCODE";		//社員コード
	public static final String COL_KYUYOYMD  	= "KYUYOYMD";	//給与計算年月日
	public static final String COL_ZZANT  		= "ZZANT";		//前月残数（当年）
	public static final String COL_ZZANZ  		= "ZZANZ";		//前月残数（前年）
	public static final String COL_SYOKA  		= "SYOKA";		//当月使用
	public static final String COL_SYOTOKU  	= "SYUTOKU";	//当月取得
	public static final String COL_HAIKI  		= "HAIKI";		//当月廃棄
	public static final String COL_TZANT  		= "TZANT";		//当月残数（当年）
	public static final String COL_TZANZ  		= "TZANZ";		//当月残数（前年）

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return MYukyuDto m_yukyuレコード
	 * @throws SQLException
	 */
	private MYukyuDto mapping() throws SQLException {
		MYukyuDto dto = new MYukyuDto();
		dto.setKCode 	(rs.getString (COL_KCODE));		//社員コード
		dto.setKyuyoYMD (rs.getInt    (COL_KYUYOYMD));	//給与計算年月日
		dto.setZZanT 	(rs.getDouble (COL_ZZANT));		//前月残数（当年）
		dto.setZZanZ 	(rs.getDouble (COL_ZZANZ));		//前月残数（前年）
		dto.setSyoka 	(rs.getDouble (COL_SYOKA));		//当月使用
		dto.setSyutoku 	(rs.getInt    (COL_SYOTOKU));	//当月取得
		dto.setHaiki 	(rs.getDouble (COL_HAIKI));		//当月廃棄
		dto.setTZanT 	(rs.getDouble (COL_TZANT));		//当月残数（当年）
		dto.setTZanZ 	(rs.getDouble (COL_TZANZ));		//当月残数（前年）
		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 kcode 社員コード(検索キー)
	 * @param kyuyoymd 給与計算年月日(検索キー)
	 * @return MYukyuDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MYukyuDto findForKey(
			String kcode,
			String kyuyoymd
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kcode);
			setParam(index++, Integer.parseInt(kyuyoymd));
			executeQuery();
			MYukyuDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * 条件文によるデータ取得
	 * @param kcode 検索社員コード
	 * @param kyuyoymd 検索給与計算年月日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MYukyuDto> findForList(
			String kcode,
			String kyuyoymd
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_KCODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_KYUYOYMD);
		sb.append(" <= ? ");
		sb.append(" ORDER BY ");
		sb.append(COL_KYUYOYMD);
		sb.append(" DESC ");
		sb.append(" LIMIT 12 ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++,   kcode  );
			setParam(index++,   Integer.parseInt(kyuyoymd));
			executeQuery();
			List<MYukyuDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * 条件文によるデータ取得
	 * @param kcode 検索社員コード
	 * @param kyuyoymd 検索給与計算年月日
	 * @return MYukyuDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MYukyuDto findForFormer(
			String kcode,
			String kyuyoymd
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_KCODE);
		sb.append(" = ? AND ");
		sb.append(COL_KYUYOYMD);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kcode  );
			setParam(index++,   Integer.parseInt(kyuyoymd));
			executeQuery();
			MYukyuDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MYukyuDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode());		//社員コード
		setParam(index++, dto.getKyuyoYMD());	//給与計算年月日
		setParam(index++, dto.getZZanT());		//前月残数（当年）
		setParam(index++, dto.getZZanZ());		//前月残数（前年）
		setParam(index++, dto.getSyoka());		//当月使用
		setParam(index++, dto.getSyutoku());	//当月取得
		setParam(index++, dto.getHaiki());		//当月廃棄
		setParam(index++, dto.getTZanT());		//当月残数（当年）
		setParam(index++, dto.getTZanZ());		//当月残数（前年）
		setCommonParams(isInsert);
	}

}
