/**
 * @(#)file    PayGuarBiz.java
 * @(#)author  Kim Myung Ho
 * @(#)version 1.0
 * @(#)date    2014-11-20
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.purchase.biz;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.Random;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
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.util.Crosscert;
import kr.co.udapsoft.common.util.PDFMaker;
import kr.co.udapsoft.ebid.buyer.common.file.FileManager;
import oracle.toplink.internal.ox.StrBuffer;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.common.util.StringUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class ContResultInsurSaveBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
		boolean success = false;
		
		if(storage.compareEvent("CHK")){
			success = signCheck("ebid_obappD",params.get("PUR_CONT_MOD_ID"));
			storage.setSuccess(success);
			
		}else if(storage.compareEvent("SAVE_INSUR_DATA")){
			//계약 정보 저장
			success = saveContProc(super.conn.getConnection(), params);
			
			super.storage.setSuccess(success);
		}else if(storage.compareEvent("DEL_INSUR_DATA")){
			//계약 정보 저장
			success = delContProc(params);
			
			super.storage.setSuccess(success);
		}
	}

	
	
	public static boolean signCheck(String signClassCd,String key_col1) throws Exception {
		Search search = new SearchImpl(); 
	 	RowSet rowSet = new RowSetImpl();
	 	StringBuffer sqlstr = new StringBuffer();
	 	String rtnStr = "";
	 	try {
	 		sqlstr.append(" SELECT  COUNT(*) AS CNT                                   		\n");
	 		sqlstr.append("  FROM (  SELECT *                                               \n");
	 		sqlstr.append("          FROM SIGN001T                                    \n");
	 		sqlstr.append("  		 WHERE SIGN_CLASS_CD = '"+signClassCd+"'		        \n");
	 		sqlstr.append("	 		 AND KEY_COL1 = '"+key_col1+"'	 						\n");
	 		sqlstr.append("  		 AND SIGN_DECI_CLASS = '14'                             \n");
	 		sqlstr.append("   		 AND ATTR_6  = '1'                                      \n");
	 		sqlstr.append("  		 UNION ALL                                              \n");
	 		sqlstr.append("  		 SELECT *                                               \n");
	 		sqlstr.append("  		 FROM SIGN001T                                    \n");
	 		sqlstr.append("          WHERE SIGN_CLASS_CD =  '"+signClassCd+"'               \n");
	 		sqlstr.append("          AND KEY_COL1 = '"+key_col1+"'	                        \n");
	 		sqlstr.append("          AND ATTR_6  = '0'                                      \n");
	 		sqlstr.append("       )                                                         \n");
	 		search.setStatement(sqlstr);
	 		rowSet = search.execute();
	 		if(rowSet != null && rowSet.size() > 0 ) {
	 			rtnStr = rowSet.getRow(0).get("CNT");
	 		}
	 		if( !"0".equals(rtnStr) ){
	 			return false;
	        }
	 	}catch(Exception e){
	 		return false;
	 	}
	 	return true;
	}
	
	
	/**
     * 계약 정보 저장
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public boolean saveContProc(Connection connection, ValueObject valueObject) throws Exception {
		
        try {
        	conn.begin();
        	
			insurSave(conn.getConnection(), params);	//ContHTML 등록
			
			return true;
        }
        catch(Exception e) {
        	System.out.println("Exception : " + e.getMessage());
            WAFLogger.error(this.getClass().getName() + " --> addChangeContProc() : " + e.getMessage());
            WAFLogger.error(e);

            conn.setRollback();
            
            this.storage.setSuccess(false);  
            this.storage.setMessage(e.getMessage());            
            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	super.conn.end();
        }        
    }
	
	/**
     * 견적대상업체 총금액 업데이트
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public void insurSave(Connection connection, ValueObject valueObject) throws Exception {
        Persistent persistent = new PersistentImpl(connection);
        StrBuffer sqlstr = new StrBuffer();
        try {
        	sqlstr.append("			UPDATE CONT_PUR_INSUR                 	\n");
        	sqlstr.append("			SET  INSUR_EMPL_YN   = ?,                  			\n");
        	sqlstr.append("			       INSUR_ACC_YN    = ?,                 		\n");
        	sqlstr.append("			       WORK_MNGR_NO    = ?,                 		\n");
        	sqlstr.append("			       BIZ_REG_NO      = ?,                   		\n");
//        	sqlstr.append("			       CORP_REG_NO     = ?,                  		\n");
        	sqlstr.append("			       LCNS_NM         = ?,                     	\n");
        	sqlstr.append("			       LCNS_REG_NO     = ?,                  		\n");
        	sqlstr.append("			       LCNS_ACQT_DATE  = to_date(?, 'YYYYMMDD'),	\n");
        	sqlstr.append("			       MTIL_AMT        = ?,                     	\n");
        	sqlstr.append("			       WORK_PLACE      = ?,                   		\n");
        	sqlstr.append("			       STATUS_CD       = '01',                 		\n");
        	sqlstr.append("			       UPD_BY          = ?,                     	\n");
        	sqlstr.append("			       UPD_DATE        = SYSDATE,                	\n");
        	sqlstr.append("			       SUB_PLACE_NO    = ?                			\n");
        	sqlstr.append("			WHERE  PUR_CONT_MOD_ID = ?							\n");
        	
        	
            persistent.setStatement(sqlstr.toString());
            persistent.addParameter(valueObject.get("INSUR_EMPL_YN"));
            persistent.addParameter(valueObject.get("INSUR_ACC_YN"));
            persistent.addParameter(valueObject.get("WORK_MNGR_NO"));
            persistent.addParameter(valueObject.get("BIZ_REG_NO"));
//            persistent.addParameter(valueObject.get("CORP_REG_NO"));
            persistent.addParameter(valueObject.get("LCNS_NM"));
            persistent.addParameter(valueObject.get("LCNS_REG_NO"));
            persistent.addParameter(valueObject.get("LCNS_ACQT_DATE").replaceAll("-", ""));
            persistent.addParameter(valueObject.get("MTIL_AMT").replaceAll(",", ""));
            persistent.addParameter(valueObject.get("INSUR_WORK_PLACE"));
            persistent.addParameter(storage.getUser().get("ACCT_ID"));
            persistent.addParameter(valueObject.get("SUB_PLACE_NO"));	//2020.07.10 협력사 사업장 관리번호 추가
            persistent.addParameter(valueObject.get("PUR_CONT_MOD_ID"));            
            persistent.execute();
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> insurSave() : " + e.getMessage());
            WAFLogger.error("SQL : " + sqlstr);
            WAFLogger.error(e);
            throw e;
        }
    }
	
	//2017.04.14 고용하수급인 삭제 기능추가
	public boolean delContProc(ValueObject valueObject) throws Exception {
        StrBuffer sqlstr = new StrBuffer();
        try {
        	    super.conn.begin();
            	Persistent persistent = new PersistentImpl(super.conn.getConnection());
        		
        		sqlstr.append("			DELETE                                                                       \n");
        		sqlstr.append("			  FROM CONT_PUR_INSUR                                                        \n");
        		sqlstr.append("			 WHERE PUR_CONT_MOD_ID = '"+valueObject.get("PUR_CONT_MOD_ID")+"'            \n");
        		
        		persistent.setStatement(sqlstr.toString());  
        		persistent.execute();
                return true;
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> insertInsur() : " + e.getMessage());
            WAFLogger.error(e);
            super.conn.setRollback();
            storage.setDetailMessage(e);
            return false;
        } finally {
        	super.conn.end();
        }     
    }
}