<commit_before>package com.puyixiaowo.fblog.controller.tools.books;

import com.puyixiaowo.core.entity.RowBounds;
import com.puyixiaowo.fblog.bean.admin.UserBean;
import com.puyixiaowo.fblog.bean.admin.book.BookBean;
import com.puyixiaowo.fblog.bean.admin.book.BookChapterBean;
import com.puyixiaowo.fblog.bean.admin.book.BookReadBean;
import com.puyixiaowo.fblog.bean.admin.book.BookSource;
import com.puyixiaowo.fblog.bean.sys.PageBean;
import com.puyixiaowo.fblog.bean.sys.ResponseBean;
import com.puyixiaowo.fblog.constants.Constants;
import com.puyixiaowo.fblog.controller.BaseController;
import com.puyixiaowo.fblog.freemarker.FreeMarkerTemplateEngine;
import com.puyixiaowo.fblog.service.book.BookChapterService;
import com.puyixiaowo.fblog.service.book.BookReadService;
import com.puyixiaowo.fblog.service.book.BookService;
import com.puyixiaowo.fblog.service.book.BookshelfService;
import com.puyixiaowo.fblog.utils.DBUtils;
import com.puyixiaowo.fblog.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Moses
 * @date 2017-12-18
 */
public class BookController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BookController.class);


    public static Object userBooks(Request request, Response response) {
        Map<String, Object> model = new HashMap<>();

        PageBean pageBean = getPageBean(request);


        try {
            UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);
            List<BookBean> list = BookService.getUserBookList(userBean.getId());
            pageBean.setList(list);
        } catch (Exception e) {
            pageBean.error(e);
        }

        UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);

        model.put("userBean", userBean);
        model.put("pageBean", pageBean);
        return new FreeMarkerTemplateEngine()
                .render(new ModelAndView(model, "tools/book/book_index.html"));
    }

    public static Object bookDetail(Request request, Response response) {
        Map<String, Object> model = new HashMap<>();

        BookBean bookBean = null;
        try {

            bookBean = getParamsEntity(request, BookBean.class, true);
            bookBean = BookService.requestBookDetail(bookBean);

            //是否在书架里
            bookBean.setOnShelf(BookshelfService.isBookOnShelf(request.session().attribute(Constants.SESSION_USER_KEY),
                    bookBean));
            //保存或更新书籍信息
            BookBean bookBeanDB = BookService.selectBookBeanByAId(bookBean.getaId());
            if (bookBeanDB != null) {
                bookBean.setId(bookBeanDB.getId());
            }
            DBUtils.insertOrUpdate(bookBean, false);
        } catch (Exception e) {
            logger.error("[书]获取章节列表异常：" + e.getMessage());
        }

        model.put("book", bookBean);

        return new FreeMarkerTemplateEngine()
                .render(new ModelAndView(model, "tools/book/book_detail.html"));
    }

    public static Object chapterContent(Request request, Response response) {
        //章节列表选择章节时有link
        String link = request.queryParams("link");
        //必传,书籍详情页面看书只有bookId
        String bookIdStr = request.queryParams("bookId");
        //仅用于接口获取到章节名为.时显示
        String chapterName = request.queryParams("chapterName");
        //1下一章，-1上一章,0最后阅读章
        Integer page = Integer.valueOf(request.queryParamOrDefault("page", "0"));

        if (StringUtils.isBlank(bookIdStr)) {
            return "bookId不可为空";
        }


        Long bookId = Long.valueOf(bookIdStr);
        Map<String, Object> model = new HashMap<>();

        try {
            UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);
            //读取读书配置
            BookReadBean bookReadBean = BookReadService.getUserReadConfig(userBean.getId(), bookId);

            BookBean bookBean = BookService.selectBookBeanById(bookId);

            if (bookBean == null) {
                return null;
            }

            BookChapterBean bookChapterBean = null;

            if (page != 0) {
                //上一章或下一章
                bookChapterBean = BookChapterService.getNextChapter(page, userBean.getId(),
                        bookId,
                        bookBean.getaId(),
                        bookReadBean.getLastReadingChapter());

                if (bookChapterBean == null) {
                    //首页或尾页跳转到书籍详情页
                    response.redirect("/book/detail?aId=" + bookBean.getaId());
                    return null;
                }

                link = bookChapterBean.getLink();
                bookReadBean.setLastReadingChapterLink(link);
                bookReadBean.setLastReadingChapter(bookChapterBean.getTitle());
            } else if (StringUtils.isBlank(link)) {

                if (bookReadBean != null) {
                    link = bookReadBean.getLastReadingChapterLink();
                } else {
                    //获取第一章
                    bookChapterBean = BookChapterService
                            .requestFirstBookChapters(userBean.getId(), bookId, bookBean.getaId());
                    link = bookChapterBean.getLink();
                }
            }
            //保存读书配置
            ResponseBean responseBean = BookReadService.saveBookRead(bookReadBean);<commit_msg>:smile:<commit_after>package com.puyixiaowo.fblog.controller.tools.books;

import com.puyixiaowo.core.entity.RowBounds;
import com.puyixiaowo.fblog.bean.admin.UserBean;
import com.puyixiaowo.fblog.bean.admin.book.BookBean;
import com.puyixiaowo.fblog.bean.admin.book.BookChapterBean;
import com.puyixiaowo.fblog.bean.admin.book.BookReadBean;
import com.puyixiaowo.fblog.bean.admin.book.BookSource;
import com.puyixiaowo.fblog.bean.sys.PageBean;
import com.puyixiaowo.fblog.bean.sys.ResponseBean;
import com.puyixiaowo.fblog.constants.Constants;
import com.puyixiaowo.fblog.controller.BaseController;
import com.puyixiaowo.fblog.freemarker.FreeMarkerTemplateEngine;
import com.puyixiaowo.fblog.service.book.BookChapterService;
import com.puyixiaowo.fblog.service.book.BookReadService;
import com.puyixiaowo.fblog.service.book.BookService;
import com.puyixiaowo.fblog.service.book.BookshelfService;
import com.puyixiaowo.fblog.utils.DBUtils;
import com.puyixiaowo.fblog.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Moses
 * @date 2017-12-18
 */
public class BookController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BookController.class);


    public static Object userBooks(Request request, Response response) {
        Map<String, Object> model = new HashMap<>();

        PageBean pageBean = getPageBean(request);


        try {
            UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);
            List<BookBean> list = BookService.getUserBookList(userBean.getId());
            pageBean.setList(list);
        } catch (Exception e) {
            pageBean.error(e);
        }

        UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);

        model.put("userBean", userBean);
        model.put("pageBean", pageBean);
        return new FreeMarkerTemplateEngine()
                .render(new ModelAndView(model, "tools/book/book_index.html"));
    }

    public static Object bookDetail(Request request, Response response) {
        Map<String, Object> model = new HashMap<>();

        BookBean bookBean = null;
        try {

            bookBean = getParamsEntity(request, BookBean.class, true);
            bookBean = BookService.requestBookDetail(bookBean);

            //是否在书架里
            bookBean.setOnShelf(BookshelfService.isBookOnShelf(request.session().attribute(Constants.SESSION_USER_KEY),
                    bookBean));
            //保存或更新书籍信息
            BookBean bookBeanDB = BookService.selectBookBeanByAId(bookBean.getaId());
            if (bookBeanDB != null) {
                bookBean.setId(bookBeanDB.getId());
            }
            DBUtils.insertOrUpdate(bookBean, false);
        } catch (Exception e) {
            logger.error("[书]获取章节列表异常：" + e.getMessage());
        }

        model.put("book", bookBean);

        return new FreeMarkerTemplateEngine()
                .render(new ModelAndView(model, "tools/book/book_detail.html"));
    }

    public static Object chapterContent(Request request, Response response) {
        //章节列表选择章节时有link
        String link = request.queryParams("link");
        //必传,书籍详情页面看书只有bookId
        String bookIdStr = request.queryParams("bookId");
        //仅用于接口获取到章节名为.时显示
        String chapterName = request.queryParams("chapterName");
        //1下一章，-1上一章,0最后阅读章
        Integer page = Integer.valueOf(request.queryParamOrDefault("page", "0"));

        if (StringUtils.isBlank(bookIdStr)) {
            return "bookId不可为空";
        }


        Long bookId = Long.valueOf(bookIdStr);
        Map<String, Object> model = new HashMap<>();

        try {
            UserBean userBean = request.session().attribute(Constants.SESSION_USER_KEY);
            //读取读书配置
            BookReadBean bookReadBean = BookReadService.getUserReadConfig(userBean.getId(), bookId);

            BookBean bookBean = BookService.selectBookBeanById(bookId);

            if (bookBean == null) {
                return null;
            }

            BookChapterBean bookChapterBean = null;

            if (page != 0) {
                //上一章或下一章
                bookChapterBean = BookChapterService.getNextChapter(page, userBean.getId(),
                        bookId,
                        bookBean.getaId(),
                        bookReadBean.getLastReadingChapter());

                if (bookChapterBean == null) {
                    //首页或尾页跳转到书籍详情页
                    response.redirect("/book/detail?aId=" + bookBean.getaId());
                    return null;
                }

                link = bookChapterBean.getLink();
                chapterName = bookChapterBean.getTitle();
            } else if (StringUtils.isBlank(link)) {

                if (bookReadBean != null) {
                    link = bookReadBean.getLastReadingChapterLink();
                } else {
                    //获取第一章
                    bookChapterBean = BookChapterService
                            .requestFirstBookChapters(userBean.getId(), bookId, bookBean.getaId());
                    link = bookChapterBean.getLink();
                }
            }
            //保存读书配置
            bookReadBean.setLastReadingChapterLink(link);
            bookReadBean.setLastReadingChapter(chapterName);
            ResponseBean responseBean = BookReadService.saveBookRead(bookReadBean);
{'fraction_non_alphanumeric': 0.10368103681036811, 'fraction_numerical': 0.0027000270002700026, 'mean_word_length': 2.4169741697416973, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '19785', 'n_tokens_mistral': 3373, 'n_tokens_neox': 3344, 'n_words': 513}