/**
 * @(#)file    CommMatlCodeSectListBiz
 * @(#)author  lsm
 * @(#)version 1.0
 * @(#)date    2014. 3. 5
 * @(#)since   JDK1.4.1
 *
 * Copyright (c) 2002-2003 www.hwenc.com, Inc.  
 * All rights reserved.
 * This software is the proprietary information of hwenc, Inc.
 */
package kr.co.udapsoft.ebid.buyer.sycs.common.biz;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
import com.udapsoft.waf.common.ui.HTMLMaker;
import kr.co.udapsoft.ebid.buyer.sycs.security.biz.SYSRightCommon;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;
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.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
public class CommMatlCodeTagSectListBiz extends Biz {
	
	private static String MATERIALGROUP_TABLE     = "PMM_MATERIALGROUP";
    private static String MATREIAL_TABLE        = "PMM_COMM_MATERIAL_MASTER";
    private static String CODE_TABLE   = "EBID_COM911T";
    

    public CommMatlCodeTagSectListBiz(HandlerStorage storage) {

        super(storage);        
    }
    public void doBiz() throws Exception {

    	String systemId   = params.get("system_id");
    	String kindCode[] = null;
    	String spec1 = "";
    	String spec2 = "";
    	String spec3 = "";
    	String spec4 = "";
    	
    	String code = params.get("code");
    	String kind_big = params.get("kind_big");
    	String kind_mid = params.get("kind_mid");
    	String kind_small = params.get("kind_small");
    	String specCode = params.get("specCode");
    	String calTpCode = params.get("calTpCode");
    	String materialGroupName = params.get("materialGroupName");
    	
        // ibsheet 단위 콤보박스
        String selectText =  getKindCodeSelectTagCombo(systemId, "style='width:220px;height=18;'");
        storage.addObject("selectText", selectText);
        
        /*   ibsheet 단위 콤보박스 (value) */
        String selectValue =  getKindCodeSelectTagCode(systemId, "style='width:220px;height=18;'");
        storage.addObject("selectValue", selectValue);
        
        
        
        // ibsheet 중점관리여부 콤보박스
        String selectStockText =  getStockSelectTagCombo(systemId, "style='width:220px;height=18;'");
        storage.addObject("selectStockText", selectStockText);
        
        /*   ibsheet 중점관리여부 콤보박스 (value) */
        String selectStockValue =  getStockSelectTagCode(systemId, "style='width:220px;height=18;'");
        storage.addObject("selectStockValue", selectStockValue);
        
        
        
        if(params.get("level").equals("1"))
        {
        	kind_big = params.get("code");
        	params.set("kind_big", kind_big);
        }
        else if(params.get("level").equals("2"))
        {
        	kindCode = code.split("-");
        	
        	kind_big = kindCode[0];
    		kind_mid = kindCode[1];
    		
    		params.set("kind_big", kind_big);
    		params.set("kind_mid", kind_mid);
        }
        	
        storage.addObject("sect_id", params.get("sect_id"));
		storage.addObject("code", params.get("code"));
		storage.addObject("level", params.get("level"));
        
    	if( params.get("level").equals("3") && !params.get("sect_id").equals("") ) {
    		
    		if(code.indexOf("-") != -1)
        	{
        		kindCode = code.split("-");
        		
        		kind_big = kindCode[0];
        		kind_mid = kindCode[1];
        		kind_small = kindCode[2];
        		
        		params.set("kind_big", kind_big);
        		params.set("kind_mid", kind_mid);
        		params.set("kind_small", kind_small);
    		
    		storage.addObject("specCode",params.get("specCode"));
    		storage.addObject("calTpCode",params.get("calTpCode"));
    		storage.addObject("materialGroupName",materialGroupName);
        	}
    		if(params.get("specCode") != null)
    		{
    			if( specCode.split("-")[0] != null ){
    				storage.addObject("spec1", specCode.split("-")[0]);
    			}
    			if( specCode.split("-")[1] != null ){
    				storage.addObject("spec2", specCode.split("-")[1]);
    			}
    			if( specCode.split("-")[2] != null ){
    				storage.addObject("spec3", specCode.split("-")[2]);
    			}
    			if( specCode.split("-")[3] != null ){
    				storage.addObject("spec4", specCode.split("-")[3]);
    			}
    		}
    	 }
    	WAFLogger.debug("params=============>" + params);
    	
        
//        if(!params.get("kind_big").equals("") || !params.get("kind_mid").equals("") || !params.get("kind_small").equals(""))
//    	{
              params.set("sheet1", getMatlCodeSectList(params));
              storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
              
              
              WAFLogger.debug(storage.getObject("ibsheet7Data"));
//    	}
    }
    
    
    public String getKindCodeSelectTagCombo(String selectedValue, String style) throws Exception {

        RowSet rowSet = getCodeRowSetCombo();

        return HTMLMaker.getSelectTagText4Sheet(rowSet);
    }
    
    public String getKindCodeSelectTagCode(String selectedValue, String style) throws Exception{

        RowSet rowSet = getCodeRowSetCombo();
        
        return HTMLMaker.getSelectTagValue4Sheet( rowSet);
    }
    
    public String getStockSelectTagCombo(String selectedValue, String style) throws Exception{

        RowSet rowSet = getStockCodeRowSetCombo();

        return HTMLMaker.getSelectTagText4Sheet(rowSet);
    }
    
