/**
 * @(#)file    ModStatusSaveBiz.java
 * @(#)author  hoonseok choi
 * @(#)date    2021.3.3
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.test.biz;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.logger.Logger;
import kr.co.hsnc.common.sql.Row;
import kr.co.hsnc.common.sql.RowImpl;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.WAFConnectionImpl;
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.util.ValueObject;
import kr.co.udapsoft.common.commonSign.CommonSignInterface;
import kr.co.udapsoft.common.commonSign.CommonSignManager;
import oracle.toplink.internal.ox.StrBuffer;

import com.udapsoft.waf.common.util.ClobUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

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

	public void doBiz() throws Exception {		
		if(storage.compareEvent("SAVE_MOD_LIST")){//SQL 수정
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveModList(params);
	        storage.setSuccess(success);
		}else if(storage.compareEvent("DEL_MOD_LIST")){//SQL 삭제
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = delModList(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_BID_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveBidStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_SUPP_BID_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveSuppBidStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_BID_MASTER_NM")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveBidMasterNm(super.conn.getConnection(),params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_TBE_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveTbeStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_EXCHANGE_VALUE")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveExchange(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_ERP_SIGN_CLASS")) {
			boolean success = true;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        saveErpSignClass(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("FORM_UPDATE") || storage.compareEvent("FORM_CONT_UPDATE")) {
			templateSaveData();
		}else if (storage.compareEvent("SAVE_PUR_CONT_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = savePurContStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_PUR_WARR_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = savePurWarrStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_SUB_CONT_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveSubContStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_SUB_WARR_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveSubWarrStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("DEL_OUT_COMP_APPR")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = delOutCompAppr(params);
	        storage.setSuccess(success);
		}else if(storage.compareEvent("START_SIGN_TEST")){//SQL 수정
			RowSet rowSignInterface = getIFSignInfo(params);
    		setSign(rowSignInterface);
			
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = signTest(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_SUB_PAY_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = saveSubPayStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_PUR_PAY_STATUS")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = savePurPayStatus(params);
	        storage.setSuccess(success);
		}else if (storage.compareEvent("SAVE_PUR_REVISE_REMAINDER")) {
			boolean success = false;
	        params.set("login_user", storage.getUser().get("ACCT_ID"));
	        success = savePurReviseRemainder(params);
	        storage.setSuccess(success);
		}
	}
	
	public boolean saveModList(ValueObject params) {
        Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
        
		sqlstr_u.append("	UPDATE "+params.get("TABLE_NM")+" SET	\n");
		sqlstr_u.append(""+params.get("SET_1")+" = "+params.get("SET_1_VAL")+"          	\n");
		if(!params.get("SET_2").isEmpty())		sqlstr_u.append(", "+params.get("SET_2")+" = "+params.get("SET_2_VAL")+"          	\n");
		if(!params.get("SET_3").isEmpty())		sqlstr_u.append(", "+params.get("SET_3")+" = "+params.get("SET_3_VAL")+"          	\n");
		if(!params.get("SET_4").isEmpty())		sqlstr_u.append(", "+params.get("SET_4")+" = "+params.get("SET_4_VAL")+"          	\n");
		sqlstr_u.append("WHERE "+params.get("WHERE")+" = '"+params.get("WHERE_VAL")+"'     	\n");
		if(!params.get("AND_1").isEmpty())		sqlstr_u.append("AND "+params.get("AND_1")+" = '"+params.get("AND_1_VAL")+"'     	\n");
		if(!params.get("AND_2").isEmpty())		sqlstr_u.append("AND "+params.get("AND_2")+" = '"+params.get("AND_2_VAL")+"'     	\n");
		if(!params.get("AND_3").isEmpty())		sqlstr_u.append("AND "+params.get("AND_3")+" = '"+params.get("AND_3_VAL")+"'     	\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            //WAFLogger.debug(params);
            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();
        }
    }
		
	public boolean delModList(ValueObject params) {
        Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
        
		sqlstr_u.append("	DELETE "+params.get("DEL_TABLE_NM")+"	\n");
		sqlstr_u.append("WHERE "+params.get("DEL_WHERE")+" = '"+params.get("DEL_WHERE_VAL")+"'     	\n");
		if(!params.get("DEL_AND_1").isEmpty())		sqlstr_u.append("AND "+params.get("DEL_AND_1")+" = '"+params.get("DEL_AND_1_VAL")+"'     	\n");
		if(!params.get("DEL_AND_2").isEmpty())		sqlstr_u.append("AND "+params.get("DEL_AND_2")+" = '"+params.get("DEL_AND_2_VAL")+"'     	\n");
		if(!params.get("DEL_AND_3").isEmpty())		sqlstr_u.append("AND "+params.get("DEL_AND_3")+" = '"+params.get("DEL_AND_3_VAL")+"'     	\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            //WAFLogger.debug(params);
            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();
        }
    }
	
	private boolean saveBidStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE BID_MASTER            				\n");
        sqlstr_u.append("				   SET STATUS_CD  = '"+params.get("MOD_STATUS_CD")+"'							\n");
        sqlstr_u.append("				 WHERE BID_NO     = '"+params.get("BID_NO")+"'								\n");
        sqlstr_u.append("				   AND BID_DEG    = '"+params.get("BID_DEG")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}

	private boolean saveSuppBidStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE BID_SUPP            				\n");
        sqlstr_u.append("				   SET bid_status_cd  = '"+params.get("SUPP_MOD_STATUS_CD")+"'							\n");
        sqlstr_u.append("				 WHERE BID_NO     = '"+params.get("BID_NO")+"'								\n");
        sqlstr_u.append("				   AND BID_DEG    = '"+params.get("BID_DEG")+"'								\n");
        sqlstr_u.append("				   AND VENDOR_CD  = '"+params.get("VENDOR_CD")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
	
	private boolean saveBidMasterNm(Connection connection, ValueObject params) {
        Persistent persistent_u = new PersistentImpl(connection);
        StrBuffer sqlstr_u = new StrBuffer();

		try {
			
			sqlstr_u.append("				UPDATE ETEC_EBID.PMM_DEMANDED_REQUEST            				\n");
	        sqlstr_u.append("				   SET DEMANDED_TITLE  = '"+params.get("MOD_BID_MASTER_NM")+"'							\n");
	        sqlstr_u.append("				 WHERE DEMANDED_ID     = '"+params.get("DEMANDED_ID")+"'								\n");
	        WAFLogger.debug(sqlstr_u.toString());
	        persistent_u.setStatement(sqlstr_u.toString());              
	        persistent_u.execute();
	        persistent_u.clearParameters();
	        sqlstr_u.reset();
            
	        sqlstr_u.append("				UPDATE ETEC_ERPIF.PMM_DEMANDED_REQUEST            				\n");
	        sqlstr_u.append("				   SET DEMANDED_TITLE  = '"+params.get("MOD_BID_MASTER_NM")+"'							\n");
	        sqlstr_u.append("				 WHERE DEMANDED_ID     = '"+params.get("DEMANDED_ID")+"'								\n");
	        WAFLogger.debug(sqlstr_u.toString());
	        persistent_u.setStatement(sqlstr_u.toString());              
	        persistent_u.execute();
	        persistent_u.clearParameters();
	        sqlstr_u.reset();
	        
	        sqlstr_u.append("				UPDATE BID_MASTER            				\n");
	        sqlstr_u.append("				   SET DEMANDED_TITLE  = '"+params.get("MOD_BID_MASTER_NM")+"'							\n");
	        sqlstr_u.append("				     , BID_MASTER_NM   = '"+params.get("MOD_BID_MASTER_NM")+"'							\n");
	        sqlstr_u.append("				 WHERE DEMANDED_ID     = '"+params.get("DEMANDED_ID")+"'								\n");
	        WAFLogger.debug(sqlstr_u.toString());
	        persistent_u.setStatement(sqlstr_u.toString());              
	        persistent_u.execute();
	        persistent_u.clearParameters();
	        sqlstr_u.reset();
			
            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();
        }
	}
	
	private boolean saveTbeStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE TBE_INFO            				\n");
        sqlstr_u.append("				   SET STATUS_CD  = '"+params.get("TBE_MOD_STATUS_CD")+"'							\n");
        sqlstr_u.append("				 WHERE BID_NO     = '"+params.get("BID_NO")+"'								\n");
        sqlstr_u.append("				   AND BID_DEG    = '"+params.get("BID_DEG")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
	
	private boolean saveExchange(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE BID_SUPP            				\n");
        sqlstr_u.append("				   SET EXCHANGE  = '"+params.get("MOD_EXCHANGE")+"'								\n");
        sqlstr_u.append("				     , WON_ESTI_TOT_AMT   =   ESTI_TOT_AMT * '"+params.get("MOD_EXCHANGE")+"'	\n");
        sqlstr_u.append("				     , WON_SELECT_TOT_AMT = SELECT_TOT_AMT * '"+params.get("MOD_EXCHANGE")+"'	\n");
        sqlstr_u.append("				 WHERE BID_NO     = '"+params.get("BID_NO")+"'									\n");
        sqlstr_u.append("				   AND BID_DEG    = '"+params.get("BID_DEG")+"'									\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
	
	public static void saveErpSignClass(ValueObject params) throws Exception{
		
		WAFConnectionImpl erpConn = new WAFConnectionImpl();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String toDay = sdf.format(date);
		
		try{
			erpConn.setDataSource("jdbc/erpiu");
			erpConn.begin();
			Persistent pstmt = new PersistentImpl(erpConn.getConnection());
        	StrBuffer sqlstr = new StrBuffer();
        	sqlstr.append("				UPDATE SIGN001T            				\n");
        	sqlstr.append("				   SET SIGN_DECI_CLASS  = '"+params.get("MOD_SIGN_DECI_CLASS")+"'							\n");
        	sqlstr.append("				 WHERE DOC_ID     = '"+params.get("DOC_ID")+"'								\n");
        	
            pstmt.setStatement(sqlstr.toString());           
            WAFLogger.debug(sqlstr.toString());
            pstmt.execute();
            pstmt.clearParameters();
		}catch (WAFSQLException e){
			WAFLogger.error("ModStatusErpModU --> ModStatusErpModU() : " + e.getMessage());
			WAFLogger.error(e);
			erpConn.setRollback();
			throw e;
        }finally{
        	erpConn.end();
        }
	}
	
	private void templateSaveData() throws Exception{

		WAFLogger.debug("=================================================");
        boolean success = false;
        
        conn.begin();
        try 
        {
        	Connection con = conn.getConnection();
        	
        	if(storage.compareEvent("FORM_UPDATE"))	success = updateInfo(con, params);
        	else if(storage.compareEvent("FORM_CONT_UPDATE")) success = contUpdateInfo(con, params);
        	
            super.storage.setSuccess(success);
            
        } catch (Exception e) {
			super.doException(e);
			super.storage.setDetailMessage(e);
			super.storage.setMessage(e.getMessage());
			super.storage.setSuccess(false);
			super.conn.setRollback();
		} finally {
			conn.end();
		} 
	}
	
	/**
     * 회사정보 저장
     * @param params
     * @return
     * @throws Exception
     */
    private boolean updateInfo(Connection con, ValueObject params) throws Exception
    {
        
        StringBuffer sqlstr = new StringBuffer();
        Persistent persistent = new PersistentImpl(con);
        
        
        try{
        	
        	sqlstr.append("UPDATE CONT_TEMPLATE     	        \n");
        	sqlstr.append("   SET UPD_DATE = SYSDATE				\n");
        	sqlstr.append("     , UPD_BY = '"+storage.getUser().get("acct_id")+"'					\n");
        	sqlstr.append("     , TEMPLATE_NAME = '"+params.get("p_title")+"'					\n");
        	sqlstr.append("  WHERE TEMPLATE_CD = '"+params.get("P_TEMPLATE_CD")+"'					\n");
        	sqlstr.append("    AND SEQ = '"+params.get("P_SEQ")+"'					\n");
        	sqlstr.append("    AND JOB_DIV = '"+params.get("P_JOB_DIV")+"'					\n");

        	
        	 persistent.setStatement(sqlstr.toString());
        	 WAFLogger.debug(sqlstr.toString());
        	
             persistent.execute();
        	ClobUtils.setClob(con, "CONT_TEMPLATE", "TEMPLATE_HTML", " WHERE TEMPLATE_CD = '"+params.get("P_TEMPLATE_CD")+"' AND SEQ = '"+params.get("P_SEQ")+"' AND JOB_DIV = '"+params.get("P_JOB_DIV")+"' ", params.get("template").replace("</mod_textarea>", "</textarea>"));
            
            return true;
            
            
        } catch(Exception e) {
            throw e;
            
        }
    }
    
    /**
     * 회사정보 저장
     * @param params
     * @return
     * @throws Exception
     */
    private boolean contUpdateInfo(Connection con, ValueObject params) throws Exception
    {
        
        StringBuffer sqlstr = new StringBuffer();
        Persistent persistent = new PersistentImpl(con);
        
        
        try{
        	if(params.get("P_GUBUN").equals("P")){
        		sqlstr.append("UPDATE CONT_PUR_HTML     	        \n");
            	sqlstr.append("   SET CHECK_YN = '"+params.get("P_CHECK_YN")+"'				\n");
            	sqlstr.append("     , OPTION_YN = '"+params.get("P_OPTION_YN")+"'					\n");
            	sqlstr.append("  WHERE PUR_CONT_MOD_ID = '"+params.get("P_CONT_MOD_ID")+"'					\n");
            	sqlstr.append("    AND SEQ = '"+params.get("P_SEQ")+"'					\n");
        	}else{
        		sqlstr.append("UPDATE CONT_OUT_HTML     	        \n");
            	sqlstr.append("   SET CHECK_YN = '"+params.get("P_CHECK_YN")+"'				\n");
            	sqlstr.append("     , OPTION_YN = '"+params.get("P_OPTION_YN")+"'					\n");
            	sqlstr.append("  WHERE SUB_CONT_MOD_ID = '"+params.get("P_CONT_MOD_ID")+"'					\n");
            	sqlstr.append("    AND SEQ = '"+params.get("P_SEQ")+"'					\n");
        	}
        	 persistent.setStatement(sqlstr.toString());
        	 WAFLogger.debug(sqlstr.toString());
        	
             persistent.execute();
             if(params.get("P_GUBUN").equals("P")){
            	 ClobUtils.setClob(con, "CONT_PUR_HTML", "TEMPLATE_HTML", " WHERE PUR_CONT_MOD_ID = '"+params.get("P_CONT_MOD_ID")+"' AND SEQ = '"+params.get("P_SEQ")+"' ", params.get("template").replace("</mod_textarea>", "</textarea>")); 
             }else{
            	 ClobUtils.setClob(con, "CONT_OUT_HTML", "TEMPLATE_HTML", " WHERE SUB_CONT_MOD_ID = '"+params.get("P_CONT_MOD_ID")+"' AND SEQ = '"+params.get("P_SEQ")+"' ", params.get("template").replace("</mod_textarea>", "</textarea>"));
             }
            
            return true;
            
            
        } catch(Exception e) {
            throw e;
            
        }
    }
    
    private boolean savePurContStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE PUR2000T            				\n");
        sqlstr_u.append("				   SET STATUS_CD  = '"+params.get("CONT_STATUS_CD")+"'							\n");
        sqlstr_u.append("				 WHERE PUR_CONT_MOD_ID     = '"+params.get("PUR_CONT_MOD_ID")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
    
    private boolean savePurWarrStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE CONT_PUR_WARR            				\n");
        sqlstr_u.append("				   SET GUAR_STATUS  = '"+params.get("GUAR_STATUS_CD")+"'							\n");
        sqlstr_u.append("				     , GUAR_ONOFF_YN  = '"+params.get("MOD_GUAR_ONOFF_YN")+"'							\n");
        sqlstr_u.append("				 WHERE PUR_CONT_MOD_ID     = '"+params.get("PUR_CONT_MOD_ID")+"'								\n");
        sqlstr_u.append("				   AND WARR_SEQ     = '"+params.get("WARR_SEQ")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
    
    private boolean saveSubContStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE SUB2000T            				\n");
        sqlstr_u.append("				   SET STATUS_CD  = '"+params.get("CONT_STATUS_CD")+"'							\n");
        sqlstr_u.append("				 WHERE SUB_CONT_MOD_ID     = '"+params.get("SUB_CONT_MOD_ID")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
    
    private boolean saveSubWarrStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE CONT_OUT_WARR            				\n");
        sqlstr_u.append("				   SET GUAR_STATUS  = '"+params.get("GUAR_STATUS_CD")+"'							\n");
        sqlstr_u.append("				     , GUAR_ONOFF_YN  = '"+params.get("MOD_GUAR_ONOFF_YN")+"'							\n");
        sqlstr_u.append("				 WHERE SUB_CONT_MOD_ID     = '"+params.get("SUB_CONT_MOD_ID")+"'								\n");
        sqlstr_u.append("				   AND WARR_SEQ     = '"+params.get("WARR_SEQ")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
    
    private boolean delOutCompAppr(ValueObject params) {
    	Persistent persistent_u = null;
    	Persistent persistent_u2 = null;
        StringBuffer sqlstr_u   = new StringBuffer();
        StringBuffer sqlstr_u2   = new StringBuffer();
        
		sqlstr_u.append("	DELETE SUPP_APPR_ITEM	\n");
		sqlstr_u.append("WHERE CORP_ID = '"+params.get("CORP_ID")+"'     	\n");
		sqlstr_u.append("  AND DOC_CD = '"+params.get("DOC_CD")+"'     	\n");
		sqlstr_u.append("  AND ARR_ID = '"+params.get("ARR_ID")+"'     	\n");
		
		sqlstr_u2.append("	DELETE SUPP_APPR	\n");
		sqlstr_u2.append("WHERE DEPT_CD = '"+params.get("DEPT_CD")+"'     	\n");
		sqlstr_u2.append("  AND CORP_ID = '"+params.get("CORP_ID")+"'     	\n");
		sqlstr_u2.append("  AND DOC_CD = '"+params.get("DOC_CD")+"'     	\n");
		sqlstr_u2.append("  AND ARR_ID = '"+params.get("ARR_ID")+"'     	\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            persistent_u.execute();
            persistent_u.clearParameters(); 
            
            persistent_u2 = new PersistentImpl(super.conn.getConnection());
            persistent_u2.setStatement(sqlstr_u2.toString());
            
            WAFLogger.debug(sqlstr_u2.toString());
            persistent_u2.execute();
            persistent_u2.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();
        }
	}
    
    private RowSet getIFSignInfo(ValueObject params) throws Exception{
    	Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StringBuffer sqlstr = new StringBuffer();       
        
        
        try {
        	sqlstr.append(" SELECT  SIGN_INTERFACE_ID											\n");
        	sqlstr.append("         ,SIGN_DECI_CLASS											\n");
        	sqlstr.append("         ,APPROVAL_DATE												\n");
        	sqlstr.append("         ,APPROVAL_SABUN												\n");
        	sqlstr.append("         ,APPROVAL_USER												\n");
        	sqlstr.append("         ,SIGN_TYPE													\n");
        	sqlstr.append("         ,KEY_COL1													\n");
        	sqlstr.append("         ,KEY_COL2													\n");
        	sqlstr.append("         ,KEY_COL3													\n");
        	sqlstr.append("         ,KEY_COL4													\n");
        	sqlstr.append("         ,KEY_COL5													\n");
        	sqlstr.append("         ,ATTR_1														\n");
        	sqlstr.append("         ,ATTR_2														\n");
        	sqlstr.append("         ,ATTR_3														\n");
        	sqlstr.append("         ,ATTR_4														\n");
        	sqlstr.append("         ,ATTR_5														\n");
        	sqlstr.append("         ,DOC_ID														\n");
        	sqlstr.append("         ,SIGN_CLASS_CD												\n");
        	sqlstr.append("         ,B.ACCT_ID													\n");
        	sqlstr.append("         ,B.PSOSO AS DEPT_CD											\n");
        	sqlstr.append("         ,GET_DEPT_NM(B.PSOSO) AS DEPT_NM							\n");
        	sqlstr.append("         ,B.DEPART_DEPT_CD AS ACC_DEPT_CD							\n");
        	sqlstr.append("         ,B.DEPART_DEPT_NM AS ACC_DEPT_NM							\n");
        	sqlstr.append(" FROM    SIGN001T A													\n");
        	sqlstr.append("         ,PUB0120T B													\n");
        	sqlstr.append(" WHERE   A.SIGN_INTERFACE_ID = '"+params.get("SIGN_INTERFACE_ID")+"'	\n");
        	sqlstr.append(" AND     A.APPROVAL_SABUN = B.USER_SABUN(+)							\n");

            search.setStatement(sqlstr.toString());
            rowSet = search.execute();            
        }
        catch(WAFSQLException se) {
            Logger.err.println("SQL : \n" + se.getStatement());
            Logger.err.println("PARAM : \n" + se.getParameter());
            throw se;
       }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
            return rowSet;
        }
    }
    
    private void setSign(RowSet rowSignInterface){
    	String driverName = "";
    	String str_doc_state = "";
    	Row row = new RowImpl();
    	
    	System.out.println("rowSignInterface.size() = "+rowSignInterface.size());
    	
        for ( int i = 0 ; i < rowSignInterface.size() ; i ++ ) {
       	 
        	row = rowSignInterface.getRow(i);
        	
            try {
                conn.begin();
                if( row.get("SIGN_CLASS_CD").equals("") ) {
                    Logger.err.println("SIGN_CLASS_CD 정보가 없습니다..\\n");
                    continue;
                }
                
                // 결재진행상태 값을 가져온다.
                str_doc_state = params.get("SIGN_DECI_CLASS");
                
                System.out.println("str_doc_state = "+str_doc_state);
                
                // 상태별 Call 처리
                try {
                    ValueObject signDoc = new ValueObject();
                    
                    signDoc.set("SIGN_INTERFACE_ID",	row.get("SIGN_INTERFACE_ID"));
                    signDoc.set("SIGN_DECI_CLASS",		row.get("SIGN_DECI_CLASS"));
                    signDoc.set("APPROVAL_DATE",		row.get("APPROVAL_DATE"));
                    signDoc.set("APPROVAL_SABUN",		row.get("APPROVAL_SABUN"));
                    signDoc.set("APPROVAL_USER",		row.get("APPROVAL_USER"));
                    signDoc.set("SIGN_TYPE",			row.get("SIGN_TYPE"));
                    signDoc.set("KEY_COL1",				row.get("KEY_COL1"));
                    signDoc.set("KEY_COL2",				row.get("KEY_COL2"));
                    signDoc.set("KEY_COL3",				row.get("KEY_COL3"));
                    signDoc.set("KEY_COL4",				row.get("KEY_COL4"));
                    signDoc.set("KEY_COL5",				row.get("KEY_COL5"));
                    signDoc.set("ATTR_1",				row.get("ATTR_1"));
                    signDoc.set("ATTR_2",				row.get("ATTR_2"));
                    signDoc.set("ATTR_3",				row.get("ATTR_3"));
                    signDoc.set("ATTR_4",				row.get("ATTR_4"));
                    signDoc.set("ATTR_5",				row.get("ATTR_5"));
                    signDoc.set("ACCT_TYPE",			row.get("ATTR_5"));
                    signDoc.set("DOC_ID",				row.get("DOC_ID"));
                    signDoc.set("SIGN_CLASS_CD",		row.get("SIGN_CLASS_CD"));
                    signDoc.set("ACCT_ID",				row.get("ACCT_ID"));
                    signDoc.set("DEPT_CD",				row.get("DEPT_CD"));
                    signDoc.set("DEPT_NM",				row.get("DEPT_NM"));
                    signDoc.set("ACC_DEPT_CD",			row.get("ACC_DEPT_CD"));
                    signDoc.set("ACC_DEPT_NM",			row.get("ACC_DEPT_NM"));
//                    signDoc.set("REQ_SABUN",			row.get("REQ_SABUN"));		//2021. 12. 결재요청자 정보 추가
//                    signDoc.set("REQ_NAME",			row.get("REQ_NAME"));		//2021. 12. 결재요청자 정보 추가
                    driverName = getSignDriverName(row.get("SIGN_CLASS_CD"));
                    
                    System.out.println("driverName = "+driverName);
                    
                    
                    if(!"".equals(driverName)){
                    	CommonSignInterface driver = CommonSignManager.createSignDriver(driverName);
                    	
                    	if ( str_doc_state.equals("12") || str_doc_state.equals("13") ){ // 1차 반려
                    		driver.doFirstRejectSign(conn.getConnection(), signDoc);
                    		
                    	} else if ( str_doc_state.equals("14") ){ // 1차 결제 완료
                    		driver.doFirstFinishSign(conn.getConnection(), signDoc);
                    		
                    	} else if ( str_doc_state.equals("22") || str_doc_state.equals("23") ){ // 2차 결제 반려
                    		//driver.doRejectSign(conn.getConnection(), signDoc);  2017.04.27 수신부서에서 반려,회수하는 경우는 GW내 결재진행이므로 주석처리. 
                    		
                    	} else if ( str_doc_state.equals("25")){ // 2차 결제 반려
                    		driver.doRejectSign(conn.getConnection(), signDoc);
                    		
                    	} else if ( str_doc_state.equals("24") ){  // 완료
                    		driver.doFinishSign(conn.getConnection(), signDoc);
                    	}
                    }
                    
                }
                catch(Exception e) {
                    Logger.err.println(driverName + ".doXxxxSign() 실행중 오류가 발생 했습니다.\\n" + e.getMessage());
                    conn.setRollback();
                    throw e;
                }
            }
            catch(Exception e) {
                Logger.err.println("doBiz() 실행중 오류가 발생 했습니다.\\n" + e.getMessage());
                conn.setRollback();
            }
            finally {
                conn.end();
            }
        }
    }    
    
    public String getSignDriverName(String signClassCd) throws Exception{
    	Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlStr = new StringBuffer();
        String rtnStr = "";
        try {
        	sqlStr.append(" SELECT	DETAILSORTNM AS DRIVER_NAME		\n");
            sqlStr.append(" FROM	EBID_COM911T					\n");
            sqlStr.append(" WHERE	BASECD = 'DRAFT'				\n");
            sqlStr.append(" AND		DETAILNM = ?					\n");
            search.setStatement(sqlStr.toString());
            search.addParameter(signClassCd);
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ) {
            	
            	rtnStr = rowSet.getRow(0).get("DRIVER_NAME");
            	
            }
            
        }catch(WAFSQLException se) {
        	Logger.err.println("SQL : \n" + se.getStatement());
            Logger.err.println("PARAM : \n" + se.getParameter());
            throw se;
        }finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
            return rtnStr;
        }
    }
    
    public boolean signTest(ValueObject params) {
        Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
        
		sqlstr_u.append("	update sign001t														\n");
		sqlstr_u.append("	   set attr_6 = 1													\n");
		sqlstr_u.append("	 where sign_interface_id = '"+params.get("SIGN_INTERFACE_ID")+"'	\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            //WAFLogger.debug(params);
            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();
        }
    }
    
    private boolean saveSubPayStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE SUB1040T            				\n");
        sqlstr_u.append("				   SET STATUS_CD  = '"+params.get("STATUS_CD2")+"'							\n");
        sqlstr_u.append("				 WHERE ACMP_ID     = '"+params.get("ACMP_ID")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
    
    private boolean savePurPayStatus(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE PUR1100T            				\n");
        sqlstr_u.append("				   SET REQ_STATUS  = '"+params.get("REQ_STATUS2")+"'							\n");
        sqlstr_u.append("				 WHERE PAYREQ_ID     = '"+params.get("PAYREQ_ID")+"'								\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}    
    
    private boolean savePurReviseRemainder(ValueObject params) {
		Persistent persistent_u = null;
        StringBuffer sqlstr_u   = new StringBuffer();
		
        sqlstr_u.append("				UPDATE PUR1100T            								\n");
        sqlstr_u.append("				   SET REMAINDER_ACMP_VAT  = 0							\n");
        sqlstr_u.append("				     , REMAINDER_ACMP_AMT  = 0							\n");
        sqlstr_u.append("				 WHERE PAYREQ_ID     = '"+params.get("PAYREQ_ID")+"'	\n");
		
		try {
            conn.begin();
            persistent_u = new PersistentImpl(super.conn.getConnection());
            persistent_u.setStatement(sqlstr_u.toString());
            
            WAFLogger.debug(sqlstr_u.toString());
            WAFLogger.debug(params);
            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();
        }
	}
}
