/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatFUWebMskmSeniSaiSunk
*	ソースファイル名	：JBSbatFUWebMskmSeniSaiSunk.java
*	作成者				：富士通
*	作成日				：2012年06月23日
*＜機能概要＞
*　ＷＥＢデータ申込遷移数再集計処理部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2012/06/23   富士通		新規作成
*********************************************************************/
package eo.business.service;

import java.io.IOException;
import java.util.ArrayList;

import eo.business.common.JBSbatBusinessService;
import eo.common.constant.JFUStrConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.db.JBSbatSQLAccess;
import eo.framework.file.JBSbatInputFileUtil;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatFUWebMskmSeniSaiSunk extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** テーブル(Ｗｅｂ申込集計結果)*/
	private static final String D_TBL_NAME_FU_T_WEBMK_SRLT = "FU_T_WEBMK_SRLT";

	/** SQL定義キー(FU_SELECT_001)*/
	private static final String FU_T_WEBMK_SRLT_FU_SELECT_001 = "FU_SELECT_001";

	/** SQL定義キー(FU_INSERT_001)*/
	private static final String FU_T_WEBMK_SRLT_FU_INSERT_001 = "FU_INSERT_001";

	/** SQL定義キー(FU_UPDATE_001)*/
	private static final String FU_T_WEBMK_SRLT_FU_UPDATE_001 = "FU_UPDATE_001";

	/** テーブルアクセスクラス(Ｗｅｂ申込集計結果)*/
	private JBSbatSQLAccess db_FU_T_WEBMK_SRLT = null;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/**スキーマ名称 */
	private static final String WEB_MSKM_SHUK_RESULT = "Web申込集計結果";

	/**メッセージID */
	private static final String EFUB0030KE = JPCBatchMessageConstant.EFUB0030KE;

	/**メッセージID */
	private static final String EFUB0060AI = JPCBatchMessageConstant.EFUB0060AI;

	/**メッセージID */
	private static final String EFUB0070AI = JPCBatchMessageConstant.EFUB0070AI;

	/** メッセージキー */
	private static final String EFUB0210KE = JPCBatchMessageConstant.EFUB0210KE;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// DBアクセスクラスを生成します
		db_FU_T_WEBMK_SRLT = new JBSbatSQLAccess(commonItem, D_TBL_NAME_FU_T_WEBMK_SRLT);

		// パラメータ読み出し
		String strFreeParam = commonItem.getFreeItem();

		// パラメータを区切り文字で分割する
		String[] lines = strFreeParam.split(JFUStrConst.S_PARAM_DELIM);
		// パラメータチェック
		if(lines.length != 1 || ("").equals(lines[0]))
		{
			throw new JBSbatBusinessException(EFUB0030KE);
		}
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/

		int iInsCnt = 0;		// Web申込集計結果登録件数
		int iUpdCnt = 0;		// Web申込集計結果更新件数

		// FREEパラメータ取得(入力中間ファイルパス)
		String strFreeParam = commonItem.getFreeItem();

		// パラメータを区切り文字で分割する
		String[] lines = strFreeParam.split(JFUStrConst.S_PARAM_DELIM);

		// パラメータ取得
		String strFreeParam1 = lines[0];	// ＳＯＲＴ済ＷＥＢ申込画面遷移結果ファイルを読込み

		// 中間ファイル読み込み
		ArrayList<String> sContMstList = null;
		try {
			sContMstList = readFile(strFreeParam1);
		} catch (Exception e) {
			throw new JBSbatBusinessException(EFUB0210KE,
					new String[] { strFreeParam1 });
		}
		// 入力ファイルにレコードがない場合はそのまま終了。
		if(sContMstList.size() == 0)
		{
			return null;
		}

		int iShukCnt = 0;					// 導線申込件数(退避兼用)
		String strTaihiShukTaiYmd = "";		// 退避用集計対象年月日
		String strTaihiShukTaiCd = "";		// 退避用Web申込集計対象種別コード
		String strTaihiWebMskm = "";		// 退避用Web申込集計対象値

		// 中間ファイルの件数分処理を繰り返す
		for(int i = 0; i < sContMstList.size(); i++)
		{

			// 読込ファイルのレコードを分割する。
			String[] filelines = sContMstList.get(i).split(JFUStrConst.S_DELIM_TAB);

			String strShukTaiYmd = filelines[0];		// 集計対象年月日
			String strShukTaiCd = filelines[1];			// Web申込集計対象種別コード
			String strWebMskm = filelines[2];			// Web申込集計対象値
			String strDsenCnt = filelines[3];			// 導線件数

			/* 前処理Web申込集計対象値と異なっている場合は処理済み分のファイルレコードを登録し、
			カウンターをリセットする。 */
			if(i > 0)
			{
				if(!strTaihiWebMskm.equals(strWebMskm))
				{
					// 読込レコードの存在チェック(Web申込集計結果)
					String strCnt = selectFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm);

					// SELECT結果が取得できた場合は更新、取得できない場合は登録を行う。
					if(JFUStrConst.S_ZERO.equals(strCnt))
					{
						insertFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm, iShukCnt);
						iInsCnt++;
					}
					else
					{
						updateFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm, iShukCnt);
						iUpdCnt++;
					}
					// カウンターリセット
					iShukCnt = 0;
				}
			}

			strTaihiShukTaiYmd = strShukTaiYmd;		// 処理済みのWeb申込集計対象値を退避
			strTaihiShukTaiCd = strShukTaiCd;		// 集計対象コード
			strTaihiWebMskm = strWebMskm;			// 処理済みのWeb申込集計対象値を退避
			iShukCnt = iShukCnt + Integer.parseInt(strDsenCnt);	// 導線申込件数をカウントする

			/* 入力ファイル最終行の場合、
			レコード出力する */
			if(i == sContMstList.size() - 1)
			{
				// 読込レコードの存在チェック(Web申込集計結果)
				String strCnt = selectFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm);

				// SELECT結果が取得できた場合は更新、取得できない場合は登録を行う。
				if(JFUStrConst.S_ZERO.equals(strCnt))
				{
					insertFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm, iShukCnt);
					iInsCnt++;
				}
				else
				{
					updateFU_T_WEBMK_SRLT(strTaihiShukTaiYmd, strTaihiShukTaiCd, strTaihiWebMskm, iShukCnt);
					iUpdCnt++;
				}
			}
		}

		super.logPrint.printBusinessErrorLog(EFUB0060AI, new String[]{WEB_MSKM_SHUK_RESULT, String.valueOf(iInsCnt)});
		super.logPrint.printBusinessErrorLog(EFUB0070AI, new String[]{WEB_MSKM_SHUK_RESULT, String.valueOf(iUpdCnt)});

		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		// DBアクセスクラスをクローズします
		db_FU_T_WEBMK_SRLT.close();
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/

	/**
	 * SQLKEY(FU_SELECT_001)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 *
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	集計対象申込年月日
	 *		 	Web申込集計種別コード
	 *		 	Web申込集計対象値
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeFU_T_WEBMK_SRLT_FU_SELECT_001(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());
		paramList.setValue(param[2].toString());

		// DBアクセスを実行します
		db_FU_T_WEBMK_SRLT.selectBySqlDefine(paramList, FU_T_WEBMK_SRLT_FU_SELECT_001);
	}

	/**
	 * SQLKEY(FU_INSERT_001)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 *
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	集計対象申込年月日
	 *		 	Web申込集計種別コード
	 *		 	Web申込集計対象値
	 *		 	申込件数(ホームネット)
	 *		 	申込件数(ホームテレビ)
	 *		 	申込件数(ホーム電話)
	 *		 	申込件数(マンションネット)
	 *		 	申込件数(マンションテレビ)
	 *		 	申込件数(マンション電話)
	 *		 	申込件数(メゾンネット)
	 *		 	申込件数(メゾンテレビ)
	 *		 	申込件数(メゾン電話)
	 *		 	申込件数(モバイル)
	 *		 	申込件数(合計)
	 *		 	割引対象申込件数
	 *		 	画面別離脱件数
	 *		 	導線別離脱件数
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeFU_T_WEBMK_SRLT_FU_INSERT_001(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());
		paramList.setValue(param[2].toString());
		paramList.setValue(param[3].toString());
		paramList.setValue(param[4].toString());
		paramList.setValue(param[5].toString());
		paramList.setValue(param[6].toString());
		paramList.setValue(param[7].toString());
		paramList.setValue(param[8].toString());
		paramList.setValue(param[9].toString());
		paramList.setValue(param[10].toString());
		paramList.setValue(param[11].toString());
		paramList.setValue(param[12].toString());
		paramList.setValue(param[13].toString());
		paramList.setValue(param[14].toString());
		paramList.setValue(param[15].toString());
		paramList.setValue(param[16].toString());

		// DBアクセスを実行します
		db_FU_T_WEBMK_SRLT.executeBySqlDefine(paramList, FU_T_WEBMK_SRLT_FU_INSERT_001);
	}

	/**
	 * SQLKEY(FU_UPDATE_001)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 *
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *		 	申込件数(ホームネット)
	 *		 	申込件数(ホームテレビ)
	 *		 	申込件数(ホーム電話)
	 *		 	申込件数(マンションネット)
	 *		 	申込件数(マンションテレビ)
	 *		 	申込件数(マンション電話)
	 *		 	申込件数(メゾンネット)
	 *		 	申込件数(メゾンテレビ)
	 *		 	申込件数(メゾン電話)
	 *		 	申込件数(モバイル)
	 *		 	申込件数(合計)
	 *		 	割引対象申込件数
	 *		 	画面別離脱件数
	 *		 	導線別離脱件数
	 *		 	集計対象申込年月日
	 *		 	Web申込集計種別コード
	 *		 	Web申込集計対象値
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeFU_T_WEBMK_SRLT_FU_UPDATE_001(Object[] param) throws Exception
	{
		// バイント変数のリストを生成します
		JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
		paramList.setValue(param[0].toString());
		paramList.setValue(param[1].toString());
		paramList.setValue(param[2].toString());
		paramList.setValue(param[3].toString());
		paramList.setValue(param[4].toString());
		paramList.setValue(param[5].toString());
		paramList.setValue(param[6].toString());
		paramList.setValue(param[7].toString());
		paramList.setValue(param[8].toString());
		paramList.setValue(param[9].toString());
		paramList.setValue(param[10].toString());
		paramList.setValue(param[11].toString());
		paramList.setValue(param[12].toString());
		paramList.setValue(param[13].toString());
		paramList.setValue(param[14].toString());
		paramList.setValue(param[15].toString());
		paramList.setValue(param[16].toString());

		// DBアクセスを実行します
		db_FU_T_WEBMK_SRLT.executeBySqlDefine(paramList, FU_T_WEBMK_SRLT_FU_UPDATE_001);
	}
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/**
	 * ファイル読み込み処理を行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数で読込ファイルディレクトリ(フルパス)を設定します。<br>
	 *
	 * 2.引数を元にファイル読込を行い、行をListへ格納します。<br>
	 *
	 * </pre>
	 * <p>
	 * @param strFileDir    出力ディレクトリ(フルパス)。
	 * @return resultList   返却用List
	 * @throws Exception    業務サービス内で発生した例外全般。
	 */
	private static ArrayList<String> readFile(String strFileDir) throws Exception
	{
		ArrayList<String> resultList = new  ArrayList<String>();

		// 外部出力ファイルパス
		JBSbatInputFileUtil inFile = null;

		try
		{
			inFile = new JBSbatInputFileUtil(strFileDir);
			inFile.setEncode(JFUStrConst.SJIS);						// 文字コード
			inFile.setLine(JFUStrConst.S_LINE_SEPARAOR_LF);			// 改行コード
			inFile.createReader();

			while(true)
			{
				String line = inFile.readLine();
				if(line == null)
				{
					break;
				}
				resultList.add(line);
			}

			return resultList;
		}
		catch (IOException e)
		{
			throw e;
		}
		finally
		{
			if (null != inFile)
			{
				// ファイルオブジェクトをクローズ
				inFile.close();
			}
		}
	}

	/**
	 * 対象データをWeb申込集計結果へ登録します。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数をリストに格納します。<br>
	 *
	 * 2.executeFU_T_WEBMK_SRLT_FU_INSERT_001を実行します。<br>
	 * </pre>
	 * <p>
	 * @param strShukTaiYmd			集計対象年月日
	 * @param strShukTaiCd			集計対象種別コード
	 * @param webMskmTai			Web申込対象コード
	 * @param iShukCnt				集計カウント
	 * @throws Exception			業務サービス内で発生した例外全般。
	 */
	private void insertFU_T_WEBMK_SRLT(String strShukTaiYmd, String strShukTaiCd, String webMskmTai,
			int iShukCnt) throws Exception
	{
		// Web申込集計結果へ更新
		JBSbatCommonDBInterface dbList2 = new JBSbatCommonDBInterface();
		dbList2. setValue(strShukTaiYmd);
		dbList2. setValue(strShukTaiCd);
		dbList2. setValue(webMskmTai);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(iShukCnt);

		executeFU_T_WEBMK_SRLT_FU_INSERT_001(dbList2.getList().toArray());
	}

	/**
	 * 対象データをWeb申込集計結果へ登録します。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数をリストに格納します。<br>
	 *
	 * 2.executeFU_T_WEBMK_SRLT_FU_UPDATE_001を実行します。<br>
	 * </pre>
	 * <p>
	 * @param strShukTaiYmd			集計対象年月日
	 * @param strShukTaiCd			集計対象種別コード
	 * @param webMskmTai			Web申込対象コード
	 * @param iShukCnt				集計カウント
	 * @throws Exception			業務サービス内で発生した例外全般。
	 */
	private void updateFU_T_WEBMK_SRLT(String strShukTaiYmd, String strShukTaiCd, String webMskmTai,
			int iShukCnt) throws Exception
	{
		// Web申込集計結果へ更新
		JBSbatCommonDBInterface dbList2 = new JBSbatCommonDBInterface();
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(null);
		dbList2. setValue(iShukCnt);
		dbList2. setValue(strShukTaiYmd);
		dbList2. setValue(strShukTaiCd);
		dbList2. setValue(webMskmTai);

		executeFU_T_WEBMK_SRLT_FU_UPDATE_001(dbList2.getList().toArray());
	}

	/**
	 * 対象データが存在するかWeb申込集計結果を検索します。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数をリストに格納します。<br>
	 *
	 * 2.executeFU_T_WEBMK_SRLT_FU_UPDATE_001を実行します。<br>
	 * </pre>
	 * <p>
	 * @param strShukTaiYmd			集計対象年月日
	 * @param strShukTaiCd			集計対象種別コード
	 * @param webMskmTai			Web申込対象コード
	 * @return						件数
	 * @throws Exception			業務サービス内で発生した例外全般。
	 */
	private String selectFU_T_WEBMK_SRLT(String strShukTaiYmd, String strShukTaiCd, String webMskmTai) throws Exception
	{
		// 総括月別サマリーへ更新
		JBSbatCommonDBInterface dbList1 = new JBSbatCommonDBInterface();
		dbList1. setValue(strShukTaiYmd);
		dbList1. setValue(strShukTaiCd);
		dbList1. setValue(webMskmTai);

		executeFU_T_WEBMK_SRLT_FU_SELECT_001(dbList1.getList().toArray());
		String strCnt = db_FU_T_WEBMK_SRLT.selectNext().getString("CNT");

		return strCnt;
	}
}
