/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatZMCisAdSend
*	ソースファイル名	：JBSbatZMCisAdSend.java
*	作成者				：富士通　
*	作成日				：2015年06月08日
*＜機能概要＞
*	CIS向け住所マスタデータファイル送信部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v16.00.00   2015/06/08	FJ)頼経		【ANK-2480-00-00】対応 新電力
*	v16.00.01   2015/07/17	FJ)頼経		【IT1-2015-0000060】対応 新電力
*	v16.00.02   2016/01/22	FJ)寺園		【IT1-2016-0000011】対応 新電力（ESB送信ジョブでの不整合）
*********************************************************************/
package eo.business.service;

import java.io.File;
import java.util.ArrayList;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JBSbatZMFileReaderUtil;
import eo.business.common.JZMBatCommon;
import eo.business.common.JZMBatConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* 住所マスタデータファイル（CIS向け）送信処理クラスです <p>
*<BR>
* @author 富士通
*/
public class JBSbatZMCisAdSend extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** 中間ファイル名 */
	private static final String ZMIFM034001 = "ZMIFM034001.csv";
	
	// IT1-2015-0000060 DEL START
	// /** 連携ファイル名先頭部 */
	// private static final String ZMIFE057_CSV = "ZMIFE057_";
	// IT1-2015-0000060 DEL END
	
	/** 取得日付 */
	private String opeDateStr = null;
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		// 他システムへ送信するファイルの配置先を取得
		String inPath = JZMBatCommon.getGaibuSendDir(JZMBatConst.ZMIFE057_DIR);
		
		// ファイル名を取得
		// IT1-2015-0000060 MOD START
		//String inFileNm = JZMBatCommon.getFileNameDatePattern(inPath, ZMIFE057_CSV, JZMBatConst.YYYYMMDDHHMMSS, JZMBatConst.CSV);
		String inFileNm = JZMBatCommon.getFileNameDatePattern(inPath, JZMBatConst.ZMIFE057, "", JZMBatConst.CSV);
		// IT1-2015-0000060 MOD END
		
		File f_dataFile = new File(inPath + inFileNm);
		
		if(inFileNm == null || JZMBatConst.BLANK.equals(inFileNm) || !f_dataFile.exists())
		{
			// CSVファイルが存在しない場合はエラー
			String midFileStr = JZMBatCommon.getMidDir() + ZMIFM034001;
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EZMB0140CW, new String[]{midFileStr});
		}
		
		// 拡張子以外のファイル名を取得
		opeDateStr = inFileNm.substring(0, inFileNm.indexOf(JZMBatConst.CSV));
		
		// IT1-2016-0000011 DEL START
//		// データカウント
//		int cnt = getFileRowCnt(inPath + inFileNm);
		// IT1-2016-0000011 DEL END
		
		StringBuffer sb = new StringBuffer();
		String inFile = sb.append(inPath).append(opeDateStr).toString();
		
		// データファイルパス
		String dataFilePath = inPath + inFileNm;
		
//		// IT1-2016-0000011 DEL START
//		// カウントファイルパス
//		String cntFilePath = inFile + JZMBatConst.CNT;
//		
//		// フラグファイルパス
//		String flgFilePath = inFile + JZMBatConst.FLG;
//		
//		JZMBatCommon.printDoubleQuoteCntFile(cntFilePath, cnt, 1);
//		JZMBatCommon.printDoubleQuoteFlgFile(flgFilePath, 1);
		// IT1-2016-0000011 DEL END
		
		super.logPrint.printDebugLog("他システム送信ディレクトリパス:" + inPath);
		super.logPrint.printDebugLog("他システム送信データファイルフルパス:" + dataFilePath);
//		// IT1-2016-0000011 DEL START
//		super.logPrint.printDebugLog("他システム送信件数ファイルフルパス:" + cntFilePath);
//		super.logPrint.printDebugLog("他システム送信フラグファイルフルパス:" + flgFilePath);
//		// IT1-2016-0000011 DEL END
		
		// ファイルパス格納用リスト
		ArrayList<String> filePathList = new ArrayList<String>();
		
		filePathList.add(dataFilePath);
//		// IT1-2016-0000011 DEL START
//		filePathList.add(cntFilePath);
//		filePathList.add(flgFilePath);
//		// IT1-2016-0000011 DEL END
		
		// 他システムに対するFTP転送
		JZMBatCommon.putFTPGaibu(commonItem, filePathList, JZMBatConst.ZMIFE057);
		
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/**
	 * ファイル内の行数を取得
	 * @param filePath filePath
	 * @return 件数
	 * @throws Exception
	 */
	private int getFileRowCnt(String filePath) throws Exception
	{
		int cnt = 0;
		JBSbatZMFileReaderUtil br = new JBSbatZMFileReaderUtil(filePath);

		while(br.readLine() != null)
		{
			cnt++;
		}
		br.close();

		return cnt;
	}
	
}
