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.common.common.MospException;
import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.PtSupportDto;

public class PtSupportDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String	TABLE				= "UT_SUPPORT";
	public static final String	KEY_1				= "K_CODE";		// 社員コード
	public static final String	KEY_2				= "STATION_CODE";	// 支援先所属コード
	public static final String	KEY_3				= "START_DATE";	// 支援開始日
																		
	// 列名
	public static final String	COL_K_CODE			= "K_CODE";		// 社員コード
	public static final String	COL_STATION_CODE	= "STATION_CODE";	// 支援先所属コード
	public static final String	COL_START_DATE		= "START_DATE";	// 支援開始日
	public static final String	COL_END_DATE		= "END_DATE";		// 支援終了日
																		
	public static final String	ALS_BELONG_STATION	= "BELONG_STATION"; // 所属コード
																		
	
	/**
	 * コンストラクタ
	 */
	public PtSupportDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @param needExtraInfo 所属、社員名要否(true：要、false：否)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PtSupportDto> mappingAll(boolean needExtraInfo) throws SQLException {
		List<PtSupportDto> all = new ArrayList<PtSupportDto>();
		while (rs.next()) {
			PtSupportDto dto = mapping();
			if (needExtraInfo) {
				dto.setBelongStation(rs.getString(ALS_BELONG_STATION));
				dto.setKName(rs.getString(PmBasisDao.COL_K_NAME));
			}
			all.add(dto);
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PtSupportDto UtSupportレコード
	 * @throws SQLException
	 */
	private PtSupportDto mapping() throws SQLException {
		PtSupportDto dto = new PtSupportDto();
		dto.setKCode(rs.getString(COL_K_CODE)); // 社員コード
		dto.setStationCode(rs.getString(COL_STATION_CODE)); // 支援先所属コード
		dto.setStartDate(rs.getDate(COL_START_DATE)); // 支援開始日
		dto.setEndDate(rs.getDate(COL_END_DATE)); // 支援終了日
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<PtSupportDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<PtSupportDto> all = mappingAll(false);
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode       社員コード(検索キー)
	 * @param stationCode 支援先所属コード(検索キー)
	 * @param startDate   支援開始日(検索キー)
	 * @return PtSupportDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PtSupportDto findForKey(String kCode, String stationCode, Date startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			setParam(index++, stationCode);
			setParam(index++, startDate);
			executeQuery();
			PtSupportDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode       社員コード(検索キー)
	 * @param stationCode 支援先所属コード(検索キー)
	 * @param startDate   支援開始日(検索キー)
	 * @return PtSupportDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PtSupportDto findForUpdate(String kCode, String stationCode, Date startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			setParam(index++, stationCode);
			setParam(index++, startDate);
			executeQuery();
			PtSupportDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param belongStation 検索所属コード
	 * @param kCode         検索社員コード
	 * @param kName         検索社員名
	 * @param stationCode   検索支援先所属コード
	 * @param startDate     検索支援開始日
	 * @param endDate       検索支援終了日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 */
	public List<PtSupportDto> findForCondition(String belongStation, String kCode, String kName, String stationCode,
			Date startDate, Date endDate) throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append("S." + COL_K_CODE + ", ");
		sb.append("S." + COL_START_DATE + ", ");
		sb.append("S." + COL_END_DATE + ", ");
		sb.append("S." + COL_STATION_CODE + ", ");
		sb.append("B." + PmBasisDao.COL_K_NAME + ", ");
		sb.append("B." + PmBasisDao.COL_STATION_CODE + " AS " + ALS_BELONG_STATION + ", ");
		sb.append("S." + INSERT_DATE + " AS " + INSERT_DATE + ", ");
		sb.append("S." + INSERT_USER + " AS " + INSERT_USER + ", ");
		sb.append("S." + UPDATE_DATE + " AS " + UPDATE_DATE + ", ");
		sb.append("S." + UPDATE_USER + " AS " + UPDATE_USER + " ");
		sb.append("FROM ");
		sb.append(TABLE + " S,");
		sb.append(PmBasisDao.TABLE + " B ");
		sb.append("WHERE ");
		sb.append("S." + COL_K_CODE + " = B." + PmBasisDao.COL_K_CODE + " ");
		sb.append("AND ");
		sb.append("S." + COL_K_CODE + " LIKE ? ");
		sb.append("AND ");
		if (stationCode != null && !stationCode.equals("")) {
			sb.append("S." + COL_STATION_CODE + " = ? ");
			sb.append("AND ");
		}
		sb.append("S." + COL_START_DATE + " >= ? ");
		sb.append("AND ");
		sb.append("S." + COL_END_DATE + " <= ? ");
		sb.append("AND ");
		sb.append("B." + PmBasisDao.COL_K_NAME + " LIKE ? ");
		sb.append("AND ");
		sb.append("(");
		sb.append("B." + PmBasisDao.COL_STATION_CODE + " IN(");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") ");
		sb.append("OR ");
		sb.append("S." + COL_STATION_CODE + " = ? ");
		sb.append(") ");
		try {
			prepareStatement(sb.toString());
			setParam(index++, kCode + "%");
			if (stationCode != null && !stationCode.equals("")) {
				setParam(index++, stationCode);
			}
			setParam(index++, startDate);
			setParam(index++, endDate);
			setParam(index++, "%" + kName + "%");
			setParam(index++, belongStation);
			setParam(index++, belongStation);
			executeQuery();
			List<PtSupportDto> list = mappingAll(true);
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(期間重複チェック用)
	 * @param kCode     検索社員コード
	 * @param startDate 検索支援開始日
	 * @param endDate   検索支援終了日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	public List<PtSupportDto> findForTermDuplicate(String kCode, Date startDate, Date endDate) throws SQLException,
			NoSuchFieldException, IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_START_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_END_DATE + " >= ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, endDate);
			setParam(index++, startDate);
			executeQuery();
			List<PtSupportDto> list = mappingAll(false);
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(月次勤務データ用)
	 * @param kCode       検索社員コード
	 * @param stationCode 検索社員コード
	 * @param startDate   検索支援開始日
	 * @param endDate     検索支援終了日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	public List<PtSupportDto> findForStation(String kCode, String stationCode, Date startDate, Date endDate)
			throws SQLException, NoSuchFieldException, IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_STATION_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_START_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_END_DATE + " >= ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, stationCode);
			setParam(index++, endDate);
			setParam(index++, startDate);
			executeQuery();
			List<PtSupportDto> list = mappingAll(false);
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得(支援チェック用)
	 * @param kCode     検索社員コード
	 * @param date      検索支援日
	 * @return 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	public PtSupportDto findForEmployee(String kCode, Date date) throws SQLException, NoSuchFieldException,
			IllegalAccessException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_K_CODE + " = ? ");
		sb.append("AND ");
		sb.append(COL_START_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_END_DATE + " >= ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, date);
			setParam(index++, date);
			executeQuery();
			PtSupportDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(PtSupportDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate();
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ更新
	 * @param dto 更新対象DTO
	 * @return int 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(PtSupportDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getStationCode());
			setParam(index++, dto.getStartDate());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(PtSupportDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getStationCode());
			setParam(index++, dto.getStartDate());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 社員によるデータ削除
	 * @param kCode 削除対象社員コード
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 */
	public int deleteForEmployee(String kCode) throws SQLException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM " + TABLE + " ");
			sb.append("WHERE ");
			sb.append(COL_K_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @param isInsert 挿入文フラグ(true：挿入文、false：更新文)
	 * @throws SQLException
	 */
	private void setParams(PtSupportDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()); // 社員コード
		setParam(index++, dto.getStationCode()); // 支援先所属コード
		setParam(index++, dto.getStartDate()); // 支援開始日
		setParam(index++, dto.getEndDate()); // 支援終了日
		setCommonParams(isInsert);
	}
	
	/**
	 * 支援社員コード取得SQL
	 * @return 支援社員コード取得SQL文字列
	 */
	public static final String getSupportQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_K_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE + " IN (");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") AND ");
		sb.append(COL_START_DATE + " <= ? ");
		sb.append("AND ");
		sb.append(COL_END_DATE + " >= ? ");
		return sb.toString();
	}
	
	/**
	 * 支援社員コード取得SQL
	 * @return 支援社員コード取得SQL文字列
	 */
	public static final String getTermSupportQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_K_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_STATION_CODE + " IN (");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") AND ((");
		sb.append(COL_START_DATE + " >= ? ");
		sb.append("AND ");
		sb.append(COL_START_DATE + " <= ? ");
		sb.append(") OR ( ");
		sb.append(COL_END_DATE + " >= ? ");
		sb.append("AND ");
		sb.append(COL_END_DATE + " <= ? ");
		sb.append(")) ");
		return sb.toString();
	}
	
}
