/**
 * @(#)file    StockDeliveryWillPriceSaveBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.7.15
 * @(#)Description: 가입고 단가 등록
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 */
package kr.co.udapsoft.ebid.buyer.warehouse.purchase.biz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;

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

public class StockDeliveryWillPriceSaveBiz extends Biz {
   public StockDeliveryWillPriceSaveBiz(HandlerStorage storage) {
	   super(storage);        
   }
   
   public void doBiz() throws Exception {
	   boolean success =  false;
	   if(storage.compareEvent("SAVE_STOCKDELIVERYWILLPRICE")) {
			String[] status = super.gets("SHEET1.status");
			
			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = saveStockDeliveryWillPrice(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					}
				}
				super.storage.setSuccess(success);
				storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			} catch (Exception e) {
				WAFLogger.error(e);
				super.conn.setRollback();
				throw new Exception(e);
				
			} finally {
				super.conn.close();
			}
   		} else if ( storage.compareEvent("GET_PMM_WAREHOUSE_CLOSE") ) {
	    	RowSet listRowSet = new RowSetImpl();
	    	listRowSet = getPmmWarehouseClose(params);
	    	storage.addRowSet("ROWSET", listRowSet);
   		}
   }

   // 저장
   public boolean saveStockDeliveryWillPrice(Connection connection, ValueObject sqlParams) throws Exception {
	   PreparedStatement pstmt = null;
	   StringBuffer sqlstr_u   = new StringBuffer();
	 //2018.11.27 가입고단가 미사용 
	   try {
	        sqlstr_u.append("	UPDATE ETEC_ERPIF.PMM_STOCK_ITEM SET     --2018.11.27 가입고단가 미사용                       			\n");
	        sqlstr_u.append("	    STOCK_PRI = ?                                    			\n");
	        sqlstr_u.append("	    , STOCK_AMT = TO_NUMBER(STOCK_QTY) * TO_NUMBER(?)			\n");
	        sqlstr_u.append("	    , EXC_PRI = ?			\n");
	        sqlstr_u.append("	    , EXC_AMT = TO_NUMBER(STOCK_QTY) * TO_NUMBER(?)			\n");
	        sqlstr_u.append("	    , UPD_BY = ?                                     			\n");
	        sqlstr_u.append("	    , UPD_DATE = SYSDATE                             			\n");
	        sqlstr_u.append("	WHERE MATERIAL_CD = ?	\n");
	        sqlstr_u.append("	AND PUR_CONT_ID = ?                                  			\n");
	        sqlstr_u.append("	AND STOCK_ID IN (	\n");
	        sqlstr_u.append("					SELECT STOCK_ID FROM ETEC_ERPIF.PMM_STOCK WHERE SUBSTR(STOCK_DATE,1,6) = ?	 --2018.11.27 가입고단가 미사용  \n");
	        sqlstr_u.append("					)		\n");
	        
	        pstmt = connection.prepareStatement(sqlstr_u.toString());
	        int paramIdx = 1;
			pstmt.setString(paramIdx++, sqlParams.get("stock_pri"));
			pstmt.setString(paramIdx++, sqlParams.get("stock_pri"));
			pstmt.setString(paramIdx++, sqlParams.get("stock_pri"));
			pstmt.setString(paramIdx++, sqlParams.get("stock_pri"));
			pstmt.setInt(paramIdx++, storage.getUser().getInt("acct_id"));
			pstmt.setString(paramIdx++, sqlParams.get("material_cd"));
			pstmt.setString(paramIdx++, sqlParams.get("pur_cont_id"));
			pstmt.setString(paramIdx++, params.get("dateFrom").replaceAll("-", ""));
			
			pstmt.executeUpdate();
			
			return true;
        }
        catch(Exception e) {
        	WAFLogger.error(e);
			throw new Exception(e);
        }
        finally {
        	try {
				if(pstmt != null) pstmt.close();
			} catch(SQLException se) {
				WAFLogger.error(se);
			}
        }
    }
   
   /**
    * 마감여부 확인
    * @author Gyeong mo An
    * @since 2014.04.18
    * @return
    * @throws Exception
    */
   public RowSet getPmmWarehouseClose(ValueObject params) throws Exception {
   	Search search 	  = new SearchImpl();
   	RowSet listRowSet = new RowSetImpl();
   	StringBuffer sql  = new StringBuffer();
   	try {
   		sql.append(" SELECT                     \n");
   		sql.append("     FLAG08          \n");
   		sql.append("     , DEPT_CD                \n");
   		sql.append("     , YYYYMM               \n");
   		sql.append(" FROM COM900V   \n");
   		sql.append(" WHERE 1=1					\n");
   		
   		if(!"".equals(params.get("dept_cd2"))) {
   			sql.append(" AND DEPT_CD = '" + params.get("dept_cd2") + "'			\n");
   		}
   		
   		if(!"".equals(params.get("dateFrom"))) {
   			sql.append(" AND YYYYMM = '" + params.get("dateFrom").replace("-", "").substring(0, 6) + "'             \n");
   		}
   		
   		search.setStatement(sql.toString());
   		listRowSet = search.execute();
   		WAFLogger.debug(sql.toString());
   	} catch(Exception e) {
   		super.doException(e);
   		this.storage.setSuccess(false);
   		this.storage.setMessage(e.getMessage());
   		storage.setDetailMessage(e);
   	} 
   	return listRowSet;
   }
}




