/*******************************************************************************
*	All Rights reserved,Copyright (c) K-Opticom
********************************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム構築
*   モジュール名    ：JCNejbCN0331KRCK
*   ソースファイル名：JCNejbCN0331KRCK.java
*   作成者          ：富士通
*   日付            ：2013年01月24日
*＜機能概要＞
*   コンテンツ契約内訳の関連制約部品クラス
*＜修正履歴＞
*   バージョン  修正日      修正者      修正内容
*   v1.00.00    2013/01/24  富士通      新規作成
*   v15.00.00   2015/08/24  FJ）宇野    【ANK-2634-00-00】２重発番チェック仕様強化
*   v23.00.00   2016/04/27  FJ）中山    【OM-2016-0000753】コンテンツ契約内訳の照会不具合対応
*
********************************************************************************/

package eo.ejb.common.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.fujitsu.futurity.model.base.CAANException;
import com.fujitsu.futurity.model.base.CAANJDBCUtil;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.base.CAANRuntimeException;

import com.fujitsu.futurity.model.ejb.common.JSYejbConnection;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;
import com.fujitsu.futurity.model.ejb.common.fw.AgentDispatchContext;

import eo.ejb.cbm.entity.CK0011ETMsg;
import eo.ejb.cbm.entity.CN0331ETMsg;
import eo.ejb.cbm.entity.ZM0121ETMsg;
import eo.ejb.cbm.entity.ZM0181ETMsg;
import eo.ejb.common.JCNModelCommon;
import eo.ejb.common.db.JCNejbCN0331DBABase;

/**
 * <p>
 * コンテンツ契約内訳の関連制約部品クラスです。
 * </p>
 * @author 富士通
 */
public class JCNejbCN0331KRCK extends JCNejbCN0331DBABase
{

	/**
	 * <p>
	 * 新しいJCNejbCN0331KRCKを作成します。
	 * </p>
	 */
	public JCNejbCN0331KRCK()
	{
		super();
	}

	/**
	 * <p>
	 * SYSIDの有効チェックを行う
	 * </p>
	 * @param inMsg 処理対象のメッセージキャリア
	 * @param sysid チェック対象のSYSID
	 * @return true:有効／false:無効
	 */
	public boolean checkSYSID(CAANMsg inMsg, String sysid, AgentDispatchContext inContext){
		
		boolean result = true;

		// お客様情報を条件付き検索
		String custNtaikaiCd;
		
		try {
			custNtaikaiCd = findByCondition(inMsg, sysid, inContext);
		} catch (CAANException e){
			throw new CAANRuntimeException(e);
		}
		
		// 取得結果なしもしくは退会済みの場合
		if (custNtaikaiCd == null || custNtaikaiCd.equals("1")){
			
			result = false;
		}
		
		return result;
	}
	
	/**
	 * コンテンツ契約内訳のデータを取得する。
	 * @param inETMsg 処理対象のメッセージキャリア
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param sysid SYSID
	 * @param contKeiNo コンテンツ契約番号
	 * @param userid 050アプリSIPユーザーID
	 * @param telno 050電話番号
	 * @return true:対象データあり／false:対象データなし
	 */
	public String[] getContUcwk(CAANMsg inETMsg, AgentDispatchContext inContext
										, String sysid, String contKeiNo, String userid, String telno)
	{
		return getCntUcwk(inETMsg, inContext, sysid, contKeiNo, userid, telno);
	}
	
