/**
 * @(#)file    SuppChoicePopBiz.java
 * @(#)author  이성민
 */
package kr.co.udapsoft.ebid.buyer.quot.purchase.biz;



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.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.ValueObject;

import com.udapsoft.modular.web.ibsheet7.IBSheet7Factory;
import com.udapsoft.waf.system.Biz;
import com.udapsoft.waf.system.HandlerStorage;


public class QuotSuppChoicePopBiz extends Biz {

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

	/* (non-Javadoc)
	 * @see kr.co.hwenc.system.Biz#doBiz()
	 */
	public void doBiz() throws Exception {
		
		WAFLogger.debug("event==============>" + storage.getEvent());
		WAFLogger.debug("params==============>" + params);
		if(storage.compareEvent("")){
			
		}
		else if(storage.compareEvent("GET_CORP_LIST"))
		{
			
	        params.set("sheet1", getList(params));
			storage.addObject("ibsheet7Data", IBSheet7Factory.createIBSheet7(params, storage.request));
		}
	}

	
	/**
     * 협력업체 목록 
     * @param Params
     * @return
     */
    private RowSet getList(ValueObject Params) {
        Search search = new SearchImpl();
        RowSet rowSet = new RowSetImpl();
        StringBuffer sqlStr = new StringBuffer();
        
        try { 
        	sqlStr.append("SELECT A.QUOT_REG_CORP_ID,                                     \n")
		        	.append("       A.JOB_DIV,                                            \n")
		        	.append("       A.IN_OUT_DIV,                                         \n")
		        	.append("       DECODE(A.IN_OUT_DIV,'I','국내','O','국외',A.IN_OUT_DIV) AS IN_OUT_DIV_NM,   \n")
		        	.append("       A.CORP_REG_NO,                                        \n")
		        	.append("       A.CORP_KOR,                                           \n")
		        	.append("       A.REP_NAME,                                           \n")
		        	.append("       A.CORP_TEL,                                           \n")
		        	.append("       A.CHARGE_NAME,                                        \n")
		        	.append("       A.CHARGE_EMAIL,                                       \n")
		        	.append("       A.HP1||A.HP2||A.HP3 AS CHARGE_HP,                     \n")
		        	.append("       A.STATUS_CD,                                          \n")
		        	.append("       A.REMARK,                                             \n")
		        	.append("       DECODE(A.STATUS_CD,'10','사용','미사용') AS STATUS_NM,   \n")
		        	.append("       A.CRE_BY,                                             \n")
		        	.append("       A.CRE_DATE                                            \n")
		        	.append("  FROM QUOT_REG_SUPP_INFO A                                  \n")
		        	.append(" WHERE A.STATUS_CD = '10'	--사용중							  \n");
			
			/*if(!params.get("corp_status").equals(""))
            {
				sqlStr.append("   AND A.JOB_DIV = ?															\n");
            }*/

            if(!params.get("corp_no").equals(""))
            {
                sqlStr.append("   AND A.CORP_REG_NO LIKE('%'|| ? ||'%')                                      \n");
             }
            if(!params.get("corp_name").equals(""))
            { 
            	if(params.get("search_gb").equals("1")){ //업체명 + 의견
            		sqlStr.append("     AND (UPPER(A.CORP_KOR) LIKE ('%'|| ? ||'%') OR UPPER(REMARK) LIKE ('%'|| ? ||'%'))      \n");
            	} else if(params.get("search_gb").equals("2")){ //업체명
            		sqlStr.append("         AND UPPER(A.CORP_KOR) LIKE ('%'|| ? ||'%')        \n");
            	} else { //의견
            		sqlStr.append("         AND UPPER(REMARK) LIKE ('%'|| ? ||'%')             \n");
            	}                
            }
            if(!params.get("in_out_div").equals(""))
            {
                sqlStr.append("   AND A.IN_OUT_DIV = '"+params.get("in_out_div")+"'                                      \n");
             }
            sqlStr.append(" ORDER BY CORP_KOR ASC, QUOT_REG_CORP_ID DESC       									\n");

            
            search.setStatement(sqlStr.toString());
            if(!params.get("corp_status").equals(""))
            {
                search.addParameter(params.get("corp_status"));
            }
            if(!params.get("corp_no").equals(""))
            {
                search.addParameter(params.get("corp_no"));
             }
            if(!params.get("corp_name").equals(""))
            {
            	if(params.get("search_gb").equals("1")){
            		search.addParameter(params.get("corp_name").trim().toUpperCase());  //2015.8.17 대소문자 
            		search.addParameter(params.get("corp_name").trim().toUpperCase());  //2015.8.17 대소문자 
            	} else {
            		search.addParameter(params.get("corp_name").trim().toUpperCase());  //2015.8.17 대소문자 
            	}            	
             }
            
            WAFLogger.debug(sqlStr.toString());
            rowSet = search.execute();
        }
        catch (WAFSQLException se) {
            super.doException(se);
            this.storage.setSuccess(false);
            this.storage.setMessage(se.getMessage());
            storage.setDetailMessage(se);
            se.printStackTrace();
        }
        finally {    
            if(rowSet == null) rowSet = new RowSetImpl();
            return rowSet;
        }
    }
}