/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：KKA181SFLogic
*   ソースファイル名：KKA181SFLogic.java
*   作成者          ：富士通
*   日付            ：2018年01月25日
*＜機能概要＞
*   eo光テレビサービス契約情報照会(API)のビューロジックです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v36.00.00   2018/01/25   FJ          新規作成 【ANK-3324-00-00】 eo光TVアプリ対応
*
**********************************************************************/
package eo.web.webview.KKA181SF;

import static com.fujitsu.futurity.web.x31.X31SWebLog.DEBUG_LOG;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fujitsu.futurity.common.JSYLogBase;
import com.fujitsu.futurity.common.JSYwebLog;
import com.fujitsu.futurity.web.x00.JCCWebServiceException;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;

import eo.common.util.JPCEditString;
import eo.web.webview.JCCWebBusinessLogic;
import eo.web.webview.common.JCCWebCommon;
import eo.web.webview.common.JKKApiCommon;
import eo.web.webview.mapping.KKSV0887_KKSV0887OPDBMapper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * KKA181のビューロジックです。<p>
 * <BR>
 * @author 富士通
 */
public class KKA181SFLogic extends JCCWebBusinessLogic
{
	
	/**
	 * 業務処理名
	 */
	private static final String BUSINESS_LOGIC_NM = "eo光テレビサービス契約情報照会";
	