    public String getStockSelectTagCode(String selectedValue, String style) throws Exception{

        RowSet rowSet = getStockCodeRowSetCombo();
        
        return HTMLMaker.getSelectTagValue4Sheet( rowSet);
    }
    
    
    /**
     * 검색을 위한 시스템 목록 리턴
     * @return
     */
    public RowSet getCodeRowSetCombo() throws Exception{
        
        Search search = new SearchImpl();
        RowSet sysRowSet = null;
        String sqlstr = "";
        try {

            sqlstr = " SELECT  DETAILNM, DETAILCD  					\n"
                   + " FROM     " + CODE_TABLE + "        				\n"
                   + " WHERE BASECD = 'UNIT_KIND_GB'      		  	\n"
                   + " ORDER BY SORT_NO ASC                          	\n";
            search.setStatement(sqlstr);
            sysRowSet = search.execute();
            WAFLogger.debug(sqlstr);
        }
        catch(Exception e) {

        	WAFLogger.error(this.getClass().getName() + " --> getSystemRowSet() : " + e.getMessage());
        	WAFLogger.error("SQL : " + search.getStatement());
        	WAFLogger.error(e);
            return null;
        }
        finally {

            if( sysRowSet == null ) sysRowSet = new RowSetImpl();
            return sysRowSet;
        }
    }
    
    
    
    /**
     * 검색을 위한 시스템 목록 리턴
     * @return
     */
    public RowSet getStockCodeRowSetCombo() throws Exception{
        
        Search search = new SearchImpl();
        RowSet sysRowSet = null;
        String sqlstr = "";
        try {

            sqlstr = " SELECT  DETAILNM, DETAILCD  					\n"
                   + " FROM     " + CODE_TABLE + "        				\n"
                   + " WHERE BASECD = 'STOCK_FLAG'      		  	\n"
                   + " ORDER BY SORT_NO ASC                          	\n";
            search.setStatement(sqlstr);
            sysRowSet = search.execute();
            
            WAFLogger.debug(sqlstr);
        }
        catch(Exception e) {

        	WAFLogger.error(this.getClass().getName() + " --> getSystemRowSet() : " + e.getMessage());
        	WAFLogger.error("SQL : " + search.getStatement());
        	WAFLogger.error(e);
            return null;
        }
        finally {

            if( sysRowSet == null ) sysRowSet = new RowSetImpl();
            return sysRowSet;
        }
    }
    
    
    
	
    private RowSet getMatlCodeSectList(ValueObject Params) throws Exception{
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        String sqlstr = "";
        try {        	
        	
        	sqlstr += " SELECT B.MATERIAL_CD,																			\n"
        			+"		          TRIM(B.MATERIAL_NAME) AS MATERIAL_NAME,															\n"
					+"		          TRIM(B.SPEC1) AS SPEC1,								\n"
					+"		          TRIM(B.SPEC2) AS SPEC2,								\n"
					+"		          TRIM(B.SPEC3) AS SPEC3,								\n"
					+"		          TRIM(B.SPEC4) AS SPEC4,								\n"
					+"		          B.IS_USE,																					\n"
					+"		          B.MATERIAL_SPEC,																					\n"
					+"		          B.PAY_DIV,																					\n"
					+"		          DECODE(B.PAY_DIV, '1', '납품정산', '2', '계획분할', '') PAY_DIV_NAME																					\n"
					+"		 FROM " + MATERIALGROUP_TABLE + " A, " + MATREIAL_TABLE + " B, PMM_MATL_CAL_TYPE C				\n"
					+"		 	WHERE A.MATERIALGROUP = B.KIND_SMALL											\n"
					+"			AND A.CAL_TP_CODE = C.CAL_TP_CODE(+)											\n"
					+"			AND A.MATERIAL_GB = 'P'																	\n";
//					if(params.get("pgm_name2").equals(""))
//					{
						if(!params.get("kind_big").equals(""))
		            	{
							sqlstr+="		 	AND B.KIND_BIG = 	?																\n";
		            	}
						if(!params.get("kind_mid").equals(""))
		            	{
							sqlstr+="		 	AND B.KIND_MID = 	?																\n";
		            	}
						if(!params.get("kind_small").equals(""))
		            	{
							sqlstr+="		 	AND B.KIND_SMALL = 	?																\n";
		            	}
//					}
			 sqlstr+="		 	AND (B.MATERIAL_NAME  LIKE '%' || ? || '%'		OR 	B.MATERIAL_CD LIKE '%' || ? || '%')											\n"
					+"		 	ORDER BY B.MATERIAL_ID ASC,	B.MATERIAL_CD ASC																\n";
        	
        	
            search.setStatement(sqlstr);
            if(!params.get("kind_big").equals(""))
        	{
        		search.addParameter(params.get("kind_big"));
        	}
        	if(!params.get("kind_mid").equals(""))
        	{
        		search.addParameter(params.get("kind_mid"));
        	}
        	if(!params.get("kind_small").equals(""))
        	{
        		search.addParameter(params.get("kind_small"));
        	}
            search.addParameter(params.get("pgm_name2"));
            search.addParameter(params.get("pgm_name2"));
            
            
            WAFLogger.debug(sqlstr);
            WAFLogger.debug(search.getParameters());
            rowSet = search.execute();

        }
        catch(Exception e) {

        	WAFLogger.error(this.getClass().getName() + " --> getSystemRowSet() : " + e.getMessage());
        	WAFLogger.error("SQL : " + search.getStatement());
        	WAFLogger.error(e);
            return null;
        }
        finally {    
        	 if(rowSet == null) rowSet = new RowSetImpl();
             return rowSet;
        }
	}
}
