/**
 * @(#)file    StockSchedulePurchaseListSaveBiz.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 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;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.util.ValueObject;

public class StockSchedulePurchaseListSaveBiz extends Biz {

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

	public void doBiz() throws Exception {
		if (storage.compareEvent("SAVE_SCHEDULE_LIST")) {
			boolean success = false;
			String[] status = super.gets("sheet1.status");

			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = saveData(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();
			}
		}
	}

	/**
	 * Persistent  Save 
	 * @param params
	 * @return
	 */
	private boolean saveData(Connection connection, ValueObject sqlParams) {
		PreparedStatement pstmt = null;
		
		try {
			StringBuffer sql = new StringBuffer();
			
			sql.append("				UPDATE PMM_STOCKSCHEDULE@CCSIF --2018.11.27 입고계획관리 미사용            				\n");
			sql.append("				SET    CONFIRM_YN  = 'N'              				\n");
			sql.append("				WHERE  SCHEDULENO  = ?								\n");
			
			pstmt = connection.prepareStatement(sql.toString());
			WAFLogger.debug(sql.toString());
			int paramIdx = 1;
			
			pstmt.setString(paramIdx++, sqlParams.get("scheduleno"));
			pstmt.executeUpdate();
						
			return true;
        }
        catch(Exception e) {
            super.doException(e);
            super.storage.setMessage(e.getMessage());
            super.storage.setDetailMessage(e);
            super.conn.setRollback();
            return false;
        }
        finally {
        	super.conn.end();
        }
	}

}
