/**
 * @(#)file    UserListBiz.java
 * @(#)author  Joung Kyu Park
 * @(#)version 1.0
 * @(#)date    May 31, 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.supplier.outside.biz;



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

import kr.co.hsnc.common.base.WAFLogger;
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 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 OutSupplierCompListBiz extends Biz {

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

	
	public void doBiz() throws Exception {

		
		
		WAFLogger.debug("OutSupplierApprListBiz event==============>" + storage.getEvent());
		
        {
//          RowSet acctTypeRowSet = common.getAcctTypeUseRowSet(storage.getUser().get("ACCT_TYPE_CODE"));
//          storage.addRowSet("AcctTypeRowSet", acctTypeRowSet);
            
		    params.set("sheet1", getList(params));
            storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
             
        }
	}
	
    /**
     * 목록을 검색하여 리턴
     * @param Params
     * @return
     */
    private RowSet getList(ValueObject Params) throws Exception {
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlStr = new StringBuffer();
        
        try {
        	
        	
        		sqlStr.append("	SELECT * FROM (																			   \n");
	        	sqlStr.append("         SELECT A.DOC_CD, B.CORP_ID, to_char(to_date(A.APPR_DY), 'yyyy-mm-dd') APPR_DY,	B.CORP_KOR,																																					\n");
	        	sqlStr.append("				   A.APPR_TOT_RATE,																	   \n");
	        	sqlStr.append("                CASE                                                                        \n");
	            sqlStr.append("                    WHEN D.ADM_YEAR = '2022'                                                \n");
	            sqlStr.append("                    THEN                                                                    \n");
	            sqlStr.append("                       CASE                                                                 \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 92 THEN 'S등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 84 THEN 'A등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 76 THEN 'B등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 68 THEN 'C등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE <  68 THEN 'D등급'                          \n");
	            sqlStr.append("                            ELSE '등급외'                                                   \n");
	            sqlStr.append("                       END                                                                  \n");
	            sqlStr.append("                    ELSE                                                                    \n");
	            sqlStr.append("                       CASE                                                                \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 100 THEN 'S등급'                        \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 85 THEN 'A등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 65 THEN 'B등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 45 THEN 'C등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE <  45 THEN 'D등급'                          \n");
	            sqlStr.append("                            ELSE '등급외'                                                   \n");
	            sqlStr.append("                            END                                                            \n");
	            sqlStr.append("                END  ITEM_STD_CD,                                                          \n");
	        	sqlStr.append("         A.ARR_ID, A.DEPT_CD, A.DEPT_NAME, APPR_BY_ID,																																			\n");
	        	sqlStr.append("         F_GET_USER_NM(APPR_BY_ID)       USER_NAME,						\n");
	        	sqlStr.append("         DECODE(B.STATUS, '', 'AA',  '00', '가입요청', '01', '대기', '02', '가등록', '03', '등록', '91', '거래중지', '99', '거래취소', '작성중')  STATUS_CODE,															\n");
	        	sqlStr.append("         C.BUILD_NAME,																																																		\n");
	        	sqlStr.append("         TO_CHAR(C.START_WORK_DATE, 'YYYY-MM-DD') START_WORK_DATE, 																																																		\n");
	        	sqlStr.append("         TO_CHAR(C.END_WORK_DATE, 'YYYY-MM-DD') END_WORK_DATE,																																																		\n");
	        	sqlStr.append("         '1' AS IMG_GB,																																																		\n");
	        	sqlStr.append("         D.ADM_YEAR																																																		\n");
	        	sqlStr.append("         FROM SUPP_APPR A, SUPP_INFO B, SUB1000T C																																											\n");
	        	sqlStr.append("            ,(SELECT DISTINCT ADM_YEAR, CORP_ID, ARR_ID FROM SUPP_APPR_ITEM) D																																																		\n");
	        	sqlStr.append("         WHERE A.CORP_ID = B.CORP_ID																																																	\n");
	        	sqlStr.append("         AND A.ORD_DOC_NO = C.CON_DOC_NO																																														\n");
	        	sqlStr.append("         AND A.DOC_CD = '12'																																																					\n");
	        	sqlStr.append("         AND B.JOB_DIV = 'O'																																																				\n");
	        	sqlStr.append("         AND B.IN_OUT_DIV != 'O'																																																			\n");
	        	sqlStr.append("         AND A.CORP_ID = D.CORP_ID																																														\n");
	        	sqlStr.append("         AND A.ARR_ID = D.ARR_ID																																														\n");	        	
	        	if(params.get("APPR_SEL").equals("A"))
	        	{
	        		sqlStr.append("         AND A.YEAR_STD IN(SELECT DISTINCT CHG_CODE01 FROM EBID_COM911T WHERE BASECD = 'SUPP_REPORT' AND DETAILCD = '12' )																																													\n");
	        	}
	        	else if(params.get("APPR_SEL").equals("B"))
	        	{
	        		sqlStr.append("         AND A.YEAR_STD NOT IN(SELECT DISTINCT CHG_CODE01 FROM EBID_COM911T WHERE BASECD = 'SUPP_REPORT' AND DETAILCD = '12' )																																												\n");
	        	}
	        	sqlStr.append("         UNION ALL																																																			\n");
	        	sqlStr.append("         SELECT A.DOC_CD, B.CORP_ID, to_char(to_date(A.APPR_DY), 'yyyy-mm-dd') APPR_DY,	B.CORP_KOR,																																					\n");
	        	sqlStr.append("				   DECODE(D.ADM_YEAR,'2022',A.APPR_TOT_RATE,ROUND(A.APPR_TOT_RATE/3, 0)) APPR_TOT_RATE,	\n");
	        	sqlStr.append("                CASE                                                                        \n");
	            sqlStr.append("                    WHEN D.ADM_YEAR = '2022'                                                \n");
	            sqlStr.append("                    THEN                                                                    \n");
	            sqlStr.append("                       CASE                                                                 \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 92 THEN 'S등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 84 THEN 'A등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 76 THEN 'B등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE >= 68 THEN 'C등급'                         \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE <  68 THEN 'D등급'                          \n");
	            sqlStr.append("                            ELSE '등급외'                                                   \n");
	            sqlStr.append("                       END                                                                  \n");
	            sqlStr.append("                    ELSE                                                                    \n");
	            sqlStr.append("                       CASE                                                                \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE / 3 >= 90 THEN 'S등급'                     \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE / 3 >= 75 THEN 'A등급'                     \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE / 3 >= 60 THEN 'B등급'                     \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE / 3 >= 45 THEN 'C등급'                     \n");
	            sqlStr.append("                            WHEN A.APPR_TOT_RATE / 3 <  45 THEN 'D등급'                      \n");
	            sqlStr.append("                            ELSE '등급외'                                                   \n");
	            sqlStr.append("                        END                                                                 \n");
	            sqlStr.append("                END  ITEM_STD_CD,                                                       \n");
	        	sqlStr.append("         A.ARR_ID, A.DEPT_CD, A.DEPT_NAME, APPR_BY_ID,																																			\n");
	        	sqlStr.append("         F_GET_USER_NM(APPR_BY_ID)       USER_NAME,						\n");
	        	sqlStr.append("         DECODE(B.STATUS, '', 'AA',  '00', '가입요청', '01', '대기', '02', '가등록', '03', '등록', '91', '거래중지', '99', '거래취소', '작성중')  STATUS_CODE,															\n");
	        	sqlStr.append("         C.BUILD_NAME,																																																		\n");
	        	sqlStr.append("         TO_CHAR(C.START_WORK_DATE, 'YYYY-MM-DD') START_WORK_DATE, 																																																		\n");
	        	sqlStr.append("         TO_CHAR(C.END_WORK_DATE, 'YYYY-MM-DD') END_WORK_DATE,																																																		\n");
	        	sqlStr.append("         '1' AS IMG_GB,																																																		\n");
	        	sqlStr.append("         D.ADM_YEAR																																																		\n");
	        	sqlStr.append("         FROM SUPP_APPR A, SUPP_INFO B, SUB1000T C																																											\n");
	        	sqlStr.append("            ,(SELECT DISTINCT ADM_YEAR, CORP_ID, ARR_ID FROM SUPP_APPR_ITEM) D																																																		\n");
	        	sqlStr.append("         WHERE A.CORP_ID = B.CORP_ID																																																	\n");
	        	sqlStr.append("         AND A.ORD_DOC_NO = C.CON_DOC_NO																																														\n");
	        	sqlStr.append("         AND A.DOC_CD = '16'																																																					\n");
	        	sqlStr.append("         AND B.JOB_DIV = 'O'																																																				\n");
	        	sqlStr.append("         AND B.IN_OUT_DIV != 'O'																																																			\n");
	        	sqlStr.append("         AND A.CORP_ID = D.CORP_ID																																														\n");
	        	sqlStr.append("         AND A.ARR_ID = D.ARR_ID																																														\n");	        	
	        	if(params.get("APPR_SEL").equals("A"))
	        	{
	        		sqlStr.append("         AND A.YEAR_STD IN(SELECT DISTINCT CHG_CODE01 FROM EBID_COM911T WHERE BASECD = 'SUPP_REPORT' AND DETAILCD = '16' )																																													\n");
	        	}
	        	else if(params.get("APPR_SEL").equals("B"))
	        	{
	        		sqlStr.append("         AND A.YEAR_STD NOT IN(SELECT DISTINCT CHG_CODE01 FROM EBID_COM911T WHERE BASECD = 'SUPP_REPORT' AND DETAILCD = '16' )																																												\n");
	        	}
	        	sqlStr.append("         ) A																																														\n");
	        	
	        	sqlStr.append("         WHERE 1=1							\n");
	        	if(!params.get("searchDeptCd").equals(""))
	        	{
	        		sqlStr.append("         AND A.DEPT_CD = '"+params.get("searchDeptCd")+"'																																																				\n");
	        	}
	        	if(!params.get("build_name").equals(""))
	        	{
	        		sqlStr.append("         AND A.BUILD_NAME LIKE '%'"+"||'"+params.get("build_name")+"'||"+"'%'																																													\n");
	        	}
	        	if(!params.get("corp_kor").equals(""))
	        	{
	        		sqlStr.append("         AND A.CORP_KOR LIKE '%'"+"||'"+params.get("corp_kor")+"'||"+"'%'																																														\n");
	        	}
	        	sqlStr.append("         ORDER BY CORP_ID DESC, ARR_ID ASC																																														\n");
        	
            
            search.setStatement(sqlStr.toString());
            WAFLogger.debug(search.getParameters());
            WAFLogger.debug(sqlStr.toString()); 
              
            /*
            //Paging 처리를 위한 Helper Object 생성
            super.createPageAnchorInfo();
            //한페이지에 가져올 목록의 갯수 지정(미지정시  waf.properties 파일의 기본값을 사용)
            //super.anchor.range = 200;
            //Paging 처리에 사용될 Parameter를  세팅
            super.anchor.setParams(this.params);
            //검색할  SQL 세팅 : SQL은 Paging처리를 하지 않고 작성
            super.anchor.setSQL(sqlStr.toString());
            //전체 Record 숫자를 조회
            super.anchor.doTotalCountSQL(); // 전체 숫자를 조회
            
            //Paging처리가 된 SQL을 Helper Object에서 가져옴
            String temp = super.anchor.getPagingSQL();
            
            search.setStatement(temp);
            */
            
            
            rowSet = search.execute();
            
        }
        catch (Exception se) {
        	WAFLogger.error("se====>" + se);
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            
            se.printStackTrace();
            throw new Exception(se);
        }
        finally {    
            if(rowSet == null) rowSet = new RowSetImpl();
            return rowSet;
        }
    }
    
    
    
}