/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACTushinInfSort
*	ソースファイル名	：JBSbatACTushinInfSort.java
*	作成者				：富士通　
*	作成日				：2013年05月07日
*＜機能概要＞
*　通信量情報ソート部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2013/05/07	FJ)狭間		新規作成【TG1-2013-0000287】FTTH通信明細／合計不正対応（フェムトセル、重複チェック）
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import eo.business.util.file.JBSbatACIFM121;
import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACTushinInfSort extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** 退避用マップリスト */
	private ArrayList<JBSbatServiceInterfaceMap> mapList = null;

	/** ソート用リスト */
	private Map<String, String> sortList = null;

	/** 前行キー */
	private String prevKey = null;

	/** 前行キー */
	private int idxList = 0;

	/**
	 * 初期処理
	 * @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 + "]");

		// 変数初期化
		this.mapList = new ArrayList<JBSbatServiceInterfaceMap>();
		this.sortList = new TreeMap<String, String>();;
		this.prevKey = "";

		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;

		if(inMap == null)
		{
			assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute]EOF inMap");
			if(this.mapList.size() > 0)
			{
				List<Entry<String, String>> sortResult = sortMap(sortList);				
				for(Entry<String, String> resultRec : sortResult)
				{
					outputInItem.addOutMapList(this.mapList.get(Integer.parseInt(resultRec.getKey())));
				}
			}
		}
		else
		{
			String inMapKey = inMap.getString(JBSbatACIFM121.NINSHO_ID)
							+ inMap.getString(JBSbatACIFM121.DATA_STKU_DATE);
			if(this.mapList.size() > 0 && !this.prevKey.equals(inMapKey))
			{
				assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute]KeyBrake");
				
				List<Entry<String, String>> sortResult = sortMap(sortList);				
				for(Entry<String, String> resultRec : sortResult)
				{
					outputInItem.addOutMapList(this.mapList.get(Integer.parseInt(resultRec.getKey())));
				}
				this.mapList.clear();
				this.sortList.clear();
				this.idxList = 0;
			}
			else
			{
				// ダミーレコード設定
				outputInItem.addOutMapList(new JBSbatServiceInterfaceMap());				
			}
			// 入力マップ退避
			String idxKey = String.valueOf(this.idxList);
			assert 	JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute]key=" + idxKey + "value=" +inMap.getString(JBSbatACIFM121.TUSHINRYO));
			this.sortList.put(idxKey, inMap.getString(JBSbatACIFM121.TUSHINRYO));
			// 入力マップ退避
			inMap.setOutFlg(true);
			this.mapList.add(this.idxList, inMap);
			this.idxList = this.idxList + 1;
			// 入力マップキー退避
			this.prevKey = inMapKey;
		}
		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]");
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**
	 * 対象マップをValueでソートします。
	 * 
	 * @param  map			ソート対象
	 * @return entry		ソート結果
	 * @throws Exception 業務サービス内で発生した例外全般 
	 */
	private List<Entry<String, String>> sortMap(Map<String, String> map) throws Exception
	{
		
		List<Map.Entry<String, String>> entry = new ArrayList<Map.Entry<String, String>>();
		
		if(map == null || map.isEmpty())
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][sortMap][ソート対象未存在]");
			return entry;
		}
		entry = new ArrayList<Map.Entry<String, String>>(map.entrySet());
		Collections.sort(entry, new Comparator<Map.Entry<String, String>>() {

			/**
			 * <p>
			 * ソート用。
			 * </p>
			 * 
			 * @param o1
			 *            入力1
			 * @param o2
			 *            入力2
			 * @return int compareTo結果
			 */
			@Override
			public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)
			{
				// キー取得
				BigDecimal o1Key = new BigDecimal(o1.getValue().toString());
				BigDecimal o2Key = new BigDecimal(o2.getValue().toString());
				return o2Key.compareTo(o1Key);
			}
		});
		return entry;
	}
}