	/**
     * お客様情報の有無チェックを行う
	 * @param inMsg 処理対象のメッセージキャリア
	 * @param inContext Agentから渡されたAgentDispatchContext
     * @param sysid   SYSID
     * @return        検索結果を格納したメッセージキャリアの配列
     * @exception CAANException エンティティ検索時のエラーを通知
     */
    private String findByCondition(CAANMsg inMsg, String sysid, AgentDispatchContext inContext) throws CAANException {

		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(CK0011ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append(" 	CUST.CUST_NTAIKAI_CD ")
					.append(" FROM ")
					.append(" 	( ")
					.append(" 	  SELECT ")
					.append(" 	    A.CUST_NTAIKAI_CD ")
					.append(" 	  FROM ")
					.append(" 	    CK_T_CUST A ")
					.append(" 	  WHERE ")
					.append(" 	    (A.SYSID,A.RSV_APLY_YMD || A.GENE_ADD_DTM)= ")
					.append(" 	      (SELECT B.SYSID,MAX(B.RSV_APLY_YMD || B.GENE_ADD_DTM) AS GENE_ADD_DTM_MAX ")
					.append(" 	       FROM CK_T_CUST B ")
					.append(" 	       WHERE B.SYSID = A.SYSID AND B.RSV_APLY_CD = '2' AND B.MK_FLG = '0' AND B.RSV_APLY_YMD <= ? ")
					.append("          GROUP BY B.SYSID) ")
					.append("       AND A.SYSID = ?) CUST");
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// パラメータの設定(運用日付を指定)
			CAANJDBCUtil.setParam(pstmt, 1, JCNModelCommon.getOpeDateTimeStamp(inMsg, inContext, null));
			// パラメータの設定(SYSIDを指定)
			CAANJDBCUtil.setParam(pstmt, 2, sysid);
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			if (rsltQuery.next()) {
				return rsltQuery.getString("CUST_NTAIKAI_CD");
			} else {
				return null;
			}
			
		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
    }
    
	/**
	*   新規発行可能な050番号の存在チェック
	* 	@param inMsg 処理対象のメッセージキャリア
	* 	@param inContext Agentから渡されたAgentDispatchContext
    *	@return        検索結果を格納したメッセージキャリアの配列
    * 	@exception CAANException エンティティ検索時のエラーを通知
    */
	public boolean checkResultTelNo(CAANMsg inMsg,AgentDispatchContext inContext){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(ZM0121ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append("  SELECT ")
					.append("    ZM0121.TELNO AS TELNO ")
					.append("  FROM ")
					.append("    ZM_M_TELNO ZM0121 ")
					.append("  LEFT OUTER JOIN  ")
					.append("    ZM_T_TELNO_RSV ZM0181 ON ZM0181.TELNO = ZM0121.TELNO AND ZM0181.MK_FLG = '0' ")
					.append("  LEFT OUTER JOIN  ")
					.append("    ZM_T_AGING ZM0111 ON ZM0111.AGING_SBT_CD = '016' AND ZM0111.AGING_TG_VALUE = ZM0121.TELNO AND ZM0111.MK_FLG = '0' ")
					.append("  WHERE ")
					.append("    ZM0121.MK_FLG = '0' ")
					.append("    AND ZM0121.SVC_DIV = ? ")
					.append("    AND ZM0111.AGING_STAT IS NULL ")
					.append("    AND ROWNUM = 1 ")
					.append("    AND ZM0181.TELNO_RSV_STAT IS NULL ");
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			int i = 0;
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			CAANJDBCUtil.setParam(pstmt, ++i, "03");
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			if (rsltQuery.next()) {
				return true;
			} else {
				return false;
			}

		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
	}
	
	/**
	*   コンテンツ契約内訳よりコンテンツ契約番号を取得する。
	* 	@param inMsg 処理対象のメッセージキャリア
	* 	@param inContext Agentから渡されたAgentDispatchContext
	* 	@param sysid	SYSID
	*	@param contKeiNo	コンテンツ契約番号
    * 	@return        検索結果を格納したメッセージキャリアの配列
    * 	@exception CAANException エンティティ検索時のエラーを通知
    */
	private String[] getCntUcwk(CAANMsg inMsg,AgentDispatchContext inContext
								, String sysid, String contKeiNo, String userid, String telno){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(CN0331ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("     UCWK.CONT_KEI_NO ")
					.append("     , UCWK.N_050_TELNO ")
					.append("     , UCWK.N_050APL_SIP_USER_ID ")
					.append(" FROM ")
					.append("     CN_T_CONT_KEI_UCWK UCWK ")
					.append(" 	WHERE  ")
 					.append(" 	  UCWK.GENE_ADD_DTM =  ")
					.append("     (SELECT  ")
					.append("        MAX(CNUTWK_GENE.GENE_ADD_DTM)  ")
					.append("      FROM  ")
					.append("        CN_T_CONT_KEI_UCWK CNUTWK_GENE  ")
					.append("      WHERE CNUTWK_GENE.CONT_KEI_UCWK_NO = UCWK.CONT_KEI_UCWK_NO ")
					.append("        AND CNUTWK_GENE.CONT_KEI_NO = UCWK.CONT_KEI_NO ")
//OM-2016-0000753 DEL START
//					.append("        AND CNUTWK_GENE.GENE_ADD_DTM <= ? ")
//OM-2016-0000753 DEL END
					.append("        AND CNUTWK_GENE.MK_FLG = '0') ")
					.append("     AND UCWK.MK_FLG = '0' ");
			
			if (sysid != null)
			{
				sql_Buff.append("     AND UCWK.SYSID = ? ");
			}
			
			if (contKeiNo != null)
			{
				sql_Buff.append("     AND UCWK.CONT_KEI_NO = ?");
			}
			
			if (userid != null)
			{
				sql_Buff.append("     AND UCWK.N_050APL_SIP_USER_ID = ?");
			}
			
			if (telno != null)
			{
				sql_Buff.append("     AND UCWK.N_050_TELNO = ?");
			}
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			int iPCnt = 0;
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
//OM-2016-0000753 DEL START
//			// パラメータの設定(運用日付を指定)
//			CAANJDBCUtil.setParam(pstmt, ++iPCnt, JCNModelCommon.getOpeDateTimeStamp(inMsg, inContext, null));
//OM-2016-0000753 DEL END
			// パラメータの設定(SYSID)
			if (sysid != null)
			{
				CAANJDBCUtil.setParam(pstmt, ++iPCnt, sysid);
			}
			// パラメータの設定(コンテンツ契約番号)
			if (contKeiNo != null)
			{
				CAANJDBCUtil.setParam(pstmt, ++iPCnt, contKeiNo);
			}
			// パラメータの設定(050アプリSIPユーザーID)
			if (userid != null)
			{
				CAANJDBCUtil.setParam(pstmt, ++iPCnt, userid);
			}
			// パラメータの設定(050電話番号)
			if (telno != null)
			{
				CAANJDBCUtil.setParam(pstmt, ++iPCnt, telno);
			}
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();

			if (rsltQuery.next()) {
				String[] outMsg1 = {
						rsltQuery.getString(CN0331ETMsg.CONT_KEI_NO)
						, rsltQuery.getString(CN0331ETMsg.N_050_TELNO)
						, rsltQuery.getString(CN0331ETMsg.N_050APL_SIP_USER_ID)
				};
			
				return outMsg1;
			} else {
				return null;
			}

		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
	}

	/**
	*   電話番号予約に予約元コンテンツ契約番号が存在するかをチェックする。
	* 	@param inMsg 処理対象のメッセージキャリア
	* 	@param inContext Agentから渡されたAgentDispatchContext
	*	@param contKeiNo	コンテンツ契約番号
	*   @return true:取得結果あり/false:取得結果なし
    * 	@exception CAANException エンティティ検索時のエラーを通知
    */
	public boolean checkTelNoRsv(CAANMsg inMsg,AgentDispatchContext inContext, String contKeiNo){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(ZM0181ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("     TELNORSV.RSV_MT_CONT_KEI_NO ")
					.append(" FROM ")
					.append("     ZM_T_TELNO_RSV TELNORSV ")
					.append(" WHERE ")
					.append("     TELNORSV.MK_FLG = 0 ")
					.append("     AND TELNORSV.RSV_MT_CONT_KEI_NO = ? ");
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			CAANJDBCUtil.setParam(pstmt, 1, contKeiNo);
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			if (rsltQuery.next()) {
				return true;
			} else {
				return false;
			}

		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
	}
	

	/**
	*   電話番号予約にSYSID、契約番号に紐付くデータが存在するかをチェックする
	* 	@param inMsg 処理対象のメッセージキャリア
	* 	@param inContext Agentから渡されたAgentDispatchContext
	* 	@param sysid SYSID
	*	@param contKeiNo	コンテンツ契約番号
	*   @return true:取得結果あり/false:取得結果なし
    * 	@exception CAANException エンティティ検索時のエラーを通知
    */
	public boolean checkContKey(CAANMsg inMsg,AgentDispatchContext inContext, String sysid, String contKeiNo){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(ZM0181ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("     TELNORSV.RSV_MT_CONT_KEI_NO ")
					.append(" FROM ")
					.append("     ZM_T_TELNO_RSV TELNORSV ")
					.append(" WHERE ")
					.append("     TELNORSV.MK_FLG = 0 ")
					.append("     AND TELNORSV.YOYAKSHA_SYSID = ? ")
					.append("     AND TELNORSV.RSV_MT_CONT_KEI_NO = ? ");
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			CAANJDBCUtil.setParam(pstmt, 1, sysid);
			CAANJDBCUtil.setParam(pstmt, 2, contKeiNo);
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			if (rsltQuery.next()) {
				return true;
			} else {
				return false;
			}

		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
	}
	
// 2015/08/24 ANK-2634-00-00 Add Start
	/**
	*   指定した050電話番号が既に使用中（サービス提供中）であるかチェックする。
	* 	@param inMsg 処理対象のメッセージキャリア
	* 	@param inContext Agentから渡されたAgentDispatchContext
	*	@param telno	050電話番号
	*   @return true:使用中でない場合/false:使用中の場合
    * 	@exception CAANException エンティティ検索時のエラーを通知
    */
	public boolean check050TelDuplicate(CAANMsg inMsg,AgentDispatchContext inContext, String telno){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(CN0331ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("     UCWK.CONT_KEI_UCWK_STAT ")
					.append(" FROM ")
					.append("     CN_T_CONT_KEI_UCWK UCWK ")
					.append(" WHERE  ")
 					.append("     UCWK.GENE_ADD_DTM =  ")
					.append("       (SELECT  ")
					.append("          MAX(CNUTWK_GENE.GENE_ADD_DTM)  ")
					.append("        FROM  ")
					.append("          CN_T_CONT_KEI_UCWK CNUTWK_GENE  ")
					.append("        WHERE CNUTWK_GENE.CONT_KEI_UCWK_NO = UCWK.CONT_KEI_UCWK_NO ")
					.append("          AND CNUTWK_GENE.CONT_KEI_NO = UCWK.CONT_KEI_NO ")
//OM-2016-0000753 DEL START
//					.append("          AND CNUTWK_GENE.GENE_ADD_DTM <= ? ")
//OM-2016-0000753 DEL END
					.append("          AND CNUTWK_GENE.MK_FLG = '0') ")
					.append("     AND UCWK.MK_FLG = '0' ")
					.append("     AND UCWK.N_050_TELNO = ?");
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
//OM-2016-0000753 MOD START
//			// パラメータの設定(運用日付を指定)
//			CAANJDBCUtil.setParam(pstmt, 1, JCNModelCommon.getOpeDateTimeStamp(inMsg, inContext, null));
//			
//			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
//			CAANJDBCUtil.setParam(pstmt, 2, telno);
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			CAANJDBCUtil.setParam(pstmt, 1, telno);
//OM-2016-0000753 MOD END
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			// 取得したレコード分繰り返す
			while (rsltQuery.next())
			{
				// コンテンツ契約内訳ステータスが100:サービス提供中の場合
				// 「false」を返却する
				if ("100".equals(rsltQuery.getString(CN0331ETMsg.CONT_KEI_UCWK_STAT)))
				{
					return false;
				}
			}
			return true;

		} catch(SQLException e) {
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				throw new CAANRuntimeException(e);
			}
		}
	}
// 2015/08/24 ANK-2634-00-00 Add End
}
