package kr.co.kihyun.beans.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
//import java.io.ByteArrayOutputStream;
//import java.io.ByteArrayInputStream;
//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.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 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;


public class AllReportList  extends DBManager {

    private static final Logger LOG = LoggerFactory.getLogger(AllReportList.class);
    private ResultSet rs = null;
    private int count = 0;
    private String[] nameList = null;
    private String[] nameUp   = null;
    private String[] nameDept = null;
    private String[] ownerName = null;
    private String[] phone = null;
    private String[] submitDate = null;
    private String[] founDation = null;
    private String upperDept = null;
    private int[] prss = null;
    private int[] downDocId = null;
    private byte[] attachments = null;
    private int tCount;
    private String[] nullNameup = null;

    //새로 추가 문정호
    private int grade = 0;
    private String organ = null;
    private String[] id = null;
    private String[] execCode = null;
    private Date[] startDate = null;
    private Date[] endDate = null;
    private int[] docId = null;
    private String[] docOwnerName=null;
    private String[] rptOwnerName = null;
    private String[] UserId = null;
    private Date[] SubmitDate = null;
    private int[] moumiacptLimitstrategy = null;
    private int[] Appro = null;
    private int[] acprss = null;
    private int sysAuth;

  /*private String[] data = null;
    private String[] totData = null;*/	  

