package jp.mosp.common.dao;

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

import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.CmAuthorityDto;

public class CmAuthorityDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE  = "CM_AUTHORITY"	;
	public static final String KEY_1 	= "AUTHCODE"		;	//権限コード

	// 列名
	public static final String COL_AUTH_CODE 		= "AUTHCODE"	;	//権限コード
	public static final String COL_AUTH_NAME 		= "AUTHNAME"	;	//権限ネーム
	public static final String COL_EMPLOYEE  		= "EMPLOYEE"	;	//従業員メニュー
	public static final String COL_MANAGER  		= "MANAGER"		;	//マネージャーメニュー
	public static final String COL_PAYROLL  		= "PAYROLL"		;	//給与メニュー
	public static final String COL_HUMAN  			= "HUMAN"		;	//人事メニュー
	public static final String COL_INSURANCE  		= "INSURANCE"	;	//社会保険メニュー
	public static final String COL_BONUS  			= "BONUS"		;	//賞与メニュー
	public static final String COL_ADJUSTMENT 		= "ADJUSTMENT"	;	//年末調整メニュー
	public static final String COL_EXPENSE  		= "EXPENSE"		;	//経費メニュー
	public static final String COL_SERVICE1  		= "SERVICE1"	;	
	public static final String COL_SERVICE2  		= "SERVICE2"	;	
	public static final String COL_SERVICE3  		= "SERVICE3"	;	
	public static final String COL_SERVICE4  		= "SERVICE4"	;	
	public static final String COL_SERVICE5  		= "SERVICE5"	;	
	public static final String COL_DELETE_FLAG		= "DELETEFLAG"	;	//削除フラグ

	/**
	 * コンストラクタ
	 */
	public CmAuthorityDao() {
		super();
	}

	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CmAuthorityDto> mappingAll() throws SQLException {
		List<CmAuthorityDto> all = new ArrayList<CmAuthorityDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}

	/**
	 * 検索結果取得(DTO)
	 * @return cm_serviceDto cm_serviceレコード
	 * @throws SQLException
	 */
	private CmAuthorityDto mapping() throws SQLException {
		CmAuthorityDto dto = new CmAuthorityDto();
		dto.setAuthCode 	(rs.getString (COL_AUTH_CODE	));	//権限コード
		dto.setAuthName 	(rs.getString (COL_AUTH_NAME	));	//権限ネーム
		dto.setEmployee 	(rs.getString (COL_EMPLOYEE		));	//従業員メニュー
		dto.setManager 		(rs.getString (COL_MANAGER		));	//マネージャーメニュー
		dto.setPayroll 		(rs.getString (COL_PAYROLL		));	//給与メニュー
		dto.setHuman 		(rs.getString (COL_HUMAN		));	//人事メニュー
		dto.setInsurance 	(rs.getString (COL_INSURANCE	));	//社会保険メニュー
		dto.setBonus 		(rs.getString (COL_BONUS		));	//賞与メニュー
		dto.setAdjustment 	(rs.getString (COL_ADJUSTMENT	));	//年末調整メニュー
		dto.setExpense 		(rs.getString (COL_EXPENSE		));	//経費メニュー
		dto.setService1 	(rs.getString (COL_SERVICE1		));	
		dto.setService2 	(rs.getString (COL_SERVICE2		));
		dto.setService3 	(rs.getString (COL_SERVICE3		));	
		dto.setService4 	(rs.getString (COL_SERVICE4		));	
		dto.setService5 	(rs.getString (COL_SERVICE5		));	
		dto.setDeleteFlag 	(rs.getInt    (COL_DELETE_FLAG	));	//削除フラグ
		return dto;
	}

	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmAuthorityDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CmAuthorityDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param authcode 権限コード(検索キー)
	 * @return CmAuthorityDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmAuthorityDto findForKey(
			String authcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, authcode);
			executeQuery();
			CmAuthorityDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param authcode 権限コード(検索キー)
	 * @return CmAuthorityDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmAuthorityDto findForUpdate(
			String authcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, authcode);
			executeQuery();
			CmAuthorityDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param authcode 権限コード
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String authcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_AUTH_CODE);
		sb.append(" LIKE ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   authcode  );
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * @return
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CmAuthorityDto> findForAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<CmAuthorityDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文による検索データ取得
	 * @param authcode 権限コード
	 * @param authname 	検索名称
	 * @param employee 	従業員メニュー
	 * @param manager 	マネージャーメニュー
	 * @param payroll 	給与メニュー
	 * @param human 	人事メニュー
	 * @param deleteflag 検索削除フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForFunctionCondition(
			String authcode,
			String authname,
			String employee,
			String manager,
			String payroll,
			String human,
			String deleteflag
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_AUTH_CODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_AUTH_NAME);
		sb.append(" LIKE ? AND ");
		sb.append(COL_EMPLOYEE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_MANAGER);
		sb.append(" LIKE ? AND ");
		sb.append(COL_PAYROLL);
		sb.append(" LIKE ? AND ");
		sb.append(COL_HUMAN);
		sb.append(" LIKE ? ");
		if (!deleteflag.equals("")) {
			sb.append(" AND ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   	authcode 	+ "%");
			setParam(index++, "%" + authname 	+ "%");
			setParam(index++, "%" + employee 	+ "%");
			setParam(index++, "%" + manager 	+ "%");
			setParam(index++, "%" + payroll 	+ "%");
			setParam(index++, "%" + human		+ "%");
			if (!deleteflag.equals("")) {
				setParam(index++, deleteflag);
			}
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文による検索データ取得
	 * @param authcode 権限コード
	 * @param authname 	検索名称
	 * @param employee 	従業員メニュー
	 * @param manager 	マネージャーメニュー
	 * @param payroll 	給与メニュー
	 * @param human 	人事メニュー
	 * @param service1 	その他１メニュー
	 * @param service2 	その他２メニュー
	 * @param deleteflag 検索削除フラグ
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForFunctionCondition(
			String authcode,
			String authname,
			String employee,
			String manager,
			String payroll,
			String human,
			String service1,
			String service2,
			String deleteflag
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_AUTH_CODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_AUTH_NAME);
		sb.append(" LIKE ? AND ");
		sb.append(COL_EMPLOYEE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_MANAGER);
		sb.append(" LIKE ? AND ");
		sb.append(COL_PAYROLL);
		sb.append(" LIKE ? AND ");
		sb.append(COL_HUMAN);
		sb.append(" LIKE ? AND ");
		sb.append(COL_SERVICE1);
		sb.append(" LIKE ? AND ");
		sb.append(COL_SERVICE2);
		sb.append(" LIKE ? ");
		if (!deleteflag.equals("")) {
			sb.append(" AND ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   	authcode 	+ "%");
			setParam(index++, "%" + authname 	+ "%");
			setParam(index++, "%" + employee 	+ "%");
			setParam(index++, "%" + manager 	+ "%");
			setParam(index++, "%" + payroll 	+ "%");
			setParam(index++, "%" + human		+ "%");
			setParam(index++, "%" + service1	+ "%");
			setParam(index++, "%" + service2	+ "%");
			if (!deleteflag.equals("")) {
				setParam(index++, deleteflag);
			}
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得（有効のみ）
	 * @param authcode 権限コード(検索キー)
	 * @return CmAuthorityDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmAuthorityDto findForAuth(
			String authcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = " + MospConst.DELETE_FLAG_OFF);
			sb.append(" AND ");
			sb.append(COL_AUTH_CODE);
			sb.append(" = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, authcode);
			executeQuery();
			CmAuthorityDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			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(CmAuthorityDto 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(CmAuthorityDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto , false);
			setParam(index++, dto.getAuthCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(CmAuthorityDto dto , boolean isInsert) throws SQLException {
		setParam(index++, dto.getAuthCode	());	//サービスコード
		setParam(index++, dto.getAuthName	());	//サービスネーム
		setParam(index++, dto.getEmployee	());	//従業員メニュー
		setParam(index++, dto.getManager	());	//マネージャーメニュー
		setParam(index++, dto.getPayroll	());	//給与メニュー
		setParam(index++, dto.getHuman		());	//人事メニュー
		setParam(index++, dto.getInsurance	());	//社会保険メニュー
		setParam(index++, dto.getBonus		());	//賞与メニュー
		setParam(index++, dto.getAdjustment	());	//年末調整メニュー
		setParam(index++, dto.getExpense	());	//経費メニュー
		setParam(index++, dto.getService1	());	
		setParam(index++, dto.getService2	());	
		setParam(index++, dto.getService3	());	
		setParam(index++, dto.getService4	());	
		setParam(index++, dto.getService5	());	
		setParam(index++, dto.getDeleteFlag	());	//削除フラグ
		setCommonParams(isInsert);
	}
}
