/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatCHKapSknSabunChsht
*	ソースファイル名	：JBSbatCHKapSknSabunChsht.java
*	作成者				：富士通　
*	作成日				：2012年09月25日
*＜機能概要＞
*　割賦債権内訳差分情報抽出部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v4.00		2012/09/25   FJ）後藤	【ANK-0546-00-00】新規作成
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.common.JBSbatMatchServiceInterface;
import eo.business.util.file.JBSbatCHIFM145;
import eo.business.util.file.JBSbatCHIFM145;
import eo.business.util.file.JBSbatCHIFM146;
import eo.common.constant.JACStrConst;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatCHKapSknSabunChsht extends JBSbatBusinessService implements JBSbatMatchServiceInterface
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** キーマッチ処理フラグ*/
	private boolean matchProcFlg;

	/** マスタファイル処理フラグ*/
	private boolean mastProcFlg;

	/** トランファイル処理フラグ*/
	private boolean tranProcFlg;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 退避．マスタキー */
	private String bkMastKey = null;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][opeDate=" + super.opeDate + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][freeItem=" + super.freeItem + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][initial]");
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @param mastMap　入力電文
	 * @param tranMap　入力電文
	 * @param outputInItem  入力情報
	 * @return JBSbatOutputItem 出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap mastMap, JBSbatServiceInterfaceMap tranMap, JBSbatOutputItem outputInItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");
		assert null != mastMap ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][inMap=" + mastMap.getMap().toString() + "]") : true;
		assert null != tranMap ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][inMap=" + tranMap.getMap().toString() + "]") : true;
		
		// 入力Ｍのみ存在する
		if (mastMap != null && tranMap == null)
		{
			this.setMastProcFlg(true);
			
			// 当日分のみの場合[マスタのみ]：割賦債権内訳差分情報作成（追加）
			makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_ADD);
			
			
		}
		// 入力Ｔのみ存在する
		else if (mastMap == null && tranMap != null)
		{
			this.setTranProcFlg(true);
			
			StringBuilder tranKey = new StringBuilder();
			
			// 請求契約番号
			tranKey.append(tranMap.getString(JBSbatCHIFM145.SEIKY_KEI_NO));
			// 請求年月
			tranKey.append(tranMap.getString(JBSbatCHIFM145.SAIKEN_YM));
			
			if(bkMastKey != null && tranKey.toString().compareTo(bkMastKey) == 0)
			{
				// 当日分にない場合[トランのみ]：割賦債権内訳差分情報作成（削除）
				makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_DEL);
			}
			
		}
		
		// 入力Ｍ、入力Ｔともに存在する
		else
		{
			// 第1マッチングキー取得
			StringBuilder tmpMastKey = new StringBuilder();
			StringBuilder tmpTranKey = new StringBuilder();
			
			// 請求契約番号
			tmpMastKey.append(mastMap.getString(JBSbatCHIFM145.SEIKY_KEI_NO));
			tmpTranKey.append(tranMap.getString(JBSbatCHIFM145.SEIKY_KEI_NO));
			
			// 債権年月
			tmpMastKey.append(mastMap.getString(JBSbatCHIFM145.SAIKEN_YM));
			tmpTranKey.append(tranMap.getString(JBSbatCHIFM145.SAIKEN_YM));
			
			String mastKey = tmpMastKey.toString();
			String tranKey = tmpTranKey.toString();
			
			// 第1マッチング処理
			if (mastKey.compareTo(tranKey) == 0)
			{
				// 第2マッチングキー
				StringBuilder tmpSbMastKey = new StringBuilder();
				StringBuilder tmpSbTranKey = new StringBuilder();

				// 請求契約番号
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.SEIKY_KEI_NO));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.SEIKY_KEI_NO));

				// サービス契約番号
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.SVC_KEI_NO));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.SVC_KEI_NO));

				// 料金グループコード
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.PRC_GRP_CD));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.PRC_GRP_CD));

				// 料金コースコード
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.PCRS_CD));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.PCRS_CD));

				// 料金サービスコード
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.PRC_SVC_CD));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.PRC_SVC_CD));

				// 料金項目コード
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.PRC_KMK_CD));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.PRC_KMK_CD));

				// 債権番号
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.SAIKEN_NO));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.SAIKEN_NO));

				// 割賦契約番号
				tmpSbMastKey.append(mastMap.getString(JBSbatCHIFM145.KAP_KEI_NO));
				tmpSbTranKey.append(tranMap.getString(JBSbatCHIFM145.KAP_KEI_NO));
				
				String sbMastKey = tmpSbMastKey.toString();
				String sbTranKey = tmpSbTranKey.toString();
				
				// 第2マッチング処理（当日分 = 前日分）
				if (sbMastKey.compareTo(sbTranKey) == 0)
				{
					this.setMatchProcFlg(true);
					this.setMastProcFlg(true);
					
					bkMastKey = mastKey;

					// 金額取得
					BigDecimal mastAmnt = mastMap.getBigDecimal(JBSbatCHIFM145.AMNT);
					BigDecimal tranAmnt = tranMap.getBigDecimal(JBSbatCHIFM145.AMNT);

					// 金額が変わっている場合
					if (mastAmnt.compareTo(tranAmnt) != 0)
					{
						// 割賦債権内訳差分情報作成（更新）
						makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_UPD);
					}
				}

				// 第2マッチング処理（当日分 < 前日分）
				else if (sbMastKey.compareTo(sbTranKey) < 0)
				{
					this.setMastProcFlg(true);

					// 当日分のみの場合[マスタのみ]：割賦債権内訳差分情報作成（追加）
					makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_ADD);
					
					bkMastKey = mastKey;
				}

				// 第2マッチング処理（請求債権内訳 > 請求内訳情報）
				else if (sbMastKey.compareTo(sbTranKey) > 0)
				{
					this.setTranProcFlg(true);
					// 当日分にない場合[トランのみ]：割賦債権内訳差分情報作成（削除）
					makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_DEL);
				}
			}
			// 第1マッチング処理「入力Ｍ＜入力Ｔ（Ｍのみ）」
			else if (mastKey.compareTo(tranKey) < 0)
			{
				// 当日分のみの場合[マスタのみ]：割賦債権内訳差分情報作成（追加）
				makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_ADD);
				
				bkMastKey = mastKey;
				
				this.setMastProcFlg(true);
			}
			// 第1マッチング処理「入力Ｍ＞入力Ｔ（Ｔのみ）」
			else
			{
				this.setTranProcFlg(true);

				if(bkMastKey != null && tranKey.compareTo(bkMastKey) == 0)
				{
					// 当日分にない場合[トランのみ]：割賦債権内訳差分情報作成（削除）
					makeUcwkInfoSabun(mastMap, tranMap, outputInItem, JACStrConst.INFO_UPDATE_FLG_DEL);
				}
			}
		}

		assert outputInItem != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute][outputInItem=" + outputInItem.getOutMapList().toString() + "]") : true;
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");

		return outputInItem;

	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][terminal]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][terminal]");
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	
	/**
	 * @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 出力情報
	 * @param infoUpdFlg 情報更新フラグ
	 */
	private void makeUcwkInfoSabun(JBSbatServiceInterfaceMap mastMap, JBSbatServiceInterfaceMap tranMap, JBSbatOutputItem outputInItem, String infoUpdFlg)
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeUcwkInfoSabun][infoUpdFlg=" + infoUpdFlg + "]");

		JBSbatServiceInterfaceMap sabunInfo = new JBSbatServiceInterfaceMap();

		// 更新フラグが更新・追加の場合、当日分割賦債権内訳情報をセットする
		if (infoUpdFlg.equals(JACStrConst.INFO_UPDATE_FLG_UPD) || infoUpdFlg.equals(JACStrConst.INFO_UPDATE_FLG_ADD))
		{
			// 債権番号
			sabunInfo.set(JBSbatCHIFM146.SAIKEN_NO,	 	mastMap.get(JBSbatCHIFM145.SAIKEN_NO));
			// 債権年月
			sabunInfo.set(JBSbatCHIFM146.SAIKEN_YM,	 	mastMap.get(JBSbatCHIFM145.SAIKEN_YM));
			// 割賦契約番号
			sabunInfo.set(JBSbatCHIFM146.KAP_KEI_NO, 	mastMap.get(JBSbatCHIFM145.KAP_KEI_NO));
			// 料金グループコード
			sabunInfo.set(JBSbatCHIFM146.PRC_GRP_CD, 	mastMap.get(JBSbatCHIFM145.PRC_GRP_CD));
			// 料金コースコード
			sabunInfo.set(JBSbatCHIFM146.PCRS_CD, 		mastMap.get(JBSbatCHIFM145.PCRS_CD));
			// 料金サービスコード
			sabunInfo.set(JBSbatCHIFM146.PRC_SVC_CD, 	mastMap.get(JBSbatCHIFM145.PRC_SVC_CD));
			// 料金項目コード
			sabunInfo.set(JBSbatCHIFM146.PRC_KMK_CD, 	mastMap.get(JBSbatCHIFM145.PRC_KMK_CD));
			// 金額
			sabunInfo.set(JBSbatCHIFM146.AMNT, 			mastMap.get(JBSbatCHIFM145.AMNT));
			// 請求契約番号
			sabunInfo.set(JBSbatCHIFM146.SEIKY_KEI_NO, 	mastMap.get(JBSbatCHIFM145.SEIKY_KEI_NO));
			// サービス契約番号
			sabunInfo.set(JBSbatCHIFM146.SVC_KEI_NO, 	mastMap.get(JBSbatCHIFM145.SVC_KEI_NO));
		}
		// 更新フラグが削除の場合、前日分割賦債権内訳情報をセットする
		else if (infoUpdFlg.equals(JACStrConst.INFO_UPDATE_FLG_DEL))
		{
			// 債権番号
			sabunInfo.set(JBSbatCHIFM146.SAIKEN_NO,	 	tranMap.get(JBSbatCHIFM145.SAIKEN_NO));
			// 債権年月
			sabunInfo.set(JBSbatCHIFM146.SAIKEN_YM,	 	tranMap.get(JBSbatCHIFM145.SAIKEN_YM));
			// 割賦契約番号
			sabunInfo.set(JBSbatCHIFM146.KAP_KEI_NO, 	tranMap.get(JBSbatCHIFM145.KAP_KEI_NO));
			// 料金グループコード
			sabunInfo.set(JBSbatCHIFM146.PRC_GRP_CD, 	tranMap.get(JBSbatCHIFM145.PRC_GRP_CD));
			// 料金コースコード
			sabunInfo.set(JBSbatCHIFM146.PCRS_CD, 		tranMap.get(JBSbatCHIFM145.PCRS_CD));
			// 料金サービスコード
			sabunInfo.set(JBSbatCHIFM146.PRC_SVC_CD, 	tranMap.get(JBSbatCHIFM145.PRC_SVC_CD));
			// 料金項目コード
			sabunInfo.set(JBSbatCHIFM146.PRC_KMK_CD, 	tranMap.get(JBSbatCHIFM145.PRC_KMK_CD));
			// 金額
			sabunInfo.set(JBSbatCHIFM146.AMNT, 			tranMap.get(JBSbatCHIFM145.AMNT));
			// 請求契約番号
			sabunInfo.set(JBSbatCHIFM146.SEIKY_KEI_NO, 	tranMap.get(JBSbatCHIFM145.SEIKY_KEI_NO));
			// サービス契約番号
			sabunInfo.set(JBSbatCHIFM146.SVC_KEI_NO, 	tranMap.get(JBSbatCHIFM145.SVC_KEI_NO));
		}

		// 情報更新フラグ
		sabunInfo.set(JBSbatCHIFM146.UPD_INFO_FLG, infoUpdFlg);
		
		// 出力フラグを設定
		sabunInfo.setOutFlg(true);
		
		// 出力共通電文に設定
		outputInItem.addOutMapList(sabunInfo);

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeUcwkInfoSabun]");

		return;
	}
}
