/*******************************************************************************
*	 All Rights reserved,Copyright (c) K-Opticom 
********************************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JSYejbEKK0491B009TPDA
*	ソースファイル名：JSYejbEKK0491B009TPDA.java
*	作成者			：EK919112
*	日付			：2015年08月11日
*＜機能概要＞
*	CMP請求書送付先情報一覧照会テンプレートDBアクセス部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v17.00.00	2015/07/28	FJ)遠藤		【ANK-2557-00-00】PMP再構築対応
*
********************************************************************************/

package eo.ejb.cbs.sqlf;

import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.base.CAANSQLFacility;
import com.fujitsu.futurity.model.base.CAANRuntimeException;
import com.fujitsu.futurity.model.base.CAANJDBCUtil;
import com.fujitsu.futurity.model.ejb.common.JSYejbConnection;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;
import com.fujitsu.futurity.model.ejb.common.StatusCodes;
import com.fujitsu.futurity.model.ejb.common.fw.TemplateDBAccessHandler;
import com.fujitsu.futurity.model.ejb.common.fw.AgentDispatchContext;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import eo.ejb.cbs.cbsmsg.EKK0491B009CBSMsg;
import eo.ejb.cbs.cbsmsg.EKK0491B009CBSMsg1List;
import eo.ejb.cbm.entity.KK0491ETMsg;
import eo.ejb.common.JKKModelCommon;

/**
*
*  テンプレートDBアクセス部品　EKK0491B009_SQLF<br>
*  入力された項目からSQL文を動的に組み立て、<br>
*  検索結果をCBSMsgにセットします。<br>
*/
public class JSYejbEKK0491B009TPDA extends CAANSQLFacility implements TemplateDBAccessHandler{
	
	/**
	 * コンストラクタ
	 */
	public JSYejbEKK0491B009TPDA()	{
	}
	
