package jp.mosp.payroll.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.dto.CmMidwayEnterDto;

public class CmMidwayEnterDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE					= "CM_MIDWAY_ENTER";
	public static final String	KEY_1					= "K_CODE";			// 社員コード
																				
	// 列名
	public static final String	COL_K_CODE				= "K_CODE";			// 社員コード
	public static final String	COL_MIDWAY_TYPE			= "MIDWAY_TYPE";		// 中途入社区分
	public static final String	COL_FORMER_INCOME		= "FORMER_INCOME";		// 前勤務先収入金額
	public static final String	COL_FORMER_INSURANCE	= "FORMER_INSURANCE";	// 前勤務先社会保険額
	public static final String	COL_FORMER_TAX			= "FORMER_TAX";		// 前勤務先所得税額
																				
	
	/**
	 * コンストラクタ
	 */
	public CmMidwayEnterDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmMidwayEnterDto> mappingAll() throws SQLException {
		List<CmMidwayEnterDto> all = new ArrayList<CmMidwayEnterDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTOのMap)
	 * @return Map 検索マップ
	 * @throws SQLException
	 */
	private Map<String, CmMidwayEnterDto> mappingAllMap() throws SQLException {
		Map<String, CmMidwayEnterDto> all = new HashMap<String, CmMidwayEnterDto>();
		while (rs.next()) {
			CmMidwayEnterDto dto = mapping();
			all.put(dto.getKCode(), dto);
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CmMidwayEnterDto レコード
	 * @throws SQLException
	 */
	private CmMidwayEnterDto mapping() throws SQLException {
		CmMidwayEnterDto dto = new CmMidwayEnterDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setMidwayType(rs.getString(COL_MIDWAY_TYPE)); // 中途入社区分
		dto.setFormerIncome(rs.getInt(COL_FORMER_INCOME)); // 前勤務先収入金額
		dto.setFormerInsurance(rs.getInt(COL_FORMER_INSURANCE)); // 前勤務先社会保険額
		dto.setFormerTax(rs.getInt(COL_FORMER_TAX)); // 前勤務先所得税額
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmMidwayEnterDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmMidwayEnterDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode           社員コード(検索キー)
	 * @return CmMidwayEnterDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmMidwayEnterDto findForKey(String kCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			CmMidwayEnterDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode 社員コード(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmMidwayEnterDto findForUpdate(String kCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			executeQuery();
			CmMidwayEnterDto 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(CmMidwayEnterDto 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(CmMidwayEnterDto 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 kCode 削除対象社員コード
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 */
	public int deleteForEmployee(String kCode) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeUpdate();
			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(CmMidwayEnterDto 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();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @return Map 検索結果(DTOのMap)
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws SQLException 
	 */
	public Map<String, CmMidwayEnterDto> findForHumanData() throws NoSuchFieldException, IllegalAccessException,
			SQLException {
		try {
			prepareStatement(getSelectQuery(getClass()));
			executeQuery();
			return mappingAllMap();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CmMidwayEnterDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()); // 社員コード
		setParam(index++, dto.getMidwayType()); // 計算単価区分
		setParam(index++, dto.getFormerIncome()); // 計算単価区分
		setParam(index++, dto.getFormerInsurance()); // 計算単価区分
		setParam(index++, dto.getFormerTax()); // 計算単価区分
		setCommonParams(isInsert);
	}
}
