海外读书

This commit is contained in:
wuchunlei
2024-11-26 15:13:38 +08:00
parent 3fc7a80580
commit a0c42ecea0
37 changed files with 1234 additions and 9 deletions

View File

@@ -0,0 +1,104 @@
package com.peanut.modules.bookAbroad.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.bookAbroad.service.BookAbroadCommentLikeService;
import com.peanut.modules.bookAbroad.service.BookAbroadCommentService;
import com.peanut.modules.bookAbroad.service.BookAbroadLableService;
import com.peanut.modules.bookAbroad.service.BookAbroadToLableService;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.MyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestController("bookAbroad")
@RequestMapping("bookAbroad")
public class BookAbroadController {
@Autowired
private MyUserService userService;
@Autowired
private BookAbroadCommentService bookAbroadCommentService;
@Autowired
private BookAbroadCommentLikeService bookAbroadCommentLikeService;
//评论
@RequestMapping("/insertBookAbroadComment")
public R insertBookAbroadComment(@RequestBody BookAbroadComment comment){
comment.setUserId(ShiroUtils.getUId());
bookAbroadCommentService.save(comment);
return R.ok();
}
//评论树
@RequestMapping("/getBookAbroadCommentTree")
public List<BookAbroadComment> getBookAbroadCommentTree(@RequestBody Map<String,Object> params) {
List<BookAbroadComment> comments = bookAbroadCommentService.list(new LambdaQueryWrapper<BookAbroadComment>()
.eq(BookAbroadComment::getBookId,params.get("bookId")));
List<BookAbroadComment> commentsTree = comments.stream().filter((bookAbroadComment) ->
bookAbroadComment.getPid() == 0
).map((comment)->{
comment.setIsLike(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,comment.getId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId())));
comment.setUserEntity(userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.select(MyUserEntity::getId,MyUserEntity::getName,MyUserEntity::getNickname,MyUserEntity::getAvatar)
.eq(MyUserEntity::getId,comment.getUserId())));
comment.setChildren(getLabelChildrens(comment,comments));
return comment;
}).collect(Collectors.toList());
return commentsTree;
}
private List<BookAbroadComment> getLabelChildrens(BookAbroadComment root,List<BookAbroadComment> all){
List<BookAbroadComment> children = all.stream().filter(bookAbroadComment -> {
return root.getId().equals(bookAbroadComment.getPid());
}).map(bookAbroadComment -> {
bookAbroadComment.setIsLike(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,bookAbroadComment.getId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId())));
bookAbroadComment.setUserEntity(userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.select(MyUserEntity::getId,MyUserEntity::getName,MyUserEntity::getNickname,MyUserEntity::getAvatar)
.eq(MyUserEntity::getId,bookAbroadComment.getUserId())));
bookAbroadComment.setChildren(getLabelChildrens(bookAbroadComment, all));
return bookAbroadComment;
}).collect(Collectors.toList());
return children;
}
//删除评论
@RequestMapping("/delBookAbroadComment")
public R delBookAbroadComment(@RequestBody Map<String,Object> params){
bookAbroadCommentService.removeById(params.get("commentId").toString());
return R.ok();
}
//点赞
@RequestMapping("/insertBookAbroadCommentLike")
public R insertBookAbroadCommentLike(@RequestBody BookAbroadCommentLike commentLike){
int count = bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,commentLike.getCommentId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId()));
if (count > 0) {
return R.error("已点赞");
}
commentLike.setUserId(ShiroUtils.getUId());
bookAbroadCommentLikeService.save(commentLike);
return R.ok();
}
//取消点赞
@RequestMapping("/delBookAbroadCommentLike")
public R delBookAbroadCommentLike(@RequestBody Map<String,Object> params){
bookAbroadCommentLikeService.removeById(params.get("commentLikeId").toString());
return R.ok();
}
}

View File

