/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：物流管理システム
*   モジュール名    ：JDKCommon03CC
*   ソースファイル名：JDKCommon03CC.java
*   作成者          ：富士通
*   日付            ：2011年11月09日
*＜機能概要＞
*   在庫出荷機器引当一覧照会画面です。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/11/09   FJ          新規作成
*   v3.00.00    2012/04/11   下原        ANK-0316-00-00
*
**********************************************************************/
package com.fujitsu.futurity.bp.custom.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fujitsu.futurity.bp.custom.mapping.DKSV0074_DKSV0074OP_EDK0011B060BSMapper;
import com.fujitsu.futurity.bp.x21.bpm.ServiceComponentRequestInvoker;
import com.fujitsu.futurity.bp.x21.bpm.db.SessionHandle;
import com.fujitsu.futurity.bp.x21.bpm.exception.RequestParameterException;
import com.fujitsu.futurity.bp.x21.bpm.parameter.IRequestParameterReadWrite;
import com.fujitsu.futurity.bp.x21.cc.AbstractCommonComponent;
import com.fujitsu.futurity.bp.x21.cc.exception.SCCallException;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;

/**
 * 在庫出荷機器引当一覧情報取得の検索処理を行います。
 * @author user
 */
public class JDKCommon03CC extends AbstractCommonComponent
{
	/**
	 * テンプレートID
	 */
	public static final String TEMPLATE_ID = "EDK0011B060";

	/**
	 * マッパー
	 */
	private static final DKSV0074_DKSV0074OP_EDK0011B060BSMapper MAPPER = new DKSV0074_DKSV0074OP_EDK0011B060BSMapper();

	/**
	 * 在庫出荷機器引当一覧再検索処理を行います。
	 * @param handle セッションハンドル
	 * @param param パラメータ
	 * @param fixedText 定型文字
	 * @return パラメータ
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public IRequestParameterReadWrite getZaikoShkaKikiSearch(SessionHandle handle, 
			IRequestParameterReadWrite param, String fixedText) throws Exception
	{
		
		// ◇ 上りマッピング処理
		// 上りマッピングの処理結果を取得するためのマップを生成
		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		// リクエストパラメータを引数に上りマッピング処理メソッドを実行
		paramMap = editInMsg(param);

		// ◇ SC呼出実行
		// SC呼び出し部品のインスタンス生成（引数にはログに出力するクラス名を渡す。空文字を設定した場合はログに出力されない）
		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
		JSYejbLog.println(JSYejbLog.DEBUG, this.getClass(), "在庫出荷機器引当一覧照会処理の実行");
		Map<?, ?> scResult = scCall.run(paramMap, handle);

		// ◇ 下りマッピング処理
		IRequestParameterReadWrite result = editOutMsg(scResult, param);
		
		if (JDKBPCommon.hasError(param))
		{
			throw new SCCallException("在庫出荷機器引当一覧照会処理失敗", "0", 
					Integer.parseInt(param.getControlMapData("returnCode").toString()));
		}
		
		JDKStockMap stocks = new JDKStockMap();

		Map dataMap = (Map)param.getData("DKSV007401CC");
		List<Map> dataList = (List)dataMap.get("EDK0011B060CBSMsg1List");
		for (Map data : dataList) {
			String warehouse = (String)dataMap.get("key_soko_cd");
			String shelf = (String)data.get("shelf_cd");
			String modelNo = (String)data.get("taknkiki_model_cd");
			String idomotoKriplaceSkcd = (String)data.get("idomoto_kriplace_skcd");
			String [] keies = {warehouse, shelf, modelNo, idomotoKriplaceSkcd};
			
			Integer moves = Integer.parseInt(data.get("cnt").toString());
			
			if (!stocks.hasKey(keies)) {
				// 宅内機器物流在庫一意照会(EKKA0020005)を実行し、在庫情報を得る
				stocks.setValue(JDKBPCommon.getStockAmount(warehouse, shelf, modelNo, idomotoKriplaceSkcd, handle, param), keies);
			}
			
			stocks.decrease(moves, keies);
			
			data.put("output_kyoka", stocks.isNegative(keies) ? 0L : 1L);
		}
		
		return result;
	}

	/**
	 * 在庫出荷機器引当一覧取得処理チェックを行います。
	 * @param handle セッションハンドル
	 * @param param パラメータ
	 * @param fixedText 定型文字
	 * @return マッピングパラメータ
	 * @throws RequestParameterException
	 */
	public  HashMap<String, Object> getInvokeCBS(SessionHandle handle
			, IRequestParameterReadWrite param, String fixedText) throws RequestParameterException 
	{
		// 在庫出荷機器引当一覧取得処理用メッセージ設定処理
		return editInMsg(param);
	}

	/**
	 * 在庫出荷機器引当一覧取得処理用メッセージ設定処理を行います。
	 * @param param パラメータ
	 * @return マッピングパラメータ
	 * @throws RequestParameterException
	 */
	private HashMap<String, Object> editInMsg(IRequestParameterReadWrite param) throws RequestParameterException
	{
		try
		{
			return MAPPER.editInMsg(param);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}

	/**
	 * 在庫出荷機器引当一覧取得処理用メッセージ取得処理を行います。
	 * @param msgList メッセージリスト
	 * @param param パラメータ
	 * @return パラメータ
	 * @throws Exception
	 */
	private IRequestParameterReadWrite editOutMsg(Map<?, ?> msgList,
			IRequestParameterReadWrite param) throws Exception
	{
		try
		{
			return MAPPER.editResultRP(msgList, param);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}

	/**
	 * サービスコンポーネント実行後に、IRequestParameterReadWriteにエラー情報をマッピングします。
	 * @param param パラメータ
	 * @param templates テンプレート
	 * @param returnCode リターンコード
	 * @return パラメータ
	 * @throws RequestParameterException
	 */
	public IRequestParameterReadWrite editErrorInfo(IRequestParameterReadWrite param
			, CAANMsg[] templates, int returnCode) throws RequestParameterException
	{
		try
		{
			return MAPPER.editErrorInfo(param, templates, returnCode);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}
}
