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

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

import jp.cloudzero.mosp.exception.CloudzeroUnsupportedException;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.CashKindListDto;
import jp.mosp.payroll.dao.DetailMoneyDao;
import jp.mosp.payroll.dao.MKihonPayrollDao;

public class CashKindListDao extends PayrollDao {
	
	//	楍柤
	public static final String	COL_K_CODE				= PhBounsDao.COL_KCODE;			// 幮堳僐乕僪
	public static final String	COL_CALC_DATE			= PhBounsDao.COL_CALC_MONTH;		// 寁嶼擭寧
	public static final String	COL_PAYDAY				= PhBounsDao.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	= "BS013";							//柧嵶僐乕僪丂
																							
	
	/**
	 * 僐儞僗僩儔僋僞
	 */
	public CashKindListDao() {
		super();
		INSERT_DATE = "";
		INSERT_USER = "";
		UPDATE_DATE = "";
		UPDATE_USER = "";
	}
	
	/**
	 * 専嶕寢壥庢摼(DTO偺List)
	 * @return List 専嶕儕僗僩
	 * @throws SQLException
	 */
	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
	 */
	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 adjustYear 懳徾擭
	 * @param lastDate   懳徾寧
	 * @return List 専嶕寢壥(DTO偺List)
	 * @throws SQLException
	 */
	public List<CashKindListDto> findForCondition(Date payday) throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		setCashKindBonus(sb);
		prepareStatement(sb.toString());
		setDateParam(index++, payday);
		sb.append(" ");
		sb.append(getOrderBy());
		sb.append(COL_K_CODE);
		try {
			//幚峴
			executeQuery();
			List<CashKindListDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 忦審暥偵傛傞尰嬥嬥妟僨乕僞庢摼(徿梌)
	 * @param sb SQL僶僢僼傽
	 */
	private void setCashKindBonus(StringBuffer sb) {
		throw new CloudzeroUnsupportedException();
/*		sb.append("SELECT ");
		sb.append(" PHBON.");
		sb.append(COL_K_CODE);
		sb.append(", ");
		sb.append(" PHBON.");
		sb.append(COL_CALC_DATE);
		sb.append(", ");
		sb.append(" PHBON.");
		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(PhBounsDao.TABLE + " PHBON ");
		sb.append(", ");
		sb.append(MKihonPayrollDao.TABLE + " MKIHON ");
		sb.append(", ");
		sb.append("PD_BON_SUM" + " PDSUM ");
		
		sb.append(" WHERE ");
		sb.append(" PHBON.");
		sb.append(COL_K_CODE);
		sb.append(" = ");
		sb.append(" MKIHON.");
		sb.append(COL_K_CODE);
		sb.append(" AND ");
		sb.append(" PHBON.");
		sb.append(COL_K_CODE);
		sb.append(" = ");
		sb.append(" PDSUM.");
		sb.append(COL_K_CODE);
		sb.append(" AND ");
		sb.append(" PHBON.");
		sb.append(COL_CALC_DATE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(" PHBON.");
		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);*/
	}
	
}
