/**
 * @(#)file    SalNewPopBiz.java
 * @(#)author  김예진
 * @(#)version 1.0
 * @(#)date    2015-01-12
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.stamptax.biz;

import java.sql.Connection;

import oracle.toplink.internal.ox.StrBuffer;
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 com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class SalNewPopBiz extends Biz {

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

	public void doBiz() throws Exception {
		
		if(storage.compareEvent("")){
			
			storage.addRowSet("CODE911", getCode911List());
			
			if(params.get("p_move").equals("")){
				ValueObject p_stamp_duty_id = getStampDutyId(super.conn.getConnection()); 
				params.set("p_stamp_duty_id", p_stamp_duty_id.get("STAMP_DUTY_ID"));
			}
            
		}else if(storage.compareEvent("SELECT_DATA")){
			if(params.get("p_move").equals("d")){
				ValueObject salstamp = getSalStamp(params.get("p_stamp_duty_id")); 
				storage.addValueObject("VALUEOBJECT", salstamp);
			}
		}
	}
	
	
	public ValueObject getStampDutyId(Connection conn) throws Exception{
        
        Persistent persistent_q = null;
        
        
        ValueObject vo = new ValueObject();
        ValueObject row = null;
        StringBuffer sqlstr =  new StringBuffer();
        
        try{
            persistent_q = new PersistentImpl(conn);
            
            sqlstr.append("         SELECT STAMP_DUTY_S.NEXTVAL AS STAMP_DUTY_ID  FROM DUAL                                                 \n");
            
            persistent_q.setStatement(sqlstr.toString()); 
            row = RowSetUtility.getValueObject(persistent_q.query().getRow(0));
            vo.set("STAMP_DUTY_ID", row.get("STAMP_DUTY_ID"));
        }
        catch(Exception e) {
            e.printStackTrace();
        }finally{
            if(vo == null) vo = new ValueObject();
        }
        return vo;
    }
	
	/**
	 * 영업_인지세 상세를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private ValueObject getSalStamp(String Stamp_duty_id) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
     
        StrBuffer sqlstr = new StrBuffer();
        
        try{
			sqlstr.append("   SELECT                                                                                               \n");
			sqlstr.append("           STAMP_DUTY_ID                                                                                \n");
			sqlstr.append("          ,STAMP_DUTY_TYPE                                                                              \n");
			sqlstr.append("          ,STAMP_DUTY_ISSUE_GB                                                                          \n");
			sqlstr.append("          ,CONT_MOD_ID                                                                                  \n");
			sqlstr.append("          ,CORP_REG_NO                                                                                  \n");
			sqlstr.append("          ,SUBSTR(CORP_REG_NO,0,3) AS CORP_REG_NO1                                         			   \n");
			sqlstr.append("          ,SUBSTR(CORP_REG_NO,4,2) AS CORP_REG_NO2                                                      \n");
			sqlstr.append("          ,SUBSTR(CORP_REG_NO,6) AS CORP_REG_NO3                                                        \n");
			sqlstr.append("          ,CORP_NAME                                                                                    \n");
			sqlstr.append("          ,CONT_NO                                                                                      \n");
			sqlstr.append("          ,MOD_NO                                                                                       \n");
			sqlstr.append("          ,STAMP_DUTY_NO                                                                                \n");
			sqlstr.append("          ,CONT_NAME                                                                                    \n");
			sqlstr.append("          ,F_GET_D_CURRENCY(CONT_AMT, F_GET_CURRENCY_DIGIT(CURRENCY_GB)) AS CONT_AMT                    \n");
			sqlstr.append("          ,CURRENCY_GB                                                                                  \n");
			sqlstr.append("          ,F_GET_D_CURRENCY(STAMP_DUTY_TAX,0) AS STAMP_DUTY_TAX                                         \n");
			sqlstr.append("          ,TO_CHAR(TO_DATE(STAMP_DUTY_DATE), 'YYYY-MM-DD') AS STAMP_DUTY_DATE                           \n");
			sqlstr.append("          ,REMARK                                                                                       \n");
			sqlstr.append("   FROM STAMP_DUTY                                                                                      \n");
			sqlstr.append("   WHERE STAMP_DUTY_ID = '"+Stamp_duty_id+"'                                                            \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;
        }
	}
	
	private RowSet getCode911List() {
		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StrBuffer sqlstr = new StrBuffer();
        try{
			sqlstr.append("  SELECT DETAILCD,DETAILNM,DETAILSORTNM,CHG_CODE01,CHG_CODE03 FROM EBID_COM911T WHERE BASECD = '911'	 ORDER BY SORT_NO 	\n");
			search.setStatement(sqlstr.toString());
            rowSet = search.execute();
        }catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }finally{    
            if(rowSet == null)rowSet = new RowSetImpl();
        }
        return rowSet;
	}

}