/**
 * @(#)file    DivContPopListSaveBiz.java
 * @(#)author  chj
 * @(#)version 1.0
 * @(#)date    2015-02-25
 * @(#)since   JDK 1.6
 */
package kr.co.udapsoft.ebid.buyer.bid.purchase.biz;



import java.sql.Connection;

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 oracle.toplink.internal.ox.StrBuffer;

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

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

	public void doBiz() throws Exception {
		boolean success = false;
		
		if(storage.compareEvent("CONFIRM_SHEET_LIST")){//확정
			//확정 업데이트
			 success = saveConfirmProc();
			 super.storage.setSuccess(success);
			 storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}else if(storage.compareEvent("DELETE_SHEET_LIST")){ //삭제
			//삭제 로직
			success = deleteConfirmProc();
			super.storage.setSuccess(success);
			//
		}
	}
	
	  /**
     * BID_SUPP_ESTI 저장
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean saveConfirmProc() {
        try {
            conn.begin();
            
            String[] status = super.gets("sheet1.STATUS");
            for (int i = 0; i < status.length; i++) {
				if (IBSheet7Utils.equalStatusUpdate(status[i])) {
					doSave(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
				}
			}
            return true;
        }
        catch(Exception e) {
        	storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
            WAFLogger.error(this.getClass().getName() + " --> saveProc() : " + e.getMessage());
            WAFLogger.error(e);
            conn.setRollback();

            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	
            conn.end();
        }
    }

	/**
	 * BID_SUPP_ESTI 저장
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean deleteConfirmProc() throws Exception {
		try {
			conn.begin();
			
			String[] status = super.gets("sheet1.STATUS");
			for (int i = 0; i < status.length; i++) {
				if (IBSheet7Utils.equalStatusUpdate(status[i])) {
					
					ValueObject vo = getBidSupp(IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
					
					if(!vo.get("CNT").equals("0")){
						String message = "결재 내역이 있습니다.";
						//                WAFLogger.error(message);
						Exception e = new Exception(message);
						throw e;
					}
					doDelete(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
				}
			}
			
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			return true;
		}
		catch(Exception e) {
			storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
			WAFLogger.error(this.getClass().getName() + " --> saveProc() : " + e.getMessage());
			WAFLogger.error(e);
			conn.setRollback();
			
			storage.setDetailMessage(e);
			return false;
		}
		finally {
			
			conn.end();
		}
	}
	
	/**
     * BID_SUPP_ESTI 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void doSave(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	
        	sqlstr.append("  UPDATE BID_SUPP				\n");
        	sqlstr.append("  SET CONFIRM_YN = 'Y'					\n");
        	sqlstr.append("    ,  BID_STATUS_CD = '20'					\n");
        	sqlstr.append("  WHERE BID_NO = ?					\n");
        	sqlstr.append("  AND   BID_DEG = ?					\n");
        	sqlstr.append("  AND   VENDOR_CD = ?				\n");

        	
        	persistent.setStatement(sqlstr.toString());
        	
    		persistent.addParameter(valueObject.get("BID_NO"));
    		persistent.addParameter(valueObject.get("BID_DEG"));
    		persistent.addParameter(valueObject.get("VENDOR_CD"));
    		persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }

	/**
	 * BID_SUPP_ESTI 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void doDelete(Connection connection, ValueObject valueObject) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		Persistent persistent2 = new PersistentImpl(connection);
		Persistent persistent3 = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		StrBuffer sqlstr2 = new StrBuffer();
		StrBuffer sqlstr3 = new StrBuffer();
		try {
			
        	sqlstr.append("  UPDATE BID_SUPP				\n");
        	sqlstr.append("  SET CONFIRM_YN = NULL					\n");
        	sqlstr.append("    , BID_STATUS_CD = '15'				\n");
        	sqlstr.append("  WHERE BID_NO = ?					\n");
        	sqlstr.append("  AND   BID_DEG = ?					\n");
			

        	sqlstr2.append("  UPDATE BID_MASTER				\n");
        	sqlstr2.append("  SET STATUS_CD = '06'					\n");
        	sqlstr2.append("  WHERE BID_NO = ?					\n");
        	sqlstr2.append("  AND   BID_DEG = ?					\n");
        	
        	sqlstr3.append("DELETE  BID_DECIDE_MAIL_LIST						\n");
        	sqlstr3.append("WHERE BID_NO = ?						\n");
			
			persistent.setStatement(sqlstr.toString());
			persistent2.setStatement(sqlstr2.toString());
			persistent3.setStatement(sqlstr3.toString());
			
    		persistent.addParameter(valueObject.get("BID_NO"));
    		persistent.addParameter(valueObject.get("BID_DEG"));
			persistent.execute();

			persistent2.addParameter(valueObject.get("BID_NO"));
			persistent2.addParameter(valueObject.get("BID_DEG"));
			persistent2.execute();
			
            persistent3.addParameter(valueObject.get("BID_NO"));
            persistent3.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
	
	public ValueObject getBidSupp( ValueObject valueObject) throws Exception {
        Search search = new SearchImpl();
        StringBuffer sql = new StringBuffer();
        ValueObject vo = new ValueObject();
        
        try {
        	sql.append("	SELECT COUNT(*) CNT FROM BID_SUPP		\n")
        		.append("		WHERE BID_NO = ?													\n")				
				.append("		AND BID_DEG = ?														\n")
        		.append("		AND BID_STATUS_CD IN ( '10' , '07')											\n")
        	.append("		AND      SUCC_YN = 'Y'											\n");
            
        	search.setStatement(sql.toString());        	
        	search.addParameter(valueObject.get("BID_NO"));
        	search.addParameter(valueObject.get("BID_DEG"));
        	
        	vo = RowSetUtility.getValueObject(search.execute().getRow(0));
            
        } catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getBidSupp() : \n" + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
			throw e;
            
        } finally {
        	if(vo == null) vo = new ValueObject();
        }
        
        return vo;
    }
	
	
	/**
	 * BID_SUPP_ESTI 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void doCheck(Connection connection, ValueObject valueObject) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		Persistent persistent2 = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		StrBuffer sqlstr2 = new StrBuffer();
		try {
			
        	sqlstr.append("  UPDATE BID_SUPP				\n");
        	sqlstr.append("  SET CONFIRM_YN = NULL					\n");
        	sqlstr.append("  WHERE BID_NO = ?					\n");
        	sqlstr.append("  AND   BID_DEG = ?					\n");
			

        	sqlstr2.append("  UPDATE BID_MASTER				\n");
        	sqlstr2.append("  SET STATUS_CD = '06'					\n");
        	sqlstr2.append("  WHERE BID_NO = ?					\n");
        	sqlstr2.append("  AND   BID_DEG = ?					\n");
        	
			
			persistent.setStatement(sqlstr.toString());
			persistent2.setStatement(sqlstr2.toString());
			
    		persistent.addParameter(valueObject.get("BID_NO"));
    		persistent.addParameter(valueObject.get("BID_DEG"));
			persistent.execute();

			persistent2.addParameter(valueObject.get("BID_NO"));
			persistent2.addParameter(valueObject.get("BID_DEG"));
			persistent2.execute();
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
}