/*******************************************************************************
*	 All Rights reserved,Copyright (c) K-Opticom 
********************************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JSYejbEKK3301B010TPDA
*	ソースファイル名：JSYejbEKK3301B010TPDA.java
*	作成者			：FJ)加藤
*	日付			：2021年05月25日
*＜機能概要＞
*	お得情報一覧照会テンプレートDBアクセス部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	ｖ53.00.00
*
********************************************************************************/

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.EKK3301B010CBSMsg;
import eo.ejb.cbs.cbsmsg.EKK3301B010CBSMsg1List;
import eo.ejb.cbs.cbsmsg.EKK3311A010CBSMsg1List;
import eo.ejb.cbm.entity.AC0091ETMsg;

/**
*
*  テンプレートDBアクセス部品　EKK3301B010_SQLF<br>
*  入力された項目からSQL文を動的に組み立て、<br>
*  検索結果をCBSMsgにセットします。<br>
*/
public class JSYejbEKK3301B010TPDA extends CAANSQLFacility implements TemplateDBAccessHandler{
	
	/**
	 * コンストラクタ
	 */
	public JSYejbEKK3301B010TPDA()	{
	}
	
	/**
	*   EKK3301B010_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(AC0091ETMsg.getTableName());
			
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
			.append("     KK3301.SYSID ")
			.append("    ,KK3301.OTOK_DIV ")
			.append("    ,KK3301.TEIAN_CHANNEL ")
			.append("    ,KK3301.KMK_01 ")
			.append("    ,KK3301.KMK_02 ")
			.append("      ,CASE WHEN (KK3301.KMK_04 IS NULL AND KK3311.KMK_04 IS NOT NULL) OR (KK3301.KMK_04 < KK3311.KMK_04 ) ")
			.append("         THEN ")
			.append("             KK3311.KMK_03 ")
			.append("         ELSE  ")
			.append("             KK3301.KMK_03 ")
			.append("       END KMK_03 ")
			.append("      ,CASE WHEN (KK3301.KMK_04 IS NULL AND KK3311.KMK_04 IS NOT NULL) OR (KK3301.KMK_04 < KK3311.KMK_04 ) ")
			.append("         THEN ")
			.append("             KK3311.KMK_04 ")
			.append("         ELSE ")
			.append("             KK3301.KMK_04 ")
			.append("       END KMK_04 ")
			.append("    ,KK3301.KMK_05 ")
			.append("    ,KK3301.KMK_06 ")
			.append("    ,KK3301.KMK_07 ")
			.append("    ,KK3301.KMK_08 ")
			.append("    ,KK3301.KMK_09 ")
			.append("    ,KK3301.KMK_10 ")
			.append("    ,KK3301.KMK_11 ")
			.append("    ,KK3301.KMK_12 ")
			.append("    ,KK3301.KMK_13 ")
			.append("    ,KK3301.KMK_14 ")
			.append("    ,KK3301.KMK_15 ")
			.append("    ,KK3301.KMK_16 ")
			.append("    ,KK3301.KMK_17 ")
			.append("    ,KK3301.KMK_18 ")
			.append("    ,KK3301.KMK_19 ")
			.append("    ,KK3301.KMK_20 ")
			.append("    ,KK3301.KMK_21 ")
			.append("    ,KK3301.KMK_22 ")
			.append("    ,KK3301.KMK_23 ")
			.append("    ,KK3301.KMK_24 ")
			.append("    ,KK3301.KMK_25 ")
			.append("    ,KK3301.KMK_26 ")
			.append("    ,KK3301.KMK_27 ")
			.append("    ,KK3301.KMK_28 ")
			.append("    ,KK3301.KMK_29 ")
			.append("    ,KK3301.KMK_30 ")
			.append("    ,KK3301.KMK_31 ")
			.append("    ,KK3301.KMK_32 ")
			.append("    ,KK3301.KMK_33 ")
			.append("    ,KK3301.KMK_34 ")
			.append("    ,KK3301.KMK_35 ")
			.append("    ,KK3301.KMK_36 ")
			.append("    ,KK3301.KMK_37 ")
			.append("    ,KK3301.KMK_38 ")
			.append("    ,KK3301.KMK_39 ")
			.append("    ,KK3301.KMK_40 ")
			.append("    ,KK3301.KMK_41 ")
			.append("    ,KK3301.KMK_42 ")
			.append("    ,KK3301.KMK_43 ")
			.append("    ,KK3301.KMK_44 ")
			.append("    ,KK3301.KMK_45 ")
			.append("    ,KK3301.KMK_46 ")
			.append("    ,KK3301.KMK_47 ")
			.append("    ,KK3301.KMK_48 ")
			.append("    ,KK3301.KMK_49 ")
			.append("    ,KK3301.KMK_50 ")
			.append("    ,KK3301.KMK_51 ")
			.append("    ,KK3301.KMK_52 ")
			.append("    ,KK3301.KMK_53 ")
			.append("    ,KK3301.KMK_54 ")
			.append("    ,KK3301.KMK_55 ")
			.append("    ,KK3301.KMK_56 ")
			.append("    ,KK3301.KMK_57 ")
			.append("    ,KK3301.KMK_58 ")
			.append("    ,KK3301.KMK_59 ")
			.append("    ,KK3301.KMK_60 ")
			.append("    ,KK3301.KMK_61 ")
			.append("    ,KK3301.KMK_62 ")
			.append("    ,KK3301.KMK_63 ")
			.append("    ,KK3301.KMK_64 ")
			.append("    ,KK3301.KMK_65 ")
			.append("    ,KK3301.KMK_66 ")
			.append("    ,KK3301.KMK_67 ")
			.append("    ,KK3301.KMK_68 ")
			.append("    ,KK3301.KMK_69 ")
			.append("    ,KK3301.KMK_70 ")
			.append("    ,KK3301.KMK_71 ")
			.append("    ,KK3301.KMK_72 ")
			.append("    ,KK3301.KMK_73 ")
			.append("    ,KK3301.KMK_74 ")
			.append("    ,KK3301.KMK_75 ")
			.append("    ,KK3301.KMK_76 ")
			.append("    ,KK3301.KMK_77 ")
			.append("    ,KK3301.KMK_78 ")
			.append("    ,KK3301.KMK_79 ")
			.append("    ,KK3301.KMK_80 ")
			.append("    ,KK3301.KMK_81 ")
			.append("    ,KK3301.KMK_82 ")
			.append("    ,KK3301.KMK_83 ")
			.append("    ,KK3301.KMK_84 ")
			.append("    ,KK3301.KMK_85 ")
			.append("    ,KK3301.KMK_86 ")
			.append("    ,KK3301.KMK_87 ")
			.append("    ,KK3301.KMK_88 ")
			.append("    ,KK3301.KMK_89 ")
			.append("    ,KK3301.KMK_90 ")
			.append("    ,KK3301.KMK_91 ")
			.append("    ,KK3301.KMK_92 ")
			.append("    ,KK3301.KMK_93 ")
			.append("    ,KK3301.KMK_94 ")
			.append("    ,KK3301.KMK_95 ")
			.append("    ,KK3301.KMK_96 ")
			.append("    ,KK3301.KMK_97 ")
			.append("    ,KK3301.KMK_98 ")
			.append("    ,KK3301.KMK_99 ")
			.append(" FROM ")
			.append("     KK_T_OTOK_INFO KK3301 ")
			.append("     LEFT JOIN ")
			.append("         (SELECT KK3311_01.SYSID ")
			.append("                 ,KK3311_01.OTOK_DIV ")
			.append("                 ,KK3311_01.TEIAN_CHANNEL ")
			.append("                 ,KK3311_01.KMK_03 ")
			.append("                 ,KK3311_01.KMK_04 ")
			.append("          FROM   KK_T_TEIAN_RSLT KK3311_01 ")
			.append("          WHERE KK3311_01.MK_FLG = '0' ")
			.append("          ) KK3311 ")
			.append("     ON  KK3301.SYSID = KK3311.SYSID ")
			.append("     AND KK3301.OTOK_DIV = KK3311.OTOK_DIV ")
			.append("     AND KK3301.TEIAN_CHANNEL = KK3311.TEIAN_CHANNEL ");
			// SQL文_個別部1
			if ( "1".equals( inMsg.getObject(EKK3301B010CBSMsg.FUNC_CODE).toString() ) ) {
 				sql_Buff.append(" WHERE ")
						.append(" 		KK3301.SYSID = ? ")
						.append(" AND	KK3301.MK_FLG = '0' ");
				
				// ＫＥＹお得区分が設定されている場合
				if (!inMsg.isNull(EKK3301B010CBSMsg.KEY_OTOK_DIV))
				{
					sql_Buff.append("             AND KK3301.OTOK_DIV = ? ");
				}
				
				// ＫＥＹ提案チャネルが設定されている場合
				if (!inMsg.isNull(EKK3301B010CBSMsg.KEY_TEIAN_CHANNEL))
				{
					sql_Buff.append("             AND KK3301.TEIAN_CHANNEL = ? ");
				}
			}
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			int k = 1;
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			if ( "1".equals( inMsg.getString(EKK3301B010CBSMsg.FUNC_CODE) ) ) {
				// パラメータの設定(ＫＥＹ＿SYSIDを指定)
				CAANJDBCUtil.setParam(pstmt, k++, inMsg.getObject(EKK3301B010CBSMsg.KEY_SYSID));
				// パラメータの設定(ＫＥＹお得区分を指定)
				if (!inMsg.isNull(EKK3301B010CBSMsg.KEY_OTOK_DIV))
				{
					CAANJDBCUtil.setParam(pstmt, k++, inMsg.getObject(EKK3301B010CBSMsg.KEY_OTOK_DIV));
				}
				// パラメータの設定(ＫＥＹ提案チャネルを指定)
				if (!inMsg.isNull(EKK3301B010CBSMsg.KEY_TEIAN_CHANNEL))
				{
					CAANJDBCUtil.setParam(pstmt, k++, inMsg.getObject(EKK3301B010CBSMsg.KEY_TEIAN_CHANNEL));
				}
			}
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			// EKK3301B010CBSMsgの明細にセットする項目
			String[] msgKeyList1 = {
					  EKK3301B010CBSMsg1List.SYSID
					, EKK3301B010CBSMsg1List.OTOK_DIV
					, EKK3301B010CBSMsg1List.TEIAN_CHANNEL
					, EKK3301B010CBSMsg1List.ITEM01
					, EKK3301B010CBSMsg1List.ITEM02
					, EKK3301B010CBSMsg1List.ITEM03
					, EKK3301B010CBSMsg1List.ITEM04
					, EKK3301B010CBSMsg1List.ITEM05
					, EKK3301B010CBSMsg1List.ITEM06
					, EKK3301B010CBSMsg1List.ITEM07
					, EKK3301B010CBSMsg1List.ITEM08
					, EKK3301B010CBSMsg1List.ITEM09
					, EKK3301B010CBSMsg1List.ITEM10
					, EKK3301B010CBSMsg1List.ITEM11
					, EKK3301B010CBSMsg1List.ITEM12
					, EKK3301B010CBSMsg1List.ITEM13
					, EKK3301B010CBSMsg1List.ITEM14
					, EKK3301B010CBSMsg1List.ITEM15
					, EKK3301B010CBSMsg1List.ITEM16
					, EKK3301B010CBSMsg1List.ITEM17
					, EKK3301B010CBSMsg1List.ITEM18
					, EKK3301B010CBSMsg1List.ITEM19
					, EKK3301B010CBSMsg1List.ITEM20
					, EKK3301B010CBSMsg1List.ITEM21
					, EKK3301B010CBSMsg1List.ITEM22
					, EKK3301B010CBSMsg1List.ITEM23
					, EKK3301B010CBSMsg1List.ITEM24
					, EKK3301B010CBSMsg1List.ITEM25
					, EKK3301B010CBSMsg1List.ITEM26
					, EKK3301B010CBSMsg1List.ITEM27
					, EKK3301B010CBSMsg1List.ITEM28
					, EKK3301B010CBSMsg1List.ITEM29
					, EKK3301B010CBSMsg1List.ITEM30
					, EKK3301B010CBSMsg1List.ITEM31
					, EKK3301B010CBSMsg1List.ITEM32
					, EKK3301B010CBSMsg1List.ITEM33
					, EKK3301B010CBSMsg1List.ITEM34
					, EKK3301B010CBSMsg1List.ITEM35
					, EKK3301B010CBSMsg1List.ITEM36
					, EKK3301B010CBSMsg1List.ITEM37
					, EKK3301B010CBSMsg1List.ITEM38
					, EKK3301B010CBSMsg1List.ITEM39
					, EKK3301B010CBSMsg1List.ITEM40
					, EKK3301B010CBSMsg1List.ITEM41
					, EKK3301B010CBSMsg1List.ITEM42
					, EKK3301B010CBSMsg1List.ITEM43
					, EKK3301B010CBSMsg1List.ITEM44
					, EKK3301B010CBSMsg1List.ITEM45
					, EKK3301B010CBSMsg1List.ITEM46
					, EKK3301B010CBSMsg1List.ITEM47
					, EKK3301B010CBSMsg1List.ITEM48
					, EKK3301B010CBSMsg1List.ITEM49
					, EKK3301B010CBSMsg1List.ITEM50
					, EKK3301B010CBSMsg1List.ITEM51
					, EKK3301B010CBSMsg1List.ITEM52
					, EKK3301B010CBSMsg1List.ITEM53
					, EKK3301B010CBSMsg1List.ITEM54
					, EKK3301B010CBSMsg1List.ITEM55
					, EKK3301B010CBSMsg1List.ITEM56
					, EKK3301B010CBSMsg1List.ITEM57
					, EKK3301B010CBSMsg1List.ITEM58
					, EKK3301B010CBSMsg1List.ITEM59
					, EKK3301B010CBSMsg1List.ITEM60
					, EKK3301B010CBSMsg1List.ITEM61
					, EKK3301B010CBSMsg1List.ITEM62
					, EKK3301B010CBSMsg1List.ITEM63
					, EKK3301B010CBSMsg1List.ITEM64
					, EKK3301B010CBSMsg1List.ITEM65
					, EKK3301B010CBSMsg1List.ITEM66
					, EKK3301B010CBSMsg1List.ITEM67
					, EKK3301B010CBSMsg1List.ITEM68
					, EKK3301B010CBSMsg1List.ITEM69
					, EKK3301B010CBSMsg1List.ITEM70
					, EKK3301B010CBSMsg1List.ITEM71
					, EKK3301B010CBSMsg1List.ITEM72
					, EKK3301B010CBSMsg1List.ITEM73
					, EKK3301B010CBSMsg1List.ITEM74
					, EKK3301B010CBSMsg1List.ITEM75
					, EKK3301B010CBSMsg1List.ITEM76
					, EKK3301B010CBSMsg1List.ITEM77
					, EKK3301B010CBSMsg1List.ITEM78
					, EKK3301B010CBSMsg1List.ITEM79
					, EKK3301B010CBSMsg1List.ITEM80
					, EKK3301B010CBSMsg1List.ITEM81
					, EKK3301B010CBSMsg1List.ITEM82
					, EKK3301B010CBSMsg1List.ITEM83
					, EKK3301B010CBSMsg1List.ITEM84
					, EKK3301B010CBSMsg1List.ITEM85
					, EKK3301B010CBSMsg1List.ITEM86
					, EKK3301B010CBSMsg1List.ITEM87
					, EKK3301B010CBSMsg1List.ITEM88
					, EKK3301B010CBSMsg1List.ITEM89
					, EKK3301B010CBSMsg1List.ITEM90
					, EKK3301B010CBSMsg1List.ITEM91
					, EKK3301B010CBSMsg1List.ITEM92
					, EKK3301B010CBSMsg1List.ITEM93
					, EKK3301B010CBSMsg1List.ITEM94
					, EKK3301B010CBSMsg1List.ITEM95
					, EKK3301B010CBSMsg1List.ITEM96
					, EKK3301B010CBSMsg1List.ITEM97
					, EKK3301B010CBSMsg1List.ITEM98
					, EKK3301B010CBSMsg1List.ITEM99
			};
			
			// 明細に処理結果(ResultSet)をマッピング
			CAANMsg[] outMsg1 = mapMessageList(rsltQuery,msgKeyList1,"eo.ejb.cbs.cbsmsg.EKK3301B010CBSMsg1List",0);
			// inMsg(CBSMsg)に明細をセット
			inMsg.set("EKK3301B010CBSMsg1List", outMsg1);
			

		} catch(SQLException e) {
			inMsg.set(EKK3301B010CBSMsg.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(EKK3301B010CBSMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
	}
}
