/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatAKContTdkSeikyYsoInfGet
*	ソースファイル名	：JBSbatAKContTdkSeikyYsoInfGet.java
*	作成者				：富士通　
*	作成日				：2013年06月20日
*＜機能概要＞
*　コンテンツ単独分請求予想情報取得部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2013/06/20   富士通		新規作成
*********************************************************************/
package eo.business.service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.util.file.JBSbatAKIFM048;
import eo.common.constant.JACStrConst;
import eo.framework.db.JBSbatSQLAccess;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.item.JBSbatServiceInterfaceMap;

/**
* 請求検証ツール<BR>
*コンテンツ単独分請求予想情報取得
*
* @author 富士通
*/
public class JBSbatAKContTdkSeikyYsoInfGet extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/** テーブル(請求)*/
	private static final String D_TBL_NAME_AK_CH_T_SEIKY = "AK_CH_T_SEIKY";

	/** SQL定義キー(AK_SELECT_008)*/
	private static final String AK_CH_T_SEIKY_AK_SELECT_008 = "AK_SELECT_008";

	/** SQL定義キー(AK_SELECT_009)*/
	private static final String AK_CH_T_SEIKY_AK_SELECT_009 = "AK_SELECT_009";

	/** テーブルアクセスクラス(請求)*/
	private JBSbatSQLAccess db_AK_CH_T_SEIKY = null;
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/** フリー項目情報格納用配列 */
	private String[] fleeItemArray = null;
	
	/** 検証対象年月の翌月２日 時分秒*/
	private String paramYmdhmsm = null;
	
	/** 検証対象年月の翌月２日 */
	private int paramYmd = 0;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);

		// DBアクセスクラスを生成します
		db_AK_CH_T_SEIKY = new JBSbatSQLAccess(commonItem, D_TBL_NAME_AK_CH_T_SEIKY);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial][opeDate=" + super.opeDate + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial][freeItem=" + super.freeItem + "]");
		
		// フリー項目情報を取得
		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]");
		
		// 出力するデータの作成
		JBSbatOutputItem item = new JBSbatOutputItem();
		
		// パラメータ（検証対象年月）の翌月２日取得
		String wkYmd = fleeItemArray[0] + "02";
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = Calendar.getInstance();
		Date date = format.parse(wkYmd);
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		paramYmd     = Integer.parseInt(format.format(cal.getTime()).toString());
		paramYmdhmsm = format.format(cal.getTime()).toString() + "999999999";
		
		// コンテンツクレカ課金レコードの検索
		this.selectContCrecaChrg();
		// コンテンツクレカ課金レコードの出力
		this.setOutputItems1(item);
		
		// コンテンツ課金訂正レコードの検索
		this.selectContChrgTeisei();
		// コンテンツ課金訂正レコードの出力
		this.setOutputItems2(item);
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
		return item;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		// DBアクセスクラスをクローズします
		db_AK_CH_T_SEIKY.close();
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**
	 * SQLKEY(AK_SELECT_008)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 * 
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 *			検証対象年月
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeAK_CH_T_SEIKY_AK_SELECT_008(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_AK_CH_T_SEIKY.selectBySqlDefine(paramList, AK_CH_T_SEIKY_AK_SELECT_008);
	}

	/**
	 * SQLKEY(AK_SELECT_009)でDBアクセスを行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.引数でバイント変数を設定します。<br>
	 *
	 * 2.DBアクセスを実行します。<br>
	 * 
	 * 3.メソッドの呼び出し方です。<br>
	 *		引数:
	 *		param:順にバイント変数の値をparam配列に入れます。バイント変数は以下に説明します。
	 * </pre>
	 * <p>
	 * @param param バイント変数の値配列。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeAK_CH_T_SEIKY_AK_SELECT_009(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_AK_CH_T_SEIKY.selectBySqlDefine(paramList, AK_CH_T_SEIKY_AK_SELECT_009);
	}
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	
	
	/**▼▼▼▼▼▼メソッド追加 開始▼▼▼▼▼▼*/
	
	/**
	 * SELECT文を発行するために必要なパラメタを設定する。
	 * コンテンツクレカ課金
	 * 
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private void selectContCrecaChrg() throws Exception {
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][AK_CH_T_SEIKY_AK_SELECT_008]");

		// DBアクセス用のパラメータ定義
		JBSbatCommonDBInterface dbList = new JBSbatCommonDBInterface();
		
		// 対象キーの設定
		dbList.setValue(fleeItemArray[0]);					// 検証対象年月
		dbList.setValue(paramYmdhmsm);						// 検証対象年月の翌月２日
		dbList.setValue(fleeItemArray[0]);					// 検証対象年月

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][AK_CH_T_SEIKY_AK_SELECT_008][dbList=" + dbList.getList().toString() + "]");
		
		// サービス契約情報抽出
		executeAK_CH_T_SEIKY_AK_SELECT_008(dbList.getList().toArray());
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][AK_CH_T_SEIKY_AK_SELECT_008]");
		
		return;
	}
	
	/**
	 * SELECT文を発行するために必要なパラメタを設定する。
	 * コンテンツ課金訂正
	 * 
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private void selectContChrgTeisei() throws Exception {
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][AK_CH_T_SEIKY_AK_SELECT_009]");

		// DBアクセス用のパラメータ定義
		JBSbatCommonDBInterface dbList = new JBSbatCommonDBInterface();
		
		// 対象キーの設定
		dbList.setValue(fleeItemArray[0]);					// 検証対象年月
		dbList.setValue(paramYmdhmsm);						// 検証対象年月の翌月２日
		dbList.setValue(fleeItemArray[0]);					// 検証対象年月

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][AK_CH_T_SEIKY_AK_SELECT_009][dbList=" + dbList.getList().toString() + "]");
		
		// サービス契約情報抽出
		executeAK_CH_T_SEIKY_AK_SELECT_009(dbList.getList().toArray());
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][AK_CH_T_SEIKY_AK_SELECT_009]");
		
		return;
	}

	/**
	 * 取得したコンテンツクレカ課金のレコードを出力する。
	 * 
	 * @param outputItem 出力情報のレコード
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private void setOutputItems1(JBSbatOutputItem outputItem) throws Exception {

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][setOutputItems1]");
		
		// DBからの検索結果の取得
		JBSbatCommonDBInterface rec = db_AK_CH_T_SEIKY.selectNext();
		
		JBSbatServiceInterfaceMap outRecord = null;
		
		while(null != rec)
		{
			
			if((rec.getString("KEISHA_TYPE_CD").equals("1") || rec.getString("KEISHA_TYPE_CD").equals("2") || rec.getString("KEISHA_TYPE_CD").equals("4")) &&
			   (rec.getString("JOIN_YMD") != null) &&
			   (rec.getString("RSV_APLY_CD") == null || rec.getString("RSV_APLY_CD").equals("2")) &&
			   (Integer.parseInt(rec.getString("RSV_APLY_YMD")) <= paramYmd)) {
				
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setOutputItems][rec=" + rec.getMap().toString() + "]");
				
				outRecord = new JBSbatServiceInterfaceMap();
				
				//取得したレコードを設定します
				
				// クレジットカード番号（暗号化）
				outRecord.setString(JBSbatAKIFM048.CRECARD_NO, rec.getString(JBSbatAKIFM048.CRECARD_NO));
				
				// ＳＹＳＩＤ
				outRecord.setString(JBSbatAKIFM048.SYSID, rec.getString(JBSbatAKIFM048.SYSID));
				
				// 課金額
				outRecord.setBigDecimal(JBSbatAKIFM048.CHRG_AMNT, rec.getBigDecimal(JBSbatAKIFM048.CHRG_AMNT));
				
				// 売上減額
				outRecord.setBigDecimal(JBSbatAKIFM048.SALES_GEN_AMNT, BigDecimal.ZERO );
				
				// 補償費
				outRecord.setBigDecimal(JBSbatAKIFM048.COMPENSATION, BigDecimal.ZERO);
				
				// 出力フラグを設定します
				outRecord.setOutFlg(true);
				// 出力レコード1件設定します
				outputItem.addOutMapList(outRecord);
			}
			// 次レコード
			rec = db_AK_CH_T_SEIKY.selectNext();
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setOutputItems1]");
	}
	
	/**
	 * 取得したコンテンツ課金訂正のレコードを出力する。
	 * 
	 * @param outputItem 出力情報のレコード
	 * @throws Exception 業務サービス内で発生した例外全般
	 */
	private void setOutputItems2(JBSbatOutputItem outputItem) throws Exception {

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][setOutputItems2]");
		
		// DBからの検索結果の取得
		JBSbatCommonDBInterface rec = db_AK_CH_T_SEIKY.selectNext();
		
		JBSbatServiceInterfaceMap outRecord = null;
		
		while(null != rec)
		{
			
			if((rec.getString("KEISHA_TYPE_CD").equals("1") || rec.getString("KEISHA_TYPE_CD").equals("2") || rec.getString("KEISHA_TYPE_CD").equals("4")) &&
			   (rec.getString("JOIN_YMD") != null) &&
			   (rec.getString("RSV_APLY_CD") == null || rec.getString("RSV_APLY_CD").equals("2")) &&
			   (Integer.parseInt(rec.getString("RSV_APLY_YMD")) <= paramYmd)) {
				
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][setOutputItems][rec=" + rec.getMap().toString() + "]");
				
				outRecord = new JBSbatServiceInterfaceMap();
				
				//取得したレコードを設定します
				
				// クレジットカード番号（暗号化）
				outRecord.setString(JBSbatAKIFM048.CRECARD_NO, rec.getString(JBSbatAKIFM048.CRECARD_NO));
				
				// ＳＹＳＩＤ
				outRecord.setString(JBSbatAKIFM048.SYSID, rec.getString(JBSbatAKIFM048.SYSID));
				
				// 課金額
				outRecord.setBigDecimal(JBSbatAKIFM048.CHRG_AMNT, BigDecimal.ZERO);
				
				// 売上減額
				outRecord.setBigDecimal(JBSbatAKIFM048.SALES_GEN_AMNT, rec.getBigDecimal(JBSbatAKIFM048.SALES_GEN_AMNT));
				
				// 補償費
				outRecord.setBigDecimal(JBSbatAKIFM048.COMPENSATION, rec.getBigDecimal(JBSbatAKIFM048.COMPENSATION));
				
				// 出力フラグを設定します
				outRecord.setOutFlg(true);
				// 出力レコード1件設定します
				outputItem.addOutMapList_2(outRecord);
			}
			// 次レコード
			rec = db_AK_CH_T_SEIKY.selectNext();	
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][setOutputItems2]");
	}
	
}