/**
 * @(#)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.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 DivContPopListSaveBiz extends Biz {

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

	public void doBiz() throws Exception {
		boolean success = false;
		
		if(storage.compareEvent("SAVE_VENDOR_LIST")){//저장
			//업체 업데이트
			 success = saveProc();
			 super.storage.setSuccess(success);
			 storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}
	
	  /**
     * BID_SUPP_ESTI 저장
	 * @param event
	 * @param valueObject
	 * @return
	 */
	public boolean saveProc() {
        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));
				}
			}
            
            doMasterSave(super.conn.getConnection(), params);
            
            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 persistent1 = new PersistentImpl(connection);
        Persistent persistent2 = new PersistentImpl(connection);
        Persistent persistent3 = new PersistentImpl(connection);
        Persistent persistent4 = new PersistentImpl(connection);
        Persistent persistent5 = new PersistentImpl(connection);
        Persistent persistent6 = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	
        	sqlstr.append("  UPDATE BID_SUPP_ESTI				\n");
        	sqlstr.append("  SET SELECT_QTY = ?					\n");
        	sqlstr.append("  WHERE BID_NO = ?					\n");
        	sqlstr.append("  AND   BID_DEG = ?					\n");
        	sqlstr.append("  AND   VENDOR_CD = ?				\n");
        	sqlstr.append("  AND   MATERIAL_CD = ?				\n");

        	
        	persistent1.setStatement(sqlstr.toString());
        	persistent2.setStatement(sqlstr.toString());
        	persistent3.setStatement(sqlstr.toString());
        	persistent4.setStatement(sqlstr.toString());
        	persistent5.setStatement(sqlstr.toString());
        	persistent6.setStatement(sqlstr.toString());
        	
        	if(!valueObject.get("V1_VENDOR_CD").equals("")){
        		persistent1.addParameter(valueObject.get("V1_SELECT_QTY"));
        		persistent1.addParameter(params.get("BID_NO"));
        		persistent1.addParameter(params.get("BID_DEG"));
        		persistent1.addParameter(valueObject.get("V1_VENDOR_CD"));
        		persistent1.addParameter(valueObject.get("MATERIAL_CD"));
        		persistent1.execute();
        	}
        	if(!valueObject.get("V2_VENDOR_CD").equals("")){
        		persistent2.addParameter(valueObject.get("V2_SELECT_QTY"));
        		persistent2.addParameter(params.get("BID_NO"));
        		persistent2.addParameter(params.get("BID_DEG"));
        		persistent2.addParameter(valueObject.get("V2_VENDOR_CD"));
        		persistent2.addParameter(valueObject.get("MATERIAL_CD"));
        		persistent2.execute();
        	}
        	if(!valueObject.get("V3_VENDOR_CD").equals("")){
        		persistent3.addParameter(valueObject.get("V3_SELECT_QTY"));
        		persistent3.addParameter(params.get("BID_NO"));
        		persistent3.addParameter(params.get("BID_DEG"));
        		persistent3.addParameter(valueObject.get("V3_VENDOR_CD"));
        		persistent3.addParameter(valueObject.get("MATERIAL_CD"));
        		persistent3.execute();
        	}
        	if(!valueObject.get("V4_VENDOR_CD").equals("")){
        		persistent4.addParameter(valueObject.get("V4_SELECT_QTY"));
        		persistent4.addParameter(params.get("BID_NO"));
        		persistent4.addParameter(params.get("BID_DEG"));
        		persistent4.addParameter(valueObject.get("V4_VENDOR_CD"));
        		persistent4.addParameter(valueObject.get("MATERIAL_CD"));
        		persistent4.execute();
        	}
        	if(!valueObject.get("V5_VENDOR_CD").equals("")){
        		persistent5.addParameter(valueObject.get("V5_SELECT_QTY"));
        		persistent5.addParameter(params.get("BID_NO"));
        		persistent5.addParameter(params.get("BID_DEG"));
        		persistent5.addParameter(valueObject.get("V5_VENDOR_CD"));
        		persistent5.addParameter(valueObject.get("MATERIAL_CD"));
        		persistent5.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 doMasterSave(Connection connection, ValueObject valueObject) throws Exception {
		Persistent persistent1 = new PersistentImpl(connection);
		Persistent persistent2 = new PersistentImpl(connection);
		Persistent persistent3 = new PersistentImpl(connection);
		Persistent persistent4 = new PersistentImpl(connection);
		Persistent persistent5 = new PersistentImpl(connection);
		Persistent persistent6 = new PersistentImpl(connection);
		StrBuffer sqlstr = new StrBuffer();
		try {
			
			sqlstr.append("  UPDATE BID_SUPP A				\n");
			sqlstr.append("  SET   A.SELECT_TOT_AMT = (SELECT  SUM(NVL(SELECT_QTY,0)* NVL(UNIT_COST,0)) FROM BID_SUPP_ESTI WHERE BID_NO = A.BID_NO AND BID_DEG = A.BID_DEG AND VENDOR_CD = A.VENDOR_CD)					\n");
			sqlstr.append("        ,  A.WON_SELECT_TOT_AMT = (SELECT  SUM(NVL(SELECT_QTY,0)* NVL(UNIT_COST,0)) FROM BID_SUPP_ESTI WHERE BID_NO = A.BID_NO AND BID_DEG = A.BID_DEG AND VENDOR_CD = A.VENDOR_CD) * DECODE(A.EXCHANGE, 0, 1, NULL, 1, A.EXCHANGE)				\n");
			sqlstr.append("        ,  A.SUCC_YN   =  DECODE((SELECT  SUM(NVL(SELECT_QTY,0)* NVL(UNIT_COST,0)) FROM BID_SUPP_ESTI WHERE BID_NO = A.BID_NO AND BID_DEG = A.BID_DEG AND VENDOR_CD = A.VENDOR_CD), 0 , 'N', 'Y')			\n");
			sqlstr.append("  WHERE A.BID_NO = ?				\n");
			sqlstr.append("  AND   A.BID_DEG = ?				\n");
			sqlstr.append("  AND   A.VENDOR_CD = ?				\n");
			
			
			persistent1.setStatement(sqlstr.toString());
			persistent2.setStatement(sqlstr.toString());
			persistent3.setStatement(sqlstr.toString());
			persistent4.setStatement(sqlstr.toString());
			persistent5.setStatement(sqlstr.toString());
			persistent6.setStatement(sqlstr.toString());
			
			if(!params.get("V1_CD").equals("")){
				persistent1.addParameter(params.get("BID_NO"));
				persistent1.addParameter(params.get("BID_DEG"));
				persistent1.addParameter(params.get("V1_CD"));
				persistent1.execute();
			}
			if(!params.get("V2_CD").equals("")){
				persistent2.addParameter(params.get("BID_NO"));
				persistent2.addParameter(params.get("BID_DEG"));
				persistent2.addParameter(params.get("V2_CD"));
				persistent2.execute();
			}
			if(!params.get("V3_CD").equals("")){
				persistent3.addParameter(params.get("BID_NO"));
				persistent3.addParameter(params.get("BID_DEG"));
				persistent3.addParameter(params.get("V3_CD"));
				persistent3.execute();
			}
			if(!params.get("V4_CD").equals("")){
				persistent4.addParameter(params.get("BID_NO"));
				persistent4.addParameter(params.get("BID_DEG"));
				persistent4.addParameter(params.get("V4_CD"));
				persistent4.execute();
			}
			if(!params.get("V5_CD").equals("")){
				persistent5.addParameter(params.get("BID_NO"));
				persistent5.addParameter(params.get("BID_DEG"));
				persistent5.addParameter(params.get("V5_CD"));
				persistent5.execute();
			}
		}
		catch(Exception e) {
			WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
			WAFLogger.error("SQL : " + sqlstr);
			WAFLogger.error(e);
			throw e;
		}
	}
	
}