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.CmSocInsuranceDto;

public class CmSocInsuranceDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String TABLE = "CM_SOC_INSURANCE" ;
	public static final String KEY_1 = "K_CODE"           ;	// 社員コード
	public static final String KEY_2 = "INS_EXPECTED_DATE";	// 社会保険適用年月
	
	// 列名
	public static final String COL_K_CODE             = "K_CODE"            ;	// 社員コード
	public static final String COL_INS_EXPECTED_DATE  = "INS_EXPECTED_DATE" ;	// 社会保険適用年月
	public static final String COL_PENSION_TYPE       = "PENSION_TYPE"      ;	// 厚生年金種別
	public static final String COL_PARTTIME_TYPE      = "PARTTIME_TYPE"     ;	// パート区分
	public static final String COL_EMPLOYEE_TYPE      = "EMPLOYEE_TYPE"     ;	// 従業員区分
	public static final String COL_CALC_INS_TYPE      = "CALC_INS_TYPE"     ;	// 社会保険区分
	public static final String COL_NURSING_INS_TYPE   = "NURSING_INS_TYPE"  ;	// 介護保険区分
	public static final String COL_CALC_UNEMPLOY_TYPE = "CALC_UNEMPLOY_TYPE";	// 雇用保険区分
	public static final String COL_BONUS_HEALTH_TYPE  = "BONUS_HEALTH_TYPE" ;	// 健康保険賞与区分
	public static final String COL_BONUS_PENSION_TYPE = "BONUS_PENSION_TYPE";	// 厚生年金賞与区分
	public static final String COL_HEALTH_NO          = "HEALTH_NO"         ;	// 健康保険番号
	public static final String COL_HEALTH_PAYMENT     = "HEALTH_PAYMENT"    ;	// 健康保険標準報酬月額
	public static final String COL_HEALTH_GET_DATE    = "HEALTH_GET_DATE"   ;	// 健康保険資格取得日
	public static final String COL_HEALTH_LOST_TYPE   = "HEALTH_LOST_TYPE"  ;	// 健康保険資格喪失区分
	public static final String COL_HEALTH_LOST_DATE   = "HEALTH_LOST_DATE"  ;	// 健康保険資格喪失日
	public static final String COL_PENSION_NO         = "PENSION_NO"        ;	// 厚生年金整理番号
	public static final String COL_PENSION_PAYMENT    = "PENSION_PAYMENT"   ;	// 厚生年金標準報酬月額
	public static final String COL_PENSION_GET_DATE   = "PENSION_GET_DATE"  ;	// 厚生年金資格取得日
	public static final String COL_PENSION_LOST_TYPE  = "PENSION_LOST_TYPE" ;	// 厚生年金資格喪失区分
	public static final String COL_PENSION_LOST_DATE  = "PENSION_LOST_DATE" ;	// 厚生年金資格喪失日
	public static final String COL_FUND_NO            = "FUND_NO"           ;	// 厚生年金基金加入番号
	public static final String COL_FUND_GET_DATE      = "FUND_GET_DATE"     ;	// 厚生年金基金資格取得日
	public static final String COL_UNEMPLOY_NO        = "UNEMPLOY_NO"       ;	// 雇用保険番号
	public static final String COL_UNEMPLOY_GET_DATE  = "UNEMPLOY_GET_DATE" ;	// 雇用保険資格取得日
	public static final String COL_UNEMPLOY_LOST_TYPE = "UNEMPLOY_LOST_TYPE";	// 雇用保険資格喪失区分
	public static final String COL_UNEMPLOY_LOST_DATE = "UNEMPLOY_LOST_DATE";	// 雇用保険資格喪失日
	
	/**
	 * コンストラクタ
	 */
	public CmSocInsuranceDao() {
		super();
	}
	/**
	 * 検索結果取得(DTOのMap)
	 * @return Map 検索マップ
	 * @throws SQLException
	 */
	private Map<String,  CmSocInsuranceDto> mappingAllMap() throws SQLException {
		Map<String, CmSocInsuranceDto> all = new HashMap<String, CmSocInsuranceDto>();
		while (rs.next()) {
			CmSocInsuranceDto dto = mapping();
			all.put(dto.getKCode(), dto);
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmSocInsuranceDto> mappingAll() throws SQLException {
		List<CmSocInsuranceDto> all = new ArrayList<CmSocInsuranceDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CmSocInsuranceDto レコード
	 * @throws SQLException
	 */
	private CmSocInsuranceDto mapping() throws SQLException {
		CmSocInsuranceDto dto = new CmSocInsuranceDto();
		dto.setKCode           (rs.getString(COL_K_CODE            ));	// 社員コード
		dto.setInsExpectedDate (rs.getDate  (COL_INS_EXPECTED_DATE ));	// 社会保険適用年月
		dto.setPensionType     (rs.getString(COL_PENSION_TYPE      ));	// 厚生年金種別
		dto.setParttimeType    (rs.getString(COL_PARTTIME_TYPE     ));	// パート区分
		dto.setEmployeeType    (rs.getString(COL_EMPLOYEE_TYPE     ));	// 従業員区分
		dto.setCalcInsType     (rs.getString(COL_CALC_INS_TYPE     ));	// 社会保険区分
		dto.setNursingInsType  (rs.getString(COL_NURSING_INS_TYPE  ));	// 介護保険区分
		dto.setCalcUnemployType(rs.getString(COL_CALC_UNEMPLOY_TYPE));	// 雇用保険区分
		dto.setBonusHealthType (rs.getString(COL_BONUS_HEALTH_TYPE ));	// 健康保険賞与区分
		dto.setBonusPensionType(rs.getString(COL_BONUS_PENSION_TYPE));	// 厚生年金賞与区分
		dto.setHealthNo        (rs.getString(COL_HEALTH_NO         ));	// 健康保険番号
		dto.setHealthPayment   (rs.getInt   (COL_HEALTH_PAYMENT    ));	// 健康保険標準報酬月額
		dto.setHealthGetDate   (rs.getDate  (COL_HEALTH_GET_DATE   ));	// 健康保険資格取得日
		dto.setHealthLostType  (rs.getString(COL_HEALTH_LOST_TYPE  ));	// 健康保険資格喪失区分
		dto.setHealthLostDate  (rs.getDate  (COL_HEALTH_LOST_DATE  ));	// 健康保険資格喪失日
		dto.setPensionNo       (rs.getString(COL_PENSION_NO        ));	// 厚生年金整理番号
		dto.setPensionPayment  (rs.getInt   (COL_PENSION_PAYMENT   ));	// 厚生年金標準報酬月額
		dto.setPensionGetDate  (rs.getDate  (COL_PENSION_GET_DATE  ));	// 厚生年金資格取得日
		dto.setPensionLostType (rs.getString(COL_PENSION_LOST_TYPE ));	// 厚生年金資格喪失区分
		dto.setPensionLostDate (rs.getDate  (COL_PENSION_LOST_DATE ));	// 厚生年金資格喪失日
		dto.setFundNo          (rs.getString(COL_FUND_NO           ));	// 厚生年金基金加入番号
		dto.setFundGetDate     (rs.getDate  (COL_FUND_GET_DATE     ));	// 厚生年金基金資格取得日
		dto.setUnemployNo      (rs.getString(COL_UNEMPLOY_NO       ));	// 雇用保険番号
		dto.setUnemployGetDate (rs.getDate  (COL_UNEMPLOY_GET_DATE ));	// 雇用保険資格取得日
		dto.setUnemployLostType(rs.getString(COL_UNEMPLOY_LOST_TYPE));	// 雇用保険資格喪失区分
		dto.setUnemployLostDate(rs.getDate  (COL_UNEMPLOY_LOST_DATE));	// 雇用保険資格喪失日
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param insExpectedDate 社会保険適用年月(検索キー)
	 * @return Map 検索結果(DTOのMap)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public Map<String, CmSocInsuranceDto> findForHumanData(
			Date insExpectedDate) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			prepareStatement(getHistoryTableForHumanData());
			setParam(index++, insExpectedDate);
			executeQuery();
			return mappingAllMap();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	
	/**
	 * 条件文によるデータ取得(人事情報出力用)
	 * @param resExpectedDate 住民税適用年(検索キー)
	 * @param kCode 社員名(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public CmSocInsuranceDto 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();
			CmSocInsuranceDto 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<CmSocInsuranceDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmSocInsuranceDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode           社員コード(検索キー)
	 * @param insExpectedDate 社会保険適用年月(検索キー)
	 * @return CmSocInsuranceDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmSocInsuranceDto findForKey(
			String kCode,
			Date   insExpectedDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode          );
			setDateParam(index++, insExpectedDate);
			executeQuery();
			CmSocInsuranceDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return CmSocInsuranceDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public CmSocInsuranceDto findForEmployee(
			String kCode
	) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			prepareStatement(getHistoryTable());
			setParam(index++, kCode);
			executeQuery();
			CmSocInsuranceDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員による配列データ取得
	 * @param kCode 社員コード(検索キー)
	 * @return CmSocInsuranceDto 検索結果(DTO)
	 * @throws SQLException
	 */
	public List<CmSocInsuranceDto> 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<CmSocInsuranceDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ取得
	 * @param kCode           社員コード(検索キー)
	 * @param insExpectedDate 社会保険適用年月(検索キー)
	 * @return CmSocInsuranceDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public CmSocInsuranceDto findForEmployee(
			String kCode,
			Date   insExpectedDate
	) throws SQLException, IllegalAccessException {
		try {
			index = 1;
			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_INS_EXPECTED_DATE + ") AS H_EXPECTED_DATE " );
			sb.append(	"FROM " + TABLE + " ");
			sb.append(	"WHERE ");
			sb.append(	COL_K_CODE + " = ? ");
			sb.append(	"AND ");
			sb.append(	COL_INS_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_INS_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE ");
			prepareStatement(sb.toString());
			setParam(index++, kCode          );
			setParam(index++, insExpectedDate);
			executeQuery();
			CmSocInsuranceDto 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 
	 * @throws NoSuchFieldException
	 */
	public CmSocInsuranceDto 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();
			CmSocInsuranceDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode           社員コード(検索キー)
	 * @param insExpectedDate 社会保険適用年月(検索キー)
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmSocInsuranceDto findForUpdate(
			String kCode          ,
			Date   insExpectedDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass())
					+ getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode          );
			setParam(index++, insExpectedDate);
			executeQuery();
			CmSocInsuranceDto 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_INS_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_INS_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_INS_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(CmSocInsuranceDto 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(CmSocInsuranceDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode          ());
			setParam(index++, dto.getInsExpectedDate());
			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(CmSocInsuranceDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode          ());
			setParam(index++, dto.getInsExpectedDate());
			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_INS_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_INS_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 + " AS H_K_CODE, ");
		sb.append(	"MAX(" + COL_INS_EXPECTED_DATE + ") AS H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE " + COL_INS_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_INS_EXPECTED_DATE + " = HISTORY.H_EXPECTED_DATE ");
		return sb.toString();
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(CmSocInsuranceDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode           ());	// 社員コード
		setParam(index++, dto.getInsExpectedDate ());	// 社会保険適用年月
		setParam(index++, dto.getPensionType     ());	// 厚生年金種別
		setParam(index++, dto.getParttimeType    ());	// パート区分
		setParam(index++, dto.getEmployeeType    ());	// 従業員区分
		setParam(index++, dto.getCalcInsType     ());	// 社会保険区分
		setParam(index++, dto.getNursingInsType  ());	// 介護保険区分
		setParam(index++, dto.getCalcUnemployType());	// 雇用保険区分
		setParam(index++, dto.getBonusHealthType ());	// 健康保険賞与区分
		setParam(index++, dto.getBonusPensionType());	// 厚生年金賞与区分
		setParam(index++, dto.getHealthNo        ());	// 健康保険番号
		setParam(index++, dto.getHealthPayment   ());	// 健康保険標準報酬月額
		setParam(index++, dto.getHealthGetDate   ());	// 健康保険資格取得日
		setParam(index++, dto.getHealthLostType  ());	// 健康保険資格喪失区分
		setParam(index++, dto.getHealthLostDate  ());	// 健康保険資格喪失日
		setParam(index++, dto.getPensionNo       ());	// 厚生年金整理番号
		setParam(index++, dto.getPensionPayment  ());	// 厚生年金標準報酬月額
		setParam(index++, dto.getPensionGetDate  ());	// 厚生年金資格取得日
		setParam(index++, dto.getPensionLostType ());	// 厚生年金資格喪失区分
		setParam(index++, dto.getPensionLostDate ());	// 厚生年金資格喪失日
		setParam(index++, dto.getFundNo          ());	// 厚生年金基金加入番号
		setParam(index++, dto.getFundGetDate     ());	// 厚生年金基金資格取得日
		setParam(index++, dto.getUnemployNo      ());	// 雇用保険番号
		setParam(index++, dto.getUnemployGetDate ());	// 雇用保険資格取得日
		setParam(index++, dto.getUnemployLostType());	// 雇用保険資格喪失区分
		setParam(index++, dto.getUnemployLostDate());	// 雇用保険資格喪失日
		setCommonParams(isInsert);
	}
	
	/**
	 * カラム名取得
	 * @param idx
	 * @return カラム名
	 */
	private String getColumn(int idx) {
		switch (idx) {
		case 1  : return COL_PENSION_TYPE       ;// 厚生年金種別
		case 2  : return COL_PARTTIME_TYPE      ;// パート区分
		case 3  : return COL_EMPLOYEE_TYPE      ;// 従業員区分
		case 4  : return COL_CALC_INS_TYPE      ;// 社会保険区分
		case 5  : return COL_NURSING_INS_TYPE   ;// 介護保険区分
		case 6  : return COL_CALC_UNEMPLOY_TYPE ;// 雇用保険区分
		case 7  : return COL_BONUS_HEALTH_TYPE  ;// 健康保険賞与区分
		case 8  : return COL_BONUS_PENSION_TYPE ;// 厚生年金賞与区分
		case 9  : return COL_HEALTH_NO          ;// 健康保険番号
		case 10  : return COL_HEALTH_PAYMENT     ;// 健康保険標準報酬月額
		case 11  : return COL_HEALTH_GET_DATE    ;// 健康保険資格取得日
		case 12  : return COL_HEALTH_LOST_TYPE   ;// 健康保険資格喪失区分
		case 13  : return COL_HEALTH_LOST_DATE   ;// 健康保険資格喪失日
		case 14  : return COL_PENSION_NO         ;// 厚生年金整理番号
		case 15  : return COL_PENSION_PAYMENT    ;// 厚生年金標準報酬月額
		case 16  : return COL_PENSION_GET_DATE   ;// 厚生年金資格取得日
		case 17  : return COL_PENSION_LOST_TYPE  ;// 厚生年金資格喪失区分
		case 18  : return COL_PENSION_LOST_DATE  ;// 厚生年金資格喪失日
		case 19  : return COL_FUND_NO            ;// 厚生年金基金加入番号
		case 20  : return COL_FUND_GET_DATE      ;// 厚生年金基金資格取得日
		case 21  : return COL_UNEMPLOY_NO        ;// 雇用保険番号
		case 22  : return COL_UNEMPLOY_GET_DATE  ;// 雇用保険資格取得日
		case 23  : return COL_UNEMPLOY_LOST_TYPE ;// 雇用保険資格喪失区分
		case 24  : return COL_UNEMPLOY_LOST_DATE ;// 雇用保険資格喪失日

		default:
			return "";
		}
		
	}
}

