/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACFtthTsnryoShuk
*	ソースファイル名	：JBSbatACFtthTsnryoShuk.java
*	作成者				：富士通　
*	作成日				：2012年03月05日
*＜機能概要＞
*　ＦＴＴＨ通信量情報集計（明細登録用）部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2012/03/05   富士通		新規作成
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatACIFM124;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACFtthTsnryoShuk extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	/** 退避.集計キー */
	String thShukeiKey 						= null;
	/** 退避.出力情報情 */
	JBSbatServiceInterfaceMap thTranMap 	= 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;
		// 初回起動時
		if(this.thShukeiKey == null)
		{
			JBSbatServiceInterfaceMap dummyMap = new JBSbatServiceInterfaceMap();
			// 出力フラグOFF
			dummyMap.setOutFlg(false);
			// 出力共通電文設定
			outputInItem.addOutMapList(dummyMap);
		}

		// 入力情報≠nullの場合
		if(inMap != null)
		{
			// 集計キー取得
			inShukeiKey = getShukeiKey(inMap);
			// 退避.集計キー≠入力.集計キー
			if(!inShukeiKey.equals(this.thShukeiKey))
			{
				// 出力情報≠nullの場合
				if(thTranMap != null)
				{
					// 利用通信量設定
					this.thTranMap.set(JBSbatACIFM124.TUSHINRYO, setTushin(this.thTranMap.getString(JBSbatACIFM124.TUSHINRYO)));
					// FTTH通信量ファイル出力
					this.thTranMap.setOutFlg(true);
					outputInItem.addOutMapList(this.thTranMap);
				}
				// 集計キー退避
				this.thShukeiKey 	= inShukeiKey;
				// 出力情報設定
				this.thTranMap 		= new JBSbatServiceInterfaceMap();
				this.thTranMap 		= inMap;
			}
			// 退避.集計キー＝入力.集計キー
			else
			{
				BigDecimal thTushin 	= new BigDecimal(this.thTranMap.getString(JBSbatACIFM124.TUSHINRYO)); 	// 退避.利用通信量
				BigDecimal inTushin 	= new BigDecimal(inMap.getString(JBSbatACIFM124.TUSHINRYO)); 			// 入力.利用通信量
				// 利用通信量サマリー
				thTushin 				= thTushin.add(inTushin);
				// 利用通信量
				this.thTranMap.set(JBSbatACIFM124.TUSHINRYO, thTushin.toString());
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][thTushin=" + thTushin.toString() + "]");
			}
		}
		// 入力情報＝nullの場合(最終レコード)
		else
		{
			// 出力情報≠nullの場合
			if(thTranMap != null)
			{
				// 利用通信量設定
				this.thTranMap.set(JBSbatACIFM124.TUSHINRYO, setTushin(this.thTranMap.getString(JBSbatACIFM124.TUSHINRYO)));
				// FTTH通信量ファイル出力
				this.thTranMap.setOutFlg(true);
				outputInItem.addOutMapList(this.thTranMap);
			}
		}
		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  tushinryo    利用通信量
	 * @return chgTushinryo 利用通信量（変換後）
	 * @throws Exception    業務サービス内で発生した例外全般
	 */
	private String setTushin(String tushinryo) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][setTushin]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setTushin][tushinryo=" + tushinryo + "]");

		BigDecimal chgTushinryo 	= new BigDecimal(tushinryo);	// 利用通信量
		// 単位変換
		chgTushinryo 				= chgTushinryo.multiply(BigDecimal.TEN);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setTushin][tushinryo=" + tushinryo + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setTushin][chgTushinryo=" + chgTushinryo.toString() + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setTushin]");
		return chgTushinryo.toString();
	}
	
	/**
	 * 集計キーを取得します。
	 * @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(JBSbatACIFM124.SVKEI_NO)); 		// サービス契約番号
			keyBuf.append(inMap.getString(JBSbatACIFM124.PCRS_CD)); 		// 料金コースコード
			keyBuf.append(inMap.getString(JBSbatACIFM124.PRC_SVC_CD)); 		// 料金サービスコード
			keyBuf.append(inMap.getString(JBSbatACIFM124.NINSHO_ID)); 		// 認証ID
			keyBuf.append(inMap.getString(JBSbatACIFM124.RIYOU_YM)); 		// 利用年月
			keyBuf.append(inMap.getString(JBSbatACIFM124.DATA_STKU_YMD)); 	// データ取得日
			shukeiKey = keyBuf.toString();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getShukeiKey][shukeiKey=" + shukeiKey + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getShukeiKey]");
	
		return shukeiKey;
	}
}
