/*
 * 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.HashMap;
import java.util.List;
import java.util.Map;

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

public class PmExtraDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE					= "UM_EXTRA";
	public static final String	KEY_1					= "K_CODE";			// 社員コード
																				
	// 列名
	public static final String	COL_K_CODE				= "K_CODE";			// 社員コード
	public static final String	COL_GENDER				= "GENDER";			// 性別
	public static final String	COL_BIRTH_DATE			= "BIRTH_DATE";		// 生年月日
	public static final String	COL_FORMER_NAME			= "FORMER_NAME";		// 旧姓
	public static final String	COL_POSTAL_CODE			= "POSTAL_CODE";		// 郵便番号
	public static final String	COL_ADDRESS				= "ADDRESS";			// 現住所
	public static final String	COL_FORMER_ADDRESS		= "FORMER_ADDRESS";	// 旧住所
	public static final String	COL_PHONE_NUMBER		= "PHONE_NUMBER";		// 電話番号
	public static final String	COL_CELLPHONE_NUMBER	= "CELLPHONE_NUMBER";	// 携帯番号
	public static final String	COL_EMERGENCY_NUMBER	= "EMERGENCY_NUMBER";	// 緊急番号
	public static final String	COL_APPOINTMENT_DATE	= "APPOINTMENT_DATE";	// 正社員登用日
	public static final String	COL_RETIRE_MONEY_DATE	= "RETIRE_MONEY_DATE";	// 退職金起算日
	public static final String	COL_EXTRA_DATE_1		= "EXTRA_DATE_1";		// 追加日付1
	public static final String	COL_EXTRA_ITEM_1		= "EXTRA_ITEM_1";		// 追加項目1
																				
	
	/**
	 * コンストラクタ
	 */
	public PmExtraDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PmExtraDto> mappingAll() throws SQLException {
		List<PmExtraDto> all = new ArrayList<PmExtraDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PmExtraDto UmBasisレコード
	 * @throws SQLException
	 */
	private PmExtraDto mapping() throws SQLException {
		PmExtraDto dto = new PmExtraDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setGender(rs.getString(COL_GENDER)); // 性別
		dto.setBirthDate(rs.getDate(COL_BIRTH_DATE)); // 生年月日
		dto.setFormerName(rs.getString(COL_FORMER_NAME)); // 旧姓
		dto.setPostalCode(rs.getString(COL_POSTAL_CODE)); // 郵便番号
		dto.setAddress(rs.getString(COL_ADDRESS)); // 現住所
		dto.setFormerAddress(rs.getString(COL_FORMER_ADDRESS)); // 旧住所
		dto.setPhoneNumber(rs.getString(COL_PHONE_NUMBER)); // 電話番号
		dto.setCellphoneNumber(rs.getString(COL_CELLPHONE_NUMBER)); // 携帯番号
		dto.setEmergencyNumber(rs.getString(COL_EMERGENCY_NUMBER)); // 緊急番号
		dto.setAppointmentDate(rs.getDate(COL_APPOINTMENT_DATE)); // 正社員登用日
		dto.setRetireMoneyDate(rs.getDate(COL_RETIRE_MONEY_DATE)); // 退職金起算日
		dto.setExtraDate1(rs.getDate(COL_EXTRA_DATE_1)); // 追加日付1
		dto.setExtraItem1(rs.getString(COL_EXTRA_ITEM_1)); // 追加項目1
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 検索結果取得(DTOのMap)
	 * @return Map 検索マップ
	 * @throws SQLException
	 */
	private Map<String, PmExtraDto> mappingAllMap() throws SQLException {
		Map<String, PmExtraDto> all = new HashMap<String, PmExtraDto>();
		while (rs.next()) {
			PmExtraDto dto = mapping();
			all.put(dto.getKCode(), dto);
		}
		return all;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PmExtraDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PmExtraDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return PmExtraDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmExtraDto findForKey(String kCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			PmExtraDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 全データ取得(人事情報出力用)
	 * @return Map 検索結果(DTOのMap)
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	public Map<String, PmExtraDto> findForHumanData() throws SQLException, NoSuchFieldException, IllegalAccessException {
		try {
			prepareStatement(getSelectQuery(getClass()));
			executeQuery();
			return mappingAllMap();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode 社員コード(検索キー)
	 * @return PmExtraDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PmExtraDto findForUpdate(String kCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			executeQuery();
			PmExtraDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			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(PmExtraDto 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(PmExtraDto 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(PmExtraDto 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
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PmExtraDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()); // 社員コード
		setParam(index++, dto.getGender()); // 性別
		setParam(index++, dto.getBirthDate()); // 生年月日
		setParam(index++, dto.getFormerName()); // 旧姓
		setParam(index++, dto.getPostalCode()); // 郵便番号
		setParam(index++, dto.getAddress()); // 現住所
		setParam(index++, dto.getFormerAddress()); // 旧住所
		setParam(index++, dto.getPhoneNumber()); // 電話番号
		setParam(index++, dto.getCellphoneNumber()); // 携帯番号
		setParam(index++, dto.getEmergencyNumber()); // 緊急番号
		setParam(index++, dto.getAppointmentDate()); // 正社員登用日
		setParam(index++, dto.getRetireMoneyDate()); // 退職金起算日
		setParam(index++, dto.getExtraDate1()); // 追加日付1
		setParam(index++, dto.getExtraItem1()); // 追加項目1
		setCommonParams(isInsert);
	}
	
}
