package kr.co.udapsoft.ebid.buyer.cont.stamptax.biz;

import java.sql.Connection;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;


/**
*
* <p>설명 : 영업_인지세 등록,수정,삭제 PROCESS</p>
* @author 김예진
* @since 	2014.02.14.
* @see 		SalNewPopSaveBiz
*/
public class SalNewPopSaveBiz extends Biz {

	public SalNewPopSaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		
		ValueObject vo = new ValueObject();

		boolean success = false;
		
		success = saveProc(params);
        storage.setSuccess(success);
        

        
        storage.addValueObject("VALUEOBJECT", vo);
        
	}
	
	
	/**
	 * 영업_인지세 테이블에 등록,수정,삭제 한다.
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean saveProc(ValueObject params) {

        try {
            conn.begin();
            
            if(storage.compareEvent("INSERT_DATA")) {
            	
            	insertSalStampForm(conn.getConnection());
            	
            } else if(storage.compareEvent("UPDATE_DATA")) {
            		
            	updateSalStampForm(conn.getConnection());
            	
            } else if(storage.compareEvent("DELETE_DATA")) {
            	
            	FileManager fileManager = new FileManager();
            	
            	fileManager.delFileMapProc(conn.getConnection(), "SAL_STAMP_DUTY", params.get("p_stamp_duty_id"));
				
            	deleteSalStampForm(conn.getConnection());
            	
            } 

            return true;
        }
        catch(Exception e) {
    
            WAFLogger.error(this.getClass().getName() + " --> saveProc() : " + e.getMessage());
            WAFLogger.error(e);
            WAFLogger.debug(e.getMessage());
            conn.setRollback();
            storage.setDetailMessage(e);
            return false;
        }
        finally {
            conn.end();
        }
    }
		
	
	/**
     * 영업_인지세 INSERT 처리
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public ValueObject insertSalStampForm(Connection connection) throws Exception {
		 Persistent persistent 	= new PersistentImpl(connection);
	     StringBuffer sql 		= new StringBuffer();
	     ValueObject retVo 		= new ValueObject();
	       
        try {
        	
        	String p_corp_reg_no = params.get("p_corp_reg_no1")+params.get("p_corp_reg_no2")+params.get("p_corp_reg_no3");

			sql.append("   INSERT INTO STAMP_DUTY (                                  \n");
			sql.append("               STAMP_DUTY_ID                                 \n");
			sql.append("              ,STAMP_DUTY_TYPE                               \n");
			sql.append("              ,STAMP_DUTY_ISSUE_GB                           \n");
		  //sql.append("              ,CONT_MOD_ID                                   \n");
			sql.append("              ,CORP_REG_NO                                   \n");
			sql.append("              ,CORP_NAME                                     \n");
			sql.append("              ,CONT_NO                                       \n");
			sql.append("              ,MOD_NO                                        \n");
			sql.append("              ,STAMP_DUTY_NO                                 \n");
			sql.append("              ,CONT_NAME                                     \n");
			sql.append("              ,CONT_AMT                                      \n");
			sql.append("              ,CURRENCY_GB                                   \n");
			sql.append("              ,STAMP_DUTY_TAX                                \n");
			sql.append("              ,STAMP_DUTY_DATE                               \n");
			sql.append("              ,REMARK                                        \n");
			sql.append("   ) VALUES (                                                \n");
			sql.append("               ?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
		  //sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("              ,?                                             \n");
			sql.append("   )                                                         \n");
        	
            persistent.setStatement(sql.toString());
            
            persistent.addParameter(params.get("p_stamp_duty_id"));
            persistent.addParameter("50");
            persistent.addParameter(params.get("p_stamp_duty_issue"));
            persistent.addParameter(p_corp_reg_no);
            persistent.addParameter(params.get("p_corp_name"));
            persistent.addParameter(params.get("p_cont_no"));
            persistent.addParameter(params.get("p_mod_no"));
            persistent.addParameter(params.get("p_stamp_duty_no"));
            persistent.addParameter(params.get("p_cont_name"));
            persistent.addParameter(params.get("p_cont_amt").replaceAll(",",""));
            persistent.addParameter(params.get("p_currency_gb"));
            persistent.addParameter(params.get("p_stamp_duty_tax").replaceAll(",",""));
            persistent.addParameter(params.get("p_stamp_duty_date").replaceAll("-",""));
            persistent.addParameter(params.get("p_remark"));
            
            persistent.execute();
            persistent.clearParameters();
           
        } catch(Exception e) {
        	super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            throw e;
        }
        
        return retVo;
    }
	
	/**
	 * 영업_인지세 UPDATE 처리
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void updateSalStampForm(Connection connection) throws Exception {
		Persistent persistent 	= new PersistentImpl(connection);
		StringBuffer sql 		= new StringBuffer();
		
		try {
			String p_corp_reg_no = params.get("p_corp_reg_no1")+params.get("p_corp_reg_no2")+params.get("p_corp_reg_no3");
			
			sql.append("	UPDATE STAMP_DUTY SET                                    	\n");
			sql.append("               STAMP_DUTY_ISSUE_GB = ?                          \n");
		  //sql.append("              ,CONT_MOD_ID         = ?                          \n");
			sql.append("              ,CORP_REG_NO         = ?                          \n");
			sql.append("              ,CORP_NAME           = ?                          \n");
			sql.append("              ,CONT_NO             = ?                          \n");
			sql.append("              ,MOD_NO              = ?                          \n");
			sql.append("              ,STAMP_DUTY_NO       = ?                          \n");
			sql.append("              ,CONT_NAME           = ?                          \n");
			sql.append("              ,CONT_AMT            = ?                          \n");
			sql.append("              ,CURRENCY_GB         = ?                          \n");
			sql.append("              ,STAMP_DUTY_TAX      = ?                          \n");
			sql.append("              ,STAMP_DUTY_DATE     = ?                          \n");
			sql.append("              ,REMARK              = ?                          \n");
			sql.append("	WHERE STAMP_DUTY_ID 	       = ?                          \n");
			
			persistent.setStatement(sql.toString());

            persistent.addParameter(params.get("p_stamp_duty_issue"));
            persistent.addParameter(p_corp_reg_no);
            persistent.addParameter(params.get("p_corp_name"));
            persistent.addParameter(params.get("p_cont_no"));
            persistent.addParameter(params.get("p_mod_no"));
            persistent.addParameter(params.get("p_stamp_duty_no"));
            persistent.addParameter(params.get("p_cont_name"));
            persistent.addParameter(params.get("p_cont_amt").replaceAll(",",""));
            persistent.addParameter(params.get("p_currency_gb"));
            persistent.addParameter(params.get("p_stamp_duty_tax").replaceAll(",",""));
            persistent.addParameter(params.get("p_stamp_duty_date").replaceAll("-",""));
            persistent.addParameter(params.get("p_remark"));
            persistent.addParameter(params.get("p_stamp_duty_id"));
            
			persistent.execute();
			persistent.clearParameters();
			
		} catch(Exception e) {
        	super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            WAFLogger.debug(e.getMessage());
            throw e;
		}
		
	}
	
	/**
	 * 영업_인지세 DELETE 처리
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void deleteSalStampForm(Connection connection) throws Exception {
		Persistent persistent 	= new PersistentImpl(connection);
		StringBuffer sql 		= new StringBuffer();
		
		try {
			
			sql.append(" DELETE FROM STAMP_DUTY			   							\n");
			sql.append(" WHERE STAMP_DUTY_ID = ?                                   	\n");
			
			persistent.setStatement(sql.toString());
			
			persistent.addParameter(params.get("p_stamp_duty_id"));
            			
			persistent.execute();
			persistent.clearParameters();
			
		} catch(Exception e) {
			super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
			throw e;
		}
		
	}
	
	
}
