/**
 *
 * @(#)author  [ok]
 * @(#)version 1.0
 * @(#)date    2014. 06. 25
 * @(#)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.buyer.portal.payment.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.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

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

	public void doBiz() throws Exception {

		WAFLogger.debug("보증정보 저장 처리");
		
		boolean success = false;
		if(storage.compareEvent("SAVE_DATA")){	// 선급금보증 데이터 저장
			success = saveData(params);
		}
/*		else if(storage.compareEvent("DELETE_DATA")){	//데이터 삭제
			success = deleteData(params);
		}else if(storage.compareEvent("REQ_DATA")){		//선급금신청 요청
			success = reqData(params);
		}*/
		storage.setSuccess(success);
		
	}

	/**
	 * 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;
		String sqlstr_i = "";
		String sqlstr_u = "";

		try {
		
			//선급금 신청 상제 정보
			String warrContId					= params.get("warrContId");				//보증ID
			String warrGubun					= params.get("warrGubun");				//보증구분
			String warrSeq						= params.get("warrSeq");				//보증일련번호
			String chk_info						= params.get("chk_info");				//발행방법
			String warrGoffice					= params.get("warrGoffice");			//보증기관
			String warrWgon						= params.get("warrWgon");				//증권번호
			String dateStart					= params.get("dateStart");				//보증기간 시작
			String dateEnd						= params.get("dateEnd");				//보증기간 끝
			String warrWamt						= params.get("warrWamt");				//보증금액
			String paperDate					= params.get("paperDate");				//발행일
			String userId						= storage.getUser().get("USER_ID");		//로그인사용자

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

			  sqlstr_i += "  INSERT INTO CONT_OUT_WARR                              \n"
				        + "   ( SUB_CONT_ID,              		                    \n"
				        + "    WARR_GUBUN,              	                    	\n"
				        + "    WARR_SEQ,              	                   			\n"
				        + "    WARR_WAMT,              	                    		\n"
				        + "    GUAR_ONOFF_YN,         	                    		\n"
				        + "    WARR_GOFFICE,         	                    		\n"
				        + "    WARR_WGNO,     	    	                    		\n"
				        + "    WARR_SDATE,     	    	                    		\n"
				        + "    WARR_EDATE,     	    	                    		\n"
				        + "    PAPERDATE,     	    	                    		\n"
				        + "    WARR_INDAY,     	    	                    		\n"
				        + "    WARR_INUSR,     	    	                    		\n"
				        + "    WARR_UPDAY,     	    	                    		\n"
				        + "    WARR_UPUSR,             			                    \n"
				        + "    TRADESIGNDATE)          			                    \n"
				        + " VALUES                    			                    \n"
				        + "    (?,  				  \n"
				        + "    ?,                     \n"
				        + "    ?,	                  \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    SYSDATE,               \n"
				        + "    ?,                     \n"
				        + "    SYSDATE,               \n"
				        + "    ?,                     \n"
				        + "    SYSDATE)	              \n";
			  
			  sqlstr_u = " UPDATE CONT_OUT_WARR      												\n"
		              + " SET    WARR_WAMT    			= ?       , 					            \n"
		              + "        GUAR_ONOFF_YN 			= ?       , 					            \n"
		              + "        WARR_GOFFICE			= ?       , 					            \n"
		              + "        WARR_WGNO				= ?       , 					            \n"
		              + "        WARR_SDATE				= ?       , 					            \n"
		              + "        WARR_EDATE				= ?       , 					            \n"
		              + "        PAPERDATE				= ?		  ,									\n"
		              + "        WARR_UPDAY				= SYSDATE ,									\n"
		              + "        WARR_UPUSR				= ?		  									\n"
		              + " WHERE  SUB_CONT_ID      		= ?        									\n" 
		              + " AND    WARR_GUBUN      		= ?        									\n" 
		              + " AND    WARR_SEQ      			= ?        									\n"; 
			  
			persistent_i.setStatement(sqlstr_i);
			persistent_u.setStatement(sqlstr_u);
			
			if("".equals(params.get("subContId"))){	//신규입력			
				
				persistent_i.addParameter(warrContId);
				persistent_i.addParameter(warrGubun);
				persistent_i.addParameter(warrSeq);
				persistent_i.addParameter(warrWamt.replaceAll(",", ""));
				persistent_i.addParameter(chk_info);
				persistent_i.addParameter(warrGoffice);
				persistent_i.addParameter(warrWgon.replaceAll("-", ""));
				persistent_i.addParameter(dateStart.replaceAll("-", ""));
				persistent_i.addParameter(dateEnd.replaceAll("-", ""));
				persistent_i.addParameter(paperDate.replaceAll("-", ""));
				persistent_i.addParameter(userId);
				persistent_i.addParameter(userId);
				
				WAFLogger.debug(sqlstr_i);
				WAFLogger.debug(persistent_i.getParameters());
				
				persistent_i.execute();
				persistent_i.clearParameters();
			}else{	//데이터 업데이트
				
				persistent_u.addParameter(warrWamt.replaceAll(",", ""));
				persistent_u.addParameter(chk_info);
				persistent_u.addParameter(warrGoffice);
				persistent_u.addParameter(warrWgon.replaceAll("-", ""));
				persistent_u.addParameter(dateStart.replaceAll("-", ""));
				persistent_u.addParameter(dateEnd.replaceAll("-", ""));
				persistent_u.addParameter(paperDate.replaceAll("-", ""));
				persistent_u.addParameter(userId);
				persistent_u.addParameter(warrContId);
				persistent_u.addParameter(warrGubun);
				persistent_u.addParameter(warrSeq);
				
				WAFLogger.debug(sqlstr_u);
				WAFLogger.debug(persistent_u.getParameters());
				
				persistent_u.execute();
				persistent_u.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();
        }
	}
	
	/**
	 * Persistent 를 이용한 Delete 처리
	 * @param params
	 * @return
	 * * 입력받은 선급금 상세 정보를 삭제합니다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean deleteData(ValueObject params) {
		
		
		Persistent persistent_d = null;
		String sqlstr_d = "";

		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID

			conn.begin();
			
			persistent_d = new PersistentImpl(super.conn.getConnection());
			
			  sqlstr_d = 	" DELETE                   		\n"
		                  + " FROM   PREPAY_OUT 			\n"
		                  + " WHERE  PREPAY_ID = ?        	\n";
		        
			persistent_d.setStatement(sqlstr_d);
			persistent_d.addParameter(prepayId);
			
			WAFLogger.debug(sqlstr_d);
			WAFLogger.debug(persistent_d.getParameters());
			
			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();
        }
	}
	
	/**
	 * Persistent 를 이용한 Save 처리
	 * @param params
	 * @return
	 * * 선급금신처요청을 처리한다.
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean reqData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";

		try {
		
			//선급금 신청 상제 정보
			String prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID
			String statusCd					= "02";									//상태(선급신청요청)
			String userId					= storage.getUser().get("USER_ID");		//로그인사용자

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

			sqlstr_u = " UPDATE PREPAY_OUT      														\n"
			          + " SET    STATUS_CD    				= ?       ,									\n"
			          + "        UPD_BY      				= ?       , 					            \n"
			          + "        UPD_DATE    				= sysdate									\n"
			          + " WHERE  PREPAY_ID      			= ?        									\n" ;
			  
			persistent_u.setStatement(sqlstr_u);
			
			persistent_u.addParameter(statusCd);
			persistent_u.addParameter(userId);
			persistent_u.addParameter(prepayId);
			
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
			
			persistent_u.execute();
			persistent_u.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();
        }	
	}
}
