diff --git a/tamguo-mms/src/main/java/com/tamguo/web/BookController.java b/tamguo-mms/src/main/java/com/tamguo/web/BookController.java index 36c3774..70da6e0 100644 --- a/tamguo-mms/src/main/java/com/tamguo/web/BookController.java +++ b/tamguo-mms/src/main/java/com/tamguo/web/BookController.java @@ -28,105 +28,145 @@ import com.tamguo.modules.book.service.IBookService; import com.tamguo.modules.book.service.IDocumentService; import com.tamguo.utils.ShiroUtils; +/** + * BookController 类,处理图书相关的请求 + */ @Controller public class BookController { - - private Logger logger = LoggerFactory.getLogger(getClass()); + + private Logger logger = LoggerFactory.getLogger(getClass()); // 创建日志记录器对象 @Autowired - private IBookService iBookService; + private IBookService iBookService; // 自动注入图书服务 @Autowired - private IDocumentService iDocumentService; - + private IDocumentService iDocumentService; // 自动注入文档服务 + + /** + * 处理编辑图书的请求 + * @param bookId 图书 ID + * @param model 模型视图对象 + * @return 模型视图对象 + */ @RequestMapping(value = "editBook/{bookId}", method = RequestMethod.GET) - public ModelAndView edit(@PathVariable String bookId , ModelAndView model) { - model.setViewName("book/edit"); - model.addObject("bookId", bookId); - return model; + public ModelAndView edit(@PathVariable String bookId, ModelAndView model) { + model.setViewName("book/edit"); // 设置视图名称 + model.addObject("bookId", bookId); // 添加图书 ID 到模型中 + return model; // 返回模型视图对象 } + /** + * 处理图书列表的请求 + * @param model 模型视图对象 + * @return 模型视图对象 + */ @RequestMapping(value = {"booklist.html"}, method = RequestMethod.GET) public ModelAndView list(ModelAndView model) { - model.setViewName("booklist"); - return model; + model.setViewName("booklist"); // 设置视图名称 + return model; // 返回模型视图对象 } + /** + * 处理获取文档列表的请求 + * @param id 图书 ID + * @return 返回处理结果 + */ @SuppressWarnings("unchecked") @RequestMapping(value = "getDocumentList", method = RequestMethod.POST) @ResponseBody public Result getDocumentList(String id) { - Map map = new HashMap<>(); + Map map = new HashMap<>(); // 创建一个 Map 对象 try { - BookEntity book = iBookService.selectById(id); - List documentList = iDocumentService.selectList(Condition.create().eq("book_id", id).eq("status", DocumentStatusEnum.NORMAL.getValue())); - - map.put("documentList", this.processDocumentList(documentList)); - map.put("book", book); + BookEntity book = iBookService.selectById(id); // 根据图书 ID 获取图书信息 + List documentList = iDocumentService.selectList(Condition.create().eq("book_id", id).eq("status", DocumentStatusEnum.NORMAL.getValue())); // 根据图书 ID 和文档状态获取文档列表 + + map.put("documentList", this.processDocumentList(documentList)); // 处理文档列表并将结果放入 Map 中 + map.put("book", book); // 将图书信息放入 Map 中 } catch (Exception e) { - logger.error(e.getMessage() , e ); - return Result.failResult("查询失败"); + logger.error(e.getMessage(), e); // 记录错误日志 + return Result.failResult("查询失败"); // 返回查询失败的结果 } - return Result.successResult(map); + return Result.successResult(map); // 返回查询成功的结果 } - + + /** + * 处理获取图书列表的请求 + * @return 返回处理结果 + */ @SuppressWarnings("unchecked") @RequestMapping(value = {"getBookList.html"}, method = RequestMethod.POST) @ResponseBody public Result getBookList() { try { - List bookList = iBookService.selectList(Condition.create().eq("owner", ShiroUtils.getMemberId()).orderDesc(Arrays.asList("create_date"))); - return Result.successResult(bookList); + List bookList = iBookService.selectList(Condition.create().eq("owner", ShiroUtils.getMemberId()).orderByDesc(Arrays.asList("create_date"))); // 根据当前用户 ID 获取图书列表 + return Result.successResult(bookList); // 返回图书列表 } catch (Exception e) { - logger.error(e.getMessage() , e); - return Result.failResult("查询失败!"); + logger.error(e.getMessage(), e); // 记录错误日志 + return Result.failResult("查询失败!"); // 返回查询失败的结果 } } - + + /** + * 处理保存图书的请求 + * @param book 图书实体 + * @return 返回处理结果 + */ @RequestMapping(value = {"saveBook"}, method = RequestMethod.POST) @ResponseBody public Result saveBook(@RequestBody BookEntity book) { try { - book.setOwner(ShiroUtils.getMemberId()); - book.setCategoryId(StringUtils.join(book.getCategoryIds(), ",")); - iBookService.saveBook(book); - return Result.result(0, null, "保存成功"); + book.setOwner(ShiroUtils.getMemberId()); // 设置图书的所有者为当前用户 + book.setCategoryId(StringUtils.join(book.getCategoryIds(), ",")); // 设置图书的分类 ID 为逗号分隔的字符串 + iBookService.saveBook(book); // 保存图书 + return Result.result(0, null, "保存成功"); // 返回保存成功的结果 } catch (Exception e) { - logger.error(e.getMessage() , e); - return Result.result(1, null, "保存失败"); + logger.error(e.getMessage(), e); // 记录错误日志 + return Result.result(1, null, "保存失败"); // 返回保存失败的结果 } } - + + /** + * 处理文档列表,生成 JSON 数组 + * @param documentList 文档列表 + * @return 返回处理后的 JSON 数组 + */ private JSONArray processDocumentList(List documentList) { - JSONArray entitys = new JSONArray(); - for(int i=0 ; i