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

import jp.mosp.human.dao.MKihonDao;
import jp.mosp.human.dto.MKihonDto;

public class MKihonPayrollDao extends MKihonDao {
	
	/**
	 * 初期処理を行う。
	 */
	public MKihonPayrollDao() {
		super();
	}
	
	/**
	 * 条件文によりデータを取得する。
	 * @param tsdate 検索日付
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> find(
			int tsdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append("(");
		sb.append(COL_TSDATE + " = 0 ");
		sb.append(" OR ");
		sb.append(COL_TSDATE + " >= ? ");
		sb.append(") ");
		sb.append(" ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, tsdate);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によりデータを取得する。(入社日〜退社日間の社員)
	 * @param  startAdjDate 入社日
	 * @param  endAdjDate   退社日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKihonDto> findForSection(
		int  startAdjDate,
		int  lastDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_NYDATE + " <= ? ");
		sb.append(" AND ");
		sb.append("(");
		sb.append(COL_TSDATE + " = 0 ");
		sb.append(" OR ");
		sb.append(COL_TSDATE + " >= ? ");
		sb.append(") ");
		sb.append(" ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, startAdjDate);
			setParam(index++, lastDate);
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 履歴データSQL取得
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryTable() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append(	"SELECT ");
		sb.append(	COL_KCODE + " H_K_CODE, ");
		sb.append(	"MAX(" + COL_NYDATE + ") H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE ");
		sb.append(	COL_NYDATE + " <= ? ");
//		sb.append(	"AND ");
//		sb.append(	COL_APPLIED_DATE + " IS NOT NULL ");
		sb.append(	"GROUP BY " + COL_KCODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_KCODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_NYDATE + " <= HISTORY.H_EXPECTED_DATE");
		sb.append(") " + TABLE +" ");
		return sb.toString();
	}

	/**
	 * 年末調整対象外データ検索用
	 * @return SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getDeleteAdjustData() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_KCODE);
		sb.append(" FROM ");
		sb.append(getHistoryTable());
		sb.append("WHERE ");
		sb.append("(");
		sb.append(COL_NYDATE);
		sb.append(" > ");
		sb.append("?");
		sb.append(") ");
		sb.append(" OR (");
		sb.append(COL_NYDATE);
		sb.append(" <= ");
		sb.append("?");
		sb.append(" AND ");
		sb.append(COL_TSDATE);
		sb.append(" <= ");
		sb.append("? ) ");
		return sb.toString();
	}

	/**
	 * 条件文によるデータ取得(年末調整計算用(個人))
	 * @param kCode       社員コード
	 * @param startDate   対象年開始日
	 * @param endDate     対象年最終日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws SQLException 
	 */
	public MKihonDto findForAdjustEmpData(
			String kCode,
			int startDate,
			int lastDate
	) throws  IllegalAccessException, SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(" * ");
		sb.append("FROM ");
		sb.append(getHistoryEmpTable());
		sb.append("WHERE ");
		sb.append(COL_NYDATE + " <= ?");
		sb.append(" AND ");
		sb.append(COL_TSDATE + " >= ?");
		sb.append(" OR ");
		sb.append(COL_TSDATE + " = 0 ");
		try {
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			setParam(index++, lastDate);
			setParam(index++, lastDate);
			setParam(index++, startDate);
			executeQuery();
			MKihonDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 履歴データSQL取得(社員)
	 * @return 履歴データ取得SQL文字列
	 * @throws IllegalAccessException 
	 */
	public String getHistoryEmpTable() throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append(getSelectStatement(getClass()));
		sb.append("FROM ");
		sb.append(TABLE + ", ");
		sb.append("(");
		sb.append(	"SELECT ");
		sb.append(	COL_KCODE + " H_K_CODE, ");
		sb.append(	"MAX(" + COL_TSDATE + ") H_EXPECTED_DATE " );
		sb.append(	"FROM " + TABLE + " ");
		sb.append(	"WHERE ");
		sb.append(	COL_KCODE + " = ? ");
		sb.append(	"AND ");
		sb.append(	COL_TSDATE + " <= ? ");
//		sb.append(	"AND ");
//		sb.append(	COL_APPLIED_DATE + " IS NOT NULL ");
		sb.append(	"GROUP BY " + COL_KCODE + " ");
		sb.append(") HISTORY ");
		sb.append("WHERE ");
		sb.append(COL_KCODE + " = HISTORY.H_K_CODE ");
		sb.append("AND ");
		sb.append(COL_TSDATE + " = HISTORY.H_EXPECTED_DATE");
		sb.append(") " + TABLE +" ");
		return sb.toString();
	}

