package kr.co.kihyun.beans.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;
import java.util.Arrays;

import java.sql.Blob;

import kr.co.kihyun.beans.entity.MDept;
import kr.co.kihyun.beans.entity.MUser;
import kr.co.kihyun.beans.entity.MoumiEntity;
import kr.co.kihyun.beans.entity.SysAuth;
import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.beans.entity.TotReportType;

import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import kr.co.kihyun.beans.entity.ecross.IRemotePersistable;
import kr.co.kihyun.beans.entity.util.QueryImpl;
import kr.co.kihyun.io.IUploadable;
import kr.co.kihyun.lang.MString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.db.*;
import java.sql.*;
import kr.co.kihyun.moumi.MoumiConfig;


public class MultiCheck_forDevServer extends DBManager {

  private static final Logger LOG = LoggerFactory.getLogger(MultiCheck.class);

         int cnt=0;
         int count = 0;
         String[] array;
         String[] nameList;  
         String[] getPassword;
         String[] deptID;
         String[] userId;
         String[] deptName;
         String[] adpInsttCode;
         String[] pNo;
         String[] kraOrgNm;

         int tcount = 0;
         ResultSet rs;
         Connection con;
         Statement stmt;
         ResultSetMetaData rsmd;
         int start = 0;
         int limit = 30;
       //int i = -1;

       /*
           String url = "jdbc:db2://10.200.30.141:43000/GACKWD";   //개발
         //String url = "jdbc:db2://10.102.40.22:41000/GACKWD";  //부산
         //String url = "jdbc:db2://10.103.40.203:11654/GACKWD"; //대구
         //String url = "jdbc:db2://10.104.40.203:11654/GACKWD"; //인천
         //String url = "jdbc:db2://10.105.40.22:41000/GACKWD";  //광주
         //String url = "jdbc:db2://10.106.40.22:41000/GACKWD";  //대전
         //String url = "jdbc:db2://10.107.40.22:41000/GACKWD";  //울산
         //String url = "jdbc:db2://10.108.40.203:11654/GACKWD"; //경기
         //String url = "jdbc:db2://10.109.40.203:11654/GACKWD"; //강원
         //String url = "jdbc:db2://10.210.40.22:41000/GACKWD";  //충북
         //String url = "jdbc:db2://10.111.40.23:41000/GACP";    //충남
         //String url = "jdbc:db2://10.112.40.203:11654/GACKWD"; //전북
         //String url = "jdbc:db2://10.113.40.23:41000/GACP";    //전남
         //String url = "jdbc:db2://10.114.40.203:11654/GACKWD"; //경북
         //String url = "jdbc:db2://10.115.50.33:11654/GACKWD";  //경남
         //String url = "jdbc:db2://10.116.40.21:41000/GACKWD";  //제주
         //String url = "jdbc:db2://10.200.90.248:17221/GACKWD"; //세종

         String loginTimeout="10";
         String maxActive="100";
         String maxIdle="25";
         String maxWait="10";
         String user="gacdbmgr";
         String password = "gacdbmgr";
     */

