/**
 *
 * @(#)author  [ok]
 * @(#)version 1.0
 * @(#)date    2014. 07. 04
 * @(#)since   JDK 1.7.2
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of udapsoft, Inc.
 */
 package kr.co.udapsoft.ebid.supplier.portal.payment.biz;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.WAFSQLException;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;




import kr.co.udapsoft.ebid.buyer.sycs.user.biz.UserCommon;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class ApplicationRequestListSaveBiz extends Biz {
	
	public ApplicationRequestListSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		
		if (storage.compareEvent("SAVE_DATA")) {
			
			boolean success = false;
			String[] status = super.gets("sheet1.STATUS");

			try {
				super.conn.begin();
				
				
				for (int i = 0; i < status.length; i++) {
					
					success = saveData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					
				}
				
				super.storage.setSuccess(success);
				storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			} catch (Exception e) {
				WAFLogger.error(e);
				super.conn.setRollback();
				throw new Exception(e);
				
			} finally {
				super.conn.close();
			}
			
		}
	}

	private boolean saveData(Connection connection, ValueObject sqlParams) throws Exception {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("	INSERT INTO SUB1040T								\n ");
			sql.append("	(													\n ");
			sql.append("		ACMP_ID,										\n ");
			sql.append("		SUB_CONT_ID,									\n ");
			sql.append("		CON_CORP_ID,									\n ");
			sql.append("		MOD_NO,											\n ");
			sql.append("		ACMP_YYYYMM,									\n ");
			sql.append("		ACMP_NO,										\n ");
			sql.append("		ACMP_DATE,										\n ");
			sql.append("		CON_START_DATE,									\n ");
			sql.append("		CON_END_DATE,									\n ");
			sql.append("		CON_CUR_CD,										\n ");
			sql.append("		CON_AMT,										\n ");
			sql.append("		CON_SUPPLY_AMT,									\n ");
			sql.append("		CON_SUPPLY_TAMT,								\n ");
			sql.append("		CON_SUPPLY_NAMT,								\n ");
			sql.append("		CON_SURTAX_AMT,									\n ");
			sql.append("		FUND_AMT,										\n ");
			sql.append("		FUND_TAMT,										\n ");
			sql.append("		FUND_NAMT,										\n ");
			
			sql.append("		PAY_DEDUCT_AMT,										\n ");
			sql.append("		PAY_DEDUCT_TAMT,										\n ");
			sql.append("		PAY_DEDUCT_NAMT,										\n ");
			sql.append("		PAY_DEDUCT_RATE,										\n ");
			sql.append("		REQU_AMT,										\n ");
			sql.append("		REQU_SUPPLY_AMT,										\n ");
			sql.append("		REQU_SUPPLY_TAMT,										\n ");
			sql.append("		REQU_SUPPLY_NAMT,										\n ");
			sql.append("		REQU_SURTAX_AMT,										\n ");
			sql.append("		PRE_FUND_AMT,										\n ");
			sql.append("		PRE_FUND_TAMT,										\n ");
			sql.append("		PRE_FUND_NAMT,										\n ");
			sql.append("		PRE_PAY_DEDUCT_AMT,										\n ");
			sql.append("		PRE_PAY_DEDUCT_TAMT,										\n ");
			sql.append("		PRE_PAY_DEDUCT_NAMT,										\n ");
			sql.append("		PRE_PAY_DEDUCT_RATE,										\n ");
			sql.append("		PRE_REQU_AMT,										\n ");
			sql.append("		PRE_REQU_SUPPLY_AMT,										\n ");
			sql.append("		PRE_REQU_SUPPLY_TAMT,										\n ");
			sql.append("		PRE_REQU_SUPPLY_NAMT,										\n ");
			sql.append("		PRE_REQU_SURTAX_AMT,										\n ");
			sql.append("		TOT_FUND_AMT,										\n ");
			sql.append("		TOT_FUND_TAMT,										\n ");
			sql.append("		TOT_FUND_NAMT,										\n ");
			sql.append("		TOT_PAY_DEDUCT_AMT,										\n ");
			sql.append("		TOT_PAY_DEDUCT_TAMT,										\n ");
			sql.append("		TOT_PAY_DEDUCT_NAMT,										\n ");
			sql.append("		TOT_PAY_DEDUCT_RATE,										\n ");
			sql.append("		TOT_REQU_AMT,										\n ");
			sql.append("		TOT_REQU_SUPPLY_AMT,										\n ");
			sql.append("		TOT_REQU_SUPPLY_TAMT,										\n ");
			sql.append("		TOT_REQU_SUPPLY_NAMT,										\n ");
			sql.append("		TOT_REQU_SURTAX_AMT,										\n ");
			sql.append("		STATUS_CD,										\n ");
			sql.append("		PROOF_CD,										\n ");
			sql.append("		DEPT_CD,										\n ");
			sql.append("		SUPP_DIRECT_YN,										\n ");
			sql.append("		LABOR_USE_YN,										\n ");
			
			sql.append("		TAX_CHARG_NM,										\n ");
			sql.append("		TAX_CHARG_HP,										\n ");
			sql.append("		TAX_CHARG_EMAIL,										\n ");
			sql.append("		TRANS_CD										\n ");
			sql.append("		) VALUES (										\n ");
			sql.append("		SUB1040T_S.NEXTVAL						\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,TO_DATE(?)										\n ");
			sql.append("		,TO_DATE(?)										\n ");
			sql.append("		,TO_DATE(?)										\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,?												\n ");
			sql.append("		,'N'											\n ");
			sql.append("		)												\n ");
			
			pstmt = connection.prepareStatement(sql.toString());
			int paramIdx = 1;
			System.out.println("***********here");
			pstmt.setInt(paramIdx++, sqlParams.getInt("SUB_CONT_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_CORP_ID"));
			pstmt.setString(paramIdx++, sqlParams.get("MOD_NO"));
			pstmt.setString(paramIdx++, sqlParams.get("ACMP_YYYYMM"));
			pstmt.setString(paramIdx++, sqlParams.get("ACMP_NO"));
			pstmt.setString(paramIdx++, sqlParams.get("ACMP_DATE"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_START_DATE"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_END_DATE"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_CUR_CD"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_SUPPLY_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_SUPPLY_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_SUPPLY_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("CON_SURTAX_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("FUND_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("FUND_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("FUND_NAMT"));
			
			pstmt.setString(paramIdx++, sqlParams.get("PAY_DEDUCT_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PAY_DEDUCT_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PAY_DEDUCT_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PAY_DEDUCT_RATE"));
			pstmt.setString(paramIdx++, sqlParams.get("REQU_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("REQU_SUPPLY_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("REQU_SUPPLY_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("REQU_SUPPLY_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("REQU_SURTAX_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_FUND_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_FUND_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_FUND_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_PAY_DEDUCT_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_PAY_DEDUCT_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_PAY_DEDUCT_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_PAY_DEDUCT_RATE"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_REQU_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_REQU_SUPPLY_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_REQU_SUPPLY_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_REQU_SUPPLY_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("PRE_REQU_SURTAX_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_FUND_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_FUND_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_FUND_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_PAY_DEDUCT_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_PAY_DEDUCT_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_PAY_DEDUCT_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_PAY_DEDUCT_RATE"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_REQU_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_REQU_SUPPLY_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_REQU_SUPPLY_TAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_REQU_SUPPLY_NAMT"));
			pstmt.setString(paramIdx++, sqlParams.get("TOT_REQU_SURTAX_AMT"));
			pstmt.setString(paramIdx++, sqlParams.get("STATUS_CD"));
			pstmt.setString(paramIdx++, sqlParams.get("PROOF_CD"));
			pstmt.setString(paramIdx++, sqlParams.get("DEPT_CD"));
			pstmt.setString(paramIdx++, sqlParams.get("SUPP_DIRECT_YN"));
			pstmt.setString(paramIdx++, sqlParams.get("LABOR_USE_YN"));
			
			pstmt.setString(paramIdx++, sqlParams.get("TAX_CHARG_NM"));
			pstmt.setString(paramIdx++, sqlParams.get("TAX_CHARG_HP"));
			pstmt.setString(paramIdx++, sqlParams.get("TAX_CHARG_EMAIL"));
			
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			WAFLogger.error(e);
			throw new Exception(e);
		} finally {
			try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
		}
	}
}
