package jp.mosp.payroll.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.CmAspUserDto;
import jp.mosp.common.dto.CmUserDto;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.dto.PsSlipDto;

public class PsSlipDao extends PayrollDao {
	
	/**
	 *  テーブル名。<br>
	 */
	public static final String TABLE = "PS_SLIP";
	
	/**
	 *  主キー1。<br>
	 */
	public static final String KEY_01 = "SLIP_CODE";
	
	/**
	 *  主キー2。<br>
	 */
	public static final String KEY_02 = "DETAIL_TYPE";
	
	/**
	 *  主キー3。<br>
	 */
	public static final String KEY_03 = "DETAIL_CODE";
		
	public static final String COL_SLIP_CODE   = "SLIP_CODE"  ;
	public static final String COL_DETAIL_TYPE = "DETAIL_TYPE";
	public static final String COL_DETAIL_CODE = "DETAIL_CODE";
	public static final String COL_VIEW_INDEX  = "VIEW_INDEX" ;
	
	private static final String DELETE_FLAG_COL = "DELETE_FLAG";
	
	/**
	 * {@link PayrollDao#PayrollDao(Properties, String, CmAspUserDto, CmUserDto, Connection)}
	 * を行う。<br>
	 * @param cfg     設定ファイル
	 * @param cmd     コマンド
	 * @param aspUser ASPユーザー
	 * @param user    ユーザー
	 * @param conn    DBコネクション
	 */
	public PsSlipDao(
			Properties   cfg,
			String       cmd,
			CmAspUserDto aspUser,
			CmUserDto    user,
			Connection   conn
	) {
		super(cfg, cmd, aspUser, user, conn);
	}
	
	/**
	 * 検索結果をDTOとして取得する。<br>
	 * @return レコード(DTO)
	 * @throws SQLException
	 */
	private PsSlipDto mapping() throws SQLException {
		PsSlipDto dto = new PsSlipDto();
		dto.setSlipCode  (rs.getString(COL_SLIP_CODE  ));
		dto.setDetailType(rs.getString(COL_DETAIL_TYPE));
		dto.setDetailCode(rs.getString(COL_DETAIL_CODE));
		dto.setViewIndex (rs.getByte  (COL_VIEW_INDEX ));
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 検索結果をDTOのListとして取得する。<br>
	 * @return 検索結果リスト
	 * @throws SQLException
	 */
	private List<PsSlipDto> mappingAll() throws SQLException {
		List<PsSlipDto> all = new ArrayList<PsSlipDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PsSlipDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データを挿入する。<br>
	 * @param dto     挿入対象DTO
	 * @param needLog ログ要否フラグ
	 * @return 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(
			PsSlipDto dto, boolean needLog
	) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate(needLog);
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データを更新する。<br>
	 * @param dto 更新対象DTO
	 * @param needLog ログ要否フラグ
	 * @return 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(
			PsSlipDto dto, boolean needLog
	) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getSlipCode  ());
			setParam(index++, dto.getDetailType());
			setParam(index++, dto.getDetailCode());
			executeUpdate(needLog);
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 伝票明細リスト取得用SQLを取得する。<br>
	 * @return 伝票明細リスト取得用SQL
	 */
	public static String getQueryForSlip(String table) {
		StringBuffer sb = new StringBuffer();
		sb.append("FROM " + table + ", " + TABLE + " ");
		sb.append("WHERE ");
		sb.append(table + "." + COL_DETAIL_CODE + " = ");
		sb.append(TABLE + "." + COL_DETAIL_CODE + " ");
		sb.append("AND ");
		sb.append(TABLE + "." + COL_SLIP_CODE + " = ? ");
		sb.append("AND ");
		sb.append(TABLE + "." + COL_DETAIL_TYPE + " = ? ");
		sb.append("AND ");
		sb.append(table + "." + DELETE_FLAG_COL + " = " + MospConst.DELETE_FLAG_OFF + " ");
		sb.append("ORDER BY " + TABLE + "." + COL_VIEW_INDEX + " ");
		return sb.toString();
	}
	
	/**
	 * パラメータを設定する。<br>
	 * @param dto      更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PsSlipDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getSlipCode  ());
		setParam(index++, dto.getDetailType());
		setParam(index++, dto.getDetailCode());
		setParam(index++, dto.getViewIndex ());
		setCommonParams(isInsert);
	}
	
}
