/**
 *
 * @(#)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 CommGongCafegorySaveBiz extends Biz {
	
	
	public CommGongCafegorySaveBiz(HandlerStorage storage) {
		super(storage);
	}

	public void doBiz() throws Exception {
		
		String event = params.get("fevent");
		String depth = params.get("depth");
		String item_cd = params.get("item_cd");
		
		storage.addObject("depth", depth);
		storage.addObject("item_cd", item_cd);
		
		try {
			super.conn.begin();
			
			
			if(event.equals("SAVE_CATEGORY"))
			{
				    boolean success = saveData_L(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_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 OUT_ITEM       \n"
				  	+ "   (  ITEM_CD                \n"	 
			        + "    , KIND_BIG               \n"		
			        + "    , KIND_MID               \n"		
			        + "    , ITEM_NAME          	\n"		
			        + "    , DEPTH             		\n"		
			        + "    , PARENT_LEVEL           \n"	
			        + "    , CRE_BY           		\n"		
			        + "    , CRE_DATE           	\n"		
			        + "    , UPD_BY           		\n"		
			        + "    , UPD_DATE           	\n"		
			        + " ) VALUES                    \n"
			        + "    (  						\n"
			        + "    ?,	                    \n"  //ITEM_CD   0900                  
			        + "    ?,                     	\n"  //KIND_BIG   08 
			        + "    ?,                     	\n"  //KIND_MID   0900 
			        + "    ?,                     	\n"  //ITEM_NAME   cccc
			        + "    ?,                     	\n"  //DEPTH     2  
			        + "    ?,                     	\n"  //PARENT_LEVEL  08
			        + "    ?,                     	\n"  //CRE_BY      
			        + "    SYSDATE,                 \n"  //CRE_DATE    
			        + "    ?,                     	\n"  //UPD_BY      
			        + "    SYSDATE                  \n"  //UPD_DATE    
			        + "    )         				\n" ;
			  
		  	persistent_i.setStatement(sqlstr);
		  	
		  	ValueObject temp = get_material_cnt(params.get("depth"), params.get("item_cd"));
		  	String ls_code = temp.get("item_cd").trim();
		  	if (params.get("depth").equals("1")){   //첫번째 레벨  		
		  		persistent_i.addParameter(ls_code);                          //ITEM_CD
			  	persistent_i.addParameter(ls_code);                          //KIND_BIG
			  	persistent_i.addParameter((ls_code + "00").trim());                   //KIND_MID
			  	persistent_i.addParameter(params.get("categoryNm"));         //ITEM_NAME
			  	persistent_i.addParameter(params.get("depth"));              //DEPTH
			  	persistent_i.addParameter("S");                              //PARENT_LEVEL
			  	persistent_i.addParameter(storage.getUser().get("ACCT_ID")); //CRE_BY
			  	persistent_i.addParameter(storage.getUser().get("ACCT_ID")); //UP_BY
			  	
		  	}
		  	else if(params.get("depth").equals("2")) //0101	01	0101	토건공사	2	01		2014-06-23 오후 6:19:00		
		  	{
		  		persistent_i.addParameter(ls_code);                          //ITEM_CD   0101
			  	persistent_i.addParameter(params.get("item_cd"));            //KIND_BIG  01
			  	persistent_i.addParameter(ls_code);                          //KIND_MID  0101
			  	persistent_i.addParameter(params.get("categoryNm"));         //ITEM_NAME
			  	persistent_i.addParameter(params.get("depth"));              //DEPTH     2
			  	persistent_i.addParameter(params.get("item_cd"));            //PARENT_LEVEL 01
			  	persistent_i.addParameter(storage.getUser().get("ACCT_ID")); //CRE_BY
			  	persistent_i.addParameter(storage.getUser().get("ACCT_ID")); //UP_BY
			  	
		  	}
		  	
		  	persistent_i.execute();
		  	persistent_i.clearParameters();
			
		  	
			return true;
        }
		catch(Exception e) {

			WAFLogger.error(this.getClass().getName() + " --> saveData_L() : " + e.getMessage());
			WAFLogger.error(e);
			WAFLogger.error(persistent_i.getParameters());
            return false;
        }
	}
	
	
	 public ValueObject get_material_cnt(String depth, String kind_big) throws Exception {
	        Search search = new SearchImpl();
	        StringBuffer sql = new StringBuffer();
	        ValueObject vo = new ValueObject();
	        
	        try {
	        	sql.append("	 SELECT DECODE(?, 1,  TO_CHAR(MAX(ITEM_CD)+1, '00') , TO_CHAR(MAX(ITEM_CD)+1, '0000')) ITEM_CD     \n");
	            sql.append("       FROM OUT_ITEM		         \n");
	            sql.append("      WHERE DEPTH = ?	             \n");
	            sql.append("        AND KIND_BIG LIKE ?	|| '%'   \n");
	            sql.append("        AND ITEM_CD != ? || '99'   \n");
	        	search.setStatement(sql.toString());  
	        	search.addParameter(depth);
	        	search.addParameter(depth);
	        	search.addParameter(kind_big);
	        	search.addParameter(kind_big);
	        	
	        	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;
	    }
	 

}
