package com.jakduk.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.jakduk.common.CommonConst;
import com.jakduk.model.db.BoardSequence;
import com.jakduk.model.web.BoardPageInfo;

/**
 * @author <a href="mailto:phjang1983@daum.net">Jang,Pyohwan</a>
 * @company  : http://jakduk.com
 * @date     : 2014. 5. 24.
 * @desc     : 공통으로 쓰이는 서비스
 */

@Service
public class CommonService {
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 게시판의 차기 글번호를 가져온다.
	 * @param name 게시판 ID
	 * @return 다음 글번호
	 */
	public Integer getNextSequence(Integer name) {
		
		Integer nextSeq = -1;
		
		Query query = new Query();
		query.addCriteria(Criteria.where("name").is(name));
		
		Update update = new Update();
		update.inc("seq", 1);
		
		FindAndModifyOptions options = new FindAndModifyOptions();
		options.returnNew(true);
		
		BoardSequence board = mongoTemplate.findAndModify(query, update, options, BoardSequence.class);
		
		if (board == null) {
			logger.debug("err result=" + board);
			return nextSeq;
		} else {
			nextSeq = board.getSeq();
			return nextSeq;
		}
	}

	/**
	 * 게시판 목록의 페이지에 대한 정보를 넘겨준다.
	 * @param page 현재 페이지 번호
	 * @param numberPosts 전체 게시물 갯수
	 * @param numberPages 전체 페이지 수
	 * @return BoardPageInfo 객체
	 */
	public BoardPageInfo getCountPages(Long page, Long numberPosts, Integer numberPages) {
		
		BoardPageInfo boardPageInfo = new BoardPageInfo();

		Long totalPages = numberPosts / CommonConst.BOARD_LINE_NUMBER;
		Long prevPage = (long) 1;
		Long nextPage = (long) 1;
		
		if ((totalPages % CommonConst.BOARD_LINE_NUMBER) > 0 || (numberPosts - CommonConst.BOARD_LINE_NUMBER) <= 0) {
			totalPages++; 
		} else if ((totalPages % CommonConst.BOARD_LINE_NUMBER) == 0) {
			totalPages--;
		}
		
		Long startPage = page - (page % numberPages) + 1;
		
		if (startPage <= 0) {
			startPage ++;
		}
		
		if (startPage > numberPages) {
			prevPage = startPage - numberPages;
		} else {
			prevPage = (long) -1;
		}
		
		Long endPage = (long) 1;
		
		if ((totalPages - page) > numberPages) {
			endPage = startPage + numberPages - 1;
			nextPage = startPage + numberPages;
		} else {
			endPage = totalPages;
			nextPage = (long) -1;
		}
		
		boardPageInfo.setStartPage(startPage);
		boardPageInfo.setEndPage(endPage);
		boardPageInfo.setPrevPage(prevPage);
		boardPageInfo.setNextPage(nextPage);
		
		return boardPageInfo;
	}
}

@@ -1867,64 +1867,57 @@
 ng t
-otalPages = numberPosts / CommonConst.BOARD_LINE_NUMBER;
+mpVal = (long) 0;%0A%09%09Long totalPages = (long) 1;%09%09
 %0A%09%09L
@@ -1976,31 +1976,31 @@
 %0A%09%09%0A%09%09if
- 
 ((
-totalPages %25
+numberPosts -
  CommonC
@@ -2030,11 +2030,30 @@
  %3E 0
- %7C%7C
+) %7B%0A%09%09%09tmpVal = (long)
  (nu
@@ -2054,33 +2054,33 @@
 g) (numberPosts 
--
+/
  CommonConst.BOA
@@ -2098,58 +2098,31 @@
 BER)
- %3C= 0) %7B%0A%09%09%09totalPages++; %0A%09%09%7D else 
+;%0A%09%09%09%0A%09%09%09
 if
- 
 ((
-totalPage
+numberPost
 s %25 
@@ -2163,16 +2163,17 @@
 0) %7B%0A%09%09%09
+%09
 totalPag
@@ -2178,11 +2178,64 @@
 ages
---;
+ = tmpVal;%0A%09%09%09%7D else %7B%0A%09%09%09%09totalPages = tmpVal + 1;%0A%09%09%09%7D
 %0A%09%09%7D

Fix calculating borad paging