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

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

import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.dto.CmCoBankDto;

public class CmCoBankDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE				= "CM_CO_BANK";
	public static final String	KEY_1				= "CO_BANK_CODE";	// 会社銀行コード
	// 列名
	public static final String	COL_CO_BANK_CODE	= "CO_BANK_CODE";	// 会社銀行コード
	public static final String	COL_BANK_CODE		= "BANK_CODE";		// 銀行コード
	public static final String	COL_BRANCH_CODE		= "BRANCH_CODE";	// 支店コード
	public static final String	COL_ACCOUNT_TYPE	= "ACCOUNT_TYPE";	// 預金区分
	public static final String	COL_ACCOUNT_NO		= "ACCOUNT_NO";	// 口座番号
	public static final String	COL_DELETE_FLAG		= "DELETE_FLAG";	// 削除フラグ
																		
	
	/**
	 * コンストラクタ
	 */
	public CmCoBankDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmCoBankDto> mappingAll() throws SQLException {
		List<CmCoBankDto> all = new ArrayList<CmCoBankDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CmCoBankDto CM_CO_BANKレコード
	 * @throws SQLException
	 */
	private CmCoBankDto mapping() throws SQLException {
		CmCoBankDto dto = new CmCoBankDto();
		dto.setCoBankCode(rs.getString(COL_CO_BANK_CODE)); // 会社銀行コード
		dto.setBankCode(rs.getString(COL_BANK_CODE)); // 銀行コード
		dto.setBranchCode(rs.getString(COL_BRANCH_CODE)); // 支店コード
		dto.setAccountType(rs.getString(COL_ACCOUNT_TYPE)); // 預金区分
		dto.setAccountNo(rs.getString(COL_ACCOUNT_NO)); // 口座番号
		dto.setDeleteFlag(rs.getInt(COL_DELETE_FLAG)); // 削除フラグ
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 登録結果
	 * @throws SQLException
	 * @throws Illega|AccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmCoBankDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmCoBankDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param coBankCode 会社銀行コード(検索キー)
	 * @return 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmCoBankDto findForKey(String coBankCode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, coBankCode);
			executeQuery();
			CmCoBankDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param coBankCode 会社銀行コード(検索キー)
	 * @return 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmCoBankDto findForUpdate(String coBankCode) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, coBankCode);
			executeQuery();
			CmCoBankDto cmCoBankDto = null;
			if (rs.next()) {
				cmCoBankDto = mapping();
			}
			return cmCoBankDto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(CmCoBankDto 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 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(CmCoBankDto dto, boolean needLog) throws SQLException, IllegalAccessException,
			NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getCoBankCode());
			executeUpdate(needLog);
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 有効データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmCoBankDto> findAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ");
			sb.append(MospConst.DELETE_FLAG_OFF);
			sb.append(" ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			executeQuery();
			List<CmCoBankDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CmCoBankDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getCoBankCode()); // 会社銀行コード
		setParam(index++, dto.getBankCode()); // 銀行コード
		setParam(index++, dto.getBranchCode()); // 支店コード
		setParam(index++, dto.getAccountType()); // 預金区分
		setParam(index++, dto.getAccountNo()); // 口座番号
		setParam(index++, dto.getDeleteFlag()); // 削除フラグ
		setCommonParams(isInsert);
	}
}
