/**
 * @(#)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.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.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 BidCurrentStatusListBiz extends Biz {

	public BidCurrentStatusListBiz(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 A.DEPT_NM -- 현장명            										\n");                                       
        	sqlstr.append("                   ,A.BID_NO										\n");
        	sqlstr.append("                   ,A.BID_DEG										\n");
        	sqlstr.append("                   ,A.BID_MASTER_NM										\n");
        	sqlstr.append("                   ,(SELECT DEMANDED_TITLE FROM PMM_DEMANDED_REQUEST WHERE DEMANDED_ID = A.DEMANDED_ID) DEMANDED_TITLE -- 발주의뢰명                                                   										\n");
        	sqlstr.append("                   ,DECODE(A.SITEDESCDATE,NULL,'-',TO_CHAR(A.SITEDESCDATE,'yyyy-mm-dd')) SITEDESCDATE -- 현장설명                                                   										\n");
        	sqlstr.append("                   ,DECODE(A.BID_TO_DT,NULL,'-',TO_CHAR(A.BID_TO_DT,'yyyy-mm-dd')) BID_TO_DT -- 견적마감                                                   										\n");
        	sqlstr.append("                   ,DECODE(B.TBE_FIN_DT,NULL,'-',TO_CHAR(B.TBE_FIN_DT,'yyyy-mm-dd')) TBE_FIN_DT -- 기술검토                                                   										\n");
        	sqlstr.append("                   ,DECODE(A.OPEN_DT,NULL,'-',TO_CHAR(A.OPEN_DT,'yyyy-mm-dd')) OPEN_DT -- 견적서 개찰                                                   										\n");
        	sqlstr.append("                   ,DECODE(A.CONT_APP_DATE,NULL,'-',TO_CHAR(A.CONT_APP_DATE,'yyyy-mm-dd')) CONT_APP_DATE -- 시행결의                                                   										\n");
        	sqlstr.append("                   ,F_GET_CODE_NAME2('BID_STATUS_CD',A.STATUS_CD) STATUS_NM -- 진행상태                                                   										\n");
        	sqlstr.append("                   ,A.STATUS_CD -- 진행상태                                                   																											\n");
        	sqlstr.append("                   ,F_GET_USER_NM(A.PUREMPNO) AS PUREMPNM    																													\n");
        	sqlstr.append("                   ,A.PUREMPNO																																									\n");
        	sqlstr.append("                   ,A.OPEN_EMPNM																																								\n");
        	sqlstr.append("                   ,B.EVAL_EMPNM                                               																													\n");
        	sqlstr.append("                 FROM BID_MASTER A, TBE_INFO B                                                   																						\n");
        	sqlstr.append("                 WHERE A.STATUS_CD NOT IN ('10','91','92','93')                                                   																		\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 <> 'O'																																				\n");
        	if(param.get("user_check").equals("false")){
        		sqlstr.append("  					 AND A.PUREMPNO =  '"+storage.getUser().get("ACCT_ID")+"'																\n");
        	}
        	if(!param.get("P_DEMANDED_STATUS").isEmpty()){
				sqlstr.append("                AND A.STATUS_CD = '"+param.get("P_DEMANDED_STATUS")+"'																									\n");
			}
        	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(!"".equals(params.get("P_DEPT_CD"))) {
        		sqlstr.append(" AND A.DEPT_CD = ?																												 \n");
        		search.addParameter(params.get("P_DEPT_CD"));
        	}
        	sqlstr.append("                 ORDER BY A.DEMANDED_DATE 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;
	}
}