/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACKaikDataShuk
*	ソースファイル名	：JBSbatACKaikDataShuk.java
*	作成者				：富士通　
*	作成日				：2013年05月01日
*＜機能概要＞
*　会計用債権回収委託データ集計部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v4.00.00	2013/05/01   FJ)小柴	【TG1-2013-0000297】新規作成
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatACIFM146;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACKaikDataShuk extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** 退避キー項目 */
	private String tempKey = null;

	/** 退避キー項目（件数） */
	private ArrayList<String> tmpCountKey = null;
	
	/** ファイル編集用退避マップ */
	private HashMap<String, String> tempMap = null;

	/** 集計（件数） */
	private BigDecimal sumCnt = BigDecimal.ZERO;
	
	/** 集計（金額） */
	private BigDecimal sumAmnt = BigDecimal.ZERO;

	/** 集計（税抜金額） */
	private BigDecimal sumZeinukiAmnt = BigDecimal.ZERO;

	/** 集計（消費税額） */
	private BigDecimal sumStax = BigDecimal.ZERO;

	/**
	 * 初期処理
	 * @param 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]");
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
		
		tmpCountKey = new ArrayList<String>();
	}

	/**
	 * 主処理
	 * @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;
		
		if(inMap != null)
		{
			// キー項目を作成
			String inKey = inMap.getString(JBSbatACIFM146.KAIK_UCWK_CD);
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][inKey=" + inKey + "]");
			
			// 件数カウント用のキー項目を作成
			String countKey = inMap.getString(JBSbatACIFM146.SEIKY_KEI_NO);
			
			// 初回以外、かつ 退避．キー項目 ≠ 入力．キー項目 の場合
			if(this.tempKey != null && !this.tempKey.equals(inKey))
			{
				// 会計用社用通話情報（集計済）作成処理
				createOutput(outputInItem);
				
				// 集計項目のクリア
				this.sumCnt = BigDecimal.ZERO;
				this.sumAmnt = BigDecimal.ZERO;
				this.sumZeinukiAmnt = BigDecimal.ZERO;
				this.sumStax = BigDecimal.ZERO;
				
				// 退避．件数カウント用キー項目を初期化
				this.tmpCountKey.clear();
				
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][tempKey=" + tempKey + "]");
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][キーブレイク]");
			}
			else if (this.tempKey == null)
			{
				// ダミーレコード設定
				outputInItem.addOutMapList(new JBSbatServiceInterfaceMap());
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][初回]");
			}
			// 退避情報の更新
			{
				// 集計する（件数）
				// 件数カウント用キー項目が異なる場合、件数を増やす
				if (!this.tmpCountKey.contains(countKey))
				{
					this.sumCnt = this.sumCnt.add(BigDecimal.ONE);
					this.tmpCountKey.add(countKey);
				}
				
				// 集計する（金額）
				this.sumAmnt = this.sumAmnt.add(new BigDecimal(inMap.getString(JBSbatACIFM146.AMNT)));
				// 集計する（税抜金額）
				this.sumZeinukiAmnt = this.sumZeinukiAmnt.add(new BigDecimal(inMap.getString(JBSbatACIFM146.ZEINUKI_AMNT)));
				// 集計する（消費税額）
				this.sumStax = this.sumStax.add(new BigDecimal(inMap.getString(JBSbatACIFM146.STAX)));
				
				// キー項目を退避
				this.tempKey = inKey;
				
				// その他の入力情報を退避
				this.tempMap = new HashMap<String, String>();
				this.tempMap.putAll(inMap.getMap());
			}
		}
		else
		{
			// 最終レコードの場合
			createOutput(outputInItem);
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力情報 ＝ NULLの場合]");
		}
		
		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 outputItem 		出力電文
	 * @throws Exception 　業務サービス内で発生した例外全般
	 */
	private void createOutput(JBSbatOutputItem outputInItem) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][createOutput]");
		
		// 会計収納情報勘定科目化
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		// 退避情報を設定
		outMap.setMap(this.tempMap);
		// 件数の設定
		outMap.setString(JBSbatACIFM146.CNT, this.sumCnt);
		// 金額の設定
		outMap.setString(JBSbatACIFM146.AMNT, this.sumAmnt);
		// 税抜金額の設定
		outMap.setString(JBSbatACIFM146.ZEINUKI_AMNT, this.sumZeinukiAmnt);
		// 消費税額の設定
		outMap.setString(JBSbatACIFM146.STAX, this.sumStax);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][createOutput][outMap=" + outMap.getMap().toString() + "]");
		
		outMap.setOutFlg(true);
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][createOutput]");
	}
}
