/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACKaikUkAdjInfShuk
*	ソースファイル名	：JBSbatACKaikUkAdjInfShuk.java
*	作成者				：富士通　
*	作成日				：2012年03月28日
*＜機能概要＞
*　会計用売掛金調整情報集計部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2012/03/28   富士通		新規作成
*	v3.00		2012/08/03   FJ）冨井	【TGI-2012-0000054】ダミーレコード設定位置修正
*	v41.00.00	2019/04/04   FJ)寺園	【ANK-3482-00-00  】会計帳票の件数カウント方法変更
*********************************************************************/
package eo.business.service;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatACIFM142;
import eo.common.constant.JACStrConst;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACKaikUkAdjInfShuk extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 退避．キー項目 */
	private String tmpBfSaveKey = null;
	
	/** 入力．キー項目 */
	private String tmpInSaveKey = null;
	
	/** 退避．集計（金額） */
	private long tmpShukPrc = 0;
	
	/** 退避．集計（件数） */
	private String tmpShukCnt = "0";
//v41.00.00 ADD START
	private long tmpShukCnt2 = 0;
//v41.00.00 ADD END
	
	/** 退避．入力電文 */
	private JBSbatServiceInterfaceMap tmpInMap = 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;
		
		//1件目のレコードの場合
		if(!outputInItem.isOutMapList())
		{
			// ダミーレコードセット
			outputInItem.addOutMapList(new JBSbatServiceInterfaceMap());
		}
		
		// 会計用売掛金調整情報（集計済）作成
		// 会計用売掛金調整情報集計（入力情報 ≠ NULLの場合）
		if(inMap != null)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力情報 ≠ NULLの場合]");
			// キー項目設定
			// 入力．キー項目　←　（入力．請求契約番号＋入力．請求番号＋入力．サービス契約番号＋入力．料金グループコード
			//                                         ＋入力．料金コースコード＋入力．料金サービスコード＋入力．料金項目コード）
			this.tmpInSaveKey = this.makeStr(inMap.getString(JBSbatACIFM142.SEIKY_KEI_NO), inMap.getString(JBSbatACIFM142.SEIKY_NO), 
									inMap.getString(JBSbatACIFM142.SVC_KEI_NO), inMap.getString(JBSbatACIFM142.PRC_GRP_CD), 
									inMap.getString(JBSbatACIFM142.PCRS_CD), inMap.getString(JBSbatACIFM142.PRC_SVC_CD), 
									inMap.getString(JBSbatACIFM142.PRC_KMK_CD));
			
			// 退避．キー項目 ≠ null（初回以外）、かつ 退避．キー項目 ≠ 入力．キー項目 の場合
			if(this.tmpBfSaveKey != null && !this.tmpInSaveKey.equals(this.tmpBfSaveKey))
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][退避．キー項目 ≠ null（初回以外）、かつ 退避．キー項目 ≠ 入力．キー項目 の場合]");
				
				// 集計（金額）から集計（件数）を設定
				this.setShukCnt();
				
				// 会計用売掛金調整情報（集計済）作成処理
				this.makeOutputInfo(outputInItem); 
				
				// 集計項目のクリア
				{
					// 集計（件数）　←　0
					// 集計（金額）　←　0
					this.tmpShukCnt = JACStrConst.CNT_ZERO;
					this.tmpShukPrc = 0;
//v41.00.00 ADD START
					this.tmpShukCnt2 = 0;
//v41.00.00 ADD END
				}
			}
			
			// 退避情報の更新
			this.updTmpInfo(inMap);
			
		}
		// 最終レコードの処理（入力情報 ＝ NULLの場合）
		else
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力情報 ＝ NULLの場合]");
			
			// 集計（金額）から集計（件数）を設定
			this.setShukCnt();
			
			// 会計用売掛金調整情報（集計済）作成処理
			this.makeOutputInfo(outputInItem); 
			
		}
		
		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  string 文字
	 * @return strBuf 連結した文字列
	 */
	private String makeStr(String ...string)
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeStr]");
		
		StringBuffer strBuf = new StringBuffer();
		for(String str:string)
		{
			strBuf.append(str);
		}
		
		assert strBuf != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][strBuf][strBuf = " + strBuf.toString() + "]") : true;
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeStr]");
		
		return strBuf.toString();
	}
	
	/**
	 * 集計（金額）から集計（件数）を設定を行います。
	 * 
	 * @throws Exception
	 */
	private void setShukCnt() throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][setShukCnt]");
		
