/**
 *
 * @(#)author  [Youngmi Im]
 * @(#)version 1.0
 * @(#)date    May 10, 2007
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.hwenc.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
 package kr.co.udapsoft.ebid.buyer.payment.outside.biz;


import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.sql.RowSet;
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 com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;

public class RejectionPopupInitBiz extends Biz {
	

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

	public void doBiz() throws Exception {

		WAFLogger.debug("반려사유 등록");
		
		RowSet listRowSet = null; 
		boolean success = false;
		
		if (storage.compareEvent("")) {
			
			WAFLogger.debug("prepayId="+params.get("prepayId"));
			WAFLogger.debug("prepayId="+params.get("payreqId"));
			WAFLogger.debug("pageDiv="+params.get("pageDiv"));
			
			//기존 등록된 반려사유 조회
			ValueObject rejectionData = getRejectionData(params);
			storage.addValueObject("rejectionData", rejectionData);	
			
			
		}  else if(storage.compareEvent("SAVE_DATA")){	
			
			success = rejectionData(params);
    		
        	storage.setSuccess(success);
		
		}
	}

    /**
     *@param ValueObject
     *@return ValueObject
     *기   능 : 반려사유조회
     *@author OK
     *@version 2014. 6. 30.
     *@since JDK1.7
     */
    public ValueObject getRejectionData(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet listRowSet = null;
        StringBuffer sql = new StringBuffer();
        ValueObject rowObject = null;
        
        String tableName = "";
        String dataId = "";
        String dataId1 = "";
        
        try {
        	
	        if(params.get("pageDiv").equals("0")){	//0:선급, 1:기성, 2:대금(납품,설치), 3:대금(선급금)
	        	tableName = "SUB2000T A1,   SUB1030T B1, SUB1020T C1, cont_out_supp D1  ";
	        	dataId = "PREPAY_ID";
	        	dataId1 = params.get("prepayId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID																	\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번																		\n");
	      	  	sql.append(" 	       RTN_EMPNM,           --반려자명																		\n");
	      	  	sql.append(" 	       A1.CON_DOC_NO,																						\n");
	      	  	sql.append(" 	       A1.BUILD_NAME,             --계약명																	\n");
	      	  	sql.append(" 	       C1.CORP_KOR,               --계약업체명																\n");
	      	  	sql.append(" 	       A1.DEPT_NAME,              --현장명																	\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_NM AS USER_NAME,   --담당자명																\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_HP AS HP,            --담당자 핸드폰번호													\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_EMAIL AS EMAIL,     --담당자 이메일														\n");
	      	  	sql.append(" 	       D1.USER_NAME  AS RES_USER_NAME																		\n");
	      	  	sql.append(" 	       ,D1.HP AS RES_HP																						\n");
	      	  	sql.append(" 	       ,D1.EMAIL AS RES_EMAIL																				\n");
	      	  	sql.append(" 	       ,D1.DIVISION AS RES_DIVISION																				\n");
	      	  	sql.append(" 	from "+tableName+"																							\n");
	      	  	sql.append("	WHERE A1.SUB_CONT_MOD_ID = B1.SUB_CONT_MOD_ID																\n"); 
	      	  	sql.append("	AND B1.CON_CORP_ID = C1.CON_CORP_ID(+)																		\n"); 
	      	  	sql.append("	AND A1.SUB_CONT_MOD_ID = D1.SUB_CONT_MOD_ID																	\n"); 
	      	  	sql.append("	AND D1.sign_seq = '1'																						\n"); 
	      	  	sql.append("	AND "+dataId+" = '"+ dataId1 +"' -- 기성ID																	\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());
	        	
	        }else if(params.get("pageDiv").equals("1")){
	        	tableName = "SUB1040T";
	        	dataId = "PREPAY_ID";
	        	dataId1 = params.get("prepayId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID										\n");
	      	  	//sql.append(" 	       RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번							\n");
	      	  	sql.append(" 	       RTN_EMPNM           --반려자명								\n");
	      	  	sql.append(" 	from "+tableName+"												\n");
	      	  	sql.append("	WHERE "+dataId+" = '"+ dataId1 +"' -- 기성ID			\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());
	      	  	
	        }else if(params.get("pageDiv").equals("2")){
	        	tableName = "PUR2000T A,  PUR1100T B,    SUPP_INFO D  ,cont_pur_supp E";
	        	dataId = "PAYREQ_ID";
	        	dataId1 = params.get("payreqId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID										\n");
	      	  	//sql.append(" 	       RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번							\n");
	      	  	sql.append(" 	       RTN_EMPNM,           --반려자명								\n");

	      	  	sql.append(" 	       A.ORD_DOC_NO AS CON_DOC_NO,								\n");
	      	  	sql.append(" 	       A.CST_DOC_NAME AS BUILD_NAME,								\n");
	      	  	sql.append(" 	       D.CORP_KOR,								\n");
	      	  	sql.append(" 	       A.DEPT_NAME,							\n");
	      	  	sql.append(" 	       B.TAX_CHARG_NM AS USER_NAME,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_HP AS HP,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_EMAIL AS EMAIL,								\n");
	      	  	sql.append(" 	       E.USER_NAME AS RES_USER_NAME,								\n");
	      	  	sql.append(" 	       E.HP AS RES_HP,								\n");
	      	  	sql.append(" 	       E.EMAIL AS RES_EMAIL,								\n");
	      	  	sql.append(" 	       E.DIVISION AS RES_DIVISION								\n");
	      	  	sql.append(" 	from "+tableName+"												\n");
	      	  	sql.append("	 WHERE A.PUR_CONT_ID = B.PUR_CONT_ID 			\n"); 
	      	  	sql.append("	AND A.CORP_ID = D.CORP_ID		\n"); 
	      	  	sql.append("	AND A.pur_cont_mod_id = E.pur_cont_mod_id		\n"); 
	      	  	sql.append("	AND E.sign_seq = '1'		\n"); 
	      	  	sql.append("	AND "+dataId+" = '"+ dataId1 +"' -- 기성ID			\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());
	        }else if(params.get("pageDiv").equals("20")){	//현장
	        	tableName = "PUR2000T A,  PUR1100T B,    SUPP_INFO D  ,cont_pur_supp E";
	        	dataId = "PAYREQ_ID";
	        	dataId1 = params.get("payreqId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID										\n");
	      	  	//sql.append(" 	       RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번							\n");
	      	  	sql.append(" 	       RTN_EMPNM,           --반려자명								\n");

	      	  	sql.append(" 	       A.ORD_DOC_NO AS CON_DOC_NO,								\n");
	      	  	sql.append(" 	       A.CST_DOC_NAME AS BUILD_NAME,								\n");
	      	  	sql.append(" 	       D.CORP_KOR,								\n");
	      	  	sql.append(" 	       A.DEPT_NAME,							\n");
	      	  	sql.append(" 	       B.TAX_CHARG_NM AS USER_NAME,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_HP AS HP,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_EMAIL AS EMAIL,								\n");
	      	  	sql.append(" 	       E.USER_NAME AS RES_USER_NAME,								\n");
	      	  	sql.append(" 	       E.HP AS RES_HP,								\n");
	      	  	sql.append(" 	       E.EMAIL AS RES_EMAIL,								\n");
	      	  	sql.append(" 	       E.DIVISION AS RES_DIVISION								\n");
	      	  	sql.append(" 	from "+tableName+"												\n");
	      	  	sql.append("	 WHERE A.PUR_CONT_ID = B.PUR_CONT_ID 			\n"); 
	      	  	sql.append("	AND A.CORP_ID = D.CORP_ID		\n"); 
	      	  	sql.append("	AND A.pur_cont_mod_id = E.pur_cont_mod_id		\n"); 
	      	  	sql.append("	AND E.sign_seq = '1'		\n"); 
	      	  	sql.append("	AND "+dataId+" = '"+ dataId1 +"' -- 기성ID			\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());	       
	        }else if(params.get("pageDiv").equals("21")){	//설치
	        	tableName = "PUR2000T A,  PUR1100T B,    SUPP_INFO D  ,cont_pur_supp E";
	        	dataId = "PAYREQ_ID";
	        	dataId1 = params.get("payreqId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID										\n");
	      	  	//sql.append(" 	       RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번							\n");
	      	  	sql.append(" 	       RTN_EMPNM,           --반려자명								\n");

	      	  	sql.append(" 	       A.ORD_DOC_NO AS CON_DOC_NO,								\n");
	      	  	sql.append(" 	       A.CST_DOC_NAME AS BUILD_NAME,								\n");
	      	  	sql.append(" 	       D.CORP_KOR,								\n");
	      	  	sql.append(" 	       A.DEPT_NAME,							\n");
	      	  	sql.append(" 	       B.TAX_CHARG_NM AS USER_NAME,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_HP AS HP,								\n");
	      	  	sql.append(" 	       B.TAX_CHARG_EMAIL AS EMAIL,								\n");
	      	  	sql.append(" 	       E.USER_NAME AS RES_USER_NAME,								\n");
	      	  	sql.append(" 	       E.HP AS RES_HP,								\n");
	      	  	sql.append(" 	       E.EMAIL AS RES_EMAIL,								\n");
	      	  	sql.append(" 	       E.DIVISION AS RES_DIVISION								\n");
	      	  	sql.append(" 	from "+tableName+"												\n");
	      	  	sql.append("	 WHERE A.PUR_CONT_ID = B.PUR_CONT_ID 			\n"); 
	      	  	sql.append("	AND A.CORP_ID = D.CORP_ID		\n"); 
	      	  	sql.append("	AND A.pur_cont_mod_id = E.pur_cont_mod_id		\n"); 
	      	  	sql.append("	AND E.sign_seq = '1'		\n"); 
	      	  	sql.append("	AND "+dataId+" = '"+ dataId1 +"' -- 기성ID			\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());	 	        	
	        }else if(params.get("pageDiv").equals("3")){	//선급금 반려
	        	tableName = "PUR2000T A1, PUR1030T B1, CONT_PUR_SUPP C1";
	        	dataId = "PREPAY_ID";
	        	dataId1 = params.get("prepayId");
	        	
	      	  	sql.append("	SELECT "+dataId+" AS PREPAY_ID,  --기성ID																	\n");
	      	  	sql.append(" 	       REPLACE(RTN_REASON, '<br//>', '\r\n') as RTN_REASON,         --반려사유								\n");
	      	  	sql.append(" 	       to_char(to_date(RTN_DATE, 'yyyymmdd'), 'yyyy-mm-dd') AS RTN_DATE, 	--반류일자						\n");
	      	  	sql.append(" 	       RTN_EMPNO,          --반려사사번																		\n");
	      	  	sql.append(" 	       RTN_EMPNM,           --반려자명																		\n");
	      	  	sql.append(" 	       A1.ORD_DOC_NO AS CON_DOC_NO,																			\n");
	      	  	sql.append(" 	       A1.CST_DOC_NAME AS BUILD_NAME,             --계약명													\n");
	      	  	sql.append(" 	       B1.CORP_KOR,               --계약업체명																\n");
	      	  	sql.append(" 	       A1.DEPT_NAME,              --현장명																	\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_NM AS USER_NAME,   --담당자명  															\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_HP AS HP,            --담당자 핸드폰번호													\n");
	      	  	sql.append(" 	       B1.TAX_CHARG_EMAIL AS EMAIL,     --담당자 이메일														\n");
	      	  	sql.append(" 	       C1.USER_NAME AS RES_USER_NAME, 																		\n");
	      	  	sql.append(" 	       C1.HP AS RES_HP, 																					\n");
	      	  	sql.append(" 	       C1.EMAIL AS RES_EMAIL, 																				\n");
	      	  	sql.append(" 	       C1.DIVISION AS RES_DIVISION																			\n");
	      	  	sql.append(" 	from "+tableName+"																							\n");
	      	  	sql.append("	WHERE A1.PUR_CONT_MOD_ID = B1.PUR_CONT_MOD_ID																\n"); 
	      	  	sql.append("	AND A1.PUR_CONT_MOD_ID = C1.PUR_CONT_MOD_ID																	\n"); 
	      	  	sql.append("	AND C1.sign_seq = '1'																						\n"); 
	      	  	sql.append("	AND B1.PREPAY_ID = '"+ dataId1 +"' -- 기성ID																	\n"); 
	      	
	      	  	search.setStatement(sql.toString());
	      	  WAFLogger.debug(sql.toString());
	        	
	        }
        
            listRowSet = search.execute();
            rowObject = RowSetUtility.getValueObject(listRowSet.getRow(0));
            
        }
        catch(Exception e) {
            WAFLogger.error(this.getClass().getName() + " --> getRejectionpData() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            return null;
        }
        finally {

            if( rowObject == null ) rowObject = new ValueObject();
            return rowObject;
        }
    }

	/**
	 * Persistent 를 이용한 반려 처리
	 * @param params
	 * @return
	 * * 경영정보에서 협력업체에 반려 처리한다.
	 * @param connection
	 * @param pgmName
	 * @param screenName
	 * @param systemCode
	 * @throws Exception
	 */
	private boolean rejectionData(ValueObject params) {
		
		Persistent persistent_u = null;
		String sqlstr_u = "";
	
        String tableName = "SUB1030T";
        
        if(params.get("pageDiv").equals("0")){	//0:선급, 1:기성
        	tableName = "SUB1030T";
        }else if(params.get("pageDiv").equals("1")){
        	tableName = "SUB1040T";
        }
        
		try {
			
			//외주_기성정보
			String prepayId					= params.get("prepayId");					//기성ID
			String statusCd					= "91";									//기성상태값 : 작성상태(01),기성신청요청(02), 기성신청(03), 기성완료(10), 반려(91)
			String userId					= storage.getUser().get("USER_ID");		//로그인사용자
			int userNum						= params.getInt("USER_ID");				//로그인사용자
			String rtnDate					= params.get("rtnDate");				//반려일
			String rtnReason				= params.get("rtnReason");				//반려사유
			
			conn.begin();
			
			persistent_u = new PersistentImpl(super.conn.getConnection());
			
			sqlstr_u = " UPDATE  "+tableName+"   													\n"
					+ " SET    STATUS_CD    			= ?       ,									\n"
					+ "        SUPP_SIGN_DT  			= null    , 					            \n"
					+ "        RTN_REASON  				= ?       , 					            \n"
					+ "        RTN_DATE  				= TO_CHAR(sysdate, 'yyyymmdd') ,	        \n"
					+ "        RTN_EMPNO  				= ?		  , 					            \n"
					+ "        RTN_EMPNM  				= ?		  , 					            \n"
					+ "        UPD_BY      				= ?       , 					            \n"
					+ "        UPD_DATE    				= sysdate									\n"
					+ " WHERE  PREPAY_ID      			= ?        									\n" ;
			
			persistent_u.setStatement(sqlstr_u);
			
			persistent_u.addParameter(statusCd);
			persistent_u.addParameter(rtnReason);
			persistent_u.addParameter(userNum);
			persistent_u.addParameter(userId);
			persistent_u.addParameter(userNum);
			persistent_u.addParameter(prepayId);
			
			WAFLogger.debug(sqlstr_u);
			WAFLogger.debug(persistent_u.getParameters());
			
			persistent_u.execute();
			persistent_u.clearParameters();
			
			return true;
		}
		catch(Exception e) {
			super.doException(e);
			super.storage.setMessage(e.getMessage());
			super.storage.setDetailMessage(e);
			super.conn.setRollback();
			return false;
		}
		finally {
			super.conn.end();
		}	
	}
}
