/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kr.co.kihyun.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
//import javax.jdo.Query;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.service.vo.CodeVO;
import kr.co.kihyun.service.vo.ReportCountVO;
import kr.co.kihyun.service.vo.ReportTotalVO;
//import kr.co.kihyun.service.vo.TotDocVO;
import kr.co.kihyun.service.vo.TotReportVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author KWON,HAN
 */
public class PrssReportsService extends BaseService {

    /**
     * 기본생성자
     */
    public PrssReportsService() {
    }

    /**
     * 생성자
     *
     * @param pm PersistenceManager
     */
    public PrssReportsService(PersistenceManager pm) {
        super(pm);
    }

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


    /**
     * 집계문서ID로 제출부서의 리스트를 반환한다.
     * @param docId - 집계문서ID
     * @return - 제출부서의 리스트
     */
    public List getReportDeptListByDocId(Long docId) {
        StringBuilder sbQuery = new StringBuilder();

        sbQuery.append(" select rt.dept_id  as value                            \n");
        sbQuery.append("      , dt.name     as code                             \n");
        sbQuery.append("   from moumi_tot_report rt                             \n");
        sbQuery.append("      , moumi_dept dt                                   \n");
        sbQuery.append("      , moumi_dept pt                                   \n");
        sbQuery.append("  where rt.doc_id = :docId                              \n");
        sbQuery.append("    and rt.type = 0                                     \n");
        sbQuery.append("    and (rt.del_type = '0' or rt.del_type = 'N')        \n");
        sbQuery.append("    and rt.doc_type = 0                                 \n");
        sbQuery.append("    and rt.dept_id = dt.id(+)                           \n");
        sbQuery.append("    and dt.organ = pt.id(+)                             \n");

        Map lvParams = new HashMap();
        lvParams.put("docId", docId);

//        LOG.debug("PrssReportsService.getReportCountByDocId ======================\nquery=\n{}\n===============================", sbQuery.toString());
//        LOG.debug("lvParams={}", lvParams);

        return (List)this.executeQuery(sbQuery, lvParams, CodeVO.class);
    }

