/**
 * @(#)file    MatlCodeSelectAllPopup.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.22
 * @(#)Description: 자재선택 팝업
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.supplier.portal.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 kr.co.udapsoft.ebid.buyer.common.lib.biz.GetMaterialKindCodeBiz;

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


public class MatlCodeSelectAllPopupBiz extends Biz {

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

    public void doBiz() throws Exception {
    	        
		if("".equals(storage.getEvent())){
			GetMaterialKindCodeBiz biz = new GetMaterialKindCodeBiz(storage);
			RowSet materialKindCodeLv1 = biz.getLv1MaterialKindCode();
			storage.addRowSet("materialKindCodeLv1", materialKindCodeLv1);
		}else if ("GET_MATL_CODE_LIST".equals(storage.getEvent())){
			getMatlCodeList();
		}
        
    }
    
    private void getMatlCodeList() throws Exception{
    	params.set("sheet1", getMatlCode(params));
		storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
    }
    
    /**
     * 자재코드 가져오기
     * @return RowSet
     */
    private RowSet getMatlCode(ValueObject Params) {
    	Search search 		= new SearchImpl();
    	RowSet rowSet 		= new RowSetImpl();
    	
        StringBuffer sql 	= new StringBuffer();
        
        try {
        	sql.append("SELECT  MATERIAL_CD                             --자재코드          \n");
        	sql.append("       ,KIND_BIG                                --대분류            \n");
        	sql.append("       ,KIND_MID                                --중분류            \n");
        	sql.append("       ,KIND_SMALL                              --소분류            \n");
        	sql.append("       ,MATERIAL_NAME                           --자재명            \n");
        	sql.append("       ,MATERIAL_SPEC                           --규격            \n");
        	sql.append("       ,UNIT_KIND                               --단위코드          \n");
        	sql.append("       ,F_GET_UNIT_KIND_NAME(MATERIAL_CD) AS UNIT_KIND_NAME                               --단위명          \n");
        	sql.append("       ,IS_USE                                  --사용여부          \n");
        	sql.append("       ,STOCK_FLAG                              --중요자재여부      \n");
        	sql.append("       ,STOCK_PLAN_FLAG                         --입고계획여부      \n");
        	sql.append(" FROM  PMM_COMM_MATERIAL_MASTER@CCSIF                                     \n");
        	sql.append("where IS_USE = 'Y'                                                  \n");

        	if (!"".equals(params.get("search_material_cd"))) {
            	sql.append(" and MATERIAL_CD LIKE UPPER(?) || '%'	\n");
            	search.addParameter(params.get("search_material_cd"));
            }
        	if (!"".equals(params.get("search_material_name"))) {
        		sql.append(" and MATERIAL_NAME LIKE '%' || ? || '%'		\n");
        		search.addParameter(params.get("search_material_name"));
        	}
        	if (!"".equals(params.get("material_Kind_Code_Lv1"))) {
            	sql.append(" and KIND_BIG = ?							\n");            
            	search.addParameter(params.get("material_Kind_Code_Lv1"));
            }
        	if (!"".equals(params.get("material_Kind_Code_Lv2"))) {
            	sql.append(" and KIND_MID = ?							\n");            
            	search.addParameter(params.get("material_Kind_Code_Lv2"));
            }
        	if (!"".equals(params.get("material_Kind_Code_Lv3"))) {
            	sql.append(" and KIND_SMALL = ?							\n");            
            	search.addParameter(params.get("material_Kind_Code_Lv3"));
            }
            sql.append("  ORDER BY MATERIAL_CD               				\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;
    }
 
}




