/**
 * @(#)file    ChgItemBiz.java
 * @(#)author  Kim Myung Ho
 * @(#)version 1.0
 * @(#)date    2014-08-11
 * @(#)since   JDK 1.4.2
 */
package kr.co.udapsoft.ebid.buyer.cont.outside.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 ContItemBiz extends Biz {

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

	public void doBiz() throws Exception {
        ValueObject user = new ValueObject();
		
		boolean success = false;
		
		//변경 내역
		if(storage.compareEvent("")){ 
			
		}else if(storage.compareEvent("SHEET_SELECT")){
			params.set("sheet1", getItemList(params));			
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));			
		}else if(storage.compareEvent("DATA_SAVE")){
		    String[] status = super.gets("sheet1.STATUS");
			
			for (int i = 0; i < status.length; i++) {
				success = UpdateData(super.conn.getConnection(), IBSheet7Utils.getIBSheet7SqlParams("SHEET1", params, i));
			}				
			super.storage.setSuccess(success);
            if(success)
            	storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));		
		}
	}
	/**
	 * 계약  내역 목록
	 * @param Params
	 * @return
	 */
	private RowSet getItemList(ValueObject param) {

		Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        
        StrBuffer sql = new StrBuffer();
        try{
        	
        	sql.append("SELECT  A.CD_CSTR                                                                                     \n");
        	sql.append("        ,A.DL_CSTR                                                                                    \n");
        	sql.append("        ,A.ITEM_NAME                                                                                  \n");
        	sql.append("        ,A.SUPPLY_AMT                                                                                 \n");
        	sql.append("        ,A.SUPPLY_TAMT                                                                                \n");
        	sql.append("        ,A.SUPPLY_NAMT                                                                                \n");
        	sql.append("        ,B.SUPPLY_AMT AS B_SUPPLY_AMT                                                                 \n");   
        	sql.append("        ,B.SUPPLY_TAMT AS B_SUPPLY_TAMT                                                               \n");    
        	sql.append("        ,B.SUPPLY_NAMT AS B_SUPPLY_NAMT                                                               \n");
        	sql.append("        ,( SELECT SUM(AA.CUR_TAMT)                                                                    \n"); 
        	sql.append("            FROM SUB1041T AA                                                                          \n");
        	sql.append("                ,SUB1040T BB                                                                          \n");
        	sql.append("           WHERE AA.ACMP_ID = BB.ACMP_ID                                                              \n");
        	sql.append("             AND BB.STATUS_CD = '10'                                                                  \n");
        	sql.append("             AND AA.SUB_CONT_ID = A.SUB_CONT_ID                                                       \n");
        	sql.append("             AND AA.CD_CSTR = A.CD_CSTR                                                               \n");
        	sql.append("             AND AA.DL_CSTR = A.DL_CSTR                                                               \n");
        	sql.append("        ) FUND_TAMT                                                                                   \n");
        	sql.append("       ,( SELECT SUM(AA.CUR_NAMT)                                                                     \n");
        	sql.append("            FROM SUB1041T AA                                                                          \n");
        	sql.append("                ,SUB1040T BB                                                                          \n");
        	sql.append("           WHERE AA.ACMP_ID = BB.ACMP_ID                                                              \n");
        	sql.append("             AND BB.STATUS_CD = '10'                                                                  \n");
        	sql.append("             AND AA.SUB_CONT_ID = A.SUB_CONT_ID                                                       \n");
        	sql.append("             AND AA.CD_CSTR = A.CD_CSTR                                                               \n");
        	sql.append("             AND AA.DL_CSTR = A.DL_CSTR                                                               \n");
        	sql.append("        ) FUND_NAMT                                                                                   \n");
        	sql.append("        ,A.SUB_CONT_MOD_ID                                                                            \n"); 
        	sql.append("        ,A.SUB_CONT_ID                                                                                \n"); 
        	sql.append("        ,A.MATERIAL_CD                                                                                \n");
        	sql.append("        ,DECODE(C.CON_SUPPLY_TAMT, 0, 0, C.CON_SUPPLY_TAMT / C.CON_SUPPLY_AMT) AS SUPP_RATE           \n");
        	sql.append("FROM    CONT_OUT_ITEM A                                                                               \n");
        	sql.append("        ,CONT_OUT_ITEM B                                                                              \n");
        	sql.append("        ,SUB2000T C                                                                                   \n");
        	sql.append("WHERE   A.SUB_CONT_ID = B.SUB_CONT_ID(+)                                                              \n");
        	sql.append("AND     A.CD_CSTR = B.CD_CSTR(+)                                                                      \n");
        	sql.append("AND     A.DL_CSTR = B.DL_CSTR(+)                                                                      \n");
        	sql.append("AND     B.SUB_CONT_MOD_ID(+) = F_GET_LAST_SUB_CONT_MOD_ID(A.SUB_CONT_MOD_ID)                          \n");
        	sql.append("AND     A.SUB_CONT_MOD_ID = C.SUB_CONT_MOD_ID                                                         \n");
        	sql.append("AND     A.SUB_CONT_MOD_ID = "+params.get("SUB_CONT_MOD_ID")+"                                         \n");
		    
            search.setStatement(sql.toString());                                                          
            WAFLogger.debug(sql.toString());                                                              
			
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);  
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
        }
        finally {    
            if(rowSet == null){
            	rowSet = new RowSetImpl();
            }
        }
        return rowSet;
	}
	
	public boolean UpdateData(Connection connection, ValueObject sqlParams) throws Exception {
		Persistent persistent = new PersistentImpl(connection);
		StringBuffer sql = new StringBuffer();
		
		try {
        	sql.append("UPDATE CONT_OUT_ITEM	          \n");
            sql.append("   SET SUPPLY_TAMT = ?	          \n");
            sql.append("      ,SUPPLY_NAMT = ?	          \n");
            sql.append("      ,SUPPLY_AMT = ?	          \n");
            sql.append("      ,UPD_BY	   = ?            \n");
            sql.append("      ,UPD_DATE	   = SYSDATE      \n");
            sql.append(" WHERE SUB_CONT_MOD_ID = ?	      \n"); 
            sql.append("   AND MATERIAL_CD     = ?	      \n");			

			persistent.setStatement(sql.toString());
			
			persistent.addParameter(sqlParams.get("SUPPLY_TAMT"));
			persistent.addParameter(sqlParams.get("SUPPLY_NAMT"));
			persistent.addParameter(sqlParams.get("SUPPLY_AMT"));
			persistent.addParameter(storage.getUser().getInt("ACCT_ID"));
			persistent.addParameter(params.get("SUB_CONT_MOD_ID"));
			persistent.addParameter(sqlParams.get("MATERIAL_CD"));
			
			persistent.execute();
			return true;
		}         
			catch(Exception e) {
			storage.addObject("ibsheet7Data", IBSheet7Utils.exceptionHandle(storage.request, e));
            WAFLogger.error(this.getClass().getName() + " --> updUser() : " + e.getMessage());
            WAFLogger.error("SQL : " + sql);
            WAFLogger.error(e);
            throw e;
        }
	}	
}


