/**
 * @(#)file    BidPlanBiz.java
 * @(#)author  Son Hyung Min 
 * @(#)version 1.0
 * @(#)date    2014-06-23
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.bid.outside.biz;



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

import oracle.toplink.internal.ox.StrBuffer;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.Row;
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.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 kr.co.udapsoft.common.code.CommCodeManager;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;
import kr.co.udapsoft.ebid.buyer.sycs.user.biz.UserCommon;

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 BidPlanSheetBiz extends Biz {

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

	public void doBiz() throws Exception {
		
		if(storage.compareEvent("SET_CORP_DELETE")){
			boolean success = false;
			String[] status = super.gets("sheet1.STATUS");
			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusDelete(status[i])) {
						success = corp_delete(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("SET_REPORT_EMP_DELETE")){
			boolean success = false;
			String[] status = super.gets("sheet2.STATUS");
			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusDelete(status[i])) {
						success = report_emp_delete(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET2", 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();
			}
		}
	}
	
	private boolean corp_delete(Connection connection, ValueObject valueObject) throws Exception{
		Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        
		try{
			sqlstr.append("DELETE BID_SUPP							\n");
			sqlstr.append("WHERE BID_NO = ?							\n");
			sqlstr.append("AND VENDOR_CD = ?							\n");
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(params.get("BID_NO"));
			persistent.addParameter(valueObject.get("VENDOR_CD"));
			persistent.execute();
			return true;
		} catch (Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
		}
	}
	
	private boolean report_emp_delete(Connection connection, ValueObject valueObject) throws Exception{
		Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        
		try{
			sqlstr.append("DELETE BID_OPEN_REPORT					\n");
			sqlstr.append("WHERE BID_NO = ?							\n");
			sqlstr.append("AND BID_DEG = ?							\n");
			sqlstr.append("AND EMP_ID = ?							\n");
			persistent.setStatement(sqlstr.toString());
			persistent.addParameter(params.get("BID_NO"));
			persistent.addParameter(params.get("BID_DEG"));
			persistent.addParameter(valueObject.get("ACCT_ID"));
			persistent.execute();
			return true;
		} catch (Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> report_emp_delete() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
		}
	}	
}