/**
 *
 * @(#)author  [lsm]
 * @(#)version 1.0
 * @(#)date   2014. 3. 5
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.hwenc.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
 package kr.co.udapsoft.ebid.buyer.sycs.common.biz;


import weblogic.wsee.jws.container.Request;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.persistent.Persistent;
import kr.co.hsnc.common.sql.persistent.PersistentImpl;
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 com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class CommMatlCodeCafegorySaveBiz extends Biz {
	
	private static String SAVE_TABLE    = "PMM_MATERIALGROUP";
	private static String SEQ_SELECT    = "SELECT DECODE(MAX(MATERIAL_ID) +1, '', 1, MAX(MATERIAL_ID)+1) FROM PMM_MATERIALGROUP";
	private static String SELECT_MAX_MATERIAL    = "SELECT MAX(MATERIALGROUP)+1 FROM PMM_MATERIALGROUP WHERE PARENT_LEVEL = '0'";
	
	
	
	public CommMatlCodeCafegorySaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		
		String event = params.get("fevent");
		String cate_gubun = params.get("cate_gubun");
		String level = params.get("level");
		String kind_big = params.get("kind_big");
		String kind_mid = params.get("kind_mid");
		
		storage.addObject("cate_gubun", cate_gubun);
		storage.addObject("level", level);
		storage.addObject("kind_big", kind_big);
		storage.addObject("kind_mid", kind_mid);
		
		try {
			super.conn.begin();
			
			
			if(event.equals("SAVE_CATEGORY"))
			{
				if(cate_gubun.equals("H"))
				{
					
					boolean success = saveData(params);
					//success = saveDataCCSIF(params);
					super.storage.setSuccess(success);
				}
				
				if(cate_gubun.equals("L") )
				{
				    
					ValueObject temp = get_material_cnt(params.get("categoryCode"));
	        		String count = temp.get("cnt");
	        		if (!"0".equals(count)) {
	        			String message = "중복된 그룹코드가 존재합니다.";
	        			Exception e = new Exception(message);
	        			ValueObject errMsg = new ValueObject();
	        			errMsg.set("message", message);
	                    
	                    storage.setMessage(message.replace("\"", ""));
	                    storage.addValueObject("VALUEOBJECT", errMsg);
	                    throw e;
	        		}else{
	        			boolean success = saveData_L(params);
	        			//success = saveData_LCCSIF(params);
	        			
	                    String message = "등록성공";
	                    ValueObject errMsg = new ValueObject();
	                    errMsg.set("errMsg", message);
	                    storage.addValueObject("VALUEOBJECT", errMsg);
	                    
	                    super.storage.setSuccess(success);
	        		}
				}
			}
			
		} catch (Exception e) {
			super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            super.storage.setSuccess(false);
		} finally {
			super.conn.end();
		}
	}
	
	/**
	 * Persistent 
	 * @param params
	 * @return
	 * * 
     * @param connection
     * @param pgmName
     * @param screenName
     * @param systemCode
     * @throws Exception
	 */
	private boolean saveData(ValueObject params) throws Exception{
		Persistent persistent_i = null;
		String sqlstr = "";
		String categoryNm = params.get("categoryNm");

		try {

			persistent_i = new PersistentImpl(super.conn.getConnection());
			
		  sqlstr   += "  INSERT INTO " + SAVE_TABLE + "            									\n"
				  	+ "   (  MATERIAL_ID              														\n"		
			        + "   	, MATERIALGROUP              													\n"		
			        + "    , PARENT_LEVEL              														\n"		
			        + "    , MATERIALLEVEL          														\n"		
			        + "    , MATERIALGROUPNAME             											\n"		
			        + "    , MATERIAL_GB           															\n"		
			        + " ) VALUES                  																\n"
			        + "    ( ("+SEQ_SELECT+"),  															\n"
			        + "    ?,																						\n"
			        + "    ?,                     																	\n"
			        + "    ?,                     																	\n"
			        + "    ?,                     																	\n"
			        + "    ?                     																	\n"
			        + "    )         																				\n" ;
			  
		  	persistent_i.setStatement(sqlstr);
		  	
		  	persistent_i.addParameter(params.get("categoryCode"));
		  	persistent_i.addParameter("0");
		  	persistent_i.addParameter("1");
		  	persistent_i.addParameter(categoryNm);
		  	persistent_i.addParameter("C");
		  	
		  	persistent_i.execute();
		  	persistent_i.clearParameters();
		  	
			return true;
        }
		catch(Exception e) {

			WAFLogger.debug(this.getClass().getName() + " --> getSystemRowSet() : " + e.getMessage());
			WAFLogger.debug(e);
            e.printStackTrace();
            return false;
        }
	}
	
	
	
	private boolean saveData_L(ValueObject params) throws Exception{
		Persistent persistent_i = null;
		String sqlstr = "";
		
		String categoryNm = params.get("categoryNm");

		try {

			persistent_i = new PersistentImpl(super.conn.getConnection());
			
		  sqlstr   += "  INSERT INTO " + SAVE_TABLE + "            									\n"
				  	+ "   (  MATERIALGROUP              													\n"		
			        + "   	, MATERIAL_ID              														\n"		
			        + "    , PARENT_LEVEL              														\n"		
			        + "    , MATERIALLEVEL          														\n"		
			        + "    , MATERIALGROUPNAME             											\n"		
			        + "    , MATERIAL_GB           															\n"		
			        + " ) VALUES                  																\n"
			        + "    (?,  																					\n"
			        + "    (SELECT DECODE(MAX(MATERIAL_ID) + 1, '', ?, MAX(MATERIAL_ID) + 1) FROM PMM_MATERIALGROUP  WHERE MATERIALLEVEL = ?  AND PARENT_LEVEL = ?	),																					\n"
			        + "    ?,                     																	\n"
			        + "    ?,                     																	\n"
			        + "    ?,                     																	\n"
			        + "    ?                     																	\n"
			        + "    )         																				\n" ;
			  
		  	persistent_i.setStatement(sqlstr);
		  	
		  	
		  	persistent_i.addParameter(params.get("categoryCode"));
		  	
		  	if(params.get("levelDepth").equals("2"))
		  	{
		  		persistent_i.addParameter(params.get("kind_big") + "01");
		  		persistent_i.addParameter("2");
		  		persistent_i.addParameter(params.get("kind_big"));
		  		persistent_i.addParameter(params.get("kind_big"));
		  	}
		  	else if(params.get("levelDepth").equals("3"))
		  	{
		  		persistent_i.addParameter(params.get("kind_mid") + "001");
		  		persistent_i.addParameter("3");
		  		persistent_i.addParameter(params.get("kind_mid"));
		  		persistent_i.addParameter(params.get("kind_mid"));
		  	}
		  	
		  	persistent_i.addParameter(params.get("levelDepth"));
		  	persistent_i.addParameter(categoryNm);
		  	persistent_i.addParameter("C");
		  	
		  	
		  	persistent_i.execute();
		  	persistent_i.clearParameters();
			
		  	
			return true;
        }
		catch(Exception e) {

			WAFLogger.error(this.getClass().getName() + " --> saveData_L() : " + e.getMessage());
			WAFLogger.error(e);
            return false;
        }
	}
	
	
	 public ValueObject get_material_cnt(String categoryCode) throws Exception {
	        Search search = new SearchImpl();
	        StringBuffer sql = new StringBuffer();
	        ValueObject vo = new ValueObject();
	        
	        try {
	        	sql.append("	 SELECT count(*)	cnt	\n");
	            sql.append("       FROM PMM_MATERIALGROUP		\n");
	            sql.append("      WHERE MATERIALGROUP = ?	\n");
	            
	        	search.setStatement(sql.toString());        	
	        	search.addParameter(categoryCode);
	            
	        	vo = RowSetUtility.getValueObject(search.execute().getRow(0));
	            
	        } catch(Exception e) {
	        	WAFLogger.error(this.getClass().getName() + " --> get_conDocNo_cnt() : \n" + e.getMessage());
	        	WAFLogger.error("SQL : " + search.getStatement());
	        	WAFLogger.error(e);
	            
	        } finally {
	        	if(vo == null) vo = new ValueObject();
	        }
	        
	        return vo;
	    }
	 

}
