/*
 * 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.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
import kr.co.kihyun.lang.MString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Kts
 */
public class BoardService  extends BaseService {
    private static final Logger LOG = LoggerFactory.getLogger(BoardService.class);

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

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

    public void createBoardListQuery(Map params) throws Exception{
        StringBuilder sbSearch = new StringBuilder();
        String findOption = (String) params.get("findOption");
        String findWord = (String) params.get("findWord");

        sbSearch.append("SELECT                                                    \n");
        sbSearch.append("  A0.POPUP_YN AS popupYn                                  \n");
        sbSearch.append("  , A0.ID AS id                                           \n");
        sbSearch.append("  , A0.WRITE_DATE AS writeDate                            \n");
        sbSearch.append("  , A0.TITLE AS title                                     \n");
        sbSearch.append("  , A0.CONTENTS AS contents                                     \n");
        sbSearch.append("  , SP_CM_DEC_FU(US.NAME) AS userName                     \n");
        sbSearch.append("  , DT.NAME AS deptName                                   \n");
        sbSearch.append("  , A0.VISIT AS visit                                     \n");
        sbSearch.append("  , CASE WHEN A0.ATTACHMENTS IS NOT NULL THEN 'Y' ELSE 'N' END isAttachments \n");
//        sbSearch.append("  , ROWNUMBER() OVER() AS rowNo                           \n");
        sbSearch.append("FROM                                                      \n");
        sbSearch.append("  MOUMI_BOARD A0                                          \n");
        sbSearch.append("  ,MOUMI_MUSER US                                         \n");
        sbSearch.append("  ,MOUMI_DEPT DT                                          \n");
        sbSearch.append("WHERE                                                     \n");
        sbSearch.append("  US.ID = A0.USER_ID                                      \n");
        sbSearch.append("  AND US.DEPT_ID = DT.ID                                  \n");
        sbSearch.append("  AND US.DEPT_ID = DT.ID                                  \n");
        sbSearch.append("  AND A0.GROUP_ID = :groupId                              \n");
        if (!MString.isNull(findOption) && !MString.isNull(findWord)) {
            if (findOption.equals("title")) {
                sbSearch.append("  AND A0.TITLE LIKE '%' || :findWord || '%'               \n");
            }else if (findOption.equals("mUser.name")) {
                sbSearch.append("  AND SP_CM_DEC_FU(US.NAME) LIKE '%' || :findWord || '%'  \n");
            }
        }

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

        sbSearch.append("ORDER BY WRITEDATE DESC                                   \n");

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

        if( LOG.isDebugEnabled() ) {
            LOG.debug("query={}", sbSearch.toString());
        }
    }

    public void isAttachment(Map params) throws Exception{
        StringBuilder sb = new StringBuilder();

        Long boardId = (Long) params.get("boardId");


        sb.append(" SELECT ID as id \n");
        sb.append(" ,TITLE as title \n");
        sb.append(" ,CONTENTS as contents \n");
        sb.append(" ,CASE WHEN ATTACHMENTS IS NOT NULL THEN 'Y' ELSE 'N' END isAttachments \n");
        sb.append(" FROM MOUMI_BOARD  \n");
        sb.append(" WHERE ID = :boardId  \n");

        this.setCountQuery(sb);
        this.setParams(params);
        this.setSearchQuery(sb);

    }
}