@@ -0,0 +1,230 @@
package com.peanut.modules.bookAbroad.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.book.service.*;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.master.service.BookAbroadToLableService;
import com.peanut.modules.bookAbroad.service.BookAbroadLableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@RestController("bookAbroadHome")
@RequestMapping("bookAbroad/home")
public class HomeController {
@Autowired
private BookReadRateService bookReadRateService;
@Autowired
private UserEbookBuyService userEbookBuyService;
@Autowired
private BookService bookService;
@Autowired
private BookAbroadLableService lableService;
@Autowired
private BookAbroadToLableService toLableService;
@Autowired
private AuthorService authorService;
@Autowired
private BookChapterService bookChapterService;
@Autowired
private BookChapterContentService bookChapterContentService;
//我的图书
@RequestMapping("/getMyBooks")
public R getMyBooks(@RequestBody Map<String,Object> params){
MPJLambdaWrapper<UserEbookBuyEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.leftJoin(BookReadRateEntity.class,BookReadRateEntity::getBookId,UserEbookBuyEntity::getBookId);
wrapper.leftJoin(BookEntity.class,BookEntity::getId,UserEbookBuyEntity::getBookId);
wrapper.eq(UserEbookBuyEntity::getUserId,ShiroUtils.getUId());
wrapper.select(BookEntity::getId,BookEntity::getName,BookEntity::getImages);
wrapper.select(BookReadRateEntity::getPrecent);
wrapper.orderByDesc(BookReadRateEntity::getUpdateTime);
wrapper.groupBy(UserEbookBuyEntity::getBookId);
Page p = userEbookBuyService.pageMaps(new Page<>(
Long.parseLong(params.get("current").toString()), Long.parseLong(params.get("limit").toString())),wrapper);
return R.ok().put("page",p);
}
//推荐图书
@RequestMapping("/getRecommendBooks")
public R getRecommendBooks(){
//查询已购买图书
LambdaQueryWrapper<UserEbookBuyEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserEbookBuyEntity::getUserId,ShiroUtils.getUId());
wrapper.groupBy(UserEbookBuyEntity::getBookId);
Set<Integer> bookIds = userEbookBuyService.getBaseMapper().selectList(wrapper).stream()
.map(UserEbookBuyEntity::getBookId).collect(Collectors.toSet());
//查询未购买图书
List<BookEntity> noBooks = bookService.list(new LambdaQueryWrapper<BookEntity>()
.eq(BookEntity::getState,1)
.eq(BookEntity::getBookType,0)
.notIn(BookEntity::getId,bookIds)
.last("limit 2"));
Set<BookEntity> books = new HashSet<>();
//查询阅读进度
List<BookReadRateEntity> readRateList = bookReadRateService.list(new LambdaQueryWrapper<BookReadRateEntity>()
.eq(BookReadRateEntity::getUserId,ShiroUtils.getUId())
.orderByDesc(BookReadRateEntity::getUpdateTime));
//根据阅读记录,查询同标签下书籍
if (readRateList.size()>0){
//书籍绑定的标签
List<BookAbroadToLable> tolableBookList = toLableService.list(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getBookId,readRateList.get(0).getBookId()));
for (BookAbroadToLable tolableBook : tolableBookList) {
//标签下的书籍
List<BookAbroadToLable> tolableLableList = toLableService.list(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getLableId,tolableBook.getLableId()));
for (BookAbroadToLable tolableLable : tolableLableList) {
if (bookIds.add(tolableLable.getBookId())) {
books.add(bookService.getById(tolableLable.getBookId()));
if (books.size()==2){
break;
}
}
}
}
}
//补充标签未填满位置
if (books.size()!=2){
int surplus = 2-books.size();
for (int i=0;i<surplus; i++){
books.add(noBooks.get(i));
}
}
return R.ok().put("books",books);
}
//获取标签列表
@RequestMapping("/getBookAbroadLableList")
public R getBookAbroadLableList(@RequestBody Map<String,Object> params) {
List<BookAbroadLable> list = lableService.list(new LambdaQueryWrapper<BookAbroadLable>()
.eq(BookAbroadLable::getPid,0)
.eq(BookAbroadLable::getType,params.get("type")));
return R.ok().put("lableList",list);
}
//获取子标签
@RequestMapping("/getBookAbroadLableListByPid")
public R getBookAbroadLableListByPid(@RequestBody Map<String,Object> params) {
List<BookAbroadLable> list = lableService.list(new LambdaQueryWrapper<BookAbroadLable>()
.eq(BookAbroadLable::getPid,params.get("pid")));
return R.ok().put("lableList",list);
}
//通过标签获取绑定图书
@RequestMapping("/getAbroadBookListByLable")
public R getAbroadBookListByLable(@RequestBody Map<String,Object> params) {
MPJLambdaWrapper<BookAbroadToLable> wrapper = new MPJLambdaWrapper();
wrapper.leftJoin(BookEntity.class,BookEntity::getId,BookAbroadToLable::getBookId);
wrapper.selectAs(BookAbroadToLable::getId,"toLableId");
wrapper.selectAs(BookEntity::getId,"bookId");
wrapper.selectAs(BookEntity::getAbroadPrice,"abroadPrice");
wrapper.select(BookEntity::getImages);
wrapper.select(BookEntity::getName);
wrapper.eq(BookAbroadToLable::getLableId,params.get("lableId"));
return R.ok().put("bookList",toLableService.listMaps(wrapper));
}
//图书详情
@RequestMapping("/getBookInfo")
public R getBookInfo(@RequestBody Map<String,Object> params) {
BookEntity bookEntity = bookService.getById(params.get("bookId").toString());
bookEntity.setAuthor(authorService.getById(bookEntity.getAuthorId()));
bookEntity.setIsBuy(false);
int count = userEbookBuyService.count(new LambdaQueryWrapper<UserEbookBuyEntity>()
.eq(UserEbookBuyEntity::getUserId,ShiroUtils.getUId())
.eq(UserEbookBuyEntity::getBookId,params.get("bookId")));
if (count > 0) {
bookEntity.setIsBuy(true);
}
return R.ok().put("bookInfo",bookEntity);
}
//图书章节
@RequestMapping("/getBookChapter")
public R getBookChapter(@RequestBody Map<String,Object> params) {
List<BookChapterEntity> chapterList = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
.eq(BookChapterEntity::getBookId,params.get("bookId"))
.orderByAsc(BookChapterEntity::getId));
return R.ok().put("chapterList",chapterList);
}
//章节内容
@RequestMapping("/getBookChapterContent")
public R getBookChapterContent(@RequestBody Map<String,Object> params) {
Page<BookChapterContentEntity> contentPage = bookChapterContentService.page(new Page<>(
Long.parseLong(params.get("current").toString()), Long.parseLong(params.get("limit").toString())),
new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookChatperId,params.get("chapterId"))
.orderByAsc(BookChapterContentEntity::getNumber));
return R.ok().put("contentPage",contentPage);
}
//获取当前书的阅读记录
@RequestMapping("/getBookReadRate")
public R getBookReadRate(@RequestBody Map<String,Object> params){
BookReadRateEntity brr = bookReadRateService.getOne(new LambdaQueryWrapper<BookReadRateEntity>()
.eq(BookReadRateEntity::getUserId,ShiroUtils.getUId())
.eq(BookReadRateEntity::getBookId,params.get("bookId")));
return R.ok().put("bookReadRate",brr);
}
//新增阅读记录
@RequestMapping("/insertBookReadRate")
public R insertBookReadRate(@RequestBody BookReadRateEntity bookReadRate){
//计算百分比
int count = bookChapterContentService.count(new LambdaQueryWrapper<BookChapterContentEntity>()
.eq(BookChapterContentEntity::getBookId,bookReadRate.getBookId()));
BookChapterContentEntity bcc = bookChapterContentService.getById(bookReadRate.getContentId());
int pre = bcc.getNumber()*100/count;
bookReadRate.setPrecent(pre);
BookReadRateEntity brr = bookReadRateService.getOne(new LambdaQueryWrapper<BookReadRateEntity>()
.eq(BookReadRateEntity::getUserId,ShiroUtils.getUId())
.eq(BookReadRateEntity::getBookId,bookReadRate.getBookId()));
if (brr==null){
bookReadRate.setUserId(ShiroUtils.getUId());
bookReadRateService.save(bookReadRate);
}else {
brr.setChapterId(bookReadRate.getChapterId());
brr.setChapterName(bookReadRate.getChapterName());
brr.setContentId(bookReadRate.getContentId());
brr.setPrecent(bookReadRate.getPrecent());
brr.setUpdateTime(new Date());
bookReadRateService.updateById(brr);
}
return R.ok();
}
//相关图书
@RequestMapping("/getRecommendBook")
public R getRecommendBook(@RequestBody Map<String,Object> params){
Set<BookEntity> bookList = new HashSet<>();
//书籍绑定的标签
List<BookAbroadToLable> tolableBookList = toLableService.list(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getBookId,params.get("bookId")));
for (BookAbroadToLable tolableBook : tolableBookList) {
//标签下的书籍
List<BookAbroadToLable> tolableLableList = toLableService.list(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getLableId,tolableBook.getLableId()));
for (BookAbroadToLable tolableLable : tolableLableList) {
bookList.add(bookService.getById(tolableLable.getBookId()));
}
}
return R.ok().put("bookList",bookList);
}
}

