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;


public class SearchAccDeptBiz extends Biz {

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

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

        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sql = new StringBuffer();

        try {		
			sql.append(" SELECT ACC_DEPT_CD DEPT_HR_CD, DEPT_NM								 \n");
			sql.append(" FROM ETEC_ERPIF.ACZ10101	                                         \n");
			sql.append(" WHERE   1=1        									             \n");
			
			//부서코드
			if( !params.get("deptCd").equals("") ){
				sql.append(" AND      ACC_DEPT_CD = '" + params.get("deptCd") + "' \n");
			}

			//부서명
			if( !params.get("deptNm").equals("") ){
				sql.append(" AND      UPPER(DEPT_NM) LIKE UPPER('%" + params.get("deptNm") + "%') \n");
			}
			
			sql.append(" ORDER BY ACC_DEPT_CD            								\n");

        	 /*2019.04.25
        	sql.append("                SELECT A.DEPT_CD              											\n");
        	sql.append("                     , A.DEPT_HR_CD     												\n");
        	sql.append("                     , SUBSTR(A.DEPT_NM, INSTR(A.DEPT_NM, ' ', -1, 1) + 1) AS DEPT_NM    			\n");
        	sql.append("               FROM ZEHR_COM_DEPT@HRIF A      											\n");
        	sql.append("               WHERE A.DEPT_CD NOT IN ('D1',  'D379' ,'D3181' ,'D3214' ,'D2')			\n");
        	sql.append("               AND SUBSTR(A.FIELD_CLASS,1,1) = '1'										\n");
        	               
        	if(!params.get("deadflg").equals("")){
        		sql.append("   AND NVL(A.USE_YN,'Y')      = '"+params.get("deadflg")+"' \n");
        	}
        	
        	if(!params.get("deptNm").equals("")){
        		sql.append("   AND  upper(A.DEPT_NM)         LIKE '%" + params.get("deptNm").toUpperCase()+"%' \n");
        	}
        	
        	sql.append(" ORDER BY A.DEPT_HR_CD, A.DEPT_NM \n");*/
        	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;
        }
    }
}
