/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatCHHjnSnkknBtNkInfNmAdd
*	ソースファイル名	：JBSbatCHHjnSnkknBtNkInfNmAdd.java
*	作成者				：富士通　
*	作成日				：2013年03月22日
*＜機能概要＞
*　法人収納機関別入金情報名称付加部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2013/03/22   富士通		新規作成
*	v4.00.00	2013/04/02   FJ)小柴	【OT-2013-0000550】会計年月を再設定する処理を追加
*********************************************************************/
package eo.business.service;

import java.util.HashMap;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatCHIFM105;
import eo.business.util.table.JBSbatCH_M_CVSTORE;
import eo.business.util.table.JBSbatKK_M_BANK;
import eo.common.constant.JACStrConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.db.JBSbatSQLAccess;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatCHHjnSnkknBtNkInfNmAdd extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** テーブル(金融機関)*/
	private static final String D_TBL_NAME_KK_M_BANK = "KK_M_BANK";

	/** テーブル(コンビニエンスストア)*/
	private static final String D_TBL_NAME_CH_M_CVSTORE = "CH_M_CVSTORE";

	/** SQL定義キー(CH_SELECT_003)*/
	private static final String KK_M_BANK_CH_SELECT_003 = "CH_SELECT_003";

	/** SQL定義キー(CH_SELECT_004)*/
	private static final String CH_M_CVSTORE_CH_SELECT_004 = "CH_SELECT_004";

	/** テーブルアクセスクラス(金融機関)*/
	private JBSbatSQLAccess db_KK_M_BANK = null;

	/** テーブルアクセスクラス(コンビニエンスストア)*/
	private JBSbatSQLAccess db_CH_M_CVSTORE = null;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 金融機関マスタマップ */
	private HashMap<String, String[]> bankMap = null;
		
	/** コンビニエンスストアマスタマップ */
	private HashMap<String, String> cvStoreMap = null;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// DBアクセスクラスを生成します
		db_KK_M_BANK = new JBSbatSQLAccess(commonItem, D_TBL_NAME_KK_M_BANK);
		db_CH_M_CVSTORE = new JBSbatSQLAccess(commonItem, D_TBL_NAME_CH_M_CVSTORE);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		// 金融機関マスタマップを初期化取得する
		bankMap = new HashMap<String, String[]>();
		bankMap.clear();
		// コンビニエンスストアマスタより情報を取得する
		cvStoreMap = getCvStoreInfo();
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @param inMap　入力電文
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap inMap) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");
		assert inMap != null ? JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute][inMap=" + inMap.getMap().toString() + "]") : true;
		
		JBSbatOutputItem outputInItem = new JBSbatOutputItem();
		
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();

		// 入力電文コピー
		outMap.setMap(inMap.getMap());
		
		
		// 会計年月を再設定する
		String shunoYmd = this.changeNull2Value(inMap.getString(JBSbatCHIFM105.OUT_ORG_SHUNO_YMD));		// 収納年月日
		String nyukinTrnYmd = this.changeNull2Value(inMap.getString(JBSbatCHIFM105.NYUKIN_TRN_YMD));	// 入金処理年月日
		
		outMap.set(JBSbatCHIFM105.SEIKY_YM, getKaikeiYm(shunoYmd, nyukinTrnYmd));						// 会計年月
		
		// 「金融関連」情報 [0]:金融機関名,[1]:金融機関支店名
		String[] bankInfo = new String[2];
		
		// 「金融関連」情報取得
		String shunoBankCd = inMap.getString(JBSbatCHIFM105.SHUNO_BANK_CD);
		String shunoBankShitenCd = inMap.getString(JBSbatCHIFM105.SHUNO_BANK_SHITEN_CD);
		
		if ((!JACStrConst.KARA_MOJI.equals(shunoBankCd) && shunoBankCd != null) && (!JACStrConst.KARA_MOJI.equals(shunoBankShitenCd) && shunoBankShitenCd != null))
		{
			// 金融機関マップアクセスキー : 金融機関コード + 金融機関支店コード
			String mapkey = this.makeStr( shunoBankCd ,  shunoBankShitenCd);	

			// 金融機関マスタデータ取得
			String[] outRetList = bankMap.get(mapkey);
			
			// 該当の金融関連情報が存在しない場合
			if (outRetList == null)
			{
				outRetList = getBankInfo(shunoBankCd, shunoBankShitenCd);
			}

			if (outRetList == null)
			{
				// ログ出力（【エラー】ＤＢ未存在エラー）
				super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.ECHB0240CW,
						new String[] {JBSbatKK_M_BANK.TABLE_NAME, "[" + super.opeDate + "]" + mapkey});
			}
			else
			{
				// 「金融関連」情報を格納
				bankInfo = outRetList;
				
				// 収納金融機関名
				String bankNm = bankInfo[0];
				if(bankNm != null)
				{
					outMap.set(JBSbatCHIFM105.SHUNO_BANK_NM,  bankNm);
				}
				
				// 収納金融機関支店名
				String bankShitenNm = bankInfo[1];
				if(bankShitenNm != null)
				{
					outMap.set(JBSbatCHIFM105.SHUNO_BANK_SHITEN_NM,  bankShitenNm);
				}
			}
		}
		
		// 「コンビニエンスストア会社名」
		String cvstoreCompNm = null;
	
		// 「コンビニエンスストア」情報取得
		String shunoCvstoreCd = inMap.getString(JBSbatCHIFM105.SHUNO_CVSTORE_CD);
		if (!JACStrConst.KARA_MOJI.equals(shunoCvstoreCd) && shunoCvstoreCd != null)
		{
			//   コンビニエンスストアマップアクセスキー
			String mapkey = shunoCvstoreCd;		// コンビニエンスストアコード
	
			// 「コンビニエンスストア会社名」取得
			cvstoreCompNm = cvStoreMap.get(mapkey);
			
			// 該当のコンビニ情報が存在しない場合
			if (cvstoreCompNm == null || cvstoreCompNm.equals(""))
			{
				// ログ出力（【エラー】ＤＢ未存在エラー）
				super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.ECHB0240CW,
						new String[] {JBSbatCH_M_CVSTORE.TABLE_NAME,  "[" + super.opeDate + "," + super.opeDate + "]" + mapkey});
			}
			
			// 収納コンビニ会社名
			if(cvstoreCompNm != null)
			{
				outMap.set(JBSbatCHIFM105.SHUNO_CVSTORE_COMP_NM,  cvstoreCompNm);
			}
		}
		
		// 出力フラグ設定
		outMap.setOutFlg(true);
		
		outputInItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute][outputInItem=" + outputInItem.getOutMapList().toString() + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
		
		return outputInItem;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		// DBアクセスクラスをクローズします
		db_KK_M_BANK.close();
		db_CH_M_CVSTORE.close();
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/

	/**
	 * SQLKEY(CH_SELECT_003)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 * 
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	予約適用年月日
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeKK_M_BANK_CH_SELECT_003(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());
		paramList.setValue(param[2].toString());
		paramList.setValue(param[3].toString());
		paramList.setValue(param[4].toString());

		// DBアクセスを実行します
		db_KK_M_BANK.selectBySqlDefine(paramList, KK_M_BANK_CH_SELECT_003);
	}

	/**
	 * SQLKEY(CH_SELECT_004)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 * 
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	コンビニエンスストア適用開始年月日
	 *		 	コンビニエンスストア適用終了年月日
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeCH_M_CVSTORE_CH_SELECT_004(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());

		// DBアクセスを実行します
		db_CH_M_CVSTORE.selectBySqlDefine(paramList, CH_M_CVSTORE_CH_SELECT_004);
	}

	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/**
	 * 金融機関情報を取得します。
	 * 
	 * 以下の項目単位で金融機関のリストを作成しマップに格納します。
	 * ・金融機関コード
	 * ・金融機関支店コード
	 * 
	 * @param shunoBankCd 金融機関コード
	 * @param shunoBankShitenCd 金融機関支店コード
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String[] getBankInfo(String shunoBankCd, String shunoBankShitenCd)
		throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getBankInfo]");
		
		// DBアクセス用のパラメータ定義
		JBSbatCommonDBInterface dbList = new JBSbatCommonDBInterface();
		dbList.setValue(shunoBankCd); 		// 金融機関コード
		dbList.setValue(shunoBankShitenCd); // 金融機関支店コード
		dbList.setValue(super.opeDate); 	// 運用日
		dbList.setValue(super.opeDate); 	// 運用日
		dbList.setValue(super.opeDate); 	// 運用日
		
		// 金融機関情報検索SQL実行
		executeKK_M_BANK_CH_SELECT_003(dbList.getList().toArray());
		JBSbatCommonDBInterface dbMap = db_KK_M_BANK.selectNext();
		
		// 取得結果判定
		if (dbMap == null)
		{
			return null;
		}
		
		// key
		String mapkey = this.makeStr(shunoBankCd, shunoBankShitenCd);	// 金融機関コード ＋ 金融機関支店コード

		// value
		String[] valueList = new String[2];
		valueList[0] = dbMap.getString(JBSbatKK_M_BANK.BANK_NM);					// 金融機関名
		valueList[1] = dbMap.getString(JBSbatKK_M_BANK.BANK_SHITEN_NM);				// 金融機関支店名
	
		// 金融機関マスタマップにデータを格納
		bankMap.put(mapkey, valueList);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getBankInfo]");
		
		return valueList;
	}


	/**
	 * コンビニエンスストア情報を取得します。
	 * 以下の項目単位で金融機関のリストを作成しマップに格納します。
	 * ・コンビニエンスストアコード
	 * 
	 * @param inMap 入力電文
	 * @return JBSbatSQLAccessBean 金融機関情報マスタデータ
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private HashMap<String, String> getCvStoreInfo() throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getCvStoreInfo]");
		
		// コンビニエンスストアマスタのデータを格納したマップ
		HashMap<String, String> createMap = new HashMap<String, String>();
		
		// DBアクセス用のパラメータ定義
		JBSbatCommonDBInterface dbList = new JBSbatCommonDBInterface();
		dbList.setValue(super.opeDate); 	// 運用日
		dbList.setValue(super.opeDate); 	// 運用日

		// コンビニエンスストアマスタデータ取得
		executeCH_M_CVSTORE_CH_SELECT_004(dbList.getList().toArray());

		// 結果取得の定義
		JBSbatCommonDBInterface dbMap = db_CH_M_CVSTORE.selectNext();
		
		// 取得結果判定
		if(dbMap == null)
		{
			// ログ出力（【エラー】ＤＢ未存在エラー）
			super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.ECHB0240CW,
					new String[] {JBSbatCH_M_CVSTORE.TABLE_NAME, dbList.getList().toString()});
		}
		
		// 検索結果がなくなるまで繰り返す
		while (dbMap != null)
		{

			// key : コンビニエンスストアコード
			String mapKey = dbMap.getString(JBSbatCH_M_CVSTORE.CVSTORE_CD);
			
			// value : コンビニエンスストア会社名
			String value = dbMap.getString(JBSbatCH_M_CVSTORE.CVSTORE_COMP_NM);
		
			// コンビニエンスストアマスタマップにデータを格納
			createMap.put(mapKey, value);

			// 次レコード取得
			dbMap = db_CH_M_CVSTORE.selectNext();
		}

		assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getBYcvstoreMst]");
		
		// コンビニエンスストアマスタデータの返却
		return createMap;
	}
	
	/**
	 * 文字を連結します
	 * @param  string 文字
	 * @return strBuf 連結した文字列
	 */
	private String makeStr(String ...string)
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeStr]");
		
		StringBuffer strBuf = new StringBuffer();
		for(String str:string)
		{
			if(str == null)
			{
				strBuf.append(JACStrConst.KARA_MOJI);
			}
			else
			{
				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();
	}
	
	/**
	 * 引数の値がNULLの場合に空文字を返す。
	 * 
	 * @param value 変換対象文字列
	 * @return String 変換後文字列
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String changeNull2Value(String value) throws Exception 
	{
		if(value == null)
		{
			return(JACStrConst.KARA_MOJI);
		}
		
		return(value);
	}
	
	/**
	 * 会計年月を取得する。
	 * 
	 * @param shunoYmd 収納年月日
	 * @param nyukinTrnYmd 入金処理年月日
	 * @return String 会計年月
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private String getKaikeiYm(String shunoYmd, String nyukinTrnYmd) throws Exception 
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getKaikeiYm]");
		
		if(shunoYmd.compareTo(nyukinTrnYmd) < 0)
		// 収納年月日 < 入金処理年月日
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getKaikeiYm][収納年月日 < 入金処理年月日]");
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getKaikeiYm]");
			
			return(nyukinTrnYmd.substring(0, 6));
		}
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getKaikeiYm][収納年月日 >= 入金処理年月日]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getKaikeiYm]");
		
		return(shunoYmd.substring(0, 6));
	}
	
}
