/**
 * @(#)file    BidPlanSignDriver.java
 * @(#)author  Son hyungmin
 * @(#)version 1.0
 * @(#)date    2014-09-29
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.dcchem.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of dcchem, Inc.
 */
package kr.co.udapsoft.common.commonSign.driver;

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.common.commonSign.CommonSignInterface;
import oracle.toplink.internal.ox.StrBuffer;

/**
 * 
 * @version 1.0
 * @author Beomgeun Lee
 */
public class PcontSignDriver implements CommonSignInterface {

    /**
     * 테슽 파일
     * 
     */
    public PcontSignDriver() {
        super();
    }

    /* 
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doCallSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doCallSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!doCallSign");
    }

    /* 
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doStartSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doStartSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!doStartSign");
    }

    /*
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doFirstFinishSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doFirstFinishSign(Connection connection, ValueObject signDoc) throws Exception {
		System.out.println("!!doFirstFinishSign");
    	System.out.println("### 결재  Strart");
    	updateMaster(connection, signDoc);
    	addBcsCont(connection, signDoc);//2017.2.7 변경시행결의후 예산등록 
    	System.out.println("### 결재  End");
    }
    
    /* 
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doFinishSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doFinishSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!doFinishSign");
    }

    /* 
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doRejectSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doFirstRejectSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!반려");
    	updateMaster(connection, signDoc);
    }
    /* 
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doRejectSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doRejectSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!doRejectSign");
    	
    }

    /*
     * (non-Javadoc)
     * @see kr.co.udap.ehr.common.sign.SignInterface#doCancelSign(java.sql.Connection, com.udapsoft.common.util.ValueObject)
     */
    public void doCancelSign(Connection connection, ValueObject signDoc) throws Exception {
    	System.out.println("!!doCancelSign");
    }
    
    
    /**
     * SAC01001 상태값을 업데이트해준다.
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void updateMaster(Connection connection, ValueObject vo) throws Exception {

        Persistent persistent = new PersistentImpl(connection);
        StringBuffer sqlstr = null;
        
        try {
        	sqlstr = new StringBuffer();
        	String status = "";
        	if(vo.get("SIGN_TYPE").equals("1")){
        		if(vo.get("SIGN_DECI_CLASS").equals("14")){
        			status = "02";//결재완료18->02 2016.03.16
        		}else {
        			status = "03";//반려  41->03
        		}
        	}
   			sqlstr.append(" UPDATE  PUR2000T  					\n");
    		sqlstr.append("      SET STATUS_CD = ?    						\n");
    		sqlstr.append(" WHERE PUR_CONT_MOD_ID	= ?    							\n");
    		persistent.setStatement(sqlstr.toString());
            persistent.addParameter(status);
            persistent.addParameter(vo.get("KEY_COL1"));
            persistent.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> updateSac01001() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	//2017.2.7 변경시행결의후 예산등록
	public boolean addBcsCont(Connection connection, ValueObject vo) throws Exception {
		//WAFLogger.debug("valueObject : " + valueObject);
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
			sqlstr.append("INSERT INTO BCS_MR 			                                                               \n");
			sqlstr.append("       (  DEPT_CD					                                                               \n");
			sqlstr.append("         ,MR_ID					                                                                   \n");
			sqlstr.append("         ,KIND_GB				                                                                  \n");
            sqlstr.append("         ,MR_NO					                                                                   \n");
            sqlstr.append("         ,TITLE					                                                                   \n");
            sqlstr.append("         ,CONAMT					                                                                   \n");
            sqlstr.append("         ,CONDATE					                                                               \n");
            sqlstr.append("         ,CONT_ID					                                                               \n");
            sqlstr.append("         ,CONT_MOD_ID				                                                               \n");
            sqlstr.append("         ,VENDOR_NM					                                                               \n");
            sqlstr.append("         ,CRE_DATE					                                                               \n");
            sqlstr.append("         ,CRE_BY					                                                                   \n");
            sqlstr.append("         ,CURRENCY_GB	)			                                                               \n");
            sqlstr.append("   SELECT A.DEPT_CD	                                                                               \n");//DEPT_CD		
			sqlstr.append("         ,	(SELECT DECODE(COUNT(*), 0, 1, MAX(MR_ID)+1) FROM BCS_MR WHERE DEPT_CD = A.DEPT_CD  )  \n");//MR_ID			
			sqlstr.append("         ,	'P'		                                                                               \n");//DEMANDED_KIND	
			sqlstr.append("         ,	 SUBSTR(A.REQ_DOC_NO, 11, LENGTH(A.REQ_DOC_NO)-10)		                               \n");//MR_NO			
			sqlstr.append("         ,	 A.CST_DOC_NAME		                                                                   \n");//TITLE			
			sqlstr.append("         ,	A.ORD_SUPPLY_AMT       		                                                           \n");//CONAMT			
			sqlstr.append("         ,	SYSDATE		                                                                           \n");//CONDATE		
			sqlstr.append("         ,	A.PUR_CONT_ID		                                                                   \n");//CONT_ID		
			sqlstr.append("         ,	A.PUR_CONT_MOD_ID                                                                      \n");//CONT_MOD_ID	
			sqlstr.append("         ,   B.MEMBER_NAME   	                                                                   \n");//VENDOR_NM		
			sqlstr.append("         ,	SYSDATE		                                                                           \n");//CRE_DATE		
			sqlstr.append("         ,	999	               	                                                                   \n");//CRE_BY			
			sqlstr.append("         ,	A.CURRENCY_GB		                                                                   \n");//CURRENCY_GB	
			sqlstr.append("   FROM PUR2000T A	                                  		                               \n");
			sqlstr.append("      , CONT_PUR_SUPP B   			                       	                               \n");
			sqlstr.append("  WHERE A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID						                               \n");
			sqlstr.append("    AND B.SIGN_SEQ = 2				                                                               \n");
			sqlstr.append("    AND A.PUR_CONT_MOD_ID= ?   				                                                       \n");
					
			persistent.setStatement(sqlstr.toString()); 
			persistent.addParameter(vo.get("KEY_COL1")); 
			persistent.execute();
			
            return true;
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            //WAFLogger.debug("Exception : " + e.getMessage());
            throw e;
        }        
    }
}