	/**
	 * eo光テレビサービス契約情報照会処理です。
	 * <br>
	 * @return 処理の成否
	 * @throws Exception エラーが発生した場合
	 */
	public boolean init() throws Exception
	{
		// サービス結果格納用サービスフォームBean
		X31SDataBeanAccess bean = null;

		//JSON形式のレスポンスデータを作成する。
		JSONObject jsonRspMap = new JSONObject();

		try
		{
			// サービス結果格納用サービスフォームBean取得
			bean = getServiceFormBean();
			
			// ■ 処理要求電文より、パラメータを取得する。
			HashMap reqMap = super.getRequestParams();

			JSYwebLog.println(JSYLogBase.EXECUTION, this.getClass(), null, "BPCON0I002",  new String[]{"KKA181_eo光テレビサービス契約情報照会_reqestMap= " + reqMap.toString()}, "");

			String receiveData = (String)reqMap.get("receiveData");
			String sysid = null;
			
			if (receiveData != null)
			{
				JSONObject jo = new JSONObject(receiveData);
				
				if (jo.has("SYSID"))
				{
					sysid = (String)jo.get("SYSID");
				}
			}
			
			// ■ 必須入力チェック
			if (JKKApiCommon.isNull(sysid))
			{
				DEBUG_LOG.debug("■必須入力チェックエラー");
				
				jsonRspMap.put("RETURN_CODE", "999999");
				jsonRspMap.put("RETURN_MSG", "システムエラー。");
				
				// 返却値の設定
				bean.sendMessageString(KKA181SFConst.RSP_MSG, X31CWebConst.DATABEAN_SET_VALUE, jsonRspMap.toString());
				return true;
			}
			
			// ■ 最大桁数チェック(SYSIDが10桁でない場合)
			if (10 != sysid.length())
			{
				DEBUG_LOG.debug("■最大桁数チェックエラー");
				
				jsonRspMap.put("RETURN_CODE", "999999");
				jsonRspMap.put("RETURN_MSG", "システムエラー。");
				
				// 返却値の設定
				bean.sendMessageString(KKA181SFConst.RSP_MSG, X31CWebConst.DATABEAN_SET_VALUE, jsonRspMap.toString());
				return true;
			}
			
			// ■ サービス呼び出し準備
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, "KKSV0887");
			
			HashMap<String, HashMap<String, Object>> inputMap = new HashMap<String, HashMap<String, Object>>();
			HashMap<String, HashMap<String, Object>> outputMap = new HashMap<String, HashMap<String, Object>>();
			
			KKSV0887_KKSV0887OPDBMapper mapper = new KKSV0887_KKSV0887OPDBMapper();
			mapper.setKKSV088701CC(reqMap, inputMap, "1", sysid);
			// ■ サービス呼出
			try
			{
				invokeService(paramMap, inputMap, outputMap);
			}
			catch(JCCWebServiceException jwse)
			{
				throw jwse;
			}
			
			// ■ サービス結果の転記
			HashMap<String, Object> simpleMap = new HashMap<String, Object>();
			
			mapper.getKKSV088701CC(simpleMap, outputMap);

			// 結果が取得できている場合
			if (!simpleMap.isEmpty())
			{
				List<Map> resultList = (List<Map>)simpleMap.get("record");
				
				JSONArray jsonTVKeiArray = new JSONArray();
				JSONArray jsonOpArray = new JSONArray();
				JSONObject jsonRec = new JSONObject();
				JSONObject jsonOp = new JSONObject();
				List<Map<String, String>> opList = null;
				
				for(Map<String, Object> rec:resultList)
				{
					// TV契約情報の設定
					setRespData(jsonRec, "PPLAN_CD_CR", rec.get("pplan_cd_cr"));
					setRespData(jsonRec, "ID_TCHISHO_PRT_YO_PPLAN_NM_CR", rec.get("id_tchisho_prt_yo_pplan_nm"));
					setRespData(jsonRec, "KEI_STAT_CD_CR", rec.get("svc_kei_stat"));
					setRespData(jsonRec, "STB_MODEL_NO", rec.get("taknkiki_model_cd"));
					setRespData(jsonRec, "STB_ID", rec.get("stb_id"));
					setRespData(jsonRec, "KEI_STAT_CD_STB", rec.get("kktk_svc_kei_stat"));

					opList = (List<Map<String, String>>)rec.get("op_channel_info_list");
					
					// オプションチャンネル情報リストの組み立て
					for(Map<String, String> opMap:opList)
					{
						jsonOp.put("PPLAN_CD_OP_CHANNEL", opMap.get("pplan_cd_op"));
						jsonOp.put("PPLAN_NM_CHANNEL", opMap.get("pplan_nm"));
						jsonOpArray.put(jsonOp);
						jsonOp = new JSONObject();
					}

					// オプションチャンネル情報リストを設定
					setRespData(jsonRec, "OP_CHANNEL_INFO_LIST", jsonOpArray);
					jsonOpArray = new JSONArray();
					
					jsonTVKeiArray.put(jsonRec);
					jsonRec = new JSONObject();
				}
				
				jsonRspMap.put("RETURN_CODE", "000000");
				jsonRspMap.put("EO_TV_KEI_INFO_LIST", jsonTVKeiArray);
				
				// 返却値の設定
				bean.sendMessageString(KKA181SFConst.RSP_MSG, X31CWebConst.DATABEAN_SET_VALUE, jsonRspMap.toString());
				return true;
			}
			// データが存在しない場合
			else
			{
				jsonRspMap.put("RETURN_CODE", "001000");
				jsonRspMap.put("RETURN_MSG", "データがありません。");
				// 返却値の設定
				bean.sendMessageString(KKA181SFConst.RSP_MSG, X31CWebConst.DATABEAN_SET_VALUE, jsonRspMap.toString());
				return true;
				
			}
		}
		catch (Exception e)
		{
			DEBUG_LOG.debug("■致命的なエラー発生");
			
			// ■例外トレースのログ出力
			JKKApiCommon.printDebugLog(e);
			
			jsonRspMap.put("RETURN_CODE", "999999");
			jsonRspMap.put("RETURN_MSG", "システムエラー。");
			
			// 返却値の設定
			bean.sendMessageString(KKA181SFConst.RSP_MSG, X31CWebConst.DATABEAN_SET_VALUE, jsonRspMap.toString());
			return true;
		}
	}
	
	/**
	 * 
	 * 返却用オブジェクトに値を設定（nullであった場合は空文字を設定）
	 * 
	 * @param jo	返却用オブジェクト
	 * @param key  項目キー
	 * @param val  値
	 * @throws Exception
	 */
	private void setRespData(JSONObject jo, String key, Object val) throws Exception {
		if (val != null)
		{
			jo.put(key, val);
		}
		else
		{
			jo.put(key, "");
		}
	}
}
