/**
 * @(#)file    ContCodeManager.java
 * @(#)author  Kim Myung Ho
 * @(#)version 1.0
 * @(#)date    2014-08-14
 * @(#)since   JDK1.6
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 */

package kr.co.udapsoft.common.code;

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.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.common.ui.HTMLMaker;

public class ContCodeManager {
	
    public static String getCcsifSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String dept_cd, String pay_div) throws Exception {

        return getCcsifSelectCodeList(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, "", null, dept_cd, pay_div);
    }
    public static String getCcsifSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, String eventStr, String dept_cd, String pay_div) throws Exception {

        return getCcsifSelectCodeList(group_cd, inputName, sytleStr, defaultOption, selectedValue, 10, eventStr, null, dept_cd, pay_div);
    }
    /**
     * 파라미터로 받은 공통코드의 코드목록을 select.js를 이용하는 HTML SELECT Tag로 리턴한다.
     * @param group_cd : 공통코드
     * @param inputName : SELECT Tag의 'name' 속성명
     * @return <code>String</code>
     * @throws Exception
     *  
     */
    
    public static String getCcsifSelectCodeList(String group_cd, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String eventStr, String strClassNm, String dept_cd, String pay_div) throws Exception {

        ValueObject rowObject = null;

        ContCodeManager contCodeManager = new ContCodeManager();
        RowSet commCodeRowSet = contCodeManager.getCodeRowSet(group_cd, dept_cd, pay_div);

        return HTMLMaker.getSelectTag(commCodeRowSet, "CODE", "NAME", inputName, sytleStr, defaultOption, selectedValue, size, strClassNm, eventStr);
    }

	
	/*
	 * 증빙구분 : []
	 */
	public static String getProofCdSelectTag(String selId, String style, String eventStr, String defaultOption, String selValue, String chg_code20)	{
		ContCodeManager contCodeManager = new ContCodeManager();    
        RowSet rowSet = contCodeManager.getProofCdRowSet(chg_code20);
        
        return HTMLMaker.getSelectTag(rowSet, "CODE", "NAME", selId, style, defaultOption, selValue, rowSet.size(), null, eventStr);
        //				 getSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, selectedValue, size, strClassNm, null);
	}
	
	/*
	 * 증빙구분 : []
	 */
	public static String getProofCdSelectTag(String selId, String style, String eventStr, String defaultOption, String selValue, String chg_code20, String attributes)	{
		ContCodeManager contCodeManager = new ContCodeManager();    
        RowSet rowSet = contCodeManager.getProofCdRowSet(chg_code20);
        
        return HTMLMaker.getSelectTag(rowSet, "CODE", "NAME", selId, style, defaultOption, selValue, rowSet.size(), attributes, eventStr);
	}

	/*
	 * 사업부에 따른 기성 Pay Group Select Tag 만들기 :
	 */
	public static String getPayGroupSelectTag(String selId, String style, String eventStr, String defaultOption, String selValue, String unitCd)	{
		ContCodeManager contCodeManager = new ContCodeManager();    
        RowSet rowSet = contCodeManager.getPayGroupRowSet(unitCd);
        
        return HTMLMaker.getSelectTag(rowSet, "CODE", "NAME", selId, style, defaultOption, selValue, rowSet.size(), null, eventStr);
	}
	
	public static String getPayGroupSelectTag(String selId, String style, String eventStr, String defaultOption, String selValue, String unitCd, String attributes)	{
		ContCodeManager contCodeManager = new ContCodeManager();    
        RowSet rowSet = contCodeManager.getPayGroupRowSet(unitCd);
        
        return HTMLMaker.getSelectTag(rowSet, "CODE", "NAME", selId, style, defaultOption, selValue, rowSet.size(), attributes, eventStr);
	}

    /**
     * 파라미터로 받은 공통코드의 코드목록을 리턴한다.
     * @param commCode 공통코드
     * @return <code>RowSet</code>
     * @throws Exception
     */
    public static RowSet getCodeRowsetStatic(String group_cd, String dept_cd, String pay_div){

        ContCodeManager contCodeManager = new ContCodeManager();
        RowSet commCodeRowSet = contCodeManager.getCodeRowSet(group_cd, dept_cd, pay_div);
        return commCodeRowSet;
    }
    
    public RowSet getCodeRowSet(String group_cd, String dept_cd, String pay_div) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        String sqlstr = "";
        try {
        	if(group_cd.equals("SUB_ACCT_GB")){
	            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILNM AS NAME, SORT_NO AS SEQ        	\n"
	                   + " FROM      EBID_COM911T       										\n"
	                   + " WHERE     BASECD = ?          										\n"
	            	   + " ORDER BY  SORT_NO              	   									\n";
	            search.setStatement(sqlstr);
	            search.addParameter(group_cd);
        	}else{
        		if(pay_div.equals("1")){
		            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILNM AS NAME, SORT_NO AS SEQ        	\n"
			                   + " FROM      EBID_COM911T       										\n"
			                   + " WHERE     BASECD = ?          										\n"
			                   + " AND CHG_CODE02 = NVL((SELECT PROC_TYP_GB FROM ETEC_ERPIF.ACZ10100 WHERE DEPT_CD = ?),2)	\n"
			            	   + " ORDER BY  SORT_NO              	   									\n";     
		            search.setStatement(sqlstr);
		            search.addParameter(group_cd);
		            search.addParameter(dept_cd);
        		}else{
		            sqlstr = " SELECT    DETAILCD AS CODE ,DETAILNM AS NAME, SORT_NO AS SEQ        	\n"
			                   + " FROM      EBID_COM911T       										\n"
			                   + " WHERE     BASECD = ?          										\n"
			                   + " AND CHG_CODE02 = 2	\n"
			            	   + " ORDER BY  SORT_NO              	   									\n";     
		            search.setStatement(sqlstr);
		            search.addParameter(group_cd);
        		}
        	}
            
            rowSet = search.execute();
            WAFLogger.debug("sql :"+sqlstr);
        }
        catch(Exception e) {

            WAFLogger.error(this.getClass().getName() + " --> getCodeRowSet() : " + e.getMessage());
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
        }
        finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
           
        }
        return rowSet;
    }

	// 증빙구분
	public RowSet getProofCdRowSet(String chg_code20) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        StringBuffer sql = new StringBuffer();
        
        try {
    		sql.append("	SELECT DETAILCD		AS CODE			\n");
    		sql.append("		 , DETAILNM 	AS NAME     	\n");
    		sql.append("		 , CHG_CODE10 	AS VAT_CLASS	\n");
    		sql.append("	  FROM EBID_COM911T            		\n");
    		//sql.append("	 WHERE BASECD 		= '007' 		\n");2018.12.17
    		sql.append("	 WHERE BASECD 		= 'PROOF_CD' 		\n");
    		sql.append("	   AND CHG_CODE20 	= ?   			\n");
    		sql.append("  ORDER BY DETAILCD             		\n");
    
            search.setStatement(sql.toString());
            
            search.addParameter(chg_code20);
            rowSet = search.execute();
            WAFLogger.debug("sql :"+sql);
            
        } catch(Exception e) {
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            
        } finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
        }
        
        return rowSet;
    }
	// 기성 PAY GROUP
	public RowSet getPayGroupRowSet(String unitCd) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        StringBuffer sql = new StringBuffer();
        
        try {
    		sql.append("	SELECT DETAILCD	AS CODE		\n");
    		sql.append("		 , DETAILNM AS NAME     \n");
    		sql.append("	  FROM EBID_COM911T              \n");
    		sql.append("	 WHERE BASECD 		= 'ERP_005' \n");
    		/*sql.append("	 WHERE BASECD 		= '005' \n");2018.12.17
    		
    		if ("10".equals(unitCd)) {	// 생활환경사업부
    			sql.append("	   AND CHG_CODE04 IN ('10', '*')	\n");
    			
    		} else if ("60".equals(unitCd)) {	// 발전사업본부
    			sql.append("	   AND CHG_CODE04 IN ('60', '*')			\n");
    			
    		}
    		else {					// 그 외 사업부
    			sql.append("	   AND CHG_CODE04 IN ('20', '*')   	\n");
    		}*/
    		
    		sql.append("  ORDER BY DETAILCD             \n");
    
            search.setStatement(sql.toString());
            
            rowSet = search.execute();
            
            WAFLogger.debug("sql :"+sql);
            
        } catch(Exception e) {
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            
        } finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
        }
        
        return rowSet;
    }
	
	
	
	
	/* 
	 * @return String	payment terms JSON표현
	 */   
	public String getJSONNotation() {
		
		
		// 직군(중) 구하기
		RowSet payGroup_01 = getPayGroupRowSetSub("01");	// 전도금정산
		RowSet payGroup_02 = getPayGroupRowSetSub("02");	// 현금(계산서)
		RowSet payGroup_11 = getPayGroupRowSetSub("11");	// 비용정산
		RowSet payGroup_21 = getPayGroupRowSetSub("21");   // 선급금
		RowSet payGroup_22 = getPayGroupRowSetSub("22");   // 선급금C
		RowSet payGroup_23 = getPayGroupRowSetSub("23");   // 선급금D
		RowSet payGroup_30 = getPayGroupRowSetSub("30");   // 어음(계산서)
		RowSet payGroup_41 = getPayGroupRowSetSub("41");   // 기성
		RowSet payGroup_42 = getPayGroupRowSetSub("42");   // 기성C
		RowSet payGroup_43 = getPayGroupRowSetSub("43");   // 기성D
		RowSet payGroup_55 = getPayGroupRowSetSub("55");   // 공용법인
		RowSet payGroup_61 = getPayGroupRowSetSub("61");   // 업무가불
		RowSet payGroup_71 = getPayGroupRowSetSub("71");   // LC
		RowSet payGroup_72 = getPayGroupRowSetSub("72");   // LC(C)
		RowSet payGroup_73 = getPayGroupRowSetSub("73");   // 외화송금
		RowSet payGroup_74 = getPayGroupRowSetSub("74");   // LC => 발전
		RowSet payGroup_91 = getPayGroupRowSetSub("91");   // 기타
		
		
		
		String category = "";
		StringBuffer paymentTerms = new StringBuffer();
		paymentTerms = getNotation(	payGroup_01, 
									payGroup_02, 
									payGroup_11, 
									payGroup_21, 
									payGroup_22,
									payGroup_23,
									payGroup_30,
									payGroup_41,
									payGroup_42,
									payGroup_43,
									payGroup_55,
									payGroup_61,
									payGroup_71,
									payGroup_72,
									payGroup_73,
									payGroup_74,
									payGroup_91);
		
		category = "{\"paymentTerms\": {" + paymentTerms + "\n\t}	\n}";
		return category;
	}
	
	/*
     * @purpose category2 내역 구하기
     */
	private StringBuffer getNotation(	RowSet payGroup_01, 
										RowSet payGroup_02, 
										RowSet payGroup_11, 
										RowSet payGroup_21, 
										RowSet payGroup_22,
										RowSet payGroup_23,
										RowSet payGroup_30,
										RowSet payGroup_41,
										RowSet payGroup_42,
										RowSet payGroup_43,
										RowSet payGroup_55,
										RowSet payGroup_61,
										RowSet payGroup_71,
										RowSet payGroup_72,
										RowSet payGroup_73,
										RowSet payGroup_74,
										RowSet payGroup_91) {
		
		StringBuffer category2 = new StringBuffer();
		
		ValueObject row = null;	  
		int size = payGroup_01.size();
		category2.append("\n\t\"01\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_01.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");		
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		
		size = payGroup_02.size();
		category2.append("\n\t\"02\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_02.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_11.size();
		category2.append("\n\t\"11\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_11.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_21.size();
		category2.append("\n\t\"21\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_21.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_22.size();
		category2.append("\n\t\"22\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_22.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_23.size();
		category2.append("\n\t\"23\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_23.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_30.size();
		category2.append("\n\t\"30\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_30.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_41.size();
		category2.append("\n\t\"41\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_41.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_42.size();
		category2.append("\n\t\"42\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_42.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_43.size();
		category2.append("\n\t\"43\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_43.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_55.size();
		category2.append("\n\t\"55\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_55.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_61.size();
		category2.append("\n\t\"61\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_61.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_71.size();
		category2.append("\n\t\"71\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_71.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_72.size();
		category2.append("\n\t\"72\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_72.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_73.size();
		category2.append("\n\t\"73\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_73.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_74.size();
		category2.append("\n\t\"74\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_74.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t],");
		
		size = payGroup_91.size();
		category2.append("\n\t\"91\":[");
		for (int i=0; i<size; i++ ) {
			row = RowSetUtility.getValueObject(payGroup_91.getRow(i));
			category2.append("\n\t\t{");
			category2.append("\"CODE\":\"" + row.get("CODE") + "\" , \"NAME\": \"").append(row.get("NAME")).append("\"}");	
			if (i != (size-1)) {
				category2.append(",");
			}
		}
		category2.append("\n\t]");		
		
		
		return category2;
	}
	
	// 증빙구분
	public RowSet getPayGroupRowSetSub(String chg_code01) {

        Search search = new SearchImpl();
        RowSet rowSet = null;
        StringBuffer sql = new StringBuffer();
        
        try {
    		sql.append("	SELECT CHG_CODE02	AS CODE		\n");
    		sql.append("		 , CHG_CODE03 	AS NAME     \n");
    		sql.append("	  FROM EBID_COM911T            	\n");
    		sql.append("	 WHERE BASECD 		= 'ERP_006' 	\n");  //2018.12.17
    		sql.append("	   AND CHG_CODE01 	= ?   		\n");
    		sql.append("  ORDER BY DETAILCD             	\n");
    
            search.setStatement(sql.toString());
            search.addParameter(chg_code01);
            
            rowSet = search.execute();
            
        } catch(Exception e) {
            WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
            
        } finally {
            if( rowSet == null ) rowSet = new RowSetImpl();
        }
        
        return rowSet;
    }	
}
