package com.example.springbook.controller;
import com.example.springbook.constants.Constants;
import com.example.springbook.model.*;
import com.example.springbook.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.util.List;
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
/**
* 添加书籍
*
* @param bookInfo
* @return
*/
@RequestMapping(value = "/addBook", produces = "application/json")
public String addBook(BookInfo bookInfo) {
log.info("接收到添加图书请求, bookInfo:{}", bookInfo);
//参数校验
if (!StringUtils.hasLength(bookInfo.getBookName())
|| !StringUtils.hasLength(bookInfo.getAuthor())
|| bookInfo.getCount() < 0
|| bookInfo.getPrice() == null
|| !StringUtils.hasLength(bookInfo.getPublish())) {
return "参数校验失败, 请检查入参";
}
Integer result = bookService.addBook(bookInfo);
if (result <= 0) {
log.error("添加图书出错:bookInfo:{}", bookInfo);
return "添加图书出错, 请联系管理人";
}
return "";
}
/**
* 通过页数获取数据
*
* @param pageRequest
* @param session
* @return
*/
@RequestMapping("/getBookListByPage")
public Result getBookListByPage(PageRequest pageRequest, HttpSession session) {
// log.info("获取图书列表, pageRequest:{}", pageRequest);
// //校验成功
// if (pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
// return Result.fail("参数校验失败");
// }
// PageResult<BookInfo> bookInfoPageResult = null;
// try {
// bookInfoPageResult = bookService.getBookListByPage(pageRequest);
// return Result.success(bookInfoPageResult);
// } catch (Exception e) {
// log.error("查询翻页信息错误,e:{}", e);
// return Result.fail(e.getMessage());
// }
log.info("获取图书列表,pageRequest:{}", pageRequest);
//判断用户是否成功登陆
if (session.getAttribute("session_user_key") == null) {
return Result.unlogin();
}
UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
if (userInfo == null || userInfo.getId() < 0 || "".equals(userInfo.getUserName())) {
return Result.unlogin();
}
//用户登录后返回图书列表
PageResult<BookInfo> pageResult = bookService.getBookListByPage(pageRequest);
return Result.success(pageResult);
}
/**
* 通过ID查询书籍
*
* @param bookId
* @return
*/
@RequestMapping("/queryBookInfoById")
public BookInfo queryBookInfoById(Integer bookId){
// long start = System.currentTimeMillis();
log.info("根据ID查询图书, bookId:"+bookId);
BookInfo bookInfo = null;
try {
bookInfo = bookService.queryBookById(bookId);
}catch (Exception e){
log.error("查询图书失败, e:{}",e);
}
// long end = System.currentTimeMillis();
// log.info("queryBookInfoById 执行耗时: "+ (end-start) + "ms");
return bookInfo;
}
/**
* 修改书籍信息
* @param bookInfo
* @return
*/
@RequestMapping(value = "/updateBook", produces = "application/json")
public String updateBook(BookInfo bookInfo) {
log.info("修改图书:{}", bookInfo);
try {
bookService.updateBook(bookInfo);
return "";
} catch (Exception e) {
log.error("修改图书失败", e);
return e.getMessage();
}
}
/**
* 批量删除图书
* @param ids
* @return
*/
@RequestMapping(value = "/batchDelete", produces = "application/json")
public String batchDelete(@RequestParam List<Integer> ids){
log.info("接收请求, 批量删除图书, 图书ID:{}",ids);
Integer result = bookService.batchDeleteBook(ids);
if (result<=0){
log.error("批量删除失败, ids:{}",ids);
return "批量删除失败, 请联系管理员";
}
return "";
}
}