/*
 * 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.system.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.system.dto.MYakusyokuDto;

public class MYakusyokuDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "M_YAKUSYOKU";
	public static final String KEY_1 	= "YCODE";		//役職コード
	public static final String KEY_2 	= "TOKYU";		//等級

	// 列名
	public static final String COL_YCODE  		= "YCODE";		//役職コード
	public static final String COL_YNAME  		= "YNAME";		//役職名
	public static final String COL_TOKYU  		= "TOKYU";		//等級
	public static final String COL_TKEKKIN  		= "TKEKKIN";	//特別休暇控除有無
	public static final String COL_KEKKIN  		= "KEKKIN";		//一般欠勤控除有無
	public static final String COL_SKEKKIN  		= "SKEKKIN";	//傷病欠勤控除有無
	public static final String COL_TIJI  			= "TIJI";		//遅刻早退控除有無
	public static final String COL_ZAJI  			= "ZAJI";		//普通残業手当有無
	public static final String COL_KSJI  			= "KSJI";		//休日残業手当有無
	public static final String COL_SNJI  			= "SNJI";		//深夜残業手当有無
	public static final String COL_KAIKIN  		= "KAIKIN";		//精皆勤手当有無
	public static final String COL_SYOKUJI 	 	= "SYOKUJI";	//食事手当有無
	public static final String COL_CYOKU1  		= "CYOKU1";		//1直手当有無
	public static final String COL_CYOKU2  		= "CYOKU2";		//2直手当有無
	public static final String COL_CYOKU3  		= "CYOKU3";		//3直手当有無
	public static final String COL_HOKA1  		= "HOKA1";		//その他支給1有無
	public static final String COL_HOKA2  		= "HOKA2";		//その他支給2有無
	public static final String COL_HOKA3  		= "HOKA3";		//その他支給3有無
	public static final String COL_HOKA4  		= "HOKA4";		//その他支給4有無
	public static final String COL_HOKA5  		= "HOKA5";		//その他支給5有無
	public static final String COL_TKEKKINTAN  	= "TKEKKINTAN";	//特別休暇控除単価
	public static final String COL_KEKKINTAN  	= "KEKKINTAN";	//一般欠勤控除単価
	public static final String COL_SKEKKINTAN  	= "SKEKKINTAN";	//傷病欠勤控除単価
	public static final String COL_TIJITAN  		= "TIJITAN";	//遅刻早退控除単価
	public static final String COL_ZAJITAN  		= "ZAJITAN";	//普通残業手当単価
	public static final String COL_KSJITAN  		= "KSJITAN";	//休日残業手当単価
	public static final String COL_SNJITAN  		= "SNJITAN";	//深夜残業手当単価
	public static final String COL_KAIKINTAN  	= "KAIKINTAN";	//精皆勤手当単価
	public static final String COL_SYOKUJITAN  	= "SYOKUJITAN";	//食事手当単価
	public static final String COL_CYOKU1TAN  	= "CYOKU1TAN";	//1直手当単価
	public static final String COL_CYOKU2TAN  	= "CYOKU2TAN";	//2直手当単価
	public static final String COL_CYOKU3TAN  	= "CYOKU3TAN";	//3直手当単価
	public static final String COL_HOKA1TAN  		= "HOKA1TAN";	//その他支給1単価
	public static final String COL_HOKA2TAN  		= "HOKA2TAN";	//その他支給2単価
	public static final String COL_HOKA3TAN  		= "HOKA3TAN";	//その他支給3単価
	public static final String COL_HOKA4TAN  		= "HOKA4TAN";	//その他支給4単価
	public static final String COL_HOKA5TAN  		= "HOKA5TAN";	//その他支給5単価

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return m_yakusyokuDto m_yakusyokuレコード
	 * @throws SQLException
	 */
	private MYakusyokuDto mapping() throws SQLException {
		MYakusyokuDto dto = new MYakusyokuDto();
		dto.setYCode 		(rs.getString (COL_YCODE  ));		//役職コード
		dto.setYName 		(rs.getString (COL_YNAME  ));		//役職名
		dto.setTokyu 		(rs.getString (COL_TOKYU  ));		//等級
		dto.setTKekkin 		(rs.getString (COL_TKEKKIN  ));		//特別休暇控除有無
		dto.setKekkin 		(rs.getString (COL_KEKKIN  ));		//一般欠勤控除有無
		dto.setSKekkin 		(rs.getString (COL_SKEKKIN  ));		//傷病欠勤控除有無
		dto.setTiJi 		(rs.getString (COL_TIJI  ));		//遅刻早退控除有無
		dto.setZaJi 		(rs.getString (COL_ZAJI  ));		//普通残業手当有無
		dto.setKsJi 		(rs.getString (COL_KSJI  ));		//休日残業手当有無
		dto.setSnJi 		(rs.getString (COL_SNJI  ));		//深夜残業手当有無
		dto.setKaikin 		(rs.getString (COL_KAIKIN  ));		//精皆勤手当有無
		dto.setSyokuji 		(rs.getString (COL_SYOKUJI ));		//食事手当有無
		dto.setCyoku1 		(rs.getString (COL_CYOKU1  ));		//1直手当有無
		dto.setCyoku2 		(rs.getString (COL_CYOKU2  ));		//2直手当有無
		dto.setCyoku3 		(rs.getString (COL_CYOKU3  ));		//3直手当有無
		dto.setHoka1 		(rs.getString (COL_HOKA1  ));		//その他支給1有無
		dto.setHoka2 		(rs.getString (COL_HOKA2  ));		//その他支給2有無
		dto.setHoka3 		(rs.getString (COL_HOKA3  ));		//その他支給3有無
		dto.setHoka4 		(rs.getString (COL_HOKA4  ));		//その他支給4有無
		dto.setHoka5 		(rs.getString (COL_HOKA5  ));		//その他支給5有無
		dto.setTKekkinTan 	(rs.getInt    (COL_TKEKKINTAN  ));	//特別休暇控除単価
		dto.setKekkinTan 	(rs.getInt    (COL_KEKKINTAN  ));	//一般欠勤控除単価
		dto.setSKekkinTan 	(rs.getInt    (COL_SKEKKINTAN  ));	//傷病欠勤控除単価
		dto.setTiJiTan 		(rs.getInt    (COL_TIJITAN  ));		//遅刻早退控除単価
		dto.setZaJiTan 		(rs.getInt    (COL_ZAJITAN  ));		//普通残業手当単価
		dto.setKsJiTan 		(rs.getInt    (COL_KSJITAN  ));		//休日残業手当単価
		dto.setSnJiTan 		(rs.getInt    (COL_SNJITAN  ));		//深夜残業手当単価
		dto.setKaikinTan 	(rs.getInt    (COL_KAIKINTAN  ));	//精皆勤手当単価
		dto.setSyokuJiTan 	(rs.getInt    (COL_SYOKUJITAN  ));	//食事手当単価
		dto.setCyoku1Tan 	(rs.getInt    (COL_CYOKU1TAN    ));	//1直手当単価
		dto.setCyoku2Tan 	(rs.getInt    (COL_CYOKU2TAN  ));	//2直手当単価
		dto.setCyoku3Tan 	(rs.getInt    (COL_CYOKU3TAN  ));	//3直手当単価
		dto.setHoka1Tan 	(rs.getInt    (COL_HOKA1TAN  ));	//その他支給1単価
		dto.setHoka2Tan 	(rs.getInt    (COL_HOKA2TAN  ));	//その他支給2単価
		dto.setHoka3Tan 	(rs.getInt    (COL_HOKA3TAN  ));	//その他支給3単価
		dto.setHoka4Tan 	(rs.getInt    (COL_HOKA4TAN  ));	//その他支給4単価
		dto.setHoka5Tan 	(rs.getInt    (COL_HOKA5TAN  ));	//その他支給5単価
		return dto;
	}

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

	/**
	 * 全データ取得(等級有りのみ)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MYakusyokuDto> findAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_TOKYU);
			sb.append(" > '0' ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<MYakusyokuDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 全データ取得(等級無しのみ)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MYakusyokuDto> findForTitle() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE NOT (");
			sb.append(COL_TOKYU);
			sb.append(" > '0' )");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<MYakusyokuDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param ycode 役職コード(検索キー)
	 * @param tokyu 等級(検索キー)
	 * @return m_yakusyokuDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MYakusyokuDto findForKey(
			String ycode,
			String tokyu
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, ycode);
			setParam(index++, tokyu);
			executeQuery();
			MYakusyokuDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * 条件文によるデータ取得
	 * @param ycode 検索役職コード
	 * @param tokyu 検索等級
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String ycode,
			String tokyu
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_YCODE  );
		sb.append(" = ? AND ");
		sb.append(COL_TOKYU  );
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   ycode  );
			setParam(index++,   tokyu   + "%");
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param ycode 検索役職コード
	 * @param tokyu 検索等級
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String ycode,
			String tokyu,
			String yname
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_YCODE  );
		sb.append(" LIKE ? AND ");
		sb.append(COL_TOKYU  );
		sb.append(" LIKE ? AND ");
		sb.append(COL_YNAME  );
		sb.append(" LIKE ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, 		ycode + "%" );
			setParam(index++, 		tokyu + "%" );
			setParam(index++, "%" + yname + "%" );
			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(MYakusyokuDto 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(MYakusyokuDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto,false);
			setParam(index++, dto.getYCode());
			setParam(index++, dto.getTokyu());
			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(MYakusyokuDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getYCode());
			setParam(index++, dto.getTokyu());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MYakusyokuDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getYCode());		//役職コード
		setParam(index++, dto.getYName());		//役職名
		setParam(index++, dto.getTokyu());		//等級
		setParam(index++, dto.getTKekkin());	//特別休暇控除有無
		setParam(index++, dto.getKekkin());		//一般欠勤控除有無
		setParam(index++, dto.getSKekkin());	//傷病欠勤控除有無
		setParam(index++, dto.getTiJi());		//遅刻早退控除有無
		setParam(index++, dto.getZaJi());		//普通残業手当有無
		setParam(index++, dto.getKsJi());		//休日残業手当有無
		setParam(index++, dto.getSnJi());		//深夜残業手当有無
		setParam(index++, dto.getKaikin());		//精皆勤手当有無
		setParam(index++, dto.getSyokuji());	//食事手当有無
		setParam(index++, dto.getCyoku1());		//1直手当有無
		setParam(index++, dto.getCyoku2());		//2直手当有無
		setParam(index++, dto.getCyoku3());		//3直手当有無
		setParam(index++, dto.getHoka1());		//その他支給1有無
		setParam(index++, dto.getHoka2());		//その他支給2有無
		setParam(index++, dto.getHoka3());		//その他支給3有無
		setParam(index++, dto.getHoka4());		//その他支給4有無
		setParam(index++, dto.getHoka5());		//その他支給5有無
		setParam(index++, dto.getTKekkinTan());	//特別休暇控除単価
		setParam(index++, dto.getKekkinTan());	//一般欠勤控除単価
		setParam(index++, dto.getSKekkinTan());	//傷病欠勤控除単価
		setParam(index++, dto.getTiJiTan());	//遅刻早退控除単価
		setParam(index++, dto.getZaJiTan());	//普通残業手当単価
		setParam(index++, dto.getKsJiTan());	//休日残業手当単価
		setParam(index++, dto.getSnJiTan());	//深夜残業手当単価
		setParam(index++, dto.getKaikinTan());	//精皆勤手当単価
		setParam(index++, dto.getSyokuJiTan());	//食事手当単価
		setParam(index++, dto.getCyoku1Tan());	//1直手当単価
		setParam(index++, dto.getCyoku2Tan());	//2直手当単価
		setParam(index++, dto.getCyoku3Tan());	//3直手当単価
		setParam(index++, dto.getHoka1Tan());	//その他支給1単価
		setParam(index++, dto.getHoka2Tan());	//その他支給2単価
		setParam(index++, dto.getHoka3Tan());	//その他支給3単価
		setParam(index++, dto.getHoka4Tan());	//その他支給4単価
		setParam(index++, dto.getHoka5Tan());	//その他支給5単価
		setCommonParams(isInsert);
	}

	/**
	 * 低職位コード抽出SQL取得
	 * @return 低データレベル職位コード抽出SQL文字列
	 */
	public static String getForLowerLevel() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_YCODE);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" WHERE ");
		sb.append(COL_YCODE + " >= ? ");
		return sb.toString();
	}

}