	/**
	 * 年月によるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @param expectedDate 適用年月(検索キー)
	 * @return UhBasisDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKihonDto findForDate(
			String  kCode, int expectedDate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_KCODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_NYDATE + " = (");
			sb.append("SELECT MAX(" + COL_NYDATE + ") ");
			sb.append("FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_KCODE + " = ? ");
			sb.append("AND ");
			sb.append(COL_NYDATE + " <= ? ");
//			sb.append("AND ");
//			sb.append(COL_APPLIED_DATE + " IS NOT NULL ");
			sb.append(")");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, kCode);
			setParam(index++, expectedDate);
			executeQuery();
			MKihonDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return m_kihonDto m_kihonレコード
	 * @throws SQLException
	 */
	private MKihonDto mapping() throws SQLException {
		MKihonDto dto = new MKihonDto();
		dto.setKCode		(rs.getString (COL_KCODE));		//社員コード
		dto.setSimei 		(rs.getString (COL_SIMEI));		//氏名
		dto.setFurigana 	(rs.getString (COL_FURIGANA));	//ふりがな
		dto.setSCode 		(rs.getString (COL_SCODE));		//所属コード
		dto.setYCode 		(rs.getString (COL_YCODE));		//役職コード
		dto.setTokyu 		(rs.getString (COL_TOKYU));		//等級
		dto.setJCode 		(rs.getString (COL_JCODE));		//事業所コード
		dto.setNyDate 		(rs.getInt    (COL_NYDATE));	//入社日
		dto.setTsDate 		(rs.getInt    (COL_TSDATE));	//退職日
		dto.setKnKubun 		(rs.getString (COL_KNKUBUN));	//休退職区分
		dto.setKyKubun 		(rs.getString (COL_KYKUBUN));	//給与区分
		dto.setTxKubun 		(rs.getString (COL_TXKUBUN));	//税区分
		dto.setTxKubunx 	(rs.getString (COL_TXKUBUNX));	//その他税区分
		dto.setKmKubun 		(rs.getString (COL_KMKUBUN));	//組合区分
		dto.setPtKubun 		(rs.getString (COL_PTKUBUN));	//パート区分
		dto.setShKubun 		(rs.getString (COL_SHKUBUN));	//支払区分
		dto.setSiwakeCd 	(rs.getString (COL_SIWAKECD));	//仕訳集計コード
		dto.setHaigusya 	(rs.getString (COL_HAIGUSYA));	//扶養配偶者
		dto.setFuyosu 		(rs.getInt    (COL_FUYOSU));	//扶養家族数
		dto.setYukyu 		(rs.getDouble (COL_YUKYU));		//有給残
		dto.setNenrei 		(rs.getInt    (COL_NENREI));	//年齢
		dto.setKinzoku 		(rs.getInt    (COL_KINZOKU));	//勤続年数
		dto.setKenpoNo 		(rs.getString (COL_KENPONO));	//健保番号
		dto.setKohoNo 		(rs.getString (COL_KOHONO));	//厚保番号
		dto.setKUser 		(rs.getString (COL_KUSER));		//ユーザ
		dto.setPassw 		(rs.getString (COL_PASSW));		//パスワード
		dto.setEmail 		(rs.getString (COL_EMAIL));		//email
		dto.setNinsyo 		(rs.getString (COL_NINSYO));	//認証者
		dto.setDataLvl 		(rs.getInt    (COL_DATALVL));	//データ権限レベル
		dto.setProgLevel 	(rs.getInt    (COL_PROGLEVEL));	//プログラム使用権限
		return dto;
	}

	/**
	 * 社員コード検索用サブクエリー
	 * @return 社員コード検索サブクエリー文字列
	 */
	public static String getSelectForSubQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_KCODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_KCODE + " LIKE ? ");
		sb.append("AND ");
		sb.append(COL_SIMEI + " LIKE ? ");
		sb.append("AND ");
		sb.append(COL_FURIGANA + " LIKE ? ");
		return sb.toString();
	}

	
	
}
