/**
 *
 * @(#)author  [Youngmi Im]
 * @(#)version 1.0
 * @(#)date    May 10, 2007
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.hwenc.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
 package kr.co.udapsoft.ebid.buyer.payment.purchase.biz;


import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.util.ValueObject;

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

public class PayRequestSaveBiz extends Biz {
	
	private static String ACCT_TABLE     = "PUB0120T";
    private static String R_TABLE        = "PUB0340T";
    private static String R_LIST_TABLE   = "PUB0350T";
    private static String PGM_TABLE      = "PUB0320T";
    private static String PGM_GR_TABLE   = "PUB0310T";
    private static String PGM_LIST_TABLE = "PUB0300T";
    private static String PGM_SYS_TABLE  = "PUB0390T";
    
	public PayRequestSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		WAFLogger.debug("SAVE TEST");
		if(storage.compareEvent("FORM_SAVE_DATA")){	//데이터 저장
			//boolean success = saveData(params);
			//super.storage.setSuccess(success);
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
		
	}

	/**
	 * Persistent 를 이용한 Save 처리
	 * @param params
	 * @return
	 * * 입력받은 프로그램 등록 정보를 등록합니다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean saveData(ValueObject params) {
		Persistent persistent_i = null;
		Persistent persistent_u = null;
		Persistent persistent_d = null;
		String sqlstr_i = "";
		String sqlstr_u = "";
		String sqlstr_d = "";

		try {

			conn.begin();
			
			persistent_i = new PersistentImpl(super.conn.getConnection());
			persistent_u = new PersistentImpl(super.conn.getConnection());
			persistent_d = new PersistentImpl(super.conn.getConnection());


			  sqlstr_i += "  INSERT INTO TEST_CHJ             \n"
				        + "   ( PGM_ID,              \n"
				        + "    PGM_NAME,              \n"
				        + "    SCREEN_NAME,           \n"
				        + "    SYSTEM_ID,             \n"
				        + "    SYSTEM_CODE,           \n"
				        + "    DSCR,                  \n"
				        + "    CRE_BY,                \n"
				        + "    CRE_DATE )             \n"
				        + " VALUES                    \n"
				        + "    ( PUB0320T_S.NEXTVAL,  \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    TO_CHAR(sysdate, 'YYYYMMDDHH24MISS') )              \n" ; 
			  
			  sqlstr_u = " UPDATE " + PGM_TABLE + "       \n"
		              + " SET    PGM_NAME    = ?       , \n"
		              + "        SCREEN_NAME = ?       , \n"
		              + "        SYSTEM_ID   = ?       , \n"
		              + "        SYSTEM_CODE = ?       , \n"
		              + "        DSCR        = ?       , \n"
		              + "        UPD_BY      = ?       , \n"
		              + "        UPD_DATE    = TO_CHAR(sysdate, 'YYYYMMDDHH24MISS')   \n"
		              + " WHERE  PGM_ID      = ?         \n" ;
			  
			
			  sqlstr_d = 	" DELETE                   \n"
		                  + " FROM   " + PGM_TABLE + " \n"
		                  + " WHERE  PGM_ID = ?        \n" ;
		        
			/* 중요
			 * system_id 값 형식은  "1@SYS" "[SYSTEM_ID@SYSTEM_CODE]" 
			 * 테이블명  PGM_SYS_TABLE 의 SYSTEM_CODE 값을 저장해야 한다.
			 */

			String formData1 = super.get("formdata1");
			String[] statustest = super.gets("sheet1.STATUS");
			String[] status  		= super.gets("STATUS");
			String[] pgm_name      	= super.gets("sheet1.NO");
			String[] screen_name    = super.gets("SCREEN_NAME");
			String[] system_id    	= super.gets("SYSTEM_ID");
			String[] dscr     		= super.gets("DSCR");
			String[] pgm_id     	= super.gets("PGM_ID");

			persistent_i.setStatement(sqlstr_i);
			persistent_u.setStatement(sqlstr_u);
			persistent_d.setStatement(sqlstr_d);
			
			if( status != null ) {
				for( int i = 0 ; i < status.length ; i++ ) {
					if( "I".equals(status[i]) ) {						
						/*persistent_i.addParameter(pgm_name[i].trim());
						persistent_i.addParameter(screen_name[i].trim());
						String[] system_code = system_id[0].split("[@]");
						persistent_i.addParameter(system_code[0]); //<== system_id
						persistent_i.addParameter(system_code[1]); //<== system_code
						persistent_i.addParameter(dscr[i].trim());
						persistent_i.addParameter(params.get("USER_ID"));
						persistent_i.execute();
						persistent_i.clearParameters();*/
					}
					
					else if( "U".equals(status[i]) ) {
						/*persistent_u.addParameter(pgm_name[i].trim());
						persistent_u.addParameter(screen_name[i].trim());
						String[] system_code = system_id[0].split("[@]");
						persistent_u.addParameter(system_code[0]); //<== system_id
						persistent_u.addParameter(system_code[1]); //<== system_code
						persistent_u.addParameter(dscr[i].trim());
						persistent_u.addParameter(params.get("USER_ID"));
						persistent_u.addParameter(pgm_id[i]);
						persistent_u.execute();
						persistent_u.clearParameters();*/
					}
					else if( "D".equals(status[i]) ) {
			
						/*persistent_d.addParameter(pgm_id[i]);
						persistent_d.execute();
						persistent_d.clearParameters();*/
					}
				}
			}
			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }
	}
}
