/**
 * @(#)file    GetMaterialKindCodeBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.23
 * @(#)Description: 자재선택 팝업
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.common.lib.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.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.waf.common.ui.HTMLMaker;
import com.udapsoft.waf.common.ui.InitCombo;

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


public class GetMaterialKindCodeBiz extends Biz {

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

    public void doBiz() throws Exception {
    	ValueObject vo = getMaterialKindCode();
    	storage.addValueObject("VALUEOBJECT", vo);
		storage.setSuccess(true);
    }
    
    public ValueObject getMaterialKindCode(){
    	ValueObject vo 			  = new ValueObject();
    	String temp 			  = params.get("materiallevel");
    	InitCombo initCombo 	  = new InitCombo();
    	RowSet materialKindCodeLv = null;
    	try {
    		if ( "2".equals(temp) ) {
    			materialKindCodeLv = getLv2MaterialKindCode(params.get("materialgroup"));
    		} else if ( "3".equals(temp) ) {
    			materialKindCodeLv = getLv3MaterialKindCode(params.get("materialgroup"));
    		}
			
    		initCombo.makeComboData(materialKindCodeLv, "materialgroupname", "materialgroup");
    		
			vo.set("materialgroup", initCombo.getCode());
			vo.set("materialgroupname", initCombo.getCodeName());
		    
    		//storage.addRowSet("materialKindCodeLv", materialKindCodeLv);
    	} catch(Exception e) {
    		
    	} finally {
			return vo;
		}
    }
    
	// 자재분류 1레벨 코드 가져오기
    public RowSet getLv1MaterialKindCode() {
    	Search search 		= new SearchImpl();
    	RowSet rowSet 		= new RowSetImpl();
        StringBuffer sql 	= new StringBuffer();
        
        /*
         * MATERIAL_ID			: 
         * MATERIALGROUP		: 자재그룹코드
         * PARENT_LEVEL			: 상위그룹
         * MATERIALLEVEL		: 레벨
         * MATERIALGROUPNAME	:
         * MATERIAL_GB			:
         */
        try {
        	sql.append("    select  material_id				\n");
        	sql.append("    , materialgroup					\n");
        	sql.append("    , materialgroup || ' : ' || materialgroupname materialgroupname				\n");
        	sql.append("    from pmm_materialgroup          \n");
        	sql.append(" 	where materiallevel = '1'			\n");            
            sql.append("  	order by materialgroup			\n");
            
            search.setStatement(sql);
        	rowSet = search.execute();
        	WAFLogger.debug(sql.toString());
        } 
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        return rowSet;
    }
    
 // 자재분류 2레벨 코드 가져오기
    public RowSet getLv2MaterialKindCode(String Params) {
    	Search search 			= new SearchImpl();
    	RowSet rowSet 			= new RowSetImpl();
        StringBuffer sql 		= new StringBuffer();
        /*
         * MATERIAL_ID			: 
         * MATERIALGROUP		: 자재그룹코드
         * PARENT_LEVEL			: 상위그룹
         * MATERIALLEVEL		: 레벨
         * MATERIALGROUPNAME	:
         * MATERIAL_GB			:
         */
        try {
        	sql.append("    select  material_id				\n");
        	sql.append("    , materialgroup					\n");
        	sql.append("    , materialgroupname				\n");
        	sql.append("    from pmm_materialgroup          \n");
        	sql.append(" 	where materiallevel = '2'			\n");
        	if (!"".equals(Params) && null != Params) {
        		sql.append(" 	and parent_level = ?		\n");
        		search.addParameter(Params);
        	}
            sql.append("  	order by materialgroup			\n");
            
            search.setStatement(sql);
            
            rowSet = search.execute();
        	WAFLogger.debug(sql.toString());
        } 
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        return rowSet;
    }
    
    // 자재분류 3레벨 코드 가져오기
    public RowSet getLv3MaterialKindCode(String Params) {
    	Search search 			= new SearchImpl();
    	RowSet rowSet 			= new RowSetImpl();
        StringBuffer sql 		= new StringBuffer();
        /*
         * MATERIAL_ID			: 
         * MATERIALGROUP		: 자재그룹코드
         * PARENT_LEVEL			: 상위그룹
         * MATERIALLEVEL		: 레벨
         * MATERIALGROUPNAME	:
         * MATERIAL_GB			:
         */
        try {
        	sql.append("    select  material_id				\n");
        	sql.append("    , materialgroup					\n");
        	sql.append("    , materialgroupname				\n");
        	sql.append("    from pmm_materialgroup          \n");
        	sql.append(" 	where materiallevel = '3'			\n");
        	if (!"".equals(Params) && null != Params) {
        		sql.append(" 	and parent_level = ?		\n");
        		search.addParameter(Params);
        	}
        	sql.append("  	order by materialgroup			\n");
        	
            search.setStatement(sql);
            
        	rowSet = search.execute();
        	WAFLogger.debug(sql.toString());
        } 
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        return rowSet;
    }
}