   /**
     * 검색조건쿼리와 레코드개수조회쿼리를 생성하는 메소드(제출현황)
     *
     * @param params - 검색조건맵
     *         - docId
     *         - findOption
     *         - filterValue
     * @참조페이지 - /webapp/totsys/common/inc/documents/report/report_list.jsp
     */
    public void createPrssReportListSearchQuery(Map params) {
        StringBuilder sbSearch = new StringBuilder();

//        String docId = (String) params.get("docId");
//        String dpId = (String) params.get("dpId");
        TotReportProcess process = null;
        if((Integer) params.get("listType") >= 0) {
            process = TotReportProcess.values()[(Integer) params.get("listType")];
        }
        String findOption = (String) params.get("findOption");
        String filterValue = (String) params.get("filterValue");
        String howSort = (String) params.get("howSort");
        String sort = (String) params.get("sort");
        String file = (String) params.get("file");
        String subDoc = (String) params.get("subDoc");


                sbSearch.append(" SELECT '1'                 AS deptNo          \n");
                sbSearch.append("      , A0.DOC_ID           AS docId           \n");
                sbSearch.append("      , A0.ID                                  \n");
              //sbSearch.append("    --, A0.MNG_CODE                            \n");
              //sbSearch.append("      , A0.OWNER_EMAIL                         \n");
                sbSearch.append("      , SP_CM_DEC_FU(A0.OWNER_NAME)  AS ownerName       \n"); /* .getOwnerName(pm) */
                sbSearch.append("      , A0.OWNER_PHONE      AS ownerPhone      \n"); /* .getAssignType */
              //sbSearch.append("    --, A0.ACPT_CODE                           \n");
              //sbSearch.append("    --, A0.ACPT_DOC_CODE                       \n");
              //sbSearch.append("    --, A0.APPRO                               \n");
                sbSearch.append("      , A0.DOC_TYPE         AS docType         \n");  /* .getAssignType */
                sbSearch.append("      , A0.DEL_TYPE         AS delType         \n");  /* .isDeleted() */
                sbSearch.append("      , A0.DEPT_ID          AS deptId          \n");  /* .getDept(pm).getId() */
                sbSearch.append("      , A0.USER_ID          AS userId          \n");  /* .getUser() .getUser(pm).getId() */
              //sbSearch.append("    --, A0.NUM                                 \n");
                sbSearch.append("      , A0.PRSS                                \n");
                sbSearch.append("      , A0.RECOG                               \n");  /* .getRecog() */
              //sbSearch.append("    --, A0.REG_DATE                            \n");
              //sbSearch.append("    --, A0.SECU_YN                             \n");
                sbSearch.append("      , A0.DOWN_DOC_ID      AS downDocId       \n");  /* .getSlaveTotDoc() */
                sbSearch.append("      , A0.SUBMIT_DATE      AS submitDate      \n");  /* .getSubmitDate() */
              //sbSearch.append("    --, A0.TABLE_NUM                           \n");
                sbSearch.append("      , A0.TYPE                                \n");  /* .getType() */
              //sbSearch.append("    --, A0.ATTACHMENTS                         \n");
              //sbSearch.append("    --, CAST(CAST(A0.ATTACHMENTS AS BLOB(4096)) as varchar(5000)) AS attachments \n");
                sbSearch.append("      , CASE WHEN A0.ATTACHMENTS IS NOT NULL THEN 'Y' ELSE 'N' END isAttachments \n");
              //sbSearch.append("    --, A0.NOTE                                \n");
              //sbSearch.append("    --, CAST(CAST(A0.NOTE AS CLOB(4096)) as varchar(5000)) AS note \n");
                sbSearch.append("      , CASE WHEN A0.NOTE IS NOT NULL THEN 'Y' ELSE 'N' END isNote \n");
                sbSearch.append("        /* -------------------------------- */ \n");
              //sbSearch.append("    --, B0.ID            AS DP_ID              \n");
                sbSearch.append("      , B0.NAME          AS deptName           \n");  /* .getDept(pm).getName() */
                sbSearch.append("      , B0.GRADE                               \n");
                sbSearch.append("      , B0.FOUNDATION                          \n");
                sbSearch.append("      , B0.ORGAN                               \n");  /* .getDept(pm).getOrgan(pm).getName() */
                sbSearch.append("      , ( SELECT NAME                          \n");
                sbSearch.append("            FROM MOUMI_DEPT                    \n");
                //sbSearch.append("           WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS NOT NULL                        \n");
                sbSearch.append("           WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS  NULL                        \n");
                sbSearch.append("                             THEN ( SELECT ORGAN FROM MOUMI_DEPT  WHERE ID = B0.UPPER_DEPT )            \n");
                sbSearch.append("                             ELSE B0.ORGAN END )                                                        \n");
                sbSearch.append("        ) AS organName                         \n");
                sbSearch.append("      , B0.UPPER_DEPT   AS upperDept           \n"); /* .getDept(pm).getUpperDept(pm) */
                sbSearch.append("      , ( SELECT NAME FROM MOUMI_DEPT WHERE ORGAN IS NOT NULL AND ID = B0.UPPER_DEPT) upperDeptName     \n");
              //sbSearch.append("    --, B0.ADDR                                \n");
              //sbSearch.append("    --, B0.HANDY_ID                            \n");
              //sbSearch.append("    --, B0.SEL_TYPE                            \n");
              //sbSearch.append("    --, B0.IS_OUT                              \n");
              //sbSearch.append("    --, B0.LOCATION                            \n");
              //sbSearch.append("    --, B0.PASSCODE                            \n");
                sbSearch.append("      , B0.PHONE        AS deptPhone           \n"); /* .getDept(pm).getPhone() */
              //sbSearch.append("    --, B0.PRIORITY                            \n");
              //sbSearch.append("    --, B0.RECO_IMG                            \n");
              //sbSearch.append("    --, B0.ROOT_DEPT                           \n");
              //sbSearch.append("    --, B0.SEC_ID                              \n");
              //sbSearch.append("    --, B0.SEQNUM                              \n");
              //sbSearch.append("    --, B0.SYS_AUTH                            \n");
                sbSearch.append("        /* -------------------------------- */ \n");
              //sbSearch.append("    --, U0.ID           AS US_ID               \n");
                sbSearch.append("      , U0.PHONE        AS userPhone           \n"); /* getUser(pm).getPhone() */
                sbSearch.append("      , A0.ATTACHMENTS_PATH	as attachmentsPath  \n");
//                sbSearch.append("      , ROWNUMBER() OVER()  AS rowNo           \n");
                sbSearch.append("   FROM MOUMI_TOT_REPORT A0                    \n");
                sbSearch.append("        LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID    \n");
                sbSearch.append("        LEFT OUTER JOIN MOUMI_MUSER U0 ON A0.USER_ID = U0.ID   \n");
                sbSearch.append("        ,MOUMI_DEPT G0                         \n");
                sbSearch.append("  WHERE A0.DOC_ID = :docId                     \n");
                sbSearch.append("    AND A0.DEL_TYPE = 'N'                      \n");
                sbSearch.append("    AND B0.ORGAN = G0.ID                       \n");

//        if(process==TotReportProcess.NOT_REPORT) filter.append(" && process.ordinal()<TotReportProcess.COMP.ordinal()");
//		  else if(process == TotReportProcess.ALL);
//                  else {
//                    if(process==TotReportProcess.COMP) filter.append(" && process==prss ");
//                    else if(process==TotReportProcess.NOTE) filter.append(" && process==prss ");
//                    else filter.append(" && process==prss");
//                }

    if(process != null) {
        if(process == TotReportProcess.NOT_REPORT) {
                sbSearch.append("    AND A0.PRSS < " + TotReportProcess.COMP.ordinal() + "          \n");
        } else if(process == TotReportProcess.ALL) {
            //Nothing
                sbSearch.append("    AND A0.PRSS = A0.PRSS                      \n");
        } else {
            if(process == TotReportProcess.COMP) {
                sbSearch.append("    AND A0.PRSS = " + process.ordinal() + "    \n");
            } else if(process == TotReportProcess.NOTE) {
                sbSearch.append("    AND A0.PRSS = " + process.ordinal() + "    \n");
            } else {
                sbSearch.append("    AND A0.PRSS = " + process.ordinal() + "    \n");
            }
        }
    }

//        if(howSort!=null && !howSort.equals("")) filter.append("&& submitDate != null");

        if(!MString.isNull(howSort) && !"".equals(howSort)) {
               sbSearch.append("    AND A0.SUBMIT_DATE IS NOT NULL             \n");
        }
        if(!MString.isNull(file) && !"".equals(file)) {
               sbSearch.append("    AND A0.ATTACHMENTS IS NOT NULL             \n");
        }
        if(!MString.isNull(subDoc) && !"".equals(subDoc)) {
               sbSearch.append("    AND A0.DOWN_DOC_ID IS NOT NULL             \n");
        }


        if (!MString.isNull(findOption) && !MString.isNull(filterValue)) {
            if (findOption.equals("mDept.name")) {
                sbSearch.append("    AND B0.NAME LIKE '%' || :filterValue || '%'            \n");
            } else if (findOption.equals("mUser.name")) {
                sbSearch.append("    AND SP_CM_DEC_FU(A0.OWNER_NAME) LIKE '%' || :filterValue || '%'  \n");
            } else if (findOption.equals("mOrgan.name")) {
                sbSearch.append("    AND G0.NAME LIKE '%' || :filterValue || '%'            \n");
            }

        }


        //레코드갯수를 구하는 부모클래스의 함수를 호출하기 위해서 멤버변수에 저장
        //페이징처리함수 호출전에 등록 필요.
        this.setCountQuery(sbSearch);
        this.setParams(params);

//        Map lvParams = new HashMap();
//        lvParams.put("docId", (Long) params.get("docId"));
//        this.setParams(lvParams);

        if(!MString.isNull(howSort) && !"".equals(howSort)) {
                sbSearch.append("  ORDER BY A0.SUBMIT_DATE  DESC                \n");

        } else {
            if (!MString.isNull(sort) && "1".equals(sort)) {
                sbSearch.append("  ORDER BY B0.ORGAN                            \n");
                sbSearch.append("         , B0.UPPER_DEPT                       \n");
                sbSearch.append("         , B0.GRADE                            \n");
                sbSearch.append("         , B0.FOUNDATION                       \n");
                sbSearch.append("         , B0.NAME                             \n");
            } else if ((!MString.isNull(sort) && "2".equals(sort)) || MString.isNull(sort)) {
                sbSearch.append("  ORDER BY B0.GRADE                            \n");
                sbSearch.append("         , B0.FOUNDATION                       \n");
                sbSearch.append("         , B0.ORGAN                            \n");
                sbSearch.append("         , B0.UPPER_DEPT                       \n");
                sbSearch.append("         , B0.NAME                             \n");
            }
        }

        //페이징처리쿼리생성함수 호출
        this.setSearchQuery(sbSearch);

//        LOG.debug("PrssReportsService.createPrssReportListSearchQuery ======================\nquery=\n{}\n===============================", sbSearch.toString());
//        LOG.debug("params={}", params);
//        LOG.debug("params={}", this.getParams());
    }