	/**
	*   EKK0491B009_SQLF<br>
	*   @param inMsg    入力値の格納されたCBSMsg <br>
	*   @return void
	*/
	public void invoke(CAANMsg inMsg,AgentDispatchContext inContext){
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(KK0491ETMsg.getTableName());
			
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("     KK0491.SEIKY_KEI_NO ")
					.append("    ,KK0491.MSKM_DTL_NO ")
					.append("    ,KK0491.SEIKY_WAY_CD ")
					.append("    ,(SELECT ZM0171_01.CD_DIV_NM ")
					.append("        FROM ZM_M_CD_NM_KANRI ZM0171_01 ")
					.append("       WHERE ZM0171_01.CD_SBT_CD = 'CD00166'  ")
					.append("         AND ZM0171_01.CD_DIV = KK0491.SEIKY_WAY_CD ")
					.append("         AND ZM0171_01.MK_FLG = '0') AS SEIKY_WAY_CD_NM ")
					.append("    ,KK0491.SEIKY_KEI_KANA ")
					.append("    ,KK0491.INFO_DSP_CD ")
					.append("    ,KK0491.SEIKYUS_HAKKO_YH ")
					.append("    ,KK0491.SKS_SHS_KSH_AD_SAI_FLG ")
					.append("    ,KK0491.SKS_SHS_NM_CUST_NM_SAI_FLG ")
					.append("    ,KK0491.SHS_HOJIN_SBT_CD ")
					.append("    ,KK0491.SHS_HOJIN_ZENGO_SHITEI_CD ")
					.append("    ,KK0491.SHS_KANA ")
					.append("    ,KK0491.SOHUS_NM ")
					.append("    ,KK0491.SOHUS_BKM ")
					.append("    ,KK0491.SOHUS_TNTSHA_NM ")
					.append("    ,KK0491.SOHUS_AD_CD ")
					.append("    ,KK0491.SOHUS_PCD ")
					.append("    ,KK0491.SOHUS_STATE_NM ")
					.append("    ,KK0491.SOHUS_CITY_NM ")
					.append("    ,KK0491.SOHUS_OAZTSU_NM ")
					.append("    ,KK0491.SOHUS_AZCHO_NM ")
					.append("    ,KK0491.SOHUS_ADRTTM ")
					.append("    ,KK0491.SOHUS_ADRRM ")
					.append("    ,KK0491.SOHUS_BNCHIGO ")
					.append("    ,KK0491.SOHUS_AD_MAN_INPUT_FLG ")
					.append("    ,KK0491.SOHUS_TELNO ")
					.append("    ,KK0491.SOHUS_FAX_NO ")
					.append("    ,KK0491.SOHUS_MLAD ")
					.append("    ,KK0491.SEIKYUS_SOHUS_CHGE_YMD ")
					.append("    ,KK0491.SEIKY_WAY_APLY_ADJ_YMD ")
					.append("    ,KK0491.RSV_TSTA_KIBO_YMD ")
					.append("    ,KK0491.RSV_APLY_YMD ")
					.append("    ,KK0491.IDO_DIV ")
					.append("    ,KK0491.FIRST_SEIKY_YM ")
					.append("    ,KK0491.FIRST_PAY_MSKMSHO_SOHU_YH ")
					.append("    ,KK0491.FIRST_PAY_MSKMSHO_SOHU_YMD ")
					.append("    ,KK0491.FIRST_PAY_MSKMSHO_RCP_YMD ")
					.append("    ,(SELECT MAX(KK0491_01.UPD_DTM) ")
					.append("        FROM KK_T_SEIKY_KEI KK0491_01 ")
					.append("       WHERE KK0491_01.SEIKY_KEI_NO = KK0491.SEIKY_KEI_NO ")
					.append("     ) AS UPD_DTM_BF ")
					.append(" FROM ")
					.append("     KK_T_SEIKY_KEI KK0491 ");
			// SQL文_個別部1
			if ( "1".equals( inMsg.getObject(EKK0491B009CBSMsg.FUNC_CODE).toString() ) ) {
 				sql_Buff.append(" WHERE ")
						.append("     KK0491.SEIKY_KEI_NO IN( ")
						.append("         SELECT ")
						.append("             KK0321.SEIKY_KEI_NO ")
						.append("         FROM ")
						.append("             KK_T_KAKINS KK0321 ")
						.append("         INNER JOIN ( ")
						.append("                 SELECT ")
						.append("                     KK0081_01.SVC_KEI_NO ")
						.append("                 FROM ")
						.append("                     KK_T_SVC_KEI KK0081_01 ")
						.append("                 WHERE KK0081_01.SYSID = ? ")
						.append("                   AND KK0081_01.SVC_KEI_STAT IN ('100', '210') ")
						.append("                   AND (KK0081_01.SVC_KEI_NO, KK0081_01.RSV_APLY_YMD || KK0081_01.GENE_ADD_DTM) = ")
						.append("                   (SELECT KK0081_GENE.SVC_KEI_NO, MAX(KK0081_GENE.RSV_APLY_YMD || KK0081_GENE.GENE_ADD_DTM) ")
						.append("                      FROM KK_T_SVC_KEI KK0081_GENE ")
						.append("                     WHERE KK0081_GENE.SVC_KEI_NO = KK0081_01.SVC_KEI_NO ")
						.append("                       AND KK0081_GENE.RSV_APLY_YMD <= ? ")
						.append("                       AND KK0081_GENE.RSV_APLY_CD = '2' ")
						.append("                       AND KK0081_GENE.MK_FLG = '0' ")
						.append("                     GROUP BY KK0081_GENE.SVC_KEI_NO) ")
						.append("         ) KK0081 ")
						.append("         ON KK0321.SVC_KEI_NO = KK0081.SVC_KEI_NO ")
						.append("         WHERE  ")
						.append("             KK0321.KAKINS_TSTAYMD <= ? ")
						.append("         AND ")
						.append("             KK0321.KAKINS_TENDYMD >= ? ")
						.append("         AND ")
						.append("             KK0321.MK_FLG = '0' ")
						.append("     ) ")
						.append(" AND (KK0491.SEIKY_KEI_NO, KK0491.RSV_APLY_YMD || KK0491.GENE_ADD_DTM) = ")
						.append("     (SELECT KK0491_GENE.SEIKY_KEI_NO, MAX(KK0491_GENE.RSV_APLY_YMD || KK0491_GENE.GENE_ADD_DTM) ")
						.append("        FROM KK_T_SEIKY_KEI KK0491_GENE ")
						.append("       WHERE KK0491_GENE.SEIKY_KEI_NO = KK0491.SEIKY_KEI_NO ")
						.append("         AND KK0491_GENE.RSV_APLY_YMD <= ? ")
						.append("         AND KK0491_GENE.RSV_APLY_CD = '2' ")
						.append("         AND KK0491_GENE.MK_FLG = '0' ")
						.append("       GROUP BY KK0491_GENE.SEIKY_KEI_NO)  ")
						.append(" ORDER BY SEIKY_KEI_NO ");
			}
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			if ( "1".equals( inMsg.getString(EKK0491B009CBSMsg.FUNC_CODE) ) ) {
				// オンライン日時の取得
				String opeDate = JKKModelCommon.getOpeDate(inMsg);
				// パラメータの設定(ＫＥＹ＿SYSIDを指定)
				CAANJDBCUtil.setParam(pstmt, 1, inMsg.getObject(EKK0491B009CBSMsg.KEY_SYSID));
				// パラメータの設定(運用日付を指定)
				CAANJDBCUtil.setParam(pstmt, 2, opeDate);
				// パラメータの設定(運用日付を指定)
				CAANJDBCUtil.setParam(pstmt, 3, opeDate);
				// パラメータの設定(運用日付を指定)
				CAANJDBCUtil.setParam(pstmt, 4, opeDate);
				// パラメータの設定(運用日付を指定)
				CAANJDBCUtil.setParam(pstmt, 5, opeDate);
			}
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			// EKK0491B009CBSMsgの明細にセットする項目
			String[] msgKeyList1 = {
					  EKK0491B009CBSMsg1List.SEIKY_KEI_NO
					, EKK0491B009CBSMsg1List.MSKM_DTL_NO
					, EKK0491B009CBSMsg1List.SEIKY_WAY_CD
					, EKK0491B009CBSMsg1List.SEIKY_WAY_CD_NM
					, EKK0491B009CBSMsg1List.SEIKY_KEI_KANA
					, EKK0491B009CBSMsg1List.INFO_DSP_CD
					, EKK0491B009CBSMsg1List.SEIKYUS_HAKKO_YH
					, EKK0491B009CBSMsg1List.SKS_SHS_KSH_AD_SAI_FLG
					, EKK0491B009CBSMsg1List.SKS_SHS_NM_CUST_NM_SAI_FLG
					, EKK0491B009CBSMsg1List.SHS_HOJIN_SBT_CD
					, EKK0491B009CBSMsg1List.SHS_HOJIN_ZENGO_SHITEI_CD
					, EKK0491B009CBSMsg1List.SHS_KANA
					, EKK0491B009CBSMsg1List.SOHUS_NM
					, EKK0491B009CBSMsg1List.SOHUS_BKM
					, EKK0491B009CBSMsg1List.SOHUS_TNTSHA_NMZ
					, EKK0491B009CBSMsg1List.SOHUS_AD_CD
					, EKK0491B009CBSMsg1List.SOHUS_PCD
					, EKK0491B009CBSMsg1List.SOHUS_STATE_NM
					, EKK0491B009CBSMsg1List.SOHUS_CITY_NM
					, EKK0491B009CBSMsg1List.SOHUS_OAZTSU_NM
					, EKK0491B009CBSMsg1List.SOHUS_AZCHO_NM
					, EKK0491B009CBSMsg1List.SOHUS_ADRTTM
					, EKK0491B009CBSMsg1List.SOHUS_ADRRM
					, EKK0491B009CBSMsg1List.SOHUS_BNCHIGO
					, EKK0491B009CBSMsg1List.SOHUS_AD_MAN_INPUT_FLG
					, EKK0491B009CBSMsg1List.SOHUS_TELNO
					, EKK0491B009CBSMsg1List.SOHUS_FAX_NO
					, EKK0491B009CBSMsg1List.SOHUS_MLAD
					, EKK0491B009CBSMsg1List.SEIKYUS_SOHUS_CHGE_YMD
					, EKK0491B009CBSMsg1List.SEIKY_WAY_APLY_ADJ_YMD
					, EKK0491B009CBSMsg1List.RSV_TSTA_KIBO_YMD
					, EKK0491B009CBSMsg1List.RSV_APLY_YMD
					, EKK0491B009CBSMsg1List.IDO_DIV
					, EKK0491B009CBSMsg1List.FIRST_SEIKY_YM
					, EKK0491B009CBSMsg1List.FIRST_PAY_MSKMSHO_SOHU_YH
					, EKK0491B009CBSMsg1List.FIRST_PAY_MSKMSHO_SOHU_YMD
					, EKK0491B009CBSMsg1List.FIRST_PAY_MSKMSHO_RCP_YMD
					, EKK0491B009CBSMsg1List.UPD_DTM_BF
			};
			
			// 明細に処理結果(ResultSet)をマッピング
			CAANMsg[] outMsg1 = mapMessageList(rsltQuery,msgKeyList1,"eo.ejb.cbs.cbsmsg.EKK0491B009CBSMsg1List",0);
			// inMsg(CBSMsg)に明細をセット
			inMsg.set("EKK0491B009CBSMsg1List", outMsg1);
			

		} catch(SQLException e) {
			inMsg.set(EKK0491B009CBSMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} finally {
			// 資源の解放
			try{
				if(rsltQuery != null){
					rsltQuery.close();
				}
				if(pstmt != null){
					pstmt.close();
				}
				if(con1 != null){
					closeConnection(con1);
				}
			}catch(SQLException e){
				inMsg.set(EKK0491B009CBSMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
	}
}
