/**
 * @(#)file    TermsAgreeBiz.java
 * @(#)author  이성민
 * @(#)version 1.0
 *
 */
package kr.co.udapsoft.ebid.supplier.portal.own.biz;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

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.ebid.buyer.common.file.FileManager;

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 ChargeListBiz extends Biz {

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

	/* (non-Javadoc)
	 * @see kr.co.hwenc.system.Biz#doBiz()
	 */
	public void doBiz() throws Exception 
	{

		
		//System.out.println("ChargeListBiz event==============>" + storage);
		
		if(storage.compareEvent(""))
		{
		    
		}
		else if(storage.compareEvent("GET_CHARGE_LIST"))
        {
            params.set("sheet1", getSuppCarge(params));
            storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
            
        }
		else if(storage.compareEvent("DELETE_DATA"))
		{
		    boolean success = false;
		    
		    String[] status = super.gets("sheet1.STATUS");
            
            for (int i = 0; i < status.length; i++) 
            {
//                if (IBSheet7Utils.equalStatusInsert(status[i])) 
//                {
//                    success = saveItemData(IBSheet7Utils.getIBSheet7SqlParams("SHEET2", params, i));
//                }
//                
//                if (IBSheet7Utils.equalStatusUpdate(status[i])) 
//                {
//                    success = updateItemData(IBSheet7Utils.getIBSheet7SqlParams("SHEET2", params, i));
//                }
                
                if (IBSheet7Utils.equalStatusDelete(status[i])) 
                {
                    success = deleteChargeData(IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
                }
            }
            
            super.storage.setSuccess(success);
            storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
            
		}
	}
	
	
	
	private boolean deleteChargeData(ValueObject sqlParams) throws Exception {
        Persistent persistent = null;
        StringBuffer sql = new StringBuffer();
        
        
        try {
            
            conn.begin();
            persistent = new PersistentImpl(super.conn.getConnection());
            
            sql.append("    DELETE FROM  SUPP_CHARGE          \n ");
            sql.append("        WHERE CORP_ID = ?                          \n ");
            sql.append("        AND USER_ID = ?                          \n ");
            
            persistent.setStatement(sql.toString());
            persistent.addParameter(sqlParams.get("CORP_ID"));
            persistent.addParameter(sqlParams.get("USER_ID"));
            
            persistent.execute();
            persistent.clearParameters();
            
            return true;
        } catch(Exception e) {
//          e.printStackTrace();
            //System.out.println("deleteChargeData===>" + e);
            WAFLogger.error(this.getClass().getName() + " --> deleteChargeData() : " + e.getMessage());
            WAFLogger.error("SQL : " + persistent.toString());
            WAFLogger.error(e);
            conn.setRollback();
            throw e;
            
        }
        
        finally {
            super.conn.end();
        }
    }
	
	
	private RowSet getSuppCarge(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlStr = new StringBuffer();
        
        try {
            sqlStr.append("            SELECT     \n")
                   .append("                      CORP_ID        \n")
                   .append("                    , USER_ID        \n")
                   .append("                    , PASSWD     \n")
                   .append("                    , USER_NAME        \n")
                   .append("                    , DIVISION        \n")
                   .append("                    , POSITION     \n")
                   .append("                    , TEL_NUM     \n")
                   .append("                    , FAX_NUM     \n")
                   .append("                    , HP1 || '-' || HP2 || '-' || HP3 AS H_PHON     \n")
                   .append("                    , EMAIL     \n")
                   .append("                    , USE_YN     \n")
                   .append("                    , DECODE(DEFAULT_YN, 'Y', '관리자', '사용자') DEFAULT_YN     \n")
                   .append("                    , DEFAULT_YN AS DEFAULT_YN_CODE     \n")
                   .append("                     FROM SUPP_CHARGE    \n")
                   .append("                    WHERE CORP_ID = ?      \n");
            if(!params.get("s_user_id").trim().equals(""))
            {
                sqlStr.append(" AND USER_ID = ?                     \n");
            }
            if(!params.get("s_user_name").trim().equals(""))
            {
                sqlStr.append(" AND USER_NAME LIKE  '%' || ? || '%'                    \n");
            }
                                               
            
            
            search.setStatement(sqlStr.toString());
            search.addParameter(params.get("p_corp_id"));
            
            if(!params.get("s_user_id").trim().equals(""))
            {
                search.addParameter(params.get("s_user_id"));
            }
            if(!params.get("s_user_name").trim().equals(""))
            {
                search.addParameter(params.get("s_user_name"));
            }
            
            //System.out.println(sqlStr.toString());
            //System.out.println(search.getParameters());
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            //System.out.println("getSuppCarge=========>" + se);
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            se.printStackTrace();
        }
        finally {    
            if(rowSet == null) rowSet = new RowSetImpl();
            return rowSet;
        }
    }
	
	
}