From ace84ef155d811149509ce2eb1aad96bbd0622db Mon Sep 17 00:00:00 2001
From: xxy <1179705413@qq.com>
Date: Sun, 1 Dec 2019 13:32:37 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0=E5=85=A5=E4=B9=A6?=
 =?UTF-8?q?=E6=9E=B6=E7=9A=84=E4=B9=A6=E7=B1=8D=E4=B8=8B=E6=AC=A1=E5=8F=AF?=
 =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E4=B8=8A=E6=AC=A1=E9=98=85=E8=AF=BB=E8=AE=B0?=
 =?UTF-8?q?=E5=BD=95=E9=98=85=E8=AF=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../books/mapper/UserRefBookMapper.java       |  4 ++++
 .../books/service/BookService.java            | 15 +++++++++-----
 .../books/service/UserService.java            |  4 ++++
 .../books/web/ApiBookController.java          |  6 +++---
 .../books/web/BookController.java             | 20 ++++++++++++++++---
 .../mybatis/mapping/UserRefBookMapper.xml     | 11 ++++++++++
 .../templates/books/book_content.html         | 11 ++++++++--
 .../templates/books/book_search.html          |  4 ++--
 sql/2019-12-01.sql                            |  1 +
 sql/books.sql                                 |  2 +-
 10 files changed, 62 insertions(+), 16 deletions(-)
 create mode 100644 sql/2019-12-01.sql

diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/mapper/UserRefBookMapper.java b/novel-front/src/main/java/xyz/zinglizingli/books/mapper/UserRefBookMapper.java
index 34414b1..0c7020b 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/mapper/UserRefBookMapper.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/mapper/UserRefBookMapper.java
@@ -28,4 +28,8 @@ public interface UserRefBookMapper {
     int updateByPrimaryKeySelective(UserRefBook record);
 
     int updateByPrimaryKey(UserRefBook record);
+
+    void updateNewstIndex(@Param("bookId") Long bookId,@Param("userId") String userId,@Param("indexNum") Integer indexNum);
+
+    Integer queryBookIndexNumber(@Param("userId") String userId,@Param("bookId") Long bookId);
 }
\ No newline at end of file
diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java b/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java
index a7b30cd..4de3098 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java
@@ -17,10 +17,7 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.client.RestTemplate;
 import tk.mybatis.orderbyhelper.OrderByHelper;
 import xyz.zinglizingli.books.constant.CacheKeyConstans;
-import xyz.zinglizingli.books.mapper.BookContentMapper;
-import xyz.zinglizingli.books.mapper.BookIndexMapper;
-import xyz.zinglizingli.books.mapper.BookMapper;
-import xyz.zinglizingli.books.mapper.ScreenBulletMapper;
+import xyz.zinglizingli.books.mapper.*;
 import xyz.zinglizingli.books.po.*;
 import xyz.zinglizingli.common.cache.CommonCacheUtil;
 import xyz.zinglizingli.common.utils.RestTemplateUtil;
@@ -45,6 +42,9 @@ public class BookService {
     @Autowired
     private ScreenBulletMapper screenBulletMapper;
 
+    @Autowired
+    private UserRefBookMapper userRefBookMapper;
+
     @Autowired
     private CommonCacheUtil cacheUtil;
 
@@ -330,9 +330,14 @@ public class BookService {
         return contentBuilder.toString();
     }
 
-    public void addVisitCount(Long bookId) {
+    public void addVisitCount(Long bookId, String userId, Integer indexNum) {
 
         bookMapper.addVisitCount(bookId);
+
+        if(org.apache.commons.lang3.StringUtils.isNotBlank(userId)) {
+            userRefBookMapper.updateNewstIndex(bookId, userId, indexNum);
+        }
+
     }
 
     public String queryIndexNameByBookIdAndIndexNum(Long bookId, Integer indexNum) {
diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/service/UserService.java b/novel-front/src/main/java/xyz/zinglizingli/books/service/UserService.java
index 2444318..9f8f765 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/service/UserService.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/service/UserService.java
@@ -83,4 +83,8 @@ public class UserService {
             addToCollect(bookId, userid);
         }
     }
+
+    public Integer queryBookIndexNumber(String userId, Long bookId) {
+        return userRefBookMapper.queryBookIndexNumber(userId,bookId);
+    }
 }
diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/web/ApiBookController.java b/novel-front/src/main/java/xyz/zinglizingli/books/web/ApiBookController.java
index e643dce..cf754b9 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/web/ApiBookController.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/web/ApiBookController.java
@@ -176,13 +176,13 @@ public class ApiBookController {
         return modelMap;
     }
 
-    @RequestMapping("addVisit")
+    /*@RequestMapping("addVisit")
     public String addVisit(@RequestParam("bookId") Long bookId) {
 
-        bookService.addVisitCount(bookId);
+        bookService.addVisitCount(bookId, userId, indexNum);
 
         return "ok";
-    }
+    }*/
 
 
 }
diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java b/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java
index e37ef74..a9e449e 100644
--- a/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java
+++ b/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java
@@ -17,6 +17,7 @@ import xyz.zinglizingli.books.po.BookContent;
 import xyz.zinglizingli.books.po.BookIndex;
 import xyz.zinglizingli.books.po.ScreenBullet;
 import xyz.zinglizingli.books.service.BookService;
+import xyz.zinglizingli.books.service.UserService;
 import xyz.zinglizingli.books.vo.BookVO;
 import xyz.zinglizingli.common.cache.CommonCacheUtil;
 
@@ -35,6 +36,9 @@ public class BookController {
     @Autowired
     private BookService bookService;
 
+    @Autowired
+    private UserService userService;
+
     @Autowired
     private CommonCacheUtil commonCacheUtil;
 
@@ -187,7 +191,16 @@ public class BookController {
     }
 
     @RequestMapping("{bookId}.html")
-    public String detail(@PathVariable("bookId") Long bookId, ModelMap modelMap) {
+    public String detail(@PathVariable("bookId") Long bookId, @RequestParam(value = "token",required = false)String token, ModelMap modelMap) {
+        String userId = commonCacheUtil.get(token);
+        if(org.apache.commons.lang3.StringUtils.isNotBlank(userId)){
+            Integer indexNumber = userService.queryBookIndexNumber(userId,bookId);
+            if(indexNumber!=null){
+                return "redirect:/book/"+bookId+"/"+indexNumber+".html";
+            }
+
+        }
+
         //查询基本信息
         Book book = bookService.queryBaseInfo(bookId);
         //查询最新目录信息
@@ -260,9 +273,10 @@ public class BookController {
 
     @RequestMapping("addVisit")
     @ResponseBody
-    public String addVisit(@RequestParam("bookId") Long bookId) {
+    public String addVisit(@RequestParam("bookId") Long bookId,@RequestParam("indexNum") Integer indexNum,@RequestParam("token") String token) {
+        String userId = commonCacheUtil.get(token);
 
-        bookService.addVisitCount(bookId);
+        bookService.addVisitCount(bookId,userId,indexNum);
 
         return "ok";
     }
diff --git a/novel-front/src/main/resources/mybatis/mapping/UserRefBookMapper.xml b/novel-front/src/main/resources/mybatis/mapping/UserRefBookMapper.xml
index 89c23f8..f4911ac 100644
--- a/novel-front/src/main/resources/mybatis/mapping/UserRefBookMapper.xml
+++ b/novel-front/src/main/resources/mybatis/mapping/UserRefBookMapper.xml
@@ -178,4 +178,15 @@
       book_id = #{bookId,jdbcType=BIGINT}
     where id = #{id,jdbcType=BIGINT}
   </update>
+
+  <update id="updateNewstIndex">
+    update user_ref_book
+    set index_num = #{indexNum}
+    where book_id = #{bookId} and user_id = #{userId}
+  </update>
+
+  <select id="queryBookIndexNumber" resultType="java.lang.Integer">
+     select index_num from user_ref_book
+     where book_id = #{bookId} and user_id = #{userId}
+  </select>
 </mapper>
\ No newline at end of file
diff --git a/novel-front/src/main/resources/templates/books/book_content.html b/novel-front/src/main/resources/templates/books/book_content.html
index a5ae916..971c767 100644
--- a/novel-front/src/main/resources/templates/books/book_content.html
+++ b/novel-front/src/main/resources/templates/books/book_content.html
@@ -375,6 +375,14 @@
 
 <input type="hidden" id="contentIdHidden" th:value="${bookContent.id}"/>
 
+<input type="hidden" id="indexNumHidden" th:value="${bookContent.indexNum}"/>
+
+<script>
+    var token = localStorage.getItem("token");
+    $.get("/book/addVisit", {"bookId": $("#bookIdHidden").val(),"indexNum":$("#indexNumHidden").val(),"token":token}, function () {
+    });
+</script>
+
 <div style="height: 50px;line-height: 50px;text-align: center" class="layui-header header header-doc  layui-bg-cyan">
 
     <div style="width:10%;float: left;margin-left: 10px">
@@ -458,8 +466,7 @@
         isMobile = isIphone || isAndroid;
 
 
-    $.get("/book/addVisit", {"bookId": $("#bookIdHidden").val()}, function () {
-    });
+
 
 </script>
 <script>
diff --git a/novel-front/src/main/resources/templates/books/book_search.html b/novel-front/src/main/resources/templates/books/book_search.html
index 69b601f..3338bd6 100644
--- a/novel-front/src/main/resources/templates/books/book_search.html
+++ b/novel-front/src/main/resources/templates/books/book_search.html
@@ -112,14 +112,14 @@
 
 
 <div th:each="book : ${books}" class="layui-row" style="margin-bottom:10px;padding:10px;background: #f2f2f2">
-    <a th:href="'/book/'+ ${book.id} + '.html'">
+    <a th:href="'/book/'+ ${book.id} + '.html'+ ${token!=null?'?token='+token:''}">
     <div class="layui-col-xs6 layui-col-sm3 layui-col-md2 layui-col-lg2" style="text-align: center">
         <img align="center"
              th:src="${book.picUrl}"/>
     </div>
     </a>
     <div style="padding: 20px" class="layui-col-xs6 layui-col-sm8 layui-col-md8 layui-col-lg8">
-        <a th:href="'/book/'+ ${book.id} + '.html'">
+        <a th:href="'/book/'+ ${book.id} + '.html'+ ${token!=null?'?token='+token:''}">
             <div class="line-limit-length" style=";color: #4c6978;font-weight: bold;font-size: 15px"
                  th:text="${book.bookName}"></div>
         </a>
diff --git a/sql/2019-12-01.sql b/sql/2019-12-01.sql
new file mode 100644
index 0000000..714b8a8
--- /dev/null
+++ b/sql/2019-12-01.sql
@@ -0,0 +1 @@
+alter table user_ref_book add column `index_num` int(5);
\ No newline at end of file
diff --git a/sql/books.sql b/sql/books.sql
index 2408626..45f5876 100644
--- a/sql/books.sql
+++ b/sql/books.sql
@@ -1009,5 +1009,5 @@ INSERT INTO `sys_dict` (`id`, `name`, `value`, `type`, `description`, `sort`, `p
 INSERT INTO `sys_dict` (`id`, `name`, `value`, `type`, `description`, `sort`, `parent_id`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`) VALUES ('128', '网游竞技', '6', 'novel_category', '小说分类', '6', NULL, NULL, NULL, NULL, NULL, '', NULL);
 INSERT INTO `sys_dict` (`id`, `name`, `value`, `type`, `description`, `sort`, `parent_id`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`) VALUES ('129', '女生频道', '7', 'novel_category', '小说分类', '7', NULL, NULL, NULL, NULL, NULL, '', NULL);
 
-
+alter table user_ref_book add column `index_num` int(5);