//v41.00.00 ADD START
		// 追加または削除の場合、集計（件数）より集計（件数）を設定
		// 集計（件数）から集計（件数）を設定
		// 集計（件数） ＞ 0 の場合 
		if(this.tmpShukCnt2 > 0)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setShukCnt][集計（件数） ＞ 0 の場合]");
			// 集計（件数） ← 1
			this.tmpShukCnt = JACStrConst.CNT_ONE;
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt][集計（件数） = " + this.tmpShukCnt + "]");
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt]");
			return;
		}
		// 集計（件数） ＜ 0 の場合 
		else if(this.tmpShukCnt2 < 0)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setShukCnt][集計（件数） ＜ 0 の場合]");
			// 集計（件数） ← -1
			this.tmpShukCnt = JACStrConst.CNT_MINUS_ONE;
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt][集計（件数） = " + this.tmpShukCnt + "]");
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt]");
			return;
		}
		// 変更の場合、集計（金額）より集計（件数）を設定
//v41.00.00 ADD END
		
		// 集計（金額）から集計（件数）を設定
		// 集計（金額） ＞ 0 の場合 
		if(this.tmpShukPrc > 0)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setShukCnt][集計（金額） ＞ 0 の場合]");
			// 集計（件数） ← 1
			this.tmpShukCnt = JACStrConst.CNT_ONE;
		}
		// 集計（金額） ＝ 0 の場合 
		else if(this.tmpShukPrc == 0)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setShukCnt][集計（金額） ＝ 0 の場合]");
			// 集計（件数） ← 0
			this.tmpShukCnt = JACStrConst.CNT_ZERO;
			
		}
		// 集計（金額） ＜ 0 の場合 
		else if(this.tmpShukPrc < 0)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setShukCnt][集計（金額） ＜ 0 の場合]");
			// 集計（件数） ← -1
			this.tmpShukCnt = JACStrConst.CNT_MINUS_ONE;
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt][集計（件数） = " + this.tmpShukCnt + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setShukCnt]");
		return;
	}
	
	/**
	 * 退避情報の更新を行います。
	 * 
	 * @param inMap 入力電文
	 * @throws Exception
	 */
	private void updTmpInfo(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][updTmpInfo]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][updTmpInfo][inMap=" + inMap.getMap().toString() + "]");
		
		// 集計（金額）←　集計（金額）　＋　入力．金額
		this.tmpShukPrc += Long.parseLong(inMap.getString(JBSbatACIFM142.AMNT));	
//v41.00.00 ADD START
		// 集計（件数）←　集計（件数）　＋　入力．件数
		this.tmpShukCnt2 += Long.parseLong(inMap.getString(JBSbatACIFM142.CNT));	
//v41.00.00 ADD END
		
		// 退避．キー項目　←　入力．キー項目
		this.tmpBfSaveKey = this.tmpInSaveKey;
		
		this.tmpInMap = new JBSbatServiceInterfaceMap();
		
		// 入力マスタ情報を全項目退避
		this.tmpInMap.setMap(inMap.getMap());
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][updTmpInfo][集計（金額） = " + String.valueOf(this.tmpShukPrc) + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][updTmpInfo][退避．キー項目 = " + this.tmpBfSaveKey + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][updTmpInfo][退避.入力電文 = " + this.tmpInMap.getMap().toString() + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][updTmpInfo]");
		return;
	}

	
	/**
	 * 会計用売掛金調整情報（集計済）作成処理を行います。
	 * @param outputInItem　出力情報
	 * @throws Exception
	 */
	private void makeOutputInfo(JBSbatOutputItem outputInItem) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutputInfo]");
		
		// 会計用売掛金調整情報（集計済）
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		// 退避入力情報を全項目コピー
		outMap.setMap(this.tmpInMap.getMap());
		
		// 単純転送以外の項目設定
		outMap.set(JBSbatACIFM142.CNT, 	this.tmpShukCnt);	// 件数
		outMap.set(JBSbatACIFM142.AMNT, this.tmpShukPrc);	// 金額
		
		// 出力フラグ設定
		outMap.setOutFlg(true);
		
		// 出力処理
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][makeOutputInfo][outputItem = " + outMap.getMap().toString() + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutputInfo]");
		return;
	}
}
