/*
 * 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.Date;
import java.util.List;

import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.dto.CashKindListDto;

/**
 * @author koike
 *
 */
public class CashKindListDao extends PayrollDao {
	
	//列名
	/**
	 * 社員コード
	 */
	public static final String	COL_K_CODE				= PhPaymentDao.COL_KCODE;
	/**
	 * 計算年月
	 */
	public static final String	COL_CALC_DATE			= PhPaymentDao.COL_CALC_MONTH;
	/**
	 * 給与支給日
	 */
	public static final String	COL_PAYDAY				= PhPaymentDao.COL_PAY_DATE;
	/**
	 * 社員氏名
	 */
	public static final String	COL_K_NAME				= MKihonPayrollDao.COL_SIMEI;
	/**
	 * 明細コード
	 */
	public static final String	COL_DETAIL_CODE			= DetailMoneyDao.COL_DETAIL_CODE;
	/**
	 * 明細金額
	 */
	public static final String	COL_AMOUNT				= DetailMoneyDao.COL_AMOUNT;
	/**
	 * 明細コード
	 */
	public static final String	COL_DETAIL_CODE_PS013	= "PS013";
	

	/**
	 * コンストラクタ
	 */
	public CashKindListDao() {
		super();
		INSERT_DATE = "";
		INSERT_USER = "";
		UPDATE_DATE = "";
		UPDATE_USER = "";
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException SQL例外が発生した場合
	 */
	private List<CashKindListDto> mappingAll() throws SQLException {
		List<CashKindListDto> all = new ArrayList<CashKindListDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return  CashKindListDto 現金金額レコード
	 * @throws SQLException SQL例外が発生した場合
	 */
	private CashKindListDto mapping() throws SQLException {
		CashKindListDto dto = new CashKindListDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setCalcDate(rs.getDate(COL_CALC_DATE)); // 計算年月
		dto.setPayday(rs.getDate(COL_PAYDAY)); // 給与支給日
		dto.setKName(rs.getString(COL_K_NAME)); // 社員氏名
		dto.setCashAmount(rs.getInt(COL_AMOUNT)); // 現金支給額
		return dto;
	}
	
	/**
	 * 条件文による現金金額データ取得
	 * @param calcDate 対象年月
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException SQL例外が発生した場合
	 */
	public List<CashKindListDto> findForCondition(Date calcDate) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			setCashKindPayment(sb);
			prepareStatement(sb.toString());
			setDateParam(index++, calcDate);
			sb.append(" ");
			sb.append(getOrderBy());
			sb.append(COL_K_CODE);
			//実行
			executeQuery();
			List<CashKindListDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文による現金金額データ取得(給与)
	 * @param sb SQLバッファ
	 */
	private void setCashKindPayment(StringBuffer sb) {
		sb.append("SELECT ");
		sb.append(" PHPAY.");
		sb.append(COL_K_CODE);
		sb.append(", ");
		sb.append(" PHPAY.");
		sb.append(COL_CALC_DATE);
		sb.append(", ");
		sb.append(" PHPAY.");
		sb.append(COL_PAYDAY);
		sb.append(", ");
		sb.append(" MKIHON.");
		sb.append(COL_K_NAME);
		sb.append(", ");
		sb.append(" PDSUM.");
		sb.append(COL_AMOUNT);
		
		sb.append(" FROM ");
		sb.append(PhPaymentDao.TABLE + " PHPAY ");
		sb.append(", ");
		sb.append(MKihonPayrollDao.TABLE + " MKIHON ");
		sb.append(", ");
		sb.append("PD_PAY_SUM" + " PDSUM ");
		
		sb.append(" WHERE ");
		sb.append(" PHPAY.");
		sb.append(COL_K_CODE);
		sb.append(" = ");
		sb.append(" MKIHON.");
		sb.append(COL_K_CODE);
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(COL_K_CODE);
		sb.append(" = ");
		sb.append(" PDSUM.");
		sb.append(COL_K_CODE);
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(COL_CALC_DATE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(COL_CALC_DATE);
		sb.append(" = ");
		sb.append(" PDSUM.");
		sb.append("CALC_DATE");
		sb.append(" AND ");
		sb.append(" PDSUM.");
		sb.append(COL_DETAIL_CODE);
		sb.append(" = ");
		sb.append("'" + COL_DETAIL_CODE_PS013 + "'");
		sb.append(" AND ");
		sb.append(" PDSUM.");
		sb.append(COL_AMOUNT);
		sb.append(" <> ");
		sb.append(PayrollConst.AMOUNT_ZERO);
	}
	
}
