/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatCHYoksiInfoSksi
*	ソースファイル名	：JBSbatCHYoksiInfoSksi.java
*	作成者				：富士通　
*	作成日				：2012年01月05日
*＜機能概要＞
*　抑止情報作成部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2012/01/05   富士通		新規作成
*	v3.00		2012/07/02   FJ）冨井	【ANK-0024-04-00】処理変更により新規作成
*********************************************************************/
package eo.business.service;

import java.util.ArrayList;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JACbatParamUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatCHIFM065;
import eo.business.util.file.JBSbatCHIFM101;
import eo.business.util.table.JBSbatKK_T_SEIKY_YOKSI;
import eo.common.constant.JACStrConst;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.db.JBSbatSQLAccess;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatCHYoksiInfoSksi extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** テーブル(請求抑止)*/
	private static final String D_TBL_NAME_KK_T_SEIKY_YOKSI = "KK_T_SEIKY_YOKSI";

	/** SQL定義キー(CH_SELECT_007)*/
	private static final String KK_T_SEIKY_YOKSI_CH_SELECT_007 = "CH_SELECT_007";

	/** テーブルアクセスクラス(請求抑止)*/
	private JBSbatSQLAccess db_KK_T_SEIKY_YOKSI = null;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** 退避．督促データ識別(抑止情報データ)*/
	private String tihTokusokuDataSkbt = null;
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// DBアクセスクラスを生成します
		db_KK_T_SEIKY_YOKSI = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_T_SEIKY_YOKSI);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial][peDate=" + super.opeDate + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial][freeItem=" + super.freeItem + "]");
		
		// 業務機能識別コード取得
		// パラメータ取得共通クラス
		JACbatParamUtil paramUtil = new JACbatParamUtil(commonItem);
		// 督促データ識別(抑止情報データ)
		this.tihTokusokuDataSkbt = paramUtil.getGyoumuParameter(JACStrConst.WKPARA_CH_TKSK_DATA_SKBT_04);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][督促データ識別(抑止情報データ)=" + tihTokusokuDataSkbt + "]");
		
		paramUtil.close();
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][initial]");
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @param inMap　入力電文
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap inMap) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");
		assert inMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][inMap=" + inMap.getMap().toString() + "]") : true;
		
		// 入力．督促データ識別 ≠ 初期処理．督促データ識別 の場合
		if(!tihTokusokuDataSkbt.equals(inMap.getString(JBSbatCHIFM101.TOKUSOKU_DATA_SKBT)))
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力．督促データ識別 ≠ 初期処理．督促データ識別 の場合]");
			// 処理を終了し次のレコードを読み込む
			return null;
		}
		
		// 請求抑止コード取得
		ArrayList<String> yoksiList = this.selectSeikyYoksiInfo(inMap);

		// 該当データなしの場合
		if(yoksiList.isEmpty())
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][ 請求抑止コード・該当データなしの場合]");
			// 処理を終了し次のレコードを読み込む
			return null;
		}
		
		// 出力共通電文
		JBSbatOutputItem outputItem = new JBSbatOutputItem();
		
		// 請求抑止コードチェック（取得件数分繰り返す）
		for(String seikyYoksiCd : yoksiList)
		{
			// 請求抑止．請求抑止コード ＝ "04"（合算請求抑止） の場合
			if(JACStrConst.SEIKY_YOKSI_CD_GSN_SEIKY.equals(seikyYoksiCd))
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][請求抑止．請求抑止コード ＝ 04（合算請求抑止） の場合]");
				this.makeOutputInfo(inMap, JACStrConst.SEIKY_YOKSI_CD_GSN_SEIKY, outputItem);
			}
			// 請求抑止．請求抑止コード ＝ "11"（督促抑止） の場合
			else if(JACStrConst.SEIKY_YOKSI_CD_TKSK.equals(seikyYoksiCd))
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][請求抑止．請求抑止コード ＝ 11（督促抑止） の場合]");
				this.makeOutputInfo(inMap, JACStrConst.SEIKY_YOKSI_CD_TKSK, outputItem);
			}
		}
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
		return outputItem;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][terminal]");
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		// DBアクセスクラスをクローズします
		db_KK_T_SEIKY_YOKSI.close();
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][terminal]");
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	
	/**
	 * SQLKEY(CH_SELECT_007)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 * 
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	請求契約番号
	 *		 	請求抑止フラグ
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeKK_T_SEIKY_YOKSI_CH_SELECT_007(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());

		// DBアクセスを実行します
		db_KK_T_SEIKY_YOKSI.selectBySqlDefine(paramList, KK_T_SEIKY_YOKSI_CH_SELECT_007);
	}
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**
	 * 抑止情報（売上オーソリ）作成処理を行います。
	 * 
	 * @param inMap 入力電文
	 * @param seikyYoksiCd 請求抑止コード
	 * @param outputInItem 出力情報
	 * @throws Exception
	 */
	private void makeOutputInfo(JBSbatServiceInterfaceMap inMap, String seikyYoksiCd, JBSbatOutputItem outputInItem) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutputInfo]");
		
		// 抑止情報（売上オーソリ）
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		// 請求契約番号
		outMap.set(JBSbatCHIFM065.SEIKY_KEI_NO, 	inMap.get(JBSbatCHIFM101.SEIKY_KEI_NO));
		// 請求年月
		outMap.set(JBSbatCHIFM065.SEIKY_YM, 		inMap.get(JBSbatCHIFM101.SEIKY_YM));
		// 発行抑止コード
		outMap.set(JBSbatCHIFM065.HAK_YOKSI_CD, 	seikyYoksiCd);
		
		// 出力フラグ設定
		outMap.setOutFlg(true);
		
		// 出力処理
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][makeOutputInfo][outputItem = " + outMap.getMap().toString() + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutputInfo]");
		return;
	}
	
	/**
	 * 請求抑止スキーマ検索し、請求抑止コードの返却を行います。
	 * @param inMap　入力電文
	 * @return ArrayList<String> 請求抑止コードのリスト（該当なしの場合、未設定）
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private ArrayList<String> selectSeikyYoksiInfo(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][selectSeikyYoksiInfo]");
		JBSbatCommonDBInterface whereMap = new JBSbatCommonDBInterface();
		
		whereMap.setValue(inMap.get(JBSbatCHIFM101.SEIKY_KEI_NO));	// 請求契約番号
		whereMap.setValue(JACStrConst.YOKSI_FLG_ARI);				// 請求抑止フラグ
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][selectSeikyYoksiInfo][whereMap=" + whereMap.getList().toString() + "]");
		
		// SQL実行
		executeKK_T_SEIKY_YOKSI_CH_SELECT_007(whereMap.getList().toArray());
		
		JBSbatCommonDBInterface dbMap = db_KK_T_SEIKY_YOKSI.selectNext();
		
		ArrayList<String> yoksiList = new ArrayList<String>();
		// 取得件数分設定を行う
		while(dbMap != null)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][selectSeikyYoksiInfo][取得あり]");
			// 検索結果をリストに設定
			yoksiList.add(dbMap.getString(JBSbatKK_T_SEIKY_YOKSI.SEIKY_YOKSI_CD));
			
			dbMap = db_KK_T_SEIKY_YOKSI.selectNext();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][selectSeikyYoksiInfo]");
		// 結果を返却
		return yoksiList;
	}
}
