/**
 * @(#)file    PreRequestSignBiz.java
 * @(#)author  OK
 * @(#)version 1.0
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.cont.purchase.biz;

import oracle.toplink.internal.ox.StrBuffer;
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 kr.co.udapsoft.ebid.buyer.common.file.FileManager;

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

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

	public void doBiz() throws Exception {
    	//WAFLogger.debug("### ContSignBiz_구매");
    	//WAFLogger.debug("### storage : " + storage.getUser());
    	//WAFLogger.debug("### params : " + params);
		
		ValueObject vo = new ValueObject();

		
		vo = getContInfo(params.get("sign_cont_mod_id"), params.get("acct_id"));
    	//WAFLogger.debug("### params : " + vo.get("DEALER_NM"));
    	
		//String doctitle = "-"+vo.get("CST_DOC_NAME")+ "_" +vo.get("DEPT_NM");
		//2015.8.10 조재윤과장 요청. 타이틀 순서변경
		String doctitle = "["+vo.get("DEPT_NM")+"]" + vo.get("CST_DOC_NAME");
        vo.set("doctitle", doctitle);
    	
		storage.addValueObject("ContInfo", vo);

		//자동 저장 파일
		RowSet rowSet = getTemplateFile(params.get("sign_cont_mod_id"));
		storage.addRowSet("AUTOFILE_LIST", rowSet);            
	}
	
	public RowSet getTemplateFile(String pur_cont_cd){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr += " SELECT FILE_ID, UNIQUE_FILE_NAME, FILE_PATH, FILE_EXT,          \n"   
            		+ "		TRIM(          \n"              
            		+ "        CASE                         \n"
				    + "       WHEN FILE_SIZE < 1024 THEN TO_CHAR(FILE_SIZE, '999,999') || ' Byte'          \n"
				    + "       ELSE          \n"
				    + "            CASE                WHEN FILE_SIZE < (1024*1024) THEN TO_CHAR(FLOOR(FILE_SIZE / 1024), '999,999') || ' KByte'          \n"
				    + "             ELSE  TO_CHAR(FLOOR(FILE_SIZE / (1024*1024)), '999,999,999') || ' MByte'          \n"
				    + "            END                           \n"                              
				    + "      END ) AS FILE_SIZE, FILE_NAME         \n"
                    + " FROM CONT_PUR_FILE      					\n"
                    + " WHERE PUR_CONT_MOD_ID =  '"+pur_cont_cd+"'		\n"
		            + " ORDER BY FILE_ID ASC      				\n";
             
            search.setStatement(sqlstr);
            listRowSet = search.execute();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{

			if( listRowSet == null ){
            	listRowSet = new RowSetImpl();
            }
            
			return listRowSet;
		}
	}
	public ValueObject getContInfo(String sign_cont_mod_id, String acct_id) {
		Search search = new SearchImpl();
		RowSet listRowSet = null;
		StrBuffer sqlstr = new StrBuffer();
		ValueObject rowObject = null;

		try {
			
			
			sqlstr.append("					SELECT A.DEALER_NM AS M_MEMBER_NAME                                                                       \n");
			sqlstr.append("					     , F_GET_USER_NM('"+acct_id+"') AS USER_NAME                                                            \n");
			sqlstr.append("					     , A.ORD_DOC_NO AS ORD_DOC_NO                                                                     		\n");
			sqlstr.append("					     , A.DEPT_CD  AS DEPT_CD                                                                          		\n");
		    sqlstr.append("					     , GET_DEPT_NM(A.DEPT_CD) AS DEPT_NM                                                              		\n");
			sqlstr.append("					     , A.CST_DOC_NAME AS CST_DOC_NAME                                                                 		\n");
			sqlstr.append("					     , C.VENDCD                AS S_VENDCD                                                            		\n");
			sqlstr.append("					     , C.MEMBER_NAME    AS S_MEMBER_NAME                                                              		\n");
			sqlstr.append("					     , TO_CHAR(A.ORD_CON_DATE , 'YYYY-MM-DD') AS ORD_CON_DATE                                         		\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("					     , F_GET_D_CURRENCY(A.ORD_SURTAX_AMT, F_GET_CURRENCY_DIGIT(A.CURRENCY_GB))   AS ORD_SURTAX_AMT          \n");
			sqlstr.append("					     , F_GET_D_CURRENCY(A.ORD_AMT, F_GET_CURRENCY_DIGIT(A.CURRENCY_GB))                  AS ORD_AMT         \n");
			sqlstr.append("					     , A.PUR_CONT_ID           AS PUR_CONT_ID                                                         		\n");
			sqlstr.append("					     , A.PUR_CONT_MOD_ID           AS PUR_CONT_MOD_ID                                                       \n");
			sqlstr.append("					     , DECODE(A.MOD_NO, 0, D.DOC_ID, E.DOC_ID) DOC_ID                                             		    \n");
			sqlstr.append("					     , DECODE(A.MOD_NO, 0, D.YEAR, E.YEAR)  YEAR                        		                            \n");
			sqlstr.append("					FROM PUR2000T A                                                                                             \n");
			sqlstr.append("					    , CONT_PUR_SUPP B                                                                                  		\n");
			sqlstr.append("					    , CONT_PUR_SUPP C                                                                                  		\n");
			sqlstr.append("					    , (SELECT KEY_COL1 AS BID_NO, KEY_COL2 AS BID_DEG, KEY_COL3 AS CORP_ID, DOC_ID , TO_CHAR(CRE_DATE,'YYYYMMDD') YEAR                          		\n");
			sqlstr.append("					             ,RANK() OVER (PARTITION BY KEY_COL1, KEY_COL2,KEY_COL3 ORDER BY DOC_ID DESC)  RNK                                                                            		\n");
			sqlstr.append("					       FROM SIGN001T                                                                            		\n");
			sqlstr.append("					       WHERE SIGN_CLASS_CD = 'ebid_ppoD'                                                                   		\n");
			sqlstr.append("					       AND SIGN_DECI_CLASS = '14'    ) D                                                                  		\n");
			sqlstr.append("					    , (SELECT MAX(KEY_COL1) PUR_CONT_MOD_ID , MAX(DOC_ID) AS DOC_ID , MAX(TO_CHAR(CRE_DATE,'YYYYMMDD')) YEAR		\n");
			sqlstr.append("					       FROM SIGN001T                                                                            		\n");
			sqlstr.append("					       WHERE SIGN_CLASS_CD = 'ebid_pcpoD'                                                                   		\n");
			sqlstr.append("					       AND SIGN_DECI_CLASS = '14'                                                                     		\n");
			sqlstr.append("					       AND KEY_COL1 = '"+sign_cont_mod_id+"'   ) E                                                     		\n");
			sqlstr.append("					WHERE  A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID                                                                \n");
			sqlstr.append("					AND    A.PUR_CONT_MOD_ID = C.PUR_CONT_MOD_ID                                                              	\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.CORP_ID = D.CORP_ID(+)                                                                           	\n");
			sqlstr.append("					AND    D.RNK(+) = 1                                                                          	\n");
			sqlstr.append("					AND    A.PUR_CONT_MOD_ID =   E.PUR_CONT_MOD_ID(+)       													\n");
			sqlstr.append("					AND    B.SIGN_SEQ = '1'                                                                                   	\n");
			sqlstr.append("					AND    C.SIGN_SEQ = '2'                                                                                   	\n");
			sqlstr.append("					AND    A.PUR_CONT_MOD_ID =   '"+sign_cont_mod_id+"'       													\n");
			
			
			search.setStatement(sqlstr.toString());
			listRowSet = search.execute();
			System.out.println(sqlstr.toString());
			//WAFLogger.debug("size : " + listRowSet.size());
			rowObject = RowSetUtility.getValueObject(listRowSet.getRow(0));
			
		} catch (Exception e) {
			WAFLogger.error(this.getClass().getName()
					+ " --> getBidMaster() : " + e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return null;
		} finally {
			if (rowObject == null)
				rowObject = new ValueObject();
			return rowObject;
		}
	}

	public RowSet getSuppInfo(String sign_cont_mod_id) {
		Search search = new SearchImpl();
		RowSet listRowSet = null;
		StrBuffer sqlstr = new StrBuffer();
		try {
			sqlstr.append("SELECT A.VENDOR_NAME		\n");
			sqlstr.append(",A.RECM_DEPT_NM		\n");
			sqlstr.append(",B.CREDIT_GRADE		\n");
			sqlstr.append(",(SELECT CST_DOC_NAME FROM PUR1000T WHERE PUR_CONT_ID = C.PUR_CONT_ID) AS CST_DOC_NAME		\n");
			sqlstr.append("FROM BID_SUPP A		\n");
			sqlstr.append(", SUPP_INFO B		\n");
			sqlstr.append(", (SELECT MAX(PUR_CONT_ID) AS PUR_CONT_ID,CORP_ID FROM PUR1000T GROUP BY CORP_ID) C		\n");
			sqlstr.append("WHERE A.VENDOR_CD = B.CORP_ID(+)		\n");
			sqlstr.append("AND A.VENDOR_CD = C.CORP_ID(+)		\n");
			sqlstr.append("AND BID_NO = '" + sign_cont_mod_id + "'			\n");
			search.setStatement(sqlstr.toString());
			//WAFLogger.debug(sqlstr);
			listRowSet = search.execute();
		} catch (Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> getSuppInfo() : "
					+ e.getMessage());
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return null;
		} finally {
			if (listRowSet == null)
				listRowSet = null;
			return listRowSet;
		}
	}
}
