海外读书

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,147 @@
package com.peanut.modules.master.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.modules.common.entity.*;
import com.peanut.modules.master.service.*;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestController("masterBookAbroad")
@RequestMapping("master/bookAbroad")
public class BookAbroadController {
@Autowired
private BookAbroadCommentService bookAbroadCommentService;
@Autowired
private com.peanut.modules.bookAbroad.service.BookAbroadLableService labelService;
@Autowired
private com.peanut.modules.bookAbroad.service.BookAbroadToLableService toLableService;
//评论列表
@RequestMapping("/getBookAbroadCommentList")
public R getBookAbroadCommentList(@RequestBody Map<String,Object> params) {
Page comments = bookAbroadCommentService.page(
new Page<>(Long.parseLong(params.get("current").toString()), Long.parseLong(params.get("limit").toString())),
new LambdaQueryWrapper<BookAbroadComment>().like(BookAbroadComment::getContent,params.get("content")));
return R.ok().put("comments",comments);
}
//删除评论
@RequestMapping("/delBookAbroadComment")
public R delBookAbroadComment(@RequestBody Map<String,Object> params){
bookAbroadCommentService.removeById(params.get("commentId").toString());
return R.ok();
}
//新增标签
@RequestMapping("/insertBookAbroadLable")
public R insertBookAbroadLable(@RequestBody BookAbroadLable lable){
labelService.save(lable);
return R.ok();
}
//标签详情
@RequestMapping("/bookAbroadLableInfo")
public R bookAbroadLableInfo(@RequestBody Map<String,Object> params){
BookAbroadLable lable = labelService.getById(params.get("id").toString());
return R.ok().put("lable",lable);
}
//修改标签
@RequestMapping("/updateBookAbroadLable")
public R updateBookAbroadLable(@RequestBody BookAbroadLable lable){
BookAbroadLable oldLable = labelService.getById(lable.getId());
if (oldLable.getIsLast()!=lable.getIsLast()) {
int count = toLableService.count(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getLableId,lable.getId()));
if (count > 0){
return R.error("存在绑定书籍,删除后再操作");
}
}
labelService.updateById(lable);
return R.ok();
}
//删除标签
@RequestMapping("/delBookAbroadLable")
public R delBookAbroadLable(@RequestBody Map<String,Object> params){
BookAbroadLable oldLable = labelService.getById(params.get("id").toString());
int count = toLableService.count(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getLableId,params.get("id").toString()));
if (count > 0){
return R.error("存在绑定书籍,删除后再操作");
}
labelService.removeById(oldLable.getId());
return R.ok();
}
//绑定图书和标签
@RequestMapping("/insertBookAbroadToLable")
public R insertBookAbroadToLable(@RequestBody BookAbroadToLable toLable){
int count = toLableService.count(new LambdaQueryWrapper<BookAbroadToLable>()
.eq(BookAbroadToLable::getBookId,toLable.getBookId())
.eq(BookAbroadToLable::getLableId,toLable.getLableId()));
if (count > 0) {
return R.error("已存在");
}
toLableService.save(toLable);
return R.ok();
}
//解绑
@RequestMapping("/delBookAbroadToLable")
public R delBookAbroadToLable(@RequestBody Map<String,Object> params){
String[] strings = params.get("ids").toString().split(",");
toLableService.removeByIds(Arrays.asList(strings));
return R.ok();
}
//通过标签获取绑定图书
@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,"id");
wrapper.select(BookEntity::getName);
wrapper.eq(BookAbroadToLable::getLableId,params.get("lableId"));
return R.ok().put("bookList",toLableService.listMaps(wrapper));
}
//标签树
@RequestMapping("/getLableTree")
public List<BookAbroadLable> getLableTree(@RequestBody Map<String,Object> params) {
List<BookAbroadLable> labels = labelService.list(new LambdaQueryWrapper<BookAbroadLable>()
.eq(BookAbroadLable::getType,params.get("type")));
List<BookAbroadLable> labelsTree = labels.stream().filter((bookAbroadLable) ->
bookAbroadLable.getPid() == 0
).map((label)->{
label.setChildren(getLabelChildrens(label,labels));
return label;
}).sorted((label1,label2)->{
return (label1.getSort() == null? 0 : label1.getSort()) - (label2.getSort()==null?0:label2.getSort());
}).collect(Collectors.toList());
return labelsTree;
}
private List<BookAbroadLable> getLabelChildrens(BookAbroadLable root,List<BookAbroadLable> all){
List<BookAbroadLable> children = all.stream().filter(bookAbroadLable -> {
return root.getId().equals(bookAbroadLable.getPid());
}).map(bookAbroadLable -> {
bookAbroadLable.setChildren(getLabelChildrens(bookAbroadLable, all));
return bookAbroadLable;
}).sorted((label1,label2)->{
return (label1.getSort()==null?0:label1.getSort()) - (label2.getSort()==null?0:label2.getSort());
}).collect(Collectors.toList());
return children;
}
}