package nlib.bbs.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import nlib.bbs.service.ArticleVO;
import nlib.bbs.service.AttachFileService;
import nlib.bbs.service.AttachFileVO;
import nlib.bbs.service.BoardService;
import nlib.util.StringUtil;

public abstract class BoardServiceImpl implements BoardService
{
	static Logger log = LoggerFactory.getLogger(BoardServiceImpl.class);
	
	@Resource(name="attachFileService")
	AttachFileService attachFileService;
	
	public abstract BoardDAO getBoardDAO();
	
	/* 
	 * 게시글 목록을 조회한다. 
	 */
	public List<ArticleVO> listArticles(ArticleVO articleVO) throws Exception {
		
		return getBoardDAO().listArticles(articleVO);
	}
	
	/** 
	 * 게시글 건수를 조회한다.  
	 */
	public int countArticles(ArticleVO articleVO) throws Exception {
		
		return getBoardDAO().countArticles(articleVO);
	}
	

	/**
	 * 조회수를 증가시킨다. 
	 * 
	 * @param 
	 * @return
	 * @throws Exception
	 */
	public int updateRead(ArticleVO articleVO) throws Exception {
		return getBoardDAO().updateRead(articleVO);
	}
	
	/**
	 * 게시글 상세 내용을 조회한다.  
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	public ArticleVO selectArticle(ArticleVO articleVO) throws Exception {
		
		if(StringUtil.isEmpty(articleVO.getArticleId())) return null;
		
		// 게시글 조회 
		ArticleVO retVO = getBoardDAO().selectArticle(articleVO);
		// 첨부파일 조회 
		if(StringUtil.isNotEmpty(retVO.getBdAttachFileId())) {
			String attachFileId = retVO.getBdAttachFileId();
			List<AttachFileVO> listFile = attachFileService.listAttachFiles(attachFileId);
			retVO.setAttachFiles(listFile);
		}
		// 조회수 증가 
		updateRead(articleVO);
		
		return retVO;
	}
	
}