/**
 * @(#)file    ContChgRcvListBiz.java
 * @(#)author  kyj
 * @(#)version 1.0
 * @(#)date    2015-01-22
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.purchase.biz;


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.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;
import oracle.toplink.internal.ox.StrBuffer;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class ContChgRcvListBiz extends Biz {

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

	public void doBiz() throws Exception {
		
		if(storage.compareEvent("")){
			
		}else if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getContChgRcvList(params));
			
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			
		}
	}


	/**
	 * 계약 대상리스트 조회
	 * @param Params
	 * @return
	 */
	private RowSet getContChgRcvList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StrBuffer sqlstr = new StrBuffer();
       
        try{
			
        	
     		sqlstr.append("    SELECT A.PUR_CONT_MOD_ID	\n");
     		sqlstr.append(" 	     ,A.ORD_DOC_NO	\n");
        	sqlstr.append(" 	     ,A.MOD_NO	\n");
        	sqlstr.append(" 	     ,A.DEPT_NAME	\n");
        	sqlstr.append(" 	     ,A.CST_DOC_NAME	\n");
        	sqlstr.append(" 	     ,TO_CHAR(A.ORD_CON_DATE, 'YYYY-MM-DD') AS ORD_CON_DATE	\n");
        	sqlstr.append(" 	     ,F.MEMBER_NAME	\n");
        	sqlstr.append(" 	     ,A.ORD_AMT 	\n");
        	sqlstr.append(" 	     ,F_GET_D_CURRENCY(A.ORD_SUPPLY_AMT, F_GET_CURRENCY_DIGIT(A.CURRENCY_GB)) AS ORD_SUPPLY_AMT	\n");
        	sqlstr.append(" 	     ,TO_CHAR(A.ORD_START_DATE, 'YYYY-MM-DD') || '~' || TO_CHAR(A.ORD_END_DATE, 'YYYY-MM-DD') AS WORK_START_END_DATE	\n");
        	sqlstr.append(" 	     ,A.ORD_START_DATE	\n");
        	sqlstr.append(" 	     ,A.CON_GB 	\n");
        	sqlstr.append(" 	     ,A.STATUS_CD 	\n");
        	sqlstr.append(" 	     ,DECODE(A.CURRENCY_GB, 'KRW', DECODE(A.MOD_NO,0,DECODE(A.CON_GB,'1','4','2','3',A.CON_GB),6), DECODE(A.MOD_NO,0,9,10)) AS TEMPLATE_CD \n");
        	sqlstr.append(" 	     ,DECODE(DECODE(A.CURRENCY_GB, 'KRW', DECODE(A.MOD_NO,0,DECODE(A.CON_GB,'1','4','2','3',A.CON_GB),6), DECODE(A.MOD_NO,0,9,10)), NULL, 0, 1) AS BTN 	\n");
        	sqlstr.append(" 	     ,A.CURRENCY_GB 	\n");
        	sqlstr.append(" 	     ,TO_CHAR(A.ORD_END_DATE, 'YYYY-MM-DD') ORD_END_DATE  	\n");
        	sqlstr.append(" 	     ,NVL(A.MOD_GB, '00') MOD_GB  	\n");
        	sqlstr.append(" 	     ,F_GET_CODE_NAME('MOD_GB',NVL(A.MOD_GB, '00')) MOD_NM   	\n");
        	sqlstr.append(" 	     , A.STATUS_CD STATUS_CD  	\n");
        	sqlstr.append(" 	     ,F_GET_CODE_NAME('CONT_STATUS_CD', A.STATUS_CD) STATUS_NM  	\n");
        	sqlstr.append(" 	     ,E.DEMANDED_ID  	\n");
        	sqlstr.append(" 	  FROM PUR2000T A,  CONT_PUR_SUPP F	, PMM_DEMANDED_REQUEST E\n");
        	sqlstr.append("   WHERE 1=1					\n");
    		sqlstr.append("   	AND A.PUR_CONT_MOD_ID = F.PUR_CONT_MOD_ID(+)				\n");
    		sqlstr.append("   	AND A.DEM_ID = E.DEMANDED_ID(+)				\n");
    		sqlstr.append("   	AND F.SIGN_SEQ(+) = 2				\n");
    		sqlstr.append("   	AND MOD_NO>0				\n");
    		
    		
    		// 현장
        	if(!param.get("P_DEPT_CD").isEmpty()) {
        		sqlstr.append(" AND A.DEPT_CD = ?																											 \n");
    			search.addParameter(params.get("P_DEPT_CD"));
        	}
        	if(!param.get("p_build_nm").isEmpty())	//계약명        		
        		sqlstr.append("   AND UPPER(A.CST_DOC_NAME) LIKE UPPER('%"+param.get("p_build_nm")+"%')				\n");
        	if(!param.get("p_dealer_nm").isEmpty())		//업체명       		
        		sqlstr.append("   AND UPPER(F.MEMBER_NAME) LIKE UPPER('%"+param.get("p_dealer_nm")+"%')				\n");
        	if(!param.get("ContFromDt").isEmpty())		//계약기간
        		sqlstr.append("   AND TO_CHAR(A.ORD_CON_DATE, 'YYYY-MM-DD') >= '"+param.get("ContFromDt")+"'				\n");
        	if(!param.get("ContToDt").isEmpty())
        		sqlstr.append("   AND TO_CHAR(A.ORD_CON_DATE, 'YYYY-MM-DD') <= '"+param.get("ContToDt")+"'				\n");
        	if(!param.get("P_CONT_REQ_STATUS").equals("00"))	//변경계약상태
        		sqlstr.append("   AND A.STATUS_CD in ("+param.get("P_CONT_REQ_STATUS")+")				\n");
        	
        	sqlstr.append("   ORDER BY A.ORD_DOC_NO,A.MOD_NO DESC,A.CST_DOC_NAME ASC 	\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();
            }
        }
        //WAFLogger.debug("### rowSet size : " + rowSet.size());
        return rowSet;
	}
	
}