/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom						 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKAuthRsltFileBnkt
*	ソースファイル名	：JBSbatKKAuthRsltFileBnkt.java
*	作成者				：富士通　
*	作成日				：2017年12月18日
*＜機能概要＞
*　クリアパスより受信したオーソリ結果ファイルを「オーソリ結果ファイル（eo）」、
*「オーソリ結果ファイル送信（mineo）」に分割する。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v36.0.0	    2017/12/18  FJ)田中 	新規作成【ANK-3313-00-00】クレジットカード非通過対応
*	v45.00.00	2019/07/18	FJ)上間		【ANK-3602-00-00】特定クレジットカードの売上オーソリ実施対応
**********************************************************************/
package eo.business.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JKKBatConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.util.JBSbatStringUtil;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKAuthRsltFileBnkt extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/**
	 * 事業者コード
	 */
	protected String jigyosha_cd = "";
	
	/**
	 * 顧客ID
	 */
	protected String cust_id = "";
	
	/**
	 * 仕向先会社コード
	 */
	protected String shikosaki_comp_cd = "";
	
	/**
	 * カードブランドコード
	 */
	protected String card_brand_cd = "";
	
	/**
	 * 問合せ用カード番号
	 */
	protected String toiawase_card_no = "";
	
	/**
	 * クレジットカード有効期限
	 */
	protected String crecard_yk_kigen = "";
	
	/**
	 * オーソリ承認番号
	 */
	protected String authori_shonin_no = "";
	
	/**
	 * クレジットカード番号預りID
	 */
	protected String crecard_no_azkri_id = "";
	
	/**
	 * クリアパス取引ID
	 */
	protected String clearpass_deal_id = "";
	
	/**
	 * カード識別コード
	 */
	protected String card_skcd = "";
	
	/**
	 * MINEO識別コード
	 */
	protected String mineo_skbt_cd = "";
	
	/**
	 * 発送日
	 */
	protected String hasso_niti = "";
	
	/**
	 * 送付日
	 */
	protected String sohu_niti = "";
	
	/**
	 * MINEO識別コードフラグ
	 */
	private static final String 	MINEO_SKBT_CD_1 = "1";
	
	/**
	 * ファイル名（拡張子）
	 */
	private static final String CSV_NM = ".csv";
		
	/**
	 * オーソリ結果ファイル（eo）のファイル格納場所
	 */
	private String send_eo_dir = "";

	/**
	 * オーソリ結果ファイル（mineo）ファイル格納場所
	 */
	private String send_mineo_dir = "";
	
	/**
	 * 分割元ファイルパス
	 */
	private String in_file_path = "";
	
	/**
	 * オーソリ結果ファイル（eo）のファイルID
	 */
	private String file_id_eo = "";
	
	/**
	 * オーソリ結果ファイル（mineo）のファイルID
	 */
	private String file_id_mineo = "";

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		//FREEITEMを取得する
		String[] free_item = commonItem.getFreeItem().split(";");
		
		// 分割ファイル格納場所を取得する
		// オーソリ結果ファイル（eo）
		send_eo_dir = free_item[0] + "/";
		
		// オーソリ結果ファイル（mineo）
		send_mineo_dir = free_item[1] + "/";
		
		// 分割元入力ファイルパスを取得する
		in_file_path = free_item[2];
		
		// オーソリ結果ファイル（eo）のファイルIDを取得する
		file_id_eo = free_item[3];
		
		// オーソリ結果ファイル（mineo）のファイルIDを取得する
		file_id_mineo = free_item[4];
		
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		super.logPrint.printDebugLog("execute_START");
		
		// エラー時の解析のためのカウンタ、業務ログに出力する
		long count =  0;
		BufferedReader in_reader = null;
		BufferedWriter out_writer1 = null;
		BufferedWriter out_writer2 = null;
		
		try
		{
			//入力ファイルの作成
			in_reader = new BufferedReader(new InputStreamReader(new FileInputStream(in_file_path), JKKBatConst.S_CHR_CODE));

			// ファイル名を編集する
			// オーソリ結果ファイル（eo）
			String out_file1 = send_eo_dir + file_id_eo + CSV_NM;
			// オーソリ結果ファイル（mineo）
			String out_file2 = send_mineo_dir + file_id_mineo + "/" + file_id_mineo + CSV_NM;

			// 分割ファイルの生成
			out_writer1 = createAuthRltFile(out_file1);
			out_writer2 = createAuthRltFile(out_file2);

			while (in_reader.ready())
			{
				count++;

				// 一行読み込む
				String rline = in_reader.readLine();

				// 読み込んだデータを取得
				String data[] = rline.split(",",0);

				// 
// ANK-3602-00-00 MOD START
//				if (data.length <= 13 && data.length >= 11)
				if (data.length <= 16 && data.length >= 14)	
// ANK-3602-00-00 MOD END
				{
					// MINEO識別コードを取得する
					String mineoSkbtCd = JBSbatStringUtil.trim(rline.split(",")[10]);

					// MINEO識別コードが"1"の場合
					if (MINEO_SKBT_CD_1.equals(mineoSkbtCd))
					{
						out_writer2.write(rline);
						out_writer2.newLine();
						continue;
					}
				}
				// MINEO識別コードが"1"以外の場合
				out_writer1.write(rline);
				out_writer1.newLine();
			}
		}
		catch(IOException ioe)
		{
			super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.EKKB1000CE, new String[] {in_file_path + "(" + count + "行目)"});
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EDKB0050CE, new String[]{ioe.getMessage()});
		}
		finally
		{
			if (in_reader != null)
			{
				in_reader.close();
			}
			
			if (out_writer1 != null)
			{
				out_writer1.close();
			}
			if (out_writer2 != null)
			{
				out_writer2.close();
			}
		}
		super.logPrint.printDebugLog("execute_END");
		return null;	
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/**
	 * ファイルクラスの生成処理を行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.分割オーソリ結果ファイル名を設定します。<br>
	 * 2.ファイル生成をします。<br>
	 * </pre>
	 * <p>
	 * @param value 分割ファイル名
	 * @return BufferedWriter 出力ファイルクラス
	 * @throws Exception 例外
	 */
	private BufferedWriter createAuthRltFile(String value) throws Exception
	{
		// 出力ファイルを作成
		// 出力ファイルの文字列指定「UTF-8」を行う
		BufferedWriter out_writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(value), JKKBatConst.S_CHR_CODE));
		return out_writer;
	}
}
