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



import java.sql.Connection;

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 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.bid.outside.biz.ScrchInfoBiz;
import oracle.toplink.internal.ox.StrBuffer;

   
public class SuccessBidSaveBiz extends Biz {

	public SuccessBidSaveBiz(HandlerStorage storage) {
		super(storage);
	}
	ValueObject rtnVo = new ValueObject();
	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
		boolean success = false;
		
		if(storage.compareEvent("SAVE_APPR") || storage.compareEvent("SAVE_SHEET")){//저장
			
				/**
				 *                        결제
				 */

			//업체 업데이트
			 success = saveProc(storage.getEvent());
			//마스터 업데이트
			 BidCommonBiz BidCommonBiz = new BidCommonBiz(storage);
			 BidCommonBiz.SP_REG_PSR_STATUS("17",params.get("DEMANDED_ID"),storage.getUser().get("acct_id"));
		}else if(storage.compareEvent("FORM_CANCEL_UPDATE")){//유찰
        	success = BidmasterStatusUpdate(params,storage.getEvent());
        	if("B".equals(params.get("DEMANDED_KIND"))) {
				update_mrreq_po(conn.getConnection(),"91");//유찰일경우 상태 취소로 전달
        	}
        	
        	if(success){
	            // SMS // // E-MAIL //
        		if("Y".equals(params.get("ONBID_YN"))){//2018.2.6 온라인입찰만 발송 
		            ScrchInfoBiz ScrchInfo = new ScrchInfoBiz(storage);
		            ScrchInfo.Send(params, storage.getEvent());
        		}
			}
        }
		
