/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKDslSsnMatch
*	ソースファイル名	：JBSbatKKDslSsnMatch.java
*	作成者				：富士通　
*	作成日				：2017年09月25日
*＜機能概要＞
*　解約精算金検証・マッチング処理部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v33.00.00	2017/09/22  FJ）平野	新規作成
*********************************************************************/
package eo.business.service;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.common.JBSbatMatchServiceInterface;
import eo.business.util.file.JBSbatKKIFM752;
import eo.business.util.file.JBSbatKKIFM753;
import eo.business.util.file.JBSbatKKIFM754;
import eo.business.util.table.JBSbatCH_T_SEIKY_UCWK;
import eo.business.util.table.JBSbatKK_T_SVC_KEI;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;


/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKDslSsnMatch extends JBSbatBusinessService implements JBSbatMatchServiceInterface
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** キーマッチ処理フラグ*/
	private boolean matchProcFlg;

	/** マスタファイル処理フラグ*/
	private boolean mastProcFlg;

	/** トランファイル処理フラグ*/
	private boolean tranProcFlg;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 前回マッチキー*/
	private String bkMatchKey = null;
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @param mastMap　入力電文
	 * @param tranMap　入力電文
	 * @param outputInItem  入力情報
	 * @return JBSbatOutputItem 出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap mastMap, JBSbatServiceInterfaceMap tranMap, JBSbatOutputItem outputInItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		assert mastMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][mastMap=" + mastMap.getMap().toString() + "]") : true;
		assert tranMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][tranMap=" + tranMap.getMap().toString() + "]") : true;
		
		// 入力Ｍのみ存在する
		if (mastMap != null && tranMap == null)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍのみ存在する]");
			
			// 出力
			makeOutmap(mastMap, null, outputInItem);
			
			this.setMastProcFlg(true);
		}
		// 入力Ｔのみ存在する
		else if (mastMap == null && tranMap != null)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｔのみ存在する]");
			
			// マッチングキー（請求契約番号 + サービス契約番号 + 精算金・違約金区分）
			String tranKey = tranMap.getString(JBSbatCH_T_SEIKY_UCWK.SEIKY_KEI_NO).concat(
					tranMap.getString(JBSbatKK_T_SVC_KEI.SVC_KEI_NO)).concat(
					tranMap.getString("SSN_PNLTY_DIV"));
			
			// 前回マッチキー = トランキーの場合
			if (tranKey.equals(bkMatchKey))
			{
				// ファイルを進める
				this.setTranProcFlg(true);
			}
			else
			{
				// 出力
				makeOutmap(null, tranMap, outputInItem);
			}
			
			// 前回マッチキーとして保持
			bkMatchKey = tranKey;
		
		}
		// 入力Ｍ、入力Ｔともに存在する
		else
		{
			// マッチングキー（請求契約番号 + サービス契約番号 + 精算金・違約金区分）
			StringBuilder sbMast = new StringBuilder();
			sbMast.append(mastMap.getString(JBSbatCH_T_SEIKY_UCWK.SEIKY_KEI_NO))
				.append(mastMap.getString(JBSbatKK_T_SVC_KEI.SVC_KEI_NO))
				.append(mastMap.getString("SSN_PNLTY_DIV"));
			String mastKey = sbMast.toString();
			
			// マッチングキー（請求契約番号 + サービス契約番号 + 精算金・違約金区分）
			StringBuilder sbTran = new StringBuilder();
			sbTran.append(tranMap.getString(JBSbatCH_T_SEIKY_UCWK.SEIKY_KEI_NO))
				.append(tranMap.getString(JBSbatKK_T_SVC_KEI.SVC_KEI_NO))
				.append(tranMap.getString("SSN_PNLTY_DIV"));
			String tranKey = sbTran.toString();
			
			// 前回マッチキー = トランキーの場合
			if (tranKey.equals(bkMatchKey))
			{
				// トランのファイルを進める
				this.setTranProcFlg(true);
			}
			// 入力Ｍ＝入力Ｔ（マッチ）
			else if (mastKey.compareTo(tranKey) == 0)
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＝入力Ｔ（マッチ）]");
				
				// 出力
				makeOutmap(mastMap, tranMap, outputInItem);
				
				// 前回マッチキー←トランキー
				bkMatchKey = tranKey;
				
				// 両方のファイルを進める
				this.setMastProcFlg(true);
				this.setTranProcFlg(true);
			}
			// 入力Ｍ＜入力Ｔ
			else if (mastKey.compareTo(tranKey) < 0)
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＜入力Ｔ]");
				
				// 出力
				makeOutmap(mastMap, null, outputInItem);
				
				// マスタのファイルを進める
				this.setMastProcFlg(true);
			}
			// 入力Ｍ＞入力Ｔ
			else
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＞入力Ｔ]");
				
				// 出力
				makeOutmap(null, tranMap, outputInItem);
				
				// 前回マッチキー←トランキー
				bkMatchKey = tranKey;
				
				// トランのファイルを進める
				this.setTranProcFlg(true);
			}
		}
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
		return outputInItem;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**
	 * @return mastProcFlg を戻します。
	 */
	public boolean isMastProcFlg()
	{
		return mastProcFlg;
	}

	/**
	 * @return matchProcFlg を戻します。
	 */
	public boolean isMatchProcFlg()
	{
		return matchProcFlg;
	}

	/**
	 * @return tranProcFlg を戻します。
	 */
	public boolean isTranProcFlg()
	{
		return tranProcFlg;
	}

	/**
	 * @param mast_ProcFlg 設定する mastProcFlg。
	 */
	public void setMastProcFlg(boolean mast_ProcFlg)
	{
		this.mastProcFlg = mast_ProcFlg;
	}

	/**
	 * @param match_ProcFlg 設定する matchProcFlg。
	 */
	public void setMatchProcFlg(boolean match_ProcFlg)
	{
		this.matchProcFlg = match_ProcFlg;
	}

	/**
	 * @param tran_ProcFlg 設定する tranProcFlg。
	 */
	public void setTranProcFlg(boolean tran_ProcFlg)
	{
		this.tranProcFlg = tran_ProcFlg;
	}

	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**
	 * 出力情報作成
	 * 
	 * @param mastMap 入力情報
	 * @param tranMap 入力情報
	 * @param outputInItem 出力情報
	 * @throws Exception
	 */
	private void makeOutmap(
			JBSbatServiceInterfaceMap mastMap, JBSbatServiceInterfaceMap tranMap, JBSbatOutputItem outputInItem) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutmap]");
		
		// 出力情報
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();

		// トラン側が空でない場合
		if (tranMap != null)
		{
			// 請求契約番号
			outMap.setString(JBSbatKKIFM754.SEIKY_KEI_NO, tranMap.getString(JBSbatKKIFM753.SEIKY_KEI_NO));
			// サービス契約番号
			outMap.setString(JBSbatKKIFM754.SVC_KEI_NO, tranMap.getString(JBSbatKKIFM753.SVC_KEI_NO));
			// 料金項目コード
			outMap.setString(JBSbatKKIFM754.PRC_KMK_CD, null);
			// 精算金・違約金区分
			outMap.setString(JBSbatKKIFM754.SSN_PNLTY_DIV, tranMap.getString(JBSbatKKIFM753.SSN_PNLTY_DIV));
			// 請求精算金金額
			outMap.setString(JBSbatKKIFM754.SIKY_SSN_AMNT, null);
			// 案内精算金金額
			outMap.setString(JBSbatKKIFM754.GUIDE_SSN_AMNT, tranMap.getString(JBSbatKKIFM753.AMNT));
			// 請求番号
			outMap.setString(JBSbatKKIFM754.SEIKY_NO, null);
			// 請求年月
			outMap.setString(JBSbatKKIFM754.SEIKY_YM, null);
			// 整理番号
			outMap.setString(JBSbatKKIFM754.SEIRI_NO, null);
			// 料金グループコード
			outMap.setString(JBSbatKKIFM754.PRC_GRP_CD, null);
			// 料金コースコード
			outMap.setString(JBSbatKKIFM754.PCRS_CD, null);
			// 料金サービスコード
			outMap.setString(JBSbatKKIFM754.PRC_SVC_CD, null);
			// サービス詳細識別番号
			outMap.setString(JBSbatKKIFM754.SVC_DTL_SKBT_NO, null);
			// 請求内訳番号
			outMap.setString(JBSbatKKIFM754.SEIKY_UCWK_NO, null);
			// レター番号
			outMap.setString(JBSbatKKIFM754.DSL_LETTER_ADD_NO, tranMap.getString(JBSbatKKIFM753.DSL_LETTER_ADD_NO));
			// レター登録年月日
			outMap.setString(JBSbatKKIFM754.DSL_LETTER_ADD_YMD, tranMap.getString(JBSbatKKIFM753.DSL_LETTER_ADD_YMD));
			// 発行区分
			outMap.setString(JBSbatKKIFM754.DSL_LETTER_HAK_DIV, tranMap.getString(JBSbatKKIFM753.DSL_LETTER_HAK_DIV));
			// 登録年月日時分秒
			outMap.setString(JBSbatKKIFM754.ADD_DTM, tranMap.getString(JBSbatKKIFM753.ADD_DTM));
			// コメント備考
			outMap.setString(JBSbatKKIFM754.COMMENT_BIKO,  tranMap.getString(JBSbatKKIFM753.COMMENT_BIKO));
			// システム備考
			outMap.setString(JBSbatKKIFM754.SYS_BIKO, null);
			// 更新年月日時分秒
			outMap.setString(JBSbatKKIFM754.UPD_DTM, tranMap.getString(JBSbatKKIFM753.UPD_DTM));
			// 特例データ区分
			outMap.setString(JBSbatKKIFM754.TR_DATA_DIV, "0");
		}
		
		// マスタ側が空でない場合
		if (mastMap != null)
		{
			// 請求契約番号
			outMap.setString(JBSbatKKIFM754.SEIKY_KEI_NO, mastMap.getString(JBSbatKKIFM752.SEIKY_KEI_NO));
			// サービス契約番号
			outMap.setString(JBSbatKKIFM754.SVC_KEI_NO, mastMap.getString(JBSbatKKIFM752.SVC_KEI_NO));
			// 料金項目コード
			outMap.setString(JBSbatKKIFM754.PRC_KMK_CD, mastMap.getString(JBSbatKKIFM752.PRC_KMK_CD));
			// 精算金・違約金区分
			outMap.setString(JBSbatKKIFM754.SSN_PNLTY_DIV, mastMap.getString(JBSbatKKIFM752.SSN_PNLTY_DIV));
			// 請求精算金金額
			outMap.setString(JBSbatKKIFM754.SIKY_SSN_AMNT, mastMap.getString(JBSbatKKIFM752.AMNT));
			// 請求番号
			outMap.setString(JBSbatKKIFM754.SEIKY_NO, mastMap.getString(JBSbatKKIFM752.SEIKY_NO));
			// 請求年月
			outMap.setString(JBSbatKKIFM754.SEIKY_YM, mastMap.getString(JBSbatKKIFM752.SEIKY_YM));
			// 整理番号
			outMap.setString(JBSbatKKIFM754.SEIRI_NO, mastMap.getString(JBSbatKKIFM752.SEIRI_NO));
			// 料金グループコード
			outMap.setString(JBSbatKKIFM754.PRC_GRP_CD, mastMap.getString(JBSbatKKIFM752.PRC_GRP_CD));
			// 料金コースコード
			outMap.setString(JBSbatKKIFM754.PCRS_CD, mastMap.getString(JBSbatKKIFM752.PCRS_CD));
			// 料金サービスコード
			outMap.setString(JBSbatKKIFM754.PRC_SVC_CD, mastMap.getString(JBSbatKKIFM752.PRC_SVC_CD));
			// サービス詳細識別番号
			outMap.setString(JBSbatKKIFM754.SVC_DTL_SKBT_NO, mastMap.getString(JBSbatKKIFM752.SVC_DTL_SKBT_NO));
			// 請求内訳番号
			outMap.setString(JBSbatKKIFM754.SEIKY_UCWK_NO, mastMap.getString(JBSbatKKIFM752.SEIKY_UCWK_NO));
			// システム備考
			outMap.setString(JBSbatKKIFM754.SYS_BIKO, mastMap.getString(JBSbatKKIFM752.SYS_BIKO));

			if(tranMap == null)
			{
			    // 案内精算金金額
			    outMap.setString(JBSbatKKIFM754.GUIDE_SSN_AMNT, null);
			    // レター番号
			    outMap.setString(JBSbatKKIFM754.DSL_LETTER_ADD_NO, null);
			    // レター登録年月日
			    outMap.setString(JBSbatKKIFM754.DSL_LETTER_ADD_YMD, null);
			    // 発行区分
			    outMap.setString(JBSbatKKIFM754.DSL_LETTER_HAK_DIV, null);
			    // 登録年月日時分秒
			    outMap.setString(JBSbatKKIFM754.ADD_DTM, null);
			    // コメント備考
			    outMap.setString(JBSbatKKIFM754.COMMENT_BIKO, null);
			    // 更新年月日時分秒
			    outMap.setString(JBSbatKKIFM754.UPD_DTM, null);
			    // 特例データ区分
			    outMap.setString(JBSbatKKIFM754.TR_DATA_DIV, "0");
			}
		}
		
		// 出力指示フラグを設定
		outMap.setOutFlg(true);
		// 解約精算金検証・マッチングファイル出力
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutmap]");
	}
}
