/**
 *
 * @(#)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.portal.payment.biz;


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.sql.util.RowSetUtility;
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.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

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

	public void doBiz() throws Exception {

		System.out.println("선급금 신청 상세 저장 처리");
		
		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 prepayId					= params.get("prepayId");				//선급ID
			String subContModId				= params.get("subContModId");			//외주계약변경ID
			String prepaySeq				= "1";									//선급금지급차수
			String corpId					= params.get("corpId");					//협력업체ID
			String prepayDate				= "";									//선급일자
			String perpayAmt				= params.get("perpayAmt");				//계약_선급금
			String perpaySupplyAmt			= "";									//계약_선급금공급가
			String subPrepaySupplyTamt		= params.get("subPrepaySupplyTamt");	//계약_선급금공급가_과세
			String subPrepaySupplyNamt		= params.get("subPrepaySupplyNamt");	//계약_선급금공급가_면세
			String subPrepaySurtaxAmt		= params.get("subPrepaySurtaxAmt");		//계약_선급금부가세
			String reqPrepayAmt				= params.get("reqPrepayAmt");			//신청_선급금합계
			String reqPrepaySupplyTmat		= params.get("reqPrepaySupplyTmat");	//신청_선급금공급가_과세
			String reqPrepaySupplyNamt		= params.get("reqPrepaySupplyNamt");	//신청_선급금공급가_면세
			String reqPrepaySurtaxAmt		= params.get("reqPrepaySurtaxAmt");		//신청_선급금부가세
			String prepayDesc				= "";									//적요
			String conCurCd					= params.get("conCurCd");				//계약통화
			String taxMethodCd				= params.get("chk_info");				//세금계산서발행방법
			String taxDy					= params.get("date1");					//작성일자(증빙일자)
			String taxChargNm				= params.get("userName");				//담당자명
			String taxChargHp				= params.get("hp");						//담당자 핸드폰번호
			String taxChargEmail			= params.get("eMail");					//담당자 이메일
			String perpayReqDiv				= params.get("perpayReqDiv");			//신청부분
			String statusCd					= "01";									//상태(작성중)
			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 PREPAY_OUT                                 \n"
				        + "   ( PREPAY_ID,              		                    \n"
				        + "    SUB_CONT_MOD_ID,              	                    \n"
				        + "    PREPAY_SEQ,              		                    \n"
				        + "    CORP_ID,              			                    \n"
				        + "    STATUS_CD,              			                    \n"
				        + "    PREPAY_DATE,              		                    \n"
				        + "    PREPAY_AMT,              		                    \n"
				        + "    PREPAY_SUPPLY_TAMT,                                  \n"
				        + "    PREPAY_SUPPLY_NAMT,                                  \n"
				        + "    PREPAY_SURTAX_AMT,              	                    \n"
				        + "    REQ_PREPAY_AMT,              	                    \n"
				        + "    REQ_PREPAY_SUPPLY_TMAT,                              \n"
				        + "    REQ_PREPAY_SUPPLY_NAMT,                              \n"
				        + "    REQ_PREPAY_SURTAX_AMT,                               \n"
				        + "    CON_CUR_CD,              		                    \n"
				        + "    TAX_METHOD_CD,              		                    \n"
				        + "    TAX_DY,              			                    \n"
				        + "    TAX_CHARG_NM,              		                    \n"
				        + "    TAX_CHARG_HP,              		                    \n"
				        + "    TAX_CHARG_EMAIL,              	                    \n"
				        + "    REQ_DIV_CD,              		                    \n"
				        + "    CRE_BY,              			                    \n"
				        + "    CRE_DATE,              			                    \n"
				        + "    UPD_BY,              			                    \n"
				        + "    UPD_DATE )             			                    \n"
				        + " VALUES                    			                    \n"
				        + "    (PREPAY_OUT_S.nextval, \n"
				        + "    ?,                     \n"
				        + "    ?,	                  \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    ?,                     \n"
				        + "    sysdate,               \n"
				        + "    ?,        			  \n"
				        + "    sysdate)               \n"; 
			  
			  sqlstr_u = " UPDATE PREPAY_OUT      														\n"
		              + " SET    TAX_METHOD_CD    			= ?       , 					            \n"
		              + "        TAX_DY 					= ?       , 					            \n"
		              + "        TAX_CHARG_NM				= ?       , 					            \n"
		              + "        TAX_CHARG_HP				= ?       , 					            \n"
		              + "        TAX_CHARG_EMAIL			= ?       , 					            \n"
		              + "        REQ_DIV_CD					= ?       , 					            \n"
		              + "        REQ_PREPAY_AMT				= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_TMAT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SUPPLY_NAMT		= ?       , 					            \n"
		              + "        REQ_PREPAY_SURTAX_AMT		= ?       , 					            \n"
		              + "        UPD_BY      				= ?       , 					            \n"
		              + "        UPD_DATE    				= sysdate									\n"
		              + " WHERE  PREPAY_ID      			= ?        									\n" ;
			  
			persistent_i.setStatement(sqlstr_i);
			persistent_u.setStatement(sqlstr_u);
			
			if("".equals(params.get("prepayId"))){	//신규입력			
				
				persistent_i.addParameter(subContModId);
				persistent_i.addParameter(prepaySeq);
				persistent_i.addParameter(corpId);
				persistent_i.addParameter(statusCd);
				persistent_i.addParameter(prepayDate);
				persistent_i.addParameter(perpayAmt.replaceAll(",", ""));
				persistent_i.addParameter(subPrepaySupplyTamt.replaceAll(",", ""));
				persistent_i.addParameter(subPrepaySupplyNamt.replaceAll(",", ""));
				persistent_i.addParameter(subPrepaySurtaxAmt.replaceAll(",", ""));
				persistent_i.addParameter(reqPrepayAmt.replaceAll(",", ""));
				persistent_i.addParameter(reqPrepaySupplyTmat.replaceAll(",", ""));
				persistent_i.addParameter(reqPrepaySupplyNamt.replaceAll(",", ""));
				persistent_i.addParameter(reqPrepaySurtaxAmt.replaceAll(",", ""));
				persistent_i.addParameter(conCurCd);
				persistent_i.addParameter(taxMethodCd);
				persistent_i.addParameter(taxDy.replaceAll("-", ""));
				persistent_i.addParameter(taxChargNm);
				persistent_i.addParameter(taxChargHp);
				persistent_i.addParameter(taxChargEmail);
				persistent_i.addParameter(perpayReqDiv);
				persistent_i.addParameter(userId);
				persistent_i.addParameter(userId);
				
				System.out.println(sqlstr_i);
				System.out.println(persistent_i.getParameters());
				
				persistent_i.execute();
				persistent_i.clearParameters();
			}else{	//데이터 업데이트
				
				persistent_u.addParameter(taxMethodCd);
				persistent_u.addParameter(taxDy.replaceAll("-", ""));
				persistent_u.addParameter(taxChargNm);
				persistent_u.addParameter(taxChargHp);
				persistent_u.addParameter(taxChargEmail);
				persistent_u.addParameter(perpayReqDiv);
				persistent_u.addParameter(reqPrepayAmt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyTmat.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySupplyNamt.replaceAll(",", ""));
				persistent_u.addParameter(reqPrepaySurtaxAmt.replaceAll(",", ""));
				persistent_u.addParameter(userId);
				persistent_u.addParameter(prepayId);
				
				System.out.println(sqlstr_u);
				System.out.println(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);
			
			System.out.println(sqlstr_d);
			System.out.println(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);
			
			System.out.println(sqlstr_u);
			System.out.println(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();
        }	
	}
}
