/*********************************************************************
*  All Rights reserved,Copyright (c) Fujitsu ,2015					 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKKaiyakuRetaHakkoSend
*	ソースファイル名	：JBSbatKKKaiyakuRetaHakkoSend.java
*	作成者				：富士通　
*	作成日				：2015年10月23日
*＜機能概要＞
*　解約レター発行ファイル送信部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v20.00.00	2015/10/23  FJ）栗秋	【ANK-2617-00-00】YSDへのＨＵＬＦＴ連携巻き取り対応
*	v20.00.01	2015/11/19  FJ）栗秋	【ST-2015-0000065】障害対応
*	v54.00.00	2021/09/01	FJ)星野		ANK-4069-00-00_バッチ・DBサーバーリプレース対応
*********************************************************************/
package eo.business.service;

import java.io.File;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCBatCommon;
import eo.business.common.JCCbatShoriIraiUtil;
import eo.business.common.JKKBatConst;
import eo.common.constant.JKKStrConst;
import eo.framework.application.JCCbatFrameworkException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKKaiyakuRetaHakkoSend extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 運用日付 */
	private String unyoDate = null;
	
	/** 記号(セミコロン) */
	private static final String DELIM  = JKKBatConst.S_PARAM_DELIM;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
		/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// 運用日付を取得
		unyoDate = commonItem.getOpeDate();

		/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		// 起動パラメータ FREE より、インターフェースIDを取得します。
		if(super.freeItem != null)
		{
			String[] freeItems = super.freeItem.split(DELIM);

			// 変数宣言
			String ifId = freeItems[0];			// FTP転送依頼するインターフェイスID
			File bfFile = new File(freeItems[1]);
// ST-2015-0000065 MOD START
//			File afFile = new File(bfFile.getPath().replace(bfFile.getName(),ifId + ".csv"));
			File afFile = new File(bfFile.getPath().replace(bfFile.getName(),ifId + ".txt"));
// ST-2015-0000065 MOD END
			String shoriIraiNo = bfFile.getName().replace(ifId + "_", ""); // ディレイド処理依頼番号
// ST-2015-0000065 MOD START
//			shoriIraiNo = shoriIraiNo.replace(".csv", "");
			shoriIraiNo = shoriIraiNo.replace(".txt", "");
// ST-2015-0000065 MOD END
			try
			{
				if (Integer.parseInt(freeItems[2]) == 0)
				{
					//ファイル送信処理
					bfFile.renameTo(afFile); //送信用のファイル名にリネーム

// ANK-4069-00-00対応 20210901 星野 DEL START
//					ArrayList<String> fileNameList = new ArrayList<String>();
//// ST-2015-0000065 MOD START
////					fileNameList.add(bfFile.getPath()); // 連携するファイル名を設定
//					fileNameList.add(afFile.getPath()); // 連携するファイル名を設定
//// ST-2015-0000065 MOD START
//					HashMap<String, Object> resultMap = new HashMap<String, Object>(200);
//					boolean resultBln = true;
//
//					resultBln = JCCBatCommon.putFTP(super.commonItem, fileNameList, ifId,  resultMap);
//					// エラー発生時
//					if (!resultBln)
//					{
//						String errKbn = "";		// エラー区分
//						String errCd = "";		// エラーコード
//						String fileName = "";	//送信ファイル名
//						HashMap<String, Object> shosai = new HashMap<String, Object>(200);		// エラー詳細
//
//						errKbn = resultMap.get("ERR_KBN").toString();
//
//						// 接続エラーの場合
//						if ("1".equals(errKbn))
//						{
//							shosai = (HashMap)resultMap.get("CONNECT_ERR");
//							if(shosai != null)
//							{
//								errCd = shosai.get("ERR_CD").toString();  // 接続エラーのときはエラーコードを設定
//							}
//							super.logPrint.printDebugLog("ファイル転送失敗" + errCd);
//							throw new JBSbatBusinessException("ESCB0110CE", new String[] {errCd});
//						}
//						if ("2".equals(errKbn))
//						{
//							// ファイルエラーの場合
//							ArrayList<HashMap> fileErr = (ArrayList<HashMap>)resultMap.get("FILE_ERR");
//							shosai = (HashMap)fileErr.get(0);
//							if(shosai != null)
//							{
//								errCd = shosai.get("ERR_CD").toString();  // 接続エラーのときはエラーコードを設定
//								fileName = shosai.get("FILE_NAME").toString();  // ファイルエラーのときは、ファイル名を設定
//							}
//							super.logPrint.printDebugLog("ファイル転送失敗" + "ファイル名" + fileName + "エラーコード" + errCd);
//							throw new JBSbatBusinessException("ESCB0010CE", new String[] {fileName, errCd});
//						}
//					}
// ANK-4069-00-00対応 20210901 星野 DEL END
					return null;
				}
				else if(Integer.parseInt(freeItems[2]) == 1)
				{
					//ファイル送信完了処理
					//ディレイド処理依頼結果更新を行う。
					JCCBatCommon.updateShoriIraiResult(commonItem, shoriIraiNo, JKKStrConst.DLYD_TRN_RSLT_CD_COMPLETE, null);
					return null;
				}
			}
			catch(Exception e)
			{
				String errorFileName = null;
				//エラーファイル名を設定
				if(true == bfFile.exists()){
					//送信用ファイルをリネームする前にエラーが発生した場合
					errorFileName = bfFile.getPath().toString();
				}
				else if(true == afFile.exists())
				{
					//送信用ファイルをリネームした後にエラーが発生した場合
					errorFileName = afFile.getPath().toString();
				}
				// ディレイド処理依頼結果コードを"業務エラー"で更新
				JCCbatShoriIraiUtil.updateShoriIraiResult(commonItem, shoriIraiNo, JKKStrConst.DLYD_TRN_RSLT_CD_APP_ERR, errorFileName);
				// コミット実行
				this.commonItem.getConnection().commit();
				throw new JCCbatFrameworkException( "解約レター発行ファイル送信で例外が発生しました。" + e);
			}
		}
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
}