/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.workflow.dao;

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

import jp.mosp.common.common.MospConst;
import jp.mosp.kintai.common.dao.MKihonDao;
import jp.mosp.workflow.dto.AttendanceRouteDto;

/**
 * @author yoshida
 *
 */
public class AttendanceRouteDao extends PmAttendanceDao {
	
	// テーブル名及びキー列名
	/**
	 * AM_ROUTE
	 */
	public static final String	TABLE_2			= "AM_ROUTE";
	
	// 列名
	/**
	 * 
	 */
	public static final String	COL_LAYER1		= "LAYER1";	// １次承認
	/**
	 * 
	 */
	public static final String	COL_LAST_LAYER	= "LASTLAYER";	// ２次承認
																
	
	/**
	 * コンストラクタ
	 */
	public AttendanceRouteDao() {
		super();
	}
	
	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException SQL例外が発生した場合
	 */
	private List<AttendanceRouteDto> mappingAll() throws SQLException {
		List<AttendanceRouteDto> all = new ArrayList<AttendanceRouteDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)
	 * @return PmAttendanceDto pm_attendanceレコード
	 * @throws SQLException SQL例外が発生した場合
	 */
	private AttendanceRouteDto mapping() throws SQLException {
		AttendanceRouteDto dto = new AttendanceRouteDto();
		dto.setRouteId(rs.getInt(COL_ROUTEID)); // ルートID
		dto.setSCode(rs.getString(COL_SCODE)); // 所属コード
		dto.setKCode(rs.getString(COL_KCODE)); // 社員コード
		dto.setJCode(rs.getString(COL_JCODE)); // 事業所コード
		dto.setLayer1(rs.getString(COL_LAYER1)); // １次承認
		dto.setLastLayer(rs.getString(COL_LAST_LAYER)); // ２次承認
		return dto;
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param sCode		検索所属コード
	 * @param kCode		検索社員コード
	 * @param kName		検索社員名
	 * @param rtCode	検索ルートコード
	 * @return	list	検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<?> findForCondition(String sCode, String kCode, String kName, String rtCode) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
		if (!sCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_SCODE);
			sb.append(" LIKE ? ");
		}
		if (!kCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_KCODE);
			sb.append(" LIKE ? ");
		}
		if (!kName.equals("")) {
			sb.append(" AND ");
			sb.append(COL_KCODE + " IN (" + MKihonDao.getSearchKNameSubQuery() + ") ");
		}
		if (!rtCode.equals("")) {
			sb.append(" AND ");
			sb.append(TABLE + "." + COL_RTCODE);
			sb.append(" LIKE ? ");
		}
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			if (!sCode.equals("")) {
				setParam(index++, sCode + "%");
			}
			if (!kCode.equals("")) {
				setParam(index++, kCode + "%");
			}
			if (!kName.equals("")) {
				setParam(index++, "%" + kName + "%");
			}
			if (!rtCode.equals("")) {
				setParam(index++, rtCode + "%");
			}
			executeQuery();
			List<?> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param sCode		検索所属コード
	 * @param kCode		検索社員コード
	 * @param kName		検索社員名
	 * @param rtCode	検索ルートコード
	 * @param jCode 	検索事業所コード
	 * @return	list	検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<?> findForCondition(String sCode, String kCode, String kName, String rtCode, String jCode)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
		if (!sCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_SCODE);
			sb.append(" = ? ");
		}
		if (!kCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_KCODE);
			sb.append(" LIKE ? ");
		}
		if (!kName.equals("")) {
			sb.append(" AND ");
			sb.append(COL_KCODE + " IN (" + MKihonDao.getSearchKNameSubQuery() + ") ");
		}
		if (!rtCode.equals("")) {
			sb.append(" AND ");
			sb.append(TABLE + "." + COL_RTCODE);
			sb.append(" = ? ");
		}
		if (!jCode.equals("")) {
			sb.append(" AND ");
			sb.append(TABLE + "." + COL_JCODE);
			sb.append(" = ? ");
		}
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			if (!sCode.equals("")) {
				setParam(index++, sCode);
			}
			if (!kCode.equals("")) {
				setParam(index++, kCode + "%");
			}
			if (!kName.equals("")) {
				setParam(index++, "%" + kName + "%");
			}
			if (!rtCode.equals("")) {
				setParam(index++, rtCode);
			}
			if (!jCode.equals("")) {
				setParam(index++, jCode);
			}
			executeQuery();
			List<?> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kcode			検索社員コード
	 * @param startDate		検索権限開始日
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AttendanceRouteDto> findForAvailable(String kcode, String startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(" ( ");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" ) ");
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			executeQuery();
			List<AttendanceRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	@Override
	public List<?> findForAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<?> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param sCode 対象所属コード
	 * @param startDate 開始年月日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AttendanceRouteDto> findForBelongAvailable(String sCode, String startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_SCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_KCODE);
		sb.append(" = '' ");
		sb.append(" AND ");
		sb.append(" ( ");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchSubQueryByStartDate() + " ) ");
		sb.append(" ) ");
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			setParam(index++, sCode);
			setParam(index++, startDate);
			setParam(index++, startDate);
			executeQuery();
			List<AttendanceRouteDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kcode			検索社員コード
	 * @param scode			検索所属コード
	 * @param jcode			検索事業所コード
	 * @param startDate		検索権限開始日
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public List<AttendanceRouteDto> findForApproval(String kcode, String scode, String jcode, String startDate)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append("(");
		sb.append("(");
		// 所属コード
		sb.append(TABLE + "." + COL_SCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		// 事業所コード
		sb.append(TABLE + "." + COL_JCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append("(");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(")");
		sb.append(")");
		sb.append(" OR ");
		sb.append("(");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchKCodeSubQueryByStartDate() + " ) ");
		sb.append(")");
		sb.append(") ");
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			// 所属コード
			setParam(index++, scode);
			// 事業所コード
			setParam(index++, jcode);
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			setParam(index++, kcode);
			setParam(index++, kcode);
			setParam(index++, Date.valueOf(startDate));
			executeQuery();
			return mappingAll();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 条件文によるデータ取得
	 * @param kCode 対象社員コード
	 * @param startDate 開始年月日
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException 			SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public AttendanceRouteDto findForKCodeAvailable(String kCode, String startDate) throws SQLException,
			IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT ");
		sb.append(COL_ROUTEID + MospConst.DATA_SEPARATOR);
		sb.append(COL_SCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_KCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_JCODE + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAYER1 + MospConst.DATA_SEPARATOR);
		sb.append(COL_LAST_LAYER);
		sb.append(" FROM ");
		sb.append(TABLE);
		sb.append(" LEFT JOIN ");
		sb.append(TABLE_2);
		sb.append(" ON ");
		sb.append(TABLE + "." + COL_RTCODE);
		sb.append(" = ");
		sb.append(TABLE_2 + "." + COL_RTCODE);
		sb.append(" WHERE ");
		sb.append(TABLE + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(TABLE_2 + "." + COL_DELETEFLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF);
		sb.append(" AND ");
		sb.append(COL_KCODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(" ( ");
		sb.append(COL_LAYER1 + " IN ( " + AmUnitDao.getSearchSubQueryByStartDate() + " ) ");
		sb.append(" OR ");
		sb.append(COL_LAST_LAYER + " IN ( " + AmUnitDao.getSearchSubQueryByStartDate() + " ) ");
		sb.append(" ) ");
		try {
			prepareStatement(sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kCode);
			setParam(index++, startDate);
			setParam(index++, startDate);
			executeQuery();
			AttendanceRouteDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
}
