package com.peanut.modules.book.controller; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.peanut.common.utils.ReadProvinceUtil; import com.peanut.modules.book.dao.BookDao; import com.peanut.modules.book.entity.*; import com.peanut.modules.book.service.*; import com.peanut.modules.book.vo.BookIndexVo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; import com.peanut.common.utils.PageUtils; import com.peanut.common.utils.R; /** * 图书表 * * @author yl * @email yl328572838@163.com * @date 2022-08-04 15:36:59 */ @Slf4j @RestController @RequestMapping("book/book") public class BookController { @Autowired private BookService bookService; @Autowired private BookChapterService bookChapterService; @Autowired private StringRedisTemplate redisTemplate; @Autowired private AuthorService authorService; @Autowired private PublisherService publisherService; @Autowired private BookShelfService bookShelfService; @Autowired private BookReadRateService bookReadRateService; @Autowired private MyUserService myUserService; @Autowired private ProvinceService provinceService; @Autowired private CityService cityService; @Autowired private CountyService countyService; @Autowired private UserEbookBuyService userEbookBuyService; @Autowired private BookDao bookDao; @Autowired private ShopProductService shopProductService; @Autowired private ShopProductBookService shopProductBookService; @Autowired private ShopProductToLabelService shopProductToLabelService; final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); /** * 列表 */ @RequestMapping("/list") public R list(@RequestParam Map params) { PageUtils page = bookService.queryPage(params); return R.ok().put("page", page); } /** * 列表 */ @RequestMapping("/listbooks") public R listbooks(@RequestParam Map params) { PageUtils page = bookService.queryPagebooks(params); return R.ok().put("page", page); } /** * 获取精品图书 * @return */ @RequestMapping("/getJPBooks") public R getJPBooks(){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(ShopProductToLabelEntity::getSplId,5);//精选图书 wrapper.eq(ShopProductToLabelEntity::getDelFlag,0); List pIds = shopProductToLabelService.getBaseMapper().selectList(wrapper).stream().map(ShopProductToLabelEntity::getProductId).collect(Collectors.toList()); List shopProductEntities = shopProductService.getBaseMapper().selectList(new LambdaQueryWrapper().in(ShopProduct::getProductId, pIds)); return R.ok().put("Products",shopProductEntities); } /** * 信息 */ @RequestMapping("/info/{id}") // @RequiresPermissions("book:book:info") public R info(@PathVariable("id") Integer id) { BookEntity book = bookService.getById(id); return R.ok().put("book", book); } /** * 获取书详情 * @param bookId * @return */ @RequestMapping("/getBookInfo") public R getBookInfo(@RequestParam Integer bookId,@RequestParam Integer userId){ BookEntity book_info = bookService.getById(bookId); book_info.setAuthor(authorService.getById(book_info.getAuthorId())); book_info.setPublisher(publisherService.getById(book_info.getPublisherId())); book_info.setProductId(shopProductBookService.getProductByBookId(bookId)); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(UserEbookBuyEntity::getUserId,userId); wrapper.eq(UserEbookBuyEntity::getBookId,bookId); List userEbookBuyEntities = userEbookBuyService.getBaseMapper().selectList(wrapper); book_info.setIsBuy(userEbookBuyEntities.size()==0?false:true); return R.ok().put("book",book_info); } /** * 信息 */ @RequestMapping("/appinfo/{id}/{userId}") // @RequiresPermissions("book:book:info") public R appinfo(@PathVariable("id") Integer id, @PathVariable("userId") Integer userId) { BookEntity book = bookService.getById(id); book.setIsBuy(true); boolean b = myUserService.bookAuthen(id, userId); if (!b) { // 无权限 book.setIsBuy(false); } //书籍详情返回,购买状态,免费章节数 String authorName = ""; String publisherName = ""; String authorId = book.getAuthorId(); String[] authorIds = authorId.split(","); List authorList = Arrays.asList(authorIds); List authorEntities = authorService.getBaseMapper().selectList(new QueryWrapper().in("id", authorList)); for (AuthorEntity authorEntity : authorEntities) { authorName += "," + authorEntity.getAuthorName(); } authorName = authorName.startsWith(",") ? authorName.substring(1) : authorName; String publisherId = book.getPublisherId(); String[] publisherIds = publisherId.split(","); List publisherList = Arrays.asList(publisherIds); List publisherEntities = publisherService.getBaseMapper().selectList(new QueryWrapper().in("id", publisherList)); for (Publisher publisher : publisherEntities) { publisherName += "," + publisher.getPublisherName(); } publisherName = publisherName.startsWith(",") ? publisherName.substring(1) : publisherName; //查询书籍阅读进度 BookReadRateEntity bookReadRateEntity = bookReadRateService.getBaseMapper().selectOne(new QueryWrapper() .eq("book_id", id) .eq("user_id", userId) ); if (bookReadRateEntity != null) { Integer chapterId = bookReadRateEntity.getChapterId(); String chapterName = bookReadRateEntity.getChapterName(); book.setChapterName(chapterName); book.setChapterId(chapterId); // 查询 书籍 章节 number BookChapterEntity bookChapterEntity = bookChapterService.getById(chapterId); if (bookChapterEntity != null) { Integer number = bookChapterEntity.getNumber(); book.setChapterNum(number); } } //查询书籍是否加入书架 Integer integer = bookShelfService.getBaseMapper().selectCount(new QueryWrapper() .eq("book_id", id) .eq("user_id", userId)); boolean flag = false; if (integer > 0) { flag = true; } book.setAuthorName(authorName); book.setPublisherName(publisherName); book.setFlag(flag); return R.ok().put("book", book); } /** * 保存 */ @RequestMapping("/save") public R save(@RequestBody BookEntity book) { bookService.save(book); return R.ok(); } /** * 获取用户未拥有的可听图书 * @return */ @RequestMapping("/getUserNobuyBooks") public R getUserNobuyBooks(@RequestParam Integer userId,@RequestParam Integer limit,@RequestParam Integer page){ List bookids = userEbookBuyService.getBaseMapper().selectList(new QueryWrapper() .select("book_id").eq("user_id", userId)); List bids = new ArrayList(); for (UserEbookBuyEntity b :bookids){ bids.add(b.getBookId()); } // Integer start = (page-1)*limit; QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("can_listen",1); if(bids.size()>0){ wrapper.notIn("id",bids); } Page bookEntityPage = bookService.getBaseMapper().selectPage(new Page(page, limit), wrapper); // Integer start = (page-1)*limit; // QueryWrapper wrapper = new QueryWrapper<>(); // wrapper.eq("t.can_listen",1); // wrapper.notIn("t1.Book_id",bids); // List bookEntities = bookDao.queryUserListenBooksNobuy(wrapper,start,limit); // Integer count = bookDao.queryUserListenBooksNobuyCount(wrapper); return R.ok().put("page",bookEntityPage); } /** * 修改 */ @RequestMapping("/update") public R update(@RequestBody BookEntity book) { bookService.updateById(book); return R.ok(); } /** * 删除 */ @RequestMapping("/delete") public R delete(@RequestBody Integer[] ids) { bookService.removeByIds(Arrays.asList(ids)); return R.ok(); } /** * 常规章节拆分 */ @RequestMapping("/getChapter") public R getChapter(@RequestParam("id") Integer id) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(new Runnable() { @Override public void run() { // bookService.getWordChapter(id); // bookService.getChapter(id); // bookService.getWord(id); bookService.getWordSection(id); } }); BookEntity bookEntity = bookService.getBaseMapper().selectById(id); bookEntity.setChapterStatus("2"); bookService.updateById(bookEntity); return R.ok(); } /** * 章节拆分 2.0 */ @RequestMapping("/getWord") public R getWord(@RequestParam("id") Integer id) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(new Runnable() { @Override public void run() { //2.0 bookService.getWord(id); } }); BookEntity bookEntity = bookService.getBaseMapper().selectById(id); bookEntity.setChapterStatus("2"); bookService.updateById(bookEntity); return R.ok(); } /** * 章节拆分 3.0小节 */ @RequestMapping("/WordSection") public R getWordSection(@RequestParam("id") Integer id) { // List kid = bookChapterService.getBaseMapper().selectList(new QueryWrapper().eq("book_id", id)); // if (kid != null) { // for (BookChapterEntity bookChapterEntity : kid) { // // 调用删除方法进行删除 // Integer id1 = bookChapterEntity.getId(); // System.out.println("===========id1=="+id1); //// bookChapterService.removeById(id1); // } // // // } ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(new Runnable() { @Override public void run() { //3.0 大小章拆分 bookService.getWordSection(id); // bookService.getChapter(id); } }); BookEntity bookEntity = bookService.getBaseMapper().selectById(id); bookEntity.setChapterStatus("2"); bookService.updateById(bookEntity); return R.ok(); } /** * 获取书的章节 * @param bookId * @param userId * @return */ @RequestMapping("/getBookCatalogue") public R getBookCatalogue(@RequestParam Integer bookId,@RequestParam Integer userId){ BookEntity book_info = bookService.getById(bookId); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(BookChapterEntity::getBookId,bookId); wrapper.eq(BookChapterEntity::getDelFlag,0); wrapper.orderByAsc(BookChapterEntity::getNumber); List chapters = bookChapterService.getBaseMapper().selectList(wrapper); for (BookChapterEntity b : chapters){ if(b.getNumber()<=book_info.getFreeChapterCount()){ b.setIsFree(1); }else{ b.setIsFree(0); } b.setBookImage(book_info.getImages()); } return R.ok().put("BookCatalogue",chapters); } /** * app 获取电子书目录 */ @RequestMapping("/getBookCatalogue1") public R getBookCatalogue1(@RequestParam("bookid") Integer id, @RequestParam("userid") Integer userid) { BookEntity bookEntity = bookService.getBaseMapper().selectById(id); if (bookEntity.getImages()==null) { return R.error("电子书目录为空"); } String images = bookEntity.getImages() ; Integer freeChapterCount = bookEntity.getFreeChapterCount(); UserEbookBuyEntity userEbookBuyEntity1 = userEbookBuyService.getBaseMapper().selectOne(new QueryWrapper().eq("book_id", id).eq("user_id", userid)); List bookChapterEntities = bookChapterService.getBaseMapper().selectList(new QueryWrapper().eq("book_id", id)); List> chapterList = new ArrayList<>(); for (BookChapterEntity bookEntitys : bookChapterEntities) { HashMap map = new HashMap<>(); map.put("bookid",id); map.put("number", bookEntitys.getNumber()); map.put("chapterId", bookEntitys.getId()); map.put("chapterName", bookEntitys.getChapter()); //freeChapterCount map.put("images",images); chapterList.add(map); } //true免费 ,false付费 if (userEbookBuyEntity1!=null) { return R.ok().put("BookCatalogue", chapterList).put("buy",true); } return R.ok().put("BookCatalogue", chapterList).put("buy",false).put("freeChapterCount",freeChapterCount); } /** * app 电子书目录 */ @RequestMapping("/getCatalogue") public R getCatalogue(@RequestParam("bookid") Integer id ) { BookEntity bookEntity = bookService.getBaseMapper().selectById(id); if (bookEntity == null) { return R.error("当前图书不存在或已删除"); } String images = bookEntity.getImages() ; Integer number=null; List bookChapterEntities = bookChapterService.getBaseMapper().selectList(new QueryWrapper().eq("book_id", id)); List> chapterList = new ArrayList<>(); for (BookChapterEntity bookEntitys : bookChapterEntities) { number = bookEntitys.getNumber(); HashMap map = new HashMap<>(); map.put("bookid",id); map.put("number", bookEntitys.getNumber()); map.put("chapterId", bookEntitys.getId()); map.put("chapterName", bookEntitys.getChapter()); //freeChapterCount map.put("images",images); chapterList.add(map); } if (number==null) { return R.error("当前电子书目录为空"); }else{ return R.ok().put("BookCatalogue",chapterList); } } //书籍详情返回,购买状态,免费章节数 /** * app 获取免费章节数 */ @RequestMapping("/getfreeChapter") public R getfreeChapter(@RequestParam("bookid") Integer id ) { BookEntity bookEntity = bookService.getBaseMapper().selectById(id); if (bookEntity == null) { return R.error("当前图书不存在或已删除"); } Integer freeChapterCount = bookEntity.getFreeChapterCount(); if (freeChapterCount == null) { return R.error("当前电子书目录为空"); } else { return R.ok().put("freeChapterCount", freeChapterCount); } } /** * 对外图标列表 */ @RequestMapping("/listForWebsite") // @RequiresPermissions("book:book:list") public R listForWebsite(@RequestParam Map params) { PageUtils page = bookService.queryPage(params); return R.ok().put("page", page); } /** * app首页数据 */ @RequestMapping("/bookIndex") // @RequiresPermissions("book:book:list") public R bookIndex() { HashMap map = new HashMap<>(); List list = new ArrayList<>(); List topList = new ArrayList<>(); List saleList = new ArrayList<>(); // 新书 List newBookList = bookService.getBaseMapper().selectList(new QueryWrapper().eq("state", 1).last("limit 6").orderByDesc("create_time")); for (BookEntity book : newBookList) { String authorName = ""; String publisherName = ""; BookIndexVo bookIndexVo = new BookIndexVo(); String authorId = book.getAuthorId(); String[] authorIds = authorId.split(","); List authorList = Arrays.asList(authorIds); List authorEntities = authorService.getBaseMapper().selectList(new QueryWrapper().in("id", authorList)); for (AuthorEntity authorEntity : authorEntities) { authorName += "," + authorEntity.getAuthorName(); } authorName = authorName.startsWith(",") ? authorName.substring(1) : authorName; String publisherId = book.getPublisherId(); String[] publisherIds = publisherId.split(","); List publisherList = Arrays.asList(publisherIds); List publisherEntities = publisherService.getBaseMapper().selectList(new QueryWrapper().in("id", publisherList)); for (Publisher publisher : publisherEntities) { publisherName += "," + publisher.getPublisherName(); } publisherName = publisherName.startsWith(",") ? publisherName.substring(1) : publisherName; bookIndexVo.setBookName(book.getName()); bookIndexVo.setAuthorName(authorName); bookIndexVo.setBookid(book.getId()); bookIndexVo.setImage(book.getImages()); bookIndexVo.setIntroduce(book.getDescription()); bookIndexVo.setPublisherName(publisherName); bookIndexVo.setIsVip(book.getIsVip()); list.add(bookIndexVo); } // 推荐 List topBookList = bookService.getBaseMapper().selectList(new QueryWrapper().eq("state", 1).eq("istop", 1).last("limit 6").orderByDesc("sort")); for (BookEntity book : topBookList) { String authorName = ""; String publisherName = ""; BookIndexVo bookIndexVo = new BookIndexVo(); String authorId = book.getAuthorId(); String[] authorIds = authorId.split(","); List authorList = Arrays.asList(authorIds); List authorEntities = authorService.getBaseMapper().selectList(new QueryWrapper().in("id", authorList)); for (AuthorEntity authorEntity : authorEntities) { authorName += "," + authorEntity.getAuthorName(); } authorName = authorName.startsWith(",") ? authorName.substring(1) : authorName; String publisherId = book.getPublisherId(); String[] publisherIds = publisherId.split(","); List publisherList = Arrays.asList(publisherIds); List publisherEntities = publisherService.getBaseMapper().selectList(new QueryWrapper().in("id", publisherList)); for (Publisher publisher : publisherEntities) { publisherName += "," + publisher.getPublisherName(); } publisherName = publisherName.startsWith(",") ? publisherName.substring(1) : publisherName; bookIndexVo.setBookName(book.getName()); bookIndexVo.setAuthorName(authorName); bookIndexVo.setBookid(book.getId()); bookIndexVo.setImage(book.getImages()); bookIndexVo.setIntroduce(book.getDescription()); bookIndexVo.setPublisherName(publisherName); bookIndexVo.setIsVip(book.getIsVip()); topList.add(bookIndexVo); } //秒杀 List saleBookList = bookService.getBaseMapper().selectList(new QueryWrapper().eq("state", 1).eq("is_sale", 1).last("limit 6").orderByDesc("create_time")); for (BookEntity book : saleBookList) { String authorName = ""; String publisherName = ""; BookIndexVo bookIndexVo = new BookIndexVo(); String authorId = book.getAuthorId(); String[] authorIds = authorId.split(","); List authorList = Arrays.asList(authorIds); List authorEntities = authorService.getBaseMapper().selectList(new QueryWrapper().in("id", authorList)); for (AuthorEntity authorEntity : authorEntities) { authorName += "," + authorEntity.getAuthorName(); } authorName = authorName.startsWith(",") ? authorName.substring(1) : authorName; String publisherId = book.getPublisherId(); String[] publisherIds = publisherId.split(","); List publisherList = Arrays.asList(publisherIds); List publisherEntities = publisherService.getBaseMapper().selectList(new QueryWrapper().in("id", publisherList)); for (Publisher publisher : publisherEntities) { publisherName += "," + publisher.getPublisherName(); } publisherName = publisherName.startsWith(",") ? publisherName.substring(1) : publisherName; bookIndexVo.setBookName(book.getName()); bookIndexVo.setAuthorName(authorName); bookIndexVo.setBookid(book.getId()); bookIndexVo.setImage(book.getImages()); bookIndexVo.setIntroduce(book.getDescription()); bookIndexVo.setPublisherName(publisherName); bookIndexVo.setIsVip(book.getIsVip()); bookIndexVo.setPrice(book.getPrice()); bookIndexVo.setSalePrice(book.getSalePrice()); saleList.add(bookIndexVo); } map.put("newBookList", list); map.put("topBookList", topList); map.put("saleList", saleList); return R.ok().put("bookIndex", map); } //精选 @RequestMapping("/getBestBook") public R getBestBook(@RequestParam Map params) { PageUtils page = bookService.getBestBook(params); return R.ok().put("page", page); } /** * 获取我的已购图书 * @param userId * @param limit * @param page * @return */ @RequestMapping("/getMyBooks") public R getMyBooks(@RequestParam Integer userId,@RequestParam Integer limit,@RequestParam Integer page){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(UserEbookBuyEntity::getUserId,userId); wrapper.groupBy(UserEbookBuyEntity::getBookId); List bookIds = userEbookBuyService.getBaseMapper().selectList(wrapper).stream().map(UserEbookBuyEntity::getBookId).collect(Collectors.toList()); LambdaQueryWrapper wrapper1 = new LambdaQueryWrapper<>(); wrapper1.eq(BookEntity::getDelFlag,0); if(bookIds.size()>0){ wrapper1.in(BookEntity::getId,bookIds); } Page bookEntityPage = bookService.getBaseMapper().selectPage(new Page<>(page, limit), wrapper1); return R.ok().put("page",bookEntityPage); } /** * 获取我的推荐图书 * @param userId * @param limit * @param page * @return */ @RequestMapping("/getBestBooks") public R getBestBooks(@RequestParam Integer userId,@RequestParam Integer limit,@RequestParam Integer page){ LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(UserEbookBuyEntity::getUserId,userId); wrapper.groupBy(UserEbookBuyEntity::getBookId); List bookIds = userEbookBuyService.getBaseMapper().selectList(wrapper).stream().map(UserEbookBuyEntity::getBookId).collect(Collectors.toList()); LambdaQueryWrapper wrapper1 = new LambdaQueryWrapper<>(); wrapper1.eq(BookEntity::getDelFlag,0); wrapper1.eq(BookEntity::getState,1); if(bookIds.size()>0){ wrapper1.notIn(BookEntity::getId,bookIds); } Page bookEntityPage = bookService.getBaseMapper().selectPage(new Page<>(page, limit), wrapper1); for (BookEntity b : bookEntityPage.getRecords()){ b.setProductId(shopProductBookService.getProductByBookId(b.getId())); } return R.ok().put("page",bookEntityPage); } //新书 @RequestMapping("/getNewBook") public R getNewBook(@RequestParam Map params) { PageUtils page = bookService.getNewBook(params); return R.ok().put("page", page); } //特价 @RequestMapping("/getSaleBook") public R getSaleBook(@RequestParam Map params) { PageUtils page = bookService.getSaleBook(params); return R.ok().put("page", page); } //生成电子书 目录 @RequestMapping("/getWordChapter") public R getWordChapter(@RequestParam Map params) { bookService.getWordChapter(14); return R.ok(); } @RequestMapping("/file") public String getFile() { String filePath = "C:\\Users\\Administrator\\IdeaProjects\\peanut_book\\2020年8月中华人民共和国县以上行政区划代码.json"; List provinceList = ReadProvinceUtil.getFile(filePath); // System.out.println(provinceList); if (provinceList != null && provinceList.size() > 0) { for (Province province : provinceList) { // ProvinceEntity provinceEntity = new ProvinceEntity(); // provinceEntity.setProvName(province.getProvName()); // provinceEntity.setCreateDate(province.getCreateDate()); // provinceEntity.setRegionCode(province.getRegionCode()); provinceService.save(province); List cityList = province.getCityList(); if (cityList != null && cityList.size() > 0) { for (City city : cityList) { // CityEntity cityEntity = new CityEntity(); // cityEntity.setCreateDate(city.getCreateDate()); // cityEntity.setCityName(city.getCityName()); // cityEntity.setRegionCode(city.getRegionCode()); // city.setProvId(province.getProvId()); city.setProvId(province.getProvId()); cityService.save(city); List countyList = city.getCountyList(); if (countyList != null && countyList.size() > 0) { for (County county : countyList) { // CountyEntity countyEntity = new CountyEntity(); county.setCityId(city.getCityId()); // countyEntity.setCountyName(county.getCountyName()); // countyEntity.setCreateDate(county.getCreateDate()); // countyEntity.setRegionCode(county.getRegionCode()); countyService.save(county); } } } } } } return null; } }