311 lines
10 KiB
Java
311 lines
10 KiB
Java
package com.peanut.modules.book.controller;
|
||
|
||
import java.io.IOException;
|
||
import java.text.Collator;
|
||
import java.util.*;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.PathVariable;
|
||
import org.springframework.web.bind.annotation.RequestBody;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RequestParam;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import com.peanut.modules.book.entity.BookChapterEntity;
|
||
import com.peanut.modules.book.service.BookChapterService;
|
||
import com.peanut.common.utils.PageUtils;
|
||
import com.peanut.common.utils.R;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
|
||
/**
|
||
*
|
||
*
|
||
* @author yl
|
||
* @email yl328572838@163.com
|
||
* @date 2022-08-12 09:53:25
|
||
*/
|
||
@RestController
|
||
@RequestMapping("book/bookchapter")
|
||
public class BookChapterController {
|
||
@Autowired
|
||
private BookChapterService bookChapterService;
|
||
|
||
/**
|
||
* 列表
|
||
*/
|
||
@RequestMapping("/list")
|
||
public R list(@RequestParam Map<String, Object> params){
|
||
PageUtils page = bookChapterService.queryPage(params);
|
||
|
||
return R.ok().put("page", page);
|
||
}
|
||
|
||
|
||
|
||
@RequestMapping("/booklist")
|
||
public R booklist(@RequestParam Map<String, Object> params){
|
||
PageUtils page = bookChapterService.queryPage1(params);
|
||
|
||
return R.ok().put("page", page);
|
||
}
|
||
|
||
|
||
/**
|
||
* 信息
|
||
*/
|
||
@RequestMapping("/info/{id}")
|
||
public R info(@PathVariable("id") Integer id){
|
||
BookChapterEntity bookChapter = bookChapterService.getById(id);
|
||
|
||
return R.ok().put("bookChapter", bookChapter);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 保存
|
||
*/
|
||
@RequestMapping("/save")
|
||
public R save(@RequestBody BookChapterEntity bookChapter) {
|
||
BookChapterEntity bookChapterEntity = new BookChapterEntity();
|
||
Integer number = bookChapter.getNumber();
|
||
Integer bookId = bookChapter.getBookId();
|
||
if (number == 0) {
|
||
return R.error("章节号不可为0,请输入正确章节号");
|
||
}
|
||
|
||
List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", bookId)
|
||
.ge("number", number));
|
||
for(BookChapterEntity entity: bookChapterEntities){
|
||
entity.setNumber(entity.getNumber() + 1);
|
||
this.bookChapterService.updateById(entity);
|
||
}
|
||
bookChapterService.save(bookChapter);
|
||
return R.ok();
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 修改
|
||
*/
|
||
@RequestMapping("/contentupdate")
|
||
public R contentupdate(@RequestBody BookChapterEntity bookChapter) {
|
||
Integer id = bookChapter.getId();
|
||
String content = bookChapter.getContent();
|
||
Integer number = bookChapter.getNumber();
|
||
Integer bookId = bookChapter.getBookId();
|
||
|
||
BookChapterEntity byId = bookChapterService.getById(id);
|
||
String content1 = byId.getContent();
|
||
if (content.equals(content1)) {
|
||
|
||
} else {
|
||
// 如果content字段和原来数据库的content内容不同则voices字段为空
|
||
bookChapter.setVoices(""); // 设置voices字段为空
|
||
|
||
}
|
||
|
||
List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", bookId)
|
||
.ge("number", number));
|
||
for(BookChapterEntity entity: bookChapterEntities){
|
||
entity.setNumber(entity.getNumber() + 1);
|
||
this.bookChapterService.updateById(entity);
|
||
}
|
||
|
||
|
||
|
||
bookChapterService.updateById(bookChapter); // 更新对象的属性
|
||
return R.ok();
|
||
|
||
}
|
||
|
||
/**
|
||
* 修改
|
||
*/
|
||
@RequestMapping("/update")
|
||
public R update(@RequestBody BookChapterEntity bookChapter) {
|
||
Integer id = bookChapter.getId();
|
||
String content = bookChapter.getContent();
|
||
Integer number = bookChapter.getNumber();
|
||
Integer bookId = bookChapter.getBookId();
|
||
|
||
BookChapterEntity byId = bookChapterService.getById(id);
|
||
String content1 = byId.getContent();
|
||
if (content.equals(content1)) {
|
||
|
||
} else {
|
||
// 如果content字段和原来数据库的content内容不同则voices字段为空
|
||
bookChapter.setVoices(""); // 设置voices字段为空
|
||
|
||
}
|
||
|
||
List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", bookId)
|
||
.ge("number", number));
|
||
System.out.println("bookChapterEntitiesbookChapterEntitiesbookChapterEntities"+bookChapterEntities);
|
||
|
||
BookChapterEntity bookChapterEntity = null;
|
||
for (BookChapterEntity entity : bookChapterEntities) {
|
||
if (entity.getNumber() == number) {
|
||
bookChapterEntity = entity;
|
||
break;
|
||
}
|
||
}
|
||
// if (bookChapterEntity != null) {
|
||
// bookChapterEntity.setNumber(1); // 设置新的章节编号为1
|
||
// bookChapterService.updateById(bookChapterEntity); // 更新章节编号为1的章节
|
||
// }
|
||
|
||
List<BookChapterEntity> bookChapterEntitiesCopy = new ArrayList<>(bookChapterEntities); // 复制章节列表
|
||
Collections.sort(bookChapterEntitiesCopy, new Comparator<BookChapterEntity>() {
|
||
@Override
|
||
public int compare(BookChapterEntity o1, BookChapterEntity o2) {
|
||
return o1.getNumber() - o2.getNumber(); // 根据章节编号进行排序
|
||
}
|
||
});
|
||
|
||
|
||
// bookChapterService.updateById(bookChapter);
|
||
// List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
// .eq("book_id", bookId)
|
||
// .ge("number", number));
|
||
//
|
||
// for(BookChapterEntity entity: bookChapterEntities){
|
||
// if (bookChapter.getNumber()>=) {
|
||
// }
|
||
// entity.setNumber(entity.getNumber() - 1);
|
||
//
|
||
// bookChapterService.updateById(entity);
|
||
// }
|
||
// List<BookChapterEntity> tempEntity = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
// .eq("book_id", bookChapter.getBookId()).orderByAsc("number")
|
||
// );
|
||
// int index = 0;
|
||
// for(BookChapterEntity entity: tempEntity){
|
||
// index ++;
|
||
// entity.setNumber(index);
|
||
// this.bookChapterService.updateById(entity);
|
||
// }
|
||
//
|
||
|
||
return R.ok();
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
public static List<BookChapterEntity> sort(List<BookChapterEntity> list){
|
||
//根据指定比较器产生的顺序对指定列表进行排序。
|
||
Collections.sort(list, new Comparator<BookChapterEntity>() {
|
||
@Override
|
||
public int compare(BookChapterEntity o1, BookChapterEntity o2) {
|
||
//获取所需语言环境的 Collator,根据所需切换其他语言环境
|
||
//Collator collator = Collator.getInstance(Locale.ENGLISH);
|
||
//return collator.compare(o1.getAddress(), o2.getAddress());
|
||
Collator collator = Collator.getInstance(Locale.CANADA);
|
||
return collator.compare(o1.getNumber(), o2.getNumber());
|
||
}
|
||
});
|
||
return list;
|
||
}
|
||
|
||
/**
|
||
* 优化删除,暂时报错未修改
|
||
*/
|
||
//TODO 优化删除,暂时报错未修改
|
||
@RequestMapping("/deletess")
|
||
public R deletess(@RequestBody Integer[] ids){
|
||
|
||
for(Integer id : ids){
|
||
|
||
BookChapterEntity chapter = bookChapterService.getBaseMapper().selectOne(new QueryWrapper<BookChapterEntity>()
|
||
.eq("id", id)
|
||
);
|
||
|
||
List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", chapter.getBookId())
|
||
.gt("number", chapter.getNumber()));
|
||
this.bookChapterService.removeById(id);
|
||
for(BookChapterEntity entity: bookChapterEntities){
|
||
entity.setNumber(entity.getNumber() - 1);
|
||
|
||
}
|
||
|
||
List<BookChapterEntity> bookChapterEntities2 = sort(bookChapterEntities);
|
||
List<BookChapterEntity> tempEntity = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", chapter.getBookId()).orderByAsc("number")
|
||
);
|
||
int index = tempEntity.size() - bookChapterEntities.size();
|
||
for(BookChapterEntity entity: bookChapterEntities2){
|
||
index ++;
|
||
entity.setNumber(index);
|
||
this.bookChapterService.updateById(entity);
|
||
}
|
||
}
|
||
|
||
return R.ok();
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
@RequestMapping("/delete")
|
||
public R delete(@RequestBody Integer[] ids) {
|
||
for(Integer id : ids){
|
||
|
||
|
||
|
||
BookChapterEntity chapter = bookChapterService.getBaseMapper().selectOne(new QueryWrapper<BookChapterEntity>()
|
||
.eq("id", id)
|
||
);
|
||
List<BookChapterEntity> bookChapterEntities = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", chapter.getBookId())
|
||
.gt("number", chapter.getNumber()));
|
||
this.bookChapterService.removeById(id);
|
||
for(BookChapterEntity entity: bookChapterEntities){
|
||
entity.setNumber(entity.getNumber() - 1);
|
||
this.bookChapterService.updateById(entity);
|
||
}
|
||
List<BookChapterEntity> tempEntity = this.bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>()
|
||
.eq("book_id", chapter.getBookId()).orderByAsc("number")
|
||
);
|
||
int index = 0;
|
||
for(BookChapterEntity entity: tempEntity){
|
||
index ++;
|
||
entity.setNumber(index);
|
||
this.bookChapterService.updateById(entity);
|
||
}
|
||
}
|
||
|
||
|
||
return R.ok();
|
||
}
|
||
|
||
}
|
||
|
||
|