/**
 * @(#)file    UserCommon.java
 * @(#)author  DaeJin Lee
 * @(#)version 1.0
 * @(#)date    2003-09-30
 * @(#)since   JDK1.3.1
 *
 * Copyright (c) 2002-2003 www.hwenc.com, Inc.
 * All rights reserved.
 * This software is the proprietary information of hwenc, Inc.
 */
package kr.co.udapsoft.ebid.buyer.test.biz;

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.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;


/**
 * 
 * 
 *
 */
public class TestCommon {

	/**
	 * 
	 */
	public TestCommon() {

		super();
	}


    /**
     * 계정타입 목록을 리턴한다.
	 * @return
	 */
	public RowSet getAcctTypeRowSet() {

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        try {
            sqlstr += " SELECT   *             \n"
                    + " FROM     PUB0110T      \n"
                    + " ORDER BY ACCT_TYPE_ID  \n" ; 
            search.setStatement(sqlstr);
            listRowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getAcctTypeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
    }

    /**
     *@param user
     *@return
     *기   능 : 유저의 계정타입 USE를 리턴한다.
     *@author NESSIE
     *@version 2006. 5. 22.
     *@since JDK1.3
     *----------------------------------------------------------
     */
    public RowSet getAcctTypeUseRowSet(String ACCT_TYPE_CODE) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        
        try {
            sqlstr += " SELECT   *             \n"
                    + " FROM     PUB0110T      \n";
            
            if (!ACCT_TYPE_CODE.equals("ADMIN")) {
             sqlstr += " WHERE USE LIKE '"+ACCT_TYPE_CODE+",%'"
                    + " OR    USE LIKE '%,"+ACCT_TYPE_CODE+"'"
                    + " OR    USE LIKE '%,"+ACCT_TYPE_CODE+",%'"
                    + " OR    USE   =  '"+ACCT_TYPE_CODE+"' ";
            }
            
            
            sqlstr += " ORDER BY ACCT_TYPE_ID  \n" ; 
            search.setStatement(sqlstr);
            listRowSet = search.execute();
            
           
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getAcctTypeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
        
    }
    
    /**
     * 계정타입 목록을 리턴한다.
     * @return
     */
    public RowSet getRightRowSet() {

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        try {
            sqlstr += " SELECT   *             \n"
                    + " FROM     PUB0340T      \n"
                    + " ORDER BY RIGHT_ID      \n" ; 
            search.setStatement(sqlstr);
            listRowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getRightRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
    }
    
    
    public RowSet getRightDefaultRowSet(String ACCT_TYPE_CODE) {

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        String sqlstr = "";
        try {
            sqlstr += " SELECT   DISTINCT B.*             \n"
                    + " FROM     PUB0110T A,PUB0340T  B    \n"
                    + " WHERE    A.ACCT_TYPE_ID=B.ACCT_TYPE_ID \n"
                    + "  AND     a.acct_type_code in (                                                  \n "
                    + getAcctTypeUseStr(getAcctTypeUseRowSet(ACCT_TYPE_CODE))       
                    + "                                )                                                 \n";
            if (!ACCT_TYPE_CODE.equals("ADMIN")) {
                sqlstr += " OR  (B.USE LIKE '"+ACCT_TYPE_CODE+",%'"
                       + " OR    B.USE LIKE '%,"+ACCT_TYPE_CODE+"'"
                       + " OR    B.USE LIKE '%,"+ACCT_TYPE_CODE+",%'"
                       + " OR    B.USE   =  '"+ACCT_TYPE_CODE+"') ";
               }
            sqlstr += " ORDER BY B.RIGHT_ID      \n" ; 
            
        
            
            search.setStatement(sqlstr);
            listRowSet = search.execute();
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getRightRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( listRowSet == null ) listRowSet = new RowSetImpl();
            return listRowSet;
        }
    }
    
    public String getAcctTypeUseStr(RowSet acctTypeRowSet){
        StringBuffer acctTypeStr = new StringBuffer();
        if (acctTypeRowSet != null && acctTypeRowSet.size()>0){
            int loop_cnt = acctTypeRowSet.size();
            
            for (int i=0; i<loop_cnt;i++){
                if (i==0) acctTypeStr.append("'"+acctTypeRowSet.getRow(i).get("acct_type_code")+"'");
                else{
                    acctTypeStr.append(",'"+acctTypeRowSet.getRow(i).get("acct_type_code")+"'");
                }
            }
        }
        
        return acctTypeStr.toString();
    }

	//협력업체별 사업자명을 가져온다.
	public static String get_Corp_Korname(String sub_pur_gubun, String corp_id) {
        
        Search search = new SearchImpl();       
        ValueObject Detail = new ValueObject();
		StringBuffer sql = new StringBuffer();		
        		
		try {            
			
			String tablename = "SUB9000T";//외주

			if(sub_pur_gubun.equals("pur")) tablename = "PUR9000T";
			
			sql.append(" SELECT  CORP_REG_NO || '     ' || CORP_KOR as CORP_KOR \n");								
			sql.append(" FROM    " + tablename + "			        \n");			
			sql.append(" WHERE   corp_id  = '" + corp_id + "'       \n");											
			               						
            search.setStatement(sql.toString()); 
			RowSet rowSet = search.execute();
            
			if( rowSet != null && rowSet.size() > 0 ) {

                return rowSet.getRow(0).get("CORP_KOR");
            }
			else 
			{
				return "";						
			}
       }
        catch(Exception e) {
    
            WAFLogger.error("UserCommon --> get_Corp_Korname() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(search.getParameters());
            WAFLogger.error(e);
            return "";
        }
    }
    
    /**
     * 본사 사용자 계정정보얻기
     * @param cmpReqID
     * @return
     */
    public ValueObject getSpotCharegEmail(String ChargeAcctID) {

        Search search = new SearchImpl();
        ValueObject srDoc = null;
        String sqlstr = "";
        try {
            sqlstr += "  SELECT    A.*,                         \n"
                    + "     (                                   \n"
                    + "      SELECT  RES1_ID || '@hanwha.co.kr' \n"
                    + "      FROM    MASTER_LOGIN               \n"
                    + "      WHERE   LOGIN_ID = A.USER_SABUN    \n"
                    + "      ) EMAIL                            \n"
                    + "  FROM     PUB0120T A                    \n"
                    + "  WHERE   ACCT_ID =  ?                   \n" ; 

   
            search.setStatement(sqlstr);
            search.addParameter(ChargeAcctID);
            
            if(search.execute().size()>0)
                srDoc = RowSetUtility.getValueObject(search.execute().getRow(0));
    
            WAFLogger.debug("params->"+search.getParameters());
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getSRDoc() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(search.getParameters());
            WAFLogger.error(e);
        }
        finally {
            if( srDoc == null ) srDoc = new ValueObject();
            return srDoc;
        }
    }
}