View File

@@ -0,0 +1,53 @@
package com.peanut.modules.bookAbroad.controller;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.peanut.common.utils.R;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.common.entity.BuyOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController("bookAbroadOrder")
@RequestMapping("bookAbroad/order")
public class OrderController {
@Autowired
private BuyOrderService buyOrderService;
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 充值专用订单生成接口
*/
@RequestMapping("/rechargeSave")
public R rechargeSave(@RequestBody BuyOrder buyOrder){
buyOrder.setOrderStatus("0");
buyOrder.setOrderType("abroadBook");
String timeId = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(timeId);
buyOrderService.save(buyOrder);
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
return R.ok().put("orderSn", timeId);
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
return message;
};
}
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.bookAbroad.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.common.entity.BookAbroadCommentLike;
public interface BookAbroadCommentLikeService extends IService<BookAbroadCommentLike> {
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.bookAbroad.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.common.entity.BookAbroadComment;
public interface BookAbroadCommentService extends IService<BookAbroadComment> {
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.bookAbroad.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.common.entity.BookAbroadLable;
public interface BookAbroadLableService extends IService<BookAbroadLable> {
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.bookAbroad.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.common.entity.BookAbroadToLable;
public interface BookAbroadToLableService extends IService<BookAbroadToLable> {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.bookAbroad.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.bookAbroad.service.BookAbroadCommentLikeService;
import com.peanut.modules.common.dao.BookAbroadCommentLikeDao;
import com.peanut.modules.common.entity.BookAbroadCommentLike;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookAbroadCommentLikeService")
public class BookAbroadCommentLikeServiceImpl extends ServiceImpl<BookAbroadCommentLikeDao, BookAbroadCommentLike> implements BookAbroadCommentLikeService {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.bookAbroad.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.common.dao.BookAbroadCommentDao;
import com.peanut.modules.common.entity.BookAbroadComment;
import com.peanut.modules.bookAbroad.service.BookAbroadCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookAbroadComment")
public class BookAbroadCommentServiceImpl extends ServiceImpl<BookAbroadCommentDao, BookAbroadComment> implements BookAbroadCommentService {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.bookAbroad.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.common.dao.BookAbroadLableDao;
import com.peanut.modules.common.entity.BookAbroadLable;
import com.peanut.modules.bookAbroad.service.BookAbroadLableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookAbroadLableService")
public class BookAbroadLableServiceImpl extends ServiceImpl<BookAbroadLableDao, BookAbroadLable> implements BookAbroadLableService {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.bookAbroad.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.bookAbroad.service.BookAbroadToLableService;
import com.peanut.modules.common.dao.BookAbroadToLableDao;
import com.peanut.modules.common.entity.BookAbroadToLable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookAbroadToLableService")
public class BookAbroadToLableServiceImpl extends ServiceImpl<BookAbroadToLableDao, BookAbroadToLable> implements BookAbroadToLableService {
}