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

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

public class CdAdjustmentDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String TABLE = "CD_ADJUSTMENT";
	public static final String KEY_1 = "K_CODE"       ;	// 社員コード
	public static final String KEY_2 = "ADJUST_YEAR"  ;	// 調整年
	
	// 列名
	public static final String COL_K_CODE               = "K_CODE"              ;	// 社員コード
	public static final String COL_ADJUST_YEAR          = "ADJUST_YEAR"         ;	// 調整年
	public static final String COL_ADJUST_STATUS        = "ADJUST_STATUS"       ;	// 年末調整計算状態
	public static final String COL_GENERAL_LIFE_INS     = "GENERAL_LIFE_INS"    ;	// 一般の生命保険料
	public static final String COL_PRIVATE_PENSION      = "PRIVATE_PENSION"     ;	// 個人年金保険料
	public static final String COL_LIFE_INS_DEDUCTION   = "LIFE_INS_DEDUCTION"  ;	// 生命保険料控除額
	public static final String COL_EARTHQUAKE_INS       = "EARTHQUAKE_INS"      ;	// 地震保険料
	public static final String COL_LONG_TERM_DAMAGE_INS = "LONG_TERM_DAMAGE_INS";	// 旧長期損害保険料
	public static final String COL_DAMAGE_INS_DEDUCTION = "DAMAGE_INS_DEDUCTION";	// 損害保険料控除額
	public static final String COL_SOCIAL_INS_DEDUCTION = "SOCIAL_INS_DEDUCTION";	// 社会保険料控除額
	public static final String COL_MUTUAL_AID_DEDUCTION = "MUTUAL_AID_DEDUCTION";	// 小規模企業共済等掛金控除額
	public static final String COL_SPOUSE_SUM_INCOME    = "SPOUSE_SUM_INCOME"   ;	// 配偶者の本年中の合計所得金額の見積額
	public static final String COL_SPOUSE_SP_DEDUCTION  = "SPOUSE_SP_DEDUCTION" ;	// 配偶者特別控除額
	public static final String COL_HOUSE_LOAN_DEDUCTION = "HOUSE_LOAN_DEDUCTION";	// 住宅借入金等特別控除額
	public static final String COL_ALLOWANCE_ADJUSTMENT = "ALLOWANCE_ADJUSTMENT";	// 支給調整額
	public static final String COL_TAX_FREE_ADJUSTMENT  = "TAX_FREE_ADJUSTMENT" ;	// 非課税調整額
	public static final String COL_SOC_INS_ADJUSTMENT   = "SOC_INS_ADJUSTMENT"  ;	// 社会保険調整額
	public static final String COL_TAX_INC_ADJUSTMENT   = "TAX_INC_ADJUSTMENT"  ;	// 所得税調整額
	public static final String COL_WITHHOLD_SLIP_NOTES  = "WITHHOLD_SLIP_NOTES" ;	// 源泉徴収票摘要欄
	public static final String COL_PAYMENT_ALLOWANCE    = "PAYMENT_ALLOWANCE"   ;	// 給与、手当等金額
	public static final String COL_PAYMENT_INCOME_TAX   = "PAYMENT_INCOME_TAX"  ;	// 給与、手当等税額
	public static final String COL_BONUS_ALLOWANCE      = "BONUS_ALLOWANCE"     ;	// 賞与等金額
	public static final String COL_BONUS_INCOME_TAX     = "BONUS_INCOME_TAX"    ;	// 賞与等税額
	public static final String COL_MIDWAY_ADJUST_INCOME = "MIDWAY_ADJUST_INCOME";	// 中途、調整収入金額
	public static final String COL_MIDWAY_ADJUST_TAX    = "MIDWAY_ADJUST_TAX"   ;	// 中途、調整収入税額
	public static final String COL_AFTER_DEDUCT_INCOME  = "AFTER_DEDUCT_INCOME" ;	// 給与所得控除後の給与等の金額
	public static final String COL_PAY_INS_DEDUCTION    = "PAY_INS_DEDUCTION"   ;	// 社会保険料等給与等からの控除分
	public static final String COL_DEPENDENTS_DEDUCTION = "DEPENDENTS_DEDUCTION";	// 配偶者控除額、扶養控除額、基礎控除額及び障害者等の控除額の合計額
	public static final String COL_CALC_TAXED_INCOME    = "CALC_TAXED_INCOME"   ;	// 差引課税給与所得金額
	public static final String COL_CALC_INCOME_TAX      = "CALC_INCOME_TAX"     ;	// 算出年税額
	public static final String COL_ADJUSTED_INCOME_TAX  = "ADJUSTED_INCOME_TAX" ;	// 年調年税額
	public static final String COL_DEDUCTED_INCOME_TAX  = "DEDUCTED_INCOME_TAX" ;	// 年調定率控除額税額
	public static final String COL_FINAL_INCOME_TAX     = "FINAL_INCOME_TAX"    ;	// 年税額
	public static final String COL_DEAD_RETIREMENT      = "DEAD_RETIREMENT"     ;	// 死亡退職
	public static final String COL_SELF_DISASTER        = "SELF_DISASTER"       ;	// 災害者
	public static final String COL_FOREIGNER            = "FOREIGNER"           ;	// 外国人
	
	/**
	 * コンストラクタ
	 */
	public CdAdjustmentDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CdAdjustmentDto> mappingAll() throws SQLException {
		List<CdAdjustmentDto> all = new ArrayList<CdAdjustmentDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CdAdjustmentDto UD_PAYMENTレコード
	 * @throws SQLException
	 */
	private CdAdjustmentDto mapping() throws SQLException {
		CdAdjustmentDto dto = new CdAdjustmentDto();
		dto.setKCode              (rs.getString(COL_K_CODE              ));	// 社員コード
		dto.setAdjustYear         (rs.getInt   (COL_ADJUST_YEAR         ));	// 調整年
		dto.setAdjustStatus       (rs.getString(COL_ADJUST_STATUS       ));	// 年末調整計算状態
		dto.setGeneralLifeIns     (rs.getInt   (COL_GENERAL_LIFE_INS    ));	// 一般の生命保険料
		dto.setPrivatePension     (rs.getInt   (COL_PRIVATE_PENSION     ));	// 個人年金保険料
		dto.setLifeInsDeduction   (rs.getInt   (COL_LIFE_INS_DEDUCTION  ));	// 生命保険料控除額
		dto.setEarthquakeIns      (rs.getInt   (COL_EARTHQUAKE_INS      ));	// 地震保険料
		dto.setLongTermDamageIns  (rs.getInt   (COL_LONG_TERM_DAMAGE_INS));	// 旧長期損害保険料
		dto.setDamageInsDeduction (rs.getInt   (COL_DAMAGE_INS_DEDUCTION));	// 損害保険料控除額
		dto.setSocialInsDeduction (rs.getInt   (COL_SOCIAL_INS_DEDUCTION));	// 社会保険料控除額
		dto.setMutualAidDeduction (rs.getInt   (COL_MUTUAL_AID_DEDUCTION));	// 小規模企業共済等掛金控除額
		dto.setSpouseSumIncome    (rs.getInt   (COL_SPOUSE_SUM_INCOME   ));	// 配偶者の本年中の合計所得金額の見積額
		dto.setSpouseSpDeduction  (rs.getInt   (COL_SPOUSE_SP_DEDUCTION ));	// 配偶者特別控除額
		dto.setHouseLoanDeduction (rs.getInt   (COL_HOUSE_LOAN_DEDUCTION));	// 住宅借入金等特別控除額
		dto.setAllowanceAdjustment(rs.getInt   (COL_ALLOWANCE_ADJUSTMENT));	// 支給調整額
		dto.setTaxFreeAdjustment  (rs.getInt   (COL_TAX_FREE_ADJUSTMENT ));	// 非課税調整額
		dto.setSocInsAdjustment   (rs.getInt   (COL_SOC_INS_ADJUSTMENT  ));	// 社会保険調整額
		dto.setTaxIncAdjustment   (rs.getInt   (COL_TAX_INC_ADJUSTMENT  ));	// 所得税調整額
		dto.setWithholdSlipNotes  (rs.getString(COL_WITHHOLD_SLIP_NOTES ));	// 源泉徴収票摘要欄
		dto.setPaymentAllowance   (rs.getInt   (COL_PAYMENT_ALLOWANCE   ));	// 給与、手当等金額
		dto.setPaymentIncomeTax   (rs.getInt   (COL_PAYMENT_INCOME_TAX  ));	// 給与、手当等税額
		dto.setBonusAllowance     (rs.getInt   (COL_BONUS_ALLOWANCE     ));	// 賞与等金額
		dto.setBonusIncomeTax     (rs.getInt   (COL_BONUS_INCOME_TAX    ));	// 賞与等税額
		dto.setMidwayAdjustIncome (rs.getInt   (COL_MIDWAY_ADJUST_INCOME));	// 中途、調整収入金額
		dto.setMidwayAdjustTax    (rs.getInt   (COL_MIDWAY_ADJUST_TAX   ));	// 中途、調整収入税額
		dto.setAfterDeductIncome  (rs.getInt   (COL_AFTER_DEDUCT_INCOME ));	// 給与所得控除後の給与等の金額
		dto.setPayInsDeduction    (rs.getInt   (COL_PAY_INS_DEDUCTION   ));	// 社会保険料等給与等からの控除分
		dto.setDependentsDeduction(rs.getInt   (COL_DEPENDENTS_DEDUCTION));	// 配偶者控除額、扶養控除額、基礎控除額及び障害者等の控除額の合計額
		dto.setCalcTaxedIncome    (rs.getInt   (COL_CALC_TAXED_INCOME   ));	// 差引課税給与所得金額
		dto.setCalcIncomeTax      (rs.getInt   (COL_CALC_INCOME_TAX     ));	// 算出年税額
		dto.setAdjustedIncomeTax  (rs.getInt   (COL_ADJUSTED_INCOME_TAX ));	// 年調年税額
		dto.setDeductedIncomeTax  (rs.getInt   (COL_DEDUCTED_INCOME_TAX ));	// 年調定率控除額税額
		dto.setFinalIncomeTax     (rs.getInt   (COL_FINAL_INCOME_TAX    ));	// 年税額
		dto.setDeadRetirement     (rs.getString(COL_DEAD_RETIREMENT     )); // 死亡退職
		dto.setSelfDisaster       (rs.getString(COL_SELF_DISASTER       ));	// 災害者
		dto.setForeigner          (rs.getString(COL_FOREIGNER           ));	// 外国人
		mappingCommonInfo(dto);
		return dto;
	}

	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CdAdjustmentDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CdAdjustmentDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode      社員コード(検索キー)
	 * @param adjustYear 調整年    (検索キー)
	 * @return CdAdjustmentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CdAdjustmentDto findForKey(
			String kCode,
			int    adjustYear
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			setParam(index++, adjustYear);
			executeQuery();
			CdAdjustmentDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param kCode      社員コード(検索キー)
	 * @param adjustYear 調整年    (検索キー)
	 * @return CdAdjustmentDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CdAdjustmentDto findForUpdate(
			String kCode,
			int    adjustYear
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			setParam(index++, adjustYear);
			executeQuery();
			CdAdjustmentDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kCode              社員コード(検索キー)
	 * @param kName              社員名(検索キー)
	 * @param kKana              ﾌﾘｶﾞﾅ(検索キー)
	 * @param adjustYear         調整年(検索キー)
	 * @param adjustStatus       年末調整ステータス(検索キー)
	 * @param finalIncomeTaxFrom 年税額From
	 * @param finalIncomeTaxTo   年税額To
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CdAdjustmentDto> findForCondition(
			String kCode             ,
			String kName             ,
			String kKana             ,
			int    adjustYear        ,
			String adjustStatus      ,
			String finalIncomeTaxFrom,
			String finalIncomeTaxTo
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " IN (" + MKihonPayrollDao.getSelectForSubQuery() + ") ");
		if (adjustYear != 0) {
			sb.append("AND ");
			sb.append(COL_ADJUST_YEAR  + " = ? ");
		}
		if (adjustStatus != null && !adjustStatus.equals("")) {
			sb.append("AND ");
			sb.append(COL_ADJUST_STATUS  + " = ? ");
		}
		if (finalIncomeTaxFrom != null && !finalIncomeTaxFrom.equals("")) {
			sb.append("AND ");
			sb.append(COL_FINAL_INCOME_TAX  + " >= ? ");
		}
		if (finalIncomeTaxTo != null && !finalIncomeTaxTo.equals("")) {
			sb.append("AND ");
			sb.append(COL_FINAL_INCOME_TAX  + " <= ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,       kCode + "%");
			setParam(index++, "%" + kName + "%");
			setParam(index++, "%" + kKana + "%");
			if (adjustYear != 0) {
				setParam(index++, adjustYear);
			}
			if (adjustStatus != null && !adjustStatus.equals("")) {
				setParam(index++, adjustStatus);
			}
			if (finalIncomeTaxFrom != null && !finalIncomeTaxFrom.equals("")) {
				setParam(index++, Integer.parseInt(finalIncomeTaxFrom));
			}
			if (finalIncomeTaxTo != null && !finalIncomeTaxTo.equals("")) {
				setParam(index++, Integer.parseInt(finalIncomeTaxTo));
			}
			executeQuery();
			List<CdAdjustmentDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 調整年取得
	 * @param adjustYear 調整年(検索キー)
	 * @return 調整年
	 * @throws SQLException 
	 */
	public List<Integer> findAdjustYear() throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT DISTINCT " + COL_ADJUST_YEAR + " ");
		sb.append("FROM " + TABLE + " ");
		sb.append("ORDER BY ");
		sb.append(COL_ADJUST_YEAR + " ");
		try {
			prepareStatement(sb.toString());
			executeQuery();
			List<Integer> list = new ArrayList<Integer>();
			while (rs.next()) {
				list.add(new Integer(rs.getInt(COL_ADJUST_YEAR)));
			}
			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(CdAdjustmentDto 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
	 * @param needLog ログ出力要否
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(CdAdjustmentDto dto, boolean needLog) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate(needLog);
			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(CdAdjustmentDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getAdjustYear());
			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(CdAdjustmentDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getAdjustYear());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 調整年のデータ削除
	 * @param adjustYear 調整年
	 * @param startDate　開始年月日
	 * @param lastDate　終了年月日
	 * @return　削除結果(件数)
	 * @throws SQLException
	 * @throws MospException
	 * @throws IllegalAccessException
	 */
	public int deleteAdjustYear(
			int   adjustYear,
			Date  startDate,
			Date  lastDate
	) throws SQLException, IllegalAccessException {
		try {
			MKihonPayrollDao mKihonPayrollDao = new MKihonPayrollDao();
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE ");
			sb.append("FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_ADJUST_YEAR + " ");
			sb.append("= ? ");
			sb.append(" AND ");
			sb.append(COL_K_CODE + " ");
			sb.append(" IN ");
			sb.append(" ( ");
			sb.append(mKihonPayrollDao.getDeleteAdjustData());
			sb.append(" ) ");
			prepareStatement(sb.toString());
			setParam(index++, adjustYear);
			setParam(index++, lastDate);
			setParam(index++, lastDate);
			setParam(index++, lastDate);
			setParam(index++, startDate);
			executeUpdate();
			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();
		}
	}
	
	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getTableForDate() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_ADJUST_YEAR + " = ? ");
		sb.append(") " + TABLE +" ");
		return sb.toString();
	}
	
	/**
	 * 条件文によりデータを取得する。<br>
	 * 指定年における確定済みデータを抽出する。<br>
	 * kCodeをnullにすると、社員コードによる指定を行わない。<br>
	 * 源泉徴収データ出力時に用いる。
	 * @param adjustYear 検索計算年月
	 * @param kCode    検索社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public  List<CdAdjustmentDto> findForWithholdVote(
			int adjustYear, String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		if (kCode != null) {
			sb.append(COL_K_CODE + " = ? ");
			sb.append("AND ");
		}
		sb.append(COL_ADJUST_YEAR + " = ? ");
		sb.append("AND ");
		sb.append(COL_ADJUST_STATUS + " IN ('" + PayrollConst.ADJU_STATUS_FIX + "','" + PayrollConst.ADJU_STATUS_OOT +"')");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			if (kCode != null) {
				setParam(index++, kCode);
			}
			setParam(index++, adjustYear);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CdAdjustmentDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode              ());	// 社員コード
		setParam(index++, dto.getAdjustYear         ());	// 調整年
		setParam(index++, dto.getAdjustStatus       ());	// 年末調整計算状態
		setParam(index++, dto.getGeneralLifeIns     ());	// 一般の生命保険料
		setParam(index++, dto.getPrivatePension     ());	// 個人年金保険料
		setParam(index++, dto.getLifeInsDeduction   ());	// 生命保険料控除額
		setParam(index++, dto.getEarthquakeIns      ());	// 地震保険料
		setParam(index++, dto.getLongTermDamageIns  ());	// 旧長期損害保険料
		setParam(index++, dto.getDamageInsDeduction ());	// 損害保険料控除額
		setParam(index++, dto.getSocialInsDeduction ());	// 社会保険料控除額
		setParam(index++, dto.getMutualAidDeduction ());	// 小規模企業共済等掛金控除額
		setParam(index++, dto.getSpouseSumIncome    ());	// 配偶者の本年中の合計所得金額の見積額
		setParam(index++, dto.getSpouseSpDeduction  ());	// 配偶者特別控除額
		setParam(index++, dto.getHouseLoanDeduction ());	// 住宅借入金等特別控除額
		setParam(index++, dto.getAllowanceAdjustment());	// 支給調整額
		setParam(index++, dto.getTaxFreeAdjustment  ());	// 非課税調整額
		setParam(index++, dto.getSocInsAdjustment   ());	// 社会保険調整額
		setParam(index++, dto.getTaxIncAdjustment   ());	// 所得税調整額
		setParam(index++, dto.getWithholdSlipNotes  ());	// 源泉徴収票摘要欄
		setParam(index++, dto.getPaymentAllowance   ());	// 給与、手当等金額
		setParam(index++, dto.getPaymentIncomeTax   ());	// 給与、手当等税額
		setParam(index++, dto.getBonusAllowance     ());	// 賞与等金額
		setParam(index++, dto.getBonusIncomeTax     ());	// 賞与等税額
		setParam(index++, dto.getMidwayAdjustIncome ());	// 中途、調整収入金額
		setParam(index++, dto.getMidwayAdjustTax    ());	// 中途、調整収入税額
		setParam(index++, dto.getAfterDeductIncome  ());	// 給与所得控除後の給与等の金額
		setParam(index++, dto.getPayInsDeduction    ());	// 社会保険料等給与等からの控除分
		setParam(index++, dto.getDependentsDeduction());	// 配偶者控除額、扶養控除額、基礎控除額及び障害者等の控除額の合計額
		setParam(index++, dto.getCalcTaxedIncome    ());	// 差引課税給与所得金額
		setParam(index++, dto.getCalcIncomeTax      ());	// 算出年税額
		setParam(index++, dto.getAdjustedIncomeTax  ());	// 年調年税額
		setParam(index++, dto.getDeductedIncomeTax  ());	// 年調定率控除額税額
		setParam(index++, dto.getFinalIncomeTax     ());	// 年税額
		setParam(index++, dto.getDeadRetirement     ());	// 死亡退職
		setParam(index++, dto.getSelfDisaster       ());	// 災害者
		setParam(index++, dto.getForeigner          ());	// 外国人
		
		setCommonParams(isInsert);
	}

}
