فهرست منبع

主编推荐 图文推荐图库-smart_recommand
更新主页 block 获取接口,新加是否连载字段。

tianyunperfect 3 سال پیش
والد
کامیت
91ee2e6c56

+ 27 - 11
book-server/src/main/java/com/book/server/controller/BookController.java

@@ -45,18 +45,32 @@ public class BookController extends BaseController {
     }
 
     /**
-     * 书城分页推荐
+     * 书城分页推荐-精彩推荐
+     * novel_end_recommend
      *
      * @param queryVO
      * @return
      */
     @PostMapping("/recommend")
-    public Result<List<Book>> recommendByRead(@RequestBody QueryVO queryVO) {
+    public Result<List<BookRes>> recommendByRead(@RequestBody QueryVO queryVO) {
         return Result.success(bookService.recommendByRead(queryVO));
     }
 
+    /**
+     * 主编推荐
+     * smart_recommand
+     *
+     * @param queryVO
+     * @return
+     */
+    @PostMapping("/smartRecommand")
+    public Result<List<BookRes>> smartRecommand(@RequestBody QueryVO queryVO) {
+        return Result.success(bookService.smartRecommand(queryVO));
+    }
+
     /**
      * 热门推荐4个
+     *
      * @param queryVO
      * @return
      */
@@ -73,7 +87,7 @@ public class BookController extends BaseController {
      * @return
      */
     @GetMapping("/getBlockByPageId")
-    @Cacheable(value = "block", key = "#pageId")
+    //@Cacheable(value = "block", key = "#pageId")
     public Result<List<BlockRes>> getBlockByPageId(Integer pageId) {
         List<BlockRes> list = bookService.getBlockByPageId(pageId);
         return Result.byObject(list);
@@ -109,22 +123,24 @@ public class BookController extends BaseController {
 
     /**
      * 获取章节列表
+     *
      * @param book
      * @return
      */
     @PostMapping("/chapterList")
-    public Result<List<Chapter>> getChaptersByPageAndRow(@RequestBody Map<String,String> book) {
-
-      List<Chapter>  chapterList =  bookService.getChaptersByBookId(book.get("bookId"));
-      if ( CollectionUtils.isEmpty(chapterList)){
-          return Result.failure(ResultCode.FAIL.getCode(),"哎呀~没有数据啦~");
-      }else {
-          return Result.byObject(chapterList);
-      }
+    public Result<List<Chapter>> getChaptersByPageAndRow(@RequestBody Map<String, String> book) {
+
+        List<Chapter> chapterList = bookService.getChaptersByBookId(book.get("bookId"));
+        if (CollectionUtils.isEmpty(chapterList)) {
+            return Result.failure(ResultCode.FAIL.getCode(), "哎呀~没有数据啦~");
+        } else {
+            return Result.byObject(chapterList);
+        }
     }
 
     /**
      * 获取热门搜索
+     *
      * @param sex
      * @return
      */

+ 5 - 1
book-server/src/main/java/com/book/server/dao/mapper/BookMapper2.java

@@ -12,5 +12,9 @@ import java.util.List;
 @Repository
 public interface BookMapper2 {
     List<BookRes> queryBookRes(@Param("queryVO") QueryVO queryVO, @Param("page") int page, @Param("size") int size);
-    List<BookRes> queryReturnRecommand(@Param("queryVO") QueryVO queryVO, @Param("page") int page, @Param("size") int size);
+    List<BookRes> topRecommend(@Param("queryVO") QueryVO queryVO, @Param("page") int page, @Param("size") int size);
+
+    List<BookRes> novelEndRecommend(@Param("queryVO") QueryVO queryVO, @Param("page") int page, @Param("size") int size);
+
+    List<BookRes> smartRecommand(@Param("queryVO") QueryVO queryVO);
 }

+ 17 - 0
book-server/src/main/java/com/book/server/dao/mapper/ManageBlockResource2Mapper.java

@@ -0,0 +1,17 @@
+package com.book.server.dao.mapper;
+
+import com.book.server.dao.entity.ManageBlockResource;
+import com.book.server.dao.entity.example.ManageBlockResourceExample;
+import com.book.server.model.VO.ManageBlockResourceRes;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Mapper
+@Repository
+public interface ManageBlockResource2Mapper {
+
+    List<ManageBlockResourceRes> getBooks(@Param("blockIds") List<Integer> blockIds);
+}

+ 2 - 1
book-server/src/main/java/com/book/server/model/VO/BlockRes.java

@@ -4,11 +4,12 @@ import com.book.server.dao.entity.ManageBlockResource;
 import lombok.Data;
 
 import java.util.List;
+import java.util.TreeSet;
 
 @Data
 public class BlockRes {
     String name;
     String secondName;
     String type;
-    List<ManageBlockResourceRes> resources;
+    TreeSet<ManageBlockResourceRes> resources;
 }

+ 2 - 0
book-server/src/main/java/com/book/server/model/VO/ManageBlockResourceRes.java

@@ -7,4 +7,6 @@ import lombok.Data;
 public class ManageBlockResourceRes extends ManageBlockResource {
     private String bookName;
     private String bookDescription;
+
+    private String isFinish;
 }

+ 3 - 1
book-server/src/main/java/com/book/server/service/BookService.java

@@ -12,7 +12,7 @@ import java.util.List;
 
 public interface BookService {
     List<BookRes> query(QueryVO queryVO);
-    List<Book> recommendByRead(QueryVO queryVO);
+    List<BookRes> recommendByRead(QueryVO queryVO);
 
     List<BlockRes> getBlockByPageId(Integer pageId);
 
@@ -23,4 +23,6 @@ public interface BookService {
     List<SearchKeyword> getSearchKeywordBySex(String sex);
 
     List<BookRes> topRecommend(QueryVO queryVO);
+
+    List<BookRes> smartRecommand(QueryVO queryVO);
 }

+ 34 - 50
book-server/src/main/java/com/book/server/service/impl/BookServiceImpl.java

@@ -1,7 +1,6 @@
 package com.book.server.service.impl;
 
 
-import com.book.server.common.entity.Result;
 import com.book.server.common.util.JsonUtils;
 import com.book.server.dao.entity.*;
 import com.book.server.dao.entity.example.*;
@@ -40,6 +39,8 @@ public class BookServiceImpl implements BookService {
     private BookCategoryMapper bookCategoryMapper;
     @Autowired
     private SearchKeywordMapper searchKeywordMapper;
+    @Autowired
+    private ManageBlockResource2Mapper manageBlockResource2Mapper;
 
     public List<BookCategory> getAllCategory() {
         List<BookCategory> bookCategories = bookCategoryMapper.selectByExample(
@@ -57,14 +58,13 @@ public class BookServiceImpl implements BookService {
     }
 
     @Override
-    public List<Book> recommendByRead(QueryVO queryVO) {
-        BookExample example = BookExample.newAndCreateCriteria()
-                .andStateEqualTo("1")
-                .andSexEqualTo(queryVO.getPageId())
-                .example();
-        example.orderBy(Book.Column.readNum.desc());
-        example.page(queryVO.getPage(), queryVO.getSize());
-        List<Book> books = bookMapper.selectByExample(example);
+    public List<BookRes> recommendByRead(QueryVO queryVO) {
+        ArrayList<BookRes> bookRes = new ArrayList<>();
+        if (queryVO.getPage() == 5) {
+            return bookRes;
+        }
+        List<BookRes> books = bookMapper2.novelEndRecommend(queryVO,
+                queryVO.getPage() * queryVO.getSize(), queryVO.getSize());
         return books;
     }
 
@@ -78,48 +78,26 @@ public class BookServiceImpl implements BookService {
         manageBlockExample.orderBy(ManageBlock.Column.weigh.desc());
         List<ManageBlock> manageBlocks = manageBlockMapper.selectByExample(manageBlockExample);
 
-        // 获取板块内部数据
-        for (ManageBlock manageBlock : manageBlocks) {
-            ManageBlockResourceExample manageBlockResourceExample = ManageBlockResourceExample.newAndCreateCriteria()
-                    .andBlockIdEqualTo(manageBlock.getId())
-                    .example();
-            manageBlockResourceExample.orderBy(ManageBlockResource.Column.weigh.desc());
-            //获取内部书籍资源
-            List<ManageBlockResource> manageBlockResources = manageBlockResourceMapper.selectByExample(manageBlockResourceExample);
-            ArrayList<ManageBlockResourceRes> manageBlockResourceResList = new ArrayList<>();
-            if (manageBlockResources.size() > 0) {
-                List<Long> collect = manageBlockResources.stream()
-                        .map(ManageBlockResource::getBookId).collect(Collectors.toList());
-                List<Book> books = bookMapper.selectByExample(
-                        BookExample.newAndCreateCriteria()
-                                .andIdIn(collect)
-                                .example()
-                );
-                HashMap<Long, Book> bookHashMap = new HashMap<>();
-                books.forEach(x -> {
-                    bookHashMap.put(x.getId(), x);
-                });
-                manageBlockResources.forEach(x -> {
-                    ManageBlockResourceRes manageBlockResourceRes = new ManageBlockResourceRes();
-                    BeanUtils.copyProperties(x, manageBlockResourceRes);
-                    manageBlockResourceResList.add(manageBlockResourceRes);
-
-                    Book book = bookHashMap.get(x.getBookId());
-                    if (book != null) {
-                        manageBlockResourceRes.setBookName(book.getName());
-                        manageBlockResourceRes.setBookDescription(book.getDescription());
-                    }
-
-                });
-            }
-            BlockRes blockRes = new BlockRes();
-            blockRes.setName(manageBlock.getName());
-            blockRes.setSecondName(manageBlock.getSecondName());
-            blockRes.setType(manageBlock.getType());
-            blockRes.setResources(manageBlockResourceResList);
 
+        // 获取已有版本的所有书籍
+        List<Integer> blockIds = manageBlocks.stream().map(ManageBlock::getId).collect(Collectors.toList());
+        List<ManageBlockResourceRes> blockResourceResList = manageBlockResource2Mapper.getBooks(blockIds);
+
+        // 合并相同 block 书籍
+        HashMap<Integer, TreeSet<ManageBlockResourceRes>> map = new HashMap<>();
+        blockResourceResList.forEach(x -> {
+            TreeSet<ManageBlockResourceRes> set = map.computeIfAbsent(x.getBlockId(), k -> new TreeSet<>((o1, o2) -> o2.getWeigh() - o1.getWeigh()));
+            set.add(x);
+        });
+
+        manageBlocks.forEach(x -> {
+            BlockRes blockRes = new BlockRes();
+            blockRes.setName(x.getName());
+            blockRes.setType(x.getType());
+            blockRes.setSecondName(x.getSecondName());
+            blockRes.setResources(map.getOrDefault(x.getId(), new TreeSet<>()));
             list.add(blockRes);
-        }
+        });
         return list;
     }
 
@@ -278,10 +256,16 @@ public class BookServiceImpl implements BookService {
 
     @Override
     public List<BookRes> topRecommend(QueryVO queryVO) {
-        List<BookRes> bookRes = bookMapper2.queryReturnRecommand(queryVO, 0, 20);
+        List<BookRes> bookRes = bookMapper2.topRecommend(queryVO, 0, 20);
         return getRandom(bookRes);
     }
 
+    @Override
+    public List<BookRes> smartRecommand(QueryVO queryVO) {
+        List<BookRes> bookRes = bookMapper2.smartRecommand(queryVO);
+        return bookRes;
+    }
+
     private List<BookRes> getRandom(List<BookRes> bookRes) {
         if (bookRes.size() <= 4) {
             return bookRes;

+ 24 - 3
book-server/src/main/resources/mapper/Book2Mapper.xml

@@ -69,7 +69,7 @@
           and (t.`name` like #{queryVO.query} or t.author like #{queryVO.query}) limit #{page}
             , #{size};
     </select>
-    <select id="queryReturnRecommand" resultMap="BaseResultMap">
+    <select id="topRecommend" resultMap="BaseResultMap">
         SELECT t.*,
                c.`name`  as "categoryName"
         FROM book t
@@ -79,8 +79,29 @@
             FROM return_recommand
             WHERE sex = #{queryVO.sex}
               AND STATUS = 'normal'
-        ) LIMIT #{page},
-            #{size}
+        ) LIMIT #{page},#{size}
+    </select>
+    <select id="novelEndRecommend" resultMap="BaseResultMap">
+        SELECT t.*,c.name AS "categoryName"  FROM
+            (
+                SELECT t.*,b.*  FROM
+                    (
+                        SELECT book_id,weight FROM novel_end_recommend
+                        WHERE sex = #{queryVO.sex} AND STATUS = 'normal'
+                    ) t
+                        LEFT JOIN book b ON b.id = t.book_id
+            ) t JOIN book_category c on t.book_category_id=c.id ORDER BY weight DESC  LIMIT #{page},#{size}
+    </select>
+    <select id="smartRecommand" resultMap="BaseResultMap">
+        SELECT t.*,c.name AS "categoryName"  FROM
+            (
+                SELECT t.*,b.*  FROM
+                    (
+                        SELECT book_id FROM smart_recommand
+                        WHERE sex = #{queryVO.sex} AND STATUS = 'normal'
+                    ) t
+                        LEFT JOIN book b ON b.id = t.book_id
+            ) t JOIN book_category c on t.book_category_id=c.id
     </select>
 
 </mapper>

+ 42 - 0
book-server/src/main/resources/mapper/ManageBlockResource2Mapper.xml

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.book.server.dao.mapper.ManageBlockResource2Mapper">
+  <resultMap id="BaseResultMap" type="com.book.server.model.VO.ManageBlockResourceRes">
+    <!--
+      WARNING - @mbg.generated
+      This element is automatically generated by MyBatis Generator, do not modify.
+      This element was generated on Sun Aug 15 14:43:42 CST 2021.
+    -->
+    <id column="id" jdbcType="INTEGER" property="id" />
+    <result column="block_id" jdbcType="INTEGER" property="blockId" />
+    <result column="type" jdbcType="CHAR" property="type" />
+    <result column="image" jdbcType="VARCHAR" property="image" />
+    <result column="book_id" jdbcType="BIGINT" property="bookId" />
+    <result column="url" jdbcType="VARCHAR" property="url" />
+    <result column="weigh" jdbcType="INTEGER" property="weigh" />
+    <result column="createtime" jdbcType="INTEGER" property="createtime" />
+    <result column="updatetime" jdbcType="INTEGER" property="updatetime" />
+    <result column="special_id" jdbcType="INTEGER" property="specialId" />
+    <result column="bookName" jdbcType="VARCHAR" property="bookName" />
+    <result column="bookDescription" jdbcType="VARCHAR" property="bookDescription" />
+    <result column="isFinish" jdbcType="VARCHAR" property="isFinish" />
+  </resultMap>
+  <select id="getBooks" resultMap="BaseResultMap">
+    SELECT
+      m.*,
+      b.NAME AS bookName,
+      b.description AS bookDescription,
+      b.is_finish AS isFinish
+    FROM
+      manage_block_resource m
+        LEFT JOIN book b ON m.book_id = b.id
+    WHERE
+      m.block_id IN (
+    <foreach collection="blockIds" item="blockId" index="index" separator="," >
+      #{blockId,jdbcType=INTEGER}
+    </foreach>
+    )
+  </select>
+
+
+</mapper>