/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACSvckeiNoSykUplKeiCtShk
*	ソースファイル名	：JBSbatACSvckeiNoSykUplKeiCtShk.java
*	作成者				：富士通　
*	作成日				：2012年03月01日
*＜機能概要＞
*　サービス契約番号集約・上限契約数集計部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2012/03/01   富士通		新規作成
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatACIFM128;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACSvckeiNoSykUplKeiCtShk extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** 退避.集計キー */
	String thShukeiKey 		= null;
	/** 退避.集約キー */
	String thShuyakuKey 	= 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  inMap            入力電文
	 * @param  outputInItem     入力情報
	 * @return JBSbatOutputItem 出力情報
	 * @throws Exception        業務サービス内で発生した例外全般
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap inMap, JBSbatOutputItem outputInItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");
		assert 	inMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][inMap=" + inMap.getMap().toString() + "]") : true;
		// 入力集計キー
		String inShukeiKey 		= null;
		// 入力集約キー
		String inShuyakuKey 	= null;
		
		// 入力情報≠nullの場合
		if(inMap != null)
		{
			// 集約キー取得
			inShuyakuKey = getShuyakuKey(inMap);
			// 集計キー取得
			inShukeiKey = getShukeiKey(inMap);

			// １件目の場合（退避がnullの場合）
			if(this.thShukeiKey == null)
			{
				assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][初回起動時]");
				
				// 退避情報の設定
				// 集約キー取得
				this.thShuyakuKey = inShuyakuKey;
				// 集約キー退避
				this.thShukeiKey = inShukeiKey;
				
				// 退避.出力情報更新（出力フラグ：False）
				outputInItem.addOutMapList(makeOutInfo(inMap));
			}
			// 退避.集計キー≠入力.集計キー
			// 退避を出力し、入力を新たに退避する。
			else if(!inShukeiKey.equals(this.thShukeiKey))
			{
				assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][集計キー（請求年月＋請求契約番号＋割引サービスコード＋サービス契約番号）のブレイク]");

				// 退避.出力情報にデータがある場合
				if(outputInItem.getOutMapList().size() == 1)
				{
					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][退避レコードの出力フラグをtureに更新]");

					// 上限契約数集計済ファイル出力
					((JBSbatServiceInterfaceMap)outputInItem.getOutMapList().get(0)).setOutFlg(true);
				}
				// 退避.出力情報更新（出力フラグ：False）
				outputInItem.addOutMapList(makeOutInfo(inMap));
				
				// 集約キー取得
				this.thShuyakuKey = inShuyakuKey;
				// 集計キー退避
				this.thShukeiKey = inShukeiKey;
			}
			else
			{
				// 退避.集約キー≠入力.集約キー
				// 退避に上限契約数をプラスする。
				if(!inShuyakuKey.equals(this.thShuyakuKey))
				{
					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][集約キー（請求年月＋請求契約番号＋割引サービスコード）のブレイク]");

					JBSbatServiceInterfaceMap tmpMap = (JBSbatServiceInterfaceMap)outputInItem.getOutMapList().get(0);
					
					BigDecimal thUpplCnt = new BigDecimal(tmpMap.getString(JBSbatACIFM128.UPPL_KEI_CNT)); 	// 退避.上限契約数
					BigDecimal inUpplCnt = new BigDecimal(inMap.getString(JBSbatACIFM128.UPPL_KEI_CNT)); 	// 入力.上限契約数

					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][上限契約数を集約する]");
					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][　退避.上限契約数：" + thUpplCnt + "]");
					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][　入力.上限契約数：" + inUpplCnt + "]");

					// 上限契約数集計
					thUpplCnt = thUpplCnt.add(inUpplCnt);

					assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][　集計後．上限契約数：" + thUpplCnt + "]");
					
					tmpMap.set(JBSbatACIFM128.UPPL_KEI_CNT, thUpplCnt.toString());

					// 集約キー退避
					this.thShuyakuKey = inShuyakuKey;
				}
			}
		}
		// 入力情報＝nullの場合(最終レコード)
		else
		{
			// 出力情報≠nullの場合
			if(outputInItem.getOutMapList().size() == 1)
			{
				assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][最終レコード時の処理]");
				assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][退避レコードの出力フラグをtureに更新]");

				// 上限契約数集計済ファイル出力
				((JBSbatServiceInterfaceMap)outputInItem.getOutMapList().get(0)).setOutFlg(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]");
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**
	 * 集計キーを取得します。
	 * @param  inMap     入力電文
	 * @return String    集計キー
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String getShukeiKey(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getShukeiKey]");
		// 集計キー
		String shukeiKey = null;
		
		StringBuffer keyBuf = new StringBuffer();
		if (inMap != null)
		{
			keyBuf.append(inMap.getString(JBSbatACIFM128.SEIKY_YM)); 		// 請求年月
			keyBuf.append(inMap.getString(JBSbatACIFM128.SEIKY_KEI_NO)); 	// 請求先番号
			keyBuf.append(inMap.getString(JBSbatACIFM128.WRIB_SVC_CD)); 	// 割引サービスコード
			shukeiKey = keyBuf.toString();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getShukeiKey][shukeiKey=" + shukeiKey + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getShukeiKey]");
	
		return shukeiKey;
	}
	
	/**
	 * 集約キーを取得します。
	 * @param  inMap     入力電文
	 * @return String    集約キー
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String getShuyakuKey(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getShuyakuKey]");
		// 集約キー
		String shuyakuKey = null;
		
		StringBuffer keyBuf = new StringBuffer();
		if (inMap != null)
		{
			keyBuf.append(inMap.getString(JBSbatACIFM128.SEIKY_YM)); 		// 請求年月
			keyBuf.append(inMap.getString(JBSbatACIFM128.SEIKY_KEI_NO)); 	// 請求先番号
			keyBuf.append(inMap.getString(JBSbatACIFM128.WRIB_SVC_CD)); 	// 割引サービスコード
			keyBuf.append(inMap.getString(JBSbatACIFM128.SVC_NO)); 			// サービス番号
			shuyakuKey = keyBuf.toString();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getShuyakuKey][shuyakuKey=" + shuyakuKey + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getShuyakuKey]");
	
		return shuyakuKey;
	}
	
	/**
	 * 出力情報編集（出力フラグFalse）
	 * @param  inMap      入力情報
	 * @return JBSbatServiceInterfaceMap 出力情報
	 * @throws Exception  業務サービス内で発生した例外全般
	 */
	private JBSbatServiceInterfaceMap makeOutInfo(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutInfo]");

		JBSbatServiceInterfaceMap rtnMap = new JBSbatServiceInterfaceMap();

		rtnMap.set(JBSbatACIFM128.SEIKY_KEI_NO, 	inMap.get(JBSbatACIFM128.SEIKY_KEI_NO)); 	// 請求先番号
		rtnMap.set(JBSbatACIFM128.SEIKY_YM, 		inMap.get(JBSbatACIFM128.SEIKY_YM)); 		// 請求年月
		rtnMap.set(JBSbatACIFM128.WRIB_SVC_CD, 		inMap.get(JBSbatACIFM128.WRIB_SVC_CD)); 	// 割引サービスコード
		rtnMap.set(JBSbatACIFM128.UPPL_KEI_CNT, 	inMap.get(JBSbatACIFM128.UPPL_KEI_CNT)); 	// 上限契約数

		assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutInfo]");
		
		return rtnMap;
	}
}