    public void IdCheck(String id) throws Exception{
        StringBuilder sql = new StringBuilder(250);       
        FileReader in = null;
           
        try {
       
            //FileReader in = new FileReader("/edudocs/wasapps/atswas/gackwd.txt");
            in = new FileReader("/edudocs/wasapps/atswas/gackwd.txt");
            
            int c;
            String s = new String();

            while((c=in.read()) != -1){
                s = s + (char)c;
                //System.out.println("test1:::::"+s);
            }

            String[] data = s.split(",");
            String[] subData=null;

            String connection1 = data[0];
            String connection2 = data[1];
            String connection3 = data[2];         
            String loginTimeout="10";
            String maxActive="100";
            String maxIdle="25";
            String maxWait="10";

            String url = connection1;   //개발
            String user = connection2;
            String password = connection3;

            //System.out.println("::url::"+url+"\n::user:"+user+"\n:::password::"+password);
            //System.out.println(":::s::::"+s);
            
            Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
            con = DriverManager.getConnection(url,user,password);

            //System.out.println("get Statement Object");

            sql.append(" select us.USRID, dual.adp_instt_code, dual.adp_instt_NM, dual.p_no ")
                    .append(" from  TN_GPSEGOAD001 dual, TN_SYMUSMUM001 us, TC_SYMOCMOC000 dt where ")
                    .append(" us.P_NO = dual.P_NO  and us.PSITN_ORG_CODE = dt.ORG_CODE ")
                    .append(" and dual.ADP_RELIS_PARNG_YMD >= (select to_char(current date,'YYYYMMDD') from SYSIBM.SYSDUMMY1) ")
                    .append(" AND us.USRID='"+id+"' union ")
                    .append(" select  us.USRID, dual.DSPTC_DEPT_CODE, dual.dsptc_dptnm, dual.p_no  ")
                    .append(" from  TN_GPSEGODS001 dual, TN_SYMUSMUM001 us, TC_SYMOCMOC000 dt where us.P_NO = dual.P_NO ")
                    .append(" and us.PSITN_ORG_CODE = dt.ORG_CODE  and dual.DSPTC_END_PARNG_YMD >= (select to_char(current date,'YYYYMMDD') from SYSIBM.SYSDUMMY1) ")
                    .append(" AND us.USRID='"+id+"'  union   select us.USRID, dual.adp_instt_code, dual.adp_instt_NM, dual.p_no ")
                    .append(" from TN_TPSEGOMR002 dual, TN_SYMUSMUM001 us, TC_SYMOCMOC000 dt where  us.P_NO = dual.P_NO  and us.PSITN_ORG_CODE = dt.ORG_CODE ")
                    .append(" and dual.ADP_RELIS_PARNG_YMD >= (select to_char(current date,'YYYYMMDD') from SYSIBM.SYSDUMMY1) AND us.USRID='"+id+"'  union  ")
                    .append(" select  us.USRID, dual.DSPTC_DEPT_CODE, dual.dsptc_dptnm, dual.p_no from TN_TPSEGOTC001 dual, TN_SYMUSMUM001 us, TC_SYMOCMOC000 dt ")
                    .append(" where us.P_NO = dual.P_NO  and us.PSITN_ORG_CODE = dt.ORG_CODE  and dual.DSPTC_END_PARNG_YMD >= (select to_char(current date,'YYYYMMDD') from SYSIBM.SYSDUMMY1) ")
                    .append(" AND us.USRID='"+id+"'")
                    .append(" union ")
                    .append(" select us.USRID,dt.ORG_CODE,dt.KRA_ORG_NM,us.P_NO  from TN_SYMUSMUM001 us, TC_SYMOCMOC000 dt ")
                    .append(" where us.PSITN_ORG_CODE = dt.ORG_CODE  AND us.USRID='"+id+"'");

            PreparedStatement stmt = con.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            stmt.setFetchSize(limit);

            rs = stmt.executeQuery();
            //System.out.println("get Result Object:::"+sql.toString());

            if (rs != null) {
                rs.last();
                tcount = rs.getRow();
                rs.beforeFirst();
            } else {
                return;
            }

            userId       = new String[tcount];
            adpInsttCode = new String[tcount];
            deptName     = new String[tcount];
            pNo          = new String[tcount];

            for (int i = 0; rs.next(); i++) {
                userId[i]  = rs.getString(1);
                adpInsttCode[i] = rs.getString(2);
                deptName[i] = rs.getString(3);
                pNo[i] = rs.getString(4);
                //System.out.println(" for grammer::::::"+userId[i]+","+adpInsttCode[i]+","+ deptName[i]+","+pNo[i]+"::i값::"+i);
            }
        } catch(ClassNotFoundException cnfex) {
            cnfex.printStackTrace();
        } catch(IOException ioex) {
            ioex.printStackTrace();
        } catch(IllegalAccessException iaex) {
            iaex.printStackTrace();
        } catch(InstantiationException instex) {
            instex.printStackTrace();
        } catch(SQLException sqlex) {
            sqlex.printStackTrace();
        } finally {
            //close(rs);
            //execClose();
            try {
                //v2 21.부적절한 자원 해제 (IO)_CWE-404 : Add by YOUNGJUN,CHO
                // properties 파일을 읽은 후, finally 에서 닫도록 수정함.
                if(in != null) {in.close();}
                //++++++++++++++++++++++++++++++++++++++++++++++++
                if(rs!=null) {rs.close();}
                if(stmt!=null) {stmt.close();}
                if(con!=null) {con.close();}
            } catch (IOException ioex) {  
                ioex.printStackTrace();
            } catch (SQLException sqlex) {  
                sqlex.printStackTrace();
            } catch (Exception e) {  
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                e.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
            }
        }   
    }

  

    public int deptUpdate(String id,String DeptID) throws SQLException{
		String sql = "";
		int i=0;
		try{
			sql="UPDATE MOUMI_MUSER SET DEPT_ID = '"+DeptID+"' WHERE ID = '"+id+"'";
			i=execUpdate(sql);
            //System.out.println(sql);
		}catch(SQLException sqlEx){
                        //18.시스템 데이터 정보노출 : Update by YOUNGJUN,CHO
			//System.out.println(SQLMessage.getMessage(sqlEx, "MultiCheck.deptUpdate(String, String)", sql));
                        sqlEx.printStackTrace();
                        //================================================
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{     
            //close(rs);
			//execClose();
            try {
                  if(rs!=null) {rs.close();}
                  if(stmt!=null) {stmt.close();}
                  if(con!=null) {con.close();}
             } catch (Exception e) {  
                //31.오류 상황 대응 부재_CWE-390 Add by YOUNGJUN,CHO
                e.printStackTrace();
                //++++++++++++++++++++++++++++++++++++++++++++++++
             }

		}
		return i;
	}

    public String[] getNameList() {
		return nameList;
	}

    //dept_id
    public String[] getDeptID() {
		return adpInsttCode;
	}

    //user_id
     public String[] getUserId() {
		return userId;
	}

     //dept_name
    public String[] getDeptName() {
		return deptName;
	}

    //p_No
    public String[] getPno() {
		return pNo;
	}
  

    public int getCount(){
          return tcount;
      }

}
