/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom  					 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACInsentivePrcCalcCH
*	ソースファイル名	：JBSbatACInsentivePrcCalcCH.java
*	作成者				：富士通　
*	作成日				：2018年04月17日
*＜機能概要＞
*　インセンティブ料金計算（請求）です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v37.00.00	2018/4/17    富士通		ANK-3366-00-00 新規作成 ｅｏ光設備卸対応
*	v39.01.00	2018/10/05   富士通		IT1-2018-0000158 前月月末取得不備対応
*   v39.02.00	2018/10/30   富士通		【IT1-2018-0000188】卸先事業者インセンティブ設定のカレント取得条件の修正
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JACbatSchdlUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatACIFM299;
import eo.business.util.file.JBSbatACIFM301;
import eo.business.util.file.JBSbatACIFM302;
import eo.business.util.table.JBSbatCH_M_ORSJGS_INV_SETE;
import eo.common.constant.JACStrConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.db.JBSbatSQLAccess;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;

/**
* インセンティブ料金計算（請求）です。 <p>
*<BR>
* @author 富士通
*/
public class JBSbatACInsentivePrcCalcCH extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** テーブル(卸先事業者インセンティブ設定)*/
	private static final String D_TBL_NAME_CH_M_ORSJGS_INV_SETE = "CH_M_ORSJGS_INV_SETE";

	/** SQL定義キー(AC_SELECT_002)*/
	private static final String CH_M_ORSJGS_INV_SETE_AC_SELECT_002 = "AC_SELECT_002";

	/** テーブルアクセスクラス(卸先事業者インセンティブ設定)*/
	private JBSbatSQLAccess db_CH_M_ORSJGS_INV_SETE = null;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/**▼▼▼▼▼▼クラス変数追加 開始▼▼▼▼▼▼*/	
	
	/** 出力フラグ */
	boolean outputFlg = false;
	
	/** 退避．卸先事業者インセンティブ設定マップ */
	private HashMap<String, List<JBSbatCommonDBInterface>> tmpOrsIesntivMap = null;
	
	/** 退避．債権譲渡手数料率リスト */
	private List<JBSbatCommonDBInterface> valueList = null;

	/**  退避．群月初日 */
	private String tmpCycFst = null;
	
	/**  退避．群月末日 */
	private String tmpCycEnd = null;
	
	/**  退避．債権譲渡手数料率 */
	private BigDecimal sknJotoComiRt = null;
	
	/**▲▲▲▲▲▲クラス変数追加 終了▲▲▲▲▲▲*/

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// DBアクセスクラスを生成します
		db_CH_M_ORSJGS_INV_SETE = new JBSbatSQLAccess(commonItem, D_TBL_NAME_CH_M_ORSJGS_INV_SETE);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		// 料金スケジュール定義アクセス部品初期処理
		JACbatSchdlUtil schdlUtil = new JACbatSchdlUtil(commonItem);
		
		try
		{
			// 請求年月を取得する
			// IT1-2018-0000158 MOD START
			//String seikyYm = schdlUtil.getBillDate(super.opeDate, JACStrConst.EVENT_CD_RYOKIN_KEISAN_YMD);
			String seikyYm = schdlUtil.getBillDate(super.opeDate, JACStrConst.EVENT_CD_SEIKY_SHORI_YMD);
			// IT1-2018-0000158 MOD END
			
			// 請求年月から、利用開始終了日取得
			String[] strEndYmd = schdlUtil.getUseStrEnd(seikyYm, JACStrConst.EVENT_CD_USE_STAYMD, JACStrConst.EVENT_CD_USE_ENDYMD);
			
			this.tmpCycFst = strEndYmd[0];	// 群月初日
			this.tmpCycEnd = strEndYmd[1];	// 群月末日
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][群月初日：" + tmpCycFst + "]");
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][群月末日：" + tmpCycEnd + "]");
		}
		finally
		{
			// 料金スケジュール定義アクセス部品クローズ
			schdlUtil.close();
		}
		
		// 卸先事業者インセンティブ設定マップを作成
		this.makeOrsIesntivMap();
		
		
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}
	/**
	 * 主処理
	 * @param inMap　入力電文
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute(JBSbatServiceInterfaceMap inMap) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		// 出力共通電文
		JBSbatOutputItem outputItem = new JBSbatOutputItem(); // 出力共通電文
		
		//マスタの取得
		this.valueList = getList(inMap);
		
// v39.02.00 IT1-2018-0000188 ADD STA
		if(this.valueList == null){
			return outputItem;
		}
// v39.02.00 IT1-2018-0000188 ADD END
		
		sknJotoComiRt = valueList.get(0).getBigDecimal(JBSbatCH_M_ORSJGS_INV_SETE.SKN_JOTO_COMMISION_RT);
		
		// 計算処理
		BigDecimal saikenJotoComm = this.getsaikenJotoPrc(inMap);
		
		// 上記で取得した値を渡す
		this.makeOutputInfo(inMap,saikenJotoComm,outputItem);
	
	return outputItem;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}
	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		// DBアクセスクラスをクローズします
		db_CH_M_ORSJGS_INV_SETE.close();
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**
	 * SQLKEY(AC_SELECT_002)で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_ORSJGS_INV_SETE_AC_SELECT_002(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
// v39.02.00 IT1-2018-0000188 ADD STA
		paramList.setValue(param[0].toString());
// v39.02.00 IT1-2018-0000188 ADD END

		// DBアクセスを実行します
		db_CH_M_ORSJGS_INV_SETE.selectBySqlDefine(paramList,CH_M_ORSJGS_INV_SETE_AC_SELECT_002);
	}
	
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**▼▼▼▼▼▼メソッド追加 開始▼▼▼▼▼▼*/
	/**
	 * 卸先事業者インセンティブ設定マスタより、卸先事業者コード、債権譲渡手数料を取得し、
	 * 退避．卸先事業者インセンティブ設定マップに保持する
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private void makeOrsIesntivMap() throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOrsIesntivMap]");

		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		
		// 群月末日
		paramList.setValue(this.tmpCycEnd);
		
		// SQL実行
		executeCH_M_ORSJGS_INV_SETE_AC_SELECT_002(paramList.getList().toArray());
		
		JBSbatCommonDBInterface dbMap = db_CH_M_ORSJGS_INV_SETE.selectNext();
		
		this.tmpOrsIesntivMap = new HashMap<String, List<JBSbatCommonDBInterface>>();
		
		
		// 検索結果0件の場合
		if(null == dbMap)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOrsIesntivMap]");
			return;
		}
			
		//ブレイク用Key
		String bKey = makeStr(dbMap.getString(JBSbatCH_M_ORSJGS_INV_SETE.ORSJGS_CD));
		
		// 退避マップに設定するバリュー：検索結果のリスト
		ArrayList<JBSbatCommonDBInterface> saikenJotoComiList = new ArrayList<JBSbatCommonDBInterface>();
		// 取得出来た場合
		while(dbMap != null)
		{
			// KEY：卸先事業者コード
			String key = makeStr(dbMap.getString(JBSbatCH_M_ORSJGS_INV_SETE.ORSJGS_CD));
		
			// キーと検索結果のキーが異なった場合
			if(!bKey.equals(key))
			{
				// 卸先事業者インセンティブ設定マップにキーとバリューを設定する
				this.tmpOrsIesntivMap.put(bKey, saikenJotoComiList);
				// キーに新たな値を設定する
				bKey = key;
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][makeOrsIesntivMap]Key:" + bKey);
				// バリューをクリアー
				saikenJotoComiList = new ArrayList<JBSbatCommonDBInterface>();
			}
			// 取得結果より卸先事業者インセンティブ設定マップを作成しリストに格納
			saikenJotoComiList.add(dbMap);
			
			// 検索結果キークリアー
			key = null;
			
			dbMap =  db_CH_M_ORSJGS_INV_SETE.selectNext();

		}
		
		// 最後の取得結果を格納
		// 割賦料金マップにキーとバリューを設定する
		this.tmpOrsIesntivMap.put(bKey, saikenJotoComiList);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOrsIesntivMap]");
		return;
	}
	
	/**
	 * 卸先事業者インセンティブ設定マップを検索し、債権譲渡手数料率を取得します
	 * @param inMap 入力電文
	 * @return List 
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private List<JBSbatCommonDBInterface> getList(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getOrosiInsentive]");
		
		// KEY：卸先事業者コード 
		String key = inMap.getString(JBSbatACIFM299.ORSJGS_CD);
		
		// 取得できなかった場合
		if(!this.tmpOrsIesntivMap.containsKey(key))
		{
// v39.02.00 IT1-2018-0000188 MOD STA
//			// DB未存在エラー
//			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][getOrosiInsentive][DB未存在エラー]");
//			String[] outCntMsg = {JBSbatCH_M_ORSJGS_INV_SETE.TABLE_NAME, key};
//			// エラーログ出力（エラーコード："EACB0250CE"（DB未存在エラー））
//			throw new JBSbatBusinessException(JPCBatchMessageConstant.EACB0250CE, outCntMsg);
			return null;
// v39.02.00 IT1-2018-0000188 MOD END
		}
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getOrosiInsentive]");

		return this.tmpOrsIesntivMap.get(key);
	}
		
	
	/**
	 * 利用料金を算出をします
	 * @param inMap 入力電文
	 * @return BigDecimal 料金
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private BigDecimal getsaikenJotoPrc(JBSbatServiceInterfaceMap inMap) throws Exception
	{
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][getsaikenJotoPrc]");
		
		BigDecimal saikenJotoComm = BigDecimal.ZERO;
		
		// 料金取得
		// 債権譲渡手数料　←　（設備卸請求情報．債権譲渡金額　×　卸先事業者インセンティブ設定．債権譲渡手数料率／１００）を小数第一位で切り捨てる
		saikenJotoComm = inMap.getBigDecimal(JBSbatACIFM301.AMNT).multiply(sknJotoComiRt).divide(new BigDecimal("100"), 0, BigDecimal.ROUND_DOWN);

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][getsaikenJotoPrc]:");
		
		return saikenJotoComm;
	}
	
	/**
	 * 利用料金情報（債権譲渡金額）を出力します
	 * 
	 * @param inMap 入力電文
	 * @param saikenJotoCommission 債権譲渡手数料
	 * @param outputInItem  出力情報
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void makeOutputInfo(JBSbatServiceInterfaceMap inMap 
									, BigDecimal saikenJotoComm 
									, JBSbatOutputItem outputItem) throws Exception
	{
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][makeOutputInfo]");
		
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		// 卸先事業者コード
		outMap.setString(JBSbatACIFM302.ORSJGS_CD, 					inMap.getString(JBSbatACIFM301.ORSJGS_CD));

		// 金額
		outMap.setBigDecimal(JBSbatACIFM302.AMNT, 					inMap.getBigDecimal(JBSbatACIFM301.AMNT));

		// 債権譲渡手数料
		outMap.setString(JBSbatACIFM302.SKN_JOTO_COMMISION, 		saikenJotoComm);

		// 出力フラグ設定
		outMap.setOutFlg(true);
		
		outputItem.addOutMapList(outMap);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][makeOutputInfo]");

		return;
	}
	
	/**
	 * 文字を連結します
	 * @param  string 文字
	 * @return strBuf 連結した文字列
	 */
		private String makeStr(String ...string)
		{
			StringBuffer strBuf = new StringBuffer();
			for(String str:string)
			{
				if(str == null)
				{
					strBuf.append(JACStrConst.KARA_MOJI);
				}
				else
				{
					strBuf.append(str);
				}
			}
			return strBuf.toString();
		}
	public static void main(String[] args) {
	
	}
		
	/**▲▲▲▲▲▲メソッド追加 終了▲▲▲▲▲▲*/
}
