first commit

This commit is contained in:
cys841515238
2023-03-02 16:13:28 +08:00
commit 2733a60b97
741 changed files with 76931 additions and 0 deletions

View File

@@ -0,0 +1,305 @@
package com.peanut.modules.book.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peanut.common.utils.BaiduVoicesUtils;
import com.peanut.modules.book.entity.BookChapterEntity;
import com.peanut.modules.book.entity.BookEntity;
import com.peanut.modules.book.entity.MyUserEntity;
import com.peanut.modules.book.service.BookChapterService;
import com.peanut.modules.book.service.BookService;
import com.peanut.modules.book.service.MyUserService;
import com.peanut.modules.oss.service.OssService;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
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.BookChapterContentEntity;
import com.peanut.modules.book.service.BookChapterContentService;
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-16 14:32:06
*/
@RestController
@RequestMapping("book/bookchaptercontent")
public class BookChapterContentController {
@Autowired
private BookChapterContentService bookChapterContentService;
@Autowired
private BookService bookService;
@Autowired
private OssService ossService;
@Autowired
private BookChapterService bookChapterService;
@Autowired
private MyUserService myUserService;
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("book:bookchaptercontent:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = bookChapterContentService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
// @RequiresPermissions("book:bookchaptercontent:info")
public R info(@PathVariable("id") Integer id){
BookChapterContentEntity bookChapterContent = bookChapterContentService.getById(id);
return R.ok().put("bookChapterContent", bookChapterContent);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("book:bookchaptercontent:save")
public R save(@RequestBody BookChapterContentEntity bookChapterContent){
bookChapterContentService.save(bookChapterContent);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("book:bookchaptercontent:update")
public R update(@RequestBody BookChapterContentEntity bookChapterContent){
bookChapterContentService.updateById(bookChapterContent);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("book:bookchaptercontent:delete")
public R delete(@RequestBody Integer[] ids){
bookChapterContentService.removeByIds(Arrays.asList(ids));
return R.ok();
}
/**
* 章节拆分转换单句
*/
@RequestMapping("/getBookVoices")
// @RequiresPermissions("book:bookchaptercontent:delete")
public R getBookVoices(@RequestParam("id") Integer id){
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
// bookChapterContentService.getBookVoices(id);
bookChapterContentService.getWordChapterParagraph(14);
}
});
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
bookEntity.setContentStatus("1");
bookService.updateById(bookEntity);
return R.ok();
}
/**
* 章节单句 转成音频
*/
@RequestMapping("/signVoices")
public R signVoices(@RequestParam("content") String content) throws Exception {
String voices = BaiduVoicesUtils.run(content);
File file = new File(voices);
if (!file.exists()) {
return R.error("语音文件未生成");
}
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
String path = ossService.uploadFileAvatar(multipartFile);
fileInputStream.close();
file.delete();
if (StringUtils.isEmpty(path)){
return R.error("语音上传失败");
}
return R.ok().put("voices",path);
}
/**
* 全部章节单句 转成音频
*/
@RequestMapping("/allVoices")
public R allVoices(@RequestParam("id") Integer id) throws Exception {
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
List<BookChapterContentEntity> book_id = bookChapterContentService.getBaseMapper().selectList(new QueryWrapper<BookChapterContentEntity>()
.eq("book_id", id));
for (BookChapterContentEntity bookContent:book_id) {
String content = bookContent.getContent();
String voices = BaiduVoicesUtils.run(content);
File file = new File(voices);
if (!file.exists()) {
bookEntity.setVoicesStatus("3");
}
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
String path = ossService.uploadFileAvatar(multipartFile);
bookContent.setVoices(path);
fileInputStream.close();
file.delete();
bookChapterContentService.updateById(bookContent);
if (StringUtils.isEmpty(path)){
bookEntity.setVoicesStatus("3");
}
}
bookEntity.setVoicesStatus("2");
bookService.updateById(bookEntity);
}
});
bookEntity.setVoicesStatus("1");
bookService.updateById(bookEntity);
return R.ok();
}
/**
* app 获取电子书章节内容
*/
@RequestMapping("/appGetBookChapterContent")
public R getBookCatalogue(@RequestParam("chapterid") Integer chapterid,
@RequestParam("bookid") Integer bookid,
@RequestParam("userId") Integer userId){
// TODO 验证 当前请求的书 是否存在免费章节数
BookEntity bookEntity = bookService.getBaseMapper().selectById(bookid);
Integer freeChapterCount = bookEntity.getFreeChapterCount();
Integer isVip = bookEntity.getIsVip(); // 0-免费 1-会免 2-付费
BookChapterEntity bookChapterEntity = bookChapterService.getBaseMapper().selectById(chapterid);
Integer number = bookChapterEntity.getNumber();
if ((number > freeChapterCount) || freeChapterCount ==0){ // 阅读章节数 大于 免费章节数
// 书籍为 会免
if (isVip == 1) {
//查询用户身份
MyUserEntity user = myUserService.getById(userId);
String vip = user.getVip();
if (!"1".equals(vip)) {
return R.error(500,"当前书籍为会员书籍,请开通会员或购买!");
}
}
if (isVip == 2) {
// 鉴权 查询权限表中 用户是否开通
boolean b = myUserService.bookAuthenticate(bookid, userId);
if (!b) {
return R.error(500,"请开通会员或购买此书籍!");
}
}
}
ArrayList<Object> list = new ArrayList<>();
List<BookChapterContentEntity> bookChapterContentEntities = bookChapterContentService.getBaseMapper().selectList(new QueryWrapper<BookChapterContentEntity>()
.eq("book_id",bookid)
.eq("book_chatper_id",chapterid));
for (BookChapterContentEntity bookChapterContentEntity : bookChapterContentEntities) {
String content = bookChapterContentEntity.getContent();
String substring = "";
if (bookChapterContentEntity.getOtherContent() != null){
// substring = bookChapterContentEntity.getOtherContent().substring(3, bookChapterContentEntity.getOtherContent().length() - 3);
substring = bookChapterContentEntity.getOtherContent().replace("<p>","").replace("</p>","");
}
StringBuffer stringBuffer = new StringBuffer(substring);
if(stringBuffer.indexOf("title") != -1){
stringBuffer.insert(stringBuffer.indexOf("title"),"width=\"100%\"");
}
if (bookChapterContentEntity.getNumber() == 1){
Integer bookChatperId = bookChapterContentEntity.getBookChatperId();
BookChapterEntity chapterEntity = bookChapterService.getById(bookChatperId);
String chapter = chapterEntity.getChapter();
content = bookChapterContentEntity.getContent().replace(chapter, "");
}
// System.out.println(content.length());
// System.out.println(content.replaceAll("[\\\\s\\\\u00A0]",""));
bookChapterContentEntity.setPicAndWord(content.replaceAll(" ","") + stringBuffer);
list.add(bookChapterContentEntity);
}
return R.ok().put("bookCatalogue",list);
}
//生成电子书 目录
@RequestMapping("/getWordChapterParagraph")
public R getWordChapterParagraph(@RequestParam Map<String, Object> params){
bookChapterContentService.getWordChapterParagraph(14);
return R.ok();
}
}