/**
 * @(#)file    QuotPlanSaveBiz.java
 * @(#)author  hooonseok choi
 * @(#)date    2023.01.19
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.quot.outside.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.logger.Logger;
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.sender.MessageSend;
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.common.util.StringUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class QuotAmtPopSaveBiz extends Biz { 

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

	public void doBiz() throws Exception {
		boolean success = false;
		
		try {
			super.conn.begin();

			Connection connection = super.conn.getConnection();

			if (storage.compareEvent("UPDATE_QUOT_DETAIL")) {  //저장
				success = updateQuotDetail(connection, params);
				super.storage.setSuccess(success);
			}
		} catch (Exception e) {
		    	System.out.println("doBiz" + e );
				WAFLogger.error(e);
				super.doException(e);
				storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
				super.storage.setMessage(e.getMessage());
				super.storage.setDetailMessage(e);

				super.conn.setRollback();
				super.storage.setSuccess(false);
		} finally {
			super.conn.end();
		}
	}

	public boolean updateQuotDetail(Connection connection, ValueObject params) throws Exception {
		Persistent persistent = null;
		StringBuffer sql = new StringBuffer();

		sql.append("   UPDATE QUOT_BID_SUPP SET 		\n");
		sql.append("		  ESTI_TOT_AMT = '"+StringUtils.replace(params.get("ESTI_TOT_AMT"),",","")+"'   	\n");
		sql.append("		, BID_STATUS_CD = '"+params.get("BID_STATUS_CD")+"'   	\n");
		sql.append("		, REMARK = '"+params.get("REMARK")+"'   				\n");
		sql.append("		, SUBMIT_DT = SYSDATE									\n");
		sql.append("		, UPD_BY = '"+storage.getUser().get("ACCT_ID")+"'		\n");
		sql.append("		, UPD_DATE = SYSDATE                 					\n");
		sql.append("	WHERE QUOT_BID_NO = '"+params.get("QUOT_BID_NO")+"'			\n");
		sql.append("	  AND QUOT_BID_DEG = '"+params.get("QUOT_BID_DEG")+"'		\n");
		sql.append("	  AND VENDOR_CD = '"+params.get("VENDOR_CD")+"'				\n");

		try {
			persistent = new PersistentImpl(connection);
			persistent.setStatement(sql.toString());
			persistent.execute();
			persistent.clearParameters();

			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			super.storage.setMessage(e.getMessage());
			super.storage.setDetailMessage(e);
			throw e;
		}
	}
	
	private String getFileCnt(ValueObject params) {
        
        Search search = new SearchImpl();
        String sqlstr = "";
        try {
            sqlstr 	= 	"select NVL(count(*),0) CNT 							\n"
            		+	"  from pub0500t a, pub0510t b					\n"
		            +	"  where a.file_id = b.file_id					\n"
		            +	"  and substr(b.file_gb,0,13) = 'QUOT_FILE_OUT'	\n"
		            +	"  and id = '"+params.get("QUOT_ID")+"'		\n";

            search.setStatement(sqlstr);
            return search.execute().getRow(0).get("CNT");
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getFileCnt() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
    }

}