     /******** 카테고리 전체 리스트 ********/
	public void executeQuery(String dtID,String findOption,Object filterValue) {
		    StringBuilder sql = new StringBuilder(250);
        try {
              if(filterValue != null && findOption == null){
                 sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append(" FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append(" WHERE  ")
                     .append("       RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }else if(filterValue != null && findOption.equals("totDoc.name")){
                  sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append(" FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append(" WHERE  ")
                     .append("      RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append("  and dc.name like '%"+filterValue+"%'")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }else if(filterValue != null && findOption.equals("totDoc.executionCode")){
                  sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append("FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append("WHERE  ")
                     .append("      RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append("  and dc.exec_code like '%"+filterValue+"%'")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }else if(filterValue != null && findOption.equals("totDoc.mUser.name")){
                  sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append("FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append("WHERE  ")
                     .append("      RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append("  and  dc.owner_name = sp_cm_enc_fu('"+filterValue+"')")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }else if(filterValue != null && findOption.equals("totDoc.mUser.name2")){
                  sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append("FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append("WHERE  ")
                     .append("      RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append("  and  rt.owner_name = sp_cm_enc_fu('"+filterValue+"')")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }else{
                  sql.append("SELECT  ")
                     .append(" COALESCE(DC.EXEC_CODE,'&nbsp;') ")
                     .append(",DC.NAME  ")
                     .append(",DT.NAME  ")
                     .append(",SP_CM_DEC_FU(DC.OWNER_NAME)  ")
                     .append(",SP_CM_DEC_FU(RT.OWNER_NAME)  ")
                     .append(",DC.START_DATE  ")
                     .append(",DC.END_DATE  ")
                     .append(",RT.PRSS  ")
                     .append(",COALESCE(RT.DOWN_DOC_ID,0) ")
                     .append(",RT.ID  ")
                     .append(",RT.USER_ID  ")
                     .append(",RT.SUBMIT_DATE  ")
                     .append(",AC.SUBMIT_LIMIT_CNT  ")
                     .append(",RT.APPRO    ")
                     .append(",AC.PRSS    ")
                     .append("FROM  ")
                     .append("   MOUMI_TOT_DOC DC ")
                     .append("  ,MOUMI_TOT_REPORT RT ")
                     .append("  ,MOUMI_DEPT DT ")
                     .append("  ,MOUMI_ACPT_LIMIT_STRATEGY AC  ")
                     .append("WHERE  ")
                     .append("      RT.DOC_ID = DC.ID  ")
                     .append("   AND DC.DEPT_ID = DT.ID  ")
                     .append("   AND DC.DEL_TYPE='N'   ")
                     .append("   AND RT.DEL_TYPE='N'  ")
                     .append("   AND RT.PRSS IN (4,5)    ")
                     .append("   AND DC.ACPT_LIMIT = AC.ID   ")
                     .append("   AND RT.DEPT_ID = '"+dtID+"'   ")
                     .append(" ORDER BY CASE WHEN DC.END_DATE < SYSDATE THEN 0 ELSE 1 END DESC,DC.END_DATE ASC ");
                }

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

                execCode  = new String[count];
                nameList  = new String[count];
                nameUp    = new String[count];
                docOwnerName = new String[count];
                rptOwnerName = new String[count];
                startDate = new Date[count];
                endDate   = new Date[count];
                prss      = new int[count];
                downDocId = new int[count];
                id        = new String[count];
                UserId    = new String[count];
                SubmitDate= new Date[count];
                moumiacptLimitstrategy = new int[count];
                Appro = new int[count];
                acprss = new int[count];
                tCount    = count;

			for (int i = 0; rs.next(); i++) {
				execCode[i] = rs.getString(1);
				nameList[i] = rs.getString(2);
				nameUp[i] = rs.getString(3);
                docOwnerName[i] = rs.getString(4);
                rptOwnerName[i] = rs.getString(5);
				startDate[i] = rs.getDate(6);
				endDate[i] = rs.getDate(7);
                prss[i] = rs.getInt(8);
                downDocId[i] =  rs.getInt(9);
                id[i]        = rs.getString(10);
                UserId[i]    = rs.getString(11);
                SubmitDate[i]= rs.getDate(12);
                moumiacptLimitstrategy[i] = rs.getInt(13);
                Appro[i] = rs.getInt(14);
                acprss[i] = rs.getInt(15);
            }
            //System.out.println(sql+"\nfindOption:::::"+findOption+"\nfilterValue::::::"+filterValue+" tCount:::"+tCount);

		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage() });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}


    public void getSysAuth(String id) {
		StringBuilder sql = new StringBuilder(150);
		try {
			sql.append("select sys_auth from moumi_muser where ID = '"+id+"'");
			rs = execQuery(sql);
			if (rs.next()) {
				sysAuth = rs.getInt(1);
			}
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}


    public String[] getNameList() {
            //24.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by KWON,HAN
 	    //	return nameList;
                
            String[] ret = null;
            if( this.nameList != null) {
                ret = new String[nameList.length];
                for (int i=0; i<nameList.length; i++) {
                    ret[i] = this.nameList[i];
                }
            }
            return ret;
            //====================================================================                
	}

     public String[] getNameUp() {
            //    return nameUp;
                
            String[] ret = null;
            if( this.nameUp != null) {
                ret = new String[nameUp.length];
                for (int i=0; i<nameUp.length; i++) {
                    ret[i] = this.nameUp[i];
                }
            }
            return ret;                
	}

    public String[] getNameDept() {
	    //	return nameDept;
                
            String[] ret = null;
            if( this.nameDept != null) {
                ret = new String[nameDept.length];
                for (int i=0; i<nameDept.length; i++) {
                    ret[i] = this.nameDept[i];
                }
            }
            return ret;                 
	}

    public String[] getOwnerName() {
	    //	return ownerName;
                
            String[] ret = null;
            if( this.ownerName != null) {
                ret = new String[ownerName.length];
                for (int i=0; i<ownerName.length; i++) {
                    ret[i] = this.ownerName[i];
                }
            }
            return ret;                
	}

    public String[] getId() {
	    //	return id;
                
            String[] ret = null;
            if( this.id != null) {
                ret = new String[id.length];
                for (int i=0; i<id.length; i++) {
                    ret[i] = this.id[i];
                }
            }
            return ret;                 
	}

    public String[]  getPhone(){
        //  return phone;
          
        String[] ret = null;
        if( this.phone != null) {
            ret = new String[phone.length];
            for (int i=0; i<phone.length; i++) {
                ret[i] = this.phone[i];
            }
        }
        return ret;           
      }

    public String[]  getFounDation(){
        //  return founDation;
          
        String[] ret = null;
        if( this.founDation != null) {
            ret = new String[founDation.length];
            for (int i=0; i<founDation.length; i++) {
                ret[i] = this.founDation[i];
            }
        }
        return ret;          
      }

     public int[]  getDowndocid(){
        //  return downDocId;
          
        int[] ret = null;
        if( this.downDocId != null) {
            ret = new int[downDocId.length];
            for (int i=0; i<downDocId.length; i++) {
                ret[i] = this.downDocId[i];
            }
        }
        return ret;          
      }

    public String[]  getDocOwnername(){
        //  return docOwnerName;
          
        String[] ret = null;
        if( this.docOwnerName != null) {
            ret = new String[docOwnerName.length];
            for (int i=0; i<docOwnerName.length; i++) {
                ret[i] = this.docOwnerName[i];
            }
        }
        return ret;          
      }

     public String[]  getRptOwnername(){
        //  return rptOwnerName;
          
        String[] ret = null;
        if( this.rptOwnerName != null) {
            ret = new String[rptOwnerName.length];
            for (int i=0; i<rptOwnerName.length; i++) {
                ret[i] = this.rptOwnerName[i];
            }
        }
        return ret;           
      }
 
    public int  getGrade(){
          return grade;
      }

   public int[]  getPrss(){
        //  return prss;
          
        int[] ret = null;
        if( this.prss != null) {
            ret = new int[prss.length];
            for (int i=0; i<prss.length; i++) {
                ret[i] = this.prss[i];
            }
        }
        return ret;          
      }

  public String  getOrgan(){
          return organ;
      }

  public String  getUpperDept(){
          return upperDept;
      }
  

   public byte[]  getAttachments(){
        //  return attachments;
          
        byte[] ret = null;
        if( this.attachments != null) {
            ret = new byte[attachments.length];
            for (int i=0; i<attachments.length; i++) {
                ret[i] = this.attachments[i];
            }
        }
        return ret;          
      }

   public int  getCount(){
          return tCount;
      }

   public String[]  getNullnameup(){
        //  return nullNameup;
        
        String[] ret = null;
        if( this.nullNameup != null) {
            ret = new String[nullNameup.length];
            for (int i=0; i<nullNameup.length; i++) {
                ret[i] = this.nullNameup[i];
            }
        }
        return ret;
     }

    public String[]  getExeccode(){
        //  return execCode;
          
        String[] ret = null;
        if( this.execCode != null) {
            ret = new String[execCode.length];
            for (int i=0; i<execCode.length; i++) {
                ret[i] = this.execCode[i];
            }
        }
        return ret;          
      }
    public Date[]  getStartdate(){
        //  return startDate;
          
        Date[] ret = null;
        if( this.startDate != null) {
            ret = new Date[startDate.length];
            for (int i=0; i<startDate.length; i++) {
                ret[i] = this.startDate[i];
            }
        }
        return ret;           
      }
    public Date[]  getEnddate(){
        //  return endDate;
          
        Date[] ret = null;
        if( this.endDate != null) {
            ret = new Date[endDate.length];
            for (int i=0; i<endDate.length; i++) {
                ret[i] = this.endDate[i];
            }
        }
        return ret;          
      }

   public int[]  getDocid(){
        //  return docId;
          
        int[] ret = null;
        if( this.docId != null) {
            ret = new int[docId.length];
            for (int i=0; i<docId.length; i++) {
                ret[i] = this.docId[i];
            }
        }
        return ret;           
      }

   public String[]  getUserId(){
        //  return UserId;
          
        String[] ret = null;
        if( this.UserId != null) {
            ret = new String[UserId.length];
            for (int i=0; i<UserId.length; i++) {
                ret[i] = this.UserId[i];
            }
        }
        return ret;          
      }

   public Date[]  getSubmitDate(){
        //  return SubmitDate;
          
        Date[] ret = null;
        if( this.SubmitDate != null) {
            ret = new Date[SubmitDate.length];
            for (int i=0; i<SubmitDate.length; i++) {
                ret[i] = this.SubmitDate[i];
            }
        }
        return ret;            
      }

   public int[]  getMoumiAcptLimitStrategy(){
        //  return moumiacptLimitstrategy;
          
        int[] ret = null;
        if( this.moumiacptLimitstrategy != null) {
            ret = new int[moumiacptLimitstrategy.length];
            for (int i=0; i<moumiacptLimitstrategy.length; i++) {
                ret[i] = this.moumiacptLimitstrategy[i];
            }
        }
        return ret;           
      }
   
   public int[]  getAppro(){
        //  return Appro;
          
        int[] ret = null;
        if( this.Appro != null) {
            ret = new int[Appro.length];
            for (int i=0; i<Appro.length; i++) {
                ret[i] = this.Appro[i];
            }
        }
        return ret;           
      }

    public int getAuthNum(){
          return sysAuth;
      }

   public int[] getAccPrss(){
        //  return acprss;
          
        int[] ret = null;
        if( this.acprss != null) {
            ret = new int[acprss.length];
            for (int i=0; i<acprss.length; i++) {
                ret[i] = this.acprss[i];
            }
        }
        return ret;            
      }


}
