/**
 * @(#)file    BidPlanBiz.java
 * @(#)author  Son Hyung Min 
 * @(#)version 1.0
 * @(#)date    2014-06-23
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.bid.purchase.biz;



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

import oracle.toplink.internal.ox.StrBuffer;
import kr.co.hsnc.common.base.WAFLogger;
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.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.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class BidIngListBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
		
		if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getBoardList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}else if(storage.compareEvent("FORM_SELECT")){
//	            user = getOrderReqeustValueObject(params.get("DEMANDED_ID"));
//	            storage.addValueObject("Order", user);
//	            /**		파일 목록 조회			 **/
//	        storage.addValueObject(user.get("DOC_ETC_IN"), getfilelist(user.get("DOC_ETC_IN"),params.get("DEMANDED_ID"),user.get("DOC_ETC_IN")));
//	        storage.addValueObject(user.get("DOC_ETC_OUT"), getfilelist(user.get("DOC_ETC_OUT"),params.get("DEMANDED_ID"),user.get("DOC_ETC_OUT")));
		}else if(storage.compareEvent("GET_CORP_LIST")){
			 params.set("sheet1", getCorpList(params));
	         storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
		
	}
	
	private ValueObject getfilelist(String filegb,String id,String returnid){
		ValueObject vo = new ValueObject();
		RowSet rowSet = FileManager.getFileList(filegb, id);
		String str="";
	        for(int i=0;i<rowSet.size();i++){
	        	Row row=rowSet.getRow(i);
	        	str += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
	        	if(i+1!=rowSet.size()){
	        		str +="\r\n";
	        	}
	        }
	        vo.set(returnid, str);
		return vo;
	}

	/**
	 * 입찰계획리스트 조회
	 * @param Params
	 * @return
	 */
	private RowSet getBoardList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append(" SELECT A.DEMANDED_ID																										\n");
        	sqlstr.append(" ,A.DEPT_CD																														\n");
        	sqlstr.append(" ,BID_NO||'-'||BID_DEG AS BID_ID																							\n");
        	sqlstr.append(" ,BID_NO																																\n");
        	sqlstr.append(" ,BID_DEG																															\n");
        	sqlstr.append(" ,BID_MASTER_NM																												\n");
        	sqlstr.append(" ,(SELECT DEPT_NM FROM ACZ10100V WHERE DEPT_CD = A.DEPT_CD) AS DEPT_NM						\n");
        	sqlstr.append(" ,A.DEMANDED_TITLE																											\n");
        	sqlstr.append(" ,A.EMP_NO																															\n");
        	sqlstr.append(" ,A.EMP_NM																															\n");
        	sqlstr.append("  ,GET_USER_BUSEO_NM(A.EMP_NO) AS BUSEO_NM                  								                    \n");
        	sqlstr.append(" ,A.DEMANDED_DATE																												\n");
        	sqlstr.append(" ,A.STATUS_CD																													\n");
        	sqlstr.append(" ,TO_CHAR(A.SITEDESCDATE,'YYYY-MM-DD HH24:MI') AS SITEDESCDATE										\n");
        	sqlstr.append(" ,TO_CHAR(A.BID_TO_DT,'YYYY-MM-DD HH24:MI') AS BID_TO_DT													\n");
        	sqlstr.append(" ,DECODE(A.POSTPONE_REASON,'','N','Y') AS POSTPONE_YN															\n");
			sqlstr.append(" ,DECODE(A.MODIFY_REASON,'','N','Y') AS MODIFY_YN																	\n");
			sqlstr.append(" ,NVL(A.OPEN_CONFIRM_YN,'N')	AS OPEN_CONFIRM_YN																\n");
        	sqlstr.append("   FROM BID_MASTER A																											\n");	
        	sqlstr.append("   WHERE 1=1																														\n");
        	sqlstr.append("   AND STATUS_CD IN ('03','04')																								\n");
        	sqlstr.append("  AND A.DEMANDED_KIND IN ('C','P','B')	       																				\n");
        	if(!"".equals(params.get("P_DEPT_CD"))) {
        		sqlstr.append(" AND A.DEPT_CD = ?																												 \n");
        		search.addParameter(params.get("P_DEPT_CD"));
        	}
        	if(!param.get("P_BID_MASTER_NM").isEmpty())
        		sqlstr.append("   AND UPPER(A.BID_MASTER_NM) LIKE '%'|| '"+param.get("P_BID_MASTER_NM").toUpperCase()+"' ||'%'							\n");

        	if(!param.get("P_SITEDESCDATE_FROM_DT").isEmpty())
        		sqlstr.append("   AND A.SITEDESCDATE >  TO_DATE('"+param.get("P_SITEDESCDATE_FROM_DT")+"'||'000000','YYYY-MM-DDHH24MISS') 					\n");
        	if(!param.get("P_SITEDESCDATE_TO_DT").isEmpty())
        		sqlstr.append("   AND A.SITEDESCDATE < TO_DATE('"+param.get("P_SITEDESCDATE_TO_DT")+"'||'235959','YYYY-MM-DDHH24MISS') 						\n");
        	
        	if(!param.get("P_BID_FROM_DT").isEmpty())
        		sqlstr.append("   AND A.BID_TO_DT >  TO_DATE('"+param.get("P_BID_FROM_DT")+"'||'000000','YYYY-MM-DDHH24MISS') 					\n");
        	if(!param.get("P_BID_TO_DT").isEmpty())
        		sqlstr.append("   AND A.BID_TO_DT < TO_DATE('"+param.get("P_BID_TO_DT")+"'||'235959','YYYY-MM-DDHH24MISS') 						\n");
        	
        	if(!param.get("P_DEMANDED_STATUS").isEmpty())
        		sqlstr.append("   AND A.STATUS_CD = '"+param.get("P_DEMANDED_STATUS")+"'																						\n");
    //    	sqlstr.append("   AND BID_TO_DT > SYSDATE																					\n");
        	sqlstr.append("   ORDER BY BID_TO_DT DESC																					\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;
	}
	

	
	/**
	 * 발주의뢰(외주) 접수 상세를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private ValueObject getOrderReqeustValueObject(String DEMANDED_ID) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
            sqlstr.append("       SELECT TO_CHAR(TO_DATE(A.DEMANDED_DATE,'YYYYMMDD'),'YYYY-MM-DD') AS DEMANDED_DATE,                                        \n");
		       sqlstr.append("       A.DEMANDED_TITLE,                                        \n");
		       sqlstr.append("       A.DEMANDED_KIND,                                        \n");
		       sqlstr.append("       A.CURRENCY_GB,                                        \n");
		       sqlstr.append("       A.DEPT_CD,                                        \n");
		       sqlstr.append("       A.EMP_NO,                                        \n");
		       sqlstr.append("       GET_USER_BUSEO_NM(A.EMP_NO) AS BUSEO_NM,                                          \n");
		       sqlstr.append("       A.DELIVERY_CONDITION,                                        \n");
		       sqlstr.append("       A.CRE_BY,                                        \n");
		       sqlstr.append("       A.CRE_DATE,                                        \n");
		       sqlstr.append("       A.UPD_BY,                                        \n");
		       sqlstr.append("       A.UPD_DATE,                                        \n");
		       sqlstr.append("       A.DEMANDED_NO,                                        \n");
		       sqlstr.append("       A.BID_APP_DATE,                                        \n");
		       sqlstr.append("       A.BID_APP_NO,                                        \n");
		       sqlstr.append("       A.PAY_ITEM,                                        \n");
		       sqlstr.append("       F_GET_PAY_ITEM_VIEW(A.PAY_ITEM,A.COST_CODE,A.COST_TYPE,A.DEPT_CD) AS PAY_ITEM_NM,                \n");
		       sqlstr.append("       A.COST_CODE,                                        \n");
		       sqlstr.append("       A.COST_TYPE,                                        \n");
		       sqlstr.append("       TO_CHAR(A.RECEIPT_DATE,'YYYY-MM-DD') AS RECEIPT_DATE,                                        \n");
		       sqlstr.append("       A.SITEDESC_FLAG,                                        \n");
		       sqlstr.append("       A.SITEDESCCHRGPRSNID,                                        \n");
		       sqlstr.append("       F_GET_USER_NM(A.SITEDESCCHRGPRSNID) AS SITEDESCCHRGPRSNNM,                                        \n");
		       sqlstr.append("       A.SITEDESCTELNO,                                        \n");
		       sqlstr.append("       TO_CHAR(A.SITEDESCDATE,'YYYY-MM-DD') AS SITEDESCDATE,                                        \n");
		       sqlstr.append("       TO_CHAR(A.SITEDESCDATE,'HH24') AS SITEDESCDATE_HH,                                        \n");
		       sqlstr.append("       TO_CHAR(A.SITEDESCDATE,'MI') AS SITEDESCDATE_MI,                                        \n");
		       sqlstr.append("       A.SITEDESCPLACE,                                        \n");
		       sqlstr.append("       A.SITEDESCEMAIL,                                        \n");
		       sqlstr.append("       A.PUREMPNO,                                        \n");
		       sqlstr.append("       F_GET_USER_NM(A.PUREMPNO) AS PUREMPNM,                                        \n");
		       sqlstr.append("       A.INNER_REMARKS,                                        \n");
		       sqlstr.append("       A.TBE_FLAG,                                        \n");
		       sqlstr.append("       A.OUTER_REMARKS,                                        \n");
		       sqlstr.append("       A.DEMANDED_ID,                                        \n");
		       sqlstr.append("       TO_CHAR(A.REQUEST_DATE,'YYYY-MM-DD') AS REQUEST_DATE,                                        \n");
		       sqlstr.append("       A.ATT_ID,                                        \n");
		       sqlstr.append("       A.BID_NO,                                        \n");
		       sqlstr.append("       A.BID_DEG,                                        \n");
		       sqlstr.append("       DECODE(A.ONBID_YN,'Y','온라인','N','오프라인','') AS ONBID_YN,                                        \n");
		       sqlstr.append("       A.CMPT_CD,                                        \n");
		       sqlstr.append("       A.SUCC_CD,                                        \n");
		       sqlstr.append("       TO_CHAR(TO_DATE(A.NOTI_DY,'YYYYMMDD'),'YYYY-MM-DD') AS NOTI_DY,                                        \n");
		       sqlstr.append("       A.SITEDESC_CRG_NM,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_FROM_DT,'YYYY-MM-DD') AS BID_FROM_DT,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_FROM_DT,'HH24') AS BID_FROM_HH,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_FROM_DT,'MI') AS BID_FROM_MI,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_TO_DT,'YYYY-MM-DD') AS BID_TO_DT,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_TO_DT,'HH24') AS BID_TO_HH,                                        \n");
		       sqlstr.append("       TO_CHAR(A.BID_TO_DT,'MI') AS BID_TO_MI,                                        \n");
		       sqlstr.append("       A.PO_EMPNM,                                        \n");
		       sqlstr.append("       A.OPEN_EMPNM,                                        \n");
		       sqlstr.append("       A.OPEN_EMPID,                                        \n");
		       sqlstr.append("       TO_CHAR(TO_DATE(A.DELI_FROM_DY,'YYYYMMDD'),'YYYY-MM-DD') AS DELI_FROM_DY,                                        \n");
		       sqlstr.append("       TO_CHAR(TO_DATE(A.DELI_TO_DY,'YYYYMMDD'),'YYYY-MM-DD') AS DELI_TO_DY,                                        \n");
		       sqlstr.append("       A.DELIVERY_PLACE,                                        \n");
		       sqlstr.append("       A.CONTFUL_RATE,                                        \n");
		       sqlstr.append("       A.CONTFUL_CONDI,                                        \n");
		       sqlstr.append("       A.WARR_RATE,                                        \n");
		       sqlstr.append("       A.WARR_CONDI,                                        \n");
		       sqlstr.append("       A.DEPT_NM,                                        \n");
		       sqlstr.append("       A.REQ_DEPT_CD,                                        \n");
		       sqlstr.append("       A.REQ_DEPT_NM,                                        \n");
		       sqlstr.append("       A.EMP_NM,                                        \n");
		       sqlstr.append("       A.STATUS_CD,                                        \n");
		       sqlstr.append("       A.OPEN_DT,                                        \n");
		       sqlstr.append("       A.CONT_APP_DATE,                                        \n");
		       sqlstr.append("       A.CONT_APP_NO,                                        \n");
		       sqlstr.append("       A.CONT_AMT,                                        \n");
		       sqlstr.append("       A.ACT_AMT,                                        \n");
		       sqlstr.append("       A.ACT_RATE,                                        \n");
		       sqlstr.append("       A.DOC_ETC_IN,                                        \n");
		       sqlstr.append("       A.DOC_ETC_OUT,                                        \n");
		       sqlstr.append("       A.BID_MASTER_NM,                                        \n");
		       sqlstr.append("       A.POSTPONE_REASON,                                        \n");
		       sqlstr.append("       A.CANCEL_REASON,                                        \n");
		       sqlstr.append("       A.CURRENCY_GB_YN,                                        \n");
		       sqlstr.append("       A.DELIVERY_CONDITION_YN,                                        \n");
		       //sqlstr.append("       A.EXCHANGE                                        \n");
		       sqlstr.append("       TO_CHAR(B.SUPP_REQ_DY,'YYYY-MM-DD') AS SUPP_REQ_DY,                                        \n");
		       sqlstr.append("       B.EVAL_EMPNM,                                        \n");
		       sqlstr.append("       B.EVAL_EMPID                                        \n");
		       sqlstr.append("        FROM BID_MASTER A  , TBE_INFO B                                      \n");
		       sqlstr.append("       WHERE A.DEMANDED_ID = '"+DEMANDED_ID+"'							\n");
		       sqlstr.append("       AND A.BID_NO = '"+params.get("BID_NO")+"'							\n");
		       sqlstr.append("       AND A.BID_DEG = '"+params.get("BID_DEG")+"'							\n");
		       sqlstr.append("       AND A.BID_NO = B.BID_NO(+)							\n");
			   sqlstr.append("       AND A.BID_DEG = B.BID_DEG(+)							\n");
			   sqlstr.append("  	  AND A.DEMANDED_KIND IN ('C','P','B')	       																									\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;
        }
	}
	
	/**
	 * 발주의뢰(외주) 접수 상세 / 견적대상업체 를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private RowSet getCorpList(ValueObject ValueObject) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append(" SELECT A.BID_NO,										\n");
        	sqlstr.append("        A.BID_DEG,											\n");
        	sqlstr.append("        A.VENDOR_CD,										\n");
        	sqlstr.append("        A.VENDOR_NO,										\n");
        	sqlstr.append("        A.VENDOR_NAME,									\n");
        	sqlstr.append("        A.REPRE_NO,										\n");
        	sqlstr.append("        A.CHARGE_NAME,									\n");
        	sqlstr.append("        A.CHAGE_HP,										\n");
        	sqlstr.append("        A.CHARGE_EMAIL,									\n");
        	sqlstr.append("        A.CURRENCY_GB,									\n");
        	sqlstr.append("        A.EXCHANGE,										\n");
        	sqlstr.append("        A.DELIVERY_CONDITION,						\n");
        	sqlstr.append("        A.SITEPARTI_YN,									\n");
        	sqlstr.append("        A.TBEAPPR_YN,									\n");
        	sqlstr.append("        A.TBEEVALUATE_YN,								\n");
        	sqlstr.append("        A.CERT_DN,											\n");
        	sqlstr.append("        A.ESTI_SIGNDATA,								\n");
        	sqlstr.append("        A.SIGN_DT,											\n");
        	sqlstr.append("        A.ATT_ID,											\n");
        	sqlstr.append("        A.RECM_DEPT_ID,									\n");
        	sqlstr.append("        A.RECM_DEPT_NM,								\n");
        	sqlstr.append("        A.RECM_EMPID,									\n");
        	sqlstr.append("        A.RECOMMEND_REASON,						\n");
        	sqlstr.append("        A.ESTI_TOT_AMT,								\n");
        	sqlstr.append("        A.WON_ESTI_TOT_AMT,						\n");
        	sqlstr.append("        A.SUCC_YN,										\n");
        	sqlstr.append("        A.FIB_REASON,									\n");
        	sqlstr.append("        A.CRE_BY,											\n");
        	sqlstr.append("        A.CRE_DATE,										\n");
        	sqlstr.append("        A.UPD_BY,											\n");
        	sqlstr.append("        A.UPD_DATE										\n");
        	sqlstr.append("   FROM BID_SUPP A										\n");
        	sqlstr.append(" WHERE BID_NO = '"+params.get("BID_NO")+"'	\n");
        	sqlstr.append(" AND BID_DEG = '"+params.get("BID_DEG")+"'		\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;
	}
	
	
}