/**
 * @(#)file    ContIntBiz.java
 * @(#)author  Kim Myung Ho
 * @(#)version 1.0
 * @(#)date    2014-07-15
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.supplier.portal.cont.biz;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import oracle.toplink.internal.ox.StrBuffer;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.base.WAFMessages;
import kr.co.hsnc.common.sql.Row;
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.common.code.CommCodeManager;
import kr.co.udapsoft.common.sender.MessageSend;
import kr.co.udapsoft.common.util.PDFMaker;
import kr.co.udapsoft.common.util.Crosscert;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;
import kr.co.udapsoft.ebid.buyer.sycs.user.biz.UserCommon;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.common.ui.InitCombo;
import com.udapsoft.waf.common.util.StringUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;


public class ContSearchBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject SearchInfo = new ValueObject();
//        //System.out.println("BidIngListBiz :  ~" + storage.getUser());
//		//System.out.println("Outside event : #" + storage.getEvent());
		//System.out.println("Outside params : " + params);
		boolean success = false;
		
		if(storage.compareEvent("")){
			
		}else if(storage.compareEvent("FORM_SELECT")){
			if(params.get("CONT_TRUE_CD").equals("00")){
				SearchInfo = getSearchCont(params);
	            storage.addValueObject("SearchInfo", SearchInfo);
			}else if(params.get("CONT_TRUE_CD").equals("10")){
				SearchInfo = getSearchResult(params);
	            storage.addValueObject("SearchInfo", SearchInfo);
			}
		}
	}
	
	
	
	 /**
     * 저장된 계약서 조회 
	 * @param 
	 * @param 
	 * @return RowSet 
	 */
	public ValueObject getSearchCont(ValueObject param){
		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append(" 		SELECT 	\n");
        	sqlstr.append(" 		    CONT_NAME	\n");
        	sqlstr.append(" 		    , CONT_DEPT	\n");
        	sqlstr.append(" 		    , TO_CHAR(CONT_DATE,'YYYY-MM-DD') AS CONT_DATE	\n");
        	sqlstr.append(" 		    , MEMBER_NAME	\n");
        	sqlstr.append(" 		    FROM (	\n");
        	sqlstr.append(" 		        SELECT 	\n");
        	sqlstr.append(" 		            BUILD_NAME AS  CONT_NAME	\n");
        	sqlstr.append(" 		            , DEPT_NAME AS  CONT_DEPT	\n");
        	sqlstr.append(" 		            , CON_DATE AS  CONT_DATE	\n");
        	sqlstr.append(" 		            , B.MEMBER_NAME	\n");
        	sqlstr.append(" 		        FROM SUB2000T A, CONT_OUT_SUPP B	\n");
        	sqlstr.append(" 		        WHERE A.SUB_CONT_MOD_ID=B.SUB_CONT_MOD_ID 	\n");
        	sqlstr.append(" 		            AND A.SUB_CONT_MOD_ID = '"+ param.get("CONT_ID") +"' 	\n");
        	sqlstr.append(" 		            AND B.SIGN_SEQ='2'	\n");
        	sqlstr.append(" 		            AND A.MOD_NO = '"+ param.get("MOD_NO") +"'	\n");
        	sqlstr.append(" 		            AND A.TRUE_RANDOM = '"+ param.get("TRUE_RANDOM") +"'	\n");
        	sqlstr.append(" 			\n");
        	sqlstr.append(" 		        UNION ALL	\n");
        	sqlstr.append(" 			\n");
        	sqlstr.append(" 		        SELECT 	\n");
        	sqlstr.append(" 		            CST_DOC_NAME AS  CONT_NAME	\n");
        	sqlstr.append(" 		            , DEPT_NAME AS  CONT_DEPT	\n");
        	sqlstr.append(" 		            , ORD_CON_DATE AS  CONT_DATE	\n");
        	sqlstr.append(" 		            , B.MEMBER_NAME	\n");
        	sqlstr.append(" 		        FROM PUR2000T A, CONT_PUR_SUPP B	\n");
        	sqlstr.append(" 		        WHERE A.PUR_CONT_MOD_ID=B.PUR_CONT_MOD_ID 	\n");
        	sqlstr.append(" 		            AND A.PUR_CONT_MOD_ID = '"+ param.get("CONT_ID") +"' 	\n");
        	sqlstr.append(" 		            AND B.SIGN_SEQ='2'	\n");
        	sqlstr.append(" 		            AND A.MOD_NO = '"+ param.get("MOD_NO") +"'	\n");
        	sqlstr.append(" 		            AND A.TRUE_RANDOM = '"+ param.get("TRUE_RANDOM") +"'	\n");
        	sqlstr.append(" 		)	\n");

			search.setStatement(sqlstr.toString());
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ){
            	valueobject = RowSetUtility.getValueObject(rowSet.getRow(0));
                this.storage.setMessage(valueobject.get("CONT_NAME") +"$"+ valueobject.get("CONT_DEPT") +"$" + valueobject.get("CONT_DATE") +"$"+ valueobject.get("MEMBER_NAME"));
            }
            	
        }
        catch(WAFSQLException se) {
        	super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            //System.out.println(sqlstr.toString());
            //System.out.println("WAFSQLException : " + se.getMessage());
        }
        finally {

        	if( valueobject == null ) valueobject = new ValueObject();
        	//System.out.println("### valueobject size : " + valueobject.size());
            return valueobject;
        }
	}
	
	public ValueObject getSearchResult(ValueObject param){
		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append(" 	SELECT 																 	\n");
        	sqlstr.append(" 	      CONT_NM AS CONT_NAME,	 											\n");
        	sqlstr.append(" 	      DEPT_NM AS CONT_DEPT,	 											\n");
        	sqlstr.append(" 	      ISU_VENDNM AS MEMBER_NAME,	 									\n");
        	sqlstr.append(" 	      ACMP_YYYY AS CONT_DATE	 										\n");
        	sqlstr.append(" 	  FROM RESULT_PLOOF_INFO	 											\n");
        	sqlstr.append(" 	  WHERE CORP_ID = '"+ param.get("CONT_ID") +"'	 						\n");
        	sqlstr.append(" 	  AND SOC_CD = '"+ param.get("MOD_NO") +"'	 							\n");
        	sqlstr.append(" 	  AND TRUE_RANDOM = '"+ param.get("TRUE_RANDOM") +"'	 				\n");
        	sqlstr.append(" 	UNION ALL																\n");
        	sqlstr.append(" 	SELECT 																 	\n");
        	sqlstr.append(" 	      CONT_NM AS CONT_NAME,	 											\n");
        	sqlstr.append(" 	      DEPT_NM AS CONT_DEPT,	 											\n");
        	sqlstr.append(" 	      ISU_VENDNM AS MEMBER_NAME,	 									\n");
        	sqlstr.append(" 	      ACMP_YYYY AS CONT_DATE	 										\n");
        	sqlstr.append(" 	  FROM PUR_RESULT_PLOOF_INFO	 										\n");
        	sqlstr.append(" 	  WHERE CORP_ID = '"+ param.get("CONT_ID") +"'	 						\n");
        	sqlstr.append(" 	  AND SOC_CD = '"+ param.get("MOD_NO") +"'	 							\n");
        	sqlstr.append(" 	  AND TRUE_RANDOM = '"+ param.get("TRUE_RANDOM") +"'	 				\n");

			search.setStatement(sqlstr.toString());
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ){
            	valueobject = RowSetUtility.getValueObject(rowSet.getRow(0));
                this.storage.setMessage(valueobject.get("CONT_NAME") +"$"+ valueobject.get("CONT_DEPT") +"$" + valueobject.get("CONT_DATE") +"$"+ valueobject.get("MEMBER_NAME"));
            }
            	
        }
        catch(WAFSQLException se) {
        	super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            //System.out.println(sqlstr.toString());
            //System.out.println("WAFSQLException : " + se.getMessage());
        }
        finally {

        	if( valueobject == null ) valueobject = new ValueObject();
        	//System.out.println("### valueobject size : " + valueobject.size());
            return valueobject;
        }
	}
}