/**
 * @(#)file    BidOpenListtBiz.java
 * @(#)author  Son Hyung Min
 * @(#)version 1.0
 * @(#)date    2014-07-14
 * @(#)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.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.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class BidOpenListBiz extends Biz {

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

	public void doBiz() throws Exception {
		
		if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getBoardList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}
	
	/**
	 * 발주의뢰(외주) 접수를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private RowSet getBoardList(ValueObject param) {
		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append("SELECT BID_NO||'-'||BID_DEG AS BID_ID																			\n");
        	sqlstr.append("     ,BID_NO																													\n");
        	sqlstr.append("     ,BID_DEG																												\n");
        	sqlstr.append("     ,DEPT_CD																												\n");
        	sqlstr.append("     ,DEPT_NM																												\n");
        	sqlstr.append("     ,DEMANDED_ID																										\n");
        	sqlstr.append("     ,BID_MASTER_NM																									\n");
        	sqlstr.append("     ,TO_CHAR(BID_TO_DT,'YYYY-MM-DD HH24:MI') AS BID_TO_DT										\n");
        	sqlstr.append("     ,TO_CHAR(OPEN_DT,'YYYY-MM-DD HH24:MI') AS OPEN_DT											\n");
        	sqlstr.append("     ,OPEN_EMPNM																										\n");
        	sqlstr.append("     ,OPEN_EMPID 																											\n");
        	sqlstr.append("  FROM BID_MASTER																										\n");
        	sqlstr.append("  WHERE BID_TO_DT < SYSDATE 																					\n");
        	sqlstr.append("  AND OPEN_EMPID =  '"+storage.getUser().get("ACCT_ID")+"'												\n");
        	sqlstr.append("  AND STATUS_CD IN ('04','05','06')																					\n");
        	sqlstr.append("  AND DEMANDED_KIND = 'O'						\n");
        	if(!param.get("P_BID_MASTER_NM").isEmpty()){
				sqlstr.append("  AND UPPER(BID_MASTER_NM) LIKE '%'||'"+param.get("P_BID_MASTER_NM").toUpperCase()+"'||'%'					\n");
			}
        	if(!"".equals(params.get("P_DEPT_CD"))) {
        		sqlstr.append(" AND DEPT_CD = ? 																												\n");
    			search.addParameter(params.get("P_DEPT_CD"));
        	}
			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;
	}
}