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 BoardModify  extends DBManager {

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

	private ResultSet rs = null;

	private int count = 0;
	private String[] popYn = null;
    private int[] id;
	private String[] writeDate = null;
	private String[] title = null;
    private String[] user_name = null;
    private String[] dept_name = null;
    private int[] visit;
    private int tCount;
    private boolean[] popup_yn;

    /******** 카테고리 전체 리스트 ********/
	public void executeQuery(String boardGroupID,String findOption,String findWord) {

		StringBuilder sql = new StringBuilder(250);

        try {

          if(findWord != null && findOption.equals("title")){
             sql.append(" SELECT A0.popup_yn popyn")
                .append(",A0.id id")
                .append(",A0.write_date writeDate")
                .append(",A0.title title")
                .append(",sp_cm_dec_fu(us.name) user_name")
                .append(",dt.name dept_name")
                .append(",A0.visit visit")
                .append(" FROM CMCDBMGR.MOUMI_BOARD A0 ")
                .append(",moumi_muser us")
                .append(",moumi_dept dt ")
                .append(" where ")
                .append("us.id=A0.USER_ID")
                .append(" and us.dept_id=dt.id")
                .append(" and us.dept_id=dt.id")
                .append(" and A0.group_ID='"+boardGroupID+"'")
                .append(" and A0.title like '%"+findWord+"%'")
                .append("ORDER BY writeDate DESC");
            }else if(findWord != null && findOption.equals("mUser.name")){
             sql.append(" SELECT A0.popup_yn popyn")
                .append(",A0.id id")
                .append(",A0.write_date writeDate")
                .append(",A0.title title")
                .append(",sp_cm_dec_fu(us.name) user_name")
                .append(",dt.name dept_name")
                .append(",A0.visit visit")
                .append(" FROM CMCDBMGR.MOUMI_BOARD A0 ")
                .append(",moumi_muser us")
                .append(",moumi_dept dt ")
                .append(" where ")
                .append("us.id=A0.USER_ID")
                .append(" and us.dept_id=dt.id")
                .append(" and us.dept_id=dt.id")
                .append(" and A0.group_ID='"+boardGroupID+"'")
                .append(" and sp_cm_dec_fu(us.name) like '%"+findWord+"%'")
                .append("ORDER BY writeDate DESC");
            }else{
              sql.append(" SELECT A0.popup_yn popyn")
                .append(",A0.id id")
                .append(",A0.write_date writeDate")
                .append(",A0.title title")
                .append(",sp_cm_dec_fu(us.name) user_name")
                .append(",dt.name dept_name")
                .append(",A0.visit visit")
                .append(" FROM CMCDBMGR.MOUMI_BOARD A0 ")
                .append(",moumi_muser us")
                .append(",moumi_dept dt ")
                .append(" where ")
                .append("us.id=A0.USER_ID")
                .append(" and us.dept_id=dt.id")
                .append(" and us.dept_id=dt.id")
                .append(" and A0.group_ID='"+boardGroupID+"'")
                .append("ORDER BY writeDate DESC");
            }

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

			popYn   = new String[count];
            id      = new int[count];
            writeDate = new String[count];
            title     = new String[count];
            user_name = new String[count];
            dept_name = new String[count];
            visit     = new int[count];
            tCount     = count;

			for (int i = 0; rs.next(); i++) {

				popYn[i]   = rs.getString(1);
                id[i]      = rs.getInt(2);
              	writeDate[i]   = rs.getString(3);
               	title[i]   = rs.getString(4);
               	user_name[i]   = rs.getString(5);
               	dept_name[i]   = rs.getString(6);
               	visit[i]   = rs.getInt(7);

			}

          //System.out.println(sql+"\n find option::::"+findOption+"\n findWord:::"+findWord);

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

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

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

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

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

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

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

    public int  getCount(){
          return tCount;
      }

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

}