   /**
     * 검색조건쿼리와 레코드개수조회쿼리를 생성하는 메소드(제출현황)
     *
     * @param params - 검색조건맵
     *         - docId
     *         - findOption
     *         - filterValue
     * @참조페이지 - /webapp/totsys/common/inc/documents/tot_reports/total_reports.jsp
     */
    public void createPrssTotalReportsSearchQuery(Map params) {
        StringBuilder sbSearch = new StringBuilder();

        Long docID = (Long)params.get("docId");
        String dpId = (String) params.get("dpId");
        String mngcode = (String) params.get("mngcode");
        TotReportProcess process = null;
        if((Integer) params.get("listType") >= 0) {
            process = TotReportProcess.values()[(Integer) params.get("listType")];
        }
        String findOption = (String) params.get("findOption");
        String filterValue = (String) params.get("filterValue");
        String howSort = (String) params.get("howSort");
        String file = (String) params.get("file");
        String subDoc = (String) params.get("subDoc");
        String sort = (String) params.get("sort");

                sbSearch.append(" SELECT X.DEPTH_NO         AS deptNo           \n");
                sbSearch.append("      , X.DOC_ID           AS docId            \n");
                sbSearch.append("      , X.ID                                   \n");
                sbSearch.append("      , X.OWNER_NAME       AS ownerName        \n");
                sbSearch.append("      , X.OWNER_PHONE      AS ownerPhone       \n");
                sbSearch.append("      , X.DOC_TYPE         AS docType          \n");
                sbSearch.append("      , X.DEL_TYPE         AS delType          \n");
                sbSearch.append("      , X.DEPT_ID          AS deptId           \n");
                sbSearch.append("      , X.USER_ID          AS userId           \n");
                sbSearch.append("      , X.PRSS                                 \n");
                sbSearch.append("      , X.RECOG                                \n");
                sbSearch.append("      , X.DOWN_DOC_ID      AS downDocId        \n");
                sbSearch.append("      , X.SUBMIT_DATE      AS submitDate       \n");
                sbSearch.append("      , X.TYPE                                 \n");
                sbSearch.append("      , X.IS_ATTACHMENTS   AS isAttachments    \n");
                sbSearch.append("      , X.IS_NOTE          AS isNote           \n");
                sbSearch.append("        /* -------------------------------- */ \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.DEPT_NAME       ELSE X.SLAVE_DEPT_NAME       END ) AS deptName       \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.GRADE           ELSE X.SLAVE_GRADE           END ) AS grade          \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.FOUNDATION      ELSE X.SLAVE_FOUNDATION      END ) AS foundation     \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.ORGAN           ELSE X.SLAVE_ORGAN           END ) AS organ          \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.ORGAN_NAME      ELSE X.SLAVE_ORGAN_NAME      END ) AS organName      \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.UPPER_DEPT      ELSE X.SLAVE_UPPER_DEPT      END ) AS upperDept      \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.UPPER_DEPT_NAME ELSE X.SLAVE_UPPER_DEPT_NAME END ) AS upperDeptName  \n");
                sbSearch.append("      , ( CASE WHEN DEPTH_NO = '1' THEN X.DEPT_PHONE      ELSE X.SLAVE_DEPT_PHONE      END ) AS deptPhone      \n");
                sbSearch.append("        /* -------------------------------- */ \n");
                sbSearch.append("      , X.attachmentsPath        AS attachmentsPath       \n");
                sbSearch.append("      , X.USER_PHONE        AS userPhone       \n");
//                sbSearch.append("      , ROWNUMBER() OVER()  AS rowNo           \n");
                sbSearch.append("   FROM (                                      \n");
                sbSearch.append("         SELECT '1' AS DEPTH_NO                \n");
                sbSearch.append("              , A0.DOC_ID                      \n");
                sbSearch.append("              , A0.ID                          \n");
                sbSearch.append("              , SP_CM_DEC_FU(A0.OWNER_NAME)  AS OWNER_NAME  \n");
                sbSearch.append("              , A0.OWNER_PHONE                 \n");
                sbSearch.append("              , A0.DOC_TYPE                    \n");
                sbSearch.append("              , A0.DEL_TYPE                    \n");
                sbSearch.append("              , A0.DEPT_ID                     \n");
                sbSearch.append("              , A0.USER_ID                     \n");
                sbSearch.append("              , A0.PRSS                        \n");
                sbSearch.append("              , A0.RECOG                       \n");
                sbSearch.append("              , A0.DOWN_DOC_ID                 \n");
                sbSearch.append("              , A0.SUBMIT_DATE                 \n");
                sbSearch.append("              , A0.TYPE                        \n");
                sbSearch.append("              , CASE WHEN A0.ATTACHMENTS IS NOT NULL THEN 'Y' ELSE 'N' END IS_ATTACHMENTS  \n");
                sbSearch.append("              , CASE WHEN A0.NOTE IS NOT NULL THEN 'Y' ELSE 'N' END IS_NOTE                \n");
                sbSearch.append("                /* -------------------------------- */                                     \n");
                sbSearch.append("              , B0.NAME          AS DEPT_NAME  \n");
                sbSearch.append("              , B0.GRADE                       \n");
                sbSearch.append("              , B0.FOUNDATION                  \n");
                sbSearch.append("              , B0.ORGAN                       \n");
                sbSearch.append("              , ( SELECT NAME                                                                          \n");
                sbSearch.append("                    FROM MOUMI_DEPT                                                                    \n");
                //sbSearch.append("                   WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS NOT NULL               \n");
                sbSearch.append("                   WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS  NULL               \n");
                sbSearch.append("                                     THEN ( SELECT ORGAN FROM MOUMI_DEPT  WHERE ID = B0.UPPER_DEPT )   \n");
                sbSearch.append("                                     ELSE B0.ORGAN END )                                               \n");
                sbSearch.append("                ) AS ORGAN_NAME                                                                        \n");
                sbSearch.append("              , B0.UPPER_DEPT                  \n");
                sbSearch.append("              , ( SELECT NAME FROM MOUMI_DEPT WHERE ORGAN IS NOT NULL AND ID = B0.UPPER_DEPT) AS UPPER_DEPT_NAME   \n");
                sbSearch.append("              , B0.PHONE        AS DEPT_PHONE                             \n");
                sbSearch.append("              , NULL             AS SLAVE_DEPT_NAME                       \n");
                sbSearch.append("              , NULL             AS SLAVE_GRADE                           \n");
                sbSearch.append("              , NULL             AS SLAVE_FOUNDATION                      \n");
                sbSearch.append("              , NULL             AS SLAVE_ORGAN                           \n");
                sbSearch.append("              , NULL             AS SLAVE_ORGAN_NAME                      \n");
                sbSearch.append("              , NULL             AS SLAVE_UPPER_DEPT                      \n");
                sbSearch.append("              , NULL             AS SLAVE_UPPER_DEPT_NAME                 \n");
                sbSearch.append("              , NULL             AS SLAVE_DEPT_PHONE                      \n");
                sbSearch.append("                /* -------------------------------- */                    \n");
                sbSearch.append("      		   , A0.ATTACHMENTS_PATH	as attachmentsPath 				   \n");
                sbSearch.append("              , U0.PHONE        AS USER_PHONE                             \n");
                sbSearch.append("           FROM MOUMI_TOT_REPORT A0                                       \n");
                sbSearch.append("                LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID       \n");
                sbSearch.append("                LEFT OUTER JOIN MOUMI_MUSER U0 ON A0.USER_ID = U0.ID      \n");
                sbSearch.append("                , MOUMI_DEPT G0      \n");
                sbSearch.append("          WHERE A0.DOC_ID = :docId             \n");
                sbSearch.append("            AND A0.DEL_TYPE = 'N'              \n");
                if( dpId.contains("A00") && docID >= 1700000000){
                	if(mngcode == ""){
                		sbSearch.append("    AND A0.MNG_CODE is null              \n");
                	}else{
                		sbSearch.append("    AND LOWER(A0.MNG_CODE) =:mngcode  \n");//20160509 추가
                	}
                }

                sbSearch.append("            AND B0.ORGAN = G0.ID              \n");

    if(process != null) {
        if(process == TotReportProcess.NOT_REPORT) {
                sbSearch.append("            AND A0.PRSS < " + TotReportProcess.COMP.ordinal() + "          \n");
        } else if(process == TotReportProcess.ALL) {
            //Nothing
                sbSearch.append("            AND A0.PRSS = A0.PRSS                      \n");
        } else {
            if(process == TotReportProcess.COMP) {
                sbSearch.append("            AND A0.PRSS = " + process.ordinal() + "    \n");
            } else if(process == TotReportProcess.NOTE) {
                sbSearch.append("            AND A0.PRSS = " + process.ordinal() + "    \n");
            } else {
                sbSearch.append("            AND A0.PRSS = " + process.ordinal() + "    \n");
            }
        }
    }

        if(!MString.isNull(howSort) && !"".equals(howSort)) {
               sbSearch.append("             AND A0.SUBMIT_DATE IS NOT NULL             \n");
        }
        if(!MString.isNull(file) && !"".equals(file)) {
               sbSearch.append("    AND A0.ATTACHMENTS IS NOT NULL             \n");
        }
        if(!MString.isNull(subDoc) && !"".equals(subDoc)) {
               sbSearch.append("    AND A0.DOWN_DOC_ID IS NOT NULL             \n");
        }

        if (!MString.isNull(findOption) && !MString.isNull(filterValue)) {
            if (findOption.equals("mDept.name")) {
                sbSearch.append("           AND B0.NAME LIKE '%' || :filterValue || '%'            \n");
            } else if (findOption.equals("mUser.name")) {
                sbSearch.append("           AND SP_CM_DEC_FU(A0.OWNER_NAME) LIKE '%' || :filterValue || '%'  \n");
            } else if (findOption.equals("mOrgan.name")) {
                sbSearch.append("    AND G0.NAME LIKE '%' || :filterValue || '%'            \n");
            }
        }

                sbSearch.append("                                               \n");
                sbSearch.append("         UNION ALL                             \n");
                sbSearch.append("                                               \n");
                sbSearch.append("         SELECT '2' AS DEPTH_NO                \n");
                sbSearch.append("              , A0.DOC_ID                      \n");
                sbSearch.append("              , A0.ID                          \n");
                sbSearch.append("              , SP_CM_DEC_FU(A0.OWNER_NAME)  AS OWNER_NAME  \n");
                sbSearch.append("              , A0.OWNER_PHONE                 \n");
                sbSearch.append("              , A0.DOC_TYPE                    \n");
                sbSearch.append("              , A0.DEL_TYPE                    \n");
                sbSearch.append("              , A0.DEPT_ID                     \n");
                sbSearch.append("              , A0.USER_ID                     \n");
                sbSearch.append("              , A0.PRSS                        \n");
                sbSearch.append("              , A0.RECOG                       \n");
                sbSearch.append("              , A0.DOWN_DOC_ID                 \n");
                sbSearch.append("              , A0.SUBMIT_DATE                 \n");
                sbSearch.append("              , A0.TYPE                        \n");
                sbSearch.append("              , CASE WHEN A0.ATTACHMENTS IS NOT NULL THEN 'Y' ELSE 'N' END IS_ATTACHMENTS  \n");
                sbSearch.append("              , CASE WHEN A0.NOTE IS NOT NULL THEN 'Y' ELSE 'N' END IS_NOTE                \n");
                sbSearch.append("                /* -------------------------------- */                                     \n");
                sbSearch.append("              , B1.NAME           AS DEPT_NAME          \n");
                sbSearch.append("              , B1.GRADE                                \n");
                sbSearch.append("              , B1.FOUNDATION                           \n");
                sbSearch.append("              , B1.ORGAN                                \n");
                sbSearch.append("              , NULL              AS ORGAN_NAME         \n");
                sbSearch.append("              , B1.UPPER_DEPT     AS UPPER_DEPT         \n");
                sbSearch.append("              , NULL              AS UPPER_DEPT_NAME    \n");
                sbSearch.append("              , B1.PHONE          AS DEPT_PHONE         \n");
                sbSearch.append("              , B0.NAME          AS SLAVE_DEPT_NAME     \n");
                sbSearch.append("              , B0.GRADE         AS SLAVE_GRADE         \n");
                sbSearch.append("              , B0.FOUNDATION    AS SLAVE_FOUNDATION    \n");
                sbSearch.append("              , B0.ORGAN         AS SLAVE_ORGAN         \n");
                sbSearch.append("              , ( SELECT NAME                                                                         \n");
                sbSearch.append("                    FROM MOUMI_DEPT                                                                   \n");
                //sbSearch.append("                   WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS NOT NULL              \n");
                sbSearch.append("                   WHERE ID = ( CASE WHEN B0.ID = B0.ORGAN AND B0.UPPER_DEPT IS NULL              \n");
                sbSearch.append("                                     THEN ( SELECT ORGAN FROM MOUMI_DEPT  WHERE ID = B0.UPPER_DEPT )  \n");
                sbSearch.append("                                     ELSE B0.ORGAN END )                                              \n");
                sbSearch.append("                ) AS SLAVE_ORGAN_NAME                                                                 \n");
                sbSearch.append("              , B0.UPPER_DEPT   AS SLAVE_UPPER_DEPT                                                   \n");
                sbSearch.append("              , ( SELECT NAME FROM MOUMI_DEPT WHERE ORGAN IS NOT NULL AND ID = B0.UPPER_DEPT) AS SLAVE_UPPER_DEPT_NAME  \n");
                sbSearch.append("      		   , A0.ATTACHMENTS_PATH	as attachmentsPath 				   \n");
                sbSearch.append("              , B0.PHONE        AS SLAVE_DEPT_PHONE                                                   \n");
                sbSearch.append("                /* -------------------------------- */  \n");
                sbSearch.append("              , U0.PHONE        AS USER_PHONE           \n");
                sbSearch.append("           FROM MOUMI_TOT_REPORT A0                                     \n");
                sbSearch.append("                LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID     \n");
                sbSearch.append("                LEFT OUTER JOIN MOUMI_MUSER U0 ON A0.USER_ID = U0.ID    \n");
                sbSearch.append("              , MOUMI_TOT_REPORT Z                                      \n");
                sbSearch.append("                LEFT OUTER JOIN MOUMI_DEPT B1 ON Z.DEPT_ID = B1.ID      \n");
                sbSearch.append("          WHERE A0.DOC_ID IN (                                          \n");
                sbSearch.append("                               SELECT A0.DOWN_DOC_ID                    \n");
                sbSearch.append("                                 FROM MOUMI_TOT_REPORT  A0              \n");
                sbSearch.append("                                      LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID   \n");
                sbSearch.append("                                      , MOUMI_DEPT G0   \n");
                sbSearch.append("                                WHERE A0.DOC_ID = :docId                \n");
                sbSearch.append("                                  AND A0.DEL_TYPE = 'N'                 \n");
                if( dpId.contains("A00") && docID >= 1700000000){//교육부 생성 문서시 제출기관 조회조건 추가
                	if(mngcode == ""){
                		sbSearch.append("    AND A0.MNG_CODE is null             \n");
                	}else{
                		sbSearch.append("    AND LOWER(A0.MNG_CODE) =:mngcode  \n");//20160509 추가
                	}
                }
                sbSearch.append("                                  AND A0.DOWN_DOC_ID IS NOT NULL        \n");
                sbSearch.append("                                  AND B0.ORGAN = G0.ID        \n");

    if(process != null) {
        if(process == TotReportProcess.NOT_REPORT) {
                sbSearch.append("                                  AND A0.PRSS < " + TotReportProcess.COMP.ordinal() + "      \n");
        } else if(process == TotReportProcess.ALL) {
            //Nothing
                sbSearch.append("                                  AND A0.PRSS = A0.PRSS                                      \n");
        } else {
            if(process == TotReportProcess.COMP) {
                sbSearch.append("                                  AND A0.PRSS = " + process.ordinal() + "                    \n");
            } else if(process == TotReportProcess.NOTE) {
                sbSearch.append("                                  AND A0.PRSS = " + process.ordinal() + "                    \n");
            } else {
                sbSearch.append("                                  AND A0.PRSS = " + process.ordinal() + "                    \n");
            }
        }
    }

        if(!MString.isNull(howSort) && !"".equals(howSort)) {
               sbSearch.append("   AND A0.SUBMIT_DATE IS NOT NULL             \n");
        }
        if(!MString.isNull(file) && !"".equals(file)) {
               sbSearch.append("    AND A0.ATTACHMENTS IS NOT NULL             \n");
        }
        if(!MString.isNull(subDoc) && !"".equals(subDoc)) {
               sbSearch.append("    AND A0.DOWN_DOC_ID IS NOT NULL             \n");
        }


        if (!MString.isNull(findOption) && !MString.isNull(filterValue)) {
            if (findOption.equals("mDept.name")) {
                sbSearch.append("                                 AND B0.NAME LIKE '%' || :filterValue || '%'                \n");
            } else if (findOption.equals("mUser.name")) {
                sbSearch.append("                                 AND SP_CM_DEC_FU(A0.OWNER_NAME) LIKE '%' || :filterValue || '%'  \n");
            } else if (findOption.equals("mOrgan.name")) {
                sbSearch.append("    AND G0.NAME LIKE '%' || :filterValue || '%'            \n");
            }
        }

                sbSearch.append("                             )                                          \n");
                sbSearch.append("            AND A0.DEL_TYPE = 'N'                                       \n");
                sbSearch.append("            AND A0.DOC_ID = Z.DOWN_DOC_ID                               \n");
                sbSearch.append("        ) X                                                             \n");

        //레코드갯수를 구하는 부모클래스의 함수를 호출하기 위해서 멤버변수에 저장
        //페이징처리함수 호출전에 등록 필요.
        this.setCountQuery(sbSearch);
        this.setParams(params);

//        Map lvParams = new HashMap();
//        lvParams.put("docId", (Long) params.get("docId"));
//        this.setParams(lvParams);

        if(!MString.isNull(howSort) && !"".equals(howSort)) {
                sbSearch.append("  ORDER BY X.SUBMIT_DATE  DESC                 \n");

        } else {
            if (!MString.isNull(sort) && "1".equals(sort)) {
                sbSearch.append("  ORDER BY X.ORGAN                             \n");
                sbSearch.append("         , X.UPPER_DEPT                        \n");
                sbSearch.append("         , X.GRADE                             \n");
                sbSearch.append("         , X.FOUNDATION                        \n");
                sbSearch.append("         , X.DEPT_NAME                         \n");
                sbSearch.append("         , X.DEPTH_NO                          \n");
                sbSearch.append("         , X.SLAVE_ORGAN                       \n");
                sbSearch.append("         , X.SLAVE_UPPER_DEPT                  \n");
                sbSearch.append("         , X.SLAVE_GRADE                       \n");
                sbSearch.append("         , X.SLAVE_FOUNDATION                  \n");
                sbSearch.append("         , X.SLAVE_DEPT_NAME                   \n");

            } else if ((!MString.isNull(sort) && "2".equals(sort)) || MString.isNull(sort)) {
                sbSearch.append("  ORDER BY X.GRADE                             \n");
                sbSearch.append("         , X.FOUNDATION                        \n");
                sbSearch.append("         , X.ORGAN                             \n");
                sbSearch.append("         , X.UPPER_DEPT                        \n");
                sbSearch.append("         , X.DEPT_NAME                         \n");
                sbSearch.append("         , X.DEPTH_NO                          \n");
                sbSearch.append("         , X.SLAVE_GRADE                       \n");
                sbSearch.append("         , X.SLAVE_FOUNDATION                  \n");
                sbSearch.append("         , X.SLAVE_ORGAN                       \n");
                sbSearch.append("         , X.SLAVE_UPPER_DEPT                  \n");
                sbSearch.append("         , X.SLAVE_DEPT_NAME                   \n");
                /* 제출일시로 조회하는 기능 추가 2015.09.21 jskim */
            } else if ((!MString.isNull(sort) && "3".equals(sort)) || MString.isNull(sort)) {
                sbSearch.append("  ORDER BY CASE WHEN X.SUBMIT_DATE IS NULL THEN 1 ELSE 0 END ASC, X.SUBMIT_DATE ASC                 \n");
            }
        }

        //페이징처리쿼리생성함수 호출
        this.setSearchQuery(sbSearch);

        LOG.debug("PrssReportsService.createPrssTotalReportsSearchQuery ======================\nquery=\n{}\n===============================", sbSearch.toString());
        LOG.debug("params={}", params);
        LOG.debug("params={}", this.getParams());
    }
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //+ createPrssTotalReportsSearchQuery() END
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * 집계문서ID로 집계현황의 진행상태별 현황을 반환한다.
     * @param docId - 집계문서ID
     * @return - 집계현황의 진행상태별 현황
     */
    public List getReportCountByDocId(Long docId) {
        StringBuilder sbQuery = new StringBuilder();


        sbQuery.append(" SELECT MAX(RT.DOC_ID) AS docId                                                            \n");
        sbQuery.append("      , COUNT( RT.DOC_ID )                                                    AS allCount  \n");
        sbQuery.append("      , SUM( CASE WHEN RT.PRSS = 6                        THEN 1 ELSE 0 END ) AS sendCount \n");
        sbQuery.append("      , SUM( CASE WHEN RT.PRSS = 12 AND DATA IS NOT NULL  THEN 1 ELSE 0 END ) AS notCount  \n");
        sbQuery.append("      , SUM( CASE WHEN RT.PRSS IN (14)                    THEN 1 ELSE 0 END ) AS noteCount \n");
        sbQuery.append("   FROM MOUMI_TOT_REPORT RT                                                                \n");
        sbQuery.append("  WHERE RT.DOC_ID = :docId                                                                 \n");
        sbQuery.append("    AND (RT.DEL_TYPE = '0' OR RT.DEL_TYPE= 'N' )                                           \n");

        Map lvParams = new HashMap();
        lvParams.put("docId", docId);

//        LOG.debug("PrssReportsService.getReportCountByDocId ======================\nquery=\n{}\n===============================", sbQuery.toString());
//        LOG.debug("lvParams={}", lvParams);

        return (List)this.executeQuery(sbQuery, lvParams, ReportCountVO.class);
    }

