/**
 * @(#)file    ErpFileListBiz.java
 * @(#)author  lee in oh
 * @(#)version 1.0
 * @(#)date    2018-09-06
 * @(#)since   JDK1.6
 *
 * Copyright (c)  www.sdcit.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.common.file.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.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;
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;

/**
 * ERP 공통 첨부파일 목록 조회
 */
public class ErpFileListBiz extends Biz {

	public ErpFileListBiz(HandlerStorage storage) {

		super(storage);
	}

	/** 
     * 첨부파일 목록을 리턴한다.
	 */
	public void doBiz() throws Exception {

        String fileGB = params.get("file_gb");
        String ID = params.get("id");
        String openType = params.get("openType");

        if(storage.compareEvent("")){
        	
        	ValueObject vo = getOpenType(fileGB, ID, openType);
        	String validYn = "N";
        	
        	try {
        		long openTime = Long.parseLong(vo.get("OPEN_TIME"));
        		long currTime = Long.parseLong(vo.get("CURR_TIME"));
        		
        		//ERP에서 팝업을 띄운시간을 체크하여 유효성체크 (10분) 2019.03.14 해제 
        		//if( currTime - openTime < 10 && currTime - openTime >= 0 ) {
        			validYn = "Y";
        		//}
        		
        	}catch (Exception e) {
        		//e.printStackTrace();
			}
        	
        	vo.set("validYn", validYn);
        	
        	storage.addValueObject("openParams", vo);
        	
        }else if(storage.compareEvent("GET_FILE_LIST")){
        	
        	RowSet fileListRowSet = FileManager.getFileList(fileGB, ID); 
        	params.set("sheet1", fileListRowSet);
        	storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
        }else if(storage.compareEvent("GET_FILE_LIST_TEXTAREA")){
        	if("".equals(ID) || ID == null){
        		ID = params.get("file_id");
        	}
        	ValueObject  vo = new ValueObject();
        	vo.set("file_txt_list", FileManager.getFileListForTextarea(fileGB, ID)); 
            storage.addValueObject("VALUEOBJECT", vo);
        }
		
	}
	
	private ValueObject getOpenType(String fileGB, String ID, String openType) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject vo = new ValueObject();
        StrBuffer sqlstr = new StrBuffer();
        
        try{
        	
        	sqlstr.append("SELECT  F_DECRYPT(?) AS FILE_GB							\n");
        	sqlstr.append("        ,F_DECRYPT(?) AS ID								\n");
        	sqlstr.append("        ,SUBSTR(F_DECRYPT(?),1,1) AS MODIFY_YN			\n");
        	sqlstr.append("        ,SUBSTR(F_DECRYPT(?),2) AS OPEN_TIME				\n");
        	sqlstr.append("        ,TO_CHAR(SYSDATE,'YYYYMMDDHH24MI') AS CURR_TIME	\n");
        	sqlstr.append("FROM    DUAL												\n");
        	
        	search.setStatement(sqlstr.toString());
        	search.addParameter(fileGB);
        	search.addParameter(ID);
        	search.addParameter(openType);
        	search.addParameter(openType);
            rowSet = search.execute();
            
            if(rowSet.size()>0){
            	vo = 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(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        return vo;
	}
	
}
