/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatCHKsdTrgtsIcrnHenshu
*	ソースファイル名	：JBSbatCHKsdTrgtsIcrnHenshu.java
*	作成者				：富士通　
 *	作成日				：2023年06月09日
*＜機能概要＞
*　貸倒れ対象者一覧編集部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v68.00.00   2023/10/25  FJ)森脇		【ANK-4475-00-00】インボイス対応 STEP2
*	v70.00.00   2024/02/05  FJ)寺田		【ANK-4539-00-00】インボイス対応 STEP3
*********************************************************************/
package eo.business.service;

import java.util.ArrayList;
import java.util.Iterator;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JACbatParamUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.common.JBSbatMatchServiceInterface;
import eo.business.common.JCHbatSeikyKaknoBusinessUtil;
import eo.business.util.file.JBSbatCHIFM137;
import eo.business.util.file.JBSbatCHIFM446;
import eo.business.util.file.JBSbatCHIFM447;
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 JBSbatCHKsdTrgtsIcrnHenshu extends JBSbatBusinessService implements JBSbatMatchServiceInterface
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** キーマッチ処理フラグ*/
	private boolean matchProcFlg;

	/** マスタファイル処理フラグ*/
	private boolean mastProcFlg;

	/** トランファイル処理フラグ*/
	private boolean tranProcFlg;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** 貸倒れ対象者一覧_見出し１７（料金項目コード） */
	private String 	fileHeader17 				= JACStrConst.KARA_MOJI;
	/** 貸倒れ対象者一覧_見出し１８（料金項目名） */
	private String 	fileHeader18 				= JACStrConst.KARA_MOJI;
	
	/** ヘッダー出力フラグ */
	private boolean headerOutFlg = true;

	/** マスタレコード保持用 **/
	JBSbatServiceInterfaceMap mastMapTmp = new JBSbatServiceInterfaceMap();

	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		// 業務パラメータ取得
		this.getWorkPara();
		
		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 mastMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][mastMap=" + mastMap.getMap().toString() + "]") : true;
		assert tranMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][tranMap=" + tranMap.getMap().toString() + "]") : true;
		
		if(headerOutFlg)
		{
			if (mastMap != null)
			{
				// ヘッダレコードの処理
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][初回レコード]");
				
				this.setMastProcFlg(true);
				// 出力情報生成
				this.makeOutputInfo(mastMap, outputInItem);
			}
			headerOutFlg = false;
		}
		else if (mastMap != null && tranMap == null)
		{
			// 入力Ｍのみ存在する
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍのみ存在する]");
			
			this.setMastProcFlg(true);
			// 出力情報生成
			this.makeOutputInfo(mastMap, outputInItem);
		}
		else if (mastMap == null && tranMap != null)
		{
			// 入力Ｔのみ存在する
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｔのみ存在する]");
			
			this.setTranProcFlg(true);
			// 出力情報生成
			this.makeOutputInfo(tranMap, outputInItem);
		}
		else
		{
			// 入力Ｍ、入力Ｔともに存在する
			
			// マスタキー
			String[] mastKey = this.getMatchingKey(mastMap);
			// トランキー
			String[] tranKey = this.getMatchingKey(tranMap);
			
			if (mastKey[0].compareTo(tranKey[0]) == 0 )
			{
				// 入力Ｍ＝入力Ｔ（マッチ）
				if (mastKey[1].compareTo(tranKey[1]) == 0 )
				{
					assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＝入力Ｔ（マッチ）]");

					this.setMastProcFlg(true);
					// 出力情報生成
					this.makeOutputInfo(mastMap, outputInItem);
				}
				else
				{
					assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ≠入力Ｔ]");

					this.setTranProcFlg(true);
					// 出力情報生成
					this.makeOutputInfo(tranMap, outputInItem);
				}
			}
			else if (mastKey[0].compareTo(tranKey[0]) < 0)
			{
				// 入力Ｍ＜入力Ｔ
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＜入力Ｔ]");
				
				this.setMastProcFlg(true);
				// 出力情報生成
				this.makeOutputInfo(mastMap, outputInItem);
			}
			else
			{
				// 入力Ｍ＞入力Ｔ
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][入力Ｍ、入力Ｔともに存在する][入力Ｍ＞入力Ｔ]");
				
				this.setTranProcFlg(true);
				// 出力情報生成
				this.makeOutputInfo(tranMap, 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]");
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	
	/**
	 * @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 trgMap 対象情報
	 * @return String マッチングキー
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String[] getMatchingKey(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		String[] matchingKey = new String[2];
		
		StringBuffer buf1 = new StringBuffer();
		StringBuffer buf2 = new StringBuffer();

		// 請求契約番号
		buf1.append(inMap.getString(JBSbatCHIFM137.SEIKY_KEI_NO));
		// 未収額変換後料金グループコード
		buf1.append(inMap.getString(JBSbatCHIFM137.PRC_GRP_CD));
		// 消費税率
		buf2.append(inMap.getString(JBSbatCHIFM137.STAX_RT));

		matchingKey[0] = buf1.toString();
		matchingKey[1] = buf2.toString();

		return matchingKey;
	}

	/**
	 * 貸倒れ対象者一覧を出力します。
	 * 
	 * @param mastMap 入力電文（貸倒れ対象者一覧）
	 * @param tranMap 入力電文（編集情報集計済）
	 * @param outputInItem  出力情報
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void makeOutputInfo(JBSbatServiceInterfaceMap inMap, JBSbatOutputItem outputInItem) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutputInfo]");
		
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		if (isMastProcFlg())
		{
			mastMapTmp = inMap;
		}
		// 入力電文コピー
		outMap.setMap(mastMapTmp.getMap());

		if (headerOutFlg)
		{
			// ヘッダレコードの編集
			outMap.setString(JBSbatCHIFM447.PRC_KMK_CD, this.fileHeader17);
			outMap.setString(JBSbatCHIFM447.PRC_KMK_NM, this.fileHeader18);
			
			// 未収情報をカンマ区切りに変換して設定
			outMap.setString(JBSbatCHIFM447.MI_INFO, mastMapTmp.getString(JBSbatCHIFM137.MI_INFO).replace(";", ","));
		}
		else if (isMastProcFlg())
		{
			// データレコードの編集
			outMap.setString(JBSbatCHIFM447.PRC_KMK_CD, new String());
			outMap.setString(JBSbatCHIFM447.PRC_KMK_NM, new String());
			
			// 未収情報をカンマ区切りに変換して設定
			outMap.setString(JBSbatCHIFM447.MI_INFO, mastMapTmp.getString(JBSbatCHIFM137.MI_INFO).replace(";", ","));
		}
		else if (isTranProcFlg())
		{
			// データレコードの編集
			outMap.setString(JBSbatCHIFM447.SEIKY_KEI_NO, inMap.getString(JBSbatCHIFM446.SEIKY_KEI_NO));
// ANK-4539-00-00 MOD START
			//outMap.setString(JBSbatCHIFM447.SEIKY_AMNT, new String());
			outMap.setString(JBSbatCHIFM447.SEIKY_AMNT, inMap.getBigDecimal(JBSbatCHIFM446.SEIKY_AMNT));
// ANK-4539-00-00 MOD END
			outMap.setString(JBSbatCHIFM447.STAX, new String());
			
			outMap.setString(JBSbatCHIFM447.STAX_RT, inMap.getString(JBSbatCHIFM446.STAX_RT));
			outMap.setString(JBSbatCHIFM447.PRC_GRP_CD, inMap.getString(JBSbatCHIFM446.PRC_GRP_CD));
			
			outMap.setString(JBSbatCHIFM447.PRC_KMK_CD, inMap.getString(JBSbatCHIFM446.PRC_KMK_CD));
			outMap.setString(JBSbatCHIFM447.PRC_KMK_NM, inMap.getString(JBSbatCHIFM446.PRC_KMK_NM));
			
			outMap.setString(JBSbatCHIFM447.CHRG_STAYMD, new String());
			outMap.setString(JBSbatCHIFM447.DSL_KISAN_YMD, new String());
			outMap.setString(JBSbatCHIFM447.LAST_RECEIPT_YMD, new String());
			outMap.setString(JBSbatCHIFM447.LATEST_SKNKSITK_YMD, new String());
			outMap.setString(JBSbatCHIFM447.TOKUSOKU_STAT, new String());

			// 未収情報を空に変換して設定
			ArrayList<String> miInfoList = new ArrayList<String>();
			String[] miInfoArray = mastMapTmp.getString(JBSbatCHIFM137.MI_INFO).split(",", -1);

			for (int i = 0; i < miInfoArray.length; i++)
			{
				miInfoList.add(new String());
			}
			outMap.setString(JBSbatCHIFM447.MI_INFO, JCHbatSeikyKaknoBusinessUtil.convListToString(miInfoList));
		}

		// 出力フラグ設定
		outMap.setOutFlg(true);
		
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutputInfo]");

		return;
	}
	
	/**
	 * 業務パラメータ管理及び、システムパラメータ管理から値を取得します。
	 * 
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void getWorkPara() throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getWorkPara]");
		
		// 業務パラメータ取得
		JACbatParamUtil paramUtil = new JACbatParamUtil(commonItem);
		try
		{
			// 貸倒れ対象者一覧_見出し１７（料金項目）
			this.fileHeader17 = paramUtil.getGyoumuParameter("CH_CHLST04101_H_17");
			// 貸倒れ対象者一覧_見出し１８（料金項目名）
			this.fileHeader18 = paramUtil.getGyoumuParameter("CH_CHLST04101_H_18");
			
		}
		finally
		{
			paramUtil.close();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getWorkPara][貸倒れ対象者一覧_見出し１７（料金項目） = " + this.fileHeader17 + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getWorkPara][貸倒れ対象者一覧_見出し１８（料金項目名） = " + this.fileHeader18 + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getWorkPara]");
		return;
	}
	
	/**
	 * マップをコピー
	 * 
	 * @param inMap
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private JBSbatServiceInterfaceMap getCopy(JBSbatServiceInterfaceMap paramMap) throws Exception
	{
		JBSbatServiceInterfaceMap resultMap = new JBSbatServiceInterfaceMap();
		
		Iterator iterator = paramMap.getMap().keySet().iterator();
		
		while (iterator.hasNext())
		{
			String key = (String)iterator.next();
			resultMap.setString(key, paramMap.getString(key));
		}
		return resultMap;
	}
	
}
