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.PayrollDao;
import jp.mosp.payroll.dto.ResidenceTaxDto;

/**
 * @author koike
 *
 */
public class ResidenceTaxDao 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_CITY_CODE			= CmCityDao.COL_CITY_CODE;
	/**
	 * 市町村名称
	 */
	public static final String	COL_CITY_NAME			= CmCityDao.COL_CITY_NAME;
	/**
	 * 納付先
	 */
	public static final String	COL_PAYMENT_CITY_CODE	= CmResidenceTaxDao.COL_PAYMENT_CITY_CODE;
	/**
	 * 納付先適用日
	 */
	public static final String	COL_RES_EXPECTED_DATE	= CmResidenceTaxDao.COL_RES_EXPECTED_DATE;
	/**
	 * 住民税
	 */
	public static final String	COL_AMOUNT				= PdPayDeductionDao.COL_AMOUNT;
	/**
	 * 
	 */
	public static final String	COL_DETAIL_CODE_PD007	= "PD007";
	

	/**
	 * コンストラクタ
	 */
	public ResidenceTaxDao() {
		super();
		INSERT_DATE = "";
		INSERT_USER = "";
		UPDATE_DATE = "";
		UPDATE_USER = "";
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException SQL例外が発生した場合
	 */
	private List<ResidenceTaxDto> mappingAll() throws SQLException {
		List<ResidenceTaxDto> all = new ArrayList<ResidenceTaxDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return CmResidenceTaxDto レコード
	 * @throws SQLException SQL例外が発生した場合
	 */
	private ResidenceTaxDto mapping() throws SQLException {
		ResidenceTaxDto dto = new ResidenceTaxDto();
		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.setResidenceTax(rs.getInt(COL_AMOUNT)); // 住民税
		dto.setCityCode(rs.getString(COL_CITY_CODE)); // 市町村コード
		dto.setCityName(rs.getString(COL_CITY_NAME)); // 市町村名称
		dto.setPaymentCityCode(rs.getString(COL_PAYMENT_CITY_CODE)); // 納付先
		return dto;
	}
	
	/**
	 * 条件文による年末調整データ取得
	 * @param calcDate 対象年月 
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException SQL例外が発生した場合
	 */
	public List<ResidenceTaxDto> findForCondition(Date calcDate) throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		setCityTax(sb);
		sb.append(" ");
		sb.append(getOrderBy());
		sb.append(" CMTAX.");
		sb.append(COL_PAYMENT_CITY_CODE);
		sb.append(" , ");
		sb.append(" PHPAY.");
		sb.append(COL_K_CODE);
		try {
			prepareStatement(sb.toString());
			//給与の場合計算月を設定
			setDateParam(index++, calcDate);
			setDateParam(index++, calcDate);
			setDateParam(index++, calcDate);
			// 実行
			executeQuery();
			List<ResidenceTaxDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	private void setCityTax(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(" PDDED.");
		sb.append(PdPayDeductionDao.COL_AMOUNT);
		sb.append(", ");
		sb.append(" CMTAX.");
		sb.append(COL_PAYMENT_CITY_CODE);
		sb.append(", ");
		sb.append(" CMCITY.");
		sb.append(COL_CITY_CODE);
		sb.append(", ");
		sb.append(" CMCITY.");
		sb.append(COL_CITY_NAME);
		sb.append(" FROM   ");
		sb.append(PhPaymentDao.TABLE + " PHPAY");
		sb.append(", ");
		sb.append(CmResidenceTaxDao.TABLE + " CMTAX");
		sb.append(", ");
		sb.append(CmCityDao.TABLE + " CMCITY");
		sb.append(", ");
		sb.append(PdPayDeductionDao.TABLE + " PDDED");
		sb.append(", ");
		sb.append(MKihonPayrollDao.TABLE + " MKIHON");
		sb.append(" WHERE ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_RES_EXPECTED_DATE);
		sb.append(" = ");
		sb.append(" ( ");
		sb.append(" SELECT ");
		sb.append(" MAX(");
		sb.append("CMTAX2.");
		sb.append(CmResidenceTaxDao.COL_RES_EXPECTED_DATE);
		sb.append(")");
		sb.append(" FROM   ");
		sb.append(CmResidenceTaxDao.TABLE + " CMTAX2");
		sb.append(" WHERE ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_K_CODE);
		sb.append(" = ");
		sb.append(" CMTAX2.");
		sb.append(CmResidenceTaxDao.COL_K_CODE);
		sb.append(" AND ");
		sb.append(" CMTAX2.");
		sb.append(CmResidenceTaxDao.COL_RES_EXPECTED_DATE);
		sb.append(" <=? ");
		sb.append(" ) ");
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(PhPaymentDao.COL_KCODE);
		sb.append(" = ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_K_CODE);
		sb.append(" AND ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_PAYMENT_CITY_CODE);
		sb.append(" = ");
		sb.append(" CMCITY.");
		sb.append(CmCityDao.COL_CITY_CODE);
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(PhPaymentDao.COL_CALC_MONTH);
		sb.append(" =? ");
		sb.append(" AND ");
		sb.append(" PDDED.");
		sb.append(PdPayDeductionDao.COL_KCODE);
		sb.append(" = ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_K_CODE);
		sb.append(" AND ");
		sb.append(" PDDED.");
		sb.append(PdPayDeductionDao.COL_CALC_DATE);
		sb.append(" =? ");
		sb.append(" AND ");
		sb.append(" PDDED.");
		sb.append(PdPayDeductionDao.COL_DETAIL_CODE);
		sb.append(" = ");
		sb.append("'" + COL_DETAIL_CODE_PD007 + "'");
		sb.append(" AND ");
		sb.append(" MKIHON.");
		sb.append(MKihonPayrollDao.COL_KCODE);
		sb.append(" = ");
		sb.append(" CMTAX.");
		sb.append(CmResidenceTaxDao.COL_K_CODE);
	}
}