		super.storage.setSuccess(success);
		if(success)
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
	}
	
	  /**
     * 사용자 정보 저장
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean saveProc(String event) {
        try {
            conn.begin();
            
            BidCommonBiz com = new BidCommonBiz(storage);
            String dupCnt = com.getDupCnt1("ebid_ppoD", params.get("BID_NO"),params.get("BID_DEG"));
            
            if( !"0".equals(dupCnt) ){
            	storage.setMessage("시행결의중 입니다.");
            	throw new Exception("시행결의중 입니다.");
            }
            
            String[] status = super.gets("sheet1.STATUS");
            for (int i = 0; i < status.length; i++) {
            	//if (IBSheet7Utils.equalStatusUpdate(status[i])) {//2018.10.1 환율계산 오류.전체재계산 
				ValueObject vo = IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i);
				if (Double.parseDouble(vo.get("ESTI_TOT_AMT_I")) > 0){  //2018.10.10 미참여업체 오류수정 
					saveData(super.conn.getConnection(), vo);
				}
				if(!vo.get("EVAL_RESULT").isEmpty() &&  "N".equals(params.get("ONBID_YN")))
					TBESave(super.conn.getConnection(), vo);
			//}
			}
            
            if( event.equals("SAVE_APPR") ) {//결재  후
            	/*CallableStatement cs 	= null;
        		String pssql			= "";
            	pssql = "{call SP_BID_TO_PURCONT(?,?,?,?)}";
            	cs = super.conn.getConnection().prepareCall(pssql);
            	cs.setString(1, params.get("BID_NO"));
            	cs.setString(2, params.get("BID_DEG"));
                cs.registerOutParameter(3, java.sql.Types.VARCHAR);
                cs.registerOutParameter(4, java.sql.Types.VARCHAR);
                cs.execute();
                rtnVo.set("OUT_SUCCESS_CD",cs.getString(3));
                rtnVo.set("OUT_MESSAGE",cs.getString(4));   
                if("N".equals(cs.getString(3))){
                	throw new Exception(cs.getString(4));
                }
            	update_BidMaster(conn.getConnection(), params);*/
            	saveBidMaster(super.conn.getConnection());
            	
            	mergeSaveBidPurOrder(super.conn.getConnection());
        	}else{
        		
        		saveBidMaster(super.conn.getConnection());
        		
        		mergeSaveBidPurOrder(super.conn.getConnection());

        	}
        		
            return true;
        }
        catch(Exception e) {
        	storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
            WAFLogger.error(this.getClass().getName() + " --> saveProc() : " + e.getMessage());
            WAFLogger.error(e);
            conn.setRollback();

            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	
            conn.end();
        }
    }
	
	/**
     * BID_SUPP 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void saveData(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	String SUCC_YN = "";
        	sqlstr.append("  UPDATE BID_SUPP	         \n");
        	sqlstr.append("  SET SUCC_YN = ?	         \n");
        	sqlstr.append("  ,DISQ_REASON = ?	         \n");
        	sqlstr.append("  ,WON_ESTI_TOT_AMT = ?	     \n");
        	sqlstr.append("  ,SELECT_TOT_AMT = ? 	     \n");
        	sqlstr.append("  ,WON_SELECT_TOT_AMT = ?	 \n");
        	sqlstr.append("  ,EXCHANGE = ?	             \n");
        	/*if(params.get("DIV_CONT_YN").equals("Y")){
	        	sqlstr.append("  ,CONT_AMT = ? 	             \n"); //2015.10.29 도급,실행 예산 분할
	        	sqlstr.append("  ,ACT_AMT = ?	             \n"); //2015.10.29
        	}*/
        	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");
            persistent.setStatement(sqlstr.toString());
            if(valueObject.get("SUCC_YN_CK").equals("1"))
            	SUCC_YN = "Y";
            else if(valueObject.get("SUCC_YN_CK").equals("0"))
            	SUCC_YN = "N";
            persistent.addParameter(SUCC_YN);
            persistent.addParameter(valueObject.get("DISQ_REASON"));
            persistent.addParameter(valueObject.get("WON_ESTI_TOT_AMT").replaceAll(",", ""));
            
            if(params.get("DIV_CONT_YN").equals("Y")){
            	persistent.addParameter(valueObject.get("SEL_ITEM_AMT").replaceAll(",", ""));
            }else{
            	if(params.get("DIV_PO_YN").equals("Y")){
            		persistent.addParameter(valueObject.get("WON_ESTI_TOT_AMT").replaceAll(",", ""));
            	}else{
            		persistent.addParameter(valueObject.get("ESTI_TOT_AMT").replaceAll(",", ""));
            	}
            }

            if(params.get("DIV_CONT_YN").equals("Y")){
            	persistent.addParameter(valueObject.get("WON_SEL_TOT_AMT").replaceAll(",", ""));
            }else{
            	persistent.addParameter(valueObject.get("WON_ESTI_TOT_AMT").replaceAll(",", ""));
            }
            
            persistent.addParameter(valueObject.get("EXCHANGE"));
            
            /*if(params.get("DIV_CONT_YN").equals("Y")){
	            ValueObject vo = getAmtValueObject(valueObject.get("BID_NO"),valueObject.get("VENDOR_CD"));
	            persistent.addParameter(vo.get("cont_amt")); //2015.10.29 도급,실행 예산 분할
	            persistent.addParameter(vo.get("act_amt")); //2015.10.29
            }*/
            
            persistent.addParameter(storage.getUser().get("ACCT_ID"));
            persistent.addParameter(valueObject.get("BID_NO"));
            persistent.addParameter(valueObject.get("BID_DEG"));
            persistent.addParameter(valueObject.get("VENDOR_CD"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(persistent.getParameters());
            WAFLogger.error(e);
            throw e;
        }
    }
	
	/**
     * BID_SUPP 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void TBESave(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("  MERGE INTO TBE_SUPP T USING	\n");
        	sqlstr.append("  (SELECT ? AS BID_NO					\n");
        	sqlstr.append("  , ? AS BID_DEG							\n");
        	sqlstr.append("  , ? AS VENDOR_CD						\n");
        	sqlstr.append("  , ? AS EVAL_RESULT					\n");
        	sqlstr.append("  FROM DUAL) D ON (T.BID_NO = D.BID_NO AND T.BID_DEG = D.BID_DEG AND T.VENDOR_CD = D.VENDOR_CD)					\n");
        	sqlstr.append("  WHEN MATCHED THEN					\n");
        	sqlstr.append("  UPDATE					\n");
        	sqlstr.append("  SET EVAL_RESULT = D.EVAL_RESULT					\n");
        	sqlstr.append("  WHEN NOT MATCHED THEN					\n");
        	sqlstr.append("  INSERT ( BID_NO,BID_DEG,VENDOR_CD,EVAL_RESULT)					\n");
        	sqlstr.append("  VALUES					\n");
        	sqlstr.append("  (D.BID_NO,D.BID_DEG,D.VENDOR_CD,D.EVAL_RESULT)					\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(valueObject.get("BID_NO"));
            persistent.addParameter(valueObject.get("BID_DEG"));
            persistent.addParameter(valueObject.get("VENDOR_CD"));
            persistent.addParameter(valueObject.get("EVAL_RESULT"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> TBESave() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	/**
     * 원화 업데이트
	 * @param connection
	 * @throws Exception
	 */
	public void saveSuppData(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("  UPDATE BID_SUPP 	\n");
        	sqlstr.append("  SET WON_ESTI_TOT_AMT=?	\n"); 
        	sqlstr.append("  ,EXCHANGE=? 	\n");
        	sqlstr.append("  WHERE BID_NO = ?	\n");
        	sqlstr.append("  AND BID_DEG = ?	\n");
        	sqlstr.append("  AND VENDOR_CD = ?	\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(params.get("BID_NO"));
            persistent.addParameter(params.get("BID_DEG"));
            persistent.addParameter(valueObject.get("VENDOR_CD"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> saveSuppData() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	/**
	 * 원화 저장 여부 BID_MASTER에 업데이트
	 * @param connection
	 * @throws Exception
	 */
	public void saveBidMaster(Connection connection) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append("  UPDATE BID_MASTER 	\n");
			sqlstr.append("  SET KRW_APPR_YN=?	\n"); 
			sqlstr.append("  ,CBC_FIRST_RATE = ?	\n");
			sqlstr.append("  ,PBC_FIRST_CONDI = ?	\n");
			sqlstr.append("  ,CPC_MID_RATE = ?	\n");
			sqlstr.append("  ,PBC_MID_CONDI = ?	\n");
			sqlstr.append("  ,PBC_REST_RATE = ?	\n");
			sqlstr.append("  ,PBC_REST_CONDI = ?	\n");
			sqlstr.append("  ,DELI_FROM_DY = ?	\n");
			sqlstr.append("  ,DELI_TO_DY = ?	\n");
			sqlstr.append("  ,CON_REMARK = ?	\n");
			sqlstr.append("  ,DIV_CONT_YN = ?	\n");
			sqlstr.append("  ,CONT_AMT = ?	\n");   //2015.02.27 실행예산 시행결의전 수정 mklee
			sqlstr.append("  ,ACT_AMT = ?	\n");   //2015.02.27 실행예산 시행결의전 수정 mklee
			sqlstr.append("  ,ACT_RATE = ?	\n");   //2015.02.27 실행예산 시행결의전 수정 mklee
			sqlstr.append("  ,PUR_AMT = ?	\n");   //2015.02.27 구매예산 시행결의전 수정 mklee
			sqlstr.append("  ,PAY_DIV = ?	\n");   //2015.03.31 납품구분 추가 chj
			sqlstr.append("  WHERE BID_NO = ?	\n");
			sqlstr.append("  AND BID_DEG = ?	\n");
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(params.get("KRW_APPR_YN"));
			persistent.addParameter(params.get("CBC_FIRST_RATE"));//선급금
			persistent.addParameter(params.get("PBC_FIRST_CONDI"));
			persistent.addParameter(params.get("CPC_MID_RATE"));//중도금
			persistent.addParameter(params.get("PBC_MID_CONDI"));
			persistent.addParameter(params.get("PBC_REST_RATE"));//잔금
			persistent.addParameter(params.get("PBC_REST_CONDI"));
			persistent.addParameter((params.get("DELI_FROM_DY")).replace("-",""));
			persistent.addParameter((params.get("DELI_TO_DY")).replace("-",""));
			persistent.addParameter(params.get("CON_REMARK"));
			persistent.addParameter(params.get("DIV_CONT_YN"));
			persistent.addParameter(StringUtils.replace(params.get("CONT_AMT"),",",""));    //2015.02.27 실행예산 시행결의전 수정 mklee
			persistent.addParameter(StringUtils.replace(params.get("ACT_AMT"),",",""));    //2015.02.27 실행예산 시행결의전 수정 mklee
			persistent.addParameter(params.get("ACT_RATE"));                               //2015.02.27 실행예산 시행결의전 수정 mklee
			persistent.addParameter(StringUtils.replace(params.get("PUR_AMT"),",",""));    //2015.02.27 구매예산 시행결의전 수정 mklee
			persistent.addParameter(params.get("PAY_DIV"));                               //2015.03.31 납품구분 추가 chj
			persistent.addParameter(params.get("BID_NO"));
			persistent.addParameter(params.get("BID_DEG"));
			persistent.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> saveSuppData() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(params.get("ACT_AMT"));
			WAFLogger.error(params.get("ACT_RATE"));
			WAFLogger.error(e);
			throw e;
		}
	}

	/**
	 * 원화 저장 여부 BID_MASTER에 업데이트
	 * @param connection
	 * @throws Exception
	 */
	public void mergeSaveBidPurOrder(Connection connection) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			
			sqlstr.append("									MERGE INTO BID_PUR_ORDER DD USING(                                                                            	\n");
			sqlstr.append("									                        SELECT  A.BID_NO                                                                      	\n");
			sqlstr.append("									                                  ,   A.BID_DEG                                                               	\n");
			sqlstr.append("									                                  ,  B.VENDOR_CD                                                              	\n");
			sqlstr.append("									                                  ,  C.BID_CONDI_NOTI                                                         	\n");
			sqlstr.append("									                                  , A.CBC_DAMAGE_RATE                                                         	\n");
			sqlstr.append("									                                  , A.CBC_DAMAGE_MAXRATE                                                      	\n");
			sqlstr.append("									                                  , A.CBC_DAMAGE_CD		                                                      	\n");
			sqlstr.append("									                                  , C.CBC_FIRST_RATE                                                          	\n");
			sqlstr.append("									                                  , C.PBC_FIRST_CONDI                                                         	\n");
			sqlstr.append("									                                  , C.CPC_MID_RATE                                                            	\n");
			sqlstr.append("									                                  , C.PBC_MID_CONDI                                                           	\n");
			sqlstr.append("									                                  , C.PBC_REST_RATE                                                           	\n");
			sqlstr.append("									                                  , C.PBC_REST_CONDI                                                          	\n");
			sqlstr.append("									                                  , C.FRSTGUAR_YN                                                             	\n");
			sqlstr.append("									                                  , A.PBC_FRSTGUAR_RATE                                                       	\n");
			sqlstr.append("									                                  , A.PBC_FRSTGUAR_CONDI                                                      	\n");
			sqlstr.append("									                                  , C.FRSTGUAR_NOT_NOTI                                                       	\n");
			sqlstr.append("									                                  , C.CONTFUL_YN                                                              	\n");
			sqlstr.append("									                                  , A.CONTFUL_RATE                                                            	\n");
			sqlstr.append("									                                  , A.CONTFUL_CONDI                                                           	\n");
			sqlstr.append("									                                  , C.CONTFUL_NOT_NOTI                                                        	\n");
			sqlstr.append("									                                  , C.WARR_YN                                                                 	\n");
			sqlstr.append("									                                  , A.WARR_RATE                                                               	\n");
			sqlstr.append("									                                  , A.WARR_CONDI                                                              	\n");
			sqlstr.append("									                                  , C.WARR_NOT_NOTI                                                           	\n");
			sqlstr.append("									                                  , A.PBC_DEFECT_CONDI                                                        	\n");
			sqlstr.append("									                                  , A.CBC_DEFECT_DAY                                                         	\n");
			sqlstr.append("									                                  , A.CBC_DERECT_TERM_CD                                                      	\n");
			sqlstr.append("									                                  , A.PBC_DEFECT_CONDI2                                                       	\n");
			sqlstr.append("									                                  , A.CBC_DEFECT_DAY2                                                         	\n");
			sqlstr.append("									                                  , A.CBC_DERECT_TERM_CD2                                                     	\n");
			sqlstr.append("									                                  , C.DEFECT_YN                                                               	\n");
			sqlstr.append("									                                  , C.DEFECT_NOT_NOTI                                                         	\n");
			sqlstr.append("									                                  , A.DELI_FROM_DY                                                            	\n");
			sqlstr.append("									                                  , A.DELI_TO_DY                                                              	\n");
			sqlstr.append("									                                  , ?      AS CRE_BY                                                      		\n");
			sqlstr.append("									                                  , SYSDATE  AS CRE_DATE                                                      	\n");
			sqlstr.append("									                        FROM BID_MASTER A ,                                                                   	\n");
			sqlstr.append("									                                   BID_SUPP B ,                                                               	\n");
			sqlstr.append("									                                   BID_PUR_CONDI C                                                            	\n");
			sqlstr.append("									                        WHERE A.BID_NO =B.BID_NO                                                              	\n");
			sqlstr.append("									                        AND      A.BID_DEG = B.BID_DEG                                                        	\n");
			sqlstr.append("									                        AND      B.BID_NO = C.BID_NO                                                          	\n");
			sqlstr.append("									                        AND      B.BID_DEG= C.BID_DEG                                                         	\n");
			sqlstr.append("									                        AND      B.VENDOR_CD = C.VENDOR_CD                                                    	\n");
			sqlstr.append("									                        AND     A.BID_NO  =  ?                                                					\n");
			sqlstr.append("									                        AND     A.BID_DEG = ?                                                               	\n");
			sqlstr.append("									                        AND     B.SUCC_YN = 'Y'                                                               	\n");
			sqlstr.append("									) MD ON (DD.BID_NO = MD.BID_NO AND DD.BID_DEG = MD.BID_DEG AND DD.VENDOR_CD = MD.VENDOR_CD)                   	\n");
			sqlstr.append("									WHEN MATCHED THEN                                                                                             	\n");
			sqlstr.append("									UPDATE SET                                                                                                    	\n");
			sqlstr.append("									        DD.BID_CONDI_NOTI  =             MD.BID_CONDI_NOTI                                                    	\n");
			sqlstr.append("									,       DD.CBC_DAMAGE_RATE =             MD.CBC_DAMAGE_RATE                                                   	\n");
			sqlstr.append("									,       DD.CBC_DAMAGE_MAXRATE =     MD.CBC_DAMAGE_MAXRATE                                                     	\n");
			sqlstr.append("									,       DD.CBC_DAMAGE_CD =     		MD.CBC_DAMAGE_CD	                                                     	\n");
			sqlstr.append("									,       DD.CBC_FIRST_RATE =             MD.CBC_FIRST_RATE                                                     	\n");
			sqlstr.append("									,       DD.PBC_FIRST_CONDI =             MD.PBC_FIRST_CONDI                                                   	\n");
			sqlstr.append("									,       DD.CPC_MID_RATE =                 MD.CPC_MID_RATE                                                     	\n");
			sqlstr.append("									,       DD.PBC_MID_CONDI =                 MD.PBC_MID_CONDI                                                   	\n");
			sqlstr.append("									,       DD.PBC_REST_RATE =                 MD.PBC_REST_RATE                                                   	\n");
			sqlstr.append("									,       DD.PBC_REST_CONDI =             MD.PBC_REST_CONDI                                                     	\n");
			sqlstr.append("									,       DD.FRSTGUAR_YN =                     MD.FRSTGUAR_YN                                                   	\n");
			sqlstr.append("									,       DD.PBC_FRSTGUAR_RATE =         MD.PBC_FRSTGUAR_RATE                                                   	\n");
			sqlstr.append("									,       DD.PBC_FRSTGUAR_CONDI =     MD.PBC_FRSTGUAR_CONDI                                                     	\n");
			sqlstr.append("									,       DD.FRSTGUAR_NOT_NOTI =         MD.FRSTGUAR_NOT_NOTI                                                   	\n");
			sqlstr.append("									,       DD.CONTFUL_YN =                     MD.CONTFUL_YN                                                     	\n");
			sqlstr.append("									,       DD.CONTFUL_RATE =                 MD.CONTFUL_RATE                                                     	\n");
			sqlstr.append("									,       DD.CONTFUL_CONDI =                 MD.CONTFUL_CONDI                                                   	\n");
			sqlstr.append("									,       DD.CONTFUL_NOT_NOTI =         MD.CONTFUL_NOT_NOTI                                                     	\n");
			sqlstr.append("									,       DD.WARR_YN =                             MD.WARR_YN                                                   	\n");
			sqlstr.append("									,       DD.WARR_RATE =                         MD.WARR_RATE                                                   	\n");
			sqlstr.append("									,       DD.WARR_CONDI =                     MD.WARR_CONDI                                                     	\n");
			sqlstr.append("									,       DD.WARR_NOT_NOTI =                 MD.WARR_NOT_NOTI                                                   	\n");
			sqlstr.append("									,       DD.DEFECT_YN =                         MD.DEFECT_YN                                                   	\n");
			sqlstr.append("									,       DD.PBC_DEFECT_CONDI =         MD.PBC_DEFECT_CONDI                                                     	\n");
			sqlstr.append("									,       DD.CBC_DEFECT_DAY =             MD.CBC_DEFECT_DAY                                                     	\n");
			sqlstr.append("									,       DD.CBC_DERECT_TERM_CD =     MD.CBC_DERECT_TERM_CD                                                     	\n");
			sqlstr.append("									,       DD.PBC_DEFECT_CONDI2 =         MD.PBC_DEFECT_CONDI2                                                   	\n");
			sqlstr.append("									,       DD.CBC_DEFECT_DAY2 =             MD.CBC_DEFECT_DAY2                                                   	\n");
			sqlstr.append("									,       DD.CBC_DERECT_TERM_CD2 =     MD.CBC_DERECT_TERM_CD2                                                   	\n");
			sqlstr.append("									,       DD.DEFECT_NOT_NOTI =             MD.DEFECT_NOT_NOTI                                                   	\n");
			sqlstr.append("									,       DD.DELI_FROM_DY =                 MD.DELI_FROM_DY                                                     	\n");
			sqlstr.append("									,       DD.DELI_TO_DY =                     MD.DELI_TO_DY                                                     	\n");
			sqlstr.append("									,       DD.UPD_BY =                             MD.CRE_BY                                                     	\n");
			sqlstr.append("									,       DD.UPD_DATE =                         MD.CRE_DATE                                                     	\n");
			sqlstr.append("									WHEN NOT MATCHED THEN                                                                                         	\n");
			sqlstr.append("									INSERT (    DD.BID_NO                                                                                         	\n");
			sqlstr.append("									                , DD.BID_DEG                                                                                  	\n");
			sqlstr.append("									                , DD.VENDOR_CD                                                                                	\n");
			sqlstr.append("									                , DD.BID_CONDI_NOTI                                                                           	\n");
			sqlstr.append("									                , DD.CBC_DAMAGE_RATE                                                                          	\n");
			sqlstr.append("									                , DD.CBC_DAMAGE_MAXRATE                                                                       	\n");
			sqlstr.append("									                , DD.CBC_DAMAGE_CD		                                                                       	\n");
			sqlstr.append("									                , DD.CBC_FIRST_RATE                                                                           	\n");
			sqlstr.append("									                , DD.PBC_FIRST_CONDI                                                                          	\n");
			sqlstr.append("									                , DD.CPC_MID_RATE                                                                             	\n");
			sqlstr.append("									                , DD.PBC_MID_CONDI                                                                            	\n");
			sqlstr.append("									                , DD.PBC_REST_RATE                                                                            	\n");
			sqlstr.append("									                , DD.PBC_REST_CONDI                                                                           	\n");
			sqlstr.append("									                , DD.FRSTGUAR_YN                                                                              	\n");
			sqlstr.append("									                , DD.PBC_FRSTGUAR_RATE                                                                        	\n");
			sqlstr.append("									                , DD.PBC_FRSTGUAR_CONDI                                                                       	\n");
			sqlstr.append("									                , DD.FRSTGUAR_NOT_NOTI                                                                        	\n");
			sqlstr.append("									                , DD.CONTFUL_YN                                                                               	\n");
			sqlstr.append("									                , DD.CONTFUL_RATE                                                                             	\n");
			sqlstr.append("									                , DD.CONTFUL_CONDI                                                                            	\n");
			sqlstr.append("									                , DD.CONTFUL_NOT_NOTI                                                                         	\n");
			sqlstr.append("									                , DD.WARR_YN                                                                                  	\n");
			sqlstr.append("									                , DD.WARR_RATE                                                                                	\n");
			sqlstr.append("									                , DD.WARR_CONDI                                                                               	\n");
			sqlstr.append("									                , DD.WARR_NOT_NOTI                                                                            	\n");
			sqlstr.append("									                , DD.DEFECT_YN                                                                                	\n");
			sqlstr.append("									                , DD.PBC_DEFECT_CONDI                                                                         	\n");
			sqlstr.append("									                , DD.CBC_DEFECT_DAY                                                                           	\n");
			sqlstr.append("									                , DD.CBC_DERECT_TERM_CD                                                                       	\n");
			sqlstr.append("									                , DD.PBC_DEFECT_CONDI2                                                                        	\n");
			sqlstr.append("									                , DD.CBC_DEFECT_DAY2                                                                          	\n");
			sqlstr.append("									                , DD.CBC_DERECT_TERM_CD2                                                                      	\n");
			sqlstr.append("									                , DD.DEFECT_NOT_NOTI                                                                          	\n");
			sqlstr.append("									                , DD.DELI_FROM_DY                                                                             	\n");
			sqlstr.append("									                , DD.DELI_TO_DY                                                                               	\n");
			sqlstr.append("									                , DD.CRE_BY                                                                                   	\n");
			sqlstr.append("									                , DD.CRE_DATE                                                                                 	\n");
			sqlstr.append("									)VALUES (                                                                                                     	\n");
			sqlstr.append("									                  MD.BID_NO                                                                                   	\n");
			sqlstr.append("									                , MD.BID_DEG                                                                                  	\n");
			sqlstr.append("									                , MD.VENDOR_CD                                                                                	\n");
			sqlstr.append("									                , MD.BID_CONDI_NOTI                                                                           	\n");
			sqlstr.append("									                , MD.CBC_DAMAGE_RATE                                                                          	\n");
			sqlstr.append("									                , MD.CBC_DAMAGE_MAXRATE                                                                       	\n");
			sqlstr.append("									                , MD.CBC_DAMAGE_CD		                                                                      	\n");
			sqlstr.append("									                , MD.CBC_FIRST_RATE                                                                           	\n");
			sqlstr.append("									                , MD.PBC_FIRST_CONDI                                                                          	\n");
			sqlstr.append("									                , MD.CPC_MID_RATE                                                                             	\n");
			sqlstr.append("									                , MD.PBC_MID_CONDI                                                                            	\n");
			sqlstr.append("									                , MD.PBC_REST_RATE                                                                            	\n");
			sqlstr.append("									                , MD.PBC_REST_CONDI                                                                           	\n");
			sqlstr.append("									                , MD.FRSTGUAR_YN                                                                              	\n");
			sqlstr.append("									                , MD.PBC_FRSTGUAR_RATE                                                                       	\n");
			sqlstr.append("									                , MD.PBC_FRSTGUAR_CONDI                                                                       	\n");
			sqlstr.append("									                , MD.FRSTGUAR_NOT_NOTI                                                                        	\n");
			sqlstr.append("									                , MD.CONTFUL_YN                                                                               	\n");
			sqlstr.append("									                , MD.CONTFUL_RATE                                                                             	\n");
			sqlstr.append("									                , MD.CONTFUL_CONDI                                                                            	\n");
			sqlstr.append("									                , MD.CONTFUL_NOT_NOTI                                                                         	\n");
			sqlstr.append("									                , MD.WARR_YN                                                                                  	\n");
			sqlstr.append("									                , MD.WARR_RATE                                                                                	\n");
			sqlstr.append("									                , MD.WARR_CONDI                                                                               	\n");
			sqlstr.append("									                , MD.WARR_NOT_NOTI                                                                            	\n");
			sqlstr.append("									                , MD.DEFECT_YN                                                                                	\n");
			sqlstr.append("									                , MD.PBC_DEFECT_CONDI                                                                         	\n");
			sqlstr.append("									                , MD.CBC_DEFECT_DAY                                                                           	\n");
			sqlstr.append("									                , MD.CBC_DERECT_TERM_CD                                                                       	\n");
			sqlstr.append("									                , MD.PBC_DEFECT_CONDI2                                                                        	\n");
			sqlstr.append("									                , MD.CBC_DEFECT_DAY2                                                                          	\n");
			sqlstr.append("									                , MD.CBC_DERECT_TERM_CD2                                                                      	\n");
			sqlstr.append("									                , MD.DEFECT_NOT_NOTI                                                                          	\n");
			sqlstr.append("									                , MD.DELI_FROM_DY                                                                             	\n");
			sqlstr.append("									                , MD.DELI_TO_DY                                                                               	\n");
			sqlstr.append("									                , MD.CRE_BY                                                                                   	\n");
			sqlstr.append("									                , MD.CRE_DATE                                                                                 	\n");
			sqlstr.append("									)																												\n");
			
			
			
			persistent.setStatement(sqlstr.toString());
			
			persistent.addParameter(storage.getUser().get("acct_id"));
			persistent.addParameter(params.get("BID_NO"));
			persistent.addParameter(params.get("BID_DEG"));
			persistent.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> saveSuppData() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(params.get("ACT_AMT"));
			WAFLogger.error(params.get("ACT_RATE"));
			WAFLogger.error(e);
			throw e;
		}
	}
	
	/**
     * 경적대상업체 저장
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void update_BidMaster(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("  UPDATE BID_MASTER			\n");
        	sqlstr.append("  SET STATUS_CD = '10'		\n");
        	sqlstr.append("  ,UPD_BY = ?						\n");
        	sqlstr.append("  ,UPD_DATE = SYSDATE		\n");
        	sqlstr.append("  WHERE BID_NO = ? 			\n");
        	sqlstr.append("  AND BID_DEG = ?				\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(storage.getUser().get("ACCT_ID"));
            persistent.addParameter(params.get("BID_NO"));
            persistent.addParameter(params.get("BID_DEG"));
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	
	/**
     * 유찰 업데이트 
	 * @param valueObject
	 * @param event
	 * @return
	 */
	public boolean BidmasterStatusUpdate(ValueObject valueObject,String event) {
		conn.begin(); 
		StrBuffer sqlstr = new StrBuffer();
        try {
        	
        	Persistent persistent = new PersistentImpl(conn.getConnection());
            sqlstr.append("UPDATE BID_MASTER		\n");
        	sqlstr.append("SET STATUS_CD = ?			\n");
       		sqlstr.append(",FIB_REASON = ?				\n");
        	sqlstr.append(",UPD_BY = ?					\n");
        	sqlstr.append(",UPD_DATE = SYSDATE		\n");
        	sqlstr.append("WHERE BID_NO = ?			\n");
        	sqlstr.append("AND BID_DEG = ?			\n");
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter("91");
            persistent.addParameter(valueObject.get("FIBREASON"));
            persistent.addParameter(storage.getUser().get("ACCT_ID"));
            persistent.addParameter(valueObject.get("BID_NO"));
            persistent.addParameter(valueObject.get("BID_DEG"));
            persistent.execute();
            return true;
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> BidmasterStatusUpdate() : " + e.getMessage());
            WAFLogger.error(e);
            conn.setRollback();
            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	
            conn.end();
        }
    }
	
	public boolean update_mrreq_po(Connection connection,String ebid_staus_code) throws Exception {
		return update_mrreq_po(connection,ebid_staus_code,"");
	}

	/**
	 * 발주의뢰 상태값 업데이트
	 * @return
	 */
	public boolean update_mrreq_po(Connection connection,String ebid_staus_code,String status_code) throws Exception {
		StrBuffer sqlstr = new StrBuffer();
		Persistent pstmt = new PersistentImpl(connection);
        try{
        		sqlstr.append("	UPDATE ETECEPC.MR_REQ_PO							\n");
				sqlstr.append("	SET EBID_STATUS = ?									\n");
				if("9".equals(status_code))
					sqlstr.append("	, REQ_STATUS = ?										\n");
				sqlstr.append("	WHERE BUYREQNO = ?									\n");
		       	pstmt.setStatement(sqlstr.toString());
		       	pstmt.addParameter(ebid_staus_code);
		       	if("9".equals(status_code))
		       		pstmt.addParameter("9");
				pstmt.addParameter(params.get("DEMANDED_ID"));
				pstmt.execute();
				return true;
	        }catch (Exception e){
				WAFLogger.error(e);
				throw new Exception(e);
	        }
	}
	
	/**
	 * 발주의뢰 상태값 업데이트
	 * @return
	 */
	private boolean update_pmm_demanded_request(Connection connection,String status_code) throws Exception {
		StrBuffer sqlstr = new StrBuffer();
		Persistent pstmt = new PersistentImpl(connection);
        try{
	    		sqlstr.append("	UPDATE WC_Z_ETEC_PMM_DEMANDED_REQUEST@ERPIU						\n");
				sqlstr.append("	SET EBID_STATUS_CD = '"+status_code+"' ,DEMANDED_STATUS = '5'	\n");
				sqlstr.append("	WHERE DEMANDED_ID = "+params.get("DEMANDED_ID")+"				\n");
		       	pstmt.setStatement(sqlstr.toString());
				pstmt.execute();
				return true;
	        }catch (Exception e){
				WAFLogger.error(e);
				throw new Exception(e);
	        }
	}
	
	private ValueObject getAmtValueObject(String BID_NO, String VENDOR_CD) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
        StrBuffer sqlstr = new StrBuffer();
        try{
	        	sqlstr.append("	SELECT SUM(A.SELECT_QTY * B.CONT_UNIT_AMT) CONT_AMT         \n");
	        	sqlstr.append("       ,SUM(A.SELECT_QTY * B.ACT_UNIT_AMT) ACT_AMT           \n");
	        	sqlstr.append("   FROM BID_SUPP_ESTI A                                      \n");
	        	sqlstr.append("       ,BID_ITEM B                                           \n");
	        	sqlstr.append("  WHERE A.BID_NO = B.BID_NO                                  \n");
	        	sqlstr.append("    AND A.BID_DEG = B.BID_DEG                                \n");
	        	sqlstr.append("    AND A.MATERIAL_CD = B.MATERIAL_CD                        \n");
	        	sqlstr.append("    AND A.BID_NO = '" + BID_NO + "'                          \n");
	        	sqlstr.append("    AND A.VENDOR_CD = '" + VENDOR_CD + "'                    \n");
	 
	        	search.setStatement(sqlstr.toString());
	            rowSet = search.execute();

	            if( rowSet != null && rowSet.size() > 0 ){
	            	valueobject = RowSetUtility.getValueObject(rowSet.getRow(0));
                }
        }
        catch(WAFSQLException se) {
        	super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {
        	if( valueobject == null ) valueobject = new ValueObject();
            return valueobject;
        }
	}
}