/**
 * @(#)file    WPRListSaveBiz.java
 * @(#)author  gyeongmo an
 * @(#)date    2014.08.28
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */
package kr.co.udapsoft.ebid.buyer.process.process.biz;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.util.ValueObject;

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 WPRListSaveBiz extends Biz {

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

	public void doBiz() throws Exception {
		boolean success = false;
		if( storage.compareEvent("SAVE_WPR_LIST") ){
			String[] status = super.gets("SHEET2.status");
			try {
				super.conn.begin();
				
				for (int i = 0; i < status.length; i++) {
					if (IBSheet7Utils.equalStatusUpdate(status[i])) {
						success = updateWPRList(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET2", params, i));
						psrUpdate(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET2", params, i));
					}
				}
				super.storage.setSuccess(success);
				storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
			} catch (Exception e) {
				WAFLogger.error(e);
				storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
				super.conn.setRollback();
				throw new Exception(e);
			} finally {
				super.conn.close();
			}
			
		}
	}
	

	
	private boolean updateWPRList(Connection connection, ValueObject sqlParams) throws Exception {
        
        PreparedStatement pstmt = null;
 	   StringBuffer sqlstr   = new StringBuffer();
 	   
 	   try {
 		  sqlstr.append("	UPDATE PSR SET         		\n");
      	sqlstr.append("		WEIGHT_FACTOR = ?  		\n");
      	sqlstr.append("		, UPD_BY = ?         		\n");
      	sqlstr.append("		, UPD_DATE = SYSDATE       		\n");
      	sqlstr.append("	WHERE PSR_ID = ?       		\n");
      	sqlstr.append("	AND PSR_ACTIVITY_CD = ?		\n");
 		   
 	        pstmt = connection.prepareStatement(sqlstr.toString());
 	        int paramIdx = 1;
 			pstmt.setString(paramIdx++, sqlParams.get("weight_factor"));
 			pstmt.setString(paramIdx++, storage.getUser().get("ACCT_ID"));
 			pstmt.setString(paramIdx++, sqlParams.get("psr_id"));
 			pstmt.setString(paramIdx++, sqlParams.get("psr_activity_cd"));
 			pstmt.executeUpdate();
 			
 			return true;
         }
         catch(Exception e) {
         	WAFLogger.error(e);
 			throw new Exception(e);
         }
         finally {
         	try {
 				if(pstmt != null) pstmt.close();
 			} catch(SQLException se) {
 				WAFLogger.error(se);
 			}
         }
	}

	public boolean psrUpdate(Connection conn, ValueObject sqlParams) throws Exception {
		CallableStatement cs 	= null;
		ValueObject rtnVo = new ValueObject();
		String pssql			= "";
		
		try {
        	pssql = "{call SP_REG_PSR_STATUS(?,?,?,?,?,?,?,?,?,?,?,?)}";
        	/*
        	 *    VI_DEPT_CD       IN VARCHAR2,         --PROJECT_CD
				   VI_ACTIVITY_CD   IN VARCHAR2,         --PSR_ACTIVITY_CD
				   VI_ACTIVITY_KIND IN VARCHAR2,         --1=PLAN, 2=FORECASTING,3=ACTUAL
				   VI_MR_NO         IN VARCHAR2,         --MR_NO
				   VI_DEMANDED_ID   IN NUMBER,           --DEMANDED_ID
				   VI_PUR_CONT_ID   IN NUMBER,           --PUR_CONT_ID
				   VI_IN_USER_CD    IN NUMBER,           --작성자 사번  
				   VI_ACTIVITY_DATE IN VARCHAR2,         --일자
				   VO_SUCCESS_CD    OUT VARCHAR2,        --성공여부 S:성공 , N:실폐
   					VO_MESSAGE       OUT VARCHAR2
        	 */
        	
        	cs = conn.prepareCall(pssql);
        	
        	for(int i=1; i<4; i++) {
        		cs.setString(1, sqlParams.get("dept_cd"));
            	cs.setString(2, sqlParams.get("psr_activity_cd"));
            	cs.setString(3, i+"");
            	cs.setString(4, sqlParams.get("mr_no"));
            	cs.setInt(5, sqlParams.getInt("demanded_id"));
            	cs.setInt(6, sqlParams.getInt("pur_cont_id"));
            	cs.setString(7, "");
            	cs.setInt(8, storage.getUser().getInt("ACCT_ID"));
            	if(i == 1) {
            		cs.setString(9, sqlParams.get("plan_dt"));
            		
            	}else if (i == 2) {
            		cs.setString(9, sqlParams.get("forecasting_dt"));
            		
            	} else if (i == 3) {
            		cs.setString(9, sqlParams.get("actual_dt"));
            		
            	}
            	cs.registerOutParameter(10, java.sql.Types.VARCHAR);
            	
            	cs.registerOutParameter(11, java.sql.Types.VARCHAR);
                cs.registerOutParameter(12, java.sql.Types.VARCHAR);
            
                /*
            	WAFLogger.debug("~~~~~~~~~~~~~~~~~~~~~~~~");
            	WAFLogger.debug("1 ==> " + sqlParams.get("dept_cd"));
            	WAFLogger.debug("2 ==> " + sqlParams.get("psr_activity_cd"));
            	WAFLogger.debug("3 ==> " + i);
            	WAFLogger.debug("4 ==> " + sqlParams.get("mr_no"));
            	WAFLogger.debug("5 ==> " + sqlParams.getInt("demanded_id"));
            	WAFLogger.debug("6 ==> " + sqlParams.getInt("pur_cont_id"));
            	WAFLogger.debug("7 ==> " + "");
            	WAFLogger.debug("8 ==> " + storage.getUser().getInt("ACCT_ID"));
            	WAFLogger.debug("9 plan ==> " + sqlParams.get("plan_dt"));
            	WAFLogger.debug("9 fore ==> " + sqlParams.get("forecasting_dt"));
            	WAFLogger.debug("9 actu ==> " + sqlParams.get("actual_dt"));
            	WAFLogger.debug("~~~~~~~~~~~~~~~~~~~~~~~~");
            	*/
            	
            	cs.execute();
            	
//            	rtnVo.set("VO_SUCCESS_CD",cs.getString(11));
//                rtnVo.set("VO_MESSAGE",cs.getString(12));
                
        	}
        	
        	

        	
            return true;
        }
        catch(Exception e) {
        	throw e;
        }
        finally {
        	super.conn.end();
        }
	}



	
}