    /**
     * 집계문서ID로 제출현황의 기관별 집계표를 반환한다.
     * @param docId - 집계문서ID
     * @return - 제출현황의 기관별 집계표
     */
    public List getReportTotalByDocId(Long docId) {
        StringBuilder sbQuery = new StringBuilder();


        sbQuery.append(" SELECT MAX(A0.DOC_ID) AS docId  \n");
        sbQuery.append("      , COUNT( A0.DOC_ID )                                                                           AS allCnt       \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE IS NULL THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE IS NULL THEN 1 ELSE 0 END ) END )                        AS allOrganCnt  \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE = 1     THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE = 1     THEN 1 ELSE 0 END ) END )                        AS allGrade1Cnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE = 2     THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE = 2     THEN 1 ELSE 0 END ) END )                        AS allGrade2Cnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE = 3     THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE = 3     THEN 1 ELSE 0 END ) END )                        AS allGrade3Cnt \n");
        // 고등학교(grade="4")가 기타학교로 합쳐서 나오지 말고, 별도로 조회하도록 수정. 2014.12.18 by YOUNGJUN,CHO
        //sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE > 4     THEN 1 ELSE 0 END )                  \n");
        //sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE > 4     THEN 1 ELSE 0 END ) END )                        AS allGrade4Cnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE = 4     THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE = 4     THEN 1 ELSE 0 END ) END )                        AS allGrade4Cnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN B0.GRADE >= 5     THEN 1 ELSE 0 END )                  \n");
        sbQuery.append("                  ELSE ( CASE WHEN B1.GRADE >= 5     THEN 1 ELSE 0 END ) END )                        AS allGrade5Cnt \n");
        sbQuery.append("      /*----------------------*/                                                                                     \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6                      THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6                      THEN 1 ELSE 0 END ) END )                       AS totReportCnt    \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE IS NULL THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE IS NULL THEN 1 ELSE 0 END ) END )                       AS organReportCnt  \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 1     THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE = 1     THEN 1 ELSE 0 END ) END )                       AS grade1ReportCnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 2     THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE = 2     THEN 1 ELSE 0 END ) END )                       AS grade2ReportCnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 3     THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE = 3     THEN 1 ELSE 0 END ) END )                       AS grade3ReportCnt \n");
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 4     THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE = 4     THEN 1 ELSE 0 END ) END )                       AS grade4ReportCnt \n");
        // 고등학교(grade="4")가 기타학교로 합쳐서 나오지 말고, 별도로 조회하도록 수정. 2014.12.18 by YOUNGJUN,CHO
        sbQuery.append("      , SUM( CASE WHEN A0.DOWN_DOC_ID IS NULL THEN ( CASE WHEN A0.PRSS = 6 AND B0.GRADE >= 5     THEN 1 ELSE 0 END )                    \n");
        sbQuery.append("                  ELSE ( CASE WHEN A1.PRSS = 6 AND B1.GRADE >= 5     THEN 1 ELSE 0 END ) END )                       AS grade5ReportCnt \n");
        sbQuery.append("   FROM MOUMI_TOT_REPORT A0                             \n");
        sbQuery.append("      , MOUMI_DEPT B0                                   \n");
        sbQuery.append("      , MOUMI_TOT_REPORT A1                             \n");
        sbQuery.append("      , MOUMI_DEPT B1                                   \n");
        sbQuery.append("  WHERE A0.DOC_ID = :docId                              \n");
        sbQuery.append("    AND A0.DEL_TYPE = 'N'                               \n");
        sbQuery.append("    AND A0.DEPT_ID = B0.ID(+)                           \n");
        sbQuery.append("    AND A0.DOWN_DOC_ID = A1.DOC_ID(+)                   \n");
        sbQuery.append("    AND A1.DEL_TYPE(+) = 'N'                            \n");
        sbQuery.append("    AND A1.DEPT_ID = B1.ID(+)                           \n");

