/**
 * @(#)file    UserFormBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.6.24
 * @(#)description : 사용자 등록
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.sycs.user.biz;

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.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 kr.co.hsnc.common.base.WAFLogger;

import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class UserFormBiz extends Biz {

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

	public void doBiz() throws Exception {
		
		UserCommon common = new UserCommon();

        String event = storage.getEvent();

        ValueObject user = new ValueObject();
        RowSet userRightRowSet = new RowSetImpl();
        RowSet rightRowSet = new RowSetImpl();

        RowSet acctTypeRowSet = common.getAcctTypeUseRowSet(storage.getUser().get("ACCT_TYPE_CODE"));
        ValueObject vo = new ValueObject();
        
        if( event.equals("GET_USERFORM_LIST") ) {
            String acctID = params.get("acct_id");
            user = getUserValueObject(acctID);
            userRightRowSet = getUserRightRowSet(acctID);
            rightRowSet = getRightRowSet(acctTypeRowSet,acctID,storage.getUser());
            
	        RowSet rowSet = FileManager.getFileList("USER_INFO", acctID);
	
	        String str="";
	        for(int i=0;i<rowSet.size();i++){
	        	Row row=rowSet.getRow(i);
	        	str += (i+1)+" : "+row.get("FILE_NAME")+" ("+ row.get("FILE_SIZE")+")";
	        	if(i+1!=rowSet.size()){
	        		str +="\r\n";
	        	}
	        }
	        
	        vo.set("USER_INFO_FILE", str);
        }
        else if( event.equals("UserForm_Reg") ) {

            rightRowSet = common.getRightDefaultRowSet(storage.getUser().get("ACCT_TYPE_CODE"));
        }
        
        storage.addValueObject("User", user);
        
        storage.addRowSet("AcctTypeRowSet", acctTypeRowSet);	// 계정타입
        
        storage.addRowSet("UserRightRowSet", userRightRowSet);	// 유저권한
        storage.addRowSet("RightRowSet", rightRowSet);			// 권한목록
        
        storage.addValueObject("USER_INFO_FILE", vo);
	}
	
	 /**
     * 사용자 권한목록을 리턴한다.
	 * @param sabun
	 * @param name
	 * @param deptCD
	 * @param acctTypeID
	 * @return
	 */
	public RowSet getUserRightRowSet(String acctID) {
        
		    Search search = new SearchImpl();
	        RowSet rowSet = new RowSetImpl();
	        String sqlstr = "";
	        try {
	        	sqlstr += " SELECT B.RIGHT_LIST_ID, A.*     \n"
                       + " FROM   PUB0340T A,              \n"
                       + "        PUB0350T B               \n"
                       + " WHERE  A.RIGHT_ID = B.RIGHT_ID  \n"
                       + " AND    B.ACCT_ID = '" + params.get("acct_id") + "'            \n" ; //여기까지
	        
	        	WAFLogger.debug(sqlstr.toString());
	        	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;
        }
    }

    /**
     * 계정타입 목록을 리턴한다.
     * @return
     */
    public RowSet getRightRowSet(RowSet acctTypeRowSet,String acctID,ValueObject user) {

    	Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        String sqlstr = "";
        try {
        	 String ACCT_TYPE_CODE = user.get("ACCT_TYPE_CODE");
             
             sqlstr += " SELECT   DISTINCT B.*             \n"
                 + " FROM     PUB0110T A,PUB0340T  B    \n"
                 + " WHERE    A.ACCT_TYPE_ID=B.ACCT_TYPE_ID \n"
                 + "  AND                                    \n";
             if (!ACCT_TYPE_CODE.equals("ADMIN")) {
                 sqlstr += "(";
             }
             sqlstr += " a.acct_type_code in (                                                  \n "
                 + getAcctTypeUseStr(acctTypeRowSet)       
                 + "                                )                                                 \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 += "  AND RIGHT_ID NOT IN( SELECT A.RIGHT_ID               \n"
                 + "                           FROM   PUB0340T A,              \n"
                 + "                                  PUB0350T B               \n"
                 + "                           WHERE  A.RIGHT_ID = B.RIGHT_ID  \n"
                 + "                           AND    B.ACCT_ID = '" + params.get("acct_id") + "'      )     \n"
                 + " ORDER BY B.RIGHT_ID      \n" ; 

          	WAFLogger.debug(sqlstr.toString());
        	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;
        }
    }
    
    public ValueObject getUserValueObject(String acctID) {
    	Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        ValueObject valueobject = null;
        String sqlstr = "";
        
        try {
        	sqlstr += " SELECT   A.*, C.USER_NAME_K as user_name,         								\n"
        		 + " 			 DECODE(A.PSOSO, null, (SELECT A.DEPT_NM  FROM ACZ10100V A  WHERE A.DEPT_CD = C.BUSEO),   							\n"
        		 + " 			 (SELECT A.DEPT_NM  FROM ACZ10100V A  WHERE A.DEPT_CD = A.PSOSO) )as Assigned_Dept,									    \n"
        		 + " 			 (SELECT A.DEPT_NM  FROM ETEC_ERPIF.ACZ10101 A  WHERE A.ACC_DEPT_CD = A.BUSEO) as Dept_NAME								    \n"
                 + " FROM     PUB0130V A, PUB0130V C   																						\n"
                 + " WHERE    A.ACCT_ID = '" + params.get("acct_id") + "' 																				\n"
        	     + " AND      A.user_sabun = C.USER_SABUN(+) 																							\n" 
                 + " AND      C.DUTYC(+) <> '30'      																								\n" ;

        	WAFLogger.debug(sqlstr.toString());
        	search.setStatement(sqlstr);
            rowSet = search.execute();
            
            if( rowSet != null && rowSet.size() > 0 ){
            	valueobject = RowSetUtility.getValueObject(rowSet.getRow(0));
            }
        }
        catch(WAFSQLException se) {
        	super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {
        	if( valueobject == null ) valueobject = new ValueObject();
            return valueobject;
        }
    }
    
    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();
    }
	
}