/**
 * @(#)file    OutEstmWriteSaveBiz.java
 * @(#)author  Son Hyung Min 
 * @(#)version 1.0
 * @(#)date    2014-06-23
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.supplier.portal.bid.biz;

import java.sql.Connection;

import oracle.toplink.internal.ox.StrBuffer;
import kr.co.udapsoft.common.util.Crosscert;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.common.code.CommCodeManager;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;

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

import crosscert.Base64;
import crosscert.Certificate;
import crosscert.Decrypt;
import crosscert.PrivateKey;
import crosscert.Verifier;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
   
public class OutEstmWriteSaveBiz extends Biz {

	Crosscert Crosscert = new Crosscert();
	public OutEstmWriteSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		params.set("login_user", storage.getUser().get("ACCT_ID"));
		boolean success = false;
		//견적 입찰 저장
		success = Save();
        super.storage.setSuccess(success);
        if(success)
        	storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
	}
	
	  /**
     * 사용자 정보 저장
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean Save() {
        try {
            conn.begin();
            
            if( storage.getEvent().equals("FORM_SAVE") ) {//견적서 제출
            	String result = Crosscert.chkSignVerify(params.get("ESTI_SIGNDATA"));//서명 인증 실시
            	if(result.equals("SIGN_SUCCESS")){//성공시 저장
            			
            		bid_supp_update(conn.getConnection(),params);
            	}else{//실패시 에러
            		throw new Exception("서명검증에 실패하였습니다.");
            	}
            }else if( storage.getEvent().equals("BIDGIVEUP_SAVE") ) {//입찰포기
            	String result = Crosscert.chkSignVerify(params.get("signeddata"));//서명 인증 실시
            	if(result.equals("SIGN_SUCCESS")){//성공시 저장
            		bidgiveup_supp_update(conn.getConnection(),params);
            	}else{//실패시 에러
            		throw new Exception("서명검증에 실패하였습니다.");
            	}
            }
            return true;
        }
        catch(Exception e) {
        	storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
            WAFLogger.error(this.getClass().getName() + " --> save() : " + e.getMessage());
            WAFLogger.error(e);
            conn.setRollback();

            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	
            conn.end();
        }
    }
	
	/**
     * 견적서 제출 저장
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void bid_supp_update(Connection connection, ValueObject valueObject) throws Exception {

        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append(" DECLARE CDATA CLOB;																		\n");
        	sqlstr.append(" BEGIN CDATA := ?;																			\n");
        	sqlstr.append(" MERGE INTO BID_SUPP T USING															\n");
        	sqlstr.append(" (SELECT distinct ? AS BID_NO,														 \n"); //2019.05.24 수정 
        	sqlstr.append("        ? AS BID_DEG,																			\n");
        	sqlstr.append("        A.CORP_ID AS VENDOR_CD,														\n");
        	sqlstr.append("       A.CORP_REG_NO AS VENDOR_NO,													\n");
        	sqlstr.append("        A.CORP_KOR AS VENDOR_NAME,													\n");
        	sqlstr.append("        A.REP_NAME AS REPRE_NO,														\n");
        	sqlstr.append("        B.USER_NAME AS CHARGE_NAME,												\n");
        	sqlstr.append("        DECODE(B.HP1,'','',B.HP1||'-'||B.HP2||'-'||B.HP3) AS CHAGE_HP,		\n");
        	sqlstr.append("        ? AS ATT_ID,																			\n");
        	sqlstr.append("        B.EMAIL AS CHARGE_EMAIL														\n");
        	sqlstr.append("  FROM SUPP_INFO A , SUPP_CHARGE B												\n");
        	sqlstr.append("   WHERE A.CORP_ID = B.CORP_ID														\n");
        	sqlstr.append("   AND A.CORP_ID = ? AND B.USER_NAME = ?) SUPP ON (SUPP.VENDOR_CD = T.VENDOR_CD AND SUPP.BID_NO = T.BID_NO AND SUPP.BID_DEG = T.BID_DEG)		\n");
        	sqlstr.append("  WHEN MATCHED THEN						\n");
        	sqlstr.append(" UPDATE SET SIGN_DT = SYSDATE		\n");
        	sqlstr.append(" ,ATT_ID = SUPP.ATT_ID					\n");
        	sqlstr.append(" ,ESTI_SIGNDATA = CDATA					\n");
        	sqlstr.append(" ,UPD_BY = ?									\n");
        	sqlstr.append(" ,UPD_DATE = SYSDATE 					\n");
        	sqlstr.append(" ,CURRENCY_GB = ? 					\n");
        	sqlstr.append(" WHEN NOT MATCHED THEN				\n");
        	sqlstr.append(" INSERT (  BID_NO							\n");
        	sqlstr.append("          ,BID_DEG								\n");
        	sqlstr.append("		  ,VENDOR_CD							\n");
        	sqlstr.append("		  ,VENDOR_NO							\n");
        	sqlstr.append("		  ,VENDOR_NAME						\n");
        	sqlstr.append("		  ,REPRE_NO							\n");
        	sqlstr.append("		  ,CHARGE_NAME						\n");
        	sqlstr.append("		  ,CHAGE_HP							\n");
        	sqlstr.append("		  ,CHARGE_EMAIL						\n");
        	sqlstr.append("		  ,SITEPARTI_YN						\n");
        	sqlstr.append("		  ,SITE_ATTEND_YN					\n");
        	sqlstr.append("		  ,ESTI_SIGNDATA					\n");
        	sqlstr.append("		  ,SIGN_DT								\n");
        	sqlstr.append("		  ,ATT_ID								\n");
        	sqlstr.append("		  ,CRE_BY								\n");
        	sqlstr.append("		  ,CRE_DATE							\n");
        	sqlstr.append("		  ,CURRENCY_GB							\n");
        	sqlstr.append(" ) VALUES ( 									\n");
        	sqlstr.append("		  SUPP.BID_NO 						\n");
        	sqlstr.append("		  ,SUPP.BID_DEG 						\n");
        	sqlstr.append("		  ,SUPP.VENDOR_CD 					\n");
        	sqlstr.append("		  ,SUPP.VENDOR_NO 					\n");
        	sqlstr.append("		  ,SUPP.VENDOR_NAME 				\n");
        	sqlstr.append("		  ,SUPP.REPRE_NO 					\n");
        	sqlstr.append("		  ,SUPP.CHARGE_NAME 				\n");
        	sqlstr.append("		  ,SUPP.CHAGE_HP 					\n");
        	sqlstr.append("		  ,SUPP.CHARGE_EMAIL				\n");
        	sqlstr.append("		  , 'N'										\n");
        	sqlstr.append("		  , 'N'										\n");
        	sqlstr.append("		  , CDATA								\n");
        	sqlstr.append("		  , SYSDATE							\n");
        	sqlstr.append("		  , SUPP.ATT_ID						\n");
        	sqlstr.append("		  ,? 										\n");
        	sqlstr.append("		  ,SYSDATE								\n");
        	sqlstr.append("		  ,? 										\n");
        	sqlstr.append(" );													\n");
        	sqlstr.append(" END;												\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(valueObject.get("ESTI_SIGNDATA"));
            persistent.addParameter(valueObject.get("BID_NO"));
            persistent.addParameter(valueObject.get("BID_DEG"));
            persistent.addParameter(storage.getUser().get("CHARGE_SEQ"));
            persistent.addParameter(valueObject.get("VENDOR_CD"));
            persistent.addParameter(storage.getUser().get("USER_NAME"));
            persistent.addParameter(storage.getUser().get("CORP_ID"));
            persistent.addParameter(valueObject.get("CURRENCY_GB"));
            persistent.addParameter(storage.getUser().get("CORP_ID"));
            persistent.addParameter(valueObject.get("CURRENCY_GB"));
            persistent.execute();

        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> bid_supp_update() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	
	/**
     * 입찰포기 저장
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void bidgiveup_supp_update(Connection connection, ValueObject valueObject) throws Exception {

        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append(" DECLARE CDATA CLOB;						\n");
        	sqlstr.append(" BEGIN CDATA := ?;							\n");
        	sqlstr.append(" UPDATE BID_SUPP							\n");
        	sqlstr.append(" SET BID_RNNC_DT = SYSDATE			\n");
        	sqlstr.append(" ,BID_RNNC_ID = ?							\n");
        	sqlstr.append(" ,BID_RNNC_NM = ?							\n");
        	sqlstr.append(" ,RNNC_REASON = ?							\n");
        	sqlstr.append(" ,RNNC_SIGNDATA = CDATA				\n");
        	sqlstr.append(" ,UPD_BY = ?									\n");
        	sqlstr.append(" ,UPD_DATE = SYSDATE 					\n");
        	sqlstr.append(" WHERE BID_NO = ?							\n");
        	sqlstr.append(" AND BID_DEG = ?								\n");
        	sqlstr.append(" AND VENDOR_CD = ?;						\n");
        	sqlstr.append(" END;												\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(valueObject.get("signeddata"));
            persistent.addParameter(storage.getUser().get("CORP_ID"));
            persistent.addParameter(storage.getUser().get("REP_NAME"));
            persistent.addParameter(valueObject.get("RNNCREASON"));
            persistent.addParameter(storage.getUser().get("CORP_ID"));
            persistent.addParameter(valueObject.get("BID_NO"));
            persistent.addParameter(valueObject.get("BID_DEG"));
            persistent.addParameter(storage.getUser().get("CORP_ID"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	

}