//        sbQuery.append(" SELECT MAX(A0.DOC_ID) AS docId                                                                  \n");
//        sbQuery.append("      , COUNT( A0.DOC_ID )                                  AS allCnt                            \n");
//        sbQuery.append("      , SUM( CASE WHEN B0.GRADE IS NULL THEN 1 ELSE 0 END ) AS allOrganCnt                       \n");
//        sbQuery.append("      , SUM( CASE WHEN B0.GRADE = 1     THEN 1 ELSE 0 END ) AS allGrade1Cnt                      \n");
//        sbQuery.append("      , SUM( CASE WHEN B0.GRADE = 2     THEN 1 ELSE 0 END ) AS allGrade2Cnt                      \n");
//        sbQuery.append("      , SUM( CASE WHEN B0.GRADE = 3     THEN 1 ELSE 0 END ) AS allGrade3Cnt                      \n");
//        sbQuery.append("      , SUM( CASE WHEN B0.GRADE > 4     THEN 1 ELSE 0 END ) AS allGrade4Cnt                      \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6                      THEN 1 ELSE 0 END ) AS totReportCnt      \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6 AND B0.GRADE IS NULL THEN 1 ELSE 0 END ) AS organReportCnt    \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 1     THEN 1 ELSE 0 END ) AS grade1ReportCnt   \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 2     THEN 1 ELSE 0 END ) AS grade2ReportCnt   \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 3     THEN 1 ELSE 0 END ) AS grade3ReportCnt   \n");
//        sbQuery.append("      , SUM( CASE WHEN A0.PRSS = 6 AND B0.GRADE = 4     THEN 1 ELSE 0 END ) AS grade4ReportCnt   \n");
//        sbQuery.append("   FROM MOUMI_TOT_REPORT A0                                   \n");
//        sbQuery.append("        LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID   \n");
//        sbQuery.append("  WHERE A0.DOC_ID = :docId                                    \n");
//        sbQuery.append("    AND A0.DEL_TYPE = 'N'                                     \n");

        Map lvParams = new HashMap();
        lvParams.put("docId", docId);

