/**
 * @(#)file    BidIngBiz.java
 * @(#)author  Son Hyung Min 
 * @(#)version 1.0
 * @(#)date    2014-06-23
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.bid.outside.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 RebidListBiz extends Biz {

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

	public void doBiz() throws Exception {
		
        if(storage.compareEvent("")){
        	
        	RowSet rsStatusCd = getStatusCdRowSet();
 			storage.addRowSet("RS_STATUS_CD", rsStatusCd);
 			
        }else if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getBoardList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}
	

	public RowSet getStatusCdRowSet() throws Exception {
		  
		  Search search = new SearchImpl();
		  RowSet listRowSet = null;
		  StringBuffer sql = new StringBuffer();
		  
		  try{
			  
			  sql.append("		SELECT DETAILCD , DETAILNM              \n");
			  sql.append("		FROM EBID_COM911T        		 \n");
			  sql.append("		WHERE BASECD = 'BID_RE_SUCCESS_CD'    \n");
			  sql.append("		AND DETAILCD  IN ( '20' , '25', '30')     \n");

			  search.setStatement(sql.toString());
	          listRowSet = search.execute();
	            
	          WAFLogger.debug(sql.toString());
		  }
		  catch(WAFSQLException e) {
	          super.doException(e);
	          return null;
	      }
	      finally {
	          if( listRowSet == null ){
	        	  listRowSet = new RowSetImpl();
	          }
	          
	      }
	      return listRowSet;
	}
	
	/**
	 * 발주의뢰(외주) 접수를 검색하여 리턴
	 * @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.BID_DEG																															\n");
        	sqlstr.append("      , B.DEPT_NM																																\n");
        	sqlstr.append("      , A.BID_NO																																\n");
        	sqlstr.append("      , A.DEMANDED_TITLE																													\n");
        	sqlstr.append("      , A.BID_MASTER_NM																													\n");
        	sqlstr.append("      , A.ONBID_YN																															\n");
        	sqlstr.append("      , TO_CHAR(A.REQUEST_DATE,'YYYY-MM-DD') AS REQUEST_DATE														\n");
        	sqlstr.append("      , TO_CHAR(A.RECEIPT_DATE,'YYYY-MM-DD') AS RECEIPT_DATE															\n");
        	sqlstr.append("      , A.STATUS_CD 																															\n");
        	sqlstr.append("      , F_GET_CODE_NAME('BID_RE_SUCCESS_CD',A.STATUS_CD) AS STATUS_NM												\n");
        	sqlstr.append("      , A.DEMANDED_ID		 																												\n");
        	sqlstr.append("      , TO_CHAR(A.OPEN_DT,'YYYY-MM-DD HH24:MI') AS OPEN_DT															\n");
        	sqlstr.append("      , A.OPEN_EMPNM		 																												\n");
        	sqlstr.append("      , TO_CHAR(C.TBE_FIN_DT,'YYYY-MM-DD HH24:MI') AS TBE_FIN_DT													\n");
        	sqlstr.append("   FROM BID_MASTER A	, ACZ10100V B	, TBE_INFO	C																				\n");
        	sqlstr.append("   WHERE A.DEPT_CD = B.DEPT_CD																										\n");
        	sqlstr.append("   AND A.BID_NO = C.BID_NO(+)																											\n");
        	sqlstr.append("   AND A.BID_DEG = C.BID_DEG(+)																										\n");
        	sqlstr.append("   AND A.STATUS_CD IN ('20', '25', '30')																								\n");
        	sqlstr.append("   AND A.DEMANDED_KIND IN ('O')	       																						\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_DEMANDED_FROM_DT").isEmpty() && !param.get("P_DEMANDED_TO_DT").isEmpty())
        		sqlstr.append("   AND A.REQUEST_DATE BETWEEN  '"+param.get("P_DEMANDED_FROM_DT")+"' AND '"+param.get("P_DEMANDED_TO_DT")+"'		\n");
        	if(!param.get("P_RCEIPT_FROM_DT").isEmpty() && !param.get("P_RCEIPT_TO_DT").isEmpty())
        		sqlstr.append("   AND A.REQUEST_DATE BETWEEN  '"+param.get("P_RCEIPT_FROM_DT")+"' AND '"+param.get("P_RCEIPT_TO_DT")+"'					\n");
        	if(!param.get("P_DEMANDED_STATUS").isEmpty())
        		sqlstr.append("   AND A.STATUS_CD = '"+param.get("P_DEMANDED_STATUS")+"'																						\n");
        	
        	sqlstr.append("   ORDER BY OPEN_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;
	}
	
}