/**
 * @(#)file    AuthBatchListBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.6.10
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.sycs.security.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 kr.co.udapsoft.ebid.buyer.sycs.user.biz.UserCommon;

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

public class AuthBatchListBiz extends Biz {

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

	public void doBiz() throws Exception {
		UserCommon common = new UserCommon();
		
		if(storage.compareEvent("")){
	        RowSet acctTypeRowSet = common.getAcctTypeUseRowSet(storage.getUser().get("ACCT_TYPE_CODE"));
	        RowSet userRightRowSet = getUserRightRowSet();
	        storage.addRowSet("AcctTypeRowSet", acctTypeRowSet);
	        storage.addRowSet("userRight_rs", userRightRowSet);
		}
		else {
			params.set("sheet1", getUserList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}

	public RowSet getUserRightRowSet() {
	    Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlstr = null;
    
    try {
        	sqlstr = new StringBuffer();
        	
        	sqlstr.append(" SELECT  RIGHT_ID, DSCR AS RIGHT_NAME										\n");
        	sqlstr.append(" FROM    PUB0340T															\n");
        	if( !storage.getUser().get("ACCT_TYPE_CODE").equals("ADMIN") ){
        		sqlstr.append(" WHERE ACCT_TYPE_ID = '" + storage.getUser().get("ACCT_TYPE_ID") + "'	\n");
        	}
        	sqlstr.append(" ORDER BY DSCR																\n");
        	
        	search.setStatement(sqlstr.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;
	}
	
	private RowSet getUserList(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        String sqlstr = "";
        try {
        	sqlstr += " SELECT	A.USER_SABUN                                                                                   \n"
        			+ "         ,A.USER_PW                                                                                     \n"
        			+ "         ,NVL(B.USER_NAME_K, USER_NAME) AS USER_NAME                                                      \n"
        			+ "         ,(SELECT C.DEPT_NM FROM ETEC_ERPIF.ACZ10101 C WHERE C.ACC_DEPT_CD = A.BUSEO) AS DEPT_NM	-- 팀		   \n"
        			+ "         ,(SELECT A.DEPT_NM  FROM ACZ10100V A  WHERE A.DEPT_CD = A.PSOSO) AS  BUSEO_NM	-- 현장		   \n"
        			+ "         ,NVL(B.GRADE_NAME,A.GRADE_NAME) AS POSITION_NAME                                              \n"
        			+ "         ,A.TEL_NO                                                                                      \n"
        			+ "         ,A.HAND_TEL_NO                                                                                 \n"
        			+ "         ,A.E_MAIL                                                                                      \n"
        			+ "         ,TO_CHAR(TO_DATE(A.CRE_DATE,'YYYYMMDDHH24MI'),'YYYY-MM-DD') AS CRE_DATE						   \n"
        			+ "         ,A.ACCT_ID                                                                                     \n"
        			+ " FROM	PUB0120T A                                                                                     \n"
        			+ " 		,PUB0130V B                                                                                     \n"
        			+ " WHERE	A.USER_SABUN = B.USER_SABUN(+)                                                                  \n"
        			+ " AND 	NVL(B.DUTYC,A.DUTYC) <> '99'                                                               \n";
 
           	if( !params.get("user_sabun").equals("") ) {
           		sqlstr += "    AND  A.USER_SABUN LIKE '%'||UPPER('" + params.get("user_sabun") + "')||'%' \n";
          	}

          	if( !params.get("user_name").equals("") ) {
          		sqlstr += "    AND  NVL(B.USER_NAME_K, USER_NAME) LIKE '%" + params.get("user_name") + "%' \n";
          	}
          	
          	if( !params.get("dept_cd").equals("") ) {
          		sqlstr += "    AND  NVL(B.BUSEO, A.BUSEO) = '" + params.get("dept_cd") + "' \n";
          	}
          	if( !params.get("acct_type_id").equals("") ) {
          		sqlstr += " AND      A.ACCT_TYPE_ID = '" + params.get("acct_type_id") + "'  \n" ;
          	}
             
            sqlstr += " ORDER BY A.USER_SABUN DESC 							\n" ; 
            	
        	search.setStatement(sqlstr);
            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;
        }
	}
	
}