package jp.mosp.payroll.bonus.dao;

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

import jp.mosp.payroll.base.PayrollDao;
import jp.mosp.payroll.bonus.dto.PtSuperiorDto;
import jp.mosp.common.common.MospException;

public class PtSuperiorDao extends PayrollDao {
	
	// テーブル名及びキー列名
	public static final String TABLE = "UT_SUPERIOR";
	public static final String KEY_1 = "K_CODE";	//社員コード
	
	// 列名
	public static final String COL_K_CODE        = "K_CODE"       ;	// 社員コード
	public static final String COL_SUPERIOR_CODE = "SUPERIOR_CODE";	// サポーター社員コード
	
	public static final String ALS_SUPERIOR_NAME = "SUPERIOR_NAME";	// サポーター社員名

	/**
	 * コンストラクタ
	 */
	public PtSuperiorDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<PtSuperiorDto> mappingAll() throws SQLException {
		List<PtSuperiorDto> all = new ArrayList<PtSuperiorDto>();
		while (rs.next()) {
			PtSuperiorDto dto = mapping();
			dto.setStationCode (rs.getString(PmBasisDao.COL_STATION_CODE));
			dto.setKName       (rs.getString(PmBasisDao.COL_K_NAME      ));
			dto.setSuperiorName(rs.getString(ALS_SUPERIOR_NAME          ));
			all.add(dto);
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PtSuperiorDto UT_SUPERIORレコード
	 * @throws SQLException
	 */
	private PtSuperiorDto mapping() throws SQLException {
		PtSuperiorDto dto = new PtSuperiorDto();
		dto.setKCode       (rs.getString(COL_K_CODE       ));	// 社員コード
		dto.setSuperiorCode(rs.getString(COL_SUPERIOR_CODE));	// サポーター社員コード
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * キーによるデータ取得
	 * @param kCode 社員コード(検索キー)
	 * @return PtSuperiorDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PtSuperiorDto findForKey(
			String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kCode);
			executeQuery();
			PtSuperiorDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * キーによる行ロック取得
	 * @param kCode 社員コード(検索キー)
	 * @return PtSuperiorDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public PtSuperiorDto findForUpdate(
			String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kCode);
			executeQuery();
			PtSuperiorDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kCode 検索社員コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 */
	public List<PtSuperiorDto> findForCondition(
			String stationCode ,
			String kCode       ,
			String kName       ,
			String superiorCode,
			String superiorName
	) throws SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append("S." + COL_K_CODE + ", ");
		sb.append("S." + COL_SUPERIOR_CODE + ", ");
		sb.append("A." + PmBasisDao.COL_K_NAME + ", ");
		sb.append("A." + PmBasisDao.COL_STATION_CODE + ", ");
		sb.append("B." + PmBasisDao.COL_K_NAME + " AS " + ALS_SUPERIOR_NAME + ", ");
		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 + " A, ");
		sb.append(PmBasisDao.TABLE + " B ");
		sb.append("WHERE ");
		sb.append("S." + COL_K_CODE + " = A." + PmBasisDao.COL_K_CODE + " ");
		sb.append("AND ");
		sb.append("S." + COL_SUPERIOR_CODE + " = B." + PmBasisDao.COL_K_CODE + " ");
		sb.append("AND ");
		sb.append("S." + COL_K_CODE + " LIKE ? ");
		sb.append("AND ");
		sb.append("S." + COL_SUPERIOR_CODE + " LIKE ? ");
		sb.append("AND ");
		sb.append("A." + PmBasisDao.COL_STATION_CODE + " IN(");
		sb.append(PmStationDao.getStationQuery());
		sb.append(") AND ");
		sb.append("A." + PmBasisDao.COL_K_NAME + " LIKE ? ");
		sb.append("AND ");
		sb.append("B." + PmBasisDao.COL_K_NAME + " LIKE ? ");
		try {
			prepareStatement(sb.toString());
			setParam(index++, kCode + "%"             );
			setParam(index++, superiorCode + "%"      );
			setParam(index++, stationCode             );
			setParam(index++, "%" + kName + "%"       );
			setParam(index++, "%" + superiorName + "%");
			executeQuery();
			List<PtSuperiorDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(PtSuperiorDto 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(PtSuperiorDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			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(PtSuperiorDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			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 + " = ? ");
			sb.append("OR ");
			sb.append(COL_SUPERIOR_CODE + " = ? ");
			prepareStatement(sb.toString());
			setParam(index++, kCode);
			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(PtSuperiorDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode       ());	// 社員コード
		setParam(index++, dto.getSuperiorCode());	// サポーター社員コード
		setCommonParams(isInsert);
	}
	
	/**
	 * サポーター社員コード取得SQL
	 * @return サポーター社員コード取得SQL文字列
	 */
	public static final String getSuperiorQuery() {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append(COL_K_CODE + " ");
		sb.append("FROM ");
		sb.append(TABLE + " ");
		sb.append("WHERE ");
		sb.append(COL_SUPERIOR_CODE + " = ? ");
		return sb.toString();
	}

}