//        LOG.debug("PrssReportsService.getReportTotalByDocId ======================\nquery=\n{}\n===============================", sbQuery.toString());
//        LOG.debug("lvParams={}", lvParams);

        return (List)this.executeQuery(sbQuery, lvParams, ReportTotalVO.class);
    }


    /**
     * 집계자료ID로 제출자료를 조회하는 메소드
     * @param docId
     * @param prss
     * @param grade
     * @return
     */
    public List getTotReportByDocId(Long docId, TotReportProcess prss, String grade) {
        StringBuilder sbQuery = new StringBuilder();

                sbQuery.append(" SELECT '1' AS depthNo                          \n");
                sbQuery.append("      , A0.ID                                   \n");
              //sbQuery.append("    --, A0.MNG_CODE                             \n");
                sbQuery.append("      , A0.OWNER_EMAIL    AS ownerEmail         \n");
                sbQuery.append("      , A0.OWNER_NAME     AS ownerName          \n");
                sbQuery.append("      , A0.OWNER_PHONE    AS ownerPhone         \n");
                sbQuery.append("      , A0.ACPT_CODE      AS acptCode           \n");
                sbQuery.append("      , A0.ACPT_DOC_CODE  AS acptDocCode        \n");
                sbQuery.append("      , A0.APPRO                                \n");
                sbQuery.append("      , A0.DOC_TYPE       AS docType            \n");
                sbQuery.append("      , A0.DEL_TYPE       AS delType            \n");
                sbQuery.append("      , A0.USER_ID        AS userId             \n");
                sbQuery.append("      , A0.NUM                                  \n");
                sbQuery.append("      , A0.PRSS                                 \n");
                sbQuery.append("      , A0.RECOG                                \n");
                sbQuery.append("      , A0.REG_DATE       AS regDate            \n");
                sbQuery.append("      , A0.SECU_YN        AS secuYn             \n");
                sbQuery.append("      , A0.SUBMIT_DATE    AS submitDate         \n");
                sbQuery.append("      , A0.TABLE_NUM      AS tableNum           \n");
                sbQuery.append("      , A0.TYPE                                 \n");
                sbQuery.append("      , B0.GRADE                                \n");
                sbQuery.append("   FROM MOUMI_TOT_REPORT A0                     \n");
                sbQuery.append("        LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID  \n");
                sbQuery.append("  WHERE A0.DOC_ID = :docId                      \n");
                sbQuery.append("    AND A0.DEL_TYPE = 'N'                       \n");
                sbQuery.append("    AND A0.PRSS =  :prss                        \n");
                sbQuery.append("    AND A0.DOWN_DOC_ID IS NULL                  \n");
        if(!MString.isNull(grade) && !"".equals(grade)) {
            if("organ".equals(grade)) {
                sbQuery.append("    AND B0.GRADE IS NULL                        \n");
            // 고등학교(grade="4")가 기타학교로 합쳐서 나오지 말고, 별도로 조회하도록 수정. 2014.12.18 by YOUNGJUN,CHO
            //} else if("4".equals(grade)) {
            } else if("5".equals(grade)) {
                sbQuery.append("    AND B0.GRADE >= :grade                      \n");
            } else {
                sbQuery.append("    AND B0.GRADE = :grade                       \n");
            }
        }

                sbQuery.append("                                                \n");
                sbQuery.append(" UNION ALL                                      \n");
                sbQuery.append("                                                \n");
                sbQuery.append(" SELECT '2' AS depthNo                          \n");
                sbQuery.append("      , A0.ID                                   \n");
              //sbQuery.append("    --, A0.MNG_CODE                             \n");
                sbQuery.append("      , A0.OWNER_EMAIL    AS ownerEmail         \n");
                sbQuery.append("      , A0.OWNER_NAME     AS ownerName          \n");
                sbQuery.append("      , A0.OWNER_PHONE    AS ownerPhone         \n");
                sbQuery.append("      , A0.ACPT_CODE      AS acptCode           \n");
                sbQuery.append("      , A0.ACPT_DOC_CODE  AS acptDocCode        \n");
                sbQuery.append("      , A0.APPRO                                \n");
                sbQuery.append("      , A0.DOC_TYPE       AS docType            \n");
                sbQuery.append("      , A0.DEL_TYPE       AS delType            \n");
                sbQuery.append("      , A0.USER_ID        AS userId             \n");
                sbQuery.append("      , A0.NUM                                  \n");
                sbQuery.append("      , A0.PRSS                                 \n");
                sbQuery.append("      , A0.RECOG                                \n");
                sbQuery.append("      , A0.REG_DATE       AS regDate            \n");
                sbQuery.append("      , A0.SECU_YN        AS secuYn             \n");
                sbQuery.append("      , A0.SUBMIT_DATE    AS submitDate         \n");
                sbQuery.append("      , A0.TABLE_NUM      AS tableNum           \n");
                sbQuery.append("      , A0.TYPE                                 \n");
                sbQuery.append("      , B0.GRADE                                \n");
                sbQuery.append("   FROM MOUMI_TOT_REPORT A0                     \n");
                sbQuery.append("        LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID    \n");
                sbQuery.append("  WHERE A0.DOC_ID IN (                                         \n");
                sbQuery.append("                      SELECT A0.DOWN_DOC_ID                    \n");
                sbQuery.append("                        FROM MOUMI_TOT_REPORT  A0              \n");
                sbQuery.append("                             LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID   \n");
                sbQuery.append("                       WHERE A0.DOC_ID = :docId                \n");
                sbQuery.append("                         AND A0.DEL_TYPE = 'N'                 \n");
                sbQuery.append("                         AND A0.DOWN_DOC_ID IS NOT NULL        \n");
                sbQuery.append("                     )                                         \n");
                sbQuery.append("    AND A0.DEL_TYPE = 'N'                       \n");
                sbQuery.append("    AND A0.PRSS =  :prss                        \n");
        if(!MString.isNull(grade) && !"".equals(grade)) {
            if("organ".equals(grade)) {
                sbQuery.append("    AND B0.GRADE IS NULL                        \n");
            // 고등학교(grade="4")가 기타학교로 합쳐서 나오지 말고, 별도로 조회하도록 수정. 2014.12.18 by YOUNGJUN,CHO
            //} else if("4".equals(grade)) {
            } else if("5".equals(grade)) {
                sbQuery.append("    AND B0.GRADE >= :grade                      \n");
            } else {
                sbQuery.append("    AND B0.GRADE = :grade                       \n");
            }
        }
                sbQuery.append("  ORDER BY depthNo, GRADE                       \n");


//                sbQuery.append(" SELECT A0.ID                                   \n");
//              //sbQuery.append("    --, A0.MNG_CODE                             \n");
//                sbQuery.append("      , A0.OWNER_EMAIL    AS ownerEmail         \n");
//                sbQuery.append("      , A0.OWNER_NAME     AS ownerName          \n");
//                sbQuery.append("      , A0.OWNER_PHONE    AS ownerPhone         \n");
//                sbQuery.append("      , A0.ACPT_CODE      AS acptCode           \n");
//                sbQuery.append("      , A0.ACPT_DOC_CODE  AS acptDocCode        \n");
//                sbQuery.append("      , A0.APPRO                                \n");
//                sbQuery.append("      , A0.DOC_TYPE       AS docType            \n");
//                sbQuery.append("      , A0.DEL_TYPE       AS delType            \n");
//                sbQuery.append("      , A0.USER_ID        AS userId             \n");
//                sbQuery.append("      , A0.NUM                                  \n");
//                sbQuery.append("      , A0.PRSS                                 \n");
//                sbQuery.append("      , A0.RECOG                                \n");
//                sbQuery.append("      , A0.REG_DATE       AS regDate            \n");
//                sbQuery.append("      , A0.SECU_YN        AS secuYn             \n");
//                sbQuery.append("      , A0.SUBMIT_DATE    AS submitDate         \n");
//                sbQuery.append("      , A0.TABLE_NUM      AS tableNum           \n");
//                sbQuery.append("      , A0.TYPE                                 \n");
//                sbQuery.append("      , B0.GRADE                                \n");
//                sbQuery.append("   FROM MOUMI_TOT_REPORT A0                     \n");
//                sbQuery.append("        LEFT OUTER JOIN MOUMI_DEPT B0 ON A0.DEPT_ID = B0.ID  \n");
//                sbQuery.append("  WHERE A0.DOC_ID = :docId                      \n");
//                sbQuery.append("    AND A0.DEL_TYPE = 'N'                       \n");
//                sbQuery.append("    AND A0.PRSS =  :prss                        \n");
//        if(!MString.isNull(grade) && !"".equals(grade)) {
//            if("organ".equals(grade)) {
//                sbQuery.append("    AND B0.GRADE IS NULL                        \n");
//            } else if("4".equals(grade)) {
//                sbQuery.append("    AND B0.GRADE >= :grade                      \n");
//            } else {
//                sbQuery.append("    AND B0.GRADE = :grade                       \n");
//            }
//        }
//                sbQuery.append("  ORDER BY B0.GRADE                             \n");

        Map lvParams = new HashMap();
        lvParams.put("docId", docId);
        lvParams.put("prss",  prss.ordinal());
        if(!MString.isNull(grade) && !"".equals(grade)) {
            if("organ".equals(grade)) {
                //Nothing
                lvParams.put("grade", 0);
            } else {
                lvParams.put("grade", MInteger.parseInt(grade));
            }
        }

//        LOG.debug("PrssReportsService.getTotReportByDocId ======================\nquery=\n{}\n===============================", sbQuery.toString());
//        LOG.debug("lvParams={}", lvParams);
//        System.out.println("grade = "+grade);

        return (List)this.executeQuery(sbQuery, lvParams, TotReportVO.class);
    }

    public List<TotReport> getRecursiveAllReports(Long docId, TotReportProcess prss, String grade) {
        List<TotReport> resultReportList = new ArrayList<TotReport>();

        List list = null;
        list = this.getTotReportByDocId(docId, prss, grade);

        if( list != null && list.size() > 0 ) {
            for(Object obj : list) {
                TotReportVO reportVO = (TotReportVO)obj;
                TotReport totReport = pm.getObjectById(TotReport.class, reportVO.getId());

//System.out.println("reportVO.getId() = "+reportVO.getId());
                if (totReport.getSlaveTotDoc() == null) {
                    resultReportList.add(totReport);
                } else {
                    //List<TotReport> slaveReportList = totReport.getSlaveTotDoc().getAllReports(pm, filterMap, false);
                    //resultReportList.addAll(getRecursiveAllReports(pm, slaveReportList, filterMap));

//System.out.println("totReport.getSlaveTotDoc().getId() = "+totReport.getSlaveTotDoc().getId());
                    List<TotReport> slaveReportList = this.getRecursiveAllReports(totReport.getSlaveTotDoc().getId(), prss  ,"");
                    resultReportList.addAll(slaveReportList);
                }
            }
        }

        return resultReportList;
    }

    //20160426 추가
    /**
     * 검색조건쿼리와 레코드개수조회쿼리를 생성하는 메소드(교육청 자료 조회 관련 IR_MOUMI_TOT_REPORT 테이블에서 IS_APLIED에 따른 데이타 조회)
     * @param params - 검색조건맵
     * @참조페이지 - doc_list.jsp
     */
    public void createIR_TOT_REPORT_SearchQuery(Map params) {
        StringBuilder sbSearch = new StringBuilder();
        //String findOption = (String) params.get("findOption");

        sbSearch.append(" SELECT *				 															\n");
        sbSearch.append(" FROM  IFDBMGR.IR_MOUMI_TOT_REPORT        \n");
        sbSearch.append(" WHERE DOC_ID    = :docID                \n");
        sbSearch.append(" AND   IS_APLIED = :isAplied             \n");

        //레코드갯수를 구하는 부모클래스의 함수를 호출하기 위해서 멤버변수에 저장
        this.setCountQuery(sbSearch);
        //조건맵을 멤버변수에 저장
        this.setParams(params);

        //검색쿼리를 멤버변수에 저장
        //this.setSearchQuery(sbSearch);

       // LOG.debug("접수자료 query={}", sbSearch.toString());
}

    //20160502 추가
    /**
     * 검색조건쿼리와 레코드 조회 쿼리를 생성하는 메소드(교육청 자료 조회 관련 IR_MOUMI_TOT_REPORT 테이블에서 IS_APLIED에 따른 데이타 조회)
     * @param params - 검색조건맵
     * @참조페이지 - report_list.jsp / total_reports.jsp
     */
    public void createIR_TOT_REPORT_ID_Srch(Map params) {
        StringBuilder sbSearch = new StringBuilder();
        //String findOption = (String) params.get("findOption");

        sbSearch.append(" SELECT distinct A.id			  \n");
        sbSearch.append(" FROM  IFDBMGR.IR_MOUMI_TOT_REPORT A      \n");
        sbSearch.append(" WHERE DOC_ID    = :docID                \n");
        sbSearch.append(" AND   IS_APLIED = :isAplied             \n");

        //레코드갯수를 구하는 부모클래스의 함수를 호출하기 위해서 멤버변수에 저장
        this.setSearchQuery(sbSearch);
        //조건맵을 멤버변수에 저장
        this.setParams(params);

        //검색쿼리를 멤버변수에 저장
        //this.setSearchQuery(sbSearch);

       // LOG.debug("접수자료 query={}", sbSearch.toString());
}


}
