/**
 * @(#)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.common.ui.InitCombo;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class BidHistoryListBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
        String event = storage.getEvent();
		if(event.equals("SHEET_SELECT")){
			params.set("sheet1", getBoardList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}else if (event.equals("GET_OUT_ITEM_CD2")) {
			ValueObject vo2 = getOutItemCd();
        	storage.addValueObject("VALUEOBJECT", vo2);
    		storage.setSuccess(true);
    	}else if (event.equals("GET_OUT_ITEM_CD3")) {
    		ValueObject vo3 = getOutItemCd();
        	storage.addValueObject("VALUEOBJECT", vo3);
    		storage.setSuccess(true);
    	}else {
			// 공종 콤보박스
			RowSet outItemTag1 = getOutItemRowSet("1");
			storage.addRowSet("outItemTag1", outItemTag1);
			RowSet outItemTag2 = getOutItemRowSet("2");
			storage.addRowSet("outItemTag2", outItemTag2);
			RowSet outItemTag3 = getOutItemRowSet("3");
			storage.addRowSet("outItemTag3", outItemTag3);
		}
	}
	
	/**
	 * 발주의뢰(외주) 접수를 검색하여 리턴
	 * @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.BID_NO, 	                                                                                   \n");
        	sqlstr.append("        A.BID_DEG, 	                                                                                   \n"); 
        	sqlstr.append("        B1.ITEM_NAME || '/' || B2.ITEM_NAME || '/' || B3.ITEM_NAME ITEM_NAME,                           \n");
        	sqlstr.append("        C.VENDOR_NAME,                                                                                  \n");
        	sqlstr.append("        A.DEPT_NM,                                                                                      \n");
        	sqlstr.append("        A.BID_MASTER_NM,                                                                                \n");
        	sqlstr.append("        ESTI_TOT_AMT AMT,                                                                               \n");
        	sqlstr.append("        DECODE(SUCC_YN, 'Y', '낙찰', '') SUCC_YN,                                                        \n");
        	sqlstr.append("        C.RNK || '/' || D.CNT as rnk ,        \n");
        	sqlstr.append("        DECODE(NVL(A.ACT_AMT,0), 0, 0, ROUND((C.ESTI_TOT_AMT/A.ACT_AMT)*100,2)) ACT_RATE      ,         \n");//실행대비
        	sqlstr.append("        DECODE(NVL(A.PUR_AMT,0), 0, 0, ROUND((C.ESTI_TOT_AMT/A.PUR_AMT)*100,2)) PUR_RATE                \n");//외주예산대비
        	sqlstr.append("   FROM BID_MASTER A,                                                                                   \n");
        	sqlstr.append("        OUT_ITEM B3,                                                                                    \n");
        	sqlstr.append("        OUT_ITEM B2,                                                                                    \n");
        	sqlstr.append("        OUT_ITEM B1,                                                                                    \n");
            sqlstr.append("        (SELECT A.*, RANK() OVER(PARTITION BY A.BID_NO, A.BID_DEG order by A.ESTI_TOT_AMT ASC) RNK FROM BID_SUPP A )  C,    \n");
            sqlstr.append("        (SELECT A.BID_NO, A.BID_DEG, COUNT(A.BID_NO) CNT                                                \n");
            sqlstr.append("           FROM BID_SUPP A                                                                              \n");
            sqlstr.append("          GROUP BY A.BID_NO, A.BID_DEG) D                                                               \n");
            sqlstr.append("   WHERE A.OUT_ITEM_CD = B3.ITEM_CD                                                                     \n"); 
            sqlstr.append("     AND B2.ITEM_CD = SUBSTR(B3.ITEM_CD,1,4)                                                            \n");
            sqlstr.append("     AND B1.ITEM_CD = SUBSTR(B3.ITEM_CD,1,2)                                                            \n");
            sqlstr.append("     AND B1.DEPTH = 1 AND B2.DEPTH = 2 AND B3.DEPTH = 3                                                 \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.BID_NO = D.BID_NO                                                                            \n");
            sqlstr.append("     AND A.BID_DEG = D.BID_DEG                                                                          \n");
            sqlstr.append("     AND A.STATUS_CD = '10'                                                                             \n");
            sqlstr.append("     AND A.BID_FROM_DT >= TO_DATE('" + params.get("signDt_from") + "')                                  \n");
            sqlstr.append("     AND A.BID_FROM_DT <= TO_DATE('" + params.get("signDt_to") + "')                                    \n");
            sqlstr.append("     AND NVL(A.PAY_DIV, '1') like '" + params.get("pay_div") + "%'                                                    \n");
            if(!"".equals(params.get("dept_nm"))) 
        		sqlstr.append(" AND A.DEPT_NM LIKE '%" +  params.get("dept_nm") +  "%'	                                           \n");
         	
        	if(!param.get("searchCorp").isEmpty())
        		sqlstr.append("     AND UPPER(C.VENDOR_NAME) LIKE '%"+param.get("searchCorp").toUpperCase()+"%'                                         \n");
            	
            if(!param.get("outItemTag1").isEmpty()){
            	if(!param.get("outItemTag2").isEmpty()){
            		if(!param.get("outItemTag3").isEmpty())
            			sqlstr.append("     AND A.OUT_ITEM_CD = '"+param.get("outItemTag3")+"'                                      \n");
            		else
            			sqlstr.append("     AND A.OUT_ITEM_CD LIKE '"+param.get("outItemTag2")+"%'                                  \n");
            	}
            	else
            		sqlstr.append("     AND A.OUT_ITEM_CD LIKE '"+param.get("outItemTag1")+"%'                                      \n");
            }
        	sqlstr.append("   ORDER BY  B1.ITEM_NAME,B2.ITEM_NAME,B3.ITEM_NAME,C.VENDOR_NAME	                                    \n");
            search.setStatement(sqlstr.toString());
            WAFLogger.debug(sqlstr.toString());
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            super.doException(se);
            WAFLogger.debug(sqlstr.toString());
            WAFLogger.debug(se.getMessage());
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {    
            if(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        return rowSet;
	}	
	
	public RowSet getOutItemRowSet(String depth) {
		Search search = new SearchImpl();
		RowSet sysRowSet = null;
		String sqlstr = "";
		try {
			sqlstr = " SELECT  ITEM_CD, ITEM_NAME		\n"
					+ " FROM   OUT_ITEM            	    \n"
					+ " WHERE DEPTH = ?	                \n"
					+ " ORDER BY ITEM_CD ASC            \n";
			
			search.setStatement(sqlstr);
			WAFLogger.debug(sqlstr.toString());
			search.addParameter(depth);
			sysRowSet = search.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> getOutItemRowSet() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return null;
		}
		finally {
			if( sysRowSet == null ) sysRowSet = new RowSetImpl();
			return sysRowSet;
		}
	}
	
	public ValueObject getOutItemCd(){
		ValueObject vo 			  = new ValueObject();
    	String temp 			  = params.get("item_depth");
    	String item_cd 			  = params.get("item_cd");
    	InitCombo initCombo 	  = new InitCombo();
    	RowSet itemDepth = null;
    	try {
    		itemDepth = getOutItemRowSet2(temp, item_cd);
    		
    		initCombo.makeComboData(itemDepth, "item_name", "item_cd");
    		
			vo.set("item_cd", initCombo.getCode());
			vo.set("item_name", initCombo.getCodeName());
    	} catch(Exception e) {
    		
    	} finally {
			return vo;
		}
    }
	public RowSet getOutItemRowSet2(String depth, String item_cd) {
		Search search = new SearchImpl();
		RowSet sysRowSet = null;
		String sqlstr = "";
		try {
			sqlstr = " SELECT  ITEM_CD, ITEM_NAME		\n"
					+ " FROM   OUT_ITEM             	\n"
					+ " WHERE DEPTH = ?	                \n"
					+ " AND ITEM_CD LIKE ? || '%'	    \n"
					+ " ORDER BY ITEM_CD ASC            \n";
			
			search.setStatement(sqlstr);
			search.addParameter(depth);
			search.addParameter(item_cd);
			sysRowSet = search.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> getOutItemRowSet2() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return null;
		}
		finally {
			if( sysRowSet == null ) sysRowSet = new RowSetImpl();
			return sysRowSet;
		}
	}
}