/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom                        *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKSetWriDataHrwk
*	ソースファイル名	：JBSbatKKSetWriDataHrwk.java
*	作成者				：富士通
*	作成日				：2018年06月25日
*＜機能概要＞
* セット割登録依頼データ振り分け部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v38.00.00	2018/06/25  FJ)森		【ANK-3436-00-00】eo光×eo電気×mineoセット割対応 新規作成
*********************************************************************/
package eo.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import eo.business.util.file.JBSbatKKIFE400001;
import eo.business.util.file.JBSbatKKIFE400002;
import eo.business.util.file.JBSbatKKIFM816;
import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JACbatParamUtil;
import eo.business.common.JBSbatBusinessService;
import eo.common.constant.JACStrConst;
import eo.framework.file.JBSbatDefFileUtil;
import eo.framework.file.JBSbatInputFileUtil;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.util.JBSbatAplConst;
import eo.framework.util.JBSbatCheckUtil;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKSetWriDataHrwk extends JBSbatBusinessService
{
	
	/** セット割管理システム対象キャンペーン情報 */
	private HashMap<String, String> wkSetWariCmpCdMap = null;
	
	/** 割引キャンペーンコードリスト */
	private ArrayList<String> wribCmpCdList = new ArrayList<String>();
	
	/** 商品券キャンペーンコードリスト */
	private ArrayList<String> shohinkCmpCdList = new ArrayList<String>();
	
	/** フリー項目情報格納用配列 */
	private String[] fleeItemArray = null;
	
	/** レコードの1項目目 */
	private String firstKmk = null;

	/** トレーラ部合計件数 */
	private int totalCnt = 0;

	/** ファイル定義 */
	private static final String PARAM_IND = "IND";
	
	/** トレーラ部レコード区分 */
	private static final String TRAILER_REC_DIV = "\"E\"";
	
	/** 文字コード(UTF-8) */
	private static final String UTF8 = "UTF-8";
	
	/** 区切り文字（カンマ） */
	private static final String COMMA = ",";

	/** キャンペーン種別（割引） */
	private static final String WRIB_CMP = "1";
	
	/** キャンペーン種別（商品券） */
	private static final String SHOHINK_CMP = "2";
	
	/** 出力共通電文クラス*/
	private JBSbatOutputItem outputBean = null;
	
	/** 定義ファイル（データ部） */
	private static final String DEF_FEILE_DATA = "KKIFE400001.def";

	/** 定義ファイル（トレーラ部） */
	private static final String DEF_FEILE_TRAILER = "KKIFE400002.def";
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		// 共通パラメータ設定
		super.setCommonInfo(commonItem);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial]");
		
		// 業務パラメータ取得
		// パラメータ取得部品の初期処理
		JACbatParamUtil paramUtil = new JACbatParamUtil(commonItem);
		
		// セット割管理システム対象キャンペーンコード
		wkSetWariCmpCdMap = paramUtil.getGyoumuParameterMap(JACStrConst.WKPARA_KK_SETWARI, super.opeDate);
		
		// パラメータ取得部品のクローズ処理
		paramUtil.close();
		
		// 業務パラメータ設定値リスト
		String[] wkParamValue = null;
		// 業務パラメータ設定値（キャンペーンコード）
		String valueCmpCd = null;
		// 業務パラメータ設定値（キャンペーン種類）
		String valueCmpKind = null;
		
		for(String value : wkSetWariCmpCdMap.values())
		{
			wkParamValue = value.split(COMMA);
			// 業務パラメータ設定値（キャンペーンコード）取得
			valueCmpCd = wkParamValue[0];
			// 業務パラメータ設定値（キャンペーン種類）取得
			valueCmpKind = wkParamValue[3];
			
			// 割引キャンペーンコードリストにキャンペーンコードを設定
			if(WRIB_CMP.equals(valueCmpKind))
			{
				wribCmpCdList.add(valueCmpCd);
			}
			// 商品券キャンペーンコードリストにキャンペーンコードを設定
			else if(SHOHINK_CMP.equals(valueCmpKind))
			{
				shohinkCmpCdList.add(valueCmpCd);
			}
		}
		
		// フリー項目情報を取得
		fleeItemArray = super.freeItem.split(JACStrConst.FREE_DIV);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][initial]");
		
		/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");

		// レコード件数
		int recCnt = 1;
		
		// データ部レコード数
		int dataRecCnt = 0;
		
		// トレーラ部レコード数
		int trailerRecCnt = 0;
		
		// ファイルエラーフラグ
		boolean fileErrFlg = false;
		
		// 行文字列
		String currentLine = null;
		
		// 行文字列配列
		String[] currentLineList = null;
		
		// 入力ファイルオブジェクト生成
		JBSbatInputFileUtil inFileObj = new JBSbatInputFileUtil(fleeItemArray[0]);
		inFileObj.setLine(JACStrConst.LINE_LF);
		inFileObj.setEncode(UTF8);
		inFileObj.createReader();
		
		// データ部とトレーラ部の定義ファイルオブジェクト生成
		JBSbatDefFileUtil dataDefFile = new JBSbatDefFileUtil(JBSbatAplConst.getAplConstValue(PARAM_IND) + DEF_FEILE_DATA, inFileObj);
		JBSbatDefFileUtil trailerDefFile = new JBSbatDefFileUtil(JBSbatAplConst.getAplConstValue(PARAM_IND) + DEF_FEILE_TRAILER, inFileObj);

		// 出力オブジェクト生成
		outputBean = new JBSbatOutputItem();
		
		// 出力情報生成
		JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
		
		// 入力レコード
		JBSbatServiceInterfaceMap inMap = null;
		// 入力レコードのキャンペーンコード
		String cmpCd = null;
		
		// 入力ファイルのレコード件数分繰り返す
		while (inFileObj.ready())
		{
			// 行文字列を取得
			currentLine = inFileObj.readLine();
			// 行文字列を分割して配列に格納
			currentLineList = currentLine.split(COMMA);
			// 1項目目を取得
			firstKmk = currentLineList[0];

			// 出力情報初期化
			outMap = new JBSbatServiceInterfaceMap();
			
			// レコード存在判定
			if (currentLine == null)
			{
				// ループを抜ける
				break;
			}
			
			// データ部の処理
			if (!TRAILER_REC_DIV.equals(firstKmk))
			{
				dataRecCnt++;
				
				// データ部として再取得
				inMap = dataDefFile.lineToObject(currentLine, inFileObj, 0);
				
				// 必須チェック、桁数チェック、属性チェック
				if (isSingleCheckKKIFE400_INF1(inMap.getMap(), recCnt))
				{
					// キャンペーンコード取得
					cmpCd = inMap.getString(JBSbatKKIFE400001.CMP_CD);
					
					// レコードを出力情報に設定
					outMap.setString(JBSbatKKIFM816.SVC_KEI_NO,		inMap.getString(JBSbatKKIFE400001.SVC_KEI_NO));		// サービス契約番号
					outMap.setString(JBSbatKKIFM816.KR_CUST_ID,		inMap.getString(JBSbatKKIFE400001.KR_CUST_ID));		// 関連お客さまＩＤ
					outMap.setString(JBSbatKKIFM816.CMP_CD,			inMap.getString(JBSbatKKIFE400001.CMP_CD));			// キャンペーンコード
					outMap.setString(JBSbatKKIFM816.CHG_DIV,			inMap.getString(JBSbatKKIFE400001.CHG_DIV));		// 変更区分
					outMap.setString(JBSbatKKIFM816.APLY_YMD,		inMap.getString(JBSbatKKIFE400001.APLY_YMD));		// 適用日
					outMap.setString(JBSbatKKIFM816.YB_KMK_1, 		inMap.getString(JBSbatKKIFE400001.YB_KMK_1));		// 予備項目１
					outMap.setString(JBSbatKKIFM816.YB_KMK_2, 		inMap.getString(JBSbatKKIFE400001.YB_KMK_2));		// 予備項目２
					outMap.setString(JBSbatKKIFM816.YB_KMK_3, 		inMap.getString(JBSbatKKIFE400001.YB_KMK_3));		// 予備項目３
					outMap.setString(JBSbatKKIFM816.YB_KMK_4, 		inMap.getString(JBSbatKKIFE400001.YB_KMK_4));		// 予備項目４
					outMap.setString(JBSbatKKIFM816.YB_KMK_5, 		inMap.getString(JBSbatKKIFE400001.YB_KMK_5));		// 予備項目５
					
					// レコード振り分け
					// 割引キャンペーンコードの場合
					if(wribCmpCdList.contains(cmpCd))
					{
						outputBean.addOutMapList(outMap);
					}
					// 商品券キャンペーンコードの場合
					else if(shohinkCmpCdList.contains(cmpCd))
					{
						outputBean.addOutMapList_2(outMap);
					}
					// いずれでもない場合
					else
					{
						commonItem.getLogPrint().printBusinessErrorLog("EKKB1780KW", new String[]{
								String.valueOf(recCnt),
								inMap.getString(JBSbatKKIFE400001.CMP_CD)
						});
						
					}
				}
			}
			// トレーラ部の処理
			else
			{
				trailerRecCnt++;
				
				// トレーラ部として再取得
				inMap = trailerDefFile.lineToObject(currentLine, inFileObj, 0);
				
				// トレーラ部合計件数取得
				totalCnt = Integer.parseInt(inMap.getString(JBSbatKKIFE400002.TOTAL_CNT));
				
				// 件数チェック
				if (dataRecCnt != totalCnt)
				{
					// エラーの場合
					fileErrFlg = true;
					// ログ出力
					String messageId = "EKKB1890KW";
					commonItem.getLogPrint().printBusinessErrorLog(messageId);
					
				}
			}
			
			// レコード件数のカウント
			recCnt++;
		}
		
		// トレーラ部レコード数チェック
		if (trailerRecCnt != 1)
		{
			fileErrFlg = true;
			commonItem.getLogPrint().printBusinessErrorLog("EKKB1790KW", new String[]{
					"トレーラ部",
					String.valueOf(recCnt - 1)
			});
		}
		
		if (fileErrFlg)
		{
			// 出力電文を初期化
			outputBean = new JBSbatOutputItem();
		}
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
		
		return outputBean;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 *入力情報（セット割登録依頼データ）の単項目チェックを行います。
	 * @param rsMap 入力データ（TXT又はDB）を格納されたHashMap。
	 * @param count 行数カウンタ
	 * @return boolean trueである場合、エラーがありません。falseである場合、エラーを発生しました。
	 */
	private boolean isSingleCheckKKIFE400_INF1(HashMap rsMap, int count)
	{
		// エラー時の引数１を設定
		String cnt = String.valueOf(count);
		// エラー時の引数２を設定
		String message = (String)rsMap.get("SVC_KEI_NO") + " " + (String)rsMap.get("KR_CUST_ID") + " " + (String)rsMap.get("CMP_CD") 
							+ " " + (String)rsMap.get("CHG_DIV") + " " + (String)rsMap.get("APLY_YMD");
		
		// 単項目チェックを行います
		String strValue = null;

		// サービス契約番号項目チェック
		strValue = (String)rsMap.get("SVC_KEI_NO");
		// 必須チェック
		if(strValue == null || "".equals(strValue))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0580TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 桁数チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"ketasuu2", "10", "10"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0590TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 属性チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"hannkakuesuuji1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0600TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}

		// 関連お客さまＩＤ項目チェック
		strValue = (String)rsMap.get("KR_CUST_ID");
		// 必須チェック
		if(strValue == null || "".equals(strValue))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0580TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 桁数チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"ketasuu2", "10", "10"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0590TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 属性チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"hannkakuesuuji1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0600TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}

		// キャンペーンコード項目チェック
		strValue = (String)rsMap.get("CMP_CD");
		// 必須チェック
		if(strValue == null || "".equals(strValue))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0580TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 桁数チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"ketasuu2", "8", "8"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0590TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 属性チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"hannkakuesuuji1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0600TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}

		// 変更区分項目チェック
		strValue = (String)rsMap.get("CHG_DIV");
		// 必須チェック
		if(strValue == null || "".equals(strValue))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0580TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 桁数チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"ketasuu2", "1", "1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0590TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 属性チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"hannkakusuuji1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0600TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}

		// 適用日項目チェック
		strValue = (String)rsMap.get("APLY_YMD");
		// 必須チェック
		if(strValue == null || "".equals(strValue))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0580TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 桁数チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"ketasuu2", "8", "8"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0590TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}
		// 属性チェック
		if(!JBSbatCheckUtil.invoke(strValue, new String[]{"year_month_day1"}))
		{
			commonItem.getLogPrint().printBusinessErrorLog("EKKB0600TW", new String[]{
					cnt,
					message.toString()});
			return false;
		}

		return true;
	}
}