package kr.co.udapsoft.ebid.buyer.common.lib.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.util.ValueObject;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

import kr.co.hsnc.common.base.WAFLogger;

public class SearchUserPopBiz extends Biz {

	public SearchUserPopBiz(HandlerStorage storage) {
		super(storage);
	}
 
    public void doBiz() throws Exception {
        storage.setParams(params);
		params.set("sheet1", getAccCode(params));
		storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
    }

    /**
     * 현장코드 검색을 처리한다.(전체)
     * @param params
     * @return <code>RowSet</code>
     */
    private RowSet getAccCode(ValueObject Params) {

        Search search = new SearchImpl();

        RowSet rowSet = new RowSetImpl();
        StringBuffer sql = new StringBuffer();
        
        try {
        	sql.append("	SELECT                                      \n");
        	 sql.append("	        A.ACCT_ID                           \n");
        	 sql.append("	        , A.USER_SABUN                      \n");
        	 sql.append("	        , A.TEL_NO                          \n");
        	 sql.append("	        , A.HAND_TEL_NO                     \n");
        	 sql.append("	        , A.E_MAIL                          \n");
        	 sql.append("	        , A.RIGHT_GB                        \n");
        	 sql.append("	        , A.JOB_DSCR                        \n");
        	 sql.append("	        , A.ACCT_TYPE_ID                    \n");
        	 sql.append("	        , A.INACTIVE_DATE                   \n");
        	 sql.append("	        , A.USER_NAME                       \n");
        	 sql.append("	        , A.POSITION_CODE                   \n");
        	 sql.append("	        , A.POSITION_NAME                   \n");
        	 sql.append("	        , A.BUSEO                           \n");
        	 sql.append("	        , NVL(B.DEPT_NM,A.DEPART_DEPT_NM) AS BUSEO_NAME           \n");
        	 sql.append("	        , A.DSO1                            \n");
        	 sql.append("	        , A.DSO2                            \n");
        	 sql.append("	        , A.PSOSO                           \n");
        	 sql.append("	        , A.DUTYC                           \n");
        	 sql.append("	        , A.GRADE_CODE                      \n");
        	 sql.append("	        , A.GRADE_NAME                      \n");
        	 sql.append("	    FROM PUB0120T A, ETEC_ERPIF.ACZ10101 B       \n");
        	 sql.append("	    WHERE A.DEPART_DEPT_CD = B.ACC_DEPT_CD(+)        \n");
        	 sql.append("	      AND A.DUTYC != 99                      \n");  //2018.10.31 퇴사자 제외  
        	 
        	 
        	 if( !params.get("S_NAME").equals("") ) {
        		 sql.append("	    AND A.USER_NAME LIKE '%' || ? || '%'    \n");
 				search.addParameter(params.get("S_NAME"));
 			}
 			if( !params.get("S_DEPT_NAME").equals("") ) {
 				sql.append("	    AND B.DEPT_NM LIKE '%' || ? || '%'      \n");
 				search.addParameter(params.get("S_DEPT_NAME"));
 			}
 			
        	 sql.append("	 ORDER BY A.ACCT_TYPE_ID, A.USER_NAME       \n");
			
			WAFLogger.debug(sql.toString());
			search.setStatement(sql.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;
        }
    }
    
    
}
