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


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

import oracle.toplink.internal.ox.StrBuffer;
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.code.CommCodeManager;
import kr.co.udapsoft.common.util.Crosscert;
import kr.co.udapsoft.common.util.PDFMaker;
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.common.ui.InitCombo;
import com.udapsoft.waf.common.util.StringUtils;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

   
public class ContResultInsertInsurBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();

		boolean success = false;
		if(storage.compareEvent("SAVE_INSUR")){		//고용보험
			success = insertInsur(params);	//고용보험
			super.storage.setSuccess(success);
		}			
	}
	
	/**
     * 고용보험 저장
	 * @param connection
	 * @param valueObject
	 * @throws Exception
	 */
	public boolean insertInsur(ValueObject valueObject) throws Exception {

        StrBuffer sqlstr = new StrBuffer();
        try {
        	conn.begin();
        	
        	String dupCnt = getDupCnt(valueObject.get("PUR_CONT_MOD_ID"));
        	if(dupCnt.equals("0")){
        		Persistent persistent = new PersistentImpl(super.conn.getConnection());
        		
        		
        		sqlstr.append("			INSERT INTO CONT_PUR_INSUR                                                                                          \n");
        		sqlstr.append("			( PUR_CONT_MOD_ID                                                                                                   \n");
        		sqlstr.append("			, PUR_CONT_ID                                                                                                       \n");
        		sqlstr.append("			, WORK_MNGR_NO                                                                                                      \n");
        		sqlstr.append("			, CORP_REG_NO                                                                                                       \n");
        		sqlstr.append("			, BIZ_REG_NO                                                                                                        \n");
        		sqlstr.append("			, WORK_PLACE                                                                                                        \n");
        		sqlstr.append("			, STATUS_CD                                                                                                        \n");
        		sqlstr.append("			, CRE_BY                                                                                                            \n");
        		sqlstr.append("			, CRE_DATE                                                                                                          \n");
        		sqlstr.append("			)SELECT A.PUR_CONT_MOD_ID                                                                                           \n");
        		sqlstr.append("			            , A.PUR_CONT_ID                                                                                         \n");
        		sqlstr.append("			            , (SELECT ORDER_COMP_NO FROM ETEC_ERPIF.ACZ10100 WHERE DEPT_CD = A.DEPT_CD) AS WORK_MNGR_NO                    \n");
        		sqlstr.append("			            , REGEXP_REPLACE(CORP_REG_NO, '^([0-9]{3})-?([0-9]{2})-?([0-9]{5})$', '\\1-\\2-\\3')  AS CORP_REG_NO    \n");
        		sqlstr.append("			            , COPR_NO AS BIZ_REG_NO                                                                                 \n");
        		sqlstr.append("			            , LEAD_PLACE                                                                                            \n");
        		sqlstr.append("			            , '01'                                                                                            		\n");
        		sqlstr.append("			            , '"+storage.getUser().get("acct_id")+"'                                                                \n");
        		sqlstr.append("			            , sysdate                                                                                               \n");
        		sqlstr.append("			 FROM PUR2000T A , CONT_PUR_SUPP B , SUPP_INFO C                                                                    \n");
        		sqlstr.append("			 WHERE A.PUR_CONT_MOD_ID = B.PUR_CONT_MOD_ID                                                                        \n");
        		sqlstr.append("			 AND  B.CORP_ID = C.CORP_ID                                                                                         \n");
        		sqlstr.append("			 AND B.SIGN_SEQ = '2'                                                                                               \n");
        		sqlstr.append("			 AND A.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);
            conn.setRollback();

            storage.setDetailMessage(e);
            return false;
        }
        finally {
        	
            conn.end();
        }
    }
	/**
	 * 			결제중인지 확인 합니다.
	 */
	private String getDupCnt(String purContModId) {

		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    CONT_PUR_INSUR                                                                    	\n");
			sqlstr.append("	WHERE PUR_CONT_MOD_ID = '"+purContModId+"'														\n");
			search.setStatement(sqlstr);
			rowSet = search.execute();
			if(rowSet != null && rowSet.size() > 0 ) {
				rtnStr = rowSet.getRow(0).get("CNT");
			}
		}
		catch (Exception e) {
			super.doException(e);
			this.storage.setSuccess(false);
			this.storage.setMessage(e.getMessage());
			storage.setDetailMessage(e);
		}

		return rtnStr;
	}
}