/**
 * @(#)file    DivContPopBiz.java
 * @(#)author  chj
 * @(#)version 1.0
 * @(#)date    2015-02-24
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.bid.purchase.biz;

import kr.co.hsnc.common.base.WAFLogger;
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.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import oracle.toplink.internal.ox.StrBuffer;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;


/**
 * 
 * @version 1.0
 */
public class DivContPopBiz extends Biz {
	BidCommonBiz BidCommonBiz = new BidCommonBiz(storage);
	public DivContPopBiz(HandlerStorage storage) {
		super(storage);
	}

    public void doBiz() throws Exception {
		
		ValueObject vo = new ValueObject();
		vo = getDivCont(params.get("bid_no"),params.get("bid_deg"));	//입찰 정보 조회

		storage.addValueObject("divCont", vo);
   }

    public ValueObject getDivCont(String bid_no,String bid_deg) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        StrBuffer sqlstr = new StrBuffer();
        ValueObject rowObject = null;
        
        try {
        	
        	
        	sqlstr.append("			SELECT  V1 , V2, V3, V4, V5 ,  V1_CD , V2_CD, V3_CD, V4_CD, V5_CD                                                                                                                                                                                                                   \n");
        	sqlstr.append("			     , (SELECT F_GET_CURRENCY_DIGIT(CURRENCY_GB) FROM BID_PUR_CONDI WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' AND VENDOR_CD = V1_CD) AS V1_POINT                                                                                                                                                                                                                 \n");
        	sqlstr.append("			     , (SELECT F_GET_CURRENCY_DIGIT(CURRENCY_GB) FROM BID_PUR_CONDI WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' AND VENDOR_CD = V2_CD) AS V2_POINT                                                                                                                                                                                                                 \n");
        	sqlstr.append("			     , (SELECT F_GET_CURRENCY_DIGIT(CURRENCY_GB) FROM BID_PUR_CONDI WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' AND VENDOR_CD = V3_CD) AS V3_POINT                                                                                                                                                                                                                \n");
        	sqlstr.append("		    	 , (SELECT F_GET_CURRENCY_DIGIT(CURRENCY_GB) FROM BID_PUR_CONDI WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' AND VENDOR_CD = V4_CD) AS V4_POINT                                                                                                                                                                                                               \n");
        	sqlstr.append("			     , (SELECT F_GET_CURRENCY_DIGIT(CURRENCY_GB) FROM BID_PUR_CONDI WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' AND VENDOR_CD = V5_CD) AS V5_POINT                                                                                                                                                                                                                 \n");
        	sqlstr.append("			FROM (                                                                                                                                                                                                                 \n");
        	sqlstr.append("			SELECT MAX(V1) AS V1                                                                                                                                                                                                                    \n");
        	sqlstr.append("			     , MAX(V2) AS V2                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V3) AS V3                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V4) AS V4                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V5) AS V5                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V6) AS V6                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V1_CD) AS V1_CD                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V2_CD) AS V2_CD                                                                                                                                                                                                                		\n");
        	sqlstr.append("			     , MAX(V3_CD) AS V3_CD                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V4_CD) AS V4_CD                                                                                                                                                                                                               		\n");
        	sqlstr.append("			     , MAX(V5_CD) AS V5_CD                                                                                                                                                                                                               		\n");
        	sqlstr.append("			FROM (                                                                                                                                                                                                                                  \n");
        	sqlstr.append("			SELECT DECODE(ESTI_NUM,1,A.VENDOR_NAME) AS V1                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,2,A.VENDOR_NAME) AS V2                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,3,A.VENDOR_NAME) AS V3                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,4,A.VENDOR_NAME) AS V4                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,5,A.VENDOR_NAME) AS V5                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,6,A.VENDOR_NAME) AS V6                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,1,A.VENDOR_CD) AS V1_CD                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,2,A.VENDOR_CD) AS V2_CD                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,3,A.VENDOR_CD) AS V3_CD                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,4,A.VENDOR_CD) AS V4_CD                                                                                                                                                                                           \n");
        	sqlstr.append("			     , DECODE(ESTI_NUM,5,A.VENDOR_CD) AS V5_CD                                                                                                                                                                                           \n");
        	sqlstr.append("			FROM (                                                                                                                                                                                                                                  \n");
        	sqlstr.append("			                SELECT ROWNUM AS ESTI_NUM                                                                                                                                                                                               \n");
        	sqlstr.append("			                         ,  AA.*                                                                                                                                                                                                        \n");
        	sqlstr.append("			                FROM (SELECT A.* FROM BID_SUPP A , (SELECT BID_NO, BID_DEG,VENDOR_CD FROM BID_SUPP WHERE BID_NO = '"+bid_no+"' AND BID_DEG = '"+bid_deg+"' GROUP BY BID_NO,BID_DEG, VENDOR_CD) B      									\n");
        	sqlstr.append("			                WHERE A.BID_NO= B.BID_NO AND A.BID_DEG = B.BID_DEG AND A.VENDOR_CD = B.VENDOR_CD AND ESTI_TOT_AMT IS NOT NULL  ORDER BY ESTI_TOT_AMT NULLS LAST, A.VENDOR_CD ) AA                                                         							\n");
        	sqlstr.append("			      ) A                                                                                                                                                                                                                         		\n");
        	sqlstr.append("			WHERE ESTI_NUM < 6                                                                                                                                                                                                                      \n");
        	sqlstr.append("			)																																																										\n");
        	sqlstr.append("			)																																																										\n");
        	
        	
        	search.setStatement(sqlstr.toString());
            listRowSet = search.execute();
            rowObject = RowSetUtility.getValueObject(listRowSet.getRow(0));
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getDivCont() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {
            if( rowObject == null ) rowObject = new ValueObject();
            return rowObject;
        }
    }
    
}
