package jp.mosp.payroll.bonus.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.dao.DetailMoneyDao;
import jp.mosp.payroll.dao.MKihonPayrollDao;
import jp.mosp.payroll.bonus.dto.CashKindListDto;

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(" ORDER BY ");
		sb.append(	COL_K_CODE              );
		try {
			//幚峴
			executeQuery();
			List<CashKindListDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 忦審暥偵傛傞尰嬥嬥妟僨乕僞庢摼(徿梌)
	 * @param sb SQL僶僢僼傽
	 * @throws SQLException
	 */
	private void setCashKindBonus (StringBuffer sb) {
		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);
	}
	
}
