/**
 * @(#)file    BidPlanBiz.java
 * @(#)author  Son Hyung Min 
 * @(#)version 1.0
 * @(#)date    2014-06-23
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.supplier.portal.quot.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.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class QuotResultBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
        ValueObject vo_file_out = new ValueObject();
        ValueObject att_file = new ValueObject();
		
		if(storage.compareEvent("SHEET_SELECT")){	//1. 견적 현황 리스트 조회
			params.set("sheet1", getBoardList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}else if(storage.compareEvent("GET_QUOT_DETAIL")){//2. 견적 현황 상세 조회
			ValueObject vo = getOrderReqeustValueObject(params);
	        storage.addValueObject("Order", vo);
	         
	        /**		파일 목록 조회			 **/
	        RowSet rowSet = FileManager.getFileList("QUOT_FILE_OUT_" + params.get("quot_id"), "");
	        RowSet rowSet2 = FileManager.getFileList("QUOT_SUPP_" + params.get("VENDOR_CD") + params.get("quot_id"), "");
	        String str="";
	        String str2="";
		       
		    for(int i=0;i<rowSet.size();i++){
			   	Row row=rowSet.getRow(i);
			   	str += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
			   	if(i+1!=rowSet.size()){
			   		str +="\r\n";
			   	}
		    }
		    
		    for(int i=0;i<rowSet2.size();i++){
			   	Row row=rowSet2.getRow(i);
			   	str2 += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
			   	if(i+1!=rowSet2.size()){
			   		str2 +="\r\n";
			   	}
		    }
	        vo_file_out.set("DOC_ETC_OUT", str);
	        att_file.set("ATT_ID", str2);
	        
		}else if(storage.compareEvent("SHEET_RESULT_SELECT")){	//3. 견적 결과 리스트 조회
			params.set("sheet1", getBoardResultList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
		storage.addValueObject("DOC_ETC_OUT", vo_file_out);
		storage.addValueObject("ATT_ID", att_file);
	}
	
	private ValueObject getfilelist(String filegb,String id,String returnid){
		ValueObject vo = new ValueObject();
		RowSet rowSet = FileManager.getFileList(filegb, id);
		String str="";
	        for(int i=0;i<rowSet.size();i++){
	        	Row row=rowSet.getRow(i);
	        	str += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
	        	if(i+1!=rowSet.size()){
	        		str +="\r\n";
	        	}
	        }
	        vo.set(returnid, str);
		return vo;
	}

	/**
	 * 견적현황 리스트를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private RowSet getBoardList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append("SELECT A.QUOT_BID_NO || '-' || A.QUOT_BID_DEG AS BID_ID,                 \n");
        	sqlstr.append("       A.QUOT_BID_NO,                                                    \n");
        	sqlstr.append("       A.QUOT_BID_DEG,                                                   \n");
        	sqlstr.append("       A.QUOT_BID_MASTER_NM,                                             \n");
        	sqlstr.append("       A.QUOT_ID,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.REQUEST_DATE, 'YYYY-MM-DD') AS REQUEST_DATE,           \n");
        	sqlstr.append("       A.QUOT_TYPE,                                                      \n");
        	sqlstr.append("       A.DEPT_CD,                                                        \n");
        	sqlstr.append("       A.DEPT_NM,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.START_WORK_DATE, 'YYYY-MM-DD') AS START_WORK_DATE,     \n");
        	sqlstr.append("       TO_CHAR (A.END_WORK_DATE, 'YYYY-MM-DD') AS END_WORK_DATE,         \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_FROM_DT, 'YYYY-MM-DD') AS QUOT_BID_FROM_DT,   \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_TO_DT, 'YYYY-MM-DD') AS QUOT_BID_TO_DT,       \n");
        	sqlstr.append("       QUOT_MAN_ID,                                                      \n");
        	sqlstr.append("       QUOT_MAN_NM,                                                      \n");
        	sqlstr.append("       A.STATUS_CD,                                                      \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_STATUS_CD', STATUS_CD) AS STATUS_NM,       \n");
        	sqlstr.append("       B.VENDOR_CD,                                                      \n");
        	sqlstr.append("       B.VENDOR_NO,                                                      \n");
        	sqlstr.append("       VENDOR_NAME,                                                      \n");
        	sqlstr.append("       B.REPRE_NO,                                                       \n");
        	sqlstr.append("       B.CHARGE_NAME,                                                    \n");
        	sqlstr.append("       B.CHAGE_HP,                                                       \n");
        	sqlstr.append("       B.CHARGE_EMAIL,                                                   \n");
        	sqlstr.append("       NVL(B.BID_STATUS_CD,'00') AS BID_STATUS_CD,                       \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_SUPP_STATUS_CD', NVL(B.BID_STATUS_CD,'00')) AS SUPP_STATUS_NM,			\n");
        	sqlstr.append("       B.ATT_ID,                                                         \n");
        	sqlstr.append("       B.FILE_GB,                                                        \n");
        	sqlstr.append("       B.ESTI_TOT_AMT,                                                   \n");
        	sqlstr.append("       B.REMARK,                                                         \n");
        	sqlstr.append("       DECODE(B.SUBMIT_DT, '', '미제출', TO_CHAR (B.SUBMIT_DT, 'YYYY-MM-DD')) SUBMIT_DT,				\n");
        	sqlstr.append("       DECODE(A.STATUS_CD,'10','불가','가능') AS SUBMIT_YN                 \n");
        	sqlstr.append("  FROM QUOT_BID_MASTER A                                                 \n");
        	sqlstr.append("     , QUOT_BID_SUPP B                                                   \n");
        	sqlstr.append(" WHERE 1=1                                                               \n");
        	sqlstr.append("   AND A.QUOT_BID_NO = B.QUOT_BID_NO                                     \n");
        	sqlstr.append("   AND A.QUOT_BID_DEG = B.QUOT_BID_DEG                                   \n");
        	sqlstr.append("   AND A.STATUS_CD = '10'                                                \n");
        	sqlstr.append("   AND B.VENDOR_CD = '"+params.get("VENDOR_CD")+"'                       \n");
        	if(!param.get("P_QUOT_BID_MASTER_NM").isEmpty())
        	        sqlstr.append("   AND UPPER(A.QUOT_BID_MASTER_NM) LIKE '%'|| '"+param.get("P_QUOT_BID_MASTER_NM").toUpperCase()+"' ||'%'	\n"); 
        	if(!param.get("p_quot_bid_from_dt").isEmpty())
        	        sqlstr.append("   AND A.QUOT_BID_TO_DT >=  TO_DATE('"+param.get("p_quot_bid_from_dt")+"'||'000000','YYYY-MM-DDHH24MISS') 	\n");
        	if(!param.get("p_quot_bid_to_dt").isEmpty())
        	        sqlstr.append("   AND A.QUOT_BID_TO_DT <= TO_DATE('"+param.get("p_quot_bid_to_dt")+"'||'235959','YYYY-MM-DDHH24MISS') 		\n");
        	sqlstr.append("   ORDER BY QUOT_BID_TO_DT 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;
	}
	
	/**
	 * 견적 현황 상세를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private ValueObject getOrderReqeustValueObject(ValueObject params) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append("SELECT A.QUOT_BID_NO,                                                    \n");
        	sqlstr.append("       A.QUOT_BID_DEG,                                                   \n");
        	sqlstr.append("       A.QUOT_BID_MASTER_NM,                                             \n");
        	sqlstr.append("       A.QUOT_ID,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.REQUEST_DATE, 'YYYY-MM-DD') AS REQUEST_DATE,           \n");
        	sqlstr.append("       A.QUOT_TYPE,                                                      \n");
        	sqlstr.append("       A.DEPT_CD,                                                        \n");
        	sqlstr.append("       A.DEPT_NM,                                                        \n");
        	sqlstr.append("       A.REQUEST_MAN_ID,                                                 \n");
        	sqlstr.append("       A.REQUEST_MAN_NM,                                                 \n");
        	sqlstr.append("       A.REQUEST_DEPT_NM,                                                \n");
        	sqlstr.append("       A.REQUEST_DEPT_CD,                                                \n");
        	sqlstr.append("       TO_CHAR (A.START_WORK_DATE, 'YYYY-MM-DD') AS START_WORK_DATE,     \n");
        	sqlstr.append("       TO_CHAR (A.END_WORK_DATE, 'YYYY-MM-DD') AS END_WORK_DATE,         \n");
        	sqlstr.append("       A.WORK_SIZE,                                                      \n");
        	sqlstr.append("       A.DEALER_NM,                                                      \n");
        	sqlstr.append("       A.SUPERVISOR,                                                     \n");
        	sqlstr.append("       A.QUOT_MAN_ID,                                                    \n");
        	sqlstr.append("       A.QUOT_MAN_NM,                                                    \n");
        	sqlstr.append("       A.QUOT_MAN_TELNO,                                                 \n");
        	sqlstr.append("       A.QUOT_MAN_EMAIL,                                                 \n");
        	sqlstr.append("       A.QUOT_MAN_FAX,                                                   \n");
        	sqlstr.append("       TO_CHAR(A.QUOT_SITEDESCDATE,'YYYY-MM-DD HH24:MI') AS QUOT_SITEDESCDATE,  \n");
        	sqlstr.append("       A.QUOT_SITEDESCPLACE,                                             \n");
        	sqlstr.append("       A.QUOT_DEPT_CD,                                                   \n");
        	sqlstr.append("       A.QUOT_VALIDATE,                                                  \n");
        	sqlstr.append("       A.WORK_PLACE,                                                     \n");
        	sqlstr.append("       A.ITEM_NM,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_FROM_DT, 'YYYY-MM-DD HH24:MI') AS QUOT_BID_FROM_DT,   \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_TO_DT, 'YYYY-MM-DD HH24:MI') AS QUOT_BID_TO_DT,       \n");
        	sqlstr.append("       A.INNER_REMARKS,                                                  \n");
        	sqlstr.append("       A.OUTER_REMARKS,                                                  \n");
        	sqlstr.append("       A.DOC_ETC_IN,                                                     \n");
        	sqlstr.append("       A.DOC_ETC_OUT,                                                    \n");
        	sqlstr.append("       A.STATUS_CD,                                                      \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_STATUS_CD', STATUS_CD) AS STATUS_NM,       \n");
        	sqlstr.append("       B.VENDOR_CD,                                                      \n");
        	sqlstr.append("       B.VENDOR_NO,                                                      \n");
        	sqlstr.append("       VENDOR_NAME,                                                      \n");
        	sqlstr.append("       B.REPRE_NO,                                                       \n");
        	sqlstr.append("       B.CHARGE_NAME,                                                    \n");
        	sqlstr.append("       B.CHAGE_HP,                                                       \n");
        	sqlstr.append("       B.CHARGE_EMAIL,                                                   \n");
        	sqlstr.append("       NVL(B.BID_STATUS_CD,'00') AS BID_STATUS_CD,                       \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_SUPP_STATUS_CD', NVL(B.BID_STATUS_CD,'00')) AS SUPP_STATUS_NM,			\n");
        	sqlstr.append("       B.ATT_ID,                                                         \n");
        	sqlstr.append("       B.FILE_GB,                                                        \n");
        	sqlstr.append("       B.ESTI_TOT_AMT,                                                   \n");
        	sqlstr.append("       B.REMARK,                                                         \n");
        	sqlstr.append("       DECODE(B.SUBMIT_DT, '', '미제출', TO_CHAR (B.SUBMIT_DT, 'YYYY-MM-DD')) SUBMIT_DT,				\n");
        	sqlstr.append("       DECODE(A.STATUS_CD,'10','불가','가능') AS SUBMIT_YN,                \n");
        	sqlstr.append("       A.URL_COL,                                                        \n");
        	sqlstr.append("       B.SITE_ATTEND_YN,                                                 \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_SUPP_SITEPARTI', B.SITEPARTI_YN) AS SITEDESC_YN_NM,					\n");
        	sqlstr.append("       B.SITE_ATTEND_REASON                                              \n");
        	sqlstr.append("  FROM QUOT_BID_MASTER A                                                 \n");
        	sqlstr.append("     , QUOT_BID_SUPP B                                                   \n");
        	sqlstr.append(" WHERE 1=1                                                               \n");
        	sqlstr.append("   AND A.QUOT_ID = '"+params.get("QUOT_ID")+"'                           \n");
        	sqlstr.append("   AND A.QUOT_BID_NO = B.QUOT_BID_NO                                     \n");
        	sqlstr.append("   AND A.QUOT_BID_DEG = B.QUOT_BID_DEG                                   \n");
        	sqlstr.append("   AND B.VENDOR_CD = '"+params.get("VENDOR_CD")+"'                       \n");
        	
        	search.setStatement(sqlstr.toString());
        	
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ){
            	valueobject = RowSetUtility.getValueObject(rowSet.getRow(0));
            }
            	
        }
        catch(WAFSQLException se) {
        	super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {

        	if( valueobject == null ) valueobject = new ValueObject();
            return valueobject;
        }
	}
	
	/**
	 * 견적현황 리스트를 검색하여 리턴
	 * @param Params
	 * @return
	 */
	private RowSet getBoardResultList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
     //   String sqlstr = "";
        StrBuffer sqlstr = new StrBuffer();
        try{
        	sqlstr.append("SELECT A.QUOT_BID_NO || '-' || A.QUOT_BID_DEG AS BID_ID,                 \n");
        	sqlstr.append("       A.QUOT_BID_NO,                                                    \n");
        	sqlstr.append("       A.QUOT_BID_DEG,                                                   \n");
        	sqlstr.append("       A.QUOT_BID_MASTER_NM,                                             \n");
        	sqlstr.append("       A.QUOT_ID,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.REQUEST_DATE, 'YYYY-MM-DD') AS REQUEST_DATE,           \n");
        	sqlstr.append("       A.QUOT_TYPE,                                                      \n");
        	sqlstr.append("       A.DEPT_CD,                                                        \n");
        	sqlstr.append("       A.DEPT_NM,                                                        \n");
        	sqlstr.append("       TO_CHAR (A.START_WORK_DATE, 'YYYY-MM-DD') AS START_WORK_DATE,     \n");
        	sqlstr.append("       TO_CHAR (A.END_WORK_DATE, 'YYYY-MM-DD') AS END_WORK_DATE,         \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_FROM_DT, 'YYYY-MM-DD') AS QUOT_BID_FROM_DT,   \n");
        	sqlstr.append("       TO_CHAR (A.QUOT_BID_TO_DT, 'YYYY-MM-DD') AS QUOT_BID_TO_DT,       \n");
        	sqlstr.append("       QUOT_MAN_ID,                                                      \n");
        	sqlstr.append("       QUOT_MAN_NM,                                                      \n");
        	sqlstr.append("       A.STATUS_CD,                                                      \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_STATUS_CD', STATUS_CD) AS STATUS_NM,       \n");
        	sqlstr.append("       B.VENDOR_CD,                                                      \n");
        	sqlstr.append("       B.VENDOR_NO,                                                      \n");
        	sqlstr.append("       VENDOR_NAME,                                                      \n");
        	sqlstr.append("       B.REPRE_NO,                                                       \n");
        	sqlstr.append("       B.CHARGE_NAME,                                                    \n");
        	sqlstr.append("       B.CHAGE_HP,                                                       \n");
        	sqlstr.append("       B.CHARGE_EMAIL,                                                   \n");
        	sqlstr.append("       NVL(B.BID_STATUS_CD,'00') AS BID_STATUS_CD,                       \n");
        	sqlstr.append("       F_GET_CODE_NAME ('QUOT_SUPP_STATUS_CD', NVL(B.BID_STATUS_CD,'00')) AS SUPP_STATUS_NM,			\n");
        	sqlstr.append("       B.ATT_ID,                                                         \n");
        	sqlstr.append("       B.FILE_GB,                                                        \n");
        	sqlstr.append("       B.ESTI_TOT_AMT,                                                   \n");
        	sqlstr.append("       B.REMARK,                                                         \n");
        	sqlstr.append("       DECODE(B.SUBMIT_DT, '', '미제출', TO_CHAR (B.SUBMIT_DT, 'YYYY-MM-DD')) SUBMIT_DT,				\n");
        	sqlstr.append("       DECODE(A.STATUS_CD,'10','불가','가능') AS SUBMIT_YN                 \n");
        	sqlstr.append("  FROM QUOT_BID_MASTER A                                                 \n");
        	sqlstr.append("     , QUOT_BID_SUPP B                                                   \n");
        	sqlstr.append(" WHERE 1=1                                                               \n");
        	sqlstr.append("   AND A.QUOT_BID_NO = B.QUOT_BID_NO                                     \n");
        	sqlstr.append("   AND A.QUOT_BID_DEG = B.QUOT_BID_DEG                                   \n");
        	sqlstr.append("   AND A.STATUS_CD = '10'                                                \n");
        	sqlstr.append("   AND B.VENDOR_CD = '"+params.get("VENDOR_CD")+"'                       \n");
        	if(!param.get("P_QUOT_BID_MASTER_NM").isEmpty())
        	        sqlstr.append("   AND UPPER(A.QUOT_BID_MASTER_NM) LIKE '%'|| '"+param.get("P_QUOT_BID_MASTER_NM").toUpperCase()+"' ||'%'	\n"); 
        	if(!param.get("p_quot_bid_from_dt").isEmpty())
        	        sqlstr.append("   AND A.QUOT_BID_TO_DT >=  TO_DATE('"+param.get("p_quot_bid_from_dt")+"'||'000000','YYYY-MM-DDHH24MISS') 	\n");
        	if(!param.get("p_quot_bid_to_dt").isEmpty())
        	        sqlstr.append("   AND A.QUOT_BID_TO_DT <= TO_DATE('"+param.get("p_quot_bid_to_dt")+"'||'235959','YYYY-MM-DDHH24MISS') 		\n");
        	sqlstr.append("   ORDER BY QUOT_BID_TO_DT 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;
	}
}