/**
 *
 * @(#)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 VendorPrintCheckSaveBiz extends Biz {
	
	public VendorPrintCheckSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {

		WAFLogger.debug("=============================================================");
		
		if(storage.getEvent().equals("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 = null;
		StringBuffer sqlStr = new StringBuffer();

		Persistent persistent1 = null;
		StringBuffer sqlStr1 = new StringBuffer();
		
		try {

			conn.begin();
			
			if(!params.get("status").equals("50")){
				persistent1 = new PersistentImpl(super.conn.getConnection());
				sqlStr1.append("  UPDATE CONT_PUR_PAYPLAN            \n");
				sqlStr1.append("   	 SET VP_DESC = ?,            \n");
				sqlStr1.append("   	     VP_CHK_EMPNO = ?,            \n");
				sqlStr1.append("   	     VP_CHK_EMPNM = ?            \n");
				sqlStr1.append("   WHERE PAYREQ_ID = ?          \n");
				persistent1.setStatement(sqlStr1.toString());
				persistent1.addParameter(params.get("VP_DESC"));
				persistent1.addParameter(params.get("VP_CHK_EMPNO"));
				persistent1.addParameter(params.get("VP_CHK_EMPNM"));
				persistent1.addParameter(params.get("payreqId"));
				WAFLogger.debug(sqlStr1.toString());
				WAFLogger.debug(persistent1.getParameters());
				persistent1.execute();
	            persistent1.clearParameters();
			}
			
            persistent = new PersistentImpl(super.conn.getConnection());
			sqlStr.append("     UPDATE PUR1100T SET            \n");
			sqlStr.append("     REQ_STATUS = ?            \n");
			
			if(params.get("status").equals("50"))
            {
			    sqlStr.append("     ,VP_RTN_DY = SYSDATE            \n");
			    sqlStr.append("     ,VP_RTN_REASON = ?            \n");
            }else{
    			sqlStr.append("     ,VP_END_DY = ?            \n");
    			sqlStr.append("     ,VP_CHK_EMPNO = ?            \n");
    			sqlStr.append("     ,VP_CHK_EMPNM = ?            \n");
    			sqlStr.append("     ,VP_OPINION = ?            \n");
    			sqlStr.append("     ,VP_RTN_REASON = ''            \n");
    			
            }
			sqlStr.append("  WHERE   PAYREQ_ID = ?          \n");
			
			persistent.setStatement(sqlStr.toString());
			persistent.addParameter(params.get("status"));
			if(params.get("status").equals("50")){
			    persistent.addParameter(params.get("return_reson"));
			}else{
				persistent.addParameter(params.get("VP_END_DY").replaceAll("-", ""));
				persistent.addParameter(params.get("VP_CHK_EMPNO"));
				persistent.addParameter(params.get("VP_CHK_EMPNM"));
				persistent.addParameter(params.get("opinion"));
			}
			persistent.addParameter(params.get("payreqId"));
			
			WAFLogger.debug(sqlStr.toString());
			WAFLogger.debug(persistent.getParameters());
			
			persistent.execute();
            persistent.clearParameters();
            
            return true;
        }
        catch(Exception e) {
            e.printStackTrace();
            WAFLogger.error(e);
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }
	}
}

