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

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.dto.CmResidenceTaxDto;

public class CmResidenceTaxDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String TABLE = "CM_RESIDENCE_TAX"  ;
	public static final String KEY_1 = "K_CODE"          ;	// 社員コード
	public static final String KEY_2 = "RES_EXPECTED_DATE";	// 住民税適用年
	
	// 列名
	public static final String COL_K_CODE            = "K_CODE"           ;	// 社員コード
	public static final String COL_RES_EXPECTED_DATE = "RES_EXPECTED_DATE";	// 住民税適用年
	public static final String COL_PAYMENT_CITY_CODE = "PAYMENT_CITY_CODE";	// 納付先市町村コード
	public static final String COL_SUBMIT_CITY_CODE  = "SUBMIT_CITY_CODE" ;	// 提出先市町村コード
	public static final String COL_JUNE_AMOUNT       = "JUNE_AMOUNT"      ;	// 初回6月分
	public static final String COL_JULY_AMOUNT       = "JULY_AMOUNT"      ;	// 7月分
	public static final String COL_AUGUST_AMOUNT     = "AUGUST_AMOUNT"    ;	// 8月分
	public static final String COL_SEPTEMBER_AMOUNT  = "SEPTEMBER_AMOUNT" ;	// 9月分
	public static final String COL_OCTOBER_AMOUNT    = "OCTOBER_AMOUNT"   ;	// 10月分
	public static final String COL_NOVEMBER_AMOUNT   = "NOVEMBER_AMOUNT"  ;	// 11月分
	public static final String COL_DECEMBER_AMOUNT   = "DECEMBER_AMOUNT"  ;	// 12月分
	public static final String COL_JANUARY_AMOUNT    = "JANUARY_AMOUNT"   ;	// 1月分
	public static final String COL_FEBRUARY_AMOUNT   = "FEBRUARY_AMOUNT"  ;	// 2月分
	public static final String COL_MARCH_AMOUNT      = "MARCH_AMOUNT"     ;	// 3月分
	public static final String COL_APRIL_AMOUNT      = "APRIL_AMOUNT"     ;	// 4月分
	public static final String COL_MAY_AMOUNT        = "MAY_AMOUNT"       ;	// 5月分
	
	/**
	 * コンストラクタ
	 */
	public CmResidenceTaxDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmResidenceTaxDto> mappingAll() throws SQLException {
		List<CmResidenceTaxDto> all = new ArrayList<CmResidenceTaxDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CmResidenceTaxDto レコード
	 * @throws SQLException
	 */
	private CmResidenceTaxDto mapping() throws SQLException {
		CmResidenceTaxDto dto = new CmResidenceTaxDto();
		dto.setKCode          (rs.getString(COL_K_CODE           ));	// 社員コード
		dto.setResExpectedDate(rs.getDate  (COL_RES_EXPECTED_DATE));	// 住民税適用年
		dto.setPaymentCityCode(rs.getString(COL_PAYMENT_CITY_CODE));	// 納付先市町村コード
		dto.setSubmitCityCode (rs.getString(COL_SUBMIT_CITY_CODE ));	// 提出先市町村コード
		dto.setJuneAmount     (rs.getInt   (COL_JUNE_AMOUNT      ));	// 初回6月分
		dto.setJulyAmount     (rs.getInt   (COL_JULY_AMOUNT      ));	// 7月分
		dto.setAugustAmount   (rs.getInt   (COL_AUGUST_AMOUNT    ));	// 8月分
		dto.setSeptemberAmount(rs.getInt   (COL_SEPTEMBER_AMOUNT ));	// 9月分
		dto.setOctoberAmount  (rs.getInt   (COL_OCTOBER_AMOUNT   ));	// 10月分
		dto.setNovemberAmount (rs.getInt   (COL_NOVEMBER_AMOUNT  ));	// 11月分
		dto.setDecemberAmount (rs.getInt   (COL_DECEMBER_AMOUNT  ));	// 12月分
		dto.setJanuaryAmount  (rs.getInt   (COL_JANUARY_AMOUNT   ));	// 1月分
		dto.setFebruaryAmount (rs.getInt   (COL_FEBRUARY_AMOUNT  ));	// 2月分
		dto.setMarchAmount    (rs.getInt   (COL_MARCH_AMOUNT     ));	// 3月分
		dto.setAprilAmount    (rs.getInt   (COL_APRIL_AMOUNT     ));	// 4月分
		dto.setMayAmount      (rs.getInt   (COL_MAY_AMOUNT       ));	// 5月分
		mappingCommonInfo(dto);
		return dto;
	}
	/**
	 * 検索結果取得(DTOのMap)
	 * @return Map 検索マップ
	 * @throws SQLException
	 */
	private Map<String,  CmResidenceTaxDto> mappingAllMap() throws SQLException {
		Map<String, CmResidenceTaxDto> all = new HashMap<String, CmResidenceTaxDto>();
		while (rs.next()) {
			CmResidenceTaxDto dto = mapping();
			all.put(dto.getKCode(), dto);
		}
		return all;
	}
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param resExpectedDate 住民税適用年(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public Map<String, CmResidenceTaxDto> findForHumanData(
			Date resExpectedDate) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			prepareStatement(getHistoryTableForHumanData());
			setParam(index++, resExpectedDate);
			executeQuery();
			return mappingAllMap();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param resExpectedDate 住民税適用年(検索キー)
	 * @param kCode 社員名(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public CmResidenceTaxDto findForHumanDataOnly(
			String kCode, Date resExpectedDate) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(" AND ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(getHistoryTableForHumanData() + sb.toString());
			setParam(index++, resExpectedDate);
			setParam(index++, kCode);
			executeQuery();
			CmResidenceTaxDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ取得
	 * @param kCode           社員コード(検索キー)
	 * @param resExpectedDate 適用年月  (検索キー)
	 * @return CmIncomeTaxDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public CmResidenceTaxDto findForEmployeeDate(
			String kCode,
			Date   resExpectedDate
	) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(getSelectStatement(getClass()) + " ");
			sb.append("FROM ");
			sb.append(getHistoryTableForDate() + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, resExpectedDate);
			setParam(index++, kCode);
			executeQuery();
			CmResidenceTaxDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmResidenceTaxDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmResidenceTaxDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode           社員コード(検索キー)
	 * @param resExpectedDate 住民税適用年(検索キー)
	 * @return CmResidenceTaxDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmResidenceTaxDto findForKey(
			String kCode,
			Date   resExpectedDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode        );
			setParam(index++, resExpectedDate);
			executeQuery();
			CmResidenceTaxDto 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
	 */
	public CmResidenceTaxDto findForEmployee(
			String kCode
	) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			prepareStatement(getHistoryTable());
			setParam(index++, kCode);
			executeQuery();
			CmResidenceTaxDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	/**
	 * 社員による配列データ取得
	 * @param kCode 社員コード(検索キー)
	 * @return CmResidenceTaxDto 検索結果(DTO)
	 * @throws SQLException
	 */
	public List<CmResidenceTaxDto> findForEmployeeList(
			String kCode
	) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT * FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeQuery();
			List<CmResidenceTaxDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員による行ロック取得
	 * @param kCode 社員コード(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException 
	 */
	public CmResidenceTaxDto findForUpdate(
			String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getForUpdate());
			setParam(index++, kCode);
			executeQuery();
			CmResidenceTaxDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode           社員コード(検索キー)
	 * @param resExpectedDate 住民税適用年(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmResidenceTaxDto findForUpdate(
			String kCode          ,
			Date   resExpectedDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass())
					+ getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode          );
			setParam(index++, resExpectedDate);
			executeQuery();
			CmResidenceTaxDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入(アップロード)
	 * @param aryHeaderNo 挿入対象ヘッダー
	 * @param aryCsvData  挿入対象データ
	 * @param kCode       社員コード
	 * @param calcDate    適用年月
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 */
	public int insertUpload(
			String[] aryHeaderNo,
			String[] aryCsvData,
			String   kCode,
			Date     calcDate
	) throws SQLException {
		try {
			Date date = new Date();
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("INSERT INTO ");
			sb.append(TABLE);
			sb.append(" ( ");
			sb.append(COL_K_CODE);
			sb.append(", ");
			sb.append(createCol(aryHeaderNo, true));
			sb.append(COL_RES_EXPECTED_DATE);
			sb.append(", ");
			if (!INSERT_DATE.equals("")) {
				sb.append(INSERT_DATE + ", ");
			}
			if (!INSERT_USER.equals("")) {
				sb.append(INSERT_USER + ", ");
			}
			if (!UPDATE_DATE.equals("")) {
				sb.append(UPDATE_DATE + ", ");
			}
			if (!UPDATE_USER.equals("")) {
				sb.append(UPDATE_USER + " ");
			}
			sb.append(" ) VALUES ( ");
			sb.append("?, ");
			sb.append(insertRow(aryCsvData));
			sb.append("?, ");
			if (!INSERT_DATE.equals("")) {
				sb.append("?, ");
			}
			if (!INSERT_USER.equals("")) {
				sb.append("?, ");
			}
			if (!UPDATE_DATE.equals("")) {
				sb.append("?, ");
			}
			if (!UPDATE_USER.equals("")) {
				sb.append("?  ");
			}
			sb.append(" ) ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			for (int i = 0; i < aryCsvData.length; i++) {
				setParam(index++, aryCsvData[i]);
			}
			setParam(index++, calcDate);
			if (!INSERT_DATE.equals("")) {
				setParam(index++, date);
			}
			if (!INSERT_USER.equals("")) {
				setParam(index++, userId);
			}
			if (!UPDATE_DATE.equals("")) {
				setParam(index++, date);
			}
			if (!UPDATE_USER.equals("")) {
				setParam(index++, userId);
			}
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 選択カラムのみSQL作成
	 * @param aryHeaderNo
	 * @param isInsert
	 * @return 選択カラムのみSQL
	 */
	public String createCol(
			String[] aryHeaderNo,
			boolean isInsert
		) {
			index = 1;
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < aryHeaderNo.length; i++) {
				sb.append(getColumn(Integer.parseInt(aryHeaderNo[i])));
				if (!isInsert) {
					sb.append(" = ? ");
				}
				sb.append(PayrollConst.STR_CSV_SEPARATOR);
			}
			return sb.toString();
	}
	
	/**
	 * 登録パラメータ生成
	 * @param aryCsvData
	 * @return 登録パラメータ
	 */
	public String insertRow(
			String[] aryCsvData
		) {
			index = 1;
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < aryCsvData.length; i++) {
				sb.append(" ? ");
				sb.append(PayrollConst.STR_CSV_SEPARATOR);
			}
			return sb.toString();
		
	}
	
	/**
	 * データ更新(アップロード)
	 * @param aryHeaderNo 更新対象ヘッダー
	 * @param aryCsvData  更新対象データ
	 * @param kCode       社員コード
	 * @param calcDate    適用年月
	 * @return int 更新結果(件数)
	 * @throws SQLException
	 */
	public int updateUpload(
			String[] aryHeaderNo,
			String[] aryCsvData,
			String   kCode,
			Date   calcDate
	) throws SQLException {
		try {
			Date date = new Date();
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("UPDATE ");
			sb.append(TABLE);
			sb.append(" SET ");
			sb.append(createCol(aryHeaderNo,false));
			sb.append(COL_RES_EXPECTED_DATE + " = ?, ");
			if (!UPDATE_DATE.equals("")) {
				sb.append(UPDATE_DATE + " = ?, ");
			}
			if (!UPDATE_USER.equals("")) {
				sb.append(UPDATE_USER + " = ? ");
			}
			sb.append(" WHERE ");
			sb.append(COL_K_CODE);
			sb.append(" = ? ");
			sb.append(" AND ");
			sb.append(COL_RES_EXPECTED_DATE);
			sb.append(" = ? ");
			prepareStatement(sb.toString());
			for (int i = 0; i < aryCsvData.length; i++) {
				setParam(index++, aryCsvData[i]);
			}
			setParam(index++, calcDate);
			if (!UPDATE_DATE.equals("")) {
				setParam(index++, date);
			}
			if (!UPDATE_USER.equals("")) {
				setParam(index++, userId);
			}
			setParam(index++, kCode);
			setParam(index++, calcDate);
			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 insert(CmResidenceTaxDto 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(CmResidenceTaxDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode          ());
			setParam(index++, dto.getResExpectedDate());
			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(CmResidenceTaxDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode          ());
			setParam(index++, dto.getResExpectedDate());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	private String getHistoryTable() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append(	"SELECT ");
		sb.append(	COL_K_CODE + " AS H_K_CODE, ");
		sb.append(	"MAX(" + COL_RES_EXPECTED_DATE + ") AS H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE ");
		sb.append(	COL_K_CODE + " = ? ");
		sb.append(	"GROUP BY " + COL_K_CODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_RES_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE ");
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	private String getHistoryTableForHumanData() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append(	"SELECT ");
		sb.append(	COL_K_CODE + " H_K_CODE, ");
		sb.append(	"MAX(" + COL_RES_EXPECTED_DATE + ") H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE " + COL_RES_EXPECTED_DATE + " <= ? ");
		sb.append(	"GROUP BY " + COL_K_CODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_RES_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE ");
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得(住民税FBデータ用)
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	private String getHistoryForFirmBanking() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append(	"SELECT ");
		sb.append(	COL_K_CODE + " H_K_CODE, ");
		sb.append(	"MAX(" + COL_RES_EXPECTED_DATE + ") H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE " + COL_RES_EXPECTED_DATE + " <= ? ");
		sb.append(	"AND " + COL_PAYMENT_CITY_CODE + " = ? ");
		sb.append(	"GROUP BY " + COL_K_CODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_RES_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE ");
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryTableForDate() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getHistoryTableForHumanData());
		sb.append(") " + TABLE +" ");
		return sb.toString();
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryTableForFB() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getHistoryForFirmBanking());
		sb.append(") " + " ");
		return sb.toString();
	}
	

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CmResidenceTaxDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode          ());	// 社員コード
		setParam(index++, dto.getResExpectedDate());	// 住民税適用年
		setParam(index++, dto.getPaymentCityCode());	// 納付先市町村コード
		setParam(index++, dto.getSubmitCityCode ());	// 提出先市町村コード
		setParam(index++, dto.getJuneAmount     ());	// 初回6月分
		setParam(index++, dto.getJulyAmount     ());	// 7月分
		setParam(index++, dto.getAugustAmount   ());	// 8月分
		setParam(index++, dto.getSeptemberAmount());	// 9月分
		setParam(index++, dto.getOctoberAmount  ());	// 10月分
		setParam(index++, dto.getNovemberAmount ());	// 11月分
		setParam(index++, dto.getDecemberAmount ());	// 12月分
		setParam(index++, dto.getJanuaryAmount  ());	// 1月分
		setParam(index++, dto.getFebruaryAmount ());	// 2月分
		setParam(index++, dto.getMarchAmount    ());	// 3月分
		setParam(index++, dto.getAprilAmount    ());	// 4月分
		setParam(index++, dto.getMayAmount      ());	// 5月分
		setCommonParams(isInsert);
	}
	
	/**
	 * カラム名取得
	 * @param idx
	 * @return カラム名
	 */
	public String getColumn(
			int idx
		) {
		switch (idx) {
		case 25 : return COL_PAYMENT_CITY_CODE;	// 納付先市町村コード
		case 26 : return COL_SUBMIT_CITY_CODE ;	// 提出先市町村コード
		case 27 : return COL_JUNE_AMOUNT      ;	// 初回6月分
		case 28 : return COL_JULY_AMOUNT      ;	// 7月分
		case 29 : return COL_AUGUST_AMOUNT    ;	// 8月分
		case 30 : return COL_SEPTEMBER_AMOUNT ;	// 9月分
		case 31 : return COL_OCTOBER_AMOUNT   ;	// 10月分
		case 32 : return COL_NOVEMBER_AMOUNT  ;	// 11月分
		case 33 : return COL_DECEMBER_AMOUNT  ;	// 12月分
		case 34 : return COL_JANUARY_AMOUNT   ;	// 1月分
		case 35 : return COL_FEBRUARY_AMOUNT  ;	// 2月分
		case 36 : return COL_MARCH_AMOUNT     ;	// 3月分
		case 37 : return COL_APRIL_AMOUNT     ;	// 4月分
		case 38 : return COL_MAY_AMOUNT       ;	// 5月分
		default:
			return "";
		}
	